blob: 91e879e804204450251a59e4be0bccf012171222 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Thomas Wouters477c8d52006-05-27 19:21:47 +00009/* enable more aggressive intra-module optimizations, where available */
10#define PY_LOCAL_AGGRESSIVE
11
Guido van Rossumb209a111997-04-29 18:18:01 +000012#include "Python.h"
Victor Stinnere560f902020-04-14 18:30:41 +020013#include "pycore_abstract.h" // _PyIndex_Check()
Victor Stinner4d231bc2019-11-14 13:36:21 +010014#include "pycore_call.h"
Victor Stinner09532fe2019-05-10 23:39:09 +020015#include "pycore_ceval.h"
Inada Naoki91234a12019-06-03 21:30:58 +090016#include "pycore_code.h"
Victor Stinner111e4ee2020-03-09 21:24:14 +010017#include "pycore_initconfig.h"
Victor Stinnerbcda8f12018-11-21 22:27:47 +010018#include "pycore_object.h"
Victor Stinner438a12d2019-05-24 17:01:38 +020019#include "pycore_pyerrors.h"
20#include "pycore_pylifecycle.h"
Victor Stinnere560f902020-04-14 18:30:41 +020021#include "pycore_pymem.h" // _PyMem_IsPtrFreed()
22#include "pycore_pystate.h" // _PyInterpreterState_GET()
Victor Stinner1c1e68c2020-03-27 15:11:45 +010023#include "pycore_sysmodule.h"
Victor Stinnerec13b932018-11-25 23:56:17 +010024#include "pycore_tupleobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000025
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000026#include "code.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040027#include "dictobject.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000028#include "frameobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000029#include "opcode.h"
Łukasz Langaa785c872016-09-09 17:37:37 -070030#include "pydtrace.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040031#include "setobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000032
Guido van Rossumc6004111993-11-05 10:22:19 +000033#include <ctype.h>
34
Guido van Rossum408027e1996-12-30 16:17:54 +000035#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000036/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037#define LLTRACE 1 /* Low-level trace feature */
38#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000039#endif
40
Victor Stinner5c75f372019-04-17 23:02:26 +020041#if !defined(Py_BUILD_CORE)
42# error "ceval.c must be build with Py_BUILD_CORE define for best performance"
43#endif
44
Hai Shi46874c22020-01-30 17:20:25 -060045_Py_IDENTIFIER(__name__);
Guido van Rossum5b722181993-03-30 17:46:03 +000046
Guido van Rossum374a9221991-04-04 10:40:29 +000047/* Forward declarations */
Victor Stinner09532fe2019-05-10 23:39:09 +020048Py_LOCAL_INLINE(PyObject *) call_function(
49 PyThreadState *tstate, PyObject ***pp_stack,
50 Py_ssize_t oparg, PyObject *kwnames);
51static PyObject * do_call_core(
52 PyThreadState *tstate, PyObject *func,
53 PyObject *callargs, PyObject *kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +000054
Guido van Rossum0a066c01992-03-27 17:29:15 +000055#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000056static int lltrace;
Victor Stinner438a12d2019-05-24 17:01:38 +020057static int prtrace(PyThreadState *, PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000058#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010059static int call_trace(Py_tracefunc, PyObject *,
60 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000062static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010063 PyThreadState *, PyFrameObject *,
64 int, PyObject *);
65static void call_exc_trace(Py_tracefunc, PyObject *,
66 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000067static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Eric Snow2ebc5ce2017-09-07 23:51:28 -060068 PyThreadState *, PyFrameObject *,
69 int *, int *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070070static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *);
71static void dtrace_function_entry(PyFrameObject *);
72static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000073
Victor Stinner438a12d2019-05-24 17:01:38 +020074static PyObject * import_name(PyThreadState *, PyFrameObject *,
75 PyObject *, PyObject *, PyObject *);
76static PyObject * import_from(PyThreadState *, PyObject *, PyObject *);
77static int import_all_from(PyThreadState *, PyObject *, PyObject *);
78static void format_exc_check_arg(PyThreadState *, PyObject *, const char *, PyObject *);
79static void format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg);
80static PyObject * unicode_concatenate(PyThreadState *, PyObject *, PyObject *,
Serhiy Storchakaab874002016-09-11 13:48:15 +030081 PyFrameObject *, const _Py_CODEUNIT *);
Victor Stinner438a12d2019-05-24 17:01:38 +020082static PyObject * special_lookup(PyThreadState *, PyObject *, _Py_Identifier *);
83static int check_args_iterable(PyThreadState *, PyObject *func, PyObject *vararg);
84static void format_kwargs_error(PyThreadState *, PyObject *func, PyObject *kwargs);
Mark Shannonfee55262019-11-21 09:11:43 +000085static void format_awaitable_error(PyThreadState *, PyTypeObject *, int, int);
Guido van Rossum374a9221991-04-04 10:40:29 +000086
Paul Prescode68140d2000-08-30 20:25:01 +000087#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000089#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000091#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 "free variable '%.200s' referenced before assignment" \
93 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000094
Guido van Rossum950361c1997-01-24 13:49:28 +000095/* Dynamic execution profile */
96#ifdef DYNAMIC_EXECUTION_PROFILE
97#ifdef DXPAIRS
98static long dxpairs[257][256];
99#define dxp dxpairs[256]
100#else
101static long dxp[256];
102#endif
103#endif
104
Inada Naoki91234a12019-06-03 21:30:58 +0900105/* per opcode cache */
Inada Naokieddef862019-06-04 07:38:10 +0900106#ifdef Py_DEBUG
107// --with-pydebug is used to find memory leak. opcache makes it harder.
108// So we disable opcache when Py_DEBUG is defined.
109// See bpo-37146
110#define OPCACHE_MIN_RUNS 0 /* disable opcache */
111#else
Inada Naoki91234a12019-06-03 21:30:58 +0900112#define OPCACHE_MIN_RUNS 1024 /* create opcache when code executed this time */
Inada Naokieddef862019-06-04 07:38:10 +0900113#endif
Inada Naoki91234a12019-06-03 21:30:58 +0900114#define OPCACHE_STATS 0 /* Enable stats */
115
116#if OPCACHE_STATS
117static size_t opcache_code_objects = 0;
118static size_t opcache_code_objects_extra_mem = 0;
119
120static size_t opcache_global_opts = 0;
121static size_t opcache_global_hits = 0;
122static size_t opcache_global_misses = 0;
123#endif
124
Victor Stinner5a3a71d2020-03-19 17:40:12 +0100125
Victor Stinnerda2914d2020-03-20 09:29:08 +0100126#ifndef NDEBUG
127/* Ensure that tstate is valid: sanity check for PyEval_AcquireThread() and
128 PyEval_RestoreThread(). Detect if tstate memory was freed. It can happen
129 when a thread continues to run after Python finalization, especially
130 daemon threads. */
131static int
132is_tstate_valid(PyThreadState *tstate)
133{
134 assert(!_PyMem_IsPtrFreed(tstate));
135 assert(!_PyMem_IsPtrFreed(tstate->interp));
136 return 1;
137}
138#endif
139
140
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000141/* This can set eval_breaker to 0 even though gil_drop_request became
142 1. We believe this is all right because the eval loop will release
143 the GIL eventually anyway. */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100144static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200145COMPUTE_EVAL_BREAKER(PyInterpreterState *interp,
Victor Stinner299b8c62020-05-05 17:40:18 +0200146 struct _ceval_runtime_state *ceval,
147 struct _ceval_state *ceval2)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100148{
Victor Stinner299b8c62020-05-05 17:40:18 +0200149 _Py_atomic_store_relaxed(&ceval2->eval_breaker,
150 _Py_atomic_load_relaxed(&ceval2->gil_drop_request)
Victor Stinner0b1e3302020-05-05 16:14:31 +0200151 | (_Py_atomic_load_relaxed(&ceval->signals_pending)
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200152 && _Py_ThreadCanHandleSignals(interp))
Victor Stinner299b8c62020-05-05 17:40:18 +0200153 | (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)
Victor Stinnerd8316882020-03-20 14:50:35 +0100154 && _Py_ThreadCanHandlePendingCalls())
Victor Stinner299b8c62020-05-05 17:40:18 +0200155 | ceval2->pending.async_exc);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100156}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000157
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000158
Victor Stinnerda2914d2020-03-20 09:29:08 +0100159static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200160SET_GIL_DROP_REQUEST(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100161{
Victor Stinner299b8c62020-05-05 17:40:18 +0200162 struct _ceval_state *ceval2 = &interp->ceval;
163 _Py_atomic_store_relaxed(&ceval2->gil_drop_request, 1);
164 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100165}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000166
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000167
Victor Stinnerda2914d2020-03-20 09:29:08 +0100168static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200169RESET_GIL_DROP_REQUEST(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100170{
Victor Stinner299b8c62020-05-05 17:40:18 +0200171 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
172 struct _ceval_state *ceval2 = &interp->ceval;
173 _Py_atomic_store_relaxed(&ceval2->gil_drop_request, 0);
174 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100175}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000176
Eric Snowfdf282d2019-01-11 14:26:55 -0700177
Victor Stinnerda2914d2020-03-20 09:29:08 +0100178static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200179SIGNAL_PENDING_CALLS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100180{
Victor Stinner299b8c62020-05-05 17:40:18 +0200181 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
182 struct _ceval_state *ceval2 = &interp->ceval;
183 _Py_atomic_store_relaxed(&ceval2->pending.calls_to_do, 1);
184 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100185}
Eric Snowfdf282d2019-01-11 14:26:55 -0700186
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000187
Victor Stinnerda2914d2020-03-20 09:29:08 +0100188static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200189UNSIGNAL_PENDING_CALLS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100190{
Victor Stinner299b8c62020-05-05 17:40:18 +0200191 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
192 struct _ceval_state *ceval2 = &interp->ceval;
193 _Py_atomic_store_relaxed(&ceval2->pending.calls_to_do, 0);
194 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100195}
196
197
198static inline void
Miss Islington (bot)e5729ae2020-11-13 06:11:38 -0800199SIGNAL_PENDING_SIGNALS(PyInterpreterState *interp, int force)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100200{
Victor Stinner299b8c62020-05-05 17:40:18 +0200201 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
202 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinner0b1e3302020-05-05 16:14:31 +0200203 _Py_atomic_store_relaxed(&ceval->signals_pending, 1);
Miss Islington (bot)e5729ae2020-11-13 06:11:38 -0800204 if (force) {
205 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
206 }
207 else {
208 /* eval_breaker is not set to 1 if thread_can_handle_signals() is false */
209 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
210 }
Victor Stinnerda2914d2020-03-20 09:29:08 +0100211}
212
213
214static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200215UNSIGNAL_PENDING_SIGNALS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100216{
Victor Stinner299b8c62020-05-05 17:40:18 +0200217 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
218 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinner0b1e3302020-05-05 16:14:31 +0200219 _Py_atomic_store_relaxed(&ceval->signals_pending, 0);
Victor Stinner299b8c62020-05-05 17:40:18 +0200220 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100221}
222
223
224static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200225SIGNAL_ASYNC_EXC(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100226{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200227 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100228 ceval2->pending.async_exc = 1;
229 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
230}
231
232
233static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200234UNSIGNAL_ASYNC_EXC(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100235{
Victor Stinner299b8c62020-05-05 17:40:18 +0200236 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
237 struct _ceval_state *ceval2 = &interp->ceval;
238 ceval2->pending.async_exc = 0;
239 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100240}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000241
242
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000243#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000244#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000245#endif
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000246#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000247
Victor Stinner6d62dc12020-06-03 20:16:39 +0200248void _Py_NO_RETURN
249_Py_FatalError_TstateNULL(const char *func)
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100250{
Victor Stinner6d62dc12020-06-03 20:16:39 +0200251 _Py_FatalErrorFunc(func,
252 "the function must be called with the GIL held, "
253 "but the GIL is released "
254 "(the current Python thread state is NULL)");
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100255}
256
257
Tim Peters7f468f22004-10-11 02:40:51 +0000258int
Victor Stinner175a7042020-03-10 00:37:48 +0100259_PyEval_ThreadsInitialized(_PyRuntimeState *runtime)
260{
261 return gil_created(&runtime->ceval.gil);
262}
263
264int
Tim Peters7f468f22004-10-11 02:40:51 +0000265PyEval_ThreadsInitialized(void)
266{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100267 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner175a7042020-03-10 00:37:48 +0100268 return _PyEval_ThreadsInitialized(runtime);
Tim Peters7f468f22004-10-11 02:40:51 +0000269}
270
Victor Stinner111e4ee2020-03-09 21:24:14 +0100271PyStatus
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200272_PyEval_InitGIL(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000273{
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200274 if (!_Py_IsMainInterpreter(tstate)) {
275 /* Currently, the GIL is shared by all interpreters,
276 and only the main interpreter is responsible to create
277 and destroy it. */
278 return _PyStatus_OK();
Victor Stinner111e4ee2020-03-09 21:24:14 +0100279 }
280
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200281 struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil;
282 assert(!gil_created(gil));
Victor Stinner85f5a692020-03-09 22:12:04 +0100283
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200284 PyThread_init_thread();
285 create_gil(gil);
286
287 take_gil(tstate);
288
289 assert(gil_created(gil));
Victor Stinner111e4ee2020-03-09 21:24:14 +0100290 return _PyStatus_OK();
291}
292
293void
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200294_PyEval_FiniGIL(PyThreadState *tstate)
295{
296 if (!_Py_IsMainInterpreter(tstate)) {
297 /* Currently, the GIL is shared by all interpreters,
298 and only the main interpreter is responsible to create
299 and destroy it. */
300 return;
301 }
302
303 struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil;
304 if (!gil_created(gil)) {
305 /* First Py_InitializeFromConfig() call: the GIL doesn't exist
306 yet: do nothing. */
307 return;
308 }
309
310 destroy_gil(gil);
311 assert(!gil_created(gil));
312}
313
314void
Victor Stinner111e4ee2020-03-09 21:24:14 +0100315PyEval_InitThreads(void)
316{
Victor Stinnerb4698ec2020-03-10 01:28:54 +0100317 /* Do nothing: kept for backward compatibility */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000318}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000319
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000320void
Inada Naoki91234a12019-06-03 21:30:58 +0900321_PyEval_Fini(void)
322{
323#if OPCACHE_STATS
324 fprintf(stderr, "-- Opcode cache number of objects = %zd\n",
325 opcache_code_objects);
326
327 fprintf(stderr, "-- Opcode cache total extra mem = %zd\n",
328 opcache_code_objects_extra_mem);
329
330 fprintf(stderr, "\n");
331
332 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL hits = %zd (%d%%)\n",
333 opcache_global_hits,
334 (int) (100.0 * opcache_global_hits /
335 (opcache_global_hits + opcache_global_misses)));
336
337 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL misses = %zd (%d%%)\n",
338 opcache_global_misses,
339 (int) (100.0 * opcache_global_misses /
340 (opcache_global_hits + opcache_global_misses)));
341
342 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL opts = %zd\n",
343 opcache_global_opts);
344
345 fprintf(stderr, "\n");
346#endif
347}
348
349void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000350PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000351{
Victor Stinner09532fe2019-05-10 23:39:09 +0200352 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200353 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinner6d62dc12020-06-03 20:16:39 +0200354 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100355
Victor Stinner85f5a692020-03-09 22:12:04 +0100356 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000357}
358
359void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000360PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000361{
Victor Stinner09532fe2019-05-10 23:39:09 +0200362 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200363 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100365 We therefore avoid PyThreadState_Get() which dumps a fatal error
Victor Stinnerda2914d2020-03-20 09:29:08 +0100366 in debug mode. */
Victor Stinner299b8c62020-05-05 17:40:18 +0200367 struct _ceval_runtime_state *ceval = &runtime->ceval;
368 struct _ceval_state *ceval2 = &tstate->interp->ceval;
369 drop_gil(ceval, ceval2, tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000370}
371
372void
Victor Stinner23ef89d2020-03-18 02:26:04 +0100373_PyEval_ReleaseLock(PyThreadState *tstate)
374{
375 struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
Victor Stinner0b1e3302020-05-05 16:14:31 +0200376 struct _ceval_state *ceval2 = &tstate->interp->ceval;
377 drop_gil(ceval, ceval2, tstate);
Victor Stinner23ef89d2020-03-18 02:26:04 +0100378}
379
380void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000381PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000382{
Victor Stinner6d62dc12020-06-03 20:16:39 +0200383 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100384
Victor Stinner85f5a692020-03-09 22:12:04 +0100385 take_gil(tstate);
Victor Stinnere225beb2019-06-03 18:14:24 +0200386
Victor Stinner85f5a692020-03-09 22:12:04 +0100387 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
388 if (_PyThreadState_Swap(gilstate, tstate) != NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100389 Py_FatalError("non-NULL old thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200390 }
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000391}
392
393void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000394PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000395{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100396 assert(is_tstate_valid(tstate));
Victor Stinner09532fe2019-05-10 23:39:09 +0200397
Victor Stinner01b1cc12019-11-20 02:27:56 +0100398 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200399 PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
400 if (new_tstate != tstate) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100401 Py_FatalError("wrong thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200402 }
Victor Stinner0b1e3302020-05-05 16:14:31 +0200403 struct _ceval_runtime_state *ceval = &runtime->ceval;
404 struct _ceval_state *ceval2 = &tstate->interp->ceval;
405 drop_gil(ceval, ceval2, tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000406}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000407
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900408#ifdef HAVE_FORK
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200409/* This function is called from PyOS_AfterFork_Child to destroy all threads
410 * which are not running in the child process, and clear internal locks
411 * which might be held by those threads.
412 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000413
414void
Victor Stinnerd5d9e812019-05-13 12:35:37 +0200415_PyEval_ReInitThreads(_PyRuntimeState *runtime)
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000416{
Victor Stinner7be4e352020-05-05 20:27:47 +0200417 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinner6d62dc12020-06-03 20:16:39 +0200418 _Py_EnsureTstateNotNULL(tstate);
Victor Stinner7be4e352020-05-05 20:27:47 +0200419
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100420 struct _gil_runtime_state *gil = &runtime->ceval.gil;
421 if (!gil_created(gil)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 return;
Victor Stinner09532fe2019-05-10 23:39:09 +0200423 }
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100424 recreate_gil(gil);
Victor Stinner85f5a692020-03-09 22:12:04 +0100425
426 take_gil(tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700427
Victor Stinner50e6e992020-03-19 02:41:21 +0100428 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900429 if (_PyThread_at_fork_reinit(&pending->lock) < 0) {
Eric Snow8479a342019-03-08 23:44:33 -0700430 Py_FatalError("Can't initialize threads for pending calls");
431 }
Jesse Nollera8513972008-07-17 16:49:17 +0000432
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200433 /* Destroy all threads except the current one */
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100434 _PyThreadState_DeleteExcept(runtime, tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000435}
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900436#endif
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000437
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000438/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600439 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000440
441void
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100442_PyEval_SignalAsyncExc(PyThreadState *tstate)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000443{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200444 assert(is_tstate_valid(tstate));
445 SIGNAL_ASYNC_EXC(tstate->interp);
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000446}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000447
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000448PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000449PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000450{
Victor Stinner09532fe2019-05-10 23:39:09 +0200451 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200452 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
Victor Stinner6d62dc12020-06-03 20:16:39 +0200453 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100454
Victor Stinner0b1e3302020-05-05 16:14:31 +0200455 struct _ceval_runtime_state *ceval = &runtime->ceval;
456 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinnere225beb2019-06-03 18:14:24 +0200457 assert(gil_created(&ceval->gil));
Victor Stinner0b1e3302020-05-05 16:14:31 +0200458 drop_gil(ceval, ceval2, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000460}
461
462void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000463PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000464{
Victor Stinner6d62dc12020-06-03 20:16:39 +0200465 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100466
Victor Stinner85f5a692020-03-09 22:12:04 +0100467 take_gil(tstate);
Victor Stinner17c68b82020-01-30 12:20:48 +0100468
Victor Stinner85f5a692020-03-09 22:12:04 +0100469 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
470 _PyThreadState_Swap(gilstate, tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000471}
472
473
Guido van Rossuma9672091994-09-14 13:31:22 +0000474/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
475 signal handlers or Mac I/O completion routines) can schedule calls
476 to a function to be called synchronously.
477 The synchronous function is called with one void* argument.
478 It should return 0 for success or -1 for failure -- failure should
479 be accompanied by an exception.
480
481 If registry succeeds, the registry function returns 0; if it fails
482 (e.g. due to too many pending calls) it returns -1 (without setting
483 an exception condition).
484
485 Note that because registry may occur from within signal handlers,
486 or other asynchronous events, calling malloc() is unsafe!
487
Guido van Rossuma9672091994-09-14 13:31:22 +0000488 Any thread can schedule pending calls, but only the main thread
489 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000490 There is no facility to schedule calls to a particular thread, but
491 that should be easy to change, should that ever be required. In
492 that case, the static variables here should go into the python
493 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000494*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000495
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200496void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200497_PyEval_SignalReceived(PyInterpreterState *interp)
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200498{
Miss Islington (bot)e5729ae2020-11-13 06:11:38 -0800499#ifdef MS_WINDOWS
500 // bpo-42296: On Windows, _PyEval_SignalReceived() is called from a signal
501 // handler which can run in a thread different than the Python thread, in
502 // which case _Py_ThreadCanHandleSignals() is wrong. Ignore
503 // _Py_ThreadCanHandleSignals() and always set eval_breaker to 1.
504 //
505 // The next eval_frame_handle_pending() call will call
506 // _Py_ThreadCanHandleSignals() to recompute eval_breaker.
507 int force = 1;
508#else
509 int force = 0;
510#endif
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200511 /* bpo-30703: Function called when the C signal handler of Python gets a
Victor Stinner50e6e992020-03-19 02:41:21 +0100512 signal. We cannot queue a callback using _PyEval_AddPendingCall() since
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200513 that function is not async-signal-safe. */
Miss Islington (bot)e5729ae2020-11-13 06:11:38 -0800514 SIGNAL_PENDING_SIGNALS(interp, force);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200515}
516
Eric Snow5be45a62019-03-08 22:47:07 -0700517/* Push one item onto the queue while holding the lock. */
518static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200519_push_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600520 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700521{
Eric Snow842a2f02019-03-15 15:47:51 -0600522 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700523 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600524 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700525 return -1; /* Queue full */
526 }
Eric Snow842a2f02019-03-15 15:47:51 -0600527 pending->calls[i].func = func;
528 pending->calls[i].arg = arg;
529 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700530 return 0;
531}
532
533/* Pop one item off the queue while holding the lock. */
534static void
Victor Stinnere225beb2019-06-03 18:14:24 +0200535_pop_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600536 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700537{
Eric Snow842a2f02019-03-15 15:47:51 -0600538 int i = pending->first;
539 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700540 return; /* Queue empty */
541 }
542
Eric Snow842a2f02019-03-15 15:47:51 -0600543 *func = pending->calls[i].func;
544 *arg = pending->calls[i].arg;
545 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700546}
547
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200548/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000549 scheduling to be made from any thread, and even from an executing
550 callback.
551 */
552
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000553int
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200554_PyEval_AddPendingCall(PyInterpreterState *interp,
Victor Stinner09532fe2019-05-10 23:39:09 +0200555 int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000556{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200557 struct _pending_calls *pending = &interp->ceval.pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600558
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200559 /* Ensure that _PyEval_InitPendingCalls() was called
560 and that _PyEval_FiniPendingCalls() is not called yet. */
561 assert(pending->lock != NULL);
562
Eric Snow842a2f02019-03-15 15:47:51 -0600563 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200564 int result = _push_pending_call(pending, func, arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600565 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700566
Victor Stinnere225beb2019-06-03 18:14:24 +0200567 /* signal main loop */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200568 SIGNAL_PENDING_CALLS(interp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000570}
571
Victor Stinner09532fe2019-05-10 23:39:09 +0200572int
573Py_AddPendingCall(int (*func)(void *), void *arg)
574{
Victor Stinner50e6e992020-03-19 02:41:21 +0100575 /* Best-effort to support subinterpreters and calls with the GIL released.
576
577 First attempt _PyThreadState_GET() since it supports subinterpreters.
578
579 If the GIL is released, _PyThreadState_GET() returns NULL . In this
580 case, use PyGILState_GetThisThreadState() which works even if the GIL
581 is released.
582
583 Sadly, PyGILState_GetThisThreadState() doesn't support subinterpreters:
584 see bpo-10915 and bpo-15751.
585
Victor Stinner8849e592020-03-18 19:28:53 +0100586 Py_AddPendingCall() doesn't require the caller to hold the GIL. */
Victor Stinner50e6e992020-03-19 02:41:21 +0100587 PyThreadState *tstate = _PyThreadState_GET();
588 if (tstate == NULL) {
589 tstate = PyGILState_GetThisThreadState();
590 }
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200591
592 PyInterpreterState *interp;
593 if (tstate != NULL) {
594 interp = tstate->interp;
595 }
596 else {
597 /* Last resort: use the main interpreter */
598 interp = _PyRuntime.interpreters.main;
599 }
600 return _PyEval_AddPendingCall(interp, func, arg);
Victor Stinner09532fe2019-05-10 23:39:09 +0200601}
602
Eric Snowfdf282d2019-01-11 14:26:55 -0700603static int
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100604handle_signals(PyThreadState *tstate)
Eric Snowfdf282d2019-01-11 14:26:55 -0700605{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200606 assert(is_tstate_valid(tstate));
607 if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
Eric Snow64d6cc82019-02-23 15:40:43 -0700608 return 0;
609 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700610
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200611 UNSIGNAL_PENDING_SIGNALS(tstate->interp);
Victor Stinner72818982020-03-26 22:28:11 +0100612 if (_PyErr_CheckSignalsTstate(tstate) < 0) {
613 /* On failure, re-schedule a call to handle_signals(). */
Miss Islington (bot)e5729ae2020-11-13 06:11:38 -0800614 SIGNAL_PENDING_SIGNALS(tstate->interp, 0);
Eric Snowfdf282d2019-01-11 14:26:55 -0700615 return -1;
616 }
617 return 0;
618}
619
620static int
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100621make_pending_calls(PyThreadState *tstate)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000622{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200623 assert(is_tstate_valid(tstate));
624
Victor Stinnerd8316882020-03-20 14:50:35 +0100625 /* only execute pending calls on main thread */
626 if (!_Py_ThreadCanHandlePendingCalls()) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200627 return 0;
628 }
629
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 /* don't perform recursive pending calls */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100631 static int busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700632 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700634 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200635 busy = 1;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100636
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200637 /* unsignal before starting to call callbacks, so that any callback
638 added in-between re-signals */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200639 UNSIGNAL_PENDING_CALLS(tstate->interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700640 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 /* perform a bounded number of calls, in case of recursion */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100643 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Eric Snowfdf282d2019-01-11 14:26:55 -0700644 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700645 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 void *arg = NULL;
647
648 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600649 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200650 _pop_pending_call(pending, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600651 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700652
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100653 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700654 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100655 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700656 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700657 res = func(arg);
658 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200659 goto error;
660 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200662
Charles-François Natalif23339a2011-07-23 18:15:43 +0200663 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700664 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200665
666error:
667 busy = 0;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200668 SIGNAL_PENDING_CALLS(tstate->interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700669 return res;
670}
671
Eric Snow842a2f02019-03-15 15:47:51 -0600672void
Victor Stinner2b1df452020-01-13 18:46:59 +0100673_Py_FinishPendingCalls(PyThreadState *tstate)
Eric Snow842a2f02019-03-15 15:47:51 -0600674{
Eric Snow842a2f02019-03-15 15:47:51 -0600675 assert(PyGILState_Check());
676
Victor Stinner50e6e992020-03-19 02:41:21 +0100677 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200678
Eric Snow842a2f02019-03-15 15:47:51 -0600679 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
680 return;
681 }
682
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100683 if (make_pending_calls(tstate) < 0) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200684 PyObject *exc, *val, *tb;
685 _PyErr_Fetch(tstate, &exc, &val, &tb);
686 PyErr_BadInternalCall();
687 _PyErr_ChainExceptions(exc, val, tb);
688 _PyErr_Print(tstate);
Eric Snow842a2f02019-03-15 15:47:51 -0600689 }
690}
691
Eric Snowfdf282d2019-01-11 14:26:55 -0700692/* Py_MakePendingCalls() is a simple wrapper for the sake
693 of backward-compatibility. */
694int
695Py_MakePendingCalls(void)
696{
697 assert(PyGILState_Check());
698
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100699 PyThreadState *tstate = _PyThreadState_GET();
700
Eric Snowfdf282d2019-01-11 14:26:55 -0700701 /* Python signal handler doesn't really queue a callback: it only signals
702 that a signal was received, see _PyEval_SignalReceived(). */
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100703 int res = handle_signals(tstate);
Eric Snowfdf282d2019-01-11 14:26:55 -0700704 if (res != 0) {
705 return res;
706 }
707
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100708 res = make_pending_calls(tstate);
Eric Snowb75b1a352019-04-12 10:20:10 -0600709 if (res != 0) {
710 return res;
711 }
712
713 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000714}
715
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000716/* The interpreter's recursion limit */
717
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000718#ifndef Py_DEFAULT_RECURSION_LIMIT
719#define Py_DEFAULT_RECURSION_LIMIT 1000
720#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600721
Eric Snow05351c12017-09-05 21:43:08 -0700722int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000723
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600724void
Victor Stinnerdab84232020-03-17 18:56:44 +0100725_PyEval_InitRuntimeState(struct _ceval_runtime_state *ceval)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600726{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600727 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Victor Stinnerdab84232020-03-17 18:56:44 +0100728 _gil_initialize(&ceval->gil);
729}
730
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200731int
Victor Stinnerdab84232020-03-17 18:56:44 +0100732_PyEval_InitState(struct _ceval_state *ceval)
733{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200734 ceval->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
735
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200736 struct _pending_calls *pending = &ceval->pending;
737 assert(pending->lock == NULL);
738
739 pending->lock = PyThread_allocate_lock();
740 if (pending->lock == NULL) {
741 return -1;
742 }
Victor Stinner7be4e352020-05-05 20:27:47 +0200743
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200744 return 0;
745}
746
747void
748_PyEval_FiniState(struct _ceval_state *ceval)
749{
750 struct _pending_calls *pending = &ceval->pending;
751 if (pending->lock != NULL) {
752 PyThread_free_lock(pending->lock);
753 pending->lock = NULL;
754 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600755}
756
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000757int
758Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000759{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200760 PyThreadState *tstate = _PyThreadState_GET();
761 return tstate->interp->ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000762}
763
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000764void
765Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000766{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200767 PyThreadState *tstate = _PyThreadState_GET();
768 tstate->interp->ceval.recursion_limit = new_limit;
769 if (_Py_IsMainInterpreter(tstate)) {
770 _Py_CheckRecursionLimit = new_limit;
771 }
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000772}
773
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100774/* The function _Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
Armin Rigo2b3eb402003-10-28 12:05:48 +0000775 if the recursion_depth reaches _Py_CheckRecursionLimit.
776 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
777 to guarantee that _Py_CheckRecursiveCall() is regularly called.
778 Without USE_STACKCHECK, there is no need for this. */
779int
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100780_Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000781{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200782 int recursion_limit = tstate->interp->ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000783
784#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700785 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 if (PyOS_CheckStack()) {
787 --tstate->recursion_depth;
Victor Stinner438a12d2019-05-24 17:01:38 +0200788 _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 return -1;
790 }
Victor Stinner4e30ed32020-05-05 16:52:52 +0200791 if (_Py_IsMainInterpreter(tstate)) {
792 /* Needed for ABI backwards-compatibility (see bpo-31857) */
793 _Py_CheckRecursionLimit = recursion_limit;
794 }
pdox18967932017-10-25 23:03:01 -0700795#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 if (tstate->recursion_critical)
797 /* Somebody asked that we don't check for recursion. */
798 return 0;
799 if (tstate->overflowed) {
800 if (tstate->recursion_depth > recursion_limit + 50) {
801 /* Overflowing while handling an overflow. Give up. */
802 Py_FatalError("Cannot recover from stack overflow.");
803 }
804 return 0;
805 }
806 if (tstate->recursion_depth > recursion_limit) {
807 --tstate->recursion_depth;
808 tstate->overflowed = 1;
Victor Stinner438a12d2019-05-24 17:01:38 +0200809 _PyErr_Format(tstate, PyExc_RecursionError,
810 "maximum recursion depth exceeded%s",
811 where);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 return -1;
813 }
814 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000815}
816
Victor Stinner09532fe2019-05-10 23:39:09 +0200817static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
Victor Stinner438a12d2019-05-24 17:01:38 +0200818static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000819
Victor Stinnere225beb2019-06-03 18:14:24 +0200820#define _Py_TracingPossible(ceval) ((ceval)->tracing_possible)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000821
Guido van Rossum374a9221991-04-04 10:40:29 +0000822
Guido van Rossumb209a111997-04-29 18:18:01 +0000823PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000824PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000825{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 return PyEval_EvalCodeEx(co,
827 globals, locals,
828 (PyObject **)NULL, 0,
829 (PyObject **)NULL, 0,
830 (PyObject **)NULL, 0,
831 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000832}
833
834
835/* Interpreter main loop */
836
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000837PyObject *
Victor Stinnerb9e68122019-11-14 12:20:46 +0100838PyEval_EvalFrame(PyFrameObject *f)
839{
Victor Stinner0b72b232020-03-12 23:18:39 +0100840 /* Function kept for backward compatibility */
Victor Stinnerb9e68122019-11-14 12:20:46 +0100841 PyThreadState *tstate = _PyThreadState_GET();
842 return _PyEval_EvalFrame(tstate, f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000843}
844
845PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000846PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000847{
Victor Stinnerb9e68122019-11-14 12:20:46 +0100848 PyThreadState *tstate = _PyThreadState_GET();
849 return _PyEval_EvalFrame(tstate, f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700850}
851
Victor Stinnerda2914d2020-03-20 09:29:08 +0100852
853/* Handle signals, pending calls, GIL drop request
854 and asynchronous exception */
855static int
856eval_frame_handle_pending(PyThreadState *tstate)
857{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100858 _PyRuntimeState * const runtime = &_PyRuntime;
859 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200860
861 /* Pending signals */
Victor Stinner299b8c62020-05-05 17:40:18 +0200862 if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
Victor Stinnerda2914d2020-03-20 09:29:08 +0100863 if (handle_signals(tstate) != 0) {
864 return -1;
865 }
866 }
867
868 /* Pending calls */
Victor Stinner299b8c62020-05-05 17:40:18 +0200869 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100870 if (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)) {
871 if (make_pending_calls(tstate) != 0) {
872 return -1;
873 }
874 }
875
876 /* GIL drop request */
Victor Stinner0b1e3302020-05-05 16:14:31 +0200877 if (_Py_atomic_load_relaxed(&ceval2->gil_drop_request)) {
Victor Stinnerda2914d2020-03-20 09:29:08 +0100878 /* Give another thread a chance */
879 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
880 Py_FatalError("tstate mix-up");
881 }
Victor Stinner0b1e3302020-05-05 16:14:31 +0200882 drop_gil(ceval, ceval2, tstate);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100883
884 /* Other threads may run now */
885
886 take_gil(tstate);
887
888 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
889 Py_FatalError("orphan tstate");
890 }
891 }
892
893 /* Check for asynchronous exception. */
894 if (tstate->async_exc != NULL) {
895 PyObject *exc = tstate->async_exc;
896 tstate->async_exc = NULL;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200897 UNSIGNAL_ASYNC_EXC(tstate->interp);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100898 _PyErr_SetNone(tstate, exc);
899 Py_DECREF(exc);
900 return -1;
901 }
902
Miss Islington (bot)e5729ae2020-11-13 06:11:38 -0800903#ifdef MS_WINDOWS
904 // bpo-42296: On Windows, _PyEval_SignalReceived() can be called in a
905 // different thread than the Python thread, in which case
906 // _Py_ThreadCanHandleSignals() is wrong. Recompute eval_breaker in the
907 // current Python thread with the correct _Py_ThreadCanHandleSignals()
908 // value. It prevents to interrupt the eval loop at every instruction if
909 // the current Python thread cannot handle signals (if
910 // _Py_ThreadCanHandleSignals() is false).
911 COMPUTE_EVAL_BREAKER(tstate->interp, ceval, ceval2);
912#endif
913
Victor Stinnerda2914d2020-03-20 09:29:08 +0100914 return 0;
915}
916
Victor Stinnerc6944e72016-11-11 02:13:35 +0100917PyObject* _Py_HOT_FUNCTION
Victor Stinner0b72b232020-03-12 23:18:39 +0100918_PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
Brett Cannon3cebf932016-09-05 15:33:46 -0700919{
Victor Stinner6d62dc12020-06-03 20:16:39 +0200920 _Py_EnsureTstateNotNULL(tstate);
Victor Stinner0b72b232020-03-12 23:18:39 +0100921
Guido van Rossum950361c1997-01-24 13:49:28 +0000922#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000924#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200925 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300926 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200927 int opcode; /* Current opcode */
928 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200929 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930 PyObject *retval = NULL; /* Return value */
Victor Stinnerdab84232020-03-17 18:56:44 +0100931 struct _ceval_state * const ceval2 = &tstate->interp->ceval;
Victor Stinner50e6e992020-03-19 02:41:21 +0100932 _Py_atomic_int * const eval_breaker = &ceval2->eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000934
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000938
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 is true when the line being executed has changed. The
940 initial values are such as to make this false the first
941 time it is tested. */
942 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000943
Serhiy Storchakaab874002016-09-11 13:48:15 +0300944 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 PyObject *names;
946 PyObject *consts;
Inada Naoki91234a12019-06-03 21:30:58 +0900947 _PyOpcache *co_opcache;
Guido van Rossum374a9221991-04-04 10:40:29 +0000948
Brett Cannon368b4b72012-04-02 12:17:59 -0400949#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200950 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400951#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200952
Antoine Pitroub52ec782009-01-25 16:34:23 +0000953/* Computed GOTOs, or
954 the-optimization-commonly-but-improperly-known-as-"threaded code"
955 using gcc's labels-as-values extension
956 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
957
958 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000960 combined with a lookup table of jump addresses. However, since the
961 indirect jump instruction is shared by all opcodes, the CPU will have a
962 hard time making the right prediction for where to jump next (actually,
963 it will be always wrong except in the uncommon case of a sequence of
964 several identical opcodes).
965
966 "Threaded code" in contrast, uses an explicit jump table and an explicit
967 indirect jump instruction at the end of each opcode. Since the jump
968 instruction is at a different address for each opcode, the CPU will make a
969 separate prediction for each of these instructions, which is equivalent to
970 predicting the second opcode of each opcode pair. These predictions have
971 a much better chance to turn out valid, especially in small bytecode loops.
972
973 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000975 and potentially many more instructions (depending on the pipeline width).
976 A correctly predicted branch, however, is nearly free.
977
978 At the time of this writing, the "threaded code" version is up to 15-20%
979 faster than the normal "switch" version, depending on the compiler and the
980 CPU architecture.
981
982 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
983 because it would render the measurements invalid.
984
985
986 NOTE: care must be taken that the compiler doesn't try to "optimize" the
987 indirect jumps by sharing them between all opcodes. Such optimizations
988 can be disabled on gcc by using the -fno-gcse flag (or possibly
989 -fno-crossjumping).
990*/
991
Antoine Pitrou042b1282010-08-13 21:15:58 +0000992#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000993#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000994#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000995#endif
996
Antoine Pitrou042b1282010-08-13 21:15:58 +0000997#ifdef HAVE_COMPUTED_GOTOS
998 #ifndef USE_COMPUTED_GOTOS
999 #define USE_COMPUTED_GOTOS 1
1000 #endif
1001#else
1002 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
1003 #error "Computed gotos are not supported on this compiler."
1004 #endif
1005 #undef USE_COMPUTED_GOTOS
1006 #define USE_COMPUTED_GOTOS 0
1007#endif
1008
1009#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +00001010/* Import the static jump table */
1011#include "opcode_targets.h"
1012
Antoine Pitroub52ec782009-01-25 16:34:23 +00001013#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -07001014 op: \
1015 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +00001016
Antoine Pitroub52ec782009-01-25 16:34:23 +00001017#ifdef LLTRACE
1018#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 { \
Victor Stinnerdab84232020-03-17 18:56:44 +01001020 if (!lltrace && !_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001022 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001023 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 } \
1025 goto fast_next_opcode; \
1026 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001027#else
1028#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 { \
Victor Stinnerdab84232020-03-17 18:56:44 +01001030 if (!_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001032 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001033 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 } \
1035 goto fast_next_opcode; \
1036 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001037#endif
1038
Victor Stinner09532fe2019-05-10 23:39:09 +02001039#define DISPATCH() \
1040 { \
1041 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
1042 FAST_DISPATCH(); \
1043 } \
1044 continue; \
1045 }
1046
Antoine Pitroub52ec782009-01-25 16:34:23 +00001047#else
Benjamin Petersonddd19492018-09-16 22:38:02 -07001048#define TARGET(op) op
Antoine Pitroub52ec782009-01-25 16:34:23 +00001049#define FAST_DISPATCH() goto fast_next_opcode
Victor Stinner09532fe2019-05-10 23:39:09 +02001050#define DISPATCH() continue
Antoine Pitroub52ec782009-01-25 16:34:23 +00001051#endif
1052
1053
Neal Norwitza81d2202002-07-14 00:27:26 +00001054/* Tuple access macros */
1055
1056#ifndef Py_DEBUG
1057#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
1058#else
1059#define GETITEM(v, i) PyTuple_GetItem((v), (i))
1060#endif
1061
Guido van Rossum374a9221991-04-04 10:40:29 +00001062/* Code access macros */
1063
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001064/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001065#define INSTR_OFFSET() \
1066 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001067#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001068 _Py_CODEUNIT word = *next_instr; \
1069 opcode = _Py_OPCODE(word); \
1070 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001071 next_instr++; \
1072 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +03001073#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
1074#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +00001075
Raymond Hettingerf606f872003-03-16 03:11:04 +00001076/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 Some opcodes tend to come in pairs thus making it possible to
1078 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001079 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001080
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081 Verifying the prediction costs a single high-speed test of a register
1082 variable against a constant. If the pairing was good, then the
1083 processor's own internal branch predication has a high likelihood of
1084 success, resulting in a nearly zero-overhead transition to the
1085 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001086 including its unpredictable switch-case branch. Combined with the
1087 processor's internal branch prediction, a successful PREDICT has the
1088 effect of making the two opcodes run as if they were a single new opcode
1089 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001090
Georg Brandl86b2fb92008-07-16 03:43:04 +00001091 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092 predictions turned-on and interpret the results as if some opcodes
1093 had been combined or turn-off predictions so that the opcode frequency
1094 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001095
1096 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 the CPU to record separate branch prediction information for each
1098 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001099
Raymond Hettingerf606f872003-03-16 03:11:04 +00001100*/
1101
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001102#define PREDICT_ID(op) PRED_##op
1103
Antoine Pitrou042b1282010-08-13 21:15:58 +00001104#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001105#define PREDICT(op) if (0) goto PREDICT_ID(op)
Raymond Hettingera7216982004-02-08 19:59:27 +00001106#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001107#define PREDICT(op) \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001108 do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001109 _Py_CODEUNIT word = *next_instr; \
1110 opcode = _Py_OPCODE(word); \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001111 if (opcode == op) { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001112 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001113 next_instr++; \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001114 goto PREDICT_ID(op); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001115 } \
1116 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +00001117#endif
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001118#define PREDICTED(op) PREDICT_ID(op):
Antoine Pitroub52ec782009-01-25 16:34:23 +00001119
Raymond Hettingerf606f872003-03-16 03:11:04 +00001120
Guido van Rossum374a9221991-04-04 10:40:29 +00001121/* Stack manipulation macros */
1122
Martin v. Löwis18e16552006-02-15 17:27:45 +00001123/* The stack can grow at most MAXINT deep, as co_nlocals and
1124 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001125#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1126#define EMPTY() (STACK_LEVEL() == 0)
1127#define TOP() (stack_pointer[-1])
1128#define SECOND() (stack_pointer[-2])
1129#define THIRD() (stack_pointer[-3])
1130#define FOURTH() (stack_pointer[-4])
1131#define PEEK(n) (stack_pointer[-(n)])
1132#define SET_TOP(v) (stack_pointer[-1] = (v))
1133#define SET_SECOND(v) (stack_pointer[-2] = (v))
1134#define SET_THIRD(v) (stack_pointer[-3] = (v))
1135#define SET_FOURTH(v) (stack_pointer[-4] = (v))
1136#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
1137#define BASIC_STACKADJ(n) (stack_pointer += n)
1138#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1139#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001140
Guido van Rossum96a42c81992-01-12 02:29:51 +00001141#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001143 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001144 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +02001145#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001146 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +00001147#define STACK_GROW(n) do { \
1148 assert(n >= 0); \
1149 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001150 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001151 assert(STACK_LEVEL() <= co->co_stacksize); \
1152 } while (0)
1153#define STACK_SHRINK(n) do { \
1154 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +02001155 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001156 (void)(BASIC_STACKADJ(-n)); \
1157 assert(STACK_LEVEL() <= co->co_stacksize); \
1158 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +00001159#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +02001160 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001161 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001162#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001163#define PUSH(v) BASIC_PUSH(v)
1164#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +00001165#define STACK_GROW(n) BASIC_STACKADJ(n)
1166#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001167#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001168#endif
1169
Guido van Rossum681d79a1995-07-18 14:51:37 +00001170/* Local variable macros */
1171
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001173
1174/* The SETLOCAL() macro must not DECREF the local variable in-place and
1175 then store the new value; it must copy the old value to a temporary
1176 value, then store the new value, and then DECREF the temporary value.
1177 This is because it is possible that during the DECREF the frame is
1178 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1179 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001181 GETLOCAL(i) = value; \
1182 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001183
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001184
1185#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 while (STACK_LEVEL() > (b)->b_level) { \
1187 PyObject *v = POP(); \
1188 Py_XDECREF(v); \
1189 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001190
1191#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001192 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +01001194 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1196 while (STACK_LEVEL() > (b)->b_level + 3) { \
1197 value = POP(); \
1198 Py_XDECREF(value); \
1199 } \
Mark Shannonae3087c2017-10-22 22:41:51 +01001200 exc_info = tstate->exc_info; \
1201 type = exc_info->exc_type; \
1202 value = exc_info->exc_value; \
1203 traceback = exc_info->exc_traceback; \
1204 exc_info->exc_type = POP(); \
1205 exc_info->exc_value = POP(); \
1206 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 Py_XDECREF(type); \
1208 Py_XDECREF(value); \
1209 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001210 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001211
Inada Naoki91234a12019-06-03 21:30:58 +09001212 /* macros for opcode cache */
1213#define OPCACHE_CHECK() \
1214 do { \
1215 co_opcache = NULL; \
1216 if (co->co_opcache != NULL) { \
1217 unsigned char co_opt_offset = \
1218 co->co_opcache_map[next_instr - first_instr]; \
1219 if (co_opt_offset > 0) { \
1220 assert(co_opt_offset <= co->co_opcache_size); \
1221 co_opcache = &co->co_opcache[co_opt_offset - 1]; \
1222 assert(co_opcache != NULL); \
Inada Naoki91234a12019-06-03 21:30:58 +09001223 } \
1224 } \
1225 } while (0)
1226
1227#if OPCACHE_STATS
1228
1229#define OPCACHE_STAT_GLOBAL_HIT() \
1230 do { \
1231 if (co->co_opcache != NULL) opcache_global_hits++; \
1232 } while (0)
1233
1234#define OPCACHE_STAT_GLOBAL_MISS() \
1235 do { \
1236 if (co->co_opcache != NULL) opcache_global_misses++; \
1237 } while (0)
1238
1239#define OPCACHE_STAT_GLOBAL_OPT() \
1240 do { \
1241 if (co->co_opcache != NULL) opcache_global_opts++; \
1242 } while (0)
1243
1244#else /* OPCACHE_STATS */
1245
1246#define OPCACHE_STAT_GLOBAL_HIT()
1247#define OPCACHE_STAT_GLOBAL_MISS()
1248#define OPCACHE_STAT_GLOBAL_OPT()
1249
1250#endif
1251
Guido van Rossuma027efa1997-05-05 20:56:21 +00001252/* Start of code */
1253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 /* push frame */
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001255 if (_Py_EnterRecursiveCall(tstate, "")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001257 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 if (tstate->use_tracing) {
1262 if (tstate->c_tracefunc != NULL) {
1263 /* tstate->c_tracefunc, if defined, is a
1264 function that will be called on *every* entry
1265 to a code block. Its return value, if not
1266 None, is a function that will be called at
1267 the start of each executed line of code.
1268 (Actually, the function must return itself
1269 in order to continue tracing.) The trace
1270 functions are called with three arguments:
1271 a pointer to the current frame, a string
1272 indicating why the function is called, and
1273 an argument which depends on the situation.
1274 The global trace function is also called
1275 whenever an exception is detected. */
1276 if (call_trace_protected(tstate->c_tracefunc,
1277 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001278 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 /* Trace function raised an error */
1280 goto exit_eval_frame;
1281 }
1282 }
1283 if (tstate->c_profilefunc != NULL) {
1284 /* Similar for c_profilefunc, except it needn't
1285 return itself and isn't called for "line" events */
1286 if (call_trace_protected(tstate->c_profilefunc,
1287 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001288 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 /* Profile function raised an error */
1290 goto exit_eval_frame;
1291 }
1292 }
1293 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001294
Łukasz Langaa785c872016-09-09 17:37:37 -07001295 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1296 dtrace_function_entry(f);
1297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 co = f->f_code;
1299 names = co->co_names;
1300 consts = co->co_consts;
1301 fastlocals = f->f_localsplus;
1302 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001303 assert(PyBytes_Check(co->co_code));
1304 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001305 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1306 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1307 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001308 /*
1309 f->f_lasti refers to the index of the last instruction,
1310 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001311
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001312 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001313 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 When the PREDICT() macros are enabled, some opcode pairs follow in
1316 direct succession without updating f->f_lasti. A successful
1317 prediction effectively links the two codes together as if they
1318 were a single new opcode; accordingly,f->f_lasti will point to
1319 the first code in the pair (for instance, GET_ITER followed by
1320 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001321 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001323 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001324 next_instr = first_instr;
1325 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001326 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1327 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001328 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 stack_pointer = f->f_stacktop;
1330 assert(stack_pointer != NULL);
1331 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001332 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001333
Inada Naoki91234a12019-06-03 21:30:58 +09001334 if (co->co_opcache_flag < OPCACHE_MIN_RUNS) {
1335 co->co_opcache_flag++;
1336 if (co->co_opcache_flag == OPCACHE_MIN_RUNS) {
1337 if (_PyCode_InitOpcache(co) < 0) {
Victor Stinner25104942020-04-24 02:43:18 +02001338 goto exit_eval_frame;
Inada Naoki91234a12019-06-03 21:30:58 +09001339 }
1340#if OPCACHE_STATS
1341 opcache_code_objects_extra_mem +=
1342 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1343 sizeof(_PyOpcache) * co->co_opcache_size;
1344 opcache_code_objects++;
1345#endif
1346 }
1347 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001348
Tim Peters5ca576e2001-06-18 22:08:13 +00001349#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001350 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001351#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001352
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001353 if (throwflag) /* support for generator.throw() */
1354 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001355
Victor Stinnerace47d72013-07-18 01:41:08 +02001356#ifdef Py_DEBUG
Victor Stinner0b72b232020-03-12 23:18:39 +01001357 /* _PyEval_EvalFrameDefault() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001358 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001359 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001360 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001361#endif
1362
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001363main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001365 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1366 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001367 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 /* Do periodic things. Doing this every time through
1370 the loop would add too much overhead, so we do it
1371 only every Nth instruction. We also do it if
Chris Jerdonek4a12d122020-05-14 19:25:45 -07001372 ``pending.calls_to_do'' is set, i.e. when an asynchronous
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 event needs attention (e.g. a signal handler or
1374 async I/O handler); see Py_AddPendingCall() and
1375 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001376
Eric Snow7bda9de2019-03-08 17:25:54 -07001377 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001378 opcode = _Py_OPCODE(*next_instr);
1379 if (opcode == SETUP_FINALLY ||
1380 opcode == SETUP_WITH ||
1381 opcode == BEFORE_ASYNC_WITH ||
1382 opcode == YIELD_FROM) {
1383 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001384 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001385 - If we're about to enter the 'with:'. It will prevent
1386 emitting a resource warning in the common idiom
1387 'with open(path) as file:'.
1388 - If we're about to enter the 'async with:'.
1389 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001390 *very* useful, but might help in some cases and it's
1391 traditional)
1392 - If we're resuming a chain of nested 'yield from' or
1393 'await' calls, then each frame is parked with YIELD_FROM
1394 as its next opcode. If the user hit control-C we want to
1395 wait until we've reached the innermost frame before
1396 running the signal handler and raising KeyboardInterrupt
1397 (see bpo-30039).
1398 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 goto fast_next_opcode;
1400 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001401
Victor Stinnerda2914d2020-03-20 09:29:08 +01001402 if (eval_frame_handle_pending(tstate) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001403 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001404 }
1405 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 fast_next_opcode:
1408 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001409
Łukasz Langaa785c872016-09-09 17:37:37 -07001410 if (PyDTrace_LINE_ENABLED())
1411 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001414
Victor Stinnerdab84232020-03-17 18:56:44 +01001415 if (_Py_TracingPossible(ceval2) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001416 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001417 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001418 /* see maybe_call_line_trace
1419 for expository comments */
1420 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 err = maybe_call_line_trace(tstate->c_tracefunc,
1423 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001424 tstate, f,
1425 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 /* Reload possibly changed frame fields */
1427 JUMPTO(f->f_lasti);
1428 if (f->f_stacktop != NULL) {
1429 stack_pointer = f->f_stacktop;
1430 f->f_stacktop = NULL;
1431 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001432 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001433 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001434 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001438
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001439 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001440 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001441#ifdef DYNAMIC_EXECUTION_PROFILE
1442#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 dxpairs[lastopcode][opcode]++;
1444 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001445#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001446 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001447#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001448
Guido van Rossum96a42c81992-01-12 02:29:51 +00001449#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001450 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001452 if (lltrace) {
1453 if (HAS_ARG(opcode)) {
1454 printf("%d: %d, %d\n",
1455 f->f_lasti, opcode, oparg);
1456 }
1457 else {
1458 printf("%d: %d\n",
1459 f->f_lasti, opcode);
1460 }
1461 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001462#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001466 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001467 It is essential that any operation that fails must goto error
1468 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001469
Benjamin Petersonddd19492018-09-16 22:38:02 -07001470 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001471 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001472 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001473
Benjamin Petersonddd19492018-09-16 22:38:02 -07001474 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001475 PyObject *value = GETLOCAL(oparg);
1476 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001477 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001478 UNBOUNDLOCAL_ERROR_MSG,
1479 PyTuple_GetItem(co->co_varnames, oparg));
1480 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001481 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001482 Py_INCREF(value);
1483 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001485 }
1486
Benjamin Petersonddd19492018-09-16 22:38:02 -07001487 case TARGET(LOAD_CONST): {
1488 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001489 PyObject *value = GETITEM(consts, oparg);
1490 Py_INCREF(value);
1491 PUSH(value);
1492 FAST_DISPATCH();
1493 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001494
Benjamin Petersonddd19492018-09-16 22:38:02 -07001495 case TARGET(STORE_FAST): {
1496 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001497 PyObject *value = POP();
1498 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001499 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001500 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001501
Benjamin Petersonddd19492018-09-16 22:38:02 -07001502 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001503 PyObject *value = POP();
1504 Py_DECREF(value);
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(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001509 PyObject *top = TOP();
1510 PyObject *second = SECOND();
1511 SET_TOP(second);
1512 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001513 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001514 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001515
Benjamin Petersonddd19492018-09-16 22:38:02 -07001516 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001517 PyObject *top = TOP();
1518 PyObject *second = SECOND();
1519 PyObject *third = THIRD();
1520 SET_TOP(second);
1521 SET_SECOND(third);
1522 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001523 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001524 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001525
Benjamin Petersonddd19492018-09-16 22:38:02 -07001526 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001527 PyObject *top = TOP();
1528 PyObject *second = SECOND();
1529 PyObject *third = THIRD();
1530 PyObject *fourth = FOURTH();
1531 SET_TOP(second);
1532 SET_SECOND(third);
1533 SET_THIRD(fourth);
1534 SET_FOURTH(top);
1535 FAST_DISPATCH();
1536 }
1537
Benjamin Petersonddd19492018-09-16 22:38:02 -07001538 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001539 PyObject *top = TOP();
1540 Py_INCREF(top);
1541 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001542 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001543 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001544
Benjamin Petersonddd19492018-09-16 22:38:02 -07001545 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001546 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001547 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001548 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001549 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001550 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001551 SET_TOP(top);
1552 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001553 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001554 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001555
Benjamin Petersonddd19492018-09-16 22:38:02 -07001556 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001557 PyObject *value = TOP();
1558 PyObject *res = PyNumber_Positive(value);
1559 Py_DECREF(value);
1560 SET_TOP(res);
1561 if (res == NULL)
1562 goto error;
1563 DISPATCH();
1564 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001565
Benjamin Petersonddd19492018-09-16 22:38:02 -07001566 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001567 PyObject *value = TOP();
1568 PyObject *res = PyNumber_Negative(value);
1569 Py_DECREF(value);
1570 SET_TOP(res);
1571 if (res == NULL)
1572 goto error;
1573 DISPATCH();
1574 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001575
Benjamin Petersonddd19492018-09-16 22:38:02 -07001576 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001577 PyObject *value = TOP();
1578 int err = PyObject_IsTrue(value);
1579 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001580 if (err == 0) {
1581 Py_INCREF(Py_True);
1582 SET_TOP(Py_True);
1583 DISPATCH();
1584 }
1585 else if (err > 0) {
1586 Py_INCREF(Py_False);
1587 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 DISPATCH();
1589 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001590 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001591 goto error;
1592 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001593
Benjamin Petersonddd19492018-09-16 22:38:02 -07001594 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001595 PyObject *value = TOP();
1596 PyObject *res = PyNumber_Invert(value);
1597 Py_DECREF(value);
1598 SET_TOP(res);
1599 if (res == NULL)
1600 goto error;
1601 DISPATCH();
1602 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001603
Benjamin Petersonddd19492018-09-16 22:38:02 -07001604 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001605 PyObject *exp = POP();
1606 PyObject *base = TOP();
1607 PyObject *res = PyNumber_Power(base, exp, Py_None);
1608 Py_DECREF(base);
1609 Py_DECREF(exp);
1610 SET_TOP(res);
1611 if (res == NULL)
1612 goto error;
1613 DISPATCH();
1614 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001615
Benjamin Petersonddd19492018-09-16 22:38:02 -07001616 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001617 PyObject *right = POP();
1618 PyObject *left = TOP();
1619 PyObject *res = PyNumber_Multiply(left, right);
1620 Py_DECREF(left);
1621 Py_DECREF(right);
1622 SET_TOP(res);
1623 if (res == NULL)
1624 goto error;
1625 DISPATCH();
1626 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001627
Benjamin Petersonddd19492018-09-16 22:38:02 -07001628 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001629 PyObject *right = POP();
1630 PyObject *left = TOP();
1631 PyObject *res = PyNumber_MatrixMultiply(left, right);
1632 Py_DECREF(left);
1633 Py_DECREF(right);
1634 SET_TOP(res);
1635 if (res == NULL)
1636 goto error;
1637 DISPATCH();
1638 }
1639
Benjamin Petersonddd19492018-09-16 22:38:02 -07001640 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001641 PyObject *divisor = POP();
1642 PyObject *dividend = TOP();
1643 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1644 Py_DECREF(dividend);
1645 Py_DECREF(divisor);
1646 SET_TOP(quotient);
1647 if (quotient == NULL)
1648 goto error;
1649 DISPATCH();
1650 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001651
Benjamin Petersonddd19492018-09-16 22:38:02 -07001652 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001653 PyObject *divisor = POP();
1654 PyObject *dividend = TOP();
1655 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1656 Py_DECREF(dividend);
1657 Py_DECREF(divisor);
1658 SET_TOP(quotient);
1659 if (quotient == NULL)
1660 goto error;
1661 DISPATCH();
1662 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001663
Benjamin Petersonddd19492018-09-16 22:38:02 -07001664 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001665 PyObject *divisor = POP();
1666 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001667 PyObject *res;
1668 if (PyUnicode_CheckExact(dividend) && (
1669 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1670 // fast path; string formatting, but not if the RHS is a str subclass
1671 // (see issue28598)
1672 res = PyUnicode_Format(dividend, divisor);
1673 } else {
1674 res = PyNumber_Remainder(dividend, divisor);
1675 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001676 Py_DECREF(divisor);
1677 Py_DECREF(dividend);
1678 SET_TOP(res);
1679 if (res == NULL)
1680 goto error;
1681 DISPATCH();
1682 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001683
Benjamin Petersonddd19492018-09-16 22:38:02 -07001684 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001685 PyObject *right = POP();
1686 PyObject *left = TOP();
1687 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001688 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1689 CPython using bytecode, it is simply worthless.
1690 See http://bugs.python.org/issue21955 and
1691 http://bugs.python.org/issue10044 for the discussion. In short,
1692 no patch shown any impact on a realistic benchmark, only a minor
1693 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001694 if (PyUnicode_CheckExact(left) &&
1695 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001696 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001697 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001698 }
1699 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001700 sum = PyNumber_Add(left, right);
1701 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001702 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001703 Py_DECREF(right);
1704 SET_TOP(sum);
1705 if (sum == NULL)
1706 goto error;
1707 DISPATCH();
1708 }
1709
Benjamin Petersonddd19492018-09-16 22:38:02 -07001710 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001711 PyObject *right = POP();
1712 PyObject *left = TOP();
1713 PyObject *diff = PyNumber_Subtract(left, right);
1714 Py_DECREF(right);
1715 Py_DECREF(left);
1716 SET_TOP(diff);
1717 if (diff == NULL)
1718 goto error;
1719 DISPATCH();
1720 }
1721
Benjamin Petersonddd19492018-09-16 22:38:02 -07001722 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001723 PyObject *sub = POP();
1724 PyObject *container = TOP();
1725 PyObject *res = PyObject_GetItem(container, sub);
1726 Py_DECREF(container);
1727 Py_DECREF(sub);
1728 SET_TOP(res);
1729 if (res == NULL)
1730 goto error;
1731 DISPATCH();
1732 }
1733
Benjamin Petersonddd19492018-09-16 22:38:02 -07001734 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001735 PyObject *right = POP();
1736 PyObject *left = TOP();
1737 PyObject *res = PyNumber_Lshift(left, right);
1738 Py_DECREF(left);
1739 Py_DECREF(right);
1740 SET_TOP(res);
1741 if (res == NULL)
1742 goto error;
1743 DISPATCH();
1744 }
1745
Benjamin Petersonddd19492018-09-16 22:38:02 -07001746 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001747 PyObject *right = POP();
1748 PyObject *left = TOP();
1749 PyObject *res = PyNumber_Rshift(left, right);
1750 Py_DECREF(left);
1751 Py_DECREF(right);
1752 SET_TOP(res);
1753 if (res == NULL)
1754 goto error;
1755 DISPATCH();
1756 }
1757
Benjamin Petersonddd19492018-09-16 22:38:02 -07001758 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001759 PyObject *right = POP();
1760 PyObject *left = TOP();
1761 PyObject *res = PyNumber_And(left, right);
1762 Py_DECREF(left);
1763 Py_DECREF(right);
1764 SET_TOP(res);
1765 if (res == NULL)
1766 goto error;
1767 DISPATCH();
1768 }
1769
Benjamin Petersonddd19492018-09-16 22:38:02 -07001770 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001771 PyObject *right = POP();
1772 PyObject *left = TOP();
1773 PyObject *res = PyNumber_Xor(left, right);
1774 Py_DECREF(left);
1775 Py_DECREF(right);
1776 SET_TOP(res);
1777 if (res == NULL)
1778 goto error;
1779 DISPATCH();
1780 }
1781
Benjamin Petersonddd19492018-09-16 22:38:02 -07001782 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001783 PyObject *right = POP();
1784 PyObject *left = TOP();
1785 PyObject *res = PyNumber_Or(left, right);
1786 Py_DECREF(left);
1787 Py_DECREF(right);
1788 SET_TOP(res);
1789 if (res == NULL)
1790 goto error;
1791 DISPATCH();
1792 }
1793
Benjamin Petersonddd19492018-09-16 22:38:02 -07001794 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001795 PyObject *v = POP();
1796 PyObject *list = PEEK(oparg);
1797 int err;
1798 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001799 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001800 if (err != 0)
1801 goto error;
1802 PREDICT(JUMP_ABSOLUTE);
1803 DISPATCH();
1804 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001805
Benjamin Petersonddd19492018-09-16 22:38:02 -07001806 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001807 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001808 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001809 int err;
1810 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001811 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001812 if (err != 0)
1813 goto error;
1814 PREDICT(JUMP_ABSOLUTE);
1815 DISPATCH();
1816 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001817
Benjamin Petersonddd19492018-09-16 22:38:02 -07001818 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001819 PyObject *exp = POP();
1820 PyObject *base = TOP();
1821 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1822 Py_DECREF(base);
1823 Py_DECREF(exp);
1824 SET_TOP(res);
1825 if (res == NULL)
1826 goto error;
1827 DISPATCH();
1828 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001829
Benjamin Petersonddd19492018-09-16 22:38:02 -07001830 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001831 PyObject *right = POP();
1832 PyObject *left = TOP();
1833 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1834 Py_DECREF(left);
1835 Py_DECREF(right);
1836 SET_TOP(res);
1837 if (res == NULL)
1838 goto error;
1839 DISPATCH();
1840 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001841
Benjamin Petersonddd19492018-09-16 22:38:02 -07001842 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001843 PyObject *right = POP();
1844 PyObject *left = TOP();
1845 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1846 Py_DECREF(left);
1847 Py_DECREF(right);
1848 SET_TOP(res);
1849 if (res == NULL)
1850 goto error;
1851 DISPATCH();
1852 }
1853
Benjamin Petersonddd19492018-09-16 22:38:02 -07001854 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001855 PyObject *divisor = POP();
1856 PyObject *dividend = TOP();
1857 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1858 Py_DECREF(dividend);
1859 Py_DECREF(divisor);
1860 SET_TOP(quotient);
1861 if (quotient == NULL)
1862 goto error;
1863 DISPATCH();
1864 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001865
Benjamin Petersonddd19492018-09-16 22:38:02 -07001866 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001867 PyObject *divisor = POP();
1868 PyObject *dividend = TOP();
1869 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1870 Py_DECREF(dividend);
1871 Py_DECREF(divisor);
1872 SET_TOP(quotient);
1873 if (quotient == NULL)
1874 goto error;
1875 DISPATCH();
1876 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001877
Benjamin Petersonddd19492018-09-16 22:38:02 -07001878 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001879 PyObject *right = POP();
1880 PyObject *left = TOP();
1881 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1882 Py_DECREF(left);
1883 Py_DECREF(right);
1884 SET_TOP(mod);
1885 if (mod == NULL)
1886 goto error;
1887 DISPATCH();
1888 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001889
Benjamin Petersonddd19492018-09-16 22:38:02 -07001890 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001891 PyObject *right = POP();
1892 PyObject *left = TOP();
1893 PyObject *sum;
1894 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001895 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001896 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001897 }
1898 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001899 sum = PyNumber_InPlaceAdd(left, right);
1900 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001901 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001902 Py_DECREF(right);
1903 SET_TOP(sum);
1904 if (sum == NULL)
1905 goto error;
1906 DISPATCH();
1907 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001908
Benjamin Petersonddd19492018-09-16 22:38:02 -07001909 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001910 PyObject *right = POP();
1911 PyObject *left = TOP();
1912 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1913 Py_DECREF(left);
1914 Py_DECREF(right);
1915 SET_TOP(diff);
1916 if (diff == NULL)
1917 goto error;
1918 DISPATCH();
1919 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001920
Benjamin Petersonddd19492018-09-16 22:38:02 -07001921 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001922 PyObject *right = POP();
1923 PyObject *left = TOP();
1924 PyObject *res = PyNumber_InPlaceLshift(left, right);
1925 Py_DECREF(left);
1926 Py_DECREF(right);
1927 SET_TOP(res);
1928 if (res == NULL)
1929 goto error;
1930 DISPATCH();
1931 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001932
Benjamin Petersonddd19492018-09-16 22:38:02 -07001933 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001934 PyObject *right = POP();
1935 PyObject *left = TOP();
1936 PyObject *res = PyNumber_InPlaceRshift(left, right);
1937 Py_DECREF(left);
1938 Py_DECREF(right);
1939 SET_TOP(res);
1940 if (res == NULL)
1941 goto error;
1942 DISPATCH();
1943 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001944
Benjamin Petersonddd19492018-09-16 22:38:02 -07001945 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001946 PyObject *right = POP();
1947 PyObject *left = TOP();
1948 PyObject *res = PyNumber_InPlaceAnd(left, right);
1949 Py_DECREF(left);
1950 Py_DECREF(right);
1951 SET_TOP(res);
1952 if (res == NULL)
1953 goto error;
1954 DISPATCH();
1955 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001956
Benjamin Petersonddd19492018-09-16 22:38:02 -07001957 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001958 PyObject *right = POP();
1959 PyObject *left = TOP();
1960 PyObject *res = PyNumber_InPlaceXor(left, right);
1961 Py_DECREF(left);
1962 Py_DECREF(right);
1963 SET_TOP(res);
1964 if (res == NULL)
1965 goto error;
1966 DISPATCH();
1967 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001968
Benjamin Petersonddd19492018-09-16 22:38:02 -07001969 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001970 PyObject *right = POP();
1971 PyObject *left = TOP();
1972 PyObject *res = PyNumber_InPlaceOr(left, right);
1973 Py_DECREF(left);
1974 Py_DECREF(right);
1975 SET_TOP(res);
1976 if (res == NULL)
1977 goto error;
1978 DISPATCH();
1979 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001980
Benjamin Petersonddd19492018-09-16 22:38:02 -07001981 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001982 PyObject *sub = TOP();
1983 PyObject *container = SECOND();
1984 PyObject *v = THIRD();
1985 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001986 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001987 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001988 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001989 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001990 Py_DECREF(container);
1991 Py_DECREF(sub);
1992 if (err != 0)
1993 goto error;
1994 DISPATCH();
1995 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001996
Benjamin Petersonddd19492018-09-16 22:38:02 -07001997 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001998 PyObject *sub = TOP();
1999 PyObject *container = SECOND();
2000 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002001 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00002002 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002003 err = PyObject_DelItem(container, sub);
2004 Py_DECREF(container);
2005 Py_DECREF(sub);
2006 if (err != 0)
2007 goto error;
2008 DISPATCH();
2009 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00002010
Benjamin Petersonddd19492018-09-16 22:38:02 -07002011 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01002012 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002013 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01002014 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002015 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002016 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002017 _PyErr_SetString(tstate, PyExc_RuntimeError,
2018 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002019 Py_DECREF(value);
2020 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002021 }
Petr Viktorinffd97532020-02-11 17:46:57 +01002022 res = PyObject_CallOneArg(hook, value);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002023 Py_DECREF(value);
2024 if (res == NULL)
2025 goto error;
2026 Py_DECREF(res);
2027 DISPATCH();
2028 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00002029
Benjamin Petersonddd19492018-09-16 22:38:02 -07002030 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002031 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002032 switch (oparg) {
2033 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002034 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02002035 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002036 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002037 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02002038 /* fall through */
2039 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02002040 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002041 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002042 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002043 break;
2044 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02002045 _PyErr_SetString(tstate, PyExc_SystemError,
2046 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002047 break;
2048 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002049 goto error;
2050 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002051
Benjamin Petersonddd19492018-09-16 22:38:02 -07002052 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002053 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002054 assert(f->f_iblock == 0);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002055 assert(EMPTY());
2056 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002057 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002058
Benjamin Petersonddd19492018-09-16 22:38:02 -07002059 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002060 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002061 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002062 PyObject *obj = TOP();
2063 PyTypeObject *type = Py_TYPE(obj);
2064
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002065 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002066 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002067 }
Yury Selivanov75445082015-05-11 22:57:16 -04002068
2069 if (getter != NULL) {
2070 iter = (*getter)(obj);
2071 Py_DECREF(obj);
2072 if (iter == NULL) {
2073 SET_TOP(NULL);
2074 goto error;
2075 }
2076 }
2077 else {
2078 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002079 _PyErr_Format(tstate, PyExc_TypeError,
2080 "'async for' requires an object with "
2081 "__aiter__ method, got %.100s",
2082 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002083 Py_DECREF(obj);
2084 goto error;
2085 }
2086
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002087 if (Py_TYPE(iter)->tp_as_async == NULL ||
2088 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002089
Yury Selivanov398ff912017-03-02 22:20:00 -05002090 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002091 _PyErr_Format(tstate, PyExc_TypeError,
2092 "'async for' received an object from __aiter__ "
2093 "that does not implement __anext__: %.100s",
2094 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002095 Py_DECREF(iter);
2096 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002097 }
2098
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002099 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04002100 DISPATCH();
2101 }
2102
Benjamin Petersonddd19492018-09-16 22:38:02 -07002103 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002104 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002105 PyObject *next_iter = NULL;
2106 PyObject *awaitable = NULL;
2107 PyObject *aiter = TOP();
2108 PyTypeObject *type = Py_TYPE(aiter);
2109
Yury Selivanoveb636452016-09-08 22:01:51 -07002110 if (PyAsyncGen_CheckExact(aiter)) {
2111 awaitable = type->tp_as_async->am_anext(aiter);
2112 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002113 goto error;
2114 }
Yury Selivanoveb636452016-09-08 22:01:51 -07002115 } else {
2116 if (type->tp_as_async != NULL){
2117 getter = type->tp_as_async->am_anext;
2118 }
Yury Selivanov75445082015-05-11 22:57:16 -04002119
Yury Selivanoveb636452016-09-08 22:01:51 -07002120 if (getter != NULL) {
2121 next_iter = (*getter)(aiter);
2122 if (next_iter == NULL) {
2123 goto error;
2124 }
2125 }
2126 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02002127 _PyErr_Format(tstate, PyExc_TypeError,
2128 "'async for' requires an iterator with "
2129 "__anext__ method, got %.100s",
2130 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07002131 goto error;
2132 }
Yury Selivanov75445082015-05-11 22:57:16 -04002133
Yury Selivanoveb636452016-09-08 22:01:51 -07002134 awaitable = _PyCoro_GetAwaitableIter(next_iter);
2135 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05002136 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07002137 PyExc_TypeError,
2138 "'async for' received an invalid object "
2139 "from __anext__: %.100s",
2140 Py_TYPE(next_iter)->tp_name);
2141
2142 Py_DECREF(next_iter);
2143 goto error;
2144 } else {
2145 Py_DECREF(next_iter);
2146 }
2147 }
Yury Selivanov75445082015-05-11 22:57:16 -04002148
2149 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002150 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002151 DISPATCH();
2152 }
2153
Benjamin Petersonddd19492018-09-16 22:38:02 -07002154 case TARGET(GET_AWAITABLE): {
2155 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002156 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002157 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002158
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002159 if (iter == NULL) {
Mark Shannonfee55262019-11-21 09:11:43 +00002160 int opcode_at_minus_3 = 0;
2161 if ((next_instr - first_instr) > 2) {
2162 opcode_at_minus_3 = _Py_OPCODE(next_instr[-3]);
2163 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002164 format_awaitable_error(tstate, Py_TYPE(iterable),
Mark Shannonfee55262019-11-21 09:11:43 +00002165 opcode_at_minus_3,
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002166 _Py_OPCODE(next_instr[-2]));
2167 }
2168
Yury Selivanov75445082015-05-11 22:57:16 -04002169 Py_DECREF(iterable);
2170
Yury Selivanovc724bae2016-03-02 11:30:46 -05002171 if (iter != NULL && PyCoro_CheckExact(iter)) {
2172 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2173 if (yf != NULL) {
2174 /* `iter` is a coroutine object that is being
2175 awaited, `yf` is a pointer to the current awaitable
2176 being awaited on. */
2177 Py_DECREF(yf);
2178 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002179 _PyErr_SetString(tstate, PyExc_RuntimeError,
2180 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002181 /* The code below jumps to `error` if `iter` is NULL. */
2182 }
2183 }
2184
Yury Selivanov75445082015-05-11 22:57:16 -04002185 SET_TOP(iter); /* Even if it's NULL */
2186
2187 if (iter == NULL) {
2188 goto error;
2189 }
2190
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002191 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002192 DISPATCH();
2193 }
2194
Benjamin Petersonddd19492018-09-16 22:38:02 -07002195 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002196 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002197 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002198 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002199 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
2200 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002201 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002202 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002203 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002204 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002205 else
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02002206 retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002207 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002208 Py_DECREF(v);
2209 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002210 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002211 if (tstate->c_tracefunc != NULL
Victor Stinner438a12d2019-05-24 17:01:38 +02002212 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002213 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002214 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002215 if (err < 0)
2216 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002217 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002218 SET_TOP(val);
2219 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002220 }
Martin Panter95f53c12016-07-18 08:23:26 +00002221 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002222 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002223 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002224 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002225 f->f_lasti -= sizeof(_Py_CODEUNIT);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002226 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002227 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002228
Benjamin Petersonddd19492018-09-16 22:38:02 -07002229 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002230 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002231
2232 if (co->co_flags & CO_ASYNC_GENERATOR) {
2233 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2234 Py_DECREF(retval);
2235 if (w == NULL) {
2236 retval = NULL;
2237 goto error;
2238 }
2239 retval = w;
2240 }
2241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002242 f->f_stacktop = stack_pointer;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002243 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002244 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002245
Benjamin Petersonddd19492018-09-16 22:38:02 -07002246 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002247 PyObject *type, *value, *traceback;
2248 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002249 PyTryBlock *b = PyFrame_BlockPop(f);
2250 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002251 _PyErr_SetString(tstate, PyExc_SystemError,
2252 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002253 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002254 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002255 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2256 STACK_LEVEL() <= (b)->b_level + 4);
2257 exc_info = tstate->exc_info;
2258 type = exc_info->exc_type;
2259 value = exc_info->exc_value;
2260 traceback = exc_info->exc_traceback;
2261 exc_info->exc_type = POP();
2262 exc_info->exc_value = POP();
2263 exc_info->exc_traceback = POP();
2264 Py_XDECREF(type);
2265 Py_XDECREF(value);
2266 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002267 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002268 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002269
Benjamin Petersonddd19492018-09-16 22:38:02 -07002270 case TARGET(POP_BLOCK): {
2271 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002272 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002273 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002274 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002275
Mark Shannonfee55262019-11-21 09:11:43 +00002276 case TARGET(RERAISE): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002277 PyObject *exc = POP();
Mark Shannonfee55262019-11-21 09:11:43 +00002278 PyObject *val = POP();
2279 PyObject *tb = POP();
2280 assert(PyExceptionClass_Check(exc));
Victor Stinner61f4db82020-01-28 03:37:45 +01002281 _PyErr_Restore(tstate, exc, val, tb);
Mark Shannonfee55262019-11-21 09:11:43 +00002282 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002283 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002284
Benjamin Petersonddd19492018-09-16 22:38:02 -07002285 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002286 PyObject *exc = POP();
2287 assert(PyExceptionClass_Check(exc));
2288 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2289 PyTryBlock *b = PyFrame_BlockPop(f);
2290 assert(b->b_type == EXCEPT_HANDLER);
2291 Py_DECREF(exc);
2292 UNWIND_EXCEPT_HANDLER(b);
2293 Py_DECREF(POP());
2294 JUMPBY(oparg);
2295 FAST_DISPATCH();
2296 }
2297 else {
2298 PyObject *val = POP();
2299 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002300 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002301 goto exception_unwind;
2302 }
2303 }
2304
Zackery Spytzce6a0702019-08-25 03:44:09 -06002305 case TARGET(LOAD_ASSERTION_ERROR): {
2306 PyObject *value = PyExc_AssertionError;
2307 Py_INCREF(value);
2308 PUSH(value);
2309 FAST_DISPATCH();
2310 }
2311
Benjamin Petersonddd19492018-09-16 22:38:02 -07002312 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002313 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002314
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002315 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002316 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002317 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002318 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002319 if (!_PyErr_Occurred(tstate)) {
2320 _PyErr_SetString(tstate, PyExc_NameError,
2321 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002322 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002323 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002324 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002325 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002326 }
2327 else {
2328 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2329 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002330 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002331 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2332 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002333 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2334 _PyErr_SetString(tstate, PyExc_NameError,
2335 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002336 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002337 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002338 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002339 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002340 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002341 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002342
Benjamin Petersonddd19492018-09-16 22:38:02 -07002343 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002344 PyObject *name = GETITEM(names, oparg);
2345 PyObject *v = POP();
2346 PyObject *ns = f->f_locals;
2347 int err;
2348 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002349 _PyErr_Format(tstate, PyExc_SystemError,
2350 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002351 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002352 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002353 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002354 if (PyDict_CheckExact(ns))
2355 err = PyDict_SetItem(ns, name, v);
2356 else
2357 err = PyObject_SetItem(ns, name, v);
2358 Py_DECREF(v);
2359 if (err != 0)
2360 goto error;
2361 DISPATCH();
2362 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002363
Benjamin Petersonddd19492018-09-16 22:38:02 -07002364 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002365 PyObject *name = GETITEM(names, oparg);
2366 PyObject *ns = f->f_locals;
2367 int err;
2368 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002369 _PyErr_Format(tstate, PyExc_SystemError,
2370 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002371 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002372 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002373 err = PyObject_DelItem(ns, name);
2374 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002375 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002376 NAME_ERROR_MSG,
2377 name);
2378 goto error;
2379 }
2380 DISPATCH();
2381 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002382
Benjamin Petersonddd19492018-09-16 22:38:02 -07002383 case TARGET(UNPACK_SEQUENCE): {
2384 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002385 PyObject *seq = POP(), *item, **items;
2386 if (PyTuple_CheckExact(seq) &&
2387 PyTuple_GET_SIZE(seq) == oparg) {
2388 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002390 item = items[oparg];
2391 Py_INCREF(item);
2392 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002393 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002394 } else if (PyList_CheckExact(seq) &&
2395 PyList_GET_SIZE(seq) == oparg) {
2396 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002397 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002398 item = items[oparg];
2399 Py_INCREF(item);
2400 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002402 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002403 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002404 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002405 } else {
2406 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002407 Py_DECREF(seq);
2408 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002409 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002410 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002411 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002412 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002413
Benjamin Petersonddd19492018-09-16 22:38:02 -07002414 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002415 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2416 PyObject *seq = POP();
2417
Victor Stinner438a12d2019-05-24 17:01:38 +02002418 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002419 stack_pointer + totalargs)) {
2420 stack_pointer += totalargs;
2421 } else {
2422 Py_DECREF(seq);
2423 goto error;
2424 }
2425 Py_DECREF(seq);
2426 DISPATCH();
2427 }
2428
Benjamin Petersonddd19492018-09-16 22:38:02 -07002429 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002430 PyObject *name = GETITEM(names, oparg);
2431 PyObject *owner = TOP();
2432 PyObject *v = SECOND();
2433 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002434 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002435 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002436 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002437 Py_DECREF(owner);
2438 if (err != 0)
2439 goto error;
2440 DISPATCH();
2441 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002442
Benjamin Petersonddd19492018-09-16 22:38:02 -07002443 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002444 PyObject *name = GETITEM(names, oparg);
2445 PyObject *owner = POP();
2446 int err;
2447 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2448 Py_DECREF(owner);
2449 if (err != 0)
2450 goto error;
2451 DISPATCH();
2452 }
2453
Benjamin Petersonddd19492018-09-16 22:38:02 -07002454 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002455 PyObject *name = GETITEM(names, oparg);
2456 PyObject *v = POP();
2457 int err;
2458 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002459 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002460 if (err != 0)
2461 goto error;
2462 DISPATCH();
2463 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002464
Benjamin Petersonddd19492018-09-16 22:38:02 -07002465 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002466 PyObject *name = GETITEM(names, oparg);
2467 int err;
2468 err = PyDict_DelItem(f->f_globals, name);
2469 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002470 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2471 format_exc_check_arg(tstate, PyExc_NameError,
2472 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002473 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002474 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002475 }
2476 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002477 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002478
Benjamin Petersonddd19492018-09-16 22:38:02 -07002479 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002480 PyObject *name = GETITEM(names, oparg);
2481 PyObject *locals = f->f_locals;
2482 PyObject *v;
2483 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002484 _PyErr_Format(tstate, PyExc_SystemError,
2485 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002486 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002487 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002488 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002489 v = PyDict_GetItemWithError(locals, name);
2490 if (v != NULL) {
2491 Py_INCREF(v);
2492 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002493 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002494 goto error;
2495 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002496 }
2497 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002498 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002499 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002500 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002501 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002502 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002503 }
2504 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002505 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002506 v = PyDict_GetItemWithError(f->f_globals, name);
2507 if (v != NULL) {
2508 Py_INCREF(v);
2509 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002510 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002511 goto error;
2512 }
2513 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002514 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002515 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002516 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002517 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002518 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002519 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002520 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002521 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002522 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002523 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002524 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002525 }
2526 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002527 v = PyObject_GetItem(f->f_builtins, name);
2528 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002529 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002530 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002531 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002532 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002533 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002534 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002535 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002536 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002537 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002538 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002539 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002540 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002541 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002542
Benjamin Petersonddd19492018-09-16 22:38:02 -07002543 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002544 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002545 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002546 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002547 && PyDict_CheckExact(f->f_builtins))
2548 {
Inada Naoki91234a12019-06-03 21:30:58 +09002549 OPCACHE_CHECK();
2550 if (co_opcache != NULL && co_opcache->optimized > 0) {
2551 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2552
2553 if (lg->globals_ver ==
2554 ((PyDictObject *)f->f_globals)->ma_version_tag
2555 && lg->builtins_ver ==
2556 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2557 {
2558 PyObject *ptr = lg->ptr;
2559 OPCACHE_STAT_GLOBAL_HIT();
2560 assert(ptr != NULL);
2561 Py_INCREF(ptr);
2562 PUSH(ptr);
2563 DISPATCH();
2564 }
2565 }
2566
2567 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002568 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002569 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002570 name);
2571 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002572 if (!_PyErr_OCCURRED()) {
2573 /* _PyDict_LoadGlobal() returns NULL without raising
2574 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002575 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002576 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002577 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002578 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002579 }
Inada Naoki91234a12019-06-03 21:30:58 +09002580
2581 if (co_opcache != NULL) {
2582 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2583
2584 if (co_opcache->optimized == 0) {
2585 /* Wasn't optimized before. */
2586 OPCACHE_STAT_GLOBAL_OPT();
2587 } else {
2588 OPCACHE_STAT_GLOBAL_MISS();
2589 }
2590
2591 co_opcache->optimized = 1;
2592 lg->globals_ver =
2593 ((PyDictObject *)f->f_globals)->ma_version_tag;
2594 lg->builtins_ver =
2595 ((PyDictObject *)f->f_builtins)->ma_version_tag;
2596 lg->ptr = v; /* borrowed */
2597 }
2598
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002599 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002600 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002601 else {
2602 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002603
2604 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09002605 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002606 v = PyObject_GetItem(f->f_globals, name);
2607 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002608 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002609 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002610 }
2611 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002612
Victor Stinnerb4efc962015-11-20 09:24:02 +01002613 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002614 v = PyObject_GetItem(f->f_builtins, name);
2615 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002616 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002617 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002618 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002619 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002620 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002621 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002622 }
2623 }
2624 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002625 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002626 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002627 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002628
Benjamin Petersonddd19492018-09-16 22:38:02 -07002629 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002630 PyObject *v = GETLOCAL(oparg);
2631 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002632 SETLOCAL(oparg, NULL);
2633 DISPATCH();
2634 }
2635 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002636 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002637 UNBOUNDLOCAL_ERROR_MSG,
2638 PyTuple_GetItem(co->co_varnames, oparg)
2639 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002640 goto error;
2641 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002642
Benjamin Petersonddd19492018-09-16 22:38:02 -07002643 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002644 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002645 PyObject *oldobj = PyCell_GET(cell);
2646 if (oldobj != NULL) {
2647 PyCell_SET(cell, NULL);
2648 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002649 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002650 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002651 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002652 goto error;
2653 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002654
Benjamin Petersonddd19492018-09-16 22:38:02 -07002655 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002656 PyObject *cell = freevars[oparg];
2657 Py_INCREF(cell);
2658 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002659 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002660 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002661
Benjamin Petersonddd19492018-09-16 22:38:02 -07002662 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002663 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002664 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002665 assert(locals);
2666 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2667 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2668 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2669 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2670 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002671 value = PyDict_GetItemWithError(locals, name);
2672 if (value != NULL) {
2673 Py_INCREF(value);
2674 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002675 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002676 goto error;
2677 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002678 }
2679 else {
2680 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002681 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002682 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002683 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002684 }
2685 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002686 }
2687 }
2688 if (!value) {
2689 PyObject *cell = freevars[oparg];
2690 value = PyCell_GET(cell);
2691 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002692 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002693 goto error;
2694 }
2695 Py_INCREF(value);
2696 }
2697 PUSH(value);
2698 DISPATCH();
2699 }
2700
Benjamin Petersonddd19492018-09-16 22:38:02 -07002701 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002702 PyObject *cell = freevars[oparg];
2703 PyObject *value = PyCell_GET(cell);
2704 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002705 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002706 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002707 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002708 Py_INCREF(value);
2709 PUSH(value);
2710 DISPATCH();
2711 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002712
Benjamin Petersonddd19492018-09-16 22:38:02 -07002713 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002714 PyObject *v = POP();
2715 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002716 PyObject *oldobj = PyCell_GET(cell);
2717 PyCell_SET(cell, v);
2718 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002719 DISPATCH();
2720 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002721
Benjamin Petersonddd19492018-09-16 22:38:02 -07002722 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002723 PyObject *str;
2724 PyObject *empty = PyUnicode_New(0, 0);
2725 if (empty == NULL) {
2726 goto error;
2727 }
2728 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2729 Py_DECREF(empty);
2730 if (str == NULL)
2731 goto error;
2732 while (--oparg >= 0) {
2733 PyObject *item = POP();
2734 Py_DECREF(item);
2735 }
2736 PUSH(str);
2737 DISPATCH();
2738 }
2739
Benjamin Petersonddd19492018-09-16 22:38:02 -07002740 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002741 PyObject *tup = PyTuple_New(oparg);
2742 if (tup == NULL)
2743 goto error;
2744 while (--oparg >= 0) {
2745 PyObject *item = POP();
2746 PyTuple_SET_ITEM(tup, oparg, item);
2747 }
2748 PUSH(tup);
2749 DISPATCH();
2750 }
2751
Benjamin Petersonddd19492018-09-16 22:38:02 -07002752 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002753 PyObject *list = PyList_New(oparg);
2754 if (list == NULL)
2755 goto error;
2756 while (--oparg >= 0) {
2757 PyObject *item = POP();
2758 PyList_SET_ITEM(list, oparg, item);
2759 }
2760 PUSH(list);
2761 DISPATCH();
2762 }
2763
Mark Shannon13bc1392020-01-23 09:25:17 +00002764 case TARGET(LIST_TO_TUPLE): {
2765 PyObject *list = POP();
2766 PyObject *tuple = PyList_AsTuple(list);
2767 Py_DECREF(list);
2768 if (tuple == NULL) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002769 goto error;
Mark Shannon13bc1392020-01-23 09:25:17 +00002770 }
2771 PUSH(tuple);
2772 DISPATCH();
2773 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002774
Mark Shannon13bc1392020-01-23 09:25:17 +00002775 case TARGET(LIST_EXTEND): {
2776 PyObject *iterable = POP();
2777 PyObject *list = PEEK(oparg);
2778 PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
2779 if (none_val == NULL) {
2780 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01002781 (Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable)))
Mark Shannon13bc1392020-01-23 09:25:17 +00002782 {
Victor Stinner61f4db82020-01-28 03:37:45 +01002783 _PyErr_Clear(tstate);
Mark Shannon13bc1392020-01-23 09:25:17 +00002784 _PyErr_Format(tstate, PyExc_TypeError,
2785 "Value after * must be an iterable, not %.200s",
2786 Py_TYPE(iterable)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002787 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002788 Py_DECREF(iterable);
2789 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002790 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002791 Py_DECREF(none_val);
2792 Py_DECREF(iterable);
2793 DISPATCH();
2794 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002795
Mark Shannon13bc1392020-01-23 09:25:17 +00002796 case TARGET(SET_UPDATE): {
2797 PyObject *iterable = POP();
2798 PyObject *set = PEEK(oparg);
2799 int err = _PySet_Update(set, iterable);
2800 Py_DECREF(iterable);
2801 if (err < 0) {
2802 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002803 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002804 DISPATCH();
2805 }
2806
Benjamin Petersonddd19492018-09-16 22:38:02 -07002807 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002808 PyObject *set = PySet_New(NULL);
2809 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002810 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002811 if (set == NULL)
2812 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002813 for (i = oparg; i > 0; i--) {
2814 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002815 if (err == 0)
2816 err = PySet_Add(set, item);
2817 Py_DECREF(item);
2818 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002819 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002820 if (err != 0) {
2821 Py_DECREF(set);
2822 goto error;
2823 }
2824 PUSH(set);
2825 DISPATCH();
2826 }
2827
Benjamin Petersonddd19492018-09-16 22:38:02 -07002828 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002829 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002830 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2831 if (map == NULL)
2832 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002833 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002834 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002835 PyObject *key = PEEK(2*i);
2836 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002837 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002838 if (err != 0) {
2839 Py_DECREF(map);
2840 goto error;
2841 }
2842 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002843
2844 while (oparg--) {
2845 Py_DECREF(POP());
2846 Py_DECREF(POP());
2847 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002848 PUSH(map);
2849 DISPATCH();
2850 }
2851
Benjamin Petersonddd19492018-09-16 22:38:02 -07002852 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002853 _Py_IDENTIFIER(__annotations__);
2854 int err;
2855 PyObject *ann_dict;
2856 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002857 _PyErr_Format(tstate, PyExc_SystemError,
2858 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002859 goto error;
2860 }
2861 /* check if __annotations__ in locals()... */
2862 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002863 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002864 &PyId___annotations__);
2865 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002866 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002867 goto error;
2868 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002869 /* ...if not, create a new one */
2870 ann_dict = PyDict_New();
2871 if (ann_dict == NULL) {
2872 goto error;
2873 }
2874 err = _PyDict_SetItemId(f->f_locals,
2875 &PyId___annotations__, ann_dict);
2876 Py_DECREF(ann_dict);
2877 if (err != 0) {
2878 goto error;
2879 }
2880 }
2881 }
2882 else {
2883 /* do the same if locals() is not a dict */
2884 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2885 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002886 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002887 }
2888 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2889 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002890 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002891 goto error;
2892 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002893 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002894 ann_dict = PyDict_New();
2895 if (ann_dict == NULL) {
2896 goto error;
2897 }
2898 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2899 Py_DECREF(ann_dict);
2900 if (err != 0) {
2901 goto error;
2902 }
2903 }
2904 else {
2905 Py_DECREF(ann_dict);
2906 }
2907 }
2908 DISPATCH();
2909 }
2910
Benjamin Petersonddd19492018-09-16 22:38:02 -07002911 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002912 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002913 PyObject *map;
2914 PyObject *keys = TOP();
2915 if (!PyTuple_CheckExact(keys) ||
2916 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002917 _PyErr_SetString(tstate, PyExc_SystemError,
2918 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002919 goto error;
2920 }
2921 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2922 if (map == NULL) {
2923 goto error;
2924 }
2925 for (i = oparg; i > 0; i--) {
2926 int err;
2927 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2928 PyObject *value = PEEK(i + 1);
2929 err = PyDict_SetItem(map, key, value);
2930 if (err != 0) {
2931 Py_DECREF(map);
2932 goto error;
2933 }
2934 }
2935
2936 Py_DECREF(POP());
2937 while (oparg--) {
2938 Py_DECREF(POP());
2939 }
2940 PUSH(map);
2941 DISPATCH();
2942 }
2943
Mark Shannon8a4cd702020-01-27 09:57:45 +00002944 case TARGET(DICT_UPDATE): {
2945 PyObject *update = POP();
2946 PyObject *dict = PEEK(oparg);
2947 if (PyDict_Update(dict, update) < 0) {
2948 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
2949 _PyErr_Format(tstate, PyExc_TypeError,
2950 "'%.200s' object is not a mapping",
Victor Stinnera102ed72020-02-07 02:24:48 +01002951 Py_TYPE(update)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002952 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002953 Py_DECREF(update);
2954 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002955 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002956 Py_DECREF(update);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002957 DISPATCH();
2958 }
2959
Mark Shannon8a4cd702020-01-27 09:57:45 +00002960 case TARGET(DICT_MERGE): {
2961 PyObject *update = POP();
2962 PyObject *dict = PEEK(oparg);
2963
2964 if (_PyDict_MergeEx(dict, update, 2) < 0) {
2965 format_kwargs_error(tstate, PEEK(2 + oparg), update);
2966 Py_DECREF(update);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002967 goto error;
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002968 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002969 Py_DECREF(update);
Brandt Bucherf185a732019-09-28 17:12:49 -07002970 PREDICT(CALL_FUNCTION_EX);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002971 DISPATCH();
2972 }
2973
Benjamin Petersonddd19492018-09-16 22:38:02 -07002974 case TARGET(MAP_ADD): {
Jörn Heisslerc8a35412019-06-22 16:40:55 +02002975 PyObject *value = TOP();
2976 PyObject *key = SECOND();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002977 PyObject *map;
2978 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002979 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002980 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002981 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002982 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002983 Py_DECREF(value);
2984 Py_DECREF(key);
2985 if (err != 0)
2986 goto error;
2987 PREDICT(JUMP_ABSOLUTE);
2988 DISPATCH();
2989 }
2990
Benjamin Petersonddd19492018-09-16 22:38:02 -07002991 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002992 PyObject *name = GETITEM(names, oparg);
2993 PyObject *owner = TOP();
2994 PyObject *res = PyObject_GetAttr(owner, name);
2995 Py_DECREF(owner);
2996 SET_TOP(res);
2997 if (res == NULL)
2998 goto error;
2999 DISPATCH();
3000 }
3001
Benjamin Petersonddd19492018-09-16 22:38:02 -07003002 case TARGET(COMPARE_OP): {
Mark Shannon9af0e472020-01-14 10:12:45 +00003003 assert(oparg <= Py_GE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003004 PyObject *right = POP();
3005 PyObject *left = TOP();
Mark Shannon9af0e472020-01-14 10:12:45 +00003006 PyObject *res = PyObject_RichCompare(left, right, oparg);
3007 SET_TOP(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003008 Py_DECREF(left);
3009 Py_DECREF(right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003010 if (res == NULL)
3011 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003012 PREDICT(POP_JUMP_IF_FALSE);
3013 PREDICT(POP_JUMP_IF_TRUE);
3014 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003015 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003016
Mark Shannon9af0e472020-01-14 10:12:45 +00003017 case TARGET(IS_OP): {
3018 PyObject *right = POP();
3019 PyObject *left = TOP();
3020 int res = (left == right)^oparg;
3021 PyObject *b = res ? Py_True : Py_False;
3022 Py_INCREF(b);
3023 SET_TOP(b);
3024 Py_DECREF(left);
3025 Py_DECREF(right);
3026 PREDICT(POP_JUMP_IF_FALSE);
3027 PREDICT(POP_JUMP_IF_TRUE);
3028 FAST_DISPATCH();
3029 }
3030
3031 case TARGET(CONTAINS_OP): {
3032 PyObject *right = POP();
3033 PyObject *left = POP();
3034 int res = PySequence_Contains(right, left);
3035 Py_DECREF(left);
3036 Py_DECREF(right);
3037 if (res < 0) {
3038 goto error;
3039 }
3040 PyObject *b = (res^oparg) ? Py_True : Py_False;
3041 Py_INCREF(b);
3042 PUSH(b);
3043 PREDICT(POP_JUMP_IF_FALSE);
3044 PREDICT(POP_JUMP_IF_TRUE);
3045 FAST_DISPATCH();
3046 }
3047
3048#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
3049 "BaseException is not allowed"
3050
3051 case TARGET(JUMP_IF_NOT_EXC_MATCH): {
3052 PyObject *right = POP();
3053 PyObject *left = POP();
3054 if (PyTuple_Check(right)) {
3055 Py_ssize_t i, length;
3056 length = PyTuple_GET_SIZE(right);
3057 for (i = 0; i < length; i++) {
3058 PyObject *exc = PyTuple_GET_ITEM(right, i);
3059 if (!PyExceptionClass_Check(exc)) {
3060 _PyErr_SetString(tstate, PyExc_TypeError,
3061 CANNOT_CATCH_MSG);
3062 Py_DECREF(left);
3063 Py_DECREF(right);
3064 goto error;
3065 }
3066 }
3067 }
3068 else {
3069 if (!PyExceptionClass_Check(right)) {
3070 _PyErr_SetString(tstate, PyExc_TypeError,
3071 CANNOT_CATCH_MSG);
3072 Py_DECREF(left);
3073 Py_DECREF(right);
3074 goto error;
3075 }
3076 }
3077 int res = PyErr_GivenExceptionMatches(left, right);
3078 Py_DECREF(left);
3079 Py_DECREF(right);
3080 if (res > 0) {
3081 /* Exception matches -- Do nothing */;
3082 }
3083 else if (res == 0) {
3084 JUMPTO(oparg);
3085 }
3086 else {
3087 goto error;
3088 }
3089 DISPATCH();
3090 }
3091
Benjamin Petersonddd19492018-09-16 22:38:02 -07003092 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003093 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003094 PyObject *fromlist = POP();
3095 PyObject *level = TOP();
3096 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003097 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003098 Py_DECREF(level);
3099 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003100 SET_TOP(res);
3101 if (res == NULL)
3102 goto error;
3103 DISPATCH();
3104 }
3105
Benjamin Petersonddd19492018-09-16 22:38:02 -07003106 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003107 PyObject *from = POP(), *locals;
3108 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003109 if (PyFrame_FastToLocalsWithError(f) < 0) {
3110 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01003111 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003112 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01003113
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003114 locals = f->f_locals;
3115 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003116 _PyErr_SetString(tstate, PyExc_SystemError,
3117 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003118 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003119 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003120 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003121 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003122 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003123 Py_DECREF(from);
3124 if (err != 0)
3125 goto error;
3126 DISPATCH();
3127 }
Guido van Rossum25831651993-05-19 14:50:45 +00003128
Benjamin Petersonddd19492018-09-16 22:38:02 -07003129 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003130 PyObject *name = GETITEM(names, oparg);
3131 PyObject *from = TOP();
3132 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003133 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003134 PUSH(res);
3135 if (res == NULL)
3136 goto error;
3137 DISPATCH();
3138 }
Thomas Wouters52152252000-08-17 22:55:00 +00003139
Benjamin Petersonddd19492018-09-16 22:38:02 -07003140 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003141 JUMPBY(oparg);
3142 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003143 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003144
Benjamin Petersonddd19492018-09-16 22:38:02 -07003145 case TARGET(POP_JUMP_IF_FALSE): {
3146 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003147 PyObject *cond = POP();
3148 int err;
3149 if (cond == Py_True) {
3150 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003151 FAST_DISPATCH();
3152 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003153 if (cond == Py_False) {
3154 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003155 JUMPTO(oparg);
3156 FAST_DISPATCH();
3157 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003158 err = PyObject_IsTrue(cond);
3159 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003160 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003161 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003162 else if (err == 0)
3163 JUMPTO(oparg);
3164 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003165 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003166 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003167 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003168
Benjamin Petersonddd19492018-09-16 22:38:02 -07003169 case TARGET(POP_JUMP_IF_TRUE): {
3170 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003171 PyObject *cond = POP();
3172 int err;
3173 if (cond == Py_False) {
3174 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003175 FAST_DISPATCH();
3176 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003177 if (cond == Py_True) {
3178 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003179 JUMPTO(oparg);
3180 FAST_DISPATCH();
3181 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003182 err = PyObject_IsTrue(cond);
3183 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003184 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003185 JUMPTO(oparg);
3186 }
3187 else if (err == 0)
3188 ;
3189 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003190 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003191 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003192 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003193
Benjamin Petersonddd19492018-09-16 22:38:02 -07003194 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003195 PyObject *cond = TOP();
3196 int err;
3197 if (cond == Py_True) {
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 FAST_DISPATCH();
3201 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003202 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003203 JUMPTO(oparg);
3204 FAST_DISPATCH();
3205 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003206 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003207 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003208 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003209 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003210 }
3211 else if (err == 0)
3212 JUMPTO(oparg);
3213 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003214 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003215 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003216 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003217
Benjamin Petersonddd19492018-09-16 22:38:02 -07003218 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003219 PyObject *cond = TOP();
3220 int err;
3221 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003222 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003223 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003224 FAST_DISPATCH();
3225 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003226 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003227 JUMPTO(oparg);
3228 FAST_DISPATCH();
3229 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003230 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003231 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003232 JUMPTO(oparg);
3233 }
3234 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003235 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003236 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003237 }
3238 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003239 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003240 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003241 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003242
Benjamin Petersonddd19492018-09-16 22:38:02 -07003243 case TARGET(JUMP_ABSOLUTE): {
3244 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003245 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003246#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003247 /* Enabling this path speeds-up all while and for-loops by bypassing
3248 the per-loop checks for signals. By default, this should be turned-off
3249 because it prevents detection of a control-break in tight loops like
3250 "while 1: pass". Compile with this option turned-on when you need
3251 the speed-up and do not need break checking inside tight loops (ones
3252 that contain only instructions ending with FAST_DISPATCH).
3253 */
3254 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003255#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003256 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003257#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003258 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003259
Benjamin Petersonddd19492018-09-16 22:38:02 -07003260 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003261 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003262 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003263 PyObject *iter = PyObject_GetIter(iterable);
3264 Py_DECREF(iterable);
3265 SET_TOP(iter);
3266 if (iter == NULL)
3267 goto error;
3268 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003269 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003270 DISPATCH();
3271 }
3272
Benjamin Petersonddd19492018-09-16 22:38:02 -07003273 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04003274 /* before: [obj]; after [getiter(obj)] */
3275 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003276 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003277 if (PyCoro_CheckExact(iterable)) {
3278 /* `iterable` is a coroutine */
3279 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3280 /* and it is used in a 'yield from' expression of a
3281 regular generator. */
3282 Py_DECREF(iterable);
3283 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003284 _PyErr_SetString(tstate, PyExc_TypeError,
3285 "cannot 'yield from' a coroutine object "
3286 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04003287 goto error;
3288 }
3289 }
3290 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003291 /* `iterable` is not a generator. */
3292 iter = PyObject_GetIter(iterable);
3293 Py_DECREF(iterable);
3294 SET_TOP(iter);
3295 if (iter == NULL)
3296 goto error;
3297 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003298 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003299 DISPATCH();
3300 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003301
Benjamin Petersonddd19492018-09-16 22:38:02 -07003302 case TARGET(FOR_ITER): {
3303 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003304 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003305 PyObject *iter = TOP();
Victor Stinnera102ed72020-02-07 02:24:48 +01003306 PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003307 if (next != NULL) {
3308 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003309 PREDICT(STORE_FAST);
3310 PREDICT(UNPACK_SEQUENCE);
3311 DISPATCH();
3312 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003313 if (_PyErr_Occurred(tstate)) {
3314 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003315 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003316 }
3317 else if (tstate->c_tracefunc != NULL) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003318 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Victor Stinner438a12d2019-05-24 17:01:38 +02003319 }
3320 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003321 }
3322 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003323 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003324 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003325 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003326 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003327 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003328 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003329
Benjamin Petersonddd19492018-09-16 22:38:02 -07003330 case TARGET(SETUP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003331 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003332 STACK_LEVEL());
3333 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003334 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003335
Benjamin Petersonddd19492018-09-16 22:38:02 -07003336 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003337 _Py_IDENTIFIER(__aenter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003338 _Py_IDENTIFIER(__aexit__);
Yury Selivanov75445082015-05-11 22:57:16 -04003339 PyObject *mgr = TOP();
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003340 PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04003341 PyObject *res;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003342 if (enter == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003343 goto error;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003344 }
3345 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__);
3346 if (exit == NULL) {
3347 Py_DECREF(enter);
3348 goto error;
3349 }
Yury Selivanov75445082015-05-11 22:57:16 -04003350 SET_TOP(exit);
Yury Selivanov75445082015-05-11 22:57:16 -04003351 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003352 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003353 Py_DECREF(enter);
3354 if (res == NULL)
3355 goto error;
3356 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003357 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003358 DISPATCH();
3359 }
3360
Benjamin Petersonddd19492018-09-16 22:38:02 -07003361 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003362 PyObject *res = POP();
3363 /* Setup the finally block before pushing the result
3364 of __aenter__ on the stack. */
3365 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3366 STACK_LEVEL());
3367 PUSH(res);
3368 DISPATCH();
3369 }
3370
Benjamin Petersonddd19492018-09-16 22:38:02 -07003371 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003372 _Py_IDENTIFIER(__enter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003373 _Py_IDENTIFIER(__exit__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003374 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003375 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003376 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003377 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08003378 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003379 }
3380 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003381 if (exit == NULL) {
3382 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003383 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003384 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003385 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003386 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003387 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003388 Py_DECREF(enter);
3389 if (res == NULL)
3390 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003391 /* Setup the finally block before pushing the result
3392 of __enter__ on the stack. */
3393 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3394 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003395
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003396 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003397 DISPATCH();
3398 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003399
Mark Shannonfee55262019-11-21 09:11:43 +00003400 case TARGET(WITH_EXCEPT_START): {
3401 /* At the top of the stack are 7 values:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003402 - (TOP, SECOND, THIRD) = exc_info()
Mark Shannonfee55262019-11-21 09:11:43 +00003403 - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER
3404 - SEVENTH: the context.__exit__ bound method
3405 We call SEVENTH(TOP, SECOND, THIRD).
3406 Then we push again the TOP exception and the __exit__
3407 return value.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003408 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003409 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003410 PyObject *exc, *val, *tb, *res;
3411
Victor Stinner842cfff2016-12-01 14:45:31 +01003412 exc = TOP();
Mark Shannonfee55262019-11-21 09:11:43 +00003413 val = SECOND();
3414 tb = THIRD();
3415 assert(exc != Py_None);
3416 assert(!PyLong_Check(exc));
3417 exit_func = PEEK(7);
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003418 PyObject *stack[4] = {NULL, exc, val, tb};
Petr Viktorinffd97532020-02-11 17:46:57 +01003419 res = PyObject_Vectorcall(exit_func, stack + 1,
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003420 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003421 if (res == NULL)
3422 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003423
Yury Selivanov75445082015-05-11 22:57:16 -04003424 PUSH(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003425 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003426 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003427
Benjamin Petersonddd19492018-09-16 22:38:02 -07003428 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10003429 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003430 PyObject *name = GETITEM(names, oparg);
3431 PyObject *obj = TOP();
3432 PyObject *meth = NULL;
3433
3434 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3435
Yury Selivanovf2392132016-12-13 19:03:51 -05003436 if (meth == NULL) {
3437 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003438 goto error;
3439 }
3440
3441 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003442 /* We can bypass temporary bound method object.
3443 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003444
INADA Naoki015bce62017-01-16 17:23:30 +09003445 meth | self | arg1 | ... | argN
3446 */
3447 SET_TOP(meth);
3448 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003449 }
3450 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003451 /* meth is not an unbound method (but a regular attr, or
3452 something was returned by a descriptor protocol). Set
3453 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003454 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003455
3456 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003457 */
INADA Naoki015bce62017-01-16 17:23:30 +09003458 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003459 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003460 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003461 }
3462 DISPATCH();
3463 }
3464
Benjamin Petersonddd19492018-09-16 22:38:02 -07003465 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003466 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003467 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003468
3469 sp = stack_pointer;
3470
INADA Naoki015bce62017-01-16 17:23:30 +09003471 meth = PEEK(oparg + 2);
3472 if (meth == NULL) {
3473 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3474 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003475
3476 Stack layout:
3477
INADA Naoki015bce62017-01-16 17:23:30 +09003478 ... | NULL | callable | arg1 | ... | argN
3479 ^- TOP()
3480 ^- (-oparg)
3481 ^- (-oparg-1)
3482 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003483
Ville Skyttä49b27342017-08-03 09:00:59 +03003484 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003485 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003486 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003487 res = call_function(tstate, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003488 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003489 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003490 }
3491 else {
3492 /* This is a method call. Stack layout:
3493
INADA Naoki015bce62017-01-16 17:23:30 +09003494 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003495 ^- TOP()
3496 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003497 ^- (-oparg-1)
3498 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003499
INADA Naoki015bce62017-01-16 17:23:30 +09003500 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003501 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003502 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003503 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003504 res = call_function(tstate, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003505 stack_pointer = sp;
3506 }
3507
3508 PUSH(res);
3509 if (res == NULL)
3510 goto error;
3511 DISPATCH();
3512 }
3513
Benjamin Petersonddd19492018-09-16 22:38:02 -07003514 case TARGET(CALL_FUNCTION): {
3515 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003516 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003517 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003518 res = call_function(tstate, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003519 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003520 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003521 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003522 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003523 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003524 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003525 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003526
Benjamin Petersonddd19492018-09-16 22:38:02 -07003527 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003528 PyObject **sp, *res, *names;
3529
3530 names = POP();
Jeroen Demeyer05677862019-08-16 12:41:27 +02003531 assert(PyTuple_Check(names));
3532 assert(PyTuple_GET_SIZE(names) <= oparg);
3533 /* We assume without checking that names contains only strings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003534 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003535 res = call_function(tstate, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003536 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003537 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003538 Py_DECREF(names);
3539
3540 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003541 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003542 }
3543 DISPATCH();
3544 }
3545
Benjamin Petersonddd19492018-09-16 22:38:02 -07003546 case TARGET(CALL_FUNCTION_EX): {
Brandt Bucherf185a732019-09-28 17:12:49 -07003547 PREDICTED(CALL_FUNCTION_EX);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003548 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003549 if (oparg & 0x01) {
3550 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003551 if (!PyDict_CheckExact(kwargs)) {
3552 PyObject *d = PyDict_New();
3553 if (d == NULL)
3554 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003555 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003556 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02003557 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003558 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003559 goto error;
3560 }
3561 Py_DECREF(kwargs);
3562 kwargs = d;
3563 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003564 assert(PyDict_CheckExact(kwargs));
3565 }
3566 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003567 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003568 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003569 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003570 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003571 goto error;
3572 }
3573 Py_SETREF(callargs, PySequence_Tuple(callargs));
3574 if (callargs == NULL) {
3575 goto error;
3576 }
3577 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003578 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003579
Victor Stinner09532fe2019-05-10 23:39:09 +02003580 result = do_call_core(tstate, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003581 Py_DECREF(func);
3582 Py_DECREF(callargs);
3583 Py_XDECREF(kwargs);
3584
3585 SET_TOP(result);
3586 if (result == NULL) {
3587 goto error;
3588 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003589 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003590 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003591
Benjamin Petersonddd19492018-09-16 22:38:02 -07003592 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003593 PyObject *qualname = POP();
3594 PyObject *codeobj = POP();
3595 PyFunctionObject *func = (PyFunctionObject *)
3596 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003597
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003598 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003599 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003600 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003601 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003602 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003603
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003604 if (oparg & 0x08) {
3605 assert(PyTuple_CheckExact(TOP()));
3606 func ->func_closure = POP();
3607 }
3608 if (oparg & 0x04) {
3609 assert(PyDict_CheckExact(TOP()));
3610 func->func_annotations = POP();
3611 }
3612 if (oparg & 0x02) {
3613 assert(PyDict_CheckExact(TOP()));
3614 func->func_kwdefaults = POP();
3615 }
3616 if (oparg & 0x01) {
3617 assert(PyTuple_CheckExact(TOP()));
3618 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003619 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003620
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003621 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003622 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003623 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003624
Benjamin Petersonddd19492018-09-16 22:38:02 -07003625 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003626 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003627 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003628 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003629 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003630 step = NULL;
3631 stop = POP();
3632 start = TOP();
3633 slice = PySlice_New(start, stop, step);
3634 Py_DECREF(start);
3635 Py_DECREF(stop);
3636 Py_XDECREF(step);
3637 SET_TOP(slice);
3638 if (slice == NULL)
3639 goto error;
3640 DISPATCH();
3641 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003642
Benjamin Petersonddd19492018-09-16 22:38:02 -07003643 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003644 /* Handles f-string value formatting. */
3645 PyObject *result;
3646 PyObject *fmt_spec;
3647 PyObject *value;
3648 PyObject *(*conv_fn)(PyObject *);
3649 int which_conversion = oparg & FVC_MASK;
3650 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3651
3652 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003653 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003654
3655 /* See if any conversion is specified. */
3656 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003657 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003658 case FVC_STR: conv_fn = PyObject_Str; break;
3659 case FVC_REPR: conv_fn = PyObject_Repr; break;
3660 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003661 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02003662 _PyErr_Format(tstate, PyExc_SystemError,
3663 "unexpected conversion flag %d",
3664 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003665 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003666 }
3667
3668 /* If there's a conversion function, call it and replace
3669 value with that result. Otherwise, just use value,
3670 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003671 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003672 result = conv_fn(value);
3673 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003674 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003675 Py_XDECREF(fmt_spec);
3676 goto error;
3677 }
3678 value = result;
3679 }
3680
3681 /* If value is a unicode object, and there's no fmt_spec,
3682 then we know the result of format(value) is value
3683 itself. In that case, skip calling format(). I plan to
3684 move this optimization in to PyObject_Format()
3685 itself. */
3686 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3687 /* Do nothing, just transfer ownership to result. */
3688 result = value;
3689 } else {
3690 /* Actually call format(). */
3691 result = PyObject_Format(value, fmt_spec);
3692 Py_DECREF(value);
3693 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003694 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003695 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003696 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003697 }
3698
Eric V. Smith135d5f42016-02-05 18:23:08 -05003699 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003700 DISPATCH();
3701 }
3702
Benjamin Petersonddd19492018-09-16 22:38:02 -07003703 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003704 int oldoparg = oparg;
3705 NEXTOPARG();
3706 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003707 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003708 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003709
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003710
Antoine Pitrou042b1282010-08-13 21:15:58 +00003711#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003712 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003713#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003714 default:
3715 fprintf(stderr,
3716 "XXX lineno: %d, opcode: %d\n",
3717 PyFrame_GetLineNumber(f),
3718 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02003719 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003720 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003721
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003722 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003723
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003724 /* This should never be reached. Every opcode should end with DISPATCH()
3725 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003726 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003727
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003728error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003729 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003730#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02003731 if (!_PyErr_Occurred(tstate)) {
3732 _PyErr_SetString(tstate, PyExc_SystemError,
3733 "error return without exception set");
3734 }
Victor Stinner365b6932013-07-12 00:11:58 +02003735#else
Victor Stinner438a12d2019-05-24 17:01:38 +02003736 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02003737#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003738
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003739 /* Log traceback info. */
3740 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003741
Benjamin Peterson51f46162013-01-23 08:38:47 -05003742 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003743 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3744 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003745
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003746exception_unwind:
3747 /* Unwind stacks if an exception occurred */
3748 while (f->f_iblock > 0) {
3749 /* Pop the current block. */
3750 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003752 if (b->b_type == EXCEPT_HANDLER) {
3753 UNWIND_EXCEPT_HANDLER(b);
3754 continue;
3755 }
3756 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003757 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003758 PyObject *exc, *val, *tb;
3759 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003760 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003761 /* Beware, this invalidates all b->b_* fields */
3762 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003763 PUSH(exc_info->exc_traceback);
3764 PUSH(exc_info->exc_value);
3765 if (exc_info->exc_type != NULL) {
3766 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003767 }
3768 else {
3769 Py_INCREF(Py_None);
3770 PUSH(Py_None);
3771 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003772 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003773 /* Make the raw exception data
3774 available to the handler,
3775 so a program can emulate the
3776 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02003777 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003778 if (tb != NULL)
3779 PyException_SetTraceback(val, tb);
3780 else
3781 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003782 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003783 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003784 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003785 exc_info->exc_value = val;
3786 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003787 if (tb == NULL)
3788 tb = Py_None;
3789 Py_INCREF(tb);
3790 PUSH(tb);
3791 PUSH(val);
3792 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003793 JUMPTO(handler);
Victor Stinnerdab84232020-03-17 18:56:44 +01003794 if (_Py_TracingPossible(ceval2)) {
Pablo Galindo4c53e632020-01-10 09:24:22 +00003795 int needs_new_execution_window = (f->f_lasti < instr_lb || f->f_lasti >= instr_ub);
3796 int needs_line_update = (f->f_lasti == instr_lb || f->f_lasti < instr_prev);
3797 /* Make sure that we trace line after exception if we are in a new execution
3798 * window or we don't need a line update and we are not in the first instruction
3799 * of the line. */
3800 if (needs_new_execution_window || (!needs_line_update && instr_lb > 0)) {
3801 instr_prev = INT_MAX;
3802 }
Mark Shannonfee55262019-11-21 09:11:43 +00003803 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003804 /* Resume normal execution */
3805 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003806 }
3807 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003808
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003809 /* End the loop as we still have an error */
3810 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003811 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003812
Pablo Galindof00828a2019-05-09 16:52:02 +01003813 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003814 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01003815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003816 /* Pop remaining stack entries. */
3817 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003818 PyObject *o = POP();
3819 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003820 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003821
Mark Shannone7c9f4a2020-01-13 12:51:26 +00003822exiting:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003823 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003824 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003825 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3826 tstate, f, PyTrace_RETURN, retval)) {
3827 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003828 }
3829 }
3830 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003831 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3832 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003833 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003834 }
3835 }
3836 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003838 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003839exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003840 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3841 dtrace_function_return(f);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01003842 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrou58720d62013-08-05 23:26:40 +02003843 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003844 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003845
Victor Stinner0b72b232020-03-12 23:18:39 +01003846 return _Py_CheckFunctionResult(tstate, NULL, retval, __func__);
Guido van Rossum374a9221991-04-04 10:40:29 +00003847}
3848
Benjamin Petersonb204a422011-06-05 22:04:07 -05003849static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003850format_missing(PyThreadState *tstate, const char *kind,
3851 PyCodeObject *co, PyObject *names)
Benjamin Petersone109c702011-06-24 09:37:26 -05003852{
3853 int err;
3854 Py_ssize_t len = PyList_GET_SIZE(names);
3855 PyObject *name_str, *comma, *tail, *tmp;
3856
3857 assert(PyList_CheckExact(names));
3858 assert(len >= 1);
3859 /* Deal with the joys of natural language. */
3860 switch (len) {
3861 case 1:
3862 name_str = PyList_GET_ITEM(names, 0);
3863 Py_INCREF(name_str);
3864 break;
3865 case 2:
3866 name_str = PyUnicode_FromFormat("%U and %U",
3867 PyList_GET_ITEM(names, len - 2),
3868 PyList_GET_ITEM(names, len - 1));
3869 break;
3870 default:
3871 tail = PyUnicode_FromFormat(", %U, and %U",
3872 PyList_GET_ITEM(names, len - 2),
3873 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003874 if (tail == NULL)
3875 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003876 /* Chop off the last two objects in the list. This shouldn't actually
3877 fail, but we can't be too careful. */
3878 err = PyList_SetSlice(names, len - 2, len, NULL);
3879 if (err == -1) {
3880 Py_DECREF(tail);
3881 return;
3882 }
3883 /* Stitch everything up into a nice comma-separated list. */
3884 comma = PyUnicode_FromString(", ");
3885 if (comma == NULL) {
3886 Py_DECREF(tail);
3887 return;
3888 }
3889 tmp = PyUnicode_Join(comma, names);
3890 Py_DECREF(comma);
3891 if (tmp == NULL) {
3892 Py_DECREF(tail);
3893 return;
3894 }
3895 name_str = PyUnicode_Concat(tmp, tail);
3896 Py_DECREF(tmp);
3897 Py_DECREF(tail);
3898 break;
3899 }
3900 if (name_str == NULL)
3901 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02003902 _PyErr_Format(tstate, PyExc_TypeError,
3903 "%U() missing %i required %s argument%s: %U",
3904 co->co_name,
3905 len,
3906 kind,
3907 len == 1 ? "" : "s",
3908 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05003909 Py_DECREF(name_str);
3910}
3911
3912static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003913missing_arguments(PyThreadState *tstate, PyCodeObject *co,
3914 Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003915 PyObject **fastlocals)
3916{
Victor Stinner74319ae2016-08-25 00:04:09 +02003917 Py_ssize_t i, j = 0;
3918 Py_ssize_t start, end;
3919 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003920 const char *kind = positional ? "positional" : "keyword-only";
3921 PyObject *missing_names;
3922
3923 /* Compute the names of the arguments that are missing. */
3924 missing_names = PyList_New(missing);
3925 if (missing_names == NULL)
3926 return;
3927 if (positional) {
3928 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01003929 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003930 }
3931 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003932 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003933 end = start + co->co_kwonlyargcount;
3934 }
3935 for (i = start; i < end; i++) {
3936 if (GETLOCAL(i) == NULL) {
3937 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3938 PyObject *name = PyObject_Repr(raw);
3939 if (name == NULL) {
3940 Py_DECREF(missing_names);
3941 return;
3942 }
3943 PyList_SET_ITEM(missing_names, j++, name);
3944 }
3945 }
3946 assert(j == missing);
Victor Stinner438a12d2019-05-24 17:01:38 +02003947 format_missing(tstate, kind, co, missing_names);
Benjamin Petersone109c702011-06-24 09:37:26 -05003948 Py_DECREF(missing_names);
3949}
3950
3951static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003952too_many_positional(PyThreadState *tstate, PyCodeObject *co,
3953 Py_ssize_t given, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003954 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003955{
3956 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003957 Py_ssize_t kwonly_given = 0;
3958 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003959 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003960 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003961
Benjamin Petersone109c702011-06-24 09:37:26 -05003962 assert((co->co_flags & CO_VARARGS) == 0);
3963 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01003964 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003965 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003966 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003967 }
3968 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003969 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01003970 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003971 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01003972 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003973 }
3974 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003975 plural = (co_argcount != 1);
3976 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003977 }
3978 if (sig == NULL)
3979 return;
3980 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003981 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3982 kwonly_sig = PyUnicode_FromFormat(format,
3983 given != 1 ? "s" : "",
3984 kwonly_given,
3985 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003986 if (kwonly_sig == NULL) {
3987 Py_DECREF(sig);
3988 return;
3989 }
3990 }
3991 else {
3992 /* This will not fail. */
3993 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003994 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003995 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003996 _PyErr_Format(tstate, PyExc_TypeError,
3997 "%U() takes %U positional argument%s but %zd%U %s given",
3998 co->co_name,
3999 sig,
4000 plural ? "s" : "",
4001 given,
4002 kwonly_sig,
4003 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004004 Py_DECREF(sig);
4005 Py_DECREF(kwonly_sig);
4006}
4007
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004008static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004009positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
4010 Py_ssize_t kwcount, PyObject* const* kwnames)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004011{
4012 int posonly_conflicts = 0;
4013 PyObject* posonly_names = PyList_New(0);
4014
4015 for(int k=0; k < co->co_posonlyargcount; k++){
4016 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
4017
4018 for (int k2=0; k2<kwcount; k2++){
4019 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
4020 PyObject* kwname = kwnames[k2];
4021 if (kwname == posonly_name){
4022 if(PyList_Append(posonly_names, kwname) != 0) {
4023 goto fail;
4024 }
4025 posonly_conflicts++;
4026 continue;
4027 }
4028
4029 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
4030
4031 if ( cmp > 0) {
4032 if(PyList_Append(posonly_names, kwname) != 0) {
4033 goto fail;
4034 }
4035 posonly_conflicts++;
4036 } else if (cmp < 0) {
4037 goto fail;
4038 }
4039
4040 }
4041 }
4042 if (posonly_conflicts) {
4043 PyObject* comma = PyUnicode_FromString(", ");
4044 if (comma == NULL) {
4045 goto fail;
4046 }
4047 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
4048 Py_DECREF(comma);
4049 if (error_names == NULL) {
4050 goto fail;
4051 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004052 _PyErr_Format(tstate, PyExc_TypeError,
4053 "%U() got some positional-only arguments passed"
4054 " as keyword arguments: '%U'",
4055 co->co_name, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004056 Py_DECREF(error_names);
4057 goto fail;
4058 }
4059
4060 Py_DECREF(posonly_names);
4061 return 0;
4062
4063fail:
4064 Py_XDECREF(posonly_names);
4065 return 1;
4066
4067}
4068
Guido van Rossumc2e20742006-02-27 22:32:47 +00004069/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02004070 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00004071 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00004072
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004073PyObject *
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004074_PyEval_EvalCode(PyThreadState *tstate,
4075 PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004076 PyObject *const *args, Py_ssize_t argcount,
4077 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03004078 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004079 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02004080 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004081 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00004082{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004083 assert(is_tstate_valid(tstate));
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004084
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00004085 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004086 PyFrameObject *f;
4087 PyObject *retval = NULL;
4088 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004089 PyObject *x, *u;
Pablo Galindocd74e662019-06-01 18:08:04 +01004090 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004091 Py_ssize_t i, j, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02004092 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00004093
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004094 if (globals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004095 _PyErr_SetString(tstate, PyExc_SystemError,
4096 "PyEval_EvalCodeEx: NULL globals");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004097 return NULL;
4098 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004099
Victor Stinnerc7020012016-08-16 23:40:29 +02004100 /* Create the frame */
INADA Naoki5a625d02016-12-24 20:19:08 +09004101 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02004102 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004103 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02004104 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004105 fastlocals = f->f_localsplus;
4106 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00004107
Victor Stinnerc7020012016-08-16 23:40:29 +02004108 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004109 if (co->co_flags & CO_VARKEYWORDS) {
4110 kwdict = PyDict_New();
4111 if (kwdict == NULL)
4112 goto fail;
4113 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02004114 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004115 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02004116 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004117 SETLOCAL(i, kwdict);
4118 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004119 else {
4120 kwdict = NULL;
4121 }
4122
Pablo Galindocd74e662019-06-01 18:08:04 +01004123 /* Copy all positional arguments into local variables */
4124 if (argcount > co->co_argcount) {
4125 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02004126 }
4127 else {
4128 n = argcount;
4129 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004130 for (j = 0; j < n; j++) {
4131 x = args[j];
4132 Py_INCREF(x);
4133 SETLOCAL(j, x);
4134 }
4135
Victor Stinnerc7020012016-08-16 23:40:29 +02004136 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004137 if (co->co_flags & CO_VARARGS) {
Sergey Fedoseev234531b2019-02-25 21:59:12 +05004138 u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02004139 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004140 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004141 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004142 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004143 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004144
Serhiy Storchakab7281052016-09-12 00:52:40 +03004145 /* Handle keyword arguments passed as two strided arrays */
4146 kwcount *= kwstep;
4147 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004148 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03004149 PyObject *keyword = kwnames[i];
4150 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02004151 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004152
Benjamin Petersonb204a422011-06-05 22:04:07 -05004153 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004154 _PyErr_Format(tstate, PyExc_TypeError,
4155 "%U() keywords must be strings",
4156 co->co_name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004157 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004158 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004159
Benjamin Petersonb204a422011-06-05 22:04:07 -05004160 /* Speed hack: do raw pointer compares. As names are
4161 normally interned this should almost always hit. */
4162 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004163 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004164 PyObject *name = co_varnames[j];
4165 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004166 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004167 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004168 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004169
Benjamin Petersonb204a422011-06-05 22:04:07 -05004170 /* Slow fallback, just in case */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004171 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004172 PyObject *name = co_varnames[j];
4173 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
4174 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004175 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004176 }
4177 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004178 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004179 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004180 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004181
Victor Stinner231d1f32017-01-11 02:12:06 +01004182 assert(j >= total_args);
4183 if (kwdict == NULL) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004184
Victor Stinner438a12d2019-05-24 17:01:38 +02004185 if (co->co_posonlyargcount
4186 && positional_only_passed_as_keyword(tstate, co,
4187 kwcount, kwnames))
4188 {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004189 goto fail;
4190 }
4191
Victor Stinner438a12d2019-05-24 17:01:38 +02004192 _PyErr_Format(tstate, PyExc_TypeError,
4193 "%U() got an unexpected keyword argument '%S'",
4194 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004195 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004196 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004197
Christian Heimes0bd447f2013-07-20 14:48:10 +02004198 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4199 goto fail;
4200 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004201 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004202
Benjamin Petersonb204a422011-06-05 22:04:07 -05004203 kw_found:
4204 if (GETLOCAL(j) != NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004205 _PyErr_Format(tstate, PyExc_TypeError,
4206 "%U() got multiple values for argument '%S'",
4207 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004208 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004209 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004210 Py_INCREF(value);
4211 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004212 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004213
4214 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004215 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004216 too_many_positional(tstate, co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004217 goto fail;
4218 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004219
4220 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004221 if (argcount < co->co_argcount) {
4222 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004223 Py_ssize_t missing = 0;
4224 for (i = argcount; i < m; i++) {
4225 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004226 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004227 }
4228 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004229 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004230 missing_arguments(tstate, co, missing, defcount, fastlocals);
Benjamin Petersone109c702011-06-24 09:37:26 -05004231 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004232 }
4233 if (n > m)
4234 i = n - m;
4235 else
4236 i = 0;
4237 for (; i < defcount; i++) {
4238 if (GETLOCAL(m+i) == NULL) {
4239 PyObject *def = defs[i];
4240 Py_INCREF(def);
4241 SETLOCAL(m+i, def);
4242 }
4243 }
4244 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004245
4246 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004247 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004248 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004249 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004250 PyObject *name;
4251 if (GETLOCAL(i) != NULL)
4252 continue;
4253 name = PyTuple_GET_ITEM(co->co_varnames, i);
4254 if (kwdefs != NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004255 PyObject *def = PyDict_GetItemWithError(kwdefs, name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004256 if (def) {
4257 Py_INCREF(def);
4258 SETLOCAL(i, def);
4259 continue;
4260 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004261 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004262 goto fail;
4263 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004264 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004265 missing++;
4266 }
4267 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004268 missing_arguments(tstate, co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004269 goto fail;
4270 }
4271 }
4272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004273 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004274 vars into frame. */
4275 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004276 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004277 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004278 /* Possibly account for the cell variable being an argument. */
4279 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004280 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004281 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004282 /* Clear the local copy. */
4283 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004284 }
4285 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004286 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004287 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004288 if (c == NULL)
4289 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004290 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004291 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004292
4293 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004294 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4295 PyObject *o = PyTuple_GET_ITEM(closure, i);
4296 Py_INCREF(o);
4297 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004298 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004299
Yury Selivanoveb636452016-09-08 22:01:51 -07004300 /* Handle generator/coroutine/asynchronous generator */
4301 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004302 PyObject *gen;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004303 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004305 /* Don't need to keep the reference to f_back, it will be set
4306 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004307 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004309 /* Create a new generator that owns the ready to run frame
4310 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004311 if (is_coro) {
4312 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004313 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4314 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004315 } else {
4316 gen = PyGen_NewWithQualName(f, name, qualname);
4317 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004318 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004319 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004320 }
INADA Naoki9c157762016-12-26 18:52:46 +09004321
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004322 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004323
Yury Selivanov75445082015-05-11 22:57:16 -04004324 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004325 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004326
Victor Stinnerb9e68122019-11-14 12:20:46 +01004327 retval = _PyEval_EvalFrame(tstate, f, 0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004328
Thomas Woutersce272b62007-09-19 21:19:28 +00004329fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004331 /* decref'ing the frame can cause __del__ methods to get invoked,
4332 which can call back into Python. While we're done with the
4333 current Python frame (f), the associated C stack is still in use,
4334 so recursion_depth must be boosted for the duration.
4335 */
INADA Naoki5a625d02016-12-24 20:19:08 +09004336 if (Py_REFCNT(f) > 1) {
4337 Py_DECREF(f);
4338 _PyObject_GC_TRACK(f);
4339 }
4340 else {
4341 ++tstate->recursion_depth;
4342 Py_DECREF(f);
4343 --tstate->recursion_depth;
4344 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004345 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004346}
4347
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004348
4349PyObject *
4350_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
4351 PyObject *const *args, Py_ssize_t argcount,
4352 PyObject *const *kwnames, PyObject *const *kwargs,
4353 Py_ssize_t kwcount, int kwstep,
4354 PyObject *const *defs, Py_ssize_t defcount,
4355 PyObject *kwdefs, PyObject *closure,
4356 PyObject *name, PyObject *qualname)
4357{
4358 PyThreadState *tstate = _PyThreadState_GET();
4359 return _PyEval_EvalCode(tstate, _co, globals, locals,
4360 args, argcount,
4361 kwnames, kwargs,
4362 kwcount, kwstep,
4363 defs, defcount,
4364 kwdefs, closure,
4365 name, qualname);
4366}
4367
Victor Stinner40ee3012014-06-16 15:59:28 +02004368PyObject *
4369PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004370 PyObject *const *args, int argcount,
4371 PyObject *const *kws, int kwcount,
4372 PyObject *const *defs, int defcount,
4373 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004374{
4375 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004376 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004377 kws, kws != NULL ? kws + 1 : NULL,
4378 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004379 defs, defcount,
4380 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004381 NULL, NULL);
4382}
Tim Peters5ca576e2001-06-18 22:08:13 +00004383
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004384static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004385special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004386{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004387 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004388 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02004389 if (res == NULL && !_PyErr_Occurred(tstate)) {
Victor Stinner4804b5b2020-05-12 01:43:38 +02004390 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(id));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004391 return NULL;
4392 }
4393 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004394}
4395
4396
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004397/* Logic for the raise statement (too complicated for inlining).
4398 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004399static int
Victor Stinner09532fe2019-05-10 23:39:09 +02004400do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004401{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004402 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004404 if (exc == NULL) {
4405 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01004406 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004407 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004408 type = exc_info->exc_type;
4409 value = exc_info->exc_value;
4410 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004411 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004412 _PyErr_SetString(tstate, PyExc_RuntimeError,
4413 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004414 return 0;
4415 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004416 Py_XINCREF(type);
4417 Py_XINCREF(value);
4418 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02004419 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004420 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004421 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004423 /* We support the following forms of raise:
4424 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004425 raise <instance>
4426 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004428 if (PyExceptionClass_Check(exc)) {
4429 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004430 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004431 if (value == NULL)
4432 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004433 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004434 _PyErr_Format(tstate, PyExc_TypeError,
4435 "calling %R should have returned an instance of "
4436 "BaseException, not %R",
4437 type, Py_TYPE(value));
4438 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004439 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004440 }
4441 else if (PyExceptionInstance_Check(exc)) {
4442 value = exc;
4443 type = PyExceptionInstance_Class(exc);
4444 Py_INCREF(type);
4445 }
4446 else {
4447 /* Not something you can raise. You get an exception
4448 anyway, just not what you specified :-) */
4449 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02004450 _PyErr_SetString(tstate, PyExc_TypeError,
4451 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004452 goto raise_error;
4453 }
Collin Winter828f04a2007-08-31 00:04:24 +00004454
Serhiy Storchakac0191582016-09-27 11:37:10 +03004455 assert(type != NULL);
4456 assert(value != NULL);
4457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004458 if (cause) {
4459 PyObject *fixed_cause;
4460 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004461 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004462 if (fixed_cause == NULL)
4463 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004464 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004465 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004466 else if (PyExceptionInstance_Check(cause)) {
4467 fixed_cause = cause;
4468 }
4469 else if (cause == Py_None) {
4470 Py_DECREF(cause);
4471 fixed_cause = NULL;
4472 }
4473 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004474 _PyErr_SetString(tstate, PyExc_TypeError,
4475 "exception causes must derive from "
4476 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004477 goto raise_error;
4478 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004479 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004480 }
Collin Winter828f04a2007-08-31 00:04:24 +00004481
Victor Stinner438a12d2019-05-24 17:01:38 +02004482 _PyErr_SetObject(tstate, type, value);
Victor Stinner61f4db82020-01-28 03:37:45 +01004483 /* _PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004484 Py_DECREF(value);
4485 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004486 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004487
4488raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004489 Py_XDECREF(value);
4490 Py_XDECREF(type);
4491 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004492 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004493}
4494
Tim Petersd6d010b2001-06-21 02:49:55 +00004495/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004496 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004497
Guido van Rossum0368b722007-05-11 16:50:42 +00004498 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4499 with a variable target.
4500*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004501
Barry Warsawe42b18f1997-08-25 22:13:04 +00004502static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004503unpack_iterable(PyThreadState *tstate, PyObject *v,
4504 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004505{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004506 int i = 0, j = 0;
4507 Py_ssize_t ll = 0;
4508 PyObject *it; /* iter(v) */
4509 PyObject *w;
4510 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004512 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004514 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004515 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004516 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01004517 Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v))
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004518 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004519 _PyErr_Format(tstate, PyExc_TypeError,
4520 "cannot unpack non-iterable %.200s object",
Victor Stinnera102ed72020-02-07 02:24:48 +01004521 Py_TYPE(v)->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004522 }
4523 return 0;
4524 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004526 for (; i < argcnt; i++) {
4527 w = PyIter_Next(it);
4528 if (w == NULL) {
4529 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004530 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04004531 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004532 _PyErr_Format(tstate, PyExc_ValueError,
4533 "not enough values to unpack "
4534 "(expected %d, got %d)",
4535 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004536 }
4537 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004538 _PyErr_Format(tstate, PyExc_ValueError,
4539 "not enough values to unpack "
4540 "(expected at least %d, got %d)",
4541 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004542 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004543 }
4544 goto Error;
4545 }
4546 *--sp = w;
4547 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004549 if (argcntafter == -1) {
4550 /* We better have exhausted the iterator now. */
4551 w = PyIter_Next(it);
4552 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004553 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004554 goto Error;
4555 Py_DECREF(it);
4556 return 1;
4557 }
4558 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02004559 _PyErr_Format(tstate, PyExc_ValueError,
4560 "too many values to unpack (expected %d)",
4561 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004562 goto Error;
4563 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004565 l = PySequence_List(it);
4566 if (l == NULL)
4567 goto Error;
4568 *--sp = l;
4569 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004571 ll = PyList_GET_SIZE(l);
4572 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004573 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04004574 "not enough values to unpack (expected at least %d, got %zd)",
4575 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004576 goto Error;
4577 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004579 /* Pop the "after-variable" args off the list. */
4580 for (j = argcntafter; j > 0; j--, i++) {
4581 *--sp = PyList_GET_ITEM(l, ll - j);
4582 }
4583 /* Resize the list. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +01004584 Py_SET_SIZE(l, ll - argcntafter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004585 Py_DECREF(it);
4586 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004587
Tim Petersd6d010b2001-06-21 02:49:55 +00004588Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004589 for (; i > 0; i--, sp++)
4590 Py_DECREF(*sp);
4591 Py_XDECREF(it);
4592 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004593}
4594
4595
Guido van Rossum96a42c81992-01-12 02:29:51 +00004596#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004597static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004598prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004599{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004600 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02004601 if (PyObject_Print(v, stdout, 0) != 0) {
4602 /* Don't know what else to do */
4603 _PyErr_Clear(tstate);
4604 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004605 printf("\n");
4606 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004607}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004608#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004609
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004610static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004611call_exc_trace(Py_tracefunc func, PyObject *self,
4612 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004613{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004614 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004615 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004616 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004617 if (value == NULL) {
4618 value = Py_None;
4619 Py_INCREF(value);
4620 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004621 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01004622 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004623 arg = PyTuple_Pack(3, type, value, traceback);
4624 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004625 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004626 return;
4627 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004628 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004629 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02004630 if (err == 0) {
4631 _PyErr_Restore(tstate, type, value, orig_traceback);
4632 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004633 else {
4634 Py_XDECREF(type);
4635 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004636 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004637 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004638}
4639
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004640static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004641call_trace_protected(Py_tracefunc func, PyObject *obj,
4642 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004643 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004644{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004645 PyObject *type, *value, *traceback;
4646 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004647 _PyErr_Fetch(tstate, &type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004648 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004649 if (err == 0)
4650 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004651 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004652 return 0;
4653 }
4654 else {
4655 Py_XDECREF(type);
4656 Py_XDECREF(value);
4657 Py_XDECREF(traceback);
4658 return -1;
4659 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004660}
4661
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004662static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004663call_trace(Py_tracefunc func, PyObject *obj,
4664 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004665 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004666{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004667 int result;
4668 if (tstate->tracing)
4669 return 0;
4670 tstate->tracing++;
4671 tstate->use_tracing = 0;
4672 result = func(obj, frame, what, arg);
4673 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4674 || (tstate->c_profilefunc != NULL));
4675 tstate->tracing--;
4676 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004677}
4678
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004679PyObject *
4680_PyEval_CallTracing(PyObject *func, PyObject *args)
4681{
Victor Stinner50b48572018-11-01 01:51:40 +01004682 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004683 int save_tracing = tstate->tracing;
4684 int save_use_tracing = tstate->use_tracing;
4685 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004686
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004687 tstate->tracing = 0;
4688 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4689 || (tstate->c_profilefunc != NULL));
4690 result = PyObject_Call(func, args, NULL);
4691 tstate->tracing = save_tracing;
4692 tstate->use_tracing = save_use_tracing;
4693 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004694}
4695
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004696/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004697static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004698maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004699 PyThreadState *tstate, PyFrameObject *frame,
4700 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004701{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004702 int result = 0;
4703 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004704
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004705 /* If the last instruction executed isn't in the current
4706 instruction window, reset the window.
4707 */
4708 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4709 PyAddrPair bounds;
4710 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4711 &bounds);
4712 *instr_lb = bounds.ap_lower;
4713 *instr_ub = bounds.ap_upper;
4714 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004715 /* If the last instruction falls at the start of a line or if it
4716 represents a jump backwards, update the frame's line number and
4717 then call the trace function if we're tracing source lines.
4718 */
4719 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004720 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004721 if (frame->f_trace_lines) {
4722 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4723 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004724 }
George King20faa682017-10-18 17:44:22 -07004725 /* Always emit an opcode event if we're tracing all opcodes. */
4726 if (frame->f_trace_opcodes) {
4727 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4728 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004729 *instr_prev = frame->f_lasti;
4730 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004731}
4732
Victor Stinner309d7cc2020-03-13 16:39:12 +01004733int
4734_PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
4735{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004736 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01004737 /* The caller must hold the GIL */
4738 assert(PyGILState_Check());
4739
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004740 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01004741 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004742 PyThreadState *current_tstate = _PyThreadState_GET();
4743 if (_PySys_Audit(current_tstate, "sys.setprofile", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01004744 return -1;
4745 }
4746
4747 PyObject *profileobj = tstate->c_profileobj;
4748
4749 tstate->c_profilefunc = NULL;
4750 tstate->c_profileobj = NULL;
4751 /* Must make sure that tracing is not ignored if 'profileobj' is freed */
4752 tstate->use_tracing = tstate->c_tracefunc != NULL;
4753 Py_XDECREF(profileobj);
4754
4755 Py_XINCREF(arg);
4756 tstate->c_profileobj = arg;
4757 tstate->c_profilefunc = func;
4758
4759 /* Flag that tracing or profiling is turned on */
4760 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
4761 return 0;
4762}
4763
Fred Drake5755ce62001-06-27 19:19:46 +00004764void
4765PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004766{
Victor Stinner309d7cc2020-03-13 16:39:12 +01004767 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01004768 if (_PyEval_SetProfile(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004769 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01004770 _PyErr_WriteUnraisableMsg("in PyEval_SetProfile", NULL);
4771 }
Victor Stinner309d7cc2020-03-13 16:39:12 +01004772}
4773
4774int
4775_PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
4776{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004777 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01004778 /* The caller must hold the GIL */
4779 assert(PyGILState_Check());
4780
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004781 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01004782 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004783 PyThreadState *current_tstate = _PyThreadState_GET();
4784 if (_PySys_Audit(current_tstate, "sys.settrace", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01004785 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004786 }
4787
Victor Stinnerda2914d2020-03-20 09:29:08 +01004788 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinner309d7cc2020-03-13 16:39:12 +01004789 PyObject *traceobj = tstate->c_traceobj;
Victor Stinnerda2914d2020-03-20 09:29:08 +01004790 ceval2->tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
Victor Stinner309d7cc2020-03-13 16:39:12 +01004791
4792 tstate->c_tracefunc = NULL;
4793 tstate->c_traceobj = NULL;
4794 /* Must make sure that profiling is not ignored if 'traceobj' is freed */
4795 tstate->use_tracing = (tstate->c_profilefunc != NULL);
4796 Py_XDECREF(traceobj);
4797
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004798 Py_XINCREF(arg);
Victor Stinner309d7cc2020-03-13 16:39:12 +01004799 tstate->c_traceobj = arg;
4800 tstate->c_tracefunc = func;
4801
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004802 /* Flag that tracing or profiling is turned on */
Victor Stinner309d7cc2020-03-13 16:39:12 +01004803 tstate->use_tracing = ((func != NULL)
4804 || (tstate->c_profilefunc != NULL));
4805
4806 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +00004807}
4808
4809void
4810PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4811{
Victor Stinner309d7cc2020-03-13 16:39:12 +01004812 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01004813 if (_PyEval_SetTrace(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004814 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01004815 _PyErr_WriteUnraisableMsg("in PyEval_SetTrace", NULL);
4816 }
Fred Draked0838392001-06-16 21:02:31 +00004817}
4818
Victor Stinner309d7cc2020-03-13 16:39:12 +01004819
Yury Selivanov75445082015-05-11 22:57:16 -04004820void
Victor Stinner838f2642019-06-13 22:41:23 +02004821_PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004822{
4823 assert(new_depth >= 0);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004824 tstate->coroutine_origin_tracking_depth = new_depth;
4825}
4826
4827int
4828_PyEval_GetCoroutineOriginTrackingDepth(void)
4829{
Victor Stinner50b48572018-11-01 01:51:40 +01004830 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004831 return tstate->coroutine_origin_tracking_depth;
4832}
4833
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004834int
Yury Selivanoveb636452016-09-08 22:01:51 -07004835_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4836{
Victor Stinner50b48572018-11-01 01:51:40 +01004837 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004838
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004839 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_firstiter", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004840 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004841 }
4842
Yury Selivanoveb636452016-09-08 22:01:51 -07004843 Py_XINCREF(firstiter);
4844 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004845 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07004846}
4847
4848PyObject *
4849_PyEval_GetAsyncGenFirstiter(void)
4850{
Victor Stinner50b48572018-11-01 01:51:40 +01004851 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004852 return tstate->async_gen_firstiter;
4853}
4854
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004855int
Yury Selivanoveb636452016-09-08 22:01:51 -07004856_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4857{
Victor Stinner50b48572018-11-01 01:51:40 +01004858 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004859
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004860 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_finalizer", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004861 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004862 }
4863
Yury Selivanoveb636452016-09-08 22:01:51 -07004864 Py_XINCREF(finalizer);
4865 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004866 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07004867}
4868
4869PyObject *
4870_PyEval_GetAsyncGenFinalizer(void)
4871{
Victor Stinner50b48572018-11-01 01:51:40 +01004872 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004873 return tstate->async_gen_finalizer;
4874}
4875
Victor Stinner438a12d2019-05-24 17:01:38 +02004876PyFrameObject *
4877PyEval_GetFrame(void)
4878{
4879 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004880 return tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02004881}
4882
Guido van Rossumb209a111997-04-29 18:18:01 +00004883PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004884PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004885{
Victor Stinner438a12d2019-05-24 17:01:38 +02004886 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004887 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004888 if (current_frame == NULL)
Victor Stinner438a12d2019-05-24 17:01:38 +02004889 return tstate->interp->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004890 else
4891 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004892}
4893
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004894/* Convenience function to get a builtin from its name */
4895PyObject *
4896_PyEval_GetBuiltinId(_Py_Identifier *name)
4897{
Victor Stinner438a12d2019-05-24 17:01:38 +02004898 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004899 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4900 if (attr) {
4901 Py_INCREF(attr);
4902 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004903 else if (!_PyErr_Occurred(tstate)) {
4904 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004905 }
4906 return attr;
4907}
4908
Guido van Rossumb209a111997-04-29 18:18:01 +00004909PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004910PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004911{
Victor Stinner438a12d2019-05-24 17:01:38 +02004912 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004913 PyFrameObject *current_frame = tstate->frame;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004914 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004915 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004916 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004917 }
4918
Victor Stinner438a12d2019-05-24 17:01:38 +02004919 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01004920 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004921 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004922
4923 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004924 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004925}
4926
Guido van Rossumb209a111997-04-29 18:18:01 +00004927PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004928PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004929{
Victor Stinner438a12d2019-05-24 17:01:38 +02004930 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004931 PyFrameObject *current_frame = tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02004932 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004933 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004934 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004935
4936 assert(current_frame->f_globals != NULL);
4937 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004938}
4939
Guido van Rossum6135a871995-01-09 17:53:26 +00004940int
Tim Peters5ba58662001-07-16 02:29:45 +00004941PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004942{
Victor Stinner438a12d2019-05-24 17:01:38 +02004943 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004944 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004945 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004946
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004947 if (current_frame != NULL) {
4948 const int codeflags = current_frame->f_code->co_flags;
4949 const int compilerflags = codeflags & PyCF_MASK;
4950 if (compilerflags) {
4951 result = 1;
4952 cf->cf_flags |= compilerflags;
4953 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004954#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004955 if (codeflags & CO_GENERATOR_ALLOWED) {
4956 result = 1;
4957 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4958 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004959#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004960 }
4961 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004962}
4963
Guido van Rossum3f5da241990-12-20 15:06:42 +00004964
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004965const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004966PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004967{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004968 if (PyMethod_Check(func))
4969 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4970 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004971 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004972 else if (PyCFunction_Check(func))
4973 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4974 else
Victor Stinnera102ed72020-02-07 02:24:48 +01004975 return Py_TYPE(func)->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004976}
4977
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004978const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004979PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004980{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004981 if (PyMethod_Check(func))
4982 return "()";
4983 else if (PyFunction_Check(func))
4984 return "()";
4985 else if (PyCFunction_Check(func))
4986 return "()";
4987 else
4988 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004989}
4990
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004991#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004992if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004993 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4994 tstate, tstate->frame, \
4995 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004996 x = NULL; \
4997 } \
4998 else { \
4999 x = call; \
5000 if (tstate->c_profilefunc != NULL) { \
5001 if (x == NULL) { \
5002 call_trace_protected(tstate->c_profilefunc, \
5003 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005004 tstate, tstate->frame, \
5005 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005006 /* XXX should pass (type, value, tb) */ \
5007 } else { \
5008 if (call_trace(tstate->c_profilefunc, \
5009 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005010 tstate, tstate->frame, \
5011 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005012 Py_DECREF(x); \
5013 x = NULL; \
5014 } \
5015 } \
5016 } \
5017 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00005018} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005019 x = call; \
5020 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00005021
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005022
5023static PyObject *
5024trace_call_function(PyThreadState *tstate,
5025 PyObject *func,
5026 PyObject **args, Py_ssize_t nargs,
5027 PyObject *kwnames)
5028{
5029 PyObject *x;
scoder4c9ea092020-05-12 16:12:41 +02005030 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Petr Viktorinffd97532020-02-11 17:46:57 +01005031 C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005032 return x;
5033 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005034 else if (Py_IS_TYPE(func, &PyMethodDescr_Type) && nargs > 0) {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005035 /* We need to create a temporary bound method as argument
5036 for profiling.
5037
5038 If nargs == 0, then this cannot work because we have no
5039 "self". In any case, the call itself would raise
5040 TypeError (foo needs an argument), so we just skip
5041 profiling. */
5042 PyObject *self = args[0];
5043 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5044 if (func == NULL) {
5045 return NULL;
5046 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005047 C_TRACE(x, PyObject_Vectorcall(func,
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02005048 args+1, nargs-1,
5049 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005050 Py_DECREF(func);
5051 return x;
5052 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005053 return PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005054}
5055
Victor Stinner415c5102017-01-11 00:54:57 +01005056/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
5057 to reduce the stack consumption. */
5058Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Victor Stinner09532fe2019-05-10 23:39:09 +02005059call_function(PyThreadState *tstate, PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005060{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005061 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005062 PyObject *func = *pfunc;
5063 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07005064 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
5065 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09005066 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005067
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005068 if (tstate->use_tracing) {
5069 x = trace_call_function(tstate, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09005070 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01005071 else {
Petr Viktorinffd97532020-02-11 17:46:57 +01005072 x = PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005073 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00005074
Victor Stinner438a12d2019-05-24 17:01:38 +02005075 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005076
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01005077 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005078 while ((*pp_stack) > pfunc) {
5079 w = EXT_POP(*pp_stack);
5080 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005081 }
Victor Stinnerace47d72013-07-18 01:41:08 +02005082
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005083 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005084}
5085
Jeremy Hylton52820442001-01-03 23:52:36 +00005086static PyObject *
Victor Stinner09532fe2019-05-10 23:39:09 +02005087do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005088{
jdemeyere89de732018-09-19 12:06:20 +02005089 PyObject *result;
5090
scoder4c9ea092020-05-12 16:12:41 +02005091 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +02005092 C_TRACE(result, PyObject_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005093 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005094 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005095 else if (Py_IS_TYPE(func, &PyMethodDescr_Type)) {
jdemeyere89de732018-09-19 12:06:20 +02005096 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
5097 if (nargs > 0 && tstate->use_tracing) {
5098 /* We need to create a temporary bound method as argument
5099 for profiling.
5100
5101 If nargs == 0, then this cannot work because we have no
5102 "self". In any case, the call itself would raise
5103 TypeError (foo needs an argument), so we just skip
5104 profiling. */
5105 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
5106 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5107 if (func == NULL) {
5108 return NULL;
5109 }
5110
Victor Stinner4d231bc2019-11-14 13:36:21 +01005111 C_TRACE(result, _PyObject_FastCallDictTstate(
5112 tstate, func,
5113 &_PyTuple_ITEMS(callargs)[1],
5114 nargs - 1,
5115 kwdict));
jdemeyere89de732018-09-19 12:06:20 +02005116 Py_DECREF(func);
5117 return result;
5118 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005119 }
jdemeyere89de732018-09-19 12:06:20 +02005120 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00005121}
5122
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005123/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005124 nb_index slot defined, and store in *pi.
5125 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08005126 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00005127 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005128*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005129int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005130_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005131{
Victor Stinner438a12d2019-05-24 17:01:38 +02005132 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005133 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005134 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005135 if (_PyIndex_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005136 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005137 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005138 return 0;
5139 }
5140 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005141 _PyErr_SetString(tstate, PyExc_TypeError,
5142 "slice indices must be integers or "
5143 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005144 return 0;
5145 }
5146 *pi = x;
5147 }
5148 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005149}
5150
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005151int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005152_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005153{
Victor Stinner438a12d2019-05-24 17:01:38 +02005154 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005155 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005156 if (_PyIndex_Check(v)) {
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005157 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005158 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005159 return 0;
5160 }
5161 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005162 _PyErr_SetString(tstate, PyExc_TypeError,
5163 "slice indices must be integers or "
5164 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005165 return 0;
5166 }
5167 *pi = x;
5168 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005169}
5170
Thomas Wouters52152252000-08-17 22:55:00 +00005171static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005172import_name(PyThreadState *tstate, PyFrameObject *f,
5173 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005174{
5175 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005176 PyObject *import_func, *res;
5177 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005178
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005179 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005180 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005181 if (!_PyErr_Occurred(tstate)) {
5182 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005183 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005184 return NULL;
5185 }
5186
5187 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005188 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005189 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02005190 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005191 return NULL;
5192 }
5193 res = PyImport_ImportModuleLevelObject(
5194 name,
5195 f->f_globals,
5196 f->f_locals == NULL ? Py_None : f->f_locals,
5197 fromlist,
5198 ilevel);
5199 return res;
5200 }
5201
5202 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005203
5204 stack[0] = name;
5205 stack[1] = f->f_globals;
5206 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5207 stack[3] = fromlist;
5208 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005209 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005210 Py_DECREF(import_func);
5211 return res;
5212}
5213
5214static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005215import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005216{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005217 PyObject *x;
Xiang Zhang4830f582017-03-21 11:13:42 +08005218 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005219
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005220 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005221 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005222 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005223 /* Issue #17636: in case this failed because of a circular relative
5224 import, try to fallback on reading the module directly from
5225 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02005226 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005227 if (pkgname == NULL) {
5228 goto error;
5229 }
Oren Milman6db70332017-09-19 14:23:01 +03005230 if (!PyUnicode_Check(pkgname)) {
5231 Py_CLEAR(pkgname);
5232 goto error;
5233 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005234 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005235 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005236 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005237 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005238 }
Eric Snow3f9eee62017-09-15 16:35:20 -06005239 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005240 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02005241 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07005242 goto error;
5243 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005244 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005245 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005246 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005247 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005248 if (pkgname == NULL) {
5249 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5250 if (pkgname_or_unknown == NULL) {
5251 Py_XDECREF(pkgpath);
5252 return NULL;
5253 }
5254 } else {
5255 pkgname_or_unknown = pkgname;
5256 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005257
5258 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005259 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08005260 errmsg = PyUnicode_FromFormat(
5261 "cannot import name %R from %R (unknown location)",
5262 name, pkgname_or_unknown
5263 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005264 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005265 PyErr_SetImportError(errmsg, pkgname, NULL);
5266 }
5267 else {
Anthony Sottile65366bc2019-09-09 08:17:50 -07005268 _Py_IDENTIFIER(__spec__);
5269 PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
Anthony Sottile65366bc2019-09-09 08:17:50 -07005270 const char *fmt =
5271 _PyModuleSpec_IsInitializing(spec) ?
5272 "cannot import name %R from partially initialized module %R "
5273 "(most likely due to a circular import) (%S)" :
5274 "cannot import name %R from %R (%S)";
5275 Py_XDECREF(spec);
5276
5277 errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
Stefan Krah027b09c2019-03-25 21:50:58 +01005278 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005279 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005280 }
5281
Xiang Zhang4830f582017-03-21 11:13:42 +08005282 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005283 Py_XDECREF(pkgname_or_unknown);
5284 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005285 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005286}
Guido van Rossumac7be682001-01-17 15:42:30 +00005287
Thomas Wouters52152252000-08-17 22:55:00 +00005288static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005289import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00005290{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005291 _Py_IDENTIFIER(__all__);
5292 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005293 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005294 int skip_leading_underscores = 0;
5295 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005296
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005297 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5298 return -1; /* Unexpected error */
5299 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005300 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005301 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5302 return -1;
5303 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005304 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005305 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005306 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005307 return -1;
5308 }
5309 all = PyMapping_Keys(dict);
5310 Py_DECREF(dict);
5311 if (all == NULL)
5312 return -1;
5313 skip_leading_underscores = 1;
5314 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005316 for (pos = 0, err = 0; ; pos++) {
5317 name = PySequence_GetItem(all, pos);
5318 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005319 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005320 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02005321 }
5322 else {
5323 _PyErr_Clear(tstate);
5324 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005325 break;
5326 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005327 if (!PyUnicode_Check(name)) {
5328 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5329 if (modname == NULL) {
5330 Py_DECREF(name);
5331 err = -1;
5332 break;
5333 }
5334 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005335 _PyErr_Format(tstate, PyExc_TypeError,
5336 "module __name__ must be a string, not %.100s",
5337 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005338 }
5339 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005340 _PyErr_Format(tstate, PyExc_TypeError,
5341 "%s in %U.%s must be str, not %.100s",
5342 skip_leading_underscores ? "Key" : "Item",
5343 modname,
5344 skip_leading_underscores ? "__dict__" : "__all__",
5345 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005346 }
5347 Py_DECREF(modname);
5348 Py_DECREF(name);
5349 err = -1;
5350 break;
5351 }
5352 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005353 if (PyUnicode_READY(name) == -1) {
5354 Py_DECREF(name);
5355 err = -1;
5356 break;
5357 }
5358 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5359 Py_DECREF(name);
5360 continue;
5361 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005362 }
5363 value = PyObject_GetAttr(v, name);
5364 if (value == NULL)
5365 err = -1;
5366 else if (PyDict_CheckExact(locals))
5367 err = PyDict_SetItem(locals, name, value);
5368 else
5369 err = PyObject_SetItem(locals, name, value);
5370 Py_DECREF(name);
5371 Py_XDECREF(value);
5372 if (err != 0)
5373 break;
5374 }
5375 Py_DECREF(all);
5376 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005377}
5378
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005379static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005380check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005381{
Victor Stinnera102ed72020-02-07 02:24:48 +01005382 if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005383 /* check_args_iterable() may be called with a live exception:
5384 * clear it to prevent calling _PyObject_FunctionStr() with an
5385 * exception set. */
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 _PyErr_Format(tstate, PyExc_TypeError,
5390 "%U argument after * must be an iterable, not %.200s",
5391 funcstr, Py_TYPE(args)->tp_name);
5392 Py_DECREF(funcstr);
5393 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005394 return -1;
5395 }
5396 return 0;
5397}
5398
5399static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005400format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005401{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005402 /* _PyDict_MergeEx raises attribute
5403 * error (percolated from an attempt
5404 * to get 'keys' attribute) instead of
5405 * a type error if its second argument
5406 * is not a mapping.
5407 */
Victor Stinner438a12d2019-05-24 17:01:38 +02005408 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005409 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005410 PyObject *funcstr = _PyObject_FunctionStr(func);
5411 if (funcstr != NULL) {
5412 _PyErr_Format(
5413 tstate, PyExc_TypeError,
5414 "%U argument after ** must be a mapping, not %.200s",
5415 funcstr, Py_TYPE(kwargs)->tp_name);
5416 Py_DECREF(funcstr);
5417 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005418 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005419 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005420 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02005421 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005422 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005423 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005424 PyObject *funcstr = _PyObject_FunctionStr(func);
5425 if (funcstr != NULL) {
5426 PyObject *key = PyTuple_GET_ITEM(val, 0);
5427 _PyErr_Format(
5428 tstate, PyExc_TypeError,
5429 "%U got multiple values for keyword argument '%S'",
5430 funcstr, key);
5431 Py_DECREF(funcstr);
5432 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005433 Py_XDECREF(exc);
5434 Py_XDECREF(val);
5435 Py_XDECREF(tb);
5436 }
5437 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005438 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005439 }
5440 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005441}
5442
Guido van Rossumac7be682001-01-17 15:42:30 +00005443static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005444format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
5445 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005446{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005447 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005449 if (!obj)
5450 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005451
Serhiy Storchaka06515832016-11-20 09:13:07 +02005452 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005453 if (!obj_str)
5454 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005455
Victor Stinner438a12d2019-05-24 17:01:38 +02005456 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005457}
Guido van Rossum950361c1997-01-24 13:49:28 +00005458
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005459static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005460format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005461{
5462 PyObject *name;
5463 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02005464 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005465 return;
5466 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5467 name = PyTuple_GET_ITEM(co->co_cellvars,
5468 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005469 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005470 PyExc_UnboundLocalError,
5471 UNBOUNDLOCAL_ERROR_MSG,
5472 name);
5473 } else {
5474 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5475 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02005476 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005477 UNBOUNDFREE_ERROR_MSG, name);
5478 }
5479}
5480
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005481static void
Mark Shannonfee55262019-11-21 09:11:43 +00005482format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevprevopcode, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005483{
5484 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5485 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005486 _PyErr_Format(tstate, PyExc_TypeError,
5487 "'async with' received an object from __aenter__ "
5488 "that does not implement __await__: %.100s",
5489 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005490 }
Mark Shannonfee55262019-11-21 09:11:43 +00005491 else if (prevopcode == WITH_EXCEPT_START || (prevopcode == CALL_FUNCTION && prevprevopcode == DUP_TOP)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005492 _PyErr_Format(tstate, PyExc_TypeError,
5493 "'async with' received an object from __aexit__ "
5494 "that does not implement __await__: %.100s",
5495 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005496 }
5497 }
5498}
5499
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005500static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005501unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005502 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005503{
5504 PyObject *res;
5505 if (Py_REFCNT(v) == 2) {
5506 /* In the common case, there are 2 references to the value
5507 * stored in 'variable' when the += is performed: one on the
5508 * value stack (in 'v') and one still stored in the
5509 * 'variable'. We try to delete the variable now to reduce
5510 * the refcnt to 1.
5511 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005512 int opcode, oparg;
5513 NEXTOPARG();
5514 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005515 case STORE_FAST:
5516 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005517 PyObject **fastlocals = f->f_localsplus;
5518 if (GETLOCAL(oparg) == v)
5519 SETLOCAL(oparg, NULL);
5520 break;
5521 }
5522 case STORE_DEREF:
5523 {
5524 PyObject **freevars = (f->f_localsplus +
5525 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005526 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005527 if (PyCell_GET(c) == v) {
5528 PyCell_SET(c, NULL);
5529 Py_DECREF(v);
5530 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005531 break;
5532 }
5533 case STORE_NAME:
5534 {
5535 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005536 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005537 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005538 if (locals && PyDict_CheckExact(locals)) {
5539 PyObject *w = PyDict_GetItemWithError(locals, name);
5540 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02005541 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005542 {
5543 Py_DECREF(v);
5544 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005545 }
5546 }
5547 break;
5548 }
5549 }
5550 }
5551 res = v;
5552 PyUnicode_Append(&res, w);
5553 return res;
5554}
5555
Guido van Rossum950361c1997-01-24 13:49:28 +00005556#ifdef DYNAMIC_EXECUTION_PROFILE
5557
Skip Montanarof118cb12001-10-15 20:51:38 +00005558static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005559getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005560{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005561 int i;
5562 PyObject *l = PyList_New(256);
5563 if (l == NULL) return NULL;
5564 for (i = 0; i < 256; i++) {
5565 PyObject *x = PyLong_FromLong(a[i]);
5566 if (x == NULL) {
5567 Py_DECREF(l);
5568 return NULL;
5569 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005570 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005571 }
5572 for (i = 0; i < 256; i++)
5573 a[i] = 0;
5574 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005575}
5576
5577PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005578_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005579{
5580#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005581 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005582#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005583 int i;
5584 PyObject *l = PyList_New(257);
5585 if (l == NULL) return NULL;
5586 for (i = 0; i < 257; i++) {
5587 PyObject *x = getarray(dxpairs[i]);
5588 if (x == NULL) {
5589 Py_DECREF(l);
5590 return NULL;
5591 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005592 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005593 }
5594 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005595#endif
5596}
5597
5598#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005599
5600Py_ssize_t
5601_PyEval_RequestCodeExtraIndex(freefunc free)
5602{
Victor Stinner81a7be32020-04-14 15:14:01 +02005603 PyInterpreterState *interp = _PyInterpreterState_GET();
Brett Cannon5c4de282016-09-07 11:16:41 -07005604 Py_ssize_t new_index;
5605
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005606 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005607 return -1;
5608 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005609 new_index = interp->co_extra_user_count++;
5610 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005611 return new_index;
5612}
Łukasz Langaa785c872016-09-09 17:37:37 -07005613
5614static void
5615dtrace_function_entry(PyFrameObject *f)
5616{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005617 const char *filename;
5618 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005619 int lineno;
5620
Victor Stinner6d86a232020-04-29 00:56:58 +02005621 PyCodeObject *code = f->f_code;
5622 filename = PyUnicode_AsUTF8(code->co_filename);
5623 funcname = PyUnicode_AsUTF8(code->co_name);
5624 lineno = PyCode_Addr2Line(code, f->f_lasti);
Łukasz Langaa785c872016-09-09 17:37:37 -07005625
Andy Lestere6be9b52020-02-11 20:28:35 -06005626 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005627}
5628
5629static void
5630dtrace_function_return(PyFrameObject *f)
5631{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005632 const char *filename;
5633 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005634 int lineno;
5635
Victor Stinner6d86a232020-04-29 00:56:58 +02005636 PyCodeObject *code = f->f_code;
5637 filename = PyUnicode_AsUTF8(code->co_filename);
5638 funcname = PyUnicode_AsUTF8(code->co_name);
5639 lineno = PyCode_Addr2Line(code, f->f_lasti);
Łukasz Langaa785c872016-09-09 17:37:37 -07005640
Andy Lestere6be9b52020-02-11 20:28:35 -06005641 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005642}
5643
5644/* DTrace equivalent of maybe_call_line_trace. */
5645static void
5646maybe_dtrace_line(PyFrameObject *frame,
5647 int *instr_lb, int *instr_ub, int *instr_prev)
5648{
5649 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005650 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005651
5652 /* If the last instruction executed isn't in the current
5653 instruction window, reset the window.
5654 */
5655 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5656 PyAddrPair bounds;
5657 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5658 &bounds);
5659 *instr_lb = bounds.ap_lower;
5660 *instr_ub = bounds.ap_upper;
5661 }
5662 /* If the last instruction falls at the start of a line or if
5663 it represents a jump backwards, update the frame's line
5664 number and call the trace function. */
5665 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5666 frame->f_lineno = line;
5667 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5668 if (!co_filename)
5669 co_filename = "?";
5670 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5671 if (!co_name)
5672 co_name = "?";
Andy Lestere6be9b52020-02-11 20:28:35 -06005673 PyDTrace_LINE(co_filename, co_name, line);
Łukasz Langaa785c872016-09-09 17:37:37 -07005674 }
5675 *instr_prev = frame->f_lasti;
5676}
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005677
5678
5679/* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions
5680 for the limited API. */
5681
5682#undef Py_EnterRecursiveCall
5683
5684int Py_EnterRecursiveCall(const char *where)
5685{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005686 return _Py_EnterRecursiveCall_inline(where);
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005687}
5688
5689#undef Py_LeaveRecursiveCall
5690
5691void Py_LeaveRecursiveCall(void)
5692{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005693 _Py_LeaveRecursiveCall_inline();
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005694}