blob: 5e5435671937726819002449c5a0d73f927c2d27 [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"
Tim Peters6d6c1a32001-08-02 04:15:00 +000032#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000033
Guido van Rossumc6004111993-11-05 10:22:19 +000034#include <ctype.h>
35
Guido van Rossum408027e1996-12-30 16:17:54 +000036#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000037/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000038#define LLTRACE 1 /* Low-level trace feature */
39#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000040#endif
41
Victor Stinner5c75f372019-04-17 23:02:26 +020042#if !defined(Py_BUILD_CORE)
43# error "ceval.c must be build with Py_BUILD_CORE define for best performance"
44#endif
45
Hai Shi46874c22020-01-30 17:20:25 -060046_Py_IDENTIFIER(__name__);
Guido van Rossum5b722181993-03-30 17:46:03 +000047
Guido van Rossum374a9221991-04-04 10:40:29 +000048/* Forward declarations */
Victor Stinner09532fe2019-05-10 23:39:09 +020049Py_LOCAL_INLINE(PyObject *) call_function(
50 PyThreadState *tstate, PyObject ***pp_stack,
51 Py_ssize_t oparg, PyObject *kwnames);
52static PyObject * do_call_core(
53 PyThreadState *tstate, PyObject *func,
54 PyObject *callargs, PyObject *kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +000055
Guido van Rossum0a066c01992-03-27 17:29:15 +000056#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000057static int lltrace;
Victor Stinner438a12d2019-05-24 17:01:38 +020058static int prtrace(PyThreadState *, PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000059#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010060static int call_trace(Py_tracefunc, PyObject *,
61 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000062 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000063static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010064 PyThreadState *, PyFrameObject *,
65 int, PyObject *);
66static void call_exc_trace(Py_tracefunc, PyObject *,
67 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000068static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Eric Snow2ebc5ce2017-09-07 23:51:28 -060069 PyThreadState *, PyFrameObject *,
70 int *, int *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070071static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *);
72static void dtrace_function_entry(PyFrameObject *);
73static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000074
Victor Stinner438a12d2019-05-24 17:01:38 +020075static PyObject * import_name(PyThreadState *, PyFrameObject *,
76 PyObject *, PyObject *, PyObject *);
77static PyObject * import_from(PyThreadState *, PyObject *, PyObject *);
78static int import_all_from(PyThreadState *, PyObject *, PyObject *);
79static void format_exc_check_arg(PyThreadState *, PyObject *, const char *, PyObject *);
80static void format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg);
81static PyObject * unicode_concatenate(PyThreadState *, PyObject *, PyObject *,
Serhiy Storchakaab874002016-09-11 13:48:15 +030082 PyFrameObject *, const _Py_CODEUNIT *);
Victor Stinner438a12d2019-05-24 17:01:38 +020083static PyObject * special_lookup(PyThreadState *, PyObject *, _Py_Identifier *);
84static int check_args_iterable(PyThreadState *, PyObject *func, PyObject *vararg);
85static void format_kwargs_error(PyThreadState *, PyObject *func, PyObject *kwargs);
Mark Shannonfee55262019-11-21 09:11:43 +000086static void format_awaitable_error(PyThreadState *, PyTypeObject *, int, int);
Guido van Rossum374a9221991-04-04 10:40:29 +000087
Paul Prescode68140d2000-08-30 20:25:01 +000088#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000090#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000092#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 "free variable '%.200s' referenced before assignment" \
94 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000095
Guido van Rossum950361c1997-01-24 13:49:28 +000096/* Dynamic execution profile */
97#ifdef DYNAMIC_EXECUTION_PROFILE
98#ifdef DXPAIRS
99static long dxpairs[257][256];
100#define dxp dxpairs[256]
101#else
102static long dxp[256];
103#endif
104#endif
105
Inada Naoki91234a12019-06-03 21:30:58 +0900106/* per opcode cache */
Inada Naokieddef862019-06-04 07:38:10 +0900107#ifdef Py_DEBUG
108// --with-pydebug is used to find memory leak. opcache makes it harder.
109// So we disable opcache when Py_DEBUG is defined.
110// See bpo-37146
111#define OPCACHE_MIN_RUNS 0 /* disable opcache */
112#else
Inada Naoki91234a12019-06-03 21:30:58 +0900113#define OPCACHE_MIN_RUNS 1024 /* create opcache when code executed this time */
Inada Naokieddef862019-06-04 07:38:10 +0900114#endif
Inada Naoki91234a12019-06-03 21:30:58 +0900115#define OPCACHE_STATS 0 /* Enable stats */
116
117#if OPCACHE_STATS
118static size_t opcache_code_objects = 0;
119static size_t opcache_code_objects_extra_mem = 0;
120
121static size_t opcache_global_opts = 0;
122static size_t opcache_global_hits = 0;
123static size_t opcache_global_misses = 0;
124#endif
125
Victor Stinner5a3a71d2020-03-19 17:40:12 +0100126
Victor Stinnerda2914d2020-03-20 09:29:08 +0100127#ifndef NDEBUG
128/* Ensure that tstate is valid: sanity check for PyEval_AcquireThread() and
129 PyEval_RestoreThread(). Detect if tstate memory was freed. It can happen
130 when a thread continues to run after Python finalization, especially
131 daemon threads. */
132static int
133is_tstate_valid(PyThreadState *tstate)
134{
135 assert(!_PyMem_IsPtrFreed(tstate));
136 assert(!_PyMem_IsPtrFreed(tstate->interp));
137 return 1;
138}
139#endif
140
141
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000142/* This can set eval_breaker to 0 even though gil_drop_request became
143 1. We believe this is all right because the eval loop will release
144 the GIL eventually anyway. */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100145static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200146COMPUTE_EVAL_BREAKER(PyInterpreterState *interp,
Victor Stinnerda2914d2020-03-20 09:29:08 +0100147 struct _ceval_runtime_state *ceval,
148 struct _ceval_state *ceval2)
149{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100150 _Py_atomic_store_relaxed(&ceval2->eval_breaker,
151 _Py_atomic_load_relaxed(&ceval->gil_drop_request)
152 | (_Py_atomic_load_relaxed(&ceval->signals_pending)
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200153 && _Py_ThreadCanHandleSignals(interp))
Victor Stinnerd8316882020-03-20 14:50:35 +0100154 | (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)
155 && _Py_ThreadCanHandlePendingCalls())
Victor Stinnerda2914d2020-03-20 09:29:08 +0100156 | ceval2->pending.async_exc);
157}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000158
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000159
Victor Stinnerda2914d2020-03-20 09:29:08 +0100160static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200161SET_GIL_DROP_REQUEST(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100162{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200163 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
164 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100165 _Py_atomic_store_relaxed(&ceval->gil_drop_request, 1);
166 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
167}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000168
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000169
Victor Stinnerda2914d2020-03-20 09:29:08 +0100170static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200171RESET_GIL_DROP_REQUEST(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100172{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200173 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
174 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100175 _Py_atomic_store_relaxed(&ceval->gil_drop_request, 0);
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200176 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100177}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000178
Eric Snowfdf282d2019-01-11 14:26:55 -0700179
Victor Stinnerda2914d2020-03-20 09:29:08 +0100180static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200181SIGNAL_PENDING_CALLS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100182{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200183 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
184 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100185 _Py_atomic_store_relaxed(&ceval2->pending.calls_to_do, 1);
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200186 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100187}
Eric Snowfdf282d2019-01-11 14:26:55 -0700188
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000189
Victor Stinnerda2914d2020-03-20 09:29:08 +0100190static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200191UNSIGNAL_PENDING_CALLS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100192{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200193 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
194 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100195 _Py_atomic_store_relaxed(&ceval2->pending.calls_to_do, 0);
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200196 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100197}
198
199
200static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200201SIGNAL_PENDING_SIGNALS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100202{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200203 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
204 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100205 _Py_atomic_store_relaxed(&ceval->signals_pending, 1);
206 /* eval_breaker is not set to 1 if thread_can_handle_signals() is false */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200207 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100208}
209
210
211static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200212UNSIGNAL_PENDING_SIGNALS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100213{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200214 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
215 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100216 _Py_atomic_store_relaxed(&ceval->signals_pending, 0);
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200217 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100218}
219
220
221static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200222SIGNAL_ASYNC_EXC(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100223{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200224 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100225 ceval2->pending.async_exc = 1;
226 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
227}
228
229
230static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200231UNSIGNAL_ASYNC_EXC(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100232{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200233 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
234 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100235 ceval2->pending.async_exc = 0;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200236 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100237}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000238
239
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000240#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000241#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000242#endif
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000243#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000244
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100245static void
246ensure_tstate_not_null(const char *func, PyThreadState *tstate)
247{
248 if (tstate == NULL) {
Victor Stinner23ef89d2020-03-18 02:26:04 +0100249 _Py_FatalErrorFunc(func,
250 "current thread state is NULL (released GIL?)");
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100251 }
252}
253
254
Tim Peters7f468f22004-10-11 02:40:51 +0000255int
Victor Stinner175a7042020-03-10 00:37:48 +0100256_PyEval_ThreadsInitialized(_PyRuntimeState *runtime)
257{
258 return gil_created(&runtime->ceval.gil);
259}
260
261int
Tim Peters7f468f22004-10-11 02:40:51 +0000262PyEval_ThreadsInitialized(void)
263{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100264 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner175a7042020-03-10 00:37:48 +0100265 return _PyEval_ThreadsInitialized(runtime);
Tim Peters7f468f22004-10-11 02:40:51 +0000266}
267
Victor Stinner111e4ee2020-03-09 21:24:14 +0100268PyStatus
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200269_PyEval_InitGIL(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000270{
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200271 if (!_Py_IsMainInterpreter(tstate)) {
272 /* Currently, the GIL is shared by all interpreters,
273 and only the main interpreter is responsible to create
274 and destroy it. */
275 return _PyStatus_OK();
Victor Stinner111e4ee2020-03-09 21:24:14 +0100276 }
277
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200278 struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil;
279 assert(!gil_created(gil));
Victor Stinner85f5a692020-03-09 22:12:04 +0100280
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200281 PyThread_init_thread();
282 create_gil(gil);
283
284 take_gil(tstate);
285
286 assert(gil_created(gil));
Victor Stinner111e4ee2020-03-09 21:24:14 +0100287 return _PyStatus_OK();
288}
289
290void
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200291_PyEval_FiniGIL(PyThreadState *tstate)
292{
293 if (!_Py_IsMainInterpreter(tstate)) {
294 /* Currently, the GIL is shared by all interpreters,
295 and only the main interpreter is responsible to create
296 and destroy it. */
297 return;
298 }
299
300 struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil;
301 if (!gil_created(gil)) {
302 /* First Py_InitializeFromConfig() call: the GIL doesn't exist
303 yet: do nothing. */
304 return;
305 }
306
307 destroy_gil(gil);
308 assert(!gil_created(gil));
309}
310
311void
Victor Stinner111e4ee2020-03-09 21:24:14 +0100312PyEval_InitThreads(void)
313{
Victor Stinnerb4698ec2020-03-10 01:28:54 +0100314 /* Do nothing: kept for backward compatibility */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000315}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000316
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000317void
Inada Naoki91234a12019-06-03 21:30:58 +0900318_PyEval_Fini(void)
319{
320#if OPCACHE_STATS
321 fprintf(stderr, "-- Opcode cache number of objects = %zd\n",
322 opcache_code_objects);
323
324 fprintf(stderr, "-- Opcode cache total extra mem = %zd\n",
325 opcache_code_objects_extra_mem);
326
327 fprintf(stderr, "\n");
328
329 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL hits = %zd (%d%%)\n",
330 opcache_global_hits,
331 (int) (100.0 * opcache_global_hits /
332 (opcache_global_hits + opcache_global_misses)));
333
334 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL misses = %zd (%d%%)\n",
335 opcache_global_misses,
336 (int) (100.0 * opcache_global_misses /
337 (opcache_global_hits + opcache_global_misses)));
338
339 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL opts = %zd\n",
340 opcache_global_opts);
341
342 fprintf(stderr, "\n");
343#endif
344}
345
346void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000347PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000348{
Victor Stinner09532fe2019-05-10 23:39:09 +0200349 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200350 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100351 ensure_tstate_not_null(__func__, tstate);
352
Victor Stinner85f5a692020-03-09 22:12:04 +0100353 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000354}
355
356void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000357PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000358{
Victor Stinner09532fe2019-05-10 23:39:09 +0200359 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200360 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100362 We therefore avoid PyThreadState_Get() which dumps a fatal error
Victor Stinnerda2914d2020-03-20 09:29:08 +0100363 in debug mode. */
364 drop_gil(&runtime->ceval, 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 Stinnerda2914d2020-03-20 09:29:08 +0100371 drop_gil(ceval, tstate);
Victor Stinner23ef89d2020-03-18 02:26:04 +0100372}
373
374void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000375PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000376{
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100377 ensure_tstate_not_null(__func__, tstate);
378
Victor Stinner85f5a692020-03-09 22:12:04 +0100379 take_gil(tstate);
Victor Stinnere225beb2019-06-03 18:14:24 +0200380
Victor Stinner85f5a692020-03-09 22:12:04 +0100381 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
382 if (_PyThreadState_Swap(gilstate, tstate) != NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100383 Py_FatalError("non-NULL old thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200384 }
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000385}
386
387void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000388PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000389{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100390 assert(is_tstate_valid(tstate));
Victor Stinner09532fe2019-05-10 23:39:09 +0200391
Victor Stinner01b1cc12019-11-20 02:27:56 +0100392 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200393 PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
394 if (new_tstate != tstate) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100395 Py_FatalError("wrong thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200396 }
Victor Stinnerda2914d2020-03-20 09:29:08 +0100397 drop_gil(&runtime->ceval, tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000398}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000399
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900400#ifdef HAVE_FORK
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200401/* This function is called from PyOS_AfterFork_Child to destroy all threads
402 * which are not running in the child process, and clear internal locks
403 * which might be held by those threads.
404 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000405
406void
Victor Stinnerd5d9e812019-05-13 12:35:37 +0200407_PyEval_ReInitThreads(_PyRuntimeState *runtime)
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000408{
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100409 struct _gil_runtime_state *gil = &runtime->ceval.gil;
410 if (!gil_created(gil)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 return;
Victor Stinner09532fe2019-05-10 23:39:09 +0200412 }
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100413 recreate_gil(gil);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100414 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
415 ensure_tstate_not_null(__func__, tstate);
Victor Stinner85f5a692020-03-09 22:12:04 +0100416
417 take_gil(tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700418
Victor Stinner50e6e992020-03-19 02:41:21 +0100419 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900420 if (_PyThread_at_fork_reinit(&pending->lock) < 0) {
Eric Snow8479a342019-03-08 23:44:33 -0700421 Py_FatalError("Can't initialize threads for pending calls");
422 }
Jesse Nollera8513972008-07-17 16:49:17 +0000423
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200424 /* Destroy all threads except the current one */
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100425 _PyThreadState_DeleteExcept(runtime, tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000426}
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900427#endif
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000428
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000429/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600430 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000431
432void
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100433_PyEval_SignalAsyncExc(PyThreadState *tstate)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000434{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200435 assert(is_tstate_valid(tstate));
436 SIGNAL_ASYNC_EXC(tstate->interp);
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000437}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000438
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000439PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000440PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000441{
Victor Stinner09532fe2019-05-10 23:39:09 +0200442 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200443 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100444
Victor Stinner09532fe2019-05-10 23:39:09 +0200445 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100446 ensure_tstate_not_null(__func__, tstate);
447
Victor Stinnere225beb2019-06-03 18:14:24 +0200448 assert(gil_created(&ceval->gil));
Victor Stinnerda2914d2020-03-20 09:29:08 +0100449 drop_gil(ceval, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000451}
452
453void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000454PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000455{
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100456 ensure_tstate_not_null(__func__, tstate);
457
Victor Stinner85f5a692020-03-09 22:12:04 +0100458 take_gil(tstate);
Victor Stinner17c68b82020-01-30 12:20:48 +0100459
Victor Stinner85f5a692020-03-09 22:12:04 +0100460 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
461 _PyThreadState_Swap(gilstate, tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000462}
463
464
Guido van Rossuma9672091994-09-14 13:31:22 +0000465/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
466 signal handlers or Mac I/O completion routines) can schedule calls
467 to a function to be called synchronously.
468 The synchronous function is called with one void* argument.
469 It should return 0 for success or -1 for failure -- failure should
470 be accompanied by an exception.
471
472 If registry succeeds, the registry function returns 0; if it fails
473 (e.g. due to too many pending calls) it returns -1 (without setting
474 an exception condition).
475
476 Note that because registry may occur from within signal handlers,
477 or other asynchronous events, calling malloc() is unsafe!
478
Guido van Rossuma9672091994-09-14 13:31:22 +0000479 Any thread can schedule pending calls, but only the main thread
480 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000481 There is no facility to schedule calls to a particular thread, but
482 that should be easy to change, should that ever be required. In
483 that case, the static variables here should go into the python
484 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000485*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000486
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200487void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200488_PyEval_SignalReceived(PyInterpreterState *interp)
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200489{
490 /* bpo-30703: Function called when the C signal handler of Python gets a
Victor Stinner50e6e992020-03-19 02:41:21 +0100491 signal. We cannot queue a callback using _PyEval_AddPendingCall() since
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200492 that function is not async-signal-safe. */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200493 SIGNAL_PENDING_SIGNALS(interp);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200494}
495
Eric Snow5be45a62019-03-08 22:47:07 -0700496/* Push one item onto the queue while holding the lock. */
497static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200498_push_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600499 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700500{
Eric Snow842a2f02019-03-15 15:47:51 -0600501 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700502 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600503 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700504 return -1; /* Queue full */
505 }
Eric Snow842a2f02019-03-15 15:47:51 -0600506 pending->calls[i].func = func;
507 pending->calls[i].arg = arg;
508 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700509 return 0;
510}
511
512/* Pop one item off the queue while holding the lock. */
513static void
Victor Stinnere225beb2019-06-03 18:14:24 +0200514_pop_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600515 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700516{
Eric Snow842a2f02019-03-15 15:47:51 -0600517 int i = pending->first;
518 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700519 return; /* Queue empty */
520 }
521
Eric Snow842a2f02019-03-15 15:47:51 -0600522 *func = pending->calls[i].func;
523 *arg = pending->calls[i].arg;
524 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700525}
526
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200527/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000528 scheduling to be made from any thread, and even from an executing
529 callback.
530 */
531
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000532int
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200533_PyEval_AddPendingCall(PyInterpreterState *interp,
Victor Stinner09532fe2019-05-10 23:39:09 +0200534 int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000535{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200536 struct _pending_calls *pending = &interp->ceval.pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600537
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200538 /* Ensure that _PyEval_InitPendingCalls() was called
539 and that _PyEval_FiniPendingCalls() is not called yet. */
540 assert(pending->lock != NULL);
541
Eric Snow842a2f02019-03-15 15:47:51 -0600542 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200543 int result = _push_pending_call(pending, func, arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600544 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700545
Victor Stinnere225beb2019-06-03 18:14:24 +0200546 /* signal main loop */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200547 SIGNAL_PENDING_CALLS(interp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000549}
550
Victor Stinner09532fe2019-05-10 23:39:09 +0200551int
552Py_AddPendingCall(int (*func)(void *), void *arg)
553{
Victor Stinner50e6e992020-03-19 02:41:21 +0100554 /* Best-effort to support subinterpreters and calls with the GIL released.
555
556 First attempt _PyThreadState_GET() since it supports subinterpreters.
557
558 If the GIL is released, _PyThreadState_GET() returns NULL . In this
559 case, use PyGILState_GetThisThreadState() which works even if the GIL
560 is released.
561
562 Sadly, PyGILState_GetThisThreadState() doesn't support subinterpreters:
563 see bpo-10915 and bpo-15751.
564
Victor Stinner8849e592020-03-18 19:28:53 +0100565 Py_AddPendingCall() doesn't require the caller to hold the GIL. */
Victor Stinner50e6e992020-03-19 02:41:21 +0100566 PyThreadState *tstate = _PyThreadState_GET();
567 if (tstate == NULL) {
568 tstate = PyGILState_GetThisThreadState();
569 }
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200570
571 PyInterpreterState *interp;
572 if (tstate != NULL) {
573 interp = tstate->interp;
574 }
575 else {
576 /* Last resort: use the main interpreter */
577 interp = _PyRuntime.interpreters.main;
578 }
579 return _PyEval_AddPendingCall(interp, func, arg);
Victor Stinner09532fe2019-05-10 23:39:09 +0200580}
581
Eric Snowfdf282d2019-01-11 14:26:55 -0700582static int
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100583handle_signals(PyThreadState *tstate)
Eric Snowfdf282d2019-01-11 14:26:55 -0700584{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200585 assert(is_tstate_valid(tstate));
586 if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
Eric Snow64d6cc82019-02-23 15:40:43 -0700587 return 0;
588 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700589
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200590 UNSIGNAL_PENDING_SIGNALS(tstate->interp);
Victor Stinner72818982020-03-26 22:28:11 +0100591 if (_PyErr_CheckSignalsTstate(tstate) < 0) {
592 /* On failure, re-schedule a call to handle_signals(). */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200593 SIGNAL_PENDING_SIGNALS(tstate->interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700594 return -1;
595 }
596 return 0;
597}
598
599static int
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100600make_pending_calls(PyThreadState *tstate)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000601{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200602 assert(is_tstate_valid(tstate));
603
Victor Stinnerd8316882020-03-20 14:50:35 +0100604 /* only execute pending calls on main thread */
605 if (!_Py_ThreadCanHandlePendingCalls()) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200606 return 0;
607 }
608
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000609 /* don't perform recursive pending calls */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100610 static int busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700611 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000612 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700613 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200614 busy = 1;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100615
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200616 /* unsignal before starting to call callbacks, so that any callback
617 added in-between re-signals */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200618 UNSIGNAL_PENDING_CALLS(tstate->interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700619 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200620
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 /* perform a bounded number of calls, in case of recursion */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100622 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Eric Snowfdf282d2019-01-11 14:26:55 -0700623 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700624 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 void *arg = NULL;
626
627 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600628 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200629 _pop_pending_call(pending, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600630 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700631
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100632 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700633 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100634 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700635 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700636 res = func(arg);
637 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200638 goto error;
639 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200641
Charles-François Natalif23339a2011-07-23 18:15:43 +0200642 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700643 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200644
645error:
646 busy = 0;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200647 SIGNAL_PENDING_CALLS(tstate->interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700648 return res;
649}
650
Eric Snow842a2f02019-03-15 15:47:51 -0600651void
Victor Stinner2b1df452020-01-13 18:46:59 +0100652_Py_FinishPendingCalls(PyThreadState *tstate)
Eric Snow842a2f02019-03-15 15:47:51 -0600653{
Eric Snow842a2f02019-03-15 15:47:51 -0600654 assert(PyGILState_Check());
655
Victor Stinner50e6e992020-03-19 02:41:21 +0100656 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200657
Eric Snow842a2f02019-03-15 15:47:51 -0600658 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
659 return;
660 }
661
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100662 if (make_pending_calls(tstate) < 0) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200663 PyObject *exc, *val, *tb;
664 _PyErr_Fetch(tstate, &exc, &val, &tb);
665 PyErr_BadInternalCall();
666 _PyErr_ChainExceptions(exc, val, tb);
667 _PyErr_Print(tstate);
Eric Snow842a2f02019-03-15 15:47:51 -0600668 }
669}
670
Eric Snowfdf282d2019-01-11 14:26:55 -0700671/* Py_MakePendingCalls() is a simple wrapper for the sake
672 of backward-compatibility. */
673int
674Py_MakePendingCalls(void)
675{
676 assert(PyGILState_Check());
677
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100678 PyThreadState *tstate = _PyThreadState_GET();
679
Eric Snowfdf282d2019-01-11 14:26:55 -0700680 /* Python signal handler doesn't really queue a callback: it only signals
681 that a signal was received, see _PyEval_SignalReceived(). */
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100682 int res = handle_signals(tstate);
Eric Snowfdf282d2019-01-11 14:26:55 -0700683 if (res != 0) {
684 return res;
685 }
686
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100687 res = make_pending_calls(tstate);
Eric Snowb75b1a352019-04-12 10:20:10 -0600688 if (res != 0) {
689 return res;
690 }
691
692 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000693}
694
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000695/* The interpreter's recursion limit */
696
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000697#ifndef Py_DEFAULT_RECURSION_LIMIT
698#define Py_DEFAULT_RECURSION_LIMIT 1000
699#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600700
Eric Snow05351c12017-09-05 21:43:08 -0700701int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000702
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600703void
Victor Stinnerdab84232020-03-17 18:56:44 +0100704_PyEval_InitRuntimeState(struct _ceval_runtime_state *ceval)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600705{
Victor Stinnerdab84232020-03-17 18:56:44 +0100706 ceval->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600707 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Victor Stinnerdab84232020-03-17 18:56:44 +0100708 _gil_initialize(&ceval->gil);
709}
710
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200711int
Victor Stinnerdab84232020-03-17 18:56:44 +0100712_PyEval_InitState(struct _ceval_state *ceval)
713{
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200714 struct _pending_calls *pending = &ceval->pending;
715 assert(pending->lock == NULL);
716
717 pending->lock = PyThread_allocate_lock();
718 if (pending->lock == NULL) {
719 return -1;
720 }
721 return 0;
722}
723
724void
725_PyEval_FiniState(struct _ceval_state *ceval)
726{
727 struct _pending_calls *pending = &ceval->pending;
728 if (pending->lock != NULL) {
729 PyThread_free_lock(pending->lock);
730 pending->lock = NULL;
731 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600732}
733
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000734int
735Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000736{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100737 struct _ceval_runtime_state *ceval = &_PyRuntime.ceval;
738 return ceval->recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000739}
740
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000741void
742Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000743{
Victor Stinnere225beb2019-06-03 18:14:24 +0200744 struct _ceval_runtime_state *ceval = &_PyRuntime.ceval;
745 ceval->recursion_limit = new_limit;
Victor Stinnerdab84232020-03-17 18:56:44 +0100746 _Py_CheckRecursionLimit = new_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000747}
748
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100749/* The function _Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
Armin Rigo2b3eb402003-10-28 12:05:48 +0000750 if the recursion_depth reaches _Py_CheckRecursionLimit.
751 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
752 to guarantee that _Py_CheckRecursiveCall() is regularly called.
753 Without USE_STACKCHECK, there is no need for this. */
754int
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100755_Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000756{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100757 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200758 int recursion_limit = runtime->ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000759
760#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700761 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 if (PyOS_CheckStack()) {
763 --tstate->recursion_depth;
Victor Stinner438a12d2019-05-24 17:01:38 +0200764 _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 return -1;
766 }
pdox18967932017-10-25 23:03:01 -0700767 /* Needed for ABI backwards-compatibility (see bpo-31857) */
Eric Snow05351c12017-09-05 21:43:08 -0700768 _Py_CheckRecursionLimit = recursion_limit;
pdox18967932017-10-25 23:03:01 -0700769#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 if (tstate->recursion_critical)
771 /* Somebody asked that we don't check for recursion. */
772 return 0;
773 if (tstate->overflowed) {
774 if (tstate->recursion_depth > recursion_limit + 50) {
775 /* Overflowing while handling an overflow. Give up. */
776 Py_FatalError("Cannot recover from stack overflow.");
777 }
778 return 0;
779 }
780 if (tstate->recursion_depth > recursion_limit) {
781 --tstate->recursion_depth;
782 tstate->overflowed = 1;
Victor Stinner438a12d2019-05-24 17:01:38 +0200783 _PyErr_Format(tstate, PyExc_RecursionError,
784 "maximum recursion depth exceeded%s",
785 where);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 return -1;
787 }
788 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000789}
790
Victor Stinner09532fe2019-05-10 23:39:09 +0200791static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
Victor Stinner438a12d2019-05-24 17:01:38 +0200792static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000793
Victor Stinnere225beb2019-06-03 18:14:24 +0200794#define _Py_TracingPossible(ceval) ((ceval)->tracing_possible)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000795
Guido van Rossum374a9221991-04-04 10:40:29 +0000796
Guido van Rossumb209a111997-04-29 18:18:01 +0000797PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000798PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000799{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 return PyEval_EvalCodeEx(co,
801 globals, locals,
802 (PyObject **)NULL, 0,
803 (PyObject **)NULL, 0,
804 (PyObject **)NULL, 0,
805 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000806}
807
808
809/* Interpreter main loop */
810
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000811PyObject *
Victor Stinnerb9e68122019-11-14 12:20:46 +0100812PyEval_EvalFrame(PyFrameObject *f)
813{
Victor Stinner0b72b232020-03-12 23:18:39 +0100814 /* Function kept for backward compatibility */
Victor Stinnerb9e68122019-11-14 12:20:46 +0100815 PyThreadState *tstate = _PyThreadState_GET();
816 return _PyEval_EvalFrame(tstate, f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000817}
818
819PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000820PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000821{
Victor Stinnerb9e68122019-11-14 12:20:46 +0100822 PyThreadState *tstate = _PyThreadState_GET();
823 return _PyEval_EvalFrame(tstate, f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700824}
825
Victor Stinnerda2914d2020-03-20 09:29:08 +0100826
827/* Handle signals, pending calls, GIL drop request
828 and asynchronous exception */
829static int
830eval_frame_handle_pending(PyThreadState *tstate)
831{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100832 _PyRuntimeState * const runtime = &_PyRuntime;
833 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200834
835 /* Pending signals */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100836 if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
837 if (handle_signals(tstate) != 0) {
838 return -1;
839 }
840 }
841
842 /* Pending calls */
843 struct _ceval_state *ceval2 = &tstate->interp->ceval;
844 if (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)) {
845 if (make_pending_calls(tstate) != 0) {
846 return -1;
847 }
848 }
849
850 /* GIL drop request */
851 if (_Py_atomic_load_relaxed(&ceval->gil_drop_request)) {
852 /* Give another thread a chance */
853 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
854 Py_FatalError("tstate mix-up");
855 }
856 drop_gil(ceval, tstate);
857
858 /* Other threads may run now */
859
860 take_gil(tstate);
861
862 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
863 Py_FatalError("orphan tstate");
864 }
865 }
866
867 /* Check for asynchronous exception. */
868 if (tstate->async_exc != NULL) {
869 PyObject *exc = tstate->async_exc;
870 tstate->async_exc = NULL;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200871 UNSIGNAL_ASYNC_EXC(tstate->interp);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100872 _PyErr_SetNone(tstate, exc);
873 Py_DECREF(exc);
874 return -1;
875 }
876
877 return 0;
878}
879
Victor Stinnerc6944e72016-11-11 02:13:35 +0100880PyObject* _Py_HOT_FUNCTION
Victor Stinner0b72b232020-03-12 23:18:39 +0100881_PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
Brett Cannon3cebf932016-09-05 15:33:46 -0700882{
Victor Stinner0b72b232020-03-12 23:18:39 +0100883 ensure_tstate_not_null(__func__, tstate);
884
Guido van Rossum950361c1997-01-24 13:49:28 +0000885#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000887#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200888 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300889 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200890 int opcode; /* Current opcode */
891 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200892 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 PyObject *retval = NULL; /* Return value */
Victor Stinnerdab84232020-03-17 18:56:44 +0100894 struct _ceval_state * const ceval2 = &tstate->interp->ceval;
Victor Stinner50e6e992020-03-19 02:41:21 +0100895 _Py_atomic_int * const eval_breaker = &ceval2->eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000899
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000901
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 is true when the line being executed has changed. The
903 initial values are such as to make this false the first
904 time it is tested. */
905 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000906
Serhiy Storchakaab874002016-09-11 13:48:15 +0300907 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 PyObject *names;
909 PyObject *consts;
Inada Naoki91234a12019-06-03 21:30:58 +0900910 _PyOpcache *co_opcache;
Guido van Rossum374a9221991-04-04 10:40:29 +0000911
Brett Cannon368b4b72012-04-02 12:17:59 -0400912#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200913 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400914#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200915
Antoine Pitroub52ec782009-01-25 16:34:23 +0000916/* Computed GOTOs, or
917 the-optimization-commonly-but-improperly-known-as-"threaded code"
918 using gcc's labels-as-values extension
919 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
920
921 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000923 combined with a lookup table of jump addresses. However, since the
924 indirect jump instruction is shared by all opcodes, the CPU will have a
925 hard time making the right prediction for where to jump next (actually,
926 it will be always wrong except in the uncommon case of a sequence of
927 several identical opcodes).
928
929 "Threaded code" in contrast, uses an explicit jump table and an explicit
930 indirect jump instruction at the end of each opcode. Since the jump
931 instruction is at a different address for each opcode, the CPU will make a
932 separate prediction for each of these instructions, which is equivalent to
933 predicting the second opcode of each opcode pair. These predictions have
934 a much better chance to turn out valid, especially in small bytecode loops.
935
936 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000938 and potentially many more instructions (depending on the pipeline width).
939 A correctly predicted branch, however, is nearly free.
940
941 At the time of this writing, the "threaded code" version is up to 15-20%
942 faster than the normal "switch" version, depending on the compiler and the
943 CPU architecture.
944
945 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
946 because it would render the measurements invalid.
947
948
949 NOTE: care must be taken that the compiler doesn't try to "optimize" the
950 indirect jumps by sharing them between all opcodes. Such optimizations
951 can be disabled on gcc by using the -fno-gcse flag (or possibly
952 -fno-crossjumping).
953*/
954
Antoine Pitrou042b1282010-08-13 21:15:58 +0000955#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000956#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000957#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000958#endif
959
Antoine Pitrou042b1282010-08-13 21:15:58 +0000960#ifdef HAVE_COMPUTED_GOTOS
961 #ifndef USE_COMPUTED_GOTOS
962 #define USE_COMPUTED_GOTOS 1
963 #endif
964#else
965 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
966 #error "Computed gotos are not supported on this compiler."
967 #endif
968 #undef USE_COMPUTED_GOTOS
969 #define USE_COMPUTED_GOTOS 0
970#endif
971
972#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000973/* Import the static jump table */
974#include "opcode_targets.h"
975
Antoine Pitroub52ec782009-01-25 16:34:23 +0000976#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -0700977 op: \
978 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000979
Antoine Pitroub52ec782009-01-25 16:34:23 +0000980#ifdef LLTRACE
981#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 { \
Victor Stinnerdab84232020-03-17 18:56:44 +0100983 if (!lltrace && !_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300985 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300986 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 } \
988 goto fast_next_opcode; \
989 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000990#else
991#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 { \
Victor Stinnerdab84232020-03-17 18:56:44 +0100993 if (!_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300995 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300996 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 } \
998 goto fast_next_opcode; \
999 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001000#endif
1001
Victor Stinner09532fe2019-05-10 23:39:09 +02001002#define DISPATCH() \
1003 { \
1004 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
1005 FAST_DISPATCH(); \
1006 } \
1007 continue; \
1008 }
1009
Antoine Pitroub52ec782009-01-25 16:34:23 +00001010#else
Benjamin Petersonddd19492018-09-16 22:38:02 -07001011#define TARGET(op) op
Antoine Pitroub52ec782009-01-25 16:34:23 +00001012#define FAST_DISPATCH() goto fast_next_opcode
Victor Stinner09532fe2019-05-10 23:39:09 +02001013#define DISPATCH() continue
Antoine Pitroub52ec782009-01-25 16:34:23 +00001014#endif
1015
1016
Neal Norwitza81d2202002-07-14 00:27:26 +00001017/* Tuple access macros */
1018
1019#ifndef Py_DEBUG
1020#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
1021#else
1022#define GETITEM(v, i) PyTuple_GetItem((v), (i))
1023#endif
1024
Guido van Rossum374a9221991-04-04 10:40:29 +00001025/* Code access macros */
1026
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001027/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001028#define INSTR_OFFSET() \
1029 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001030#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001031 _Py_CODEUNIT word = *next_instr; \
1032 opcode = _Py_OPCODE(word); \
1033 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001034 next_instr++; \
1035 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +03001036#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
1037#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +00001038
Raymond Hettingerf606f872003-03-16 03:11:04 +00001039/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 Some opcodes tend to come in pairs thus making it possible to
1041 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001042 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001043
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 Verifying the prediction costs a single high-speed test of a register
1045 variable against a constant. If the pairing was good, then the
1046 processor's own internal branch predication has a high likelihood of
1047 success, resulting in a nearly zero-overhead transition to the
1048 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001049 including its unpredictable switch-case branch. Combined with the
1050 processor's internal branch prediction, a successful PREDICT has the
1051 effect of making the two opcodes run as if they were a single new opcode
1052 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001053
Georg Brandl86b2fb92008-07-16 03:43:04 +00001054 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 predictions turned-on and interpret the results as if some opcodes
1056 had been combined or turn-off predictions so that the opcode frequency
1057 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001058
1059 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 the CPU to record separate branch prediction information for each
1061 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001062
Raymond Hettingerf606f872003-03-16 03:11:04 +00001063*/
1064
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001065#define PREDICT_ID(op) PRED_##op
1066
Antoine Pitrou042b1282010-08-13 21:15:58 +00001067#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001068#define PREDICT(op) if (0) goto PREDICT_ID(op)
Raymond Hettingera7216982004-02-08 19:59:27 +00001069#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001070#define PREDICT(op) \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001071 do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001072 _Py_CODEUNIT word = *next_instr; \
1073 opcode = _Py_OPCODE(word); \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001074 if (opcode == op) { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001075 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001076 next_instr++; \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001077 goto PREDICT_ID(op); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001078 } \
1079 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +00001080#endif
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001081#define PREDICTED(op) PREDICT_ID(op):
Antoine Pitroub52ec782009-01-25 16:34:23 +00001082
Raymond Hettingerf606f872003-03-16 03:11:04 +00001083
Guido van Rossum374a9221991-04-04 10:40:29 +00001084/* Stack manipulation macros */
1085
Martin v. Löwis18e16552006-02-15 17:27:45 +00001086/* The stack can grow at most MAXINT deep, as co_nlocals and
1087 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001088#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1089#define EMPTY() (STACK_LEVEL() == 0)
1090#define TOP() (stack_pointer[-1])
1091#define SECOND() (stack_pointer[-2])
1092#define THIRD() (stack_pointer[-3])
1093#define FOURTH() (stack_pointer[-4])
1094#define PEEK(n) (stack_pointer[-(n)])
1095#define SET_TOP(v) (stack_pointer[-1] = (v))
1096#define SET_SECOND(v) (stack_pointer[-2] = (v))
1097#define SET_THIRD(v) (stack_pointer[-3] = (v))
1098#define SET_FOURTH(v) (stack_pointer[-4] = (v))
1099#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
1100#define BASIC_STACKADJ(n) (stack_pointer += n)
1101#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1102#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001103
Guido van Rossum96a42c81992-01-12 02:29:51 +00001104#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001106 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001107 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +02001108#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001109 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +00001110#define STACK_GROW(n) do { \
1111 assert(n >= 0); \
1112 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001113 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001114 assert(STACK_LEVEL() <= co->co_stacksize); \
1115 } while (0)
1116#define STACK_SHRINK(n) do { \
1117 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +02001118 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001119 (void)(BASIC_STACKADJ(-n)); \
1120 assert(STACK_LEVEL() <= co->co_stacksize); \
1121 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +00001122#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +02001123 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001124 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001125#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001126#define PUSH(v) BASIC_PUSH(v)
1127#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +00001128#define STACK_GROW(n) BASIC_STACKADJ(n)
1129#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001130#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001131#endif
1132
Guido van Rossum681d79a1995-07-18 14:51:37 +00001133/* Local variable macros */
1134
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001136
1137/* The SETLOCAL() macro must not DECREF the local variable in-place and
1138 then store the new value; it must copy the old value to a temporary
1139 value, then store the new value, and then DECREF the temporary value.
1140 This is because it is possible that during the DECREF the frame is
1141 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1142 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001144 GETLOCAL(i) = value; \
1145 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001146
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001147
1148#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 while (STACK_LEVEL() > (b)->b_level) { \
1150 PyObject *v = POP(); \
1151 Py_XDECREF(v); \
1152 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001153
1154#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001155 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +01001157 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1159 while (STACK_LEVEL() > (b)->b_level + 3) { \
1160 value = POP(); \
1161 Py_XDECREF(value); \
1162 } \
Mark Shannonae3087c2017-10-22 22:41:51 +01001163 exc_info = tstate->exc_info; \
1164 type = exc_info->exc_type; \
1165 value = exc_info->exc_value; \
1166 traceback = exc_info->exc_traceback; \
1167 exc_info->exc_type = POP(); \
1168 exc_info->exc_value = POP(); \
1169 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001170 Py_XDECREF(type); \
1171 Py_XDECREF(value); \
1172 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001173 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001174
Inada Naoki91234a12019-06-03 21:30:58 +09001175 /* macros for opcode cache */
1176#define OPCACHE_CHECK() \
1177 do { \
1178 co_opcache = NULL; \
1179 if (co->co_opcache != NULL) { \
1180 unsigned char co_opt_offset = \
1181 co->co_opcache_map[next_instr - first_instr]; \
1182 if (co_opt_offset > 0) { \
1183 assert(co_opt_offset <= co->co_opcache_size); \
1184 co_opcache = &co->co_opcache[co_opt_offset - 1]; \
1185 assert(co_opcache != NULL); \
Inada Naoki91234a12019-06-03 21:30:58 +09001186 } \
1187 } \
1188 } while (0)
1189
1190#if OPCACHE_STATS
1191
1192#define OPCACHE_STAT_GLOBAL_HIT() \
1193 do { \
1194 if (co->co_opcache != NULL) opcache_global_hits++; \
1195 } while (0)
1196
1197#define OPCACHE_STAT_GLOBAL_MISS() \
1198 do { \
1199 if (co->co_opcache != NULL) opcache_global_misses++; \
1200 } while (0)
1201
1202#define OPCACHE_STAT_GLOBAL_OPT() \
1203 do { \
1204 if (co->co_opcache != NULL) opcache_global_opts++; \
1205 } while (0)
1206
1207#else /* OPCACHE_STATS */
1208
1209#define OPCACHE_STAT_GLOBAL_HIT()
1210#define OPCACHE_STAT_GLOBAL_MISS()
1211#define OPCACHE_STAT_GLOBAL_OPT()
1212
1213#endif
1214
Guido van Rossuma027efa1997-05-05 20:56:21 +00001215/* Start of code */
1216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 /* push frame */
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001218 if (_Py_EnterRecursiveCall(tstate, "")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001220 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001221
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 if (tstate->use_tracing) {
1225 if (tstate->c_tracefunc != NULL) {
1226 /* tstate->c_tracefunc, if defined, is a
1227 function that will be called on *every* entry
1228 to a code block. Its return value, if not
1229 None, is a function that will be called at
1230 the start of each executed line of code.
1231 (Actually, the function must return itself
1232 in order to continue tracing.) The trace
1233 functions are called with three arguments:
1234 a pointer to the current frame, a string
1235 indicating why the function is called, and
1236 an argument which depends on the situation.
1237 The global trace function is also called
1238 whenever an exception is detected. */
1239 if (call_trace_protected(tstate->c_tracefunc,
1240 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001241 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 /* Trace function raised an error */
1243 goto exit_eval_frame;
1244 }
1245 }
1246 if (tstate->c_profilefunc != NULL) {
1247 /* Similar for c_profilefunc, except it needn't
1248 return itself and isn't called for "line" events */
1249 if (call_trace_protected(tstate->c_profilefunc,
1250 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001251 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 /* Profile function raised an error */
1253 goto exit_eval_frame;
1254 }
1255 }
1256 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001257
Łukasz Langaa785c872016-09-09 17:37:37 -07001258 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1259 dtrace_function_entry(f);
1260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 co = f->f_code;
1262 names = co->co_names;
1263 consts = co->co_consts;
1264 fastlocals = f->f_localsplus;
1265 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001266 assert(PyBytes_Check(co->co_code));
1267 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001268 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1269 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1270 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001271 /*
1272 f->f_lasti refers to the index of the last instruction,
1273 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001274
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001275 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001276 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 When the PREDICT() macros are enabled, some opcode pairs follow in
1279 direct succession without updating f->f_lasti. A successful
1280 prediction effectively links the two codes together as if they
1281 were a single new opcode; accordingly,f->f_lasti will point to
1282 the first code in the pair (for instance, GET_ITER followed by
1283 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001284 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001286 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001287 next_instr = first_instr;
1288 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001289 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1290 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001291 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 stack_pointer = f->f_stacktop;
1293 assert(stack_pointer != NULL);
1294 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001295 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001296
Inada Naoki91234a12019-06-03 21:30:58 +09001297 if (co->co_opcache_flag < OPCACHE_MIN_RUNS) {
1298 co->co_opcache_flag++;
1299 if (co->co_opcache_flag == OPCACHE_MIN_RUNS) {
1300 if (_PyCode_InitOpcache(co) < 0) {
1301 return NULL;
1302 }
1303#if OPCACHE_STATS
1304 opcache_code_objects_extra_mem +=
1305 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1306 sizeof(_PyOpcache) * co->co_opcache_size;
1307 opcache_code_objects++;
1308#endif
1309 }
1310 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001311
Tim Peters5ca576e2001-06-18 22:08:13 +00001312#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001313 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001314#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001315
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001316 if (throwflag) /* support for generator.throw() */
1317 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001318
Victor Stinnerace47d72013-07-18 01:41:08 +02001319#ifdef Py_DEBUG
Victor Stinner0b72b232020-03-12 23:18:39 +01001320 /* _PyEval_EvalFrameDefault() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001321 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001322 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001323 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001324#endif
1325
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001326main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1329 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001330 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 /* Do periodic things. Doing this every time through
1333 the loop would add too much overhead, so we do it
1334 only every Nth instruction. We also do it if
1335 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1336 event needs attention (e.g. a signal handler or
1337 async I/O handler); see Py_AddPendingCall() and
1338 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001339
Eric Snow7bda9de2019-03-08 17:25:54 -07001340 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001341 opcode = _Py_OPCODE(*next_instr);
1342 if (opcode == SETUP_FINALLY ||
1343 opcode == SETUP_WITH ||
1344 opcode == BEFORE_ASYNC_WITH ||
1345 opcode == YIELD_FROM) {
1346 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001347 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001348 - If we're about to enter the 'with:'. It will prevent
1349 emitting a resource warning in the common idiom
1350 'with open(path) as file:'.
1351 - If we're about to enter the 'async with:'.
1352 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001353 *very* useful, but might help in some cases and it's
1354 traditional)
1355 - If we're resuming a chain of nested 'yield from' or
1356 'await' calls, then each frame is parked with YIELD_FROM
1357 as its next opcode. If the user hit control-C we want to
1358 wait until we've reached the innermost frame before
1359 running the signal handler and raising KeyboardInterrupt
1360 (see bpo-30039).
1361 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 goto fast_next_opcode;
1363 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001364
Victor Stinnerda2914d2020-03-20 09:29:08 +01001365 if (eval_frame_handle_pending(tstate) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001366 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 }
1368 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370 fast_next_opcode:
1371 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001372
Łukasz Langaa785c872016-09-09 17:37:37 -07001373 if (PyDTrace_LINE_ENABLED())
1374 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001377
Victor Stinnerdab84232020-03-17 18:56:44 +01001378 if (_Py_TracingPossible(ceval2) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001379 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001380 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 /* see maybe_call_line_trace
1382 for expository comments */
1383 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 err = maybe_call_line_trace(tstate->c_tracefunc,
1386 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001387 tstate, f,
1388 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 /* Reload possibly changed frame fields */
1390 JUMPTO(f->f_lasti);
1391 if (f->f_stacktop != NULL) {
1392 stack_pointer = f->f_stacktop;
1393 f->f_stacktop = NULL;
1394 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001395 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001397 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001401
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001402 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001403 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001404#ifdef DYNAMIC_EXECUTION_PROFILE
1405#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 dxpairs[lastopcode][opcode]++;
1407 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001408#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001409 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001410#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001411
Guido van Rossum96a42c81992-01-12 02:29:51 +00001412#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001414
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001415 if (lltrace) {
1416 if (HAS_ARG(opcode)) {
1417 printf("%d: %d, %d\n",
1418 f->f_lasti, opcode, oparg);
1419 }
1420 else {
1421 printf("%d: %d\n",
1422 f->f_lasti, opcode);
1423 }
1424 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001425#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001429 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001430 It is essential that any operation that fails must goto error
1431 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001432
Benjamin Petersonddd19492018-09-16 22:38:02 -07001433 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001435 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001436
Benjamin Petersonddd19492018-09-16 22:38:02 -07001437 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001438 PyObject *value = GETLOCAL(oparg);
1439 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001440 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001441 UNBOUNDLOCAL_ERROR_MSG,
1442 PyTuple_GetItem(co->co_varnames, oparg));
1443 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001444 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001445 Py_INCREF(value);
1446 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001447 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001448 }
1449
Benjamin Petersonddd19492018-09-16 22:38:02 -07001450 case TARGET(LOAD_CONST): {
1451 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001452 PyObject *value = GETITEM(consts, oparg);
1453 Py_INCREF(value);
1454 PUSH(value);
1455 FAST_DISPATCH();
1456 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001457
Benjamin Petersonddd19492018-09-16 22:38:02 -07001458 case TARGET(STORE_FAST): {
1459 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001460 PyObject *value = POP();
1461 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001462 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001463 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001464
Benjamin Petersonddd19492018-09-16 22:38:02 -07001465 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001466 PyObject *value = POP();
1467 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001468 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001469 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001470
Benjamin Petersonddd19492018-09-16 22:38:02 -07001471 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001472 PyObject *top = TOP();
1473 PyObject *second = SECOND();
1474 SET_TOP(second);
1475 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001476 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001477 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001478
Benjamin Petersonddd19492018-09-16 22:38:02 -07001479 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001480 PyObject *top = TOP();
1481 PyObject *second = SECOND();
1482 PyObject *third = THIRD();
1483 SET_TOP(second);
1484 SET_SECOND(third);
1485 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001486 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001487 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001488
Benjamin Petersonddd19492018-09-16 22:38:02 -07001489 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001490 PyObject *top = TOP();
1491 PyObject *second = SECOND();
1492 PyObject *third = THIRD();
1493 PyObject *fourth = FOURTH();
1494 SET_TOP(second);
1495 SET_SECOND(third);
1496 SET_THIRD(fourth);
1497 SET_FOURTH(top);
1498 FAST_DISPATCH();
1499 }
1500
Benjamin Petersonddd19492018-09-16 22:38:02 -07001501 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001502 PyObject *top = TOP();
1503 Py_INCREF(top);
1504 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001505 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001506 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001507
Benjamin Petersonddd19492018-09-16 22:38:02 -07001508 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001509 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001510 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001511 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001512 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001513 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001514 SET_TOP(top);
1515 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001516 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001517 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001518
Benjamin Petersonddd19492018-09-16 22:38:02 -07001519 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001520 PyObject *value = TOP();
1521 PyObject *res = PyNumber_Positive(value);
1522 Py_DECREF(value);
1523 SET_TOP(res);
1524 if (res == NULL)
1525 goto error;
1526 DISPATCH();
1527 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001528
Benjamin Petersonddd19492018-09-16 22:38:02 -07001529 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001530 PyObject *value = TOP();
1531 PyObject *res = PyNumber_Negative(value);
1532 Py_DECREF(value);
1533 SET_TOP(res);
1534 if (res == NULL)
1535 goto error;
1536 DISPATCH();
1537 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001538
Benjamin Petersonddd19492018-09-16 22:38:02 -07001539 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001540 PyObject *value = TOP();
1541 int err = PyObject_IsTrue(value);
1542 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001543 if (err == 0) {
1544 Py_INCREF(Py_True);
1545 SET_TOP(Py_True);
1546 DISPATCH();
1547 }
1548 else if (err > 0) {
1549 Py_INCREF(Py_False);
1550 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001551 DISPATCH();
1552 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001553 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001554 goto error;
1555 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001556
Benjamin Petersonddd19492018-09-16 22:38:02 -07001557 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001558 PyObject *value = TOP();
1559 PyObject *res = PyNumber_Invert(value);
1560 Py_DECREF(value);
1561 SET_TOP(res);
1562 if (res == NULL)
1563 goto error;
1564 DISPATCH();
1565 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001566
Benjamin Petersonddd19492018-09-16 22:38:02 -07001567 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001568 PyObject *exp = POP();
1569 PyObject *base = TOP();
1570 PyObject *res = PyNumber_Power(base, exp, Py_None);
1571 Py_DECREF(base);
1572 Py_DECREF(exp);
1573 SET_TOP(res);
1574 if (res == NULL)
1575 goto error;
1576 DISPATCH();
1577 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001578
Benjamin Petersonddd19492018-09-16 22:38:02 -07001579 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001580 PyObject *right = POP();
1581 PyObject *left = TOP();
1582 PyObject *res = PyNumber_Multiply(left, right);
1583 Py_DECREF(left);
1584 Py_DECREF(right);
1585 SET_TOP(res);
1586 if (res == NULL)
1587 goto error;
1588 DISPATCH();
1589 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001590
Benjamin Petersonddd19492018-09-16 22:38:02 -07001591 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001592 PyObject *right = POP();
1593 PyObject *left = TOP();
1594 PyObject *res = PyNumber_MatrixMultiply(left, right);
1595 Py_DECREF(left);
1596 Py_DECREF(right);
1597 SET_TOP(res);
1598 if (res == NULL)
1599 goto error;
1600 DISPATCH();
1601 }
1602
Benjamin Petersonddd19492018-09-16 22:38:02 -07001603 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001604 PyObject *divisor = POP();
1605 PyObject *dividend = TOP();
1606 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1607 Py_DECREF(dividend);
1608 Py_DECREF(divisor);
1609 SET_TOP(quotient);
1610 if (quotient == NULL)
1611 goto error;
1612 DISPATCH();
1613 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001614
Benjamin Petersonddd19492018-09-16 22:38:02 -07001615 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001616 PyObject *divisor = POP();
1617 PyObject *dividend = TOP();
1618 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1619 Py_DECREF(dividend);
1620 Py_DECREF(divisor);
1621 SET_TOP(quotient);
1622 if (quotient == NULL)
1623 goto error;
1624 DISPATCH();
1625 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001626
Benjamin Petersonddd19492018-09-16 22:38:02 -07001627 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001628 PyObject *divisor = POP();
1629 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001630 PyObject *res;
1631 if (PyUnicode_CheckExact(dividend) && (
1632 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1633 // fast path; string formatting, but not if the RHS is a str subclass
1634 // (see issue28598)
1635 res = PyUnicode_Format(dividend, divisor);
1636 } else {
1637 res = PyNumber_Remainder(dividend, divisor);
1638 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001639 Py_DECREF(divisor);
1640 Py_DECREF(dividend);
1641 SET_TOP(res);
1642 if (res == NULL)
1643 goto error;
1644 DISPATCH();
1645 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001646
Benjamin Petersonddd19492018-09-16 22:38:02 -07001647 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001648 PyObject *right = POP();
1649 PyObject *left = TOP();
1650 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001651 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1652 CPython using bytecode, it is simply worthless.
1653 See http://bugs.python.org/issue21955 and
1654 http://bugs.python.org/issue10044 for the discussion. In short,
1655 no patch shown any impact on a realistic benchmark, only a minor
1656 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001657 if (PyUnicode_CheckExact(left) &&
1658 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001659 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001660 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001661 }
1662 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001663 sum = PyNumber_Add(left, right);
1664 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001665 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001666 Py_DECREF(right);
1667 SET_TOP(sum);
1668 if (sum == NULL)
1669 goto error;
1670 DISPATCH();
1671 }
1672
Benjamin Petersonddd19492018-09-16 22:38:02 -07001673 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001674 PyObject *right = POP();
1675 PyObject *left = TOP();
1676 PyObject *diff = PyNumber_Subtract(left, right);
1677 Py_DECREF(right);
1678 Py_DECREF(left);
1679 SET_TOP(diff);
1680 if (diff == NULL)
1681 goto error;
1682 DISPATCH();
1683 }
1684
Benjamin Petersonddd19492018-09-16 22:38:02 -07001685 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001686 PyObject *sub = POP();
1687 PyObject *container = TOP();
1688 PyObject *res = PyObject_GetItem(container, sub);
1689 Py_DECREF(container);
1690 Py_DECREF(sub);
1691 SET_TOP(res);
1692 if (res == NULL)
1693 goto error;
1694 DISPATCH();
1695 }
1696
Benjamin Petersonddd19492018-09-16 22:38:02 -07001697 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001698 PyObject *right = POP();
1699 PyObject *left = TOP();
1700 PyObject *res = PyNumber_Lshift(left, right);
1701 Py_DECREF(left);
1702 Py_DECREF(right);
1703 SET_TOP(res);
1704 if (res == NULL)
1705 goto error;
1706 DISPATCH();
1707 }
1708
Benjamin Petersonddd19492018-09-16 22:38:02 -07001709 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001710 PyObject *right = POP();
1711 PyObject *left = TOP();
1712 PyObject *res = PyNumber_Rshift(left, right);
1713 Py_DECREF(left);
1714 Py_DECREF(right);
1715 SET_TOP(res);
1716 if (res == NULL)
1717 goto error;
1718 DISPATCH();
1719 }
1720
Benjamin Petersonddd19492018-09-16 22:38:02 -07001721 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001722 PyObject *right = POP();
1723 PyObject *left = TOP();
1724 PyObject *res = PyNumber_And(left, right);
1725 Py_DECREF(left);
1726 Py_DECREF(right);
1727 SET_TOP(res);
1728 if (res == NULL)
1729 goto error;
1730 DISPATCH();
1731 }
1732
Benjamin Petersonddd19492018-09-16 22:38:02 -07001733 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001734 PyObject *right = POP();
1735 PyObject *left = TOP();
1736 PyObject *res = PyNumber_Xor(left, right);
1737 Py_DECREF(left);
1738 Py_DECREF(right);
1739 SET_TOP(res);
1740 if (res == NULL)
1741 goto error;
1742 DISPATCH();
1743 }
1744
Benjamin Petersonddd19492018-09-16 22:38:02 -07001745 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001746 PyObject *right = POP();
1747 PyObject *left = TOP();
1748 PyObject *res = PyNumber_Or(left, right);
1749 Py_DECREF(left);
1750 Py_DECREF(right);
1751 SET_TOP(res);
1752 if (res == NULL)
1753 goto error;
1754 DISPATCH();
1755 }
1756
Benjamin Petersonddd19492018-09-16 22:38:02 -07001757 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001758 PyObject *v = POP();
1759 PyObject *list = PEEK(oparg);
1760 int err;
1761 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001762 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001763 if (err != 0)
1764 goto error;
1765 PREDICT(JUMP_ABSOLUTE);
1766 DISPATCH();
1767 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001768
Benjamin Petersonddd19492018-09-16 22:38:02 -07001769 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001770 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001771 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001772 int err;
1773 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001774 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001775 if (err != 0)
1776 goto error;
1777 PREDICT(JUMP_ABSOLUTE);
1778 DISPATCH();
1779 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001780
Benjamin Petersonddd19492018-09-16 22:38:02 -07001781 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001782 PyObject *exp = POP();
1783 PyObject *base = TOP();
1784 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1785 Py_DECREF(base);
1786 Py_DECREF(exp);
1787 SET_TOP(res);
1788 if (res == NULL)
1789 goto error;
1790 DISPATCH();
1791 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001792
Benjamin Petersonddd19492018-09-16 22:38:02 -07001793 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001794 PyObject *right = POP();
1795 PyObject *left = TOP();
1796 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1797 Py_DECREF(left);
1798 Py_DECREF(right);
1799 SET_TOP(res);
1800 if (res == NULL)
1801 goto error;
1802 DISPATCH();
1803 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001804
Benjamin Petersonddd19492018-09-16 22:38:02 -07001805 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001806 PyObject *right = POP();
1807 PyObject *left = TOP();
1808 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1809 Py_DECREF(left);
1810 Py_DECREF(right);
1811 SET_TOP(res);
1812 if (res == NULL)
1813 goto error;
1814 DISPATCH();
1815 }
1816
Benjamin Petersonddd19492018-09-16 22:38:02 -07001817 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001818 PyObject *divisor = POP();
1819 PyObject *dividend = TOP();
1820 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1821 Py_DECREF(dividend);
1822 Py_DECREF(divisor);
1823 SET_TOP(quotient);
1824 if (quotient == NULL)
1825 goto error;
1826 DISPATCH();
1827 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001828
Benjamin Petersonddd19492018-09-16 22:38:02 -07001829 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001830 PyObject *divisor = POP();
1831 PyObject *dividend = TOP();
1832 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1833 Py_DECREF(dividend);
1834 Py_DECREF(divisor);
1835 SET_TOP(quotient);
1836 if (quotient == NULL)
1837 goto error;
1838 DISPATCH();
1839 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001840
Benjamin Petersonddd19492018-09-16 22:38:02 -07001841 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001842 PyObject *right = POP();
1843 PyObject *left = TOP();
1844 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1845 Py_DECREF(left);
1846 Py_DECREF(right);
1847 SET_TOP(mod);
1848 if (mod == NULL)
1849 goto error;
1850 DISPATCH();
1851 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001852
Benjamin Petersonddd19492018-09-16 22:38:02 -07001853 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001854 PyObject *right = POP();
1855 PyObject *left = TOP();
1856 PyObject *sum;
1857 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001858 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001859 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001860 }
1861 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001862 sum = PyNumber_InPlaceAdd(left, right);
1863 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001864 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001865 Py_DECREF(right);
1866 SET_TOP(sum);
1867 if (sum == NULL)
1868 goto error;
1869 DISPATCH();
1870 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001871
Benjamin Petersonddd19492018-09-16 22:38:02 -07001872 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001873 PyObject *right = POP();
1874 PyObject *left = TOP();
1875 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1876 Py_DECREF(left);
1877 Py_DECREF(right);
1878 SET_TOP(diff);
1879 if (diff == NULL)
1880 goto error;
1881 DISPATCH();
1882 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001883
Benjamin Petersonddd19492018-09-16 22:38:02 -07001884 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001885 PyObject *right = POP();
1886 PyObject *left = TOP();
1887 PyObject *res = PyNumber_InPlaceLshift(left, right);
1888 Py_DECREF(left);
1889 Py_DECREF(right);
1890 SET_TOP(res);
1891 if (res == NULL)
1892 goto error;
1893 DISPATCH();
1894 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001895
Benjamin Petersonddd19492018-09-16 22:38:02 -07001896 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001897 PyObject *right = POP();
1898 PyObject *left = TOP();
1899 PyObject *res = PyNumber_InPlaceRshift(left, right);
1900 Py_DECREF(left);
1901 Py_DECREF(right);
1902 SET_TOP(res);
1903 if (res == NULL)
1904 goto error;
1905 DISPATCH();
1906 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001907
Benjamin Petersonddd19492018-09-16 22:38:02 -07001908 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001909 PyObject *right = POP();
1910 PyObject *left = TOP();
1911 PyObject *res = PyNumber_InPlaceAnd(left, right);
1912 Py_DECREF(left);
1913 Py_DECREF(right);
1914 SET_TOP(res);
1915 if (res == NULL)
1916 goto error;
1917 DISPATCH();
1918 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001919
Benjamin Petersonddd19492018-09-16 22:38:02 -07001920 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001921 PyObject *right = POP();
1922 PyObject *left = TOP();
1923 PyObject *res = PyNumber_InPlaceXor(left, right);
1924 Py_DECREF(left);
1925 Py_DECREF(right);
1926 SET_TOP(res);
1927 if (res == NULL)
1928 goto error;
1929 DISPATCH();
1930 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001931
Benjamin Petersonddd19492018-09-16 22:38:02 -07001932 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001933 PyObject *right = POP();
1934 PyObject *left = TOP();
1935 PyObject *res = PyNumber_InPlaceOr(left, right);
1936 Py_DECREF(left);
1937 Py_DECREF(right);
1938 SET_TOP(res);
1939 if (res == NULL)
1940 goto error;
1941 DISPATCH();
1942 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001943
Benjamin Petersonddd19492018-09-16 22:38:02 -07001944 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001945 PyObject *sub = TOP();
1946 PyObject *container = SECOND();
1947 PyObject *v = THIRD();
1948 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001949 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001950 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001951 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001952 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001953 Py_DECREF(container);
1954 Py_DECREF(sub);
1955 if (err != 0)
1956 goto error;
1957 DISPATCH();
1958 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001959
Benjamin Petersonddd19492018-09-16 22:38:02 -07001960 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001961 PyObject *sub = TOP();
1962 PyObject *container = SECOND();
1963 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001964 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00001965 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001966 err = PyObject_DelItem(container, sub);
1967 Py_DECREF(container);
1968 Py_DECREF(sub);
1969 if (err != 0)
1970 goto error;
1971 DISPATCH();
1972 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001973
Benjamin Petersonddd19492018-09-16 22:38:02 -07001974 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01001975 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001976 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001977 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001978 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001979 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001980 _PyErr_SetString(tstate, PyExc_RuntimeError,
1981 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001982 Py_DECREF(value);
1983 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001984 }
Petr Viktorinffd97532020-02-11 17:46:57 +01001985 res = PyObject_CallOneArg(hook, value);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001986 Py_DECREF(value);
1987 if (res == NULL)
1988 goto error;
1989 Py_DECREF(res);
1990 DISPATCH();
1991 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001992
Benjamin Petersonddd19492018-09-16 22:38:02 -07001993 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001994 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001995 switch (oparg) {
1996 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001997 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001998 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001999 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002000 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02002001 /* fall through */
2002 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02002003 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002004 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002005 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002006 break;
2007 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02002008 _PyErr_SetString(tstate, PyExc_SystemError,
2009 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002010 break;
2011 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002012 goto error;
2013 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002014
Benjamin Petersonddd19492018-09-16 22:38:02 -07002015 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002016 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002017 assert(f->f_iblock == 0);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002018 assert(EMPTY());
2019 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002020 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002021
Benjamin Petersonddd19492018-09-16 22:38:02 -07002022 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002023 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002024 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002025 PyObject *obj = TOP();
2026 PyTypeObject *type = Py_TYPE(obj);
2027
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002028 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002029 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002030 }
Yury Selivanov75445082015-05-11 22:57:16 -04002031
2032 if (getter != NULL) {
2033 iter = (*getter)(obj);
2034 Py_DECREF(obj);
2035 if (iter == NULL) {
2036 SET_TOP(NULL);
2037 goto error;
2038 }
2039 }
2040 else {
2041 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002042 _PyErr_Format(tstate, PyExc_TypeError,
2043 "'async for' requires an object with "
2044 "__aiter__ method, got %.100s",
2045 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002046 Py_DECREF(obj);
2047 goto error;
2048 }
2049
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002050 if (Py_TYPE(iter)->tp_as_async == NULL ||
2051 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002052
Yury Selivanov398ff912017-03-02 22:20:00 -05002053 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002054 _PyErr_Format(tstate, PyExc_TypeError,
2055 "'async for' received an object from __aiter__ "
2056 "that does not implement __anext__: %.100s",
2057 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002058 Py_DECREF(iter);
2059 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002060 }
2061
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002062 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04002063 DISPATCH();
2064 }
2065
Benjamin Petersonddd19492018-09-16 22:38:02 -07002066 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002067 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002068 PyObject *next_iter = NULL;
2069 PyObject *awaitable = NULL;
2070 PyObject *aiter = TOP();
2071 PyTypeObject *type = Py_TYPE(aiter);
2072
Yury Selivanoveb636452016-09-08 22:01:51 -07002073 if (PyAsyncGen_CheckExact(aiter)) {
2074 awaitable = type->tp_as_async->am_anext(aiter);
2075 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002076 goto error;
2077 }
Yury Selivanoveb636452016-09-08 22:01:51 -07002078 } else {
2079 if (type->tp_as_async != NULL){
2080 getter = type->tp_as_async->am_anext;
2081 }
Yury Selivanov75445082015-05-11 22:57:16 -04002082
Yury Selivanoveb636452016-09-08 22:01:51 -07002083 if (getter != NULL) {
2084 next_iter = (*getter)(aiter);
2085 if (next_iter == NULL) {
2086 goto error;
2087 }
2088 }
2089 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02002090 _PyErr_Format(tstate, PyExc_TypeError,
2091 "'async for' requires an iterator with "
2092 "__anext__ method, got %.100s",
2093 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07002094 goto error;
2095 }
Yury Selivanov75445082015-05-11 22:57:16 -04002096
Yury Selivanoveb636452016-09-08 22:01:51 -07002097 awaitable = _PyCoro_GetAwaitableIter(next_iter);
2098 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05002099 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07002100 PyExc_TypeError,
2101 "'async for' received an invalid object "
2102 "from __anext__: %.100s",
2103 Py_TYPE(next_iter)->tp_name);
2104
2105 Py_DECREF(next_iter);
2106 goto error;
2107 } else {
2108 Py_DECREF(next_iter);
2109 }
2110 }
Yury Selivanov75445082015-05-11 22:57:16 -04002111
2112 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002113 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002114 DISPATCH();
2115 }
2116
Benjamin Petersonddd19492018-09-16 22:38:02 -07002117 case TARGET(GET_AWAITABLE): {
2118 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002119 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002120 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002121
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002122 if (iter == NULL) {
Mark Shannonfee55262019-11-21 09:11:43 +00002123 int opcode_at_minus_3 = 0;
2124 if ((next_instr - first_instr) > 2) {
2125 opcode_at_minus_3 = _Py_OPCODE(next_instr[-3]);
2126 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002127 format_awaitable_error(tstate, Py_TYPE(iterable),
Mark Shannonfee55262019-11-21 09:11:43 +00002128 opcode_at_minus_3,
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002129 _Py_OPCODE(next_instr[-2]));
2130 }
2131
Yury Selivanov75445082015-05-11 22:57:16 -04002132 Py_DECREF(iterable);
2133
Yury Selivanovc724bae2016-03-02 11:30:46 -05002134 if (iter != NULL && PyCoro_CheckExact(iter)) {
2135 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2136 if (yf != NULL) {
2137 /* `iter` is a coroutine object that is being
2138 awaited, `yf` is a pointer to the current awaitable
2139 being awaited on. */
2140 Py_DECREF(yf);
2141 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002142 _PyErr_SetString(tstate, PyExc_RuntimeError,
2143 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002144 /* The code below jumps to `error` if `iter` is NULL. */
2145 }
2146 }
2147
Yury Selivanov75445082015-05-11 22:57:16 -04002148 SET_TOP(iter); /* Even if it's NULL */
2149
2150 if (iter == NULL) {
2151 goto error;
2152 }
2153
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002154 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002155 DISPATCH();
2156 }
2157
Benjamin Petersonddd19492018-09-16 22:38:02 -07002158 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002159 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002160 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002161 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002162 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
2163 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002164 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002165 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002166 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002167 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002168 else
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02002169 retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002170 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002171 Py_DECREF(v);
2172 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002173 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002174 if (tstate->c_tracefunc != NULL
Victor Stinner438a12d2019-05-24 17:01:38 +02002175 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002176 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002177 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002178 if (err < 0)
2179 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002180 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002181 SET_TOP(val);
2182 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002183 }
Martin Panter95f53c12016-07-18 08:23:26 +00002184 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002185 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002186 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002187 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002188 f->f_lasti -= sizeof(_Py_CODEUNIT);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002189 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002190 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002191
Benjamin Petersonddd19492018-09-16 22:38:02 -07002192 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002193 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002194
2195 if (co->co_flags & CO_ASYNC_GENERATOR) {
2196 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2197 Py_DECREF(retval);
2198 if (w == NULL) {
2199 retval = NULL;
2200 goto error;
2201 }
2202 retval = w;
2203 }
2204
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002205 f->f_stacktop = stack_pointer;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002206 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002207 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002208
Benjamin Petersonddd19492018-09-16 22:38:02 -07002209 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002210 PyObject *type, *value, *traceback;
2211 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002212 PyTryBlock *b = PyFrame_BlockPop(f);
2213 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002214 _PyErr_SetString(tstate, PyExc_SystemError,
2215 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002216 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002217 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002218 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2219 STACK_LEVEL() <= (b)->b_level + 4);
2220 exc_info = tstate->exc_info;
2221 type = exc_info->exc_type;
2222 value = exc_info->exc_value;
2223 traceback = exc_info->exc_traceback;
2224 exc_info->exc_type = POP();
2225 exc_info->exc_value = POP();
2226 exc_info->exc_traceback = POP();
2227 Py_XDECREF(type);
2228 Py_XDECREF(value);
2229 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002230 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002231 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002232
Benjamin Petersonddd19492018-09-16 22:38:02 -07002233 case TARGET(POP_BLOCK): {
2234 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002235 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002236 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002237 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002238
Mark Shannonfee55262019-11-21 09:11:43 +00002239 case TARGET(RERAISE): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002240 PyObject *exc = POP();
Mark Shannonfee55262019-11-21 09:11:43 +00002241 PyObject *val = POP();
2242 PyObject *tb = POP();
2243 assert(PyExceptionClass_Check(exc));
Victor Stinner61f4db82020-01-28 03:37:45 +01002244 _PyErr_Restore(tstate, exc, val, tb);
Mark Shannonfee55262019-11-21 09:11:43 +00002245 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002246 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002247
Benjamin Petersonddd19492018-09-16 22:38:02 -07002248 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002249 PyObject *exc = POP();
2250 assert(PyExceptionClass_Check(exc));
2251 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2252 PyTryBlock *b = PyFrame_BlockPop(f);
2253 assert(b->b_type == EXCEPT_HANDLER);
2254 Py_DECREF(exc);
2255 UNWIND_EXCEPT_HANDLER(b);
2256 Py_DECREF(POP());
2257 JUMPBY(oparg);
2258 FAST_DISPATCH();
2259 }
2260 else {
2261 PyObject *val = POP();
2262 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002263 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002264 goto exception_unwind;
2265 }
2266 }
2267
Zackery Spytzce6a0702019-08-25 03:44:09 -06002268 case TARGET(LOAD_ASSERTION_ERROR): {
2269 PyObject *value = PyExc_AssertionError;
2270 Py_INCREF(value);
2271 PUSH(value);
2272 FAST_DISPATCH();
2273 }
2274
Benjamin Petersonddd19492018-09-16 22:38:02 -07002275 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002276 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002277
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002278 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002279 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002280 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002281 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002282 if (!_PyErr_Occurred(tstate)) {
2283 _PyErr_SetString(tstate, PyExc_NameError,
2284 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002285 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002286 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002287 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002288 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002289 }
2290 else {
2291 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2292 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002293 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002294 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2295 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002296 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2297 _PyErr_SetString(tstate, PyExc_NameError,
2298 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002299 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002300 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002301 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002302 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002303 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002304 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002305
Benjamin Petersonddd19492018-09-16 22:38:02 -07002306 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002307 PyObject *name = GETITEM(names, oparg);
2308 PyObject *v = POP();
2309 PyObject *ns = f->f_locals;
2310 int err;
2311 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002312 _PyErr_Format(tstate, PyExc_SystemError,
2313 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002314 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002315 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002316 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002317 if (PyDict_CheckExact(ns))
2318 err = PyDict_SetItem(ns, name, v);
2319 else
2320 err = PyObject_SetItem(ns, name, v);
2321 Py_DECREF(v);
2322 if (err != 0)
2323 goto error;
2324 DISPATCH();
2325 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002326
Benjamin Petersonddd19492018-09-16 22:38:02 -07002327 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002328 PyObject *name = GETITEM(names, oparg);
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 when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002334 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002335 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002336 err = PyObject_DelItem(ns, name);
2337 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002338 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002339 NAME_ERROR_MSG,
2340 name);
2341 goto error;
2342 }
2343 DISPATCH();
2344 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002345
Benjamin Petersonddd19492018-09-16 22:38:02 -07002346 case TARGET(UNPACK_SEQUENCE): {
2347 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002348 PyObject *seq = POP(), *item, **items;
2349 if (PyTuple_CheckExact(seq) &&
2350 PyTuple_GET_SIZE(seq) == oparg) {
2351 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002352 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002353 item = items[oparg];
2354 Py_INCREF(item);
2355 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002356 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002357 } else if (PyList_CheckExact(seq) &&
2358 PyList_GET_SIZE(seq) == oparg) {
2359 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002360 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002361 item = items[oparg];
2362 Py_INCREF(item);
2363 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002364 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002365 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002367 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002368 } else {
2369 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002370 Py_DECREF(seq);
2371 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002372 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002373 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002374 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002375 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002376
Benjamin Petersonddd19492018-09-16 22:38:02 -07002377 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002378 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2379 PyObject *seq = POP();
2380
Victor Stinner438a12d2019-05-24 17:01:38 +02002381 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002382 stack_pointer + totalargs)) {
2383 stack_pointer += totalargs;
2384 } else {
2385 Py_DECREF(seq);
2386 goto error;
2387 }
2388 Py_DECREF(seq);
2389 DISPATCH();
2390 }
2391
Benjamin Petersonddd19492018-09-16 22:38:02 -07002392 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002393 PyObject *name = GETITEM(names, oparg);
2394 PyObject *owner = TOP();
2395 PyObject *v = SECOND();
2396 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002397 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002398 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002399 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002400 Py_DECREF(owner);
2401 if (err != 0)
2402 goto error;
2403 DISPATCH();
2404 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002405
Benjamin Petersonddd19492018-09-16 22:38:02 -07002406 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002407 PyObject *name = GETITEM(names, oparg);
2408 PyObject *owner = POP();
2409 int err;
2410 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2411 Py_DECREF(owner);
2412 if (err != 0)
2413 goto error;
2414 DISPATCH();
2415 }
2416
Benjamin Petersonddd19492018-09-16 22:38:02 -07002417 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002418 PyObject *name = GETITEM(names, oparg);
2419 PyObject *v = POP();
2420 int err;
2421 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002422 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002423 if (err != 0)
2424 goto error;
2425 DISPATCH();
2426 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002427
Benjamin Petersonddd19492018-09-16 22:38:02 -07002428 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002429 PyObject *name = GETITEM(names, oparg);
2430 int err;
2431 err = PyDict_DelItem(f->f_globals, name);
2432 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002433 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2434 format_exc_check_arg(tstate, PyExc_NameError,
2435 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002436 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002437 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002438 }
2439 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002440 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002441
Benjamin Petersonddd19492018-09-16 22:38:02 -07002442 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002443 PyObject *name = GETITEM(names, oparg);
2444 PyObject *locals = f->f_locals;
2445 PyObject *v;
2446 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002447 _PyErr_Format(tstate, PyExc_SystemError,
2448 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002449 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002450 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002451 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002452 v = PyDict_GetItemWithError(locals, name);
2453 if (v != NULL) {
2454 Py_INCREF(v);
2455 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002456 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002457 goto error;
2458 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002459 }
2460 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002461 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002462 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002463 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002464 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002465 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002466 }
2467 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002468 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002469 v = PyDict_GetItemWithError(f->f_globals, name);
2470 if (v != NULL) {
2471 Py_INCREF(v);
2472 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002473 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002474 goto error;
2475 }
2476 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002477 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002478 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002479 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002480 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002481 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002482 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002483 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002484 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002485 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002486 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002487 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002488 }
2489 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002490 v = PyObject_GetItem(f->f_builtins, name);
2491 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002492 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002493 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002494 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002495 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002496 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002497 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002498 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002499 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002500 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002501 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002502 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002503 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002504 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002505
Benjamin Petersonddd19492018-09-16 22:38:02 -07002506 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002507 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002508 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002509 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002510 && PyDict_CheckExact(f->f_builtins))
2511 {
Inada Naoki91234a12019-06-03 21:30:58 +09002512 OPCACHE_CHECK();
2513 if (co_opcache != NULL && co_opcache->optimized > 0) {
2514 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2515
2516 if (lg->globals_ver ==
2517 ((PyDictObject *)f->f_globals)->ma_version_tag
2518 && lg->builtins_ver ==
2519 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2520 {
2521 PyObject *ptr = lg->ptr;
2522 OPCACHE_STAT_GLOBAL_HIT();
2523 assert(ptr != NULL);
2524 Py_INCREF(ptr);
2525 PUSH(ptr);
2526 DISPATCH();
2527 }
2528 }
2529
2530 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002531 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002532 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002533 name);
2534 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002535 if (!_PyErr_OCCURRED()) {
2536 /* _PyDict_LoadGlobal() returns NULL without raising
2537 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002538 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002539 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002540 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002541 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002542 }
Inada Naoki91234a12019-06-03 21:30:58 +09002543
2544 if (co_opcache != NULL) {
2545 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2546
2547 if (co_opcache->optimized == 0) {
2548 /* Wasn't optimized before. */
2549 OPCACHE_STAT_GLOBAL_OPT();
2550 } else {
2551 OPCACHE_STAT_GLOBAL_MISS();
2552 }
2553
2554 co_opcache->optimized = 1;
2555 lg->globals_ver =
2556 ((PyDictObject *)f->f_globals)->ma_version_tag;
2557 lg->builtins_ver =
2558 ((PyDictObject *)f->f_builtins)->ma_version_tag;
2559 lg->ptr = v; /* borrowed */
2560 }
2561
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002562 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002563 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002564 else {
2565 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002566
2567 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09002568 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002569 v = PyObject_GetItem(f->f_globals, name);
2570 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002571 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002572 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002573 }
2574 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002575
Victor Stinnerb4efc962015-11-20 09:24:02 +01002576 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002577 v = PyObject_GetItem(f->f_builtins, name);
2578 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002579 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002580 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002581 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002582 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002583 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002584 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002585 }
2586 }
2587 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002588 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002589 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002590 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002591
Benjamin Petersonddd19492018-09-16 22:38:02 -07002592 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002593 PyObject *v = GETLOCAL(oparg);
2594 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002595 SETLOCAL(oparg, NULL);
2596 DISPATCH();
2597 }
2598 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002599 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002600 UNBOUNDLOCAL_ERROR_MSG,
2601 PyTuple_GetItem(co->co_varnames, oparg)
2602 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002603 goto error;
2604 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002605
Benjamin Petersonddd19492018-09-16 22:38:02 -07002606 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002607 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002608 PyObject *oldobj = PyCell_GET(cell);
2609 if (oldobj != NULL) {
2610 PyCell_SET(cell, NULL);
2611 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002612 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002613 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002614 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002615 goto error;
2616 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002617
Benjamin Petersonddd19492018-09-16 22:38:02 -07002618 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002619 PyObject *cell = freevars[oparg];
2620 Py_INCREF(cell);
2621 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002622 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002623 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002624
Benjamin Petersonddd19492018-09-16 22:38:02 -07002625 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002626 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002627 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002628 assert(locals);
2629 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2630 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2631 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2632 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2633 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002634 value = PyDict_GetItemWithError(locals, name);
2635 if (value != NULL) {
2636 Py_INCREF(value);
2637 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002638 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002639 goto error;
2640 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002641 }
2642 else {
2643 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002644 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002645 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002646 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002647 }
2648 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002649 }
2650 }
2651 if (!value) {
2652 PyObject *cell = freevars[oparg];
2653 value = PyCell_GET(cell);
2654 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002655 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002656 goto error;
2657 }
2658 Py_INCREF(value);
2659 }
2660 PUSH(value);
2661 DISPATCH();
2662 }
2663
Benjamin Petersonddd19492018-09-16 22:38:02 -07002664 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002665 PyObject *cell = freevars[oparg];
2666 PyObject *value = PyCell_GET(cell);
2667 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002668 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002669 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002670 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002671 Py_INCREF(value);
2672 PUSH(value);
2673 DISPATCH();
2674 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002675
Benjamin Petersonddd19492018-09-16 22:38:02 -07002676 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002677 PyObject *v = POP();
2678 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002679 PyObject *oldobj = PyCell_GET(cell);
2680 PyCell_SET(cell, v);
2681 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002682 DISPATCH();
2683 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002684
Benjamin Petersonddd19492018-09-16 22:38:02 -07002685 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002686 PyObject *str;
2687 PyObject *empty = PyUnicode_New(0, 0);
2688 if (empty == NULL) {
2689 goto error;
2690 }
2691 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2692 Py_DECREF(empty);
2693 if (str == NULL)
2694 goto error;
2695 while (--oparg >= 0) {
2696 PyObject *item = POP();
2697 Py_DECREF(item);
2698 }
2699 PUSH(str);
2700 DISPATCH();
2701 }
2702
Benjamin Petersonddd19492018-09-16 22:38:02 -07002703 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002704 PyObject *tup = PyTuple_New(oparg);
2705 if (tup == NULL)
2706 goto error;
2707 while (--oparg >= 0) {
2708 PyObject *item = POP();
2709 PyTuple_SET_ITEM(tup, oparg, item);
2710 }
2711 PUSH(tup);
2712 DISPATCH();
2713 }
2714
Benjamin Petersonddd19492018-09-16 22:38:02 -07002715 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002716 PyObject *list = PyList_New(oparg);
2717 if (list == NULL)
2718 goto error;
2719 while (--oparg >= 0) {
2720 PyObject *item = POP();
2721 PyList_SET_ITEM(list, oparg, item);
2722 }
2723 PUSH(list);
2724 DISPATCH();
2725 }
2726
Mark Shannon13bc1392020-01-23 09:25:17 +00002727 case TARGET(LIST_TO_TUPLE): {
2728 PyObject *list = POP();
2729 PyObject *tuple = PyList_AsTuple(list);
2730 Py_DECREF(list);
2731 if (tuple == NULL) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002732 goto error;
Mark Shannon13bc1392020-01-23 09:25:17 +00002733 }
2734 PUSH(tuple);
2735 DISPATCH();
2736 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002737
Mark Shannon13bc1392020-01-23 09:25:17 +00002738 case TARGET(LIST_EXTEND): {
2739 PyObject *iterable = POP();
2740 PyObject *list = PEEK(oparg);
2741 PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
2742 if (none_val == NULL) {
2743 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01002744 (Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable)))
Mark Shannon13bc1392020-01-23 09:25:17 +00002745 {
Victor Stinner61f4db82020-01-28 03:37:45 +01002746 _PyErr_Clear(tstate);
Mark Shannon13bc1392020-01-23 09:25:17 +00002747 _PyErr_Format(tstate, PyExc_TypeError,
2748 "Value after * must be an iterable, not %.200s",
2749 Py_TYPE(iterable)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002750 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002751 Py_DECREF(iterable);
2752 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002753 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002754 Py_DECREF(none_val);
2755 Py_DECREF(iterable);
2756 DISPATCH();
2757 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002758
Mark Shannon13bc1392020-01-23 09:25:17 +00002759 case TARGET(SET_UPDATE): {
2760 PyObject *iterable = POP();
2761 PyObject *set = PEEK(oparg);
2762 int err = _PySet_Update(set, iterable);
2763 Py_DECREF(iterable);
2764 if (err < 0) {
2765 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002766 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002767 DISPATCH();
2768 }
2769
Benjamin Petersonddd19492018-09-16 22:38:02 -07002770 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002771 PyObject *set = PySet_New(NULL);
2772 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002773 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002774 if (set == NULL)
2775 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002776 for (i = oparg; i > 0; i--) {
2777 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002778 if (err == 0)
2779 err = PySet_Add(set, item);
2780 Py_DECREF(item);
2781 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002782 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002783 if (err != 0) {
2784 Py_DECREF(set);
2785 goto error;
2786 }
2787 PUSH(set);
2788 DISPATCH();
2789 }
2790
Benjamin Petersonddd19492018-09-16 22:38:02 -07002791 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002792 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002793 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2794 if (map == NULL)
2795 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002796 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002797 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002798 PyObject *key = PEEK(2*i);
2799 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002800 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002801 if (err != 0) {
2802 Py_DECREF(map);
2803 goto error;
2804 }
2805 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002806
2807 while (oparg--) {
2808 Py_DECREF(POP());
2809 Py_DECREF(POP());
2810 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002811 PUSH(map);
2812 DISPATCH();
2813 }
2814
Benjamin Petersonddd19492018-09-16 22:38:02 -07002815 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002816 _Py_IDENTIFIER(__annotations__);
2817 int err;
2818 PyObject *ann_dict;
2819 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002820 _PyErr_Format(tstate, PyExc_SystemError,
2821 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002822 goto error;
2823 }
2824 /* check if __annotations__ in locals()... */
2825 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002826 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002827 &PyId___annotations__);
2828 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002829 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002830 goto error;
2831 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002832 /* ...if not, create a new one */
2833 ann_dict = PyDict_New();
2834 if (ann_dict == NULL) {
2835 goto error;
2836 }
2837 err = _PyDict_SetItemId(f->f_locals,
2838 &PyId___annotations__, ann_dict);
2839 Py_DECREF(ann_dict);
2840 if (err != 0) {
2841 goto error;
2842 }
2843 }
2844 }
2845 else {
2846 /* do the same if locals() is not a dict */
2847 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2848 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002849 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002850 }
2851 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2852 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002853 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002854 goto error;
2855 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002856 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002857 ann_dict = PyDict_New();
2858 if (ann_dict == NULL) {
2859 goto error;
2860 }
2861 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2862 Py_DECREF(ann_dict);
2863 if (err != 0) {
2864 goto error;
2865 }
2866 }
2867 else {
2868 Py_DECREF(ann_dict);
2869 }
2870 }
2871 DISPATCH();
2872 }
2873
Benjamin Petersonddd19492018-09-16 22:38:02 -07002874 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002875 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002876 PyObject *map;
2877 PyObject *keys = TOP();
2878 if (!PyTuple_CheckExact(keys) ||
2879 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002880 _PyErr_SetString(tstate, PyExc_SystemError,
2881 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002882 goto error;
2883 }
2884 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2885 if (map == NULL) {
2886 goto error;
2887 }
2888 for (i = oparg; i > 0; i--) {
2889 int err;
2890 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2891 PyObject *value = PEEK(i + 1);
2892 err = PyDict_SetItem(map, key, value);
2893 if (err != 0) {
2894 Py_DECREF(map);
2895 goto error;
2896 }
2897 }
2898
2899 Py_DECREF(POP());
2900 while (oparg--) {
2901 Py_DECREF(POP());
2902 }
2903 PUSH(map);
2904 DISPATCH();
2905 }
2906
Mark Shannon8a4cd702020-01-27 09:57:45 +00002907 case TARGET(DICT_UPDATE): {
2908 PyObject *update = POP();
2909 PyObject *dict = PEEK(oparg);
2910 if (PyDict_Update(dict, update) < 0) {
2911 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
2912 _PyErr_Format(tstate, PyExc_TypeError,
2913 "'%.200s' object is not a mapping",
Victor Stinnera102ed72020-02-07 02:24:48 +01002914 Py_TYPE(update)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002915 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002916 Py_DECREF(update);
2917 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002918 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002919 Py_DECREF(update);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002920 DISPATCH();
2921 }
2922
Mark Shannon8a4cd702020-01-27 09:57:45 +00002923 case TARGET(DICT_MERGE): {
2924 PyObject *update = POP();
2925 PyObject *dict = PEEK(oparg);
2926
2927 if (_PyDict_MergeEx(dict, update, 2) < 0) {
2928 format_kwargs_error(tstate, PEEK(2 + oparg), update);
2929 Py_DECREF(update);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002930 goto error;
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002931 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002932 Py_DECREF(update);
Brandt Bucherf185a732019-09-28 17:12:49 -07002933 PREDICT(CALL_FUNCTION_EX);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002934 DISPATCH();
2935 }
2936
Benjamin Petersonddd19492018-09-16 22:38:02 -07002937 case TARGET(MAP_ADD): {
Jörn Heisslerc8a35412019-06-22 16:40:55 +02002938 PyObject *value = TOP();
2939 PyObject *key = SECOND();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002940 PyObject *map;
2941 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002942 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002943 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002944 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002945 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002946 Py_DECREF(value);
2947 Py_DECREF(key);
2948 if (err != 0)
2949 goto error;
2950 PREDICT(JUMP_ABSOLUTE);
2951 DISPATCH();
2952 }
2953
Benjamin Petersonddd19492018-09-16 22:38:02 -07002954 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002955 PyObject *name = GETITEM(names, oparg);
2956 PyObject *owner = TOP();
2957 PyObject *res = PyObject_GetAttr(owner, name);
2958 Py_DECREF(owner);
2959 SET_TOP(res);
2960 if (res == NULL)
2961 goto error;
2962 DISPATCH();
2963 }
2964
Benjamin Petersonddd19492018-09-16 22:38:02 -07002965 case TARGET(COMPARE_OP): {
Mark Shannon9af0e472020-01-14 10:12:45 +00002966 assert(oparg <= Py_GE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002967 PyObject *right = POP();
2968 PyObject *left = TOP();
Mark Shannon9af0e472020-01-14 10:12:45 +00002969 PyObject *res = PyObject_RichCompare(left, right, oparg);
2970 SET_TOP(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002971 Py_DECREF(left);
2972 Py_DECREF(right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002973 if (res == NULL)
2974 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002975 PREDICT(POP_JUMP_IF_FALSE);
2976 PREDICT(POP_JUMP_IF_TRUE);
2977 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002978 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002979
Mark Shannon9af0e472020-01-14 10:12:45 +00002980 case TARGET(IS_OP): {
2981 PyObject *right = POP();
2982 PyObject *left = TOP();
2983 int res = (left == right)^oparg;
2984 PyObject *b = res ? Py_True : Py_False;
2985 Py_INCREF(b);
2986 SET_TOP(b);
2987 Py_DECREF(left);
2988 Py_DECREF(right);
2989 PREDICT(POP_JUMP_IF_FALSE);
2990 PREDICT(POP_JUMP_IF_TRUE);
2991 FAST_DISPATCH();
2992 }
2993
2994 case TARGET(CONTAINS_OP): {
2995 PyObject *right = POP();
2996 PyObject *left = POP();
2997 int res = PySequence_Contains(right, left);
2998 Py_DECREF(left);
2999 Py_DECREF(right);
3000 if (res < 0) {
3001 goto error;
3002 }
3003 PyObject *b = (res^oparg) ? Py_True : Py_False;
3004 Py_INCREF(b);
3005 PUSH(b);
3006 PREDICT(POP_JUMP_IF_FALSE);
3007 PREDICT(POP_JUMP_IF_TRUE);
3008 FAST_DISPATCH();
3009 }
3010
3011#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
3012 "BaseException is not allowed"
3013
3014 case TARGET(JUMP_IF_NOT_EXC_MATCH): {
3015 PyObject *right = POP();
3016 PyObject *left = POP();
3017 if (PyTuple_Check(right)) {
3018 Py_ssize_t i, length;
3019 length = PyTuple_GET_SIZE(right);
3020 for (i = 0; i < length; i++) {
3021 PyObject *exc = PyTuple_GET_ITEM(right, i);
3022 if (!PyExceptionClass_Check(exc)) {
3023 _PyErr_SetString(tstate, PyExc_TypeError,
3024 CANNOT_CATCH_MSG);
3025 Py_DECREF(left);
3026 Py_DECREF(right);
3027 goto error;
3028 }
3029 }
3030 }
3031 else {
3032 if (!PyExceptionClass_Check(right)) {
3033 _PyErr_SetString(tstate, PyExc_TypeError,
3034 CANNOT_CATCH_MSG);
3035 Py_DECREF(left);
3036 Py_DECREF(right);
3037 goto error;
3038 }
3039 }
3040 int res = PyErr_GivenExceptionMatches(left, right);
3041 Py_DECREF(left);
3042 Py_DECREF(right);
3043 if (res > 0) {
3044 /* Exception matches -- Do nothing */;
3045 }
3046 else if (res == 0) {
3047 JUMPTO(oparg);
3048 }
3049 else {
3050 goto error;
3051 }
3052 DISPATCH();
3053 }
3054
Benjamin Petersonddd19492018-09-16 22:38:02 -07003055 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003056 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003057 PyObject *fromlist = POP();
3058 PyObject *level = TOP();
3059 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003060 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003061 Py_DECREF(level);
3062 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003063 SET_TOP(res);
3064 if (res == NULL)
3065 goto error;
3066 DISPATCH();
3067 }
3068
Benjamin Petersonddd19492018-09-16 22:38:02 -07003069 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003070 PyObject *from = POP(), *locals;
3071 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003072 if (PyFrame_FastToLocalsWithError(f) < 0) {
3073 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01003074 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003075 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01003076
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003077 locals = f->f_locals;
3078 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003079 _PyErr_SetString(tstate, PyExc_SystemError,
3080 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003081 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003082 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003083 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003084 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003085 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003086 Py_DECREF(from);
3087 if (err != 0)
3088 goto error;
3089 DISPATCH();
3090 }
Guido van Rossum25831651993-05-19 14:50:45 +00003091
Benjamin Petersonddd19492018-09-16 22:38:02 -07003092 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003093 PyObject *name = GETITEM(names, oparg);
3094 PyObject *from = TOP();
3095 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003096 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003097 PUSH(res);
3098 if (res == NULL)
3099 goto error;
3100 DISPATCH();
3101 }
Thomas Wouters52152252000-08-17 22:55:00 +00003102
Benjamin Petersonddd19492018-09-16 22:38:02 -07003103 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003104 JUMPBY(oparg);
3105 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003106 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003107
Benjamin Petersonddd19492018-09-16 22:38:02 -07003108 case TARGET(POP_JUMP_IF_FALSE): {
3109 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003110 PyObject *cond = POP();
3111 int err;
3112 if (cond == Py_True) {
3113 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003114 FAST_DISPATCH();
3115 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003116 if (cond == Py_False) {
3117 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003118 JUMPTO(oparg);
3119 FAST_DISPATCH();
3120 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003121 err = PyObject_IsTrue(cond);
3122 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003123 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003124 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003125 else if (err == 0)
3126 JUMPTO(oparg);
3127 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003128 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003129 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003130 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003131
Benjamin Petersonddd19492018-09-16 22:38:02 -07003132 case TARGET(POP_JUMP_IF_TRUE): {
3133 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003134 PyObject *cond = POP();
3135 int err;
3136 if (cond == Py_False) {
3137 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003138 FAST_DISPATCH();
3139 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003140 if (cond == Py_True) {
3141 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003142 JUMPTO(oparg);
3143 FAST_DISPATCH();
3144 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003145 err = PyObject_IsTrue(cond);
3146 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003147 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003148 JUMPTO(oparg);
3149 }
3150 else if (err == 0)
3151 ;
3152 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003153 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003154 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003155 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003156
Benjamin Petersonddd19492018-09-16 22:38:02 -07003157 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003158 PyObject *cond = TOP();
3159 int err;
3160 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003161 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003162 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003163 FAST_DISPATCH();
3164 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003165 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003166 JUMPTO(oparg);
3167 FAST_DISPATCH();
3168 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003169 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003170 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003171 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003172 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003173 }
3174 else if (err == 0)
3175 JUMPTO(oparg);
3176 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003177 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003178 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003179 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003180
Benjamin Petersonddd19492018-09-16 22:38:02 -07003181 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003182 PyObject *cond = TOP();
3183 int err;
3184 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003185 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003186 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003187 FAST_DISPATCH();
3188 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003189 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003190 JUMPTO(oparg);
3191 FAST_DISPATCH();
3192 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003193 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003194 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003195 JUMPTO(oparg);
3196 }
3197 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003198 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003199 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003200 }
3201 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003202 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003203 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003204 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003205
Benjamin Petersonddd19492018-09-16 22:38:02 -07003206 case TARGET(JUMP_ABSOLUTE): {
3207 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003208 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003209#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003210 /* Enabling this path speeds-up all while and for-loops by bypassing
3211 the per-loop checks for signals. By default, this should be turned-off
3212 because it prevents detection of a control-break in tight loops like
3213 "while 1: pass". Compile with this option turned-on when you need
3214 the speed-up and do not need break checking inside tight loops (ones
3215 that contain only instructions ending with FAST_DISPATCH).
3216 */
3217 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003218#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003219 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003220#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003221 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003222
Benjamin Petersonddd19492018-09-16 22:38:02 -07003223 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003224 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003225 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003226 PyObject *iter = PyObject_GetIter(iterable);
3227 Py_DECREF(iterable);
3228 SET_TOP(iter);
3229 if (iter == NULL)
3230 goto error;
3231 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003232 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003233 DISPATCH();
3234 }
3235
Benjamin Petersonddd19492018-09-16 22:38:02 -07003236 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04003237 /* before: [obj]; after [getiter(obj)] */
3238 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003239 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003240 if (PyCoro_CheckExact(iterable)) {
3241 /* `iterable` is a coroutine */
3242 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3243 /* and it is used in a 'yield from' expression of a
3244 regular generator. */
3245 Py_DECREF(iterable);
3246 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003247 _PyErr_SetString(tstate, PyExc_TypeError,
3248 "cannot 'yield from' a coroutine object "
3249 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04003250 goto error;
3251 }
3252 }
3253 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003254 /* `iterable` is not a generator. */
3255 iter = PyObject_GetIter(iterable);
3256 Py_DECREF(iterable);
3257 SET_TOP(iter);
3258 if (iter == NULL)
3259 goto error;
3260 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003261 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003262 DISPATCH();
3263 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003264
Benjamin Petersonddd19492018-09-16 22:38:02 -07003265 case TARGET(FOR_ITER): {
3266 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003267 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003268 PyObject *iter = TOP();
Victor Stinnera102ed72020-02-07 02:24:48 +01003269 PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003270 if (next != NULL) {
3271 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003272 PREDICT(STORE_FAST);
3273 PREDICT(UNPACK_SEQUENCE);
3274 DISPATCH();
3275 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003276 if (_PyErr_Occurred(tstate)) {
3277 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003278 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003279 }
3280 else if (tstate->c_tracefunc != NULL) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003281 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Victor Stinner438a12d2019-05-24 17:01:38 +02003282 }
3283 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003284 }
3285 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003286 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003287 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003288 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003289 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003290 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003291 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003292
Benjamin Petersonddd19492018-09-16 22:38:02 -07003293 case TARGET(SETUP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003294 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003295 STACK_LEVEL());
3296 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003297 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003298
Benjamin Petersonddd19492018-09-16 22:38:02 -07003299 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003300 _Py_IDENTIFIER(__aenter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003301 _Py_IDENTIFIER(__aexit__);
Yury Selivanov75445082015-05-11 22:57:16 -04003302 PyObject *mgr = TOP();
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003303 PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04003304 PyObject *res;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003305 if (enter == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003306 goto error;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003307 }
3308 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__);
3309 if (exit == NULL) {
3310 Py_DECREF(enter);
3311 goto error;
3312 }
Yury Selivanov75445082015-05-11 22:57:16 -04003313 SET_TOP(exit);
Yury Selivanov75445082015-05-11 22:57:16 -04003314 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003315 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003316 Py_DECREF(enter);
3317 if (res == NULL)
3318 goto error;
3319 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003320 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003321 DISPATCH();
3322 }
3323
Benjamin Petersonddd19492018-09-16 22:38:02 -07003324 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003325 PyObject *res = POP();
3326 /* Setup the finally block before pushing the result
3327 of __aenter__ on the stack. */
3328 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3329 STACK_LEVEL());
3330 PUSH(res);
3331 DISPATCH();
3332 }
3333
Benjamin Petersonddd19492018-09-16 22:38:02 -07003334 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003335 _Py_IDENTIFIER(__enter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003336 _Py_IDENTIFIER(__exit__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003337 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003338 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003339 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003340 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08003341 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003342 }
3343 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003344 if (exit == NULL) {
3345 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003346 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003347 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003348 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003349 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003350 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003351 Py_DECREF(enter);
3352 if (res == NULL)
3353 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003354 /* Setup the finally block before pushing the result
3355 of __enter__ on the stack. */
3356 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3357 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003358
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003359 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003360 DISPATCH();
3361 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003362
Mark Shannonfee55262019-11-21 09:11:43 +00003363 case TARGET(WITH_EXCEPT_START): {
3364 /* At the top of the stack are 7 values:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003365 - (TOP, SECOND, THIRD) = exc_info()
Mark Shannonfee55262019-11-21 09:11:43 +00003366 - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER
3367 - SEVENTH: the context.__exit__ bound method
3368 We call SEVENTH(TOP, SECOND, THIRD).
3369 Then we push again the TOP exception and the __exit__
3370 return value.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003371 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003372 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003373 PyObject *exc, *val, *tb, *res;
3374
Victor Stinner842cfff2016-12-01 14:45:31 +01003375 exc = TOP();
Mark Shannonfee55262019-11-21 09:11:43 +00003376 val = SECOND();
3377 tb = THIRD();
3378 assert(exc != Py_None);
3379 assert(!PyLong_Check(exc));
3380 exit_func = PEEK(7);
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003381 PyObject *stack[4] = {NULL, exc, val, tb};
Petr Viktorinffd97532020-02-11 17:46:57 +01003382 res = PyObject_Vectorcall(exit_func, stack + 1,
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003383 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003384 if (res == NULL)
3385 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003386
Yury Selivanov75445082015-05-11 22:57:16 -04003387 PUSH(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003388 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003389 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003390
Benjamin Petersonddd19492018-09-16 22:38:02 -07003391 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10003392 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003393 PyObject *name = GETITEM(names, oparg);
3394 PyObject *obj = TOP();
3395 PyObject *meth = NULL;
3396
3397 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3398
Yury Selivanovf2392132016-12-13 19:03:51 -05003399 if (meth == NULL) {
3400 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003401 goto error;
3402 }
3403
3404 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003405 /* We can bypass temporary bound method object.
3406 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003407
INADA Naoki015bce62017-01-16 17:23:30 +09003408 meth | self | arg1 | ... | argN
3409 */
3410 SET_TOP(meth);
3411 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003412 }
3413 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003414 /* meth is not an unbound method (but a regular attr, or
3415 something was returned by a descriptor protocol). Set
3416 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003417 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003418
3419 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003420 */
INADA Naoki015bce62017-01-16 17:23:30 +09003421 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003422 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003423 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003424 }
3425 DISPATCH();
3426 }
3427
Benjamin Petersonddd19492018-09-16 22:38:02 -07003428 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003429 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003430 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003431
3432 sp = stack_pointer;
3433
INADA Naoki015bce62017-01-16 17:23:30 +09003434 meth = PEEK(oparg + 2);
3435 if (meth == NULL) {
3436 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3437 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003438
3439 Stack layout:
3440
INADA Naoki015bce62017-01-16 17:23:30 +09003441 ... | NULL | callable | arg1 | ... | argN
3442 ^- TOP()
3443 ^- (-oparg)
3444 ^- (-oparg-1)
3445 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003446
Ville Skyttä49b27342017-08-03 09:00:59 +03003447 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003448 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003449 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003450 res = call_function(tstate, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003451 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003452 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003453 }
3454 else {
3455 /* This is a method call. Stack layout:
3456
INADA Naoki015bce62017-01-16 17:23:30 +09003457 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003458 ^- TOP()
3459 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003460 ^- (-oparg-1)
3461 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003462
INADA Naoki015bce62017-01-16 17:23:30 +09003463 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003464 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003465 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003466 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003467 res = call_function(tstate, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003468 stack_pointer = sp;
3469 }
3470
3471 PUSH(res);
3472 if (res == NULL)
3473 goto error;
3474 DISPATCH();
3475 }
3476
Benjamin Petersonddd19492018-09-16 22:38:02 -07003477 case TARGET(CALL_FUNCTION): {
3478 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003479 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003480 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003481 res = call_function(tstate, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003482 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003483 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003484 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003485 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003486 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003487 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003488 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003489
Benjamin Petersonddd19492018-09-16 22:38:02 -07003490 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003491 PyObject **sp, *res, *names;
3492
3493 names = POP();
Jeroen Demeyer05677862019-08-16 12:41:27 +02003494 assert(PyTuple_Check(names));
3495 assert(PyTuple_GET_SIZE(names) <= oparg);
3496 /* We assume without checking that names contains only strings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003497 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003498 res = call_function(tstate, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003499 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003500 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003501 Py_DECREF(names);
3502
3503 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003504 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003505 }
3506 DISPATCH();
3507 }
3508
Benjamin Petersonddd19492018-09-16 22:38:02 -07003509 case TARGET(CALL_FUNCTION_EX): {
Brandt Bucherf185a732019-09-28 17:12:49 -07003510 PREDICTED(CALL_FUNCTION_EX);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003511 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003512 if (oparg & 0x01) {
3513 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003514 if (!PyDict_CheckExact(kwargs)) {
3515 PyObject *d = PyDict_New();
3516 if (d == NULL)
3517 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003518 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003519 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02003520 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003521 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003522 goto error;
3523 }
3524 Py_DECREF(kwargs);
3525 kwargs = d;
3526 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003527 assert(PyDict_CheckExact(kwargs));
3528 }
3529 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003530 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003531 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003532 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003533 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003534 goto error;
3535 }
3536 Py_SETREF(callargs, PySequence_Tuple(callargs));
3537 if (callargs == NULL) {
3538 goto error;
3539 }
3540 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003541 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003542
Victor Stinner09532fe2019-05-10 23:39:09 +02003543 result = do_call_core(tstate, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003544 Py_DECREF(func);
3545 Py_DECREF(callargs);
3546 Py_XDECREF(kwargs);
3547
3548 SET_TOP(result);
3549 if (result == NULL) {
3550 goto error;
3551 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003552 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003553 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003554
Benjamin Petersonddd19492018-09-16 22:38:02 -07003555 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003556 PyObject *qualname = POP();
3557 PyObject *codeobj = POP();
3558 PyFunctionObject *func = (PyFunctionObject *)
3559 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003560
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003561 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003562 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003563 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003564 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003565 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003566
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003567 if (oparg & 0x08) {
3568 assert(PyTuple_CheckExact(TOP()));
3569 func ->func_closure = POP();
3570 }
3571 if (oparg & 0x04) {
3572 assert(PyDict_CheckExact(TOP()));
3573 func->func_annotations = POP();
3574 }
3575 if (oparg & 0x02) {
3576 assert(PyDict_CheckExact(TOP()));
3577 func->func_kwdefaults = POP();
3578 }
3579 if (oparg & 0x01) {
3580 assert(PyTuple_CheckExact(TOP()));
3581 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003582 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003583
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003584 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003585 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003586 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003587
Benjamin Petersonddd19492018-09-16 22:38:02 -07003588 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003589 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003590 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003591 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003592 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003593 step = NULL;
3594 stop = POP();
3595 start = TOP();
3596 slice = PySlice_New(start, stop, step);
3597 Py_DECREF(start);
3598 Py_DECREF(stop);
3599 Py_XDECREF(step);
3600 SET_TOP(slice);
3601 if (slice == NULL)
3602 goto error;
3603 DISPATCH();
3604 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003605
Benjamin Petersonddd19492018-09-16 22:38:02 -07003606 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003607 /* Handles f-string value formatting. */
3608 PyObject *result;
3609 PyObject *fmt_spec;
3610 PyObject *value;
3611 PyObject *(*conv_fn)(PyObject *);
3612 int which_conversion = oparg & FVC_MASK;
3613 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3614
3615 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003616 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003617
3618 /* See if any conversion is specified. */
3619 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003620 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003621 case FVC_STR: conv_fn = PyObject_Str; break;
3622 case FVC_REPR: conv_fn = PyObject_Repr; break;
3623 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003624 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02003625 _PyErr_Format(tstate, PyExc_SystemError,
3626 "unexpected conversion flag %d",
3627 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003628 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003629 }
3630
3631 /* If there's a conversion function, call it and replace
3632 value with that result. Otherwise, just use value,
3633 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003634 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003635 result = conv_fn(value);
3636 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003637 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003638 Py_XDECREF(fmt_spec);
3639 goto error;
3640 }
3641 value = result;
3642 }
3643
3644 /* If value is a unicode object, and there's no fmt_spec,
3645 then we know the result of format(value) is value
3646 itself. In that case, skip calling format(). I plan to
3647 move this optimization in to PyObject_Format()
3648 itself. */
3649 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3650 /* Do nothing, just transfer ownership to result. */
3651 result = value;
3652 } else {
3653 /* Actually call format(). */
3654 result = PyObject_Format(value, fmt_spec);
3655 Py_DECREF(value);
3656 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003657 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003658 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003659 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003660 }
3661
Eric V. Smith135d5f42016-02-05 18:23:08 -05003662 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003663 DISPATCH();
3664 }
3665
Benjamin Petersonddd19492018-09-16 22:38:02 -07003666 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003667 int oldoparg = oparg;
3668 NEXTOPARG();
3669 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003670 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003671 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003672
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003673
Antoine Pitrou042b1282010-08-13 21:15:58 +00003674#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003675 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003676#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003677 default:
3678 fprintf(stderr,
3679 "XXX lineno: %d, opcode: %d\n",
3680 PyFrame_GetLineNumber(f),
3681 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02003682 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003683 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003684
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003685 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003686
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003687 /* This should never be reached. Every opcode should end with DISPATCH()
3688 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003689 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003690
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003691error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003692 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003693#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02003694 if (!_PyErr_Occurred(tstate)) {
3695 _PyErr_SetString(tstate, PyExc_SystemError,
3696 "error return without exception set");
3697 }
Victor Stinner365b6932013-07-12 00:11:58 +02003698#else
Victor Stinner438a12d2019-05-24 17:01:38 +02003699 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02003700#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003701
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003702 /* Log traceback info. */
3703 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003704
Benjamin Peterson51f46162013-01-23 08:38:47 -05003705 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003706 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3707 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003708
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003709exception_unwind:
3710 /* Unwind stacks if an exception occurred */
3711 while (f->f_iblock > 0) {
3712 /* Pop the current block. */
3713 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003715 if (b->b_type == EXCEPT_HANDLER) {
3716 UNWIND_EXCEPT_HANDLER(b);
3717 continue;
3718 }
3719 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003720 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003721 PyObject *exc, *val, *tb;
3722 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003723 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003724 /* Beware, this invalidates all b->b_* fields */
3725 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003726 PUSH(exc_info->exc_traceback);
3727 PUSH(exc_info->exc_value);
3728 if (exc_info->exc_type != NULL) {
3729 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003730 }
3731 else {
3732 Py_INCREF(Py_None);
3733 PUSH(Py_None);
3734 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003735 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003736 /* Make the raw exception data
3737 available to the handler,
3738 so a program can emulate the
3739 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02003740 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003741 if (tb != NULL)
3742 PyException_SetTraceback(val, tb);
3743 else
3744 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003745 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003746 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003747 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003748 exc_info->exc_value = val;
3749 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003750 if (tb == NULL)
3751 tb = Py_None;
3752 Py_INCREF(tb);
3753 PUSH(tb);
3754 PUSH(val);
3755 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003756 JUMPTO(handler);
Victor Stinnerdab84232020-03-17 18:56:44 +01003757 if (_Py_TracingPossible(ceval2)) {
Pablo Galindo4c53e632020-01-10 09:24:22 +00003758 int needs_new_execution_window = (f->f_lasti < instr_lb || f->f_lasti >= instr_ub);
3759 int needs_line_update = (f->f_lasti == instr_lb || f->f_lasti < instr_prev);
3760 /* Make sure that we trace line after exception if we are in a new execution
3761 * window or we don't need a line update and we are not in the first instruction
3762 * of the line. */
3763 if (needs_new_execution_window || (!needs_line_update && instr_lb > 0)) {
3764 instr_prev = INT_MAX;
3765 }
Mark Shannonfee55262019-11-21 09:11:43 +00003766 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003767 /* Resume normal execution */
3768 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003769 }
3770 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003771
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003772 /* End the loop as we still have an error */
3773 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003774 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003775
Pablo Galindof00828a2019-05-09 16:52:02 +01003776 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003777 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01003778
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003779 /* Pop remaining stack entries. */
3780 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003781 PyObject *o = POP();
3782 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003783 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003784
Mark Shannone7c9f4a2020-01-13 12:51:26 +00003785exiting:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003786 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003787 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003788 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3789 tstate, f, PyTrace_RETURN, retval)) {
3790 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003791 }
3792 }
3793 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003794 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3795 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003796 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003797 }
3798 }
3799 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003801 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003802exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003803 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3804 dtrace_function_return(f);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01003805 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrou58720d62013-08-05 23:26:40 +02003806 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003807 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003808
Victor Stinner0b72b232020-03-12 23:18:39 +01003809 return _Py_CheckFunctionResult(tstate, NULL, retval, __func__);
Guido van Rossum374a9221991-04-04 10:40:29 +00003810}
3811
Benjamin Petersonb204a422011-06-05 22:04:07 -05003812static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003813format_missing(PyThreadState *tstate, const char *kind,
3814 PyCodeObject *co, PyObject *names)
Benjamin Petersone109c702011-06-24 09:37:26 -05003815{
3816 int err;
3817 Py_ssize_t len = PyList_GET_SIZE(names);
3818 PyObject *name_str, *comma, *tail, *tmp;
3819
3820 assert(PyList_CheckExact(names));
3821 assert(len >= 1);
3822 /* Deal with the joys of natural language. */
3823 switch (len) {
3824 case 1:
3825 name_str = PyList_GET_ITEM(names, 0);
3826 Py_INCREF(name_str);
3827 break;
3828 case 2:
3829 name_str = PyUnicode_FromFormat("%U and %U",
3830 PyList_GET_ITEM(names, len - 2),
3831 PyList_GET_ITEM(names, len - 1));
3832 break;
3833 default:
3834 tail = PyUnicode_FromFormat(", %U, and %U",
3835 PyList_GET_ITEM(names, len - 2),
3836 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003837 if (tail == NULL)
3838 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003839 /* Chop off the last two objects in the list. This shouldn't actually
3840 fail, but we can't be too careful. */
3841 err = PyList_SetSlice(names, len - 2, len, NULL);
3842 if (err == -1) {
3843 Py_DECREF(tail);
3844 return;
3845 }
3846 /* Stitch everything up into a nice comma-separated list. */
3847 comma = PyUnicode_FromString(", ");
3848 if (comma == NULL) {
3849 Py_DECREF(tail);
3850 return;
3851 }
3852 tmp = PyUnicode_Join(comma, names);
3853 Py_DECREF(comma);
3854 if (tmp == NULL) {
3855 Py_DECREF(tail);
3856 return;
3857 }
3858 name_str = PyUnicode_Concat(tmp, tail);
3859 Py_DECREF(tmp);
3860 Py_DECREF(tail);
3861 break;
3862 }
3863 if (name_str == NULL)
3864 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02003865 _PyErr_Format(tstate, PyExc_TypeError,
3866 "%U() missing %i required %s argument%s: %U",
3867 co->co_name,
3868 len,
3869 kind,
3870 len == 1 ? "" : "s",
3871 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05003872 Py_DECREF(name_str);
3873}
3874
3875static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003876missing_arguments(PyThreadState *tstate, PyCodeObject *co,
3877 Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003878 PyObject **fastlocals)
3879{
Victor Stinner74319ae2016-08-25 00:04:09 +02003880 Py_ssize_t i, j = 0;
3881 Py_ssize_t start, end;
3882 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003883 const char *kind = positional ? "positional" : "keyword-only";
3884 PyObject *missing_names;
3885
3886 /* Compute the names of the arguments that are missing. */
3887 missing_names = PyList_New(missing);
3888 if (missing_names == NULL)
3889 return;
3890 if (positional) {
3891 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01003892 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003893 }
3894 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003895 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003896 end = start + co->co_kwonlyargcount;
3897 }
3898 for (i = start; i < end; i++) {
3899 if (GETLOCAL(i) == NULL) {
3900 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3901 PyObject *name = PyObject_Repr(raw);
3902 if (name == NULL) {
3903 Py_DECREF(missing_names);
3904 return;
3905 }
3906 PyList_SET_ITEM(missing_names, j++, name);
3907 }
3908 }
3909 assert(j == missing);
Victor Stinner438a12d2019-05-24 17:01:38 +02003910 format_missing(tstate, kind, co, missing_names);
Benjamin Petersone109c702011-06-24 09:37:26 -05003911 Py_DECREF(missing_names);
3912}
3913
3914static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003915too_many_positional(PyThreadState *tstate, PyCodeObject *co,
3916 Py_ssize_t given, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003917 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003918{
3919 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003920 Py_ssize_t kwonly_given = 0;
3921 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003922 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003923 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003924
Benjamin Petersone109c702011-06-24 09:37:26 -05003925 assert((co->co_flags & CO_VARARGS) == 0);
3926 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01003927 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003928 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003929 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003930 }
3931 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003932 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01003933 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003934 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01003935 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003936 }
3937 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003938 plural = (co_argcount != 1);
3939 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003940 }
3941 if (sig == NULL)
3942 return;
3943 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003944 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3945 kwonly_sig = PyUnicode_FromFormat(format,
3946 given != 1 ? "s" : "",
3947 kwonly_given,
3948 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003949 if (kwonly_sig == NULL) {
3950 Py_DECREF(sig);
3951 return;
3952 }
3953 }
3954 else {
3955 /* This will not fail. */
3956 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003957 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003958 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003959 _PyErr_Format(tstate, PyExc_TypeError,
3960 "%U() takes %U positional argument%s but %zd%U %s given",
3961 co->co_name,
3962 sig,
3963 plural ? "s" : "",
3964 given,
3965 kwonly_sig,
3966 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003967 Py_DECREF(sig);
3968 Py_DECREF(kwonly_sig);
3969}
3970
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003971static int
Victor Stinner438a12d2019-05-24 17:01:38 +02003972positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
3973 Py_ssize_t kwcount, PyObject* const* kwnames)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003974{
3975 int posonly_conflicts = 0;
3976 PyObject* posonly_names = PyList_New(0);
3977
3978 for(int k=0; k < co->co_posonlyargcount; k++){
3979 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
3980
3981 for (int k2=0; k2<kwcount; k2++){
3982 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
3983 PyObject* kwname = kwnames[k2];
3984 if (kwname == posonly_name){
3985 if(PyList_Append(posonly_names, kwname) != 0) {
3986 goto fail;
3987 }
3988 posonly_conflicts++;
3989 continue;
3990 }
3991
3992 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
3993
3994 if ( cmp > 0) {
3995 if(PyList_Append(posonly_names, kwname) != 0) {
3996 goto fail;
3997 }
3998 posonly_conflicts++;
3999 } else if (cmp < 0) {
4000 goto fail;
4001 }
4002
4003 }
4004 }
4005 if (posonly_conflicts) {
4006 PyObject* comma = PyUnicode_FromString(", ");
4007 if (comma == NULL) {
4008 goto fail;
4009 }
4010 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
4011 Py_DECREF(comma);
4012 if (error_names == NULL) {
4013 goto fail;
4014 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004015 _PyErr_Format(tstate, PyExc_TypeError,
4016 "%U() got some positional-only arguments passed"
4017 " as keyword arguments: '%U'",
4018 co->co_name, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004019 Py_DECREF(error_names);
4020 goto fail;
4021 }
4022
4023 Py_DECREF(posonly_names);
4024 return 0;
4025
4026fail:
4027 Py_XDECREF(posonly_names);
4028 return 1;
4029
4030}
4031
Guido van Rossumc2e20742006-02-27 22:32:47 +00004032/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02004033 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00004034 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00004035
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004036PyObject *
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004037_PyEval_EvalCode(PyThreadState *tstate,
4038 PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004039 PyObject *const *args, Py_ssize_t argcount,
4040 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03004041 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004042 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02004043 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004044 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00004045{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004046 assert(is_tstate_valid(tstate));
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004047
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00004048 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004049 PyFrameObject *f;
4050 PyObject *retval = NULL;
4051 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004052 PyObject *x, *u;
Pablo Galindocd74e662019-06-01 18:08:04 +01004053 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004054 Py_ssize_t i, j, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02004055 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00004056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004057 if (globals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004058 _PyErr_SetString(tstate, PyExc_SystemError,
4059 "PyEval_EvalCodeEx: NULL globals");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004060 return NULL;
4061 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004062
Victor Stinnerc7020012016-08-16 23:40:29 +02004063 /* Create the frame */
INADA Naoki5a625d02016-12-24 20:19:08 +09004064 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02004065 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004066 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02004067 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004068 fastlocals = f->f_localsplus;
4069 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00004070
Victor Stinnerc7020012016-08-16 23:40:29 +02004071 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004072 if (co->co_flags & CO_VARKEYWORDS) {
4073 kwdict = PyDict_New();
4074 if (kwdict == NULL)
4075 goto fail;
4076 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02004077 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004078 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02004079 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004080 SETLOCAL(i, kwdict);
4081 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004082 else {
4083 kwdict = NULL;
4084 }
4085
Pablo Galindocd74e662019-06-01 18:08:04 +01004086 /* Copy all positional arguments into local variables */
4087 if (argcount > co->co_argcount) {
4088 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02004089 }
4090 else {
4091 n = argcount;
4092 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004093 for (j = 0; j < n; j++) {
4094 x = args[j];
4095 Py_INCREF(x);
4096 SETLOCAL(j, x);
4097 }
4098
Victor Stinnerc7020012016-08-16 23:40:29 +02004099 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004100 if (co->co_flags & CO_VARARGS) {
Sergey Fedoseev234531b2019-02-25 21:59:12 +05004101 u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02004102 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004103 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004104 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004105 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004106 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004107
Serhiy Storchakab7281052016-09-12 00:52:40 +03004108 /* Handle keyword arguments passed as two strided arrays */
4109 kwcount *= kwstep;
4110 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004111 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03004112 PyObject *keyword = kwnames[i];
4113 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02004114 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004115
Benjamin Petersonb204a422011-06-05 22:04:07 -05004116 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004117 _PyErr_Format(tstate, PyExc_TypeError,
4118 "%U() keywords must be strings",
4119 co->co_name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004120 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004121 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004122
Benjamin Petersonb204a422011-06-05 22:04:07 -05004123 /* Speed hack: do raw pointer compares. As names are
4124 normally interned this should almost always hit. */
4125 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004126 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004127 PyObject *name = co_varnames[j];
4128 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004129 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004130 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004131 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004132
Benjamin Petersonb204a422011-06-05 22:04:07 -05004133 /* Slow fallback, just in case */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004134 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004135 PyObject *name = co_varnames[j];
4136 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
4137 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004138 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004139 }
4140 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004141 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004142 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004143 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004144
Victor Stinner231d1f32017-01-11 02:12:06 +01004145 assert(j >= total_args);
4146 if (kwdict == NULL) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004147
Victor Stinner438a12d2019-05-24 17:01:38 +02004148 if (co->co_posonlyargcount
4149 && positional_only_passed_as_keyword(tstate, co,
4150 kwcount, kwnames))
4151 {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004152 goto fail;
4153 }
4154
Victor Stinner438a12d2019-05-24 17:01:38 +02004155 _PyErr_Format(tstate, PyExc_TypeError,
4156 "%U() got an unexpected keyword argument '%S'",
4157 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004158 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004159 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004160
Christian Heimes0bd447f2013-07-20 14:48:10 +02004161 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4162 goto fail;
4163 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004164 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004165
Benjamin Petersonb204a422011-06-05 22:04:07 -05004166 kw_found:
4167 if (GETLOCAL(j) != NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004168 _PyErr_Format(tstate, PyExc_TypeError,
4169 "%U() got multiple values for argument '%S'",
4170 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004171 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004172 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004173 Py_INCREF(value);
4174 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004175 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004176
4177 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004178 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004179 too_many_positional(tstate, co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004180 goto fail;
4181 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004182
4183 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004184 if (argcount < co->co_argcount) {
4185 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004186 Py_ssize_t missing = 0;
4187 for (i = argcount; i < m; i++) {
4188 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004189 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004190 }
4191 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004192 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004193 missing_arguments(tstate, co, missing, defcount, fastlocals);
Benjamin Petersone109c702011-06-24 09:37:26 -05004194 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004195 }
4196 if (n > m)
4197 i = n - m;
4198 else
4199 i = 0;
4200 for (; i < defcount; i++) {
4201 if (GETLOCAL(m+i) == NULL) {
4202 PyObject *def = defs[i];
4203 Py_INCREF(def);
4204 SETLOCAL(m+i, def);
4205 }
4206 }
4207 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004208
4209 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004210 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004211 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004212 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004213 PyObject *name;
4214 if (GETLOCAL(i) != NULL)
4215 continue;
4216 name = PyTuple_GET_ITEM(co->co_varnames, i);
4217 if (kwdefs != NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004218 PyObject *def = PyDict_GetItemWithError(kwdefs, name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004219 if (def) {
4220 Py_INCREF(def);
4221 SETLOCAL(i, def);
4222 continue;
4223 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004224 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004225 goto fail;
4226 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004227 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004228 missing++;
4229 }
4230 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004231 missing_arguments(tstate, co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004232 goto fail;
4233 }
4234 }
4235
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004236 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004237 vars into frame. */
4238 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004239 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004240 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004241 /* Possibly account for the cell variable being an argument. */
4242 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004243 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004244 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004245 /* Clear the local copy. */
4246 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004247 }
4248 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004249 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004250 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004251 if (c == NULL)
4252 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004253 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004254 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004255
4256 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004257 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4258 PyObject *o = PyTuple_GET_ITEM(closure, i);
4259 Py_INCREF(o);
4260 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004261 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004262
Yury Selivanoveb636452016-09-08 22:01:51 -07004263 /* Handle generator/coroutine/asynchronous generator */
4264 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004265 PyObject *gen;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004266 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004268 /* Don't need to keep the reference to f_back, it will be set
4269 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004270 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004272 /* Create a new generator that owns the ready to run frame
4273 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004274 if (is_coro) {
4275 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004276 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4277 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004278 } else {
4279 gen = PyGen_NewWithQualName(f, name, qualname);
4280 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004281 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004282 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004283 }
INADA Naoki9c157762016-12-26 18:52:46 +09004284
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004285 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004286
Yury Selivanov75445082015-05-11 22:57:16 -04004287 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004288 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004289
Victor Stinnerb9e68122019-11-14 12:20:46 +01004290 retval = _PyEval_EvalFrame(tstate, f, 0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004291
Thomas Woutersce272b62007-09-19 21:19:28 +00004292fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004294 /* decref'ing the frame can cause __del__ methods to get invoked,
4295 which can call back into Python. While we're done with the
4296 current Python frame (f), the associated C stack is still in use,
4297 so recursion_depth must be boosted for the duration.
4298 */
INADA Naoki5a625d02016-12-24 20:19:08 +09004299 if (Py_REFCNT(f) > 1) {
4300 Py_DECREF(f);
4301 _PyObject_GC_TRACK(f);
4302 }
4303 else {
4304 ++tstate->recursion_depth;
4305 Py_DECREF(f);
4306 --tstate->recursion_depth;
4307 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004308 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004309}
4310
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004311
4312PyObject *
4313_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
4314 PyObject *const *args, Py_ssize_t argcount,
4315 PyObject *const *kwnames, PyObject *const *kwargs,
4316 Py_ssize_t kwcount, int kwstep,
4317 PyObject *const *defs, Py_ssize_t defcount,
4318 PyObject *kwdefs, PyObject *closure,
4319 PyObject *name, PyObject *qualname)
4320{
4321 PyThreadState *tstate = _PyThreadState_GET();
4322 return _PyEval_EvalCode(tstate, _co, globals, locals,
4323 args, argcount,
4324 kwnames, kwargs,
4325 kwcount, kwstep,
4326 defs, defcount,
4327 kwdefs, closure,
4328 name, qualname);
4329}
4330
Victor Stinner40ee3012014-06-16 15:59:28 +02004331PyObject *
4332PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004333 PyObject *const *args, int argcount,
4334 PyObject *const *kws, int kwcount,
4335 PyObject *const *defs, int defcount,
4336 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004337{
4338 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004339 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004340 kws, kws != NULL ? kws + 1 : NULL,
4341 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004342 defs, defcount,
4343 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004344 NULL, NULL);
4345}
Tim Peters5ca576e2001-06-18 22:08:13 +00004346
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004347static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004348special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004349{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004350 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004351 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02004352 if (res == NULL && !_PyErr_Occurred(tstate)) {
4353 _PyErr_SetObject(tstate, PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004354 return NULL;
4355 }
4356 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004357}
4358
4359
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004360/* Logic for the raise statement (too complicated for inlining).
4361 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004362static int
Victor Stinner09532fe2019-05-10 23:39:09 +02004363do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004364{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004365 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004367 if (exc == NULL) {
4368 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01004369 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004370 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004371 type = exc_info->exc_type;
4372 value = exc_info->exc_value;
4373 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004374 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004375 _PyErr_SetString(tstate, PyExc_RuntimeError,
4376 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004377 return 0;
4378 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004379 Py_XINCREF(type);
4380 Py_XINCREF(value);
4381 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02004382 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004383 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004384 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004386 /* We support the following forms of raise:
4387 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004388 raise <instance>
4389 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004391 if (PyExceptionClass_Check(exc)) {
4392 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004393 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004394 if (value == NULL)
4395 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004396 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004397 _PyErr_Format(tstate, PyExc_TypeError,
4398 "calling %R should have returned an instance of "
4399 "BaseException, not %R",
4400 type, Py_TYPE(value));
4401 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004402 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004403 }
4404 else if (PyExceptionInstance_Check(exc)) {
4405 value = exc;
4406 type = PyExceptionInstance_Class(exc);
4407 Py_INCREF(type);
4408 }
4409 else {
4410 /* Not something you can raise. You get an exception
4411 anyway, just not what you specified :-) */
4412 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02004413 _PyErr_SetString(tstate, PyExc_TypeError,
4414 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004415 goto raise_error;
4416 }
Collin Winter828f04a2007-08-31 00:04:24 +00004417
Serhiy Storchakac0191582016-09-27 11:37:10 +03004418 assert(type != NULL);
4419 assert(value != NULL);
4420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004421 if (cause) {
4422 PyObject *fixed_cause;
4423 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004424 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004425 if (fixed_cause == NULL)
4426 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004427 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004428 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004429 else if (PyExceptionInstance_Check(cause)) {
4430 fixed_cause = cause;
4431 }
4432 else if (cause == Py_None) {
4433 Py_DECREF(cause);
4434 fixed_cause = NULL;
4435 }
4436 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004437 _PyErr_SetString(tstate, PyExc_TypeError,
4438 "exception causes must derive from "
4439 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004440 goto raise_error;
4441 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004442 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004443 }
Collin Winter828f04a2007-08-31 00:04:24 +00004444
Victor Stinner438a12d2019-05-24 17:01:38 +02004445 _PyErr_SetObject(tstate, type, value);
Victor Stinner61f4db82020-01-28 03:37:45 +01004446 /* _PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004447 Py_DECREF(value);
4448 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004449 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004450
4451raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004452 Py_XDECREF(value);
4453 Py_XDECREF(type);
4454 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004455 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004456}
4457
Tim Petersd6d010b2001-06-21 02:49:55 +00004458/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004459 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004460
Guido van Rossum0368b722007-05-11 16:50:42 +00004461 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4462 with a variable target.
4463*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004464
Barry Warsawe42b18f1997-08-25 22:13:04 +00004465static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004466unpack_iterable(PyThreadState *tstate, PyObject *v,
4467 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004468{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004469 int i = 0, j = 0;
4470 Py_ssize_t ll = 0;
4471 PyObject *it; /* iter(v) */
4472 PyObject *w;
4473 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004475 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004477 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004478 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004479 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01004480 Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v))
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004481 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004482 _PyErr_Format(tstate, PyExc_TypeError,
4483 "cannot unpack non-iterable %.200s object",
Victor Stinnera102ed72020-02-07 02:24:48 +01004484 Py_TYPE(v)->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004485 }
4486 return 0;
4487 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004489 for (; i < argcnt; i++) {
4490 w = PyIter_Next(it);
4491 if (w == NULL) {
4492 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004493 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04004494 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004495 _PyErr_Format(tstate, PyExc_ValueError,
4496 "not enough values to unpack "
4497 "(expected %d, got %d)",
4498 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004499 }
4500 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004501 _PyErr_Format(tstate, PyExc_ValueError,
4502 "not enough values to unpack "
4503 "(expected at least %d, got %d)",
4504 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004505 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004506 }
4507 goto Error;
4508 }
4509 *--sp = w;
4510 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004512 if (argcntafter == -1) {
4513 /* We better have exhausted the iterator now. */
4514 w = PyIter_Next(it);
4515 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004516 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004517 goto Error;
4518 Py_DECREF(it);
4519 return 1;
4520 }
4521 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02004522 _PyErr_Format(tstate, PyExc_ValueError,
4523 "too many values to unpack (expected %d)",
4524 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004525 goto Error;
4526 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004527
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004528 l = PySequence_List(it);
4529 if (l == NULL)
4530 goto Error;
4531 *--sp = l;
4532 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004534 ll = PyList_GET_SIZE(l);
4535 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004536 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04004537 "not enough values to unpack (expected at least %d, got %zd)",
4538 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004539 goto Error;
4540 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004542 /* Pop the "after-variable" args off the list. */
4543 for (j = argcntafter; j > 0; j--, i++) {
4544 *--sp = PyList_GET_ITEM(l, ll - j);
4545 }
4546 /* Resize the list. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +01004547 Py_SET_SIZE(l, ll - argcntafter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004548 Py_DECREF(it);
4549 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004550
Tim Petersd6d010b2001-06-21 02:49:55 +00004551Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004552 for (; i > 0; i--, sp++)
4553 Py_DECREF(*sp);
4554 Py_XDECREF(it);
4555 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004556}
4557
4558
Guido van Rossum96a42c81992-01-12 02:29:51 +00004559#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004560static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004561prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004562{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004563 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02004564 if (PyObject_Print(v, stdout, 0) != 0) {
4565 /* Don't know what else to do */
4566 _PyErr_Clear(tstate);
4567 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004568 printf("\n");
4569 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004570}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004571#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004572
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004573static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004574call_exc_trace(Py_tracefunc func, PyObject *self,
4575 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004576{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004577 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004578 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004579 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004580 if (value == NULL) {
4581 value = Py_None;
4582 Py_INCREF(value);
4583 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004584 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01004585 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004586 arg = PyTuple_Pack(3, type, value, traceback);
4587 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004588 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004589 return;
4590 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004591 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004592 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02004593 if (err == 0) {
4594 _PyErr_Restore(tstate, type, value, orig_traceback);
4595 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004596 else {
4597 Py_XDECREF(type);
4598 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004599 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004600 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004601}
4602
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004603static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004604call_trace_protected(Py_tracefunc func, PyObject *obj,
4605 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004606 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004607{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004608 PyObject *type, *value, *traceback;
4609 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004610 _PyErr_Fetch(tstate, &type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004611 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004612 if (err == 0)
4613 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004614 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004615 return 0;
4616 }
4617 else {
4618 Py_XDECREF(type);
4619 Py_XDECREF(value);
4620 Py_XDECREF(traceback);
4621 return -1;
4622 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004623}
4624
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004625static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004626call_trace(Py_tracefunc func, PyObject *obj,
4627 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004628 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004629{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004630 int result;
4631 if (tstate->tracing)
4632 return 0;
4633 tstate->tracing++;
4634 tstate->use_tracing = 0;
4635 result = func(obj, frame, what, arg);
4636 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4637 || (tstate->c_profilefunc != NULL));
4638 tstate->tracing--;
4639 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004640}
4641
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004642PyObject *
4643_PyEval_CallTracing(PyObject *func, PyObject *args)
4644{
Victor Stinner50b48572018-11-01 01:51:40 +01004645 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004646 int save_tracing = tstate->tracing;
4647 int save_use_tracing = tstate->use_tracing;
4648 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004649
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004650 tstate->tracing = 0;
4651 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4652 || (tstate->c_profilefunc != NULL));
4653 result = PyObject_Call(func, args, NULL);
4654 tstate->tracing = save_tracing;
4655 tstate->use_tracing = save_use_tracing;
4656 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004657}
4658
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004659/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004660static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004661maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004662 PyThreadState *tstate, PyFrameObject *frame,
4663 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004664{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004665 int result = 0;
4666 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004668 /* If the last instruction executed isn't in the current
4669 instruction window, reset the window.
4670 */
4671 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4672 PyAddrPair bounds;
4673 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4674 &bounds);
4675 *instr_lb = bounds.ap_lower;
4676 *instr_ub = bounds.ap_upper;
4677 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004678 /* If the last instruction falls at the start of a line or if it
4679 represents a jump backwards, update the frame's line number and
4680 then call the trace function if we're tracing source lines.
4681 */
4682 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004683 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004684 if (frame->f_trace_lines) {
4685 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4686 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004687 }
George King20faa682017-10-18 17:44:22 -07004688 /* Always emit an opcode event if we're tracing all opcodes. */
4689 if (frame->f_trace_opcodes) {
4690 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4691 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004692 *instr_prev = frame->f_lasti;
4693 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004694}
4695
Victor Stinner309d7cc2020-03-13 16:39:12 +01004696int
4697_PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
4698{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004699 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01004700 /* The caller must hold the GIL */
4701 assert(PyGILState_Check());
4702
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004703 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01004704 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004705 PyThreadState *current_tstate = _PyThreadState_GET();
4706 if (_PySys_Audit(current_tstate, "sys.setprofile", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01004707 return -1;
4708 }
4709
4710 PyObject *profileobj = tstate->c_profileobj;
4711
4712 tstate->c_profilefunc = NULL;
4713 tstate->c_profileobj = NULL;
4714 /* Must make sure that tracing is not ignored if 'profileobj' is freed */
4715 tstate->use_tracing = tstate->c_tracefunc != NULL;
4716 Py_XDECREF(profileobj);
4717
4718 Py_XINCREF(arg);
4719 tstate->c_profileobj = arg;
4720 tstate->c_profilefunc = func;
4721
4722 /* Flag that tracing or profiling is turned on */
4723 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
4724 return 0;
4725}
4726
Fred Drake5755ce62001-06-27 19:19:46 +00004727void
4728PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004729{
Victor Stinner309d7cc2020-03-13 16:39:12 +01004730 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01004731 if (_PyEval_SetProfile(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004732 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01004733 _PyErr_WriteUnraisableMsg("in PyEval_SetProfile", NULL);
4734 }
Victor Stinner309d7cc2020-03-13 16:39:12 +01004735}
4736
4737int
4738_PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
4739{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004740 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01004741 /* The caller must hold the GIL */
4742 assert(PyGILState_Check());
4743
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004744 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01004745 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004746 PyThreadState *current_tstate = _PyThreadState_GET();
4747 if (_PySys_Audit(current_tstate, "sys.settrace", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01004748 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004749 }
4750
Victor Stinnerda2914d2020-03-20 09:29:08 +01004751 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinner309d7cc2020-03-13 16:39:12 +01004752 PyObject *traceobj = tstate->c_traceobj;
Victor Stinnerda2914d2020-03-20 09:29:08 +01004753 ceval2->tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
Victor Stinner309d7cc2020-03-13 16:39:12 +01004754
4755 tstate->c_tracefunc = NULL;
4756 tstate->c_traceobj = NULL;
4757 /* Must make sure that profiling is not ignored if 'traceobj' is freed */
4758 tstate->use_tracing = (tstate->c_profilefunc != NULL);
4759 Py_XDECREF(traceobj);
4760
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004761 Py_XINCREF(arg);
Victor Stinner309d7cc2020-03-13 16:39:12 +01004762 tstate->c_traceobj = arg;
4763 tstate->c_tracefunc = func;
4764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004765 /* Flag that tracing or profiling is turned on */
Victor Stinner309d7cc2020-03-13 16:39:12 +01004766 tstate->use_tracing = ((func != NULL)
4767 || (tstate->c_profilefunc != NULL));
4768
4769 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +00004770}
4771
4772void
4773PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4774{
Victor Stinner309d7cc2020-03-13 16:39:12 +01004775 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01004776 if (_PyEval_SetTrace(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004777 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01004778 _PyErr_WriteUnraisableMsg("in PyEval_SetTrace", NULL);
4779 }
Fred Draked0838392001-06-16 21:02:31 +00004780}
4781
Victor Stinner309d7cc2020-03-13 16:39:12 +01004782
Yury Selivanov75445082015-05-11 22:57:16 -04004783void
Victor Stinner838f2642019-06-13 22:41:23 +02004784_PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004785{
4786 assert(new_depth >= 0);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004787 tstate->coroutine_origin_tracking_depth = new_depth;
4788}
4789
4790int
4791_PyEval_GetCoroutineOriginTrackingDepth(void)
4792{
Victor Stinner50b48572018-11-01 01:51:40 +01004793 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004794 return tstate->coroutine_origin_tracking_depth;
4795}
4796
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004797int
Yury Selivanoveb636452016-09-08 22:01:51 -07004798_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4799{
Victor Stinner50b48572018-11-01 01:51:40 +01004800 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004801
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004802 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_firstiter", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004803 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004804 }
4805
Yury Selivanoveb636452016-09-08 22:01:51 -07004806 Py_XINCREF(firstiter);
4807 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004808 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07004809}
4810
4811PyObject *
4812_PyEval_GetAsyncGenFirstiter(void)
4813{
Victor Stinner50b48572018-11-01 01:51:40 +01004814 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004815 return tstate->async_gen_firstiter;
4816}
4817
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004818int
Yury Selivanoveb636452016-09-08 22:01:51 -07004819_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4820{
Victor Stinner50b48572018-11-01 01:51:40 +01004821 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004822
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004823 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_finalizer", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004824 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004825 }
4826
Yury Selivanoveb636452016-09-08 22:01:51 -07004827 Py_XINCREF(finalizer);
4828 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004829 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07004830}
4831
4832PyObject *
4833_PyEval_GetAsyncGenFinalizer(void)
4834{
Victor Stinner50b48572018-11-01 01:51:40 +01004835 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004836 return tstate->async_gen_finalizer;
4837}
4838
Victor Stinner438a12d2019-05-24 17:01:38 +02004839PyFrameObject *
4840PyEval_GetFrame(void)
4841{
4842 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004843 return tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02004844}
4845
Guido van Rossumb209a111997-04-29 18:18:01 +00004846PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004847PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004848{
Victor Stinner438a12d2019-05-24 17:01:38 +02004849 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004850 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004851 if (current_frame == NULL)
Victor Stinner438a12d2019-05-24 17:01:38 +02004852 return tstate->interp->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004853 else
4854 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004855}
4856
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004857/* Convenience function to get a builtin from its name */
4858PyObject *
4859_PyEval_GetBuiltinId(_Py_Identifier *name)
4860{
Victor Stinner438a12d2019-05-24 17:01:38 +02004861 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004862 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4863 if (attr) {
4864 Py_INCREF(attr);
4865 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004866 else if (!_PyErr_Occurred(tstate)) {
4867 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004868 }
4869 return attr;
4870}
4871
Guido van Rossumb209a111997-04-29 18:18:01 +00004872PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004873PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004874{
Victor Stinner438a12d2019-05-24 17:01:38 +02004875 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004876 PyFrameObject *current_frame = tstate->frame;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004877 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004878 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004879 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004880 }
4881
Victor Stinner438a12d2019-05-24 17:01:38 +02004882 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01004883 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004884 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004885
4886 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004887 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004888}
4889
Guido van Rossumb209a111997-04-29 18:18:01 +00004890PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004891PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004892{
Victor Stinner438a12d2019-05-24 17:01:38 +02004893 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004894 PyFrameObject *current_frame = tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02004895 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004896 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004897 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004898
4899 assert(current_frame->f_globals != NULL);
4900 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004901}
4902
Guido van Rossum6135a871995-01-09 17:53:26 +00004903int
Tim Peters5ba58662001-07-16 02:29:45 +00004904PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004905{
Victor Stinner438a12d2019-05-24 17:01:38 +02004906 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004907 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004908 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004909
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004910 if (current_frame != NULL) {
4911 const int codeflags = current_frame->f_code->co_flags;
4912 const int compilerflags = codeflags & PyCF_MASK;
4913 if (compilerflags) {
4914 result = 1;
4915 cf->cf_flags |= compilerflags;
4916 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004917#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004918 if (codeflags & CO_GENERATOR_ALLOWED) {
4919 result = 1;
4920 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4921 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004922#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004923 }
4924 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004925}
4926
Guido van Rossum3f5da241990-12-20 15:06:42 +00004927
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004928const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004929PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004930{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004931 if (PyMethod_Check(func))
4932 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4933 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004934 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004935 else if (PyCFunction_Check(func))
4936 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4937 else
Victor Stinnera102ed72020-02-07 02:24:48 +01004938 return Py_TYPE(func)->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004939}
4940
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004941const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004942PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004943{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004944 if (PyMethod_Check(func))
4945 return "()";
4946 else if (PyFunction_Check(func))
4947 return "()";
4948 else if (PyCFunction_Check(func))
4949 return "()";
4950 else
4951 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004952}
4953
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004954#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004955if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004956 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4957 tstate, tstate->frame, \
4958 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004959 x = NULL; \
4960 } \
4961 else { \
4962 x = call; \
4963 if (tstate->c_profilefunc != NULL) { \
4964 if (x == NULL) { \
4965 call_trace_protected(tstate->c_profilefunc, \
4966 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004967 tstate, tstate->frame, \
4968 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004969 /* XXX should pass (type, value, tb) */ \
4970 } else { \
4971 if (call_trace(tstate->c_profilefunc, \
4972 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004973 tstate, tstate->frame, \
4974 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004975 Py_DECREF(x); \
4976 x = NULL; \
4977 } \
4978 } \
4979 } \
4980 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004981} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004982 x = call; \
4983 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004984
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004985
4986static PyObject *
4987trace_call_function(PyThreadState *tstate,
4988 PyObject *func,
4989 PyObject **args, Py_ssize_t nargs,
4990 PyObject *kwnames)
4991{
4992 PyObject *x;
4993 if (PyCFunction_Check(func)) {
Petr Viktorinffd97532020-02-11 17:46:57 +01004994 C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004995 return x;
4996 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06004997 else if (Py_IS_TYPE(func, &PyMethodDescr_Type) && nargs > 0) {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004998 /* We need to create a temporary bound method as argument
4999 for profiling.
5000
5001 If nargs == 0, then this cannot work because we have no
5002 "self". In any case, the call itself would raise
5003 TypeError (foo needs an argument), so we just skip
5004 profiling. */
5005 PyObject *self = args[0];
5006 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5007 if (func == NULL) {
5008 return NULL;
5009 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005010 C_TRACE(x, PyObject_Vectorcall(func,
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02005011 args+1, nargs-1,
5012 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005013 Py_DECREF(func);
5014 return x;
5015 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005016 return PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005017}
5018
Victor Stinner415c5102017-01-11 00:54:57 +01005019/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
5020 to reduce the stack consumption. */
5021Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Victor Stinner09532fe2019-05-10 23:39:09 +02005022call_function(PyThreadState *tstate, PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005023{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005024 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005025 PyObject *func = *pfunc;
5026 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07005027 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
5028 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09005029 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005030
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005031 if (tstate->use_tracing) {
5032 x = trace_call_function(tstate, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09005033 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01005034 else {
Petr Viktorinffd97532020-02-11 17:46:57 +01005035 x = PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005036 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00005037
Victor Stinner438a12d2019-05-24 17:01:38 +02005038 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005039
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01005040 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005041 while ((*pp_stack) > pfunc) {
5042 w = EXT_POP(*pp_stack);
5043 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005044 }
Victor Stinnerace47d72013-07-18 01:41:08 +02005045
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005046 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005047}
5048
Jeremy Hylton52820442001-01-03 23:52:36 +00005049static PyObject *
Victor Stinner09532fe2019-05-10 23:39:09 +02005050do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005051{
jdemeyere89de732018-09-19 12:06:20 +02005052 PyObject *result;
5053
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005054 if (PyCFunction_Check(func)) {
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +02005055 C_TRACE(result, PyObject_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005056 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005057 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005058 else if (Py_IS_TYPE(func, &PyMethodDescr_Type)) {
jdemeyere89de732018-09-19 12:06:20 +02005059 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
5060 if (nargs > 0 && tstate->use_tracing) {
5061 /* We need to create a temporary bound method as argument
5062 for profiling.
5063
5064 If nargs == 0, then this cannot work because we have no
5065 "self". In any case, the call itself would raise
5066 TypeError (foo needs an argument), so we just skip
5067 profiling. */
5068 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
5069 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5070 if (func == NULL) {
5071 return NULL;
5072 }
5073
Victor Stinner4d231bc2019-11-14 13:36:21 +01005074 C_TRACE(result, _PyObject_FastCallDictTstate(
5075 tstate, func,
5076 &_PyTuple_ITEMS(callargs)[1],
5077 nargs - 1,
5078 kwdict));
jdemeyere89de732018-09-19 12:06:20 +02005079 Py_DECREF(func);
5080 return result;
5081 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005082 }
jdemeyere89de732018-09-19 12:06:20 +02005083 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00005084}
5085
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005086/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005087 nb_index slot defined, and store in *pi.
5088 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08005089 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00005090 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005091*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005092int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005093_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005094{
Victor Stinner438a12d2019-05-24 17:01:38 +02005095 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005096 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005097 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005098 if (_PyIndex_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005099 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005100 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005101 return 0;
5102 }
5103 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005104 _PyErr_SetString(tstate, PyExc_TypeError,
5105 "slice indices must be integers or "
5106 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005107 return 0;
5108 }
5109 *pi = x;
5110 }
5111 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005112}
5113
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005114int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005115_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005116{
Victor Stinner438a12d2019-05-24 17:01:38 +02005117 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005118 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005119 if (_PyIndex_Check(v)) {
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005120 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005121 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005122 return 0;
5123 }
5124 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005125 _PyErr_SetString(tstate, PyExc_TypeError,
5126 "slice indices must be integers or "
5127 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005128 return 0;
5129 }
5130 *pi = x;
5131 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005132}
5133
Thomas Wouters52152252000-08-17 22:55:00 +00005134static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005135import_name(PyThreadState *tstate, PyFrameObject *f,
5136 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005137{
5138 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005139 PyObject *import_func, *res;
5140 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005141
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005142 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005143 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005144 if (!_PyErr_Occurred(tstate)) {
5145 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005146 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005147 return NULL;
5148 }
5149
5150 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005151 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005152 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02005153 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005154 return NULL;
5155 }
5156 res = PyImport_ImportModuleLevelObject(
5157 name,
5158 f->f_globals,
5159 f->f_locals == NULL ? Py_None : f->f_locals,
5160 fromlist,
5161 ilevel);
5162 return res;
5163 }
5164
5165 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005166
5167 stack[0] = name;
5168 stack[1] = f->f_globals;
5169 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5170 stack[3] = fromlist;
5171 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005172 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005173 Py_DECREF(import_func);
5174 return res;
5175}
5176
5177static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005178import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005179{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005180 PyObject *x;
Xiang Zhang4830f582017-03-21 11:13:42 +08005181 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005182
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005183 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005184 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005185 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005186 /* Issue #17636: in case this failed because of a circular relative
5187 import, try to fallback on reading the module directly from
5188 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02005189 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005190 if (pkgname == NULL) {
5191 goto error;
5192 }
Oren Milman6db70332017-09-19 14:23:01 +03005193 if (!PyUnicode_Check(pkgname)) {
5194 Py_CLEAR(pkgname);
5195 goto error;
5196 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005197 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005198 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005199 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005200 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005201 }
Eric Snow3f9eee62017-09-15 16:35:20 -06005202 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005203 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02005204 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07005205 goto error;
5206 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005207 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005208 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005209 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005210 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005211 if (pkgname == NULL) {
5212 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5213 if (pkgname_or_unknown == NULL) {
5214 Py_XDECREF(pkgpath);
5215 return NULL;
5216 }
5217 } else {
5218 pkgname_or_unknown = pkgname;
5219 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005220
5221 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005222 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08005223 errmsg = PyUnicode_FromFormat(
5224 "cannot import name %R from %R (unknown location)",
5225 name, pkgname_or_unknown
5226 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005227 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005228 PyErr_SetImportError(errmsg, pkgname, NULL);
5229 }
5230 else {
Anthony Sottile65366bc2019-09-09 08:17:50 -07005231 _Py_IDENTIFIER(__spec__);
5232 PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
Anthony Sottile65366bc2019-09-09 08:17:50 -07005233 const char *fmt =
5234 _PyModuleSpec_IsInitializing(spec) ?
5235 "cannot import name %R from partially initialized module %R "
5236 "(most likely due to a circular import) (%S)" :
5237 "cannot import name %R from %R (%S)";
5238 Py_XDECREF(spec);
5239
5240 errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
Stefan Krah027b09c2019-03-25 21:50:58 +01005241 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005242 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005243 }
5244
Xiang Zhang4830f582017-03-21 11:13:42 +08005245 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005246 Py_XDECREF(pkgname_or_unknown);
5247 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005248 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005249}
Guido van Rossumac7be682001-01-17 15:42:30 +00005250
Thomas Wouters52152252000-08-17 22:55:00 +00005251static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005252import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00005253{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005254 _Py_IDENTIFIER(__all__);
5255 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005256 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005257 int skip_leading_underscores = 0;
5258 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005259
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005260 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5261 return -1; /* Unexpected error */
5262 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005263 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005264 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5265 return -1;
5266 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005267 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005268 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005269 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005270 return -1;
5271 }
5272 all = PyMapping_Keys(dict);
5273 Py_DECREF(dict);
5274 if (all == NULL)
5275 return -1;
5276 skip_leading_underscores = 1;
5277 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005279 for (pos = 0, err = 0; ; pos++) {
5280 name = PySequence_GetItem(all, pos);
5281 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005282 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005283 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02005284 }
5285 else {
5286 _PyErr_Clear(tstate);
5287 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005288 break;
5289 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005290 if (!PyUnicode_Check(name)) {
5291 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5292 if (modname == NULL) {
5293 Py_DECREF(name);
5294 err = -1;
5295 break;
5296 }
5297 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005298 _PyErr_Format(tstate, PyExc_TypeError,
5299 "module __name__ must be a string, not %.100s",
5300 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005301 }
5302 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005303 _PyErr_Format(tstate, PyExc_TypeError,
5304 "%s in %U.%s must be str, not %.100s",
5305 skip_leading_underscores ? "Key" : "Item",
5306 modname,
5307 skip_leading_underscores ? "__dict__" : "__all__",
5308 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005309 }
5310 Py_DECREF(modname);
5311 Py_DECREF(name);
5312 err = -1;
5313 break;
5314 }
5315 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005316 if (PyUnicode_READY(name) == -1) {
5317 Py_DECREF(name);
5318 err = -1;
5319 break;
5320 }
5321 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5322 Py_DECREF(name);
5323 continue;
5324 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005325 }
5326 value = PyObject_GetAttr(v, name);
5327 if (value == NULL)
5328 err = -1;
5329 else if (PyDict_CheckExact(locals))
5330 err = PyDict_SetItem(locals, name, value);
5331 else
5332 err = PyObject_SetItem(locals, name, value);
5333 Py_DECREF(name);
5334 Py_XDECREF(value);
5335 if (err != 0)
5336 break;
5337 }
5338 Py_DECREF(all);
5339 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005340}
5341
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005342static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005343check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005344{
Victor Stinnera102ed72020-02-07 02:24:48 +01005345 if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005346 /* check_args_iterable() may be called with a live exception:
5347 * clear it to prevent calling _PyObject_FunctionStr() with an
5348 * exception set. */
Victor Stinner61f4db82020-01-28 03:37:45 +01005349 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005350 PyObject *funcstr = _PyObject_FunctionStr(func);
5351 if (funcstr != NULL) {
5352 _PyErr_Format(tstate, PyExc_TypeError,
5353 "%U argument after * must be an iterable, not %.200s",
5354 funcstr, Py_TYPE(args)->tp_name);
5355 Py_DECREF(funcstr);
5356 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005357 return -1;
5358 }
5359 return 0;
5360}
5361
5362static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005363format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005364{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005365 /* _PyDict_MergeEx raises attribute
5366 * error (percolated from an attempt
5367 * to get 'keys' attribute) instead of
5368 * a type error if its second argument
5369 * is not a mapping.
5370 */
Victor Stinner438a12d2019-05-24 17:01:38 +02005371 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005372 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005373 PyObject *funcstr = _PyObject_FunctionStr(func);
5374 if (funcstr != NULL) {
5375 _PyErr_Format(
5376 tstate, PyExc_TypeError,
5377 "%U argument after ** must be a mapping, not %.200s",
5378 funcstr, Py_TYPE(kwargs)->tp_name);
5379 Py_DECREF(funcstr);
5380 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005381 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005382 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005383 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02005384 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005385 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005386 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005387 PyObject *funcstr = _PyObject_FunctionStr(func);
5388 if (funcstr != NULL) {
5389 PyObject *key = PyTuple_GET_ITEM(val, 0);
5390 _PyErr_Format(
5391 tstate, PyExc_TypeError,
5392 "%U got multiple values for keyword argument '%S'",
5393 funcstr, key);
5394 Py_DECREF(funcstr);
5395 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005396 Py_XDECREF(exc);
5397 Py_XDECREF(val);
5398 Py_XDECREF(tb);
5399 }
5400 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005401 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005402 }
5403 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005404}
5405
Guido van Rossumac7be682001-01-17 15:42:30 +00005406static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005407format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
5408 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005409{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005410 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005412 if (!obj)
5413 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005414
Serhiy Storchaka06515832016-11-20 09:13:07 +02005415 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005416 if (!obj_str)
5417 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005418
Victor Stinner438a12d2019-05-24 17:01:38 +02005419 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005420}
Guido van Rossum950361c1997-01-24 13:49:28 +00005421
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005422static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005423format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005424{
5425 PyObject *name;
5426 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02005427 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005428 return;
5429 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5430 name = PyTuple_GET_ITEM(co->co_cellvars,
5431 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005432 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005433 PyExc_UnboundLocalError,
5434 UNBOUNDLOCAL_ERROR_MSG,
5435 name);
5436 } else {
5437 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5438 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02005439 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005440 UNBOUNDFREE_ERROR_MSG, name);
5441 }
5442}
5443
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005444static void
Mark Shannonfee55262019-11-21 09:11:43 +00005445format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevprevopcode, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005446{
5447 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5448 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005449 _PyErr_Format(tstate, PyExc_TypeError,
5450 "'async with' received an object from __aenter__ "
5451 "that does not implement __await__: %.100s",
5452 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005453 }
Mark Shannonfee55262019-11-21 09:11:43 +00005454 else if (prevopcode == WITH_EXCEPT_START || (prevopcode == CALL_FUNCTION && prevprevopcode == DUP_TOP)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005455 _PyErr_Format(tstate, PyExc_TypeError,
5456 "'async with' received an object from __aexit__ "
5457 "that does not implement __await__: %.100s",
5458 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005459 }
5460 }
5461}
5462
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005463static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005464unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005465 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005466{
5467 PyObject *res;
5468 if (Py_REFCNT(v) == 2) {
5469 /* In the common case, there are 2 references to the value
5470 * stored in 'variable' when the += is performed: one on the
5471 * value stack (in 'v') and one still stored in the
5472 * 'variable'. We try to delete the variable now to reduce
5473 * the refcnt to 1.
5474 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005475 int opcode, oparg;
5476 NEXTOPARG();
5477 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005478 case STORE_FAST:
5479 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005480 PyObject **fastlocals = f->f_localsplus;
5481 if (GETLOCAL(oparg) == v)
5482 SETLOCAL(oparg, NULL);
5483 break;
5484 }
5485 case STORE_DEREF:
5486 {
5487 PyObject **freevars = (f->f_localsplus +
5488 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005489 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005490 if (PyCell_GET(c) == v) {
5491 PyCell_SET(c, NULL);
5492 Py_DECREF(v);
5493 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005494 break;
5495 }
5496 case STORE_NAME:
5497 {
5498 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005499 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005500 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005501 if (locals && PyDict_CheckExact(locals)) {
5502 PyObject *w = PyDict_GetItemWithError(locals, name);
5503 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02005504 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005505 {
5506 Py_DECREF(v);
5507 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005508 }
5509 }
5510 break;
5511 }
5512 }
5513 }
5514 res = v;
5515 PyUnicode_Append(&res, w);
5516 return res;
5517}
5518
Guido van Rossum950361c1997-01-24 13:49:28 +00005519#ifdef DYNAMIC_EXECUTION_PROFILE
5520
Skip Montanarof118cb12001-10-15 20:51:38 +00005521static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005522getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005523{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005524 int i;
5525 PyObject *l = PyList_New(256);
5526 if (l == NULL) return NULL;
5527 for (i = 0; i < 256; i++) {
5528 PyObject *x = PyLong_FromLong(a[i]);
5529 if (x == NULL) {
5530 Py_DECREF(l);
5531 return NULL;
5532 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005533 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005534 }
5535 for (i = 0; i < 256; i++)
5536 a[i] = 0;
5537 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005538}
5539
5540PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005541_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005542{
5543#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005544 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005545#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005546 int i;
5547 PyObject *l = PyList_New(257);
5548 if (l == NULL) return NULL;
5549 for (i = 0; i < 257; i++) {
5550 PyObject *x = getarray(dxpairs[i]);
5551 if (x == NULL) {
5552 Py_DECREF(l);
5553 return NULL;
5554 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005555 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005556 }
5557 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005558#endif
5559}
5560
5561#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005562
5563Py_ssize_t
5564_PyEval_RequestCodeExtraIndex(freefunc free)
5565{
Victor Stinner81a7be32020-04-14 15:14:01 +02005566 PyInterpreterState *interp = _PyInterpreterState_GET();
Brett Cannon5c4de282016-09-07 11:16:41 -07005567 Py_ssize_t new_index;
5568
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005569 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005570 return -1;
5571 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005572 new_index = interp->co_extra_user_count++;
5573 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005574 return new_index;
5575}
Łukasz Langaa785c872016-09-09 17:37:37 -07005576
5577static void
5578dtrace_function_entry(PyFrameObject *f)
5579{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005580 const char *filename;
5581 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005582 int lineno;
5583
5584 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5585 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5586 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5587
Andy Lestere6be9b52020-02-11 20:28:35 -06005588 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005589}
5590
5591static void
5592dtrace_function_return(PyFrameObject *f)
5593{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005594 const char *filename;
5595 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005596 int lineno;
5597
5598 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5599 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5600 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5601
Andy Lestere6be9b52020-02-11 20:28:35 -06005602 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005603}
5604
5605/* DTrace equivalent of maybe_call_line_trace. */
5606static void
5607maybe_dtrace_line(PyFrameObject *frame,
5608 int *instr_lb, int *instr_ub, int *instr_prev)
5609{
5610 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005611 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005612
5613 /* If the last instruction executed isn't in the current
5614 instruction window, reset the window.
5615 */
5616 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5617 PyAddrPair bounds;
5618 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5619 &bounds);
5620 *instr_lb = bounds.ap_lower;
5621 *instr_ub = bounds.ap_upper;
5622 }
5623 /* If the last instruction falls at the start of a line or if
5624 it represents a jump backwards, update the frame's line
5625 number and call the trace function. */
5626 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5627 frame->f_lineno = line;
5628 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5629 if (!co_filename)
5630 co_filename = "?";
5631 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5632 if (!co_name)
5633 co_name = "?";
Andy Lestere6be9b52020-02-11 20:28:35 -06005634 PyDTrace_LINE(co_filename, co_name, line);
Łukasz Langaa785c872016-09-09 17:37:37 -07005635 }
5636 *instr_prev = frame->f_lasti;
5637}
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005638
5639
5640/* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions
5641 for the limited API. */
5642
5643#undef Py_EnterRecursiveCall
5644
5645int Py_EnterRecursiveCall(const char *where)
5646{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005647 return _Py_EnterRecursiveCall_inline(where);
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005648}
5649
5650#undef Py_LeaveRecursiveCall
5651
5652void Py_LeaveRecursiveCall(void)
5653{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005654 _Py_LeaveRecursiveCall_inline();
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005655}