blob: d6b786dc2cda0b7288bba14228b27464d8c71303 [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 Stinner384621c2020-06-22 17:27:35 +020014#include "pycore_call.h" // _PyObject_FastCallDictTstate()
15#include "pycore_ceval.h" // _PyEval_SignalAsyncExc()
16#include "pycore_code.h" // _PyCode_InitOpcache()
17#include "pycore_initconfig.h" // _PyStatus_OK()
18#include "pycore_object.h" // _PyObject_GC_TRACK()
19#include "pycore_pyerrors.h" // _PyErr_Fetch()
20#include "pycore_pylifecycle.h" // _PyErr_Print()
Victor Stinnere560f902020-04-14 18:30:41 +020021#include "pycore_pymem.h" // _PyMem_IsPtrFreed()
22#include "pycore_pystate.h" // _PyInterpreterState_GET()
Victor Stinner384621c2020-06-22 17:27:35 +020023#include "pycore_sysmodule.h" // _PySys_Audit()
24#include "pycore_tuple.h" // _PyTuple_ITEMS()
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 *,
Mark Shannon877df852020-11-12 09:43:29 +000069 PyCodeAddressRange *, int *);
70static void maybe_dtrace_line(PyFrameObject *, PyCodeAddressRange *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070071static 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
Pablo Galindo109826c2020-10-20 06:22:44 +0100114#define OPCODE_CACHE_MAX_TRIES 20
Inada Naoki91234a12019-06-03 21:30:58 +0900115#define OPCACHE_STATS 0 /* Enable stats */
116
117#if OPCACHE_STATS
118static size_t opcache_code_objects = 0;
119static size_t opcache_code_objects_extra_mem = 0;
120
121static size_t opcache_global_opts = 0;
122static size_t opcache_global_hits = 0;
123static size_t opcache_global_misses = 0;
Pablo Galindo109826c2020-10-20 06:22:44 +0100124
125static size_t opcache_attr_opts = 0;
126static size_t opcache_attr_hits = 0;
127static size_t opcache_attr_misses = 0;
128static size_t opcache_attr_deopts = 0;
129static size_t opcache_attr_total = 0;
Inada Naoki91234a12019-06-03 21:30:58 +0900130#endif
131
Victor Stinner5a3a71d2020-03-19 17:40:12 +0100132
Victor Stinnerda2914d2020-03-20 09:29:08 +0100133#ifndef NDEBUG
134/* Ensure that tstate is valid: sanity check for PyEval_AcquireThread() and
135 PyEval_RestoreThread(). Detect if tstate memory was freed. It can happen
136 when a thread continues to run after Python finalization, especially
137 daemon threads. */
138static int
139is_tstate_valid(PyThreadState *tstate)
140{
141 assert(!_PyMem_IsPtrFreed(tstate));
142 assert(!_PyMem_IsPtrFreed(tstate->interp));
143 return 1;
144}
145#endif
146
147
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000148/* This can set eval_breaker to 0 even though gil_drop_request became
149 1. We believe this is all right because the eval loop will release
150 the GIL eventually anyway. */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100151static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200152COMPUTE_EVAL_BREAKER(PyInterpreterState *interp,
Victor Stinner299b8c62020-05-05 17:40:18 +0200153 struct _ceval_runtime_state *ceval,
154 struct _ceval_state *ceval2)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100155{
Victor Stinner299b8c62020-05-05 17:40:18 +0200156 _Py_atomic_store_relaxed(&ceval2->eval_breaker,
157 _Py_atomic_load_relaxed(&ceval2->gil_drop_request)
Victor Stinner0b1e3302020-05-05 16:14:31 +0200158 | (_Py_atomic_load_relaxed(&ceval->signals_pending)
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200159 && _Py_ThreadCanHandleSignals(interp))
Victor Stinner299b8c62020-05-05 17:40:18 +0200160 | (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)
Victor Stinnerd8316882020-03-20 14:50:35 +0100161 && _Py_ThreadCanHandlePendingCalls())
Victor Stinner299b8c62020-05-05 17:40:18 +0200162 | ceval2->pending.async_exc);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100163}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000164
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000165
Victor Stinnerda2914d2020-03-20 09:29:08 +0100166static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200167SET_GIL_DROP_REQUEST(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100168{
Victor Stinner299b8c62020-05-05 17:40:18 +0200169 struct _ceval_state *ceval2 = &interp->ceval;
170 _Py_atomic_store_relaxed(&ceval2->gil_drop_request, 1);
171 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100172}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000173
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000174
Victor Stinnerda2914d2020-03-20 09:29:08 +0100175static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200176RESET_GIL_DROP_REQUEST(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100177{
Victor Stinner299b8c62020-05-05 17:40:18 +0200178 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
179 struct _ceval_state *ceval2 = &interp->ceval;
180 _Py_atomic_store_relaxed(&ceval2->gil_drop_request, 0);
181 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100182}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000183
Eric Snowfdf282d2019-01-11 14:26:55 -0700184
Victor Stinnerda2914d2020-03-20 09:29:08 +0100185static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200186SIGNAL_PENDING_CALLS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100187{
Victor Stinner299b8c62020-05-05 17:40:18 +0200188 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
189 struct _ceval_state *ceval2 = &interp->ceval;
190 _Py_atomic_store_relaxed(&ceval2->pending.calls_to_do, 1);
191 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100192}
Eric Snowfdf282d2019-01-11 14:26:55 -0700193
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000194
Victor Stinnerda2914d2020-03-20 09:29:08 +0100195static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200196UNSIGNAL_PENDING_CALLS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100197{
Victor Stinner299b8c62020-05-05 17:40:18 +0200198 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
199 struct _ceval_state *ceval2 = &interp->ceval;
200 _Py_atomic_store_relaxed(&ceval2->pending.calls_to_do, 0);
201 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100202}
203
204
205static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200206SIGNAL_PENDING_SIGNALS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100207{
Victor Stinner299b8c62020-05-05 17:40:18 +0200208 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
209 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinner0b1e3302020-05-05 16:14:31 +0200210 _Py_atomic_store_relaxed(&ceval->signals_pending, 1);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100211 /* eval_breaker is not set to 1 if thread_can_handle_signals() is false */
Victor Stinner299b8c62020-05-05 17:40:18 +0200212 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100213}
214
215
216static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200217UNSIGNAL_PENDING_SIGNALS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100218{
Victor Stinner299b8c62020-05-05 17:40:18 +0200219 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
220 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinner0b1e3302020-05-05 16:14:31 +0200221 _Py_atomic_store_relaxed(&ceval->signals_pending, 0);
Victor Stinner299b8c62020-05-05 17:40:18 +0200222 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100223}
224
225
226static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200227SIGNAL_ASYNC_EXC(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100228{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200229 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100230 ceval2->pending.async_exc = 1;
231 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
232}
233
234
235static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200236UNSIGNAL_ASYNC_EXC(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100237{
Victor Stinner299b8c62020-05-05 17:40:18 +0200238 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
239 struct _ceval_state *ceval2 = &interp->ceval;
240 ceval2->pending.async_exc = 0;
241 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100242}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000243
244
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000245#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000246#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000247#endif
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000248#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000249
Victor Stinner3026cad2020-06-01 16:02:40 +0200250void _Py_NO_RETURN
251_Py_FatalError_TstateNULL(const char *func)
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100252{
Victor Stinner3026cad2020-06-01 16:02:40 +0200253 _Py_FatalErrorFunc(func,
254 "the function must be called with the GIL held, "
255 "but the GIL is released "
256 "(the current Python thread state is NULL)");
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100257}
258
Victor Stinner7be4e352020-05-05 20:27:47 +0200259#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
260int
261_PyEval_ThreadsInitialized(PyInterpreterState *interp)
262{
263 return gil_created(&interp->ceval.gil);
264}
265
266int
267PyEval_ThreadsInitialized(void)
268{
269 // Fatal error if there is no current interpreter
270 PyInterpreterState *interp = PyInterpreterState_Get();
271 return _PyEval_ThreadsInitialized(interp);
272}
273#else
Tim Peters7f468f22004-10-11 02:40:51 +0000274int
Victor Stinner175a7042020-03-10 00:37:48 +0100275_PyEval_ThreadsInitialized(_PyRuntimeState *runtime)
276{
277 return gil_created(&runtime->ceval.gil);
278}
279
280int
Tim Peters7f468f22004-10-11 02:40:51 +0000281PyEval_ThreadsInitialized(void)
282{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100283 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner175a7042020-03-10 00:37:48 +0100284 return _PyEval_ThreadsInitialized(runtime);
Tim Peters7f468f22004-10-11 02:40:51 +0000285}
Victor Stinner7be4e352020-05-05 20:27:47 +0200286#endif
Tim Peters7f468f22004-10-11 02:40:51 +0000287
Victor Stinner111e4ee2020-03-09 21:24:14 +0100288PyStatus
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200289_PyEval_InitGIL(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000290{
Victor Stinner7be4e352020-05-05 20:27:47 +0200291#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200292 if (!_Py_IsMainInterpreter(tstate)) {
293 /* Currently, the GIL is shared by all interpreters,
294 and only the main interpreter is responsible to create
295 and destroy it. */
296 return _PyStatus_OK();
Victor Stinner111e4ee2020-03-09 21:24:14 +0100297 }
Victor Stinner7be4e352020-05-05 20:27:47 +0200298#endif
Victor Stinner111e4ee2020-03-09 21:24:14 +0100299
Victor Stinner7be4e352020-05-05 20:27:47 +0200300#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
301 struct _gil_runtime_state *gil = &tstate->interp->ceval.gil;
302#else
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200303 struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil;
Victor Stinner7be4e352020-05-05 20:27:47 +0200304#endif
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200305 assert(!gil_created(gil));
Victor Stinner85f5a692020-03-09 22:12:04 +0100306
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200307 PyThread_init_thread();
308 create_gil(gil);
309
310 take_gil(tstate);
311
312 assert(gil_created(gil));
Victor Stinner111e4ee2020-03-09 21:24:14 +0100313 return _PyStatus_OK();
314}
315
316void
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200317_PyEval_FiniGIL(PyThreadState *tstate)
318{
Victor Stinner7be4e352020-05-05 20:27:47 +0200319#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200320 if (!_Py_IsMainInterpreter(tstate)) {
321 /* Currently, the GIL is shared by all interpreters,
322 and only the main interpreter is responsible to create
323 and destroy it. */
324 return;
325 }
Victor Stinner7be4e352020-05-05 20:27:47 +0200326#endif
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200327
Victor Stinner7be4e352020-05-05 20:27:47 +0200328#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
329 struct _gil_runtime_state *gil = &tstate->interp->ceval.gil;
330#else
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200331 struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil;
Victor Stinner7be4e352020-05-05 20:27:47 +0200332#endif
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200333 if (!gil_created(gil)) {
334 /* First Py_InitializeFromConfig() call: the GIL doesn't exist
335 yet: do nothing. */
336 return;
337 }
338
339 destroy_gil(gil);
340 assert(!gil_created(gil));
341}
342
343void
Victor Stinner111e4ee2020-03-09 21:24:14 +0100344PyEval_InitThreads(void)
345{
Victor Stinnerb4698ec2020-03-10 01:28:54 +0100346 /* Do nothing: kept for backward compatibility */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000347}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000348
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000349void
Inada Naoki91234a12019-06-03 21:30:58 +0900350_PyEval_Fini(void)
351{
352#if OPCACHE_STATS
353 fprintf(stderr, "-- Opcode cache number of objects = %zd\n",
354 opcache_code_objects);
355
356 fprintf(stderr, "-- Opcode cache total extra mem = %zd\n",
357 opcache_code_objects_extra_mem);
358
359 fprintf(stderr, "\n");
360
361 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL hits = %zd (%d%%)\n",
362 opcache_global_hits,
363 (int) (100.0 * opcache_global_hits /
364 (opcache_global_hits + opcache_global_misses)));
365
366 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL misses = %zd (%d%%)\n",
367 opcache_global_misses,
368 (int) (100.0 * opcache_global_misses /
369 (opcache_global_hits + opcache_global_misses)));
370
371 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL opts = %zd\n",
372 opcache_global_opts);
373
374 fprintf(stderr, "\n");
Pablo Galindo109826c2020-10-20 06:22:44 +0100375
376 fprintf(stderr, "-- Opcode cache LOAD_ATTR hits = %zd (%d%%)\n",
377 opcache_attr_hits,
378 (int) (100.0 * opcache_attr_hits /
379 opcache_attr_total));
380
381 fprintf(stderr, "-- Opcode cache LOAD_ATTR misses = %zd (%d%%)\n",
382 opcache_attr_misses,
383 (int) (100.0 * opcache_attr_misses /
384 opcache_attr_total));
385
386 fprintf(stderr, "-- Opcode cache LOAD_ATTR opts = %zd\n",
387 opcache_attr_opts);
388
389 fprintf(stderr, "-- Opcode cache LOAD_ATTR deopts = %zd\n",
390 opcache_attr_deopts);
391
392 fprintf(stderr, "-- Opcode cache LOAD_ATTR total = %zd\n",
393 opcache_attr_total);
Inada Naoki91234a12019-06-03 21:30:58 +0900394#endif
395}
396
397void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000398PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000399{
Victor Stinner09532fe2019-05-10 23:39:09 +0200400 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200401 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinner3026cad2020-06-01 16:02:40 +0200402 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100403
Victor Stinner85f5a692020-03-09 22:12:04 +0100404 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000405}
406
407void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000408PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000409{
Victor Stinner09532fe2019-05-10 23:39:09 +0200410 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200411 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100413 We therefore avoid PyThreadState_Get() which dumps a fatal error
Victor Stinnerda2914d2020-03-20 09:29:08 +0100414 in debug mode. */
Victor Stinner299b8c62020-05-05 17:40:18 +0200415 struct _ceval_runtime_state *ceval = &runtime->ceval;
416 struct _ceval_state *ceval2 = &tstate->interp->ceval;
417 drop_gil(ceval, ceval2, tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000418}
419
420void
Victor Stinner23ef89d2020-03-18 02:26:04 +0100421_PyEval_ReleaseLock(PyThreadState *tstate)
422{
423 struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
Victor Stinner0b1e3302020-05-05 16:14:31 +0200424 struct _ceval_state *ceval2 = &tstate->interp->ceval;
425 drop_gil(ceval, ceval2, tstate);
Victor Stinner23ef89d2020-03-18 02:26:04 +0100426}
427
428void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000429PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000430{
Victor Stinner3026cad2020-06-01 16:02:40 +0200431 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100432
Victor Stinner85f5a692020-03-09 22:12:04 +0100433 take_gil(tstate);
Victor Stinnere225beb2019-06-03 18:14:24 +0200434
Victor Stinner85f5a692020-03-09 22:12:04 +0100435 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
Victor Stinnere838a932020-05-05 19:56:48 +0200436#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
437 (void)_PyThreadState_Swap(gilstate, tstate);
438#else
Victor Stinner85f5a692020-03-09 22:12:04 +0100439 if (_PyThreadState_Swap(gilstate, tstate) != NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100440 Py_FatalError("non-NULL old thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200441 }
Victor Stinnere838a932020-05-05 19:56:48 +0200442#endif
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000443}
444
445void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000446PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000447{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100448 assert(is_tstate_valid(tstate));
Victor Stinner09532fe2019-05-10 23:39:09 +0200449
Victor Stinner01b1cc12019-11-20 02:27:56 +0100450 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200451 PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
452 if (new_tstate != tstate) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100453 Py_FatalError("wrong thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200454 }
Victor Stinner0b1e3302020-05-05 16:14:31 +0200455 struct _ceval_runtime_state *ceval = &runtime->ceval;
456 struct _ceval_state *ceval2 = &tstate->interp->ceval;
457 drop_gil(ceval, ceval2, tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000458}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000459
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900460#ifdef HAVE_FORK
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200461/* This function is called from PyOS_AfterFork_Child to destroy all threads
Victor Stinner26881c82020-06-02 15:51:37 +0200462 which are not running in the child process, and clear internal locks
463 which might be held by those threads. */
464PyStatus
Victor Stinner317bab02020-06-02 18:44:54 +0200465_PyEval_ReInitThreads(PyThreadState *tstate)
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000466{
Victor Stinner317bab02020-06-02 18:44:54 +0200467 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner7be4e352020-05-05 20:27:47 +0200468
469#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
470 struct _gil_runtime_state *gil = &tstate->interp->ceval.gil;
471#else
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100472 struct _gil_runtime_state *gil = &runtime->ceval.gil;
Victor Stinner7be4e352020-05-05 20:27:47 +0200473#endif
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100474 if (!gil_created(gil)) {
Victor Stinner26881c82020-06-02 15:51:37 +0200475 return _PyStatus_OK();
Victor Stinner09532fe2019-05-10 23:39:09 +0200476 }
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100477 recreate_gil(gil);
Victor Stinner85f5a692020-03-09 22:12:04 +0100478
479 take_gil(tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700480
Victor Stinner50e6e992020-03-19 02:41:21 +0100481 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900482 if (_PyThread_at_fork_reinit(&pending->lock) < 0) {
Victor Stinner26881c82020-06-02 15:51:37 +0200483 return _PyStatus_ERR("Can't reinitialize pending calls lock");
Eric Snow8479a342019-03-08 23:44:33 -0700484 }
Jesse Nollera8513972008-07-17 16:49:17 +0000485
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200486 /* Destroy all threads except the current one */
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100487 _PyThreadState_DeleteExcept(runtime, tstate);
Victor Stinner26881c82020-06-02 15:51:37 +0200488 return _PyStatus_OK();
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000489}
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900490#endif
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000491
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000492/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600493 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000494
495void
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100496_PyEval_SignalAsyncExc(PyThreadState *tstate)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000497{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200498 assert(is_tstate_valid(tstate));
499 SIGNAL_ASYNC_EXC(tstate->interp);
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000500}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000501
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000502PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000503PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000504{
Victor Stinner09532fe2019-05-10 23:39:09 +0200505 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere838a932020-05-05 19:56:48 +0200506#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
507 PyThreadState *old_tstate = _PyThreadState_GET();
508 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, old_tstate);
509#else
Victor Stinner09532fe2019-05-10 23:39:09 +0200510 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
Victor Stinnere838a932020-05-05 19:56:48 +0200511#endif
Victor Stinner3026cad2020-06-01 16:02:40 +0200512 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100513
Victor Stinner0b1e3302020-05-05 16:14:31 +0200514 struct _ceval_runtime_state *ceval = &runtime->ceval;
515 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinner7be4e352020-05-05 20:27:47 +0200516#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
517 assert(gil_created(&ceval2->gil));
518#else
Victor Stinnere225beb2019-06-03 18:14:24 +0200519 assert(gil_created(&ceval->gil));
Victor Stinner7be4e352020-05-05 20:27:47 +0200520#endif
Victor Stinner0b1e3302020-05-05 16:14:31 +0200521 drop_gil(ceval, ceval2, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000523}
524
525void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000526PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000527{
Victor Stinner3026cad2020-06-01 16:02:40 +0200528 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100529
Victor Stinner85f5a692020-03-09 22:12:04 +0100530 take_gil(tstate);
Victor Stinner17c68b82020-01-30 12:20:48 +0100531
Victor Stinner85f5a692020-03-09 22:12:04 +0100532 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
533 _PyThreadState_Swap(gilstate, tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000534}
535
536
Guido van Rossuma9672091994-09-14 13:31:22 +0000537/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
538 signal handlers or Mac I/O completion routines) can schedule calls
539 to a function to be called synchronously.
540 The synchronous function is called with one void* argument.
541 It should return 0 for success or -1 for failure -- failure should
542 be accompanied by an exception.
543
544 If registry succeeds, the registry function returns 0; if it fails
545 (e.g. due to too many pending calls) it returns -1 (without setting
546 an exception condition).
547
548 Note that because registry may occur from within signal handlers,
549 or other asynchronous events, calling malloc() is unsafe!
550
Guido van Rossuma9672091994-09-14 13:31:22 +0000551 Any thread can schedule pending calls, but only the main thread
552 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000553 There is no facility to schedule calls to a particular thread, but
554 that should be easy to change, should that ever be required. In
555 that case, the static variables here should go into the python
556 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000557*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000558
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200559void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200560_PyEval_SignalReceived(PyInterpreterState *interp)
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200561{
562 /* bpo-30703: Function called when the C signal handler of Python gets a
Victor Stinner50e6e992020-03-19 02:41:21 +0100563 signal. We cannot queue a callback using _PyEval_AddPendingCall() since
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200564 that function is not async-signal-safe. */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200565 SIGNAL_PENDING_SIGNALS(interp);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200566}
567
Eric Snow5be45a62019-03-08 22:47:07 -0700568/* Push one item onto the queue while holding the lock. */
569static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200570_push_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600571 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700572{
Eric Snow842a2f02019-03-15 15:47:51 -0600573 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700574 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600575 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700576 return -1; /* Queue full */
577 }
Eric Snow842a2f02019-03-15 15:47:51 -0600578 pending->calls[i].func = func;
579 pending->calls[i].arg = arg;
580 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700581 return 0;
582}
583
584/* Pop one item off the queue while holding the lock. */
585static void
Victor Stinnere225beb2019-06-03 18:14:24 +0200586_pop_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600587 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700588{
Eric Snow842a2f02019-03-15 15:47:51 -0600589 int i = pending->first;
590 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700591 return; /* Queue empty */
592 }
593
Eric Snow842a2f02019-03-15 15:47:51 -0600594 *func = pending->calls[i].func;
595 *arg = pending->calls[i].arg;
596 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700597}
598
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200599/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000600 scheduling to be made from any thread, and even from an executing
601 callback.
602 */
603
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000604int
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200605_PyEval_AddPendingCall(PyInterpreterState *interp,
Victor Stinner09532fe2019-05-10 23:39:09 +0200606 int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000607{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200608 struct _pending_calls *pending = &interp->ceval.pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600609
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200610 /* Ensure that _PyEval_InitPendingCalls() was called
611 and that _PyEval_FiniPendingCalls() is not called yet. */
612 assert(pending->lock != NULL);
613
Eric Snow842a2f02019-03-15 15:47:51 -0600614 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200615 int result = _push_pending_call(pending, func, arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600616 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700617
Victor Stinnere225beb2019-06-03 18:14:24 +0200618 /* signal main loop */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200619 SIGNAL_PENDING_CALLS(interp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000621}
622
Victor Stinner09532fe2019-05-10 23:39:09 +0200623int
624Py_AddPendingCall(int (*func)(void *), void *arg)
625{
Victor Stinner50e6e992020-03-19 02:41:21 +0100626 /* Best-effort to support subinterpreters and calls with the GIL released.
627
628 First attempt _PyThreadState_GET() since it supports subinterpreters.
629
630 If the GIL is released, _PyThreadState_GET() returns NULL . In this
631 case, use PyGILState_GetThisThreadState() which works even if the GIL
632 is released.
633
634 Sadly, PyGILState_GetThisThreadState() doesn't support subinterpreters:
635 see bpo-10915 and bpo-15751.
636
Victor Stinner8849e592020-03-18 19:28:53 +0100637 Py_AddPendingCall() doesn't require the caller to hold the GIL. */
Victor Stinner50e6e992020-03-19 02:41:21 +0100638 PyThreadState *tstate = _PyThreadState_GET();
639 if (tstate == NULL) {
640 tstate = PyGILState_GetThisThreadState();
641 }
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200642
643 PyInterpreterState *interp;
644 if (tstate != NULL) {
645 interp = tstate->interp;
646 }
647 else {
648 /* Last resort: use the main interpreter */
649 interp = _PyRuntime.interpreters.main;
650 }
651 return _PyEval_AddPendingCall(interp, func, arg);
Victor Stinner09532fe2019-05-10 23:39:09 +0200652}
653
Eric Snowfdf282d2019-01-11 14:26:55 -0700654static int
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100655handle_signals(PyThreadState *tstate)
Eric Snowfdf282d2019-01-11 14:26:55 -0700656{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200657 assert(is_tstate_valid(tstate));
658 if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
Eric Snow64d6cc82019-02-23 15:40:43 -0700659 return 0;
660 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700661
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200662 UNSIGNAL_PENDING_SIGNALS(tstate->interp);
Victor Stinner72818982020-03-26 22:28:11 +0100663 if (_PyErr_CheckSignalsTstate(tstate) < 0) {
664 /* On failure, re-schedule a call to handle_signals(). */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200665 SIGNAL_PENDING_SIGNALS(tstate->interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700666 return -1;
667 }
668 return 0;
669}
670
671static int
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100672make_pending_calls(PyThreadState *tstate)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000673{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200674 assert(is_tstate_valid(tstate));
675
Victor Stinnerd8316882020-03-20 14:50:35 +0100676 /* only execute pending calls on main thread */
677 if (!_Py_ThreadCanHandlePendingCalls()) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200678 return 0;
679 }
680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 /* don't perform recursive pending calls */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100682 static int busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700683 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700685 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200686 busy = 1;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100687
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200688 /* unsignal before starting to call callbacks, so that any callback
689 added in-between re-signals */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200690 UNSIGNAL_PENDING_CALLS(tstate->interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700691 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 /* perform a bounded number of calls, in case of recursion */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100694 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Eric Snowfdf282d2019-01-11 14:26:55 -0700695 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700696 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 void *arg = NULL;
698
699 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600700 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200701 _pop_pending_call(pending, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600702 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700703
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100704 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700705 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100706 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700707 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700708 res = func(arg);
709 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200710 goto error;
711 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200713
Charles-François Natalif23339a2011-07-23 18:15:43 +0200714 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700715 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200716
717error:
718 busy = 0;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200719 SIGNAL_PENDING_CALLS(tstate->interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700720 return res;
721}
722
Eric Snow842a2f02019-03-15 15:47:51 -0600723void
Victor Stinner2b1df452020-01-13 18:46:59 +0100724_Py_FinishPendingCalls(PyThreadState *tstate)
Eric Snow842a2f02019-03-15 15:47:51 -0600725{
Eric Snow842a2f02019-03-15 15:47:51 -0600726 assert(PyGILState_Check());
727
Victor Stinner50e6e992020-03-19 02:41:21 +0100728 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200729
Eric Snow842a2f02019-03-15 15:47:51 -0600730 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
731 return;
732 }
733
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100734 if (make_pending_calls(tstate) < 0) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200735 PyObject *exc, *val, *tb;
736 _PyErr_Fetch(tstate, &exc, &val, &tb);
737 PyErr_BadInternalCall();
738 _PyErr_ChainExceptions(exc, val, tb);
739 _PyErr_Print(tstate);
Eric Snow842a2f02019-03-15 15:47:51 -0600740 }
741}
742
Eric Snowfdf282d2019-01-11 14:26:55 -0700743/* Py_MakePendingCalls() is a simple wrapper for the sake
744 of backward-compatibility. */
745int
746Py_MakePendingCalls(void)
747{
748 assert(PyGILState_Check());
749
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100750 PyThreadState *tstate = _PyThreadState_GET();
751
Eric Snowfdf282d2019-01-11 14:26:55 -0700752 /* Python signal handler doesn't really queue a callback: it only signals
753 that a signal was received, see _PyEval_SignalReceived(). */
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100754 int res = handle_signals(tstate);
Eric Snowfdf282d2019-01-11 14:26:55 -0700755 if (res != 0) {
756 return res;
757 }
758
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100759 res = make_pending_calls(tstate);
Eric Snowb75b1a352019-04-12 10:20:10 -0600760 if (res != 0) {
761 return res;
762 }
763
764 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000765}
766
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000767/* The interpreter's recursion limit */
768
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000769#ifndef Py_DEFAULT_RECURSION_LIMIT
Victor Stinner19c3ac92020-09-23 14:04:57 +0200770# define Py_DEFAULT_RECURSION_LIMIT 1000
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000771#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600772
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600773void
Victor Stinnerdab84232020-03-17 18:56:44 +0100774_PyEval_InitRuntimeState(struct _ceval_runtime_state *ceval)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600775{
Victor Stinner7be4e352020-05-05 20:27:47 +0200776#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinnerdab84232020-03-17 18:56:44 +0100777 _gil_initialize(&ceval->gil);
Victor Stinner7be4e352020-05-05 20:27:47 +0200778#endif
Victor Stinnerdab84232020-03-17 18:56:44 +0100779}
780
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200781int
Victor Stinnerdab84232020-03-17 18:56:44 +0100782_PyEval_InitState(struct _ceval_state *ceval)
783{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200784 ceval->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
785
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200786 struct _pending_calls *pending = &ceval->pending;
787 assert(pending->lock == NULL);
788
789 pending->lock = PyThread_allocate_lock();
790 if (pending->lock == NULL) {
791 return -1;
792 }
Victor Stinner7be4e352020-05-05 20:27:47 +0200793
794#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
795 _gil_initialize(&ceval->gil);
796#endif
797
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200798 return 0;
799}
800
801void
802_PyEval_FiniState(struct _ceval_state *ceval)
803{
804 struct _pending_calls *pending = &ceval->pending;
805 if (pending->lock != NULL) {
806 PyThread_free_lock(pending->lock);
807 pending->lock = NULL;
808 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600809}
810
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000811int
812Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000813{
Victor Stinner1bcc32f2020-06-10 20:08:26 +0200814 PyInterpreterState *interp = _PyInterpreterState_GET();
815 return interp->ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000816}
817
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000818void
819Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000820{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200821 PyThreadState *tstate = _PyThreadState_GET();
822 tstate->interp->ceval.recursion_limit = new_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000823}
824
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100825/* The function _Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
Victor Stinner19c3ac92020-09-23 14:04:57 +0200826 if the recursion_depth reaches recursion_limit.
827 If USE_STACKCHECK, the macro decrements recursion_limit
Armin Rigo2b3eb402003-10-28 12:05:48 +0000828 to guarantee that _Py_CheckRecursiveCall() is regularly called.
829 Without USE_STACKCHECK, there is no need for this. */
830int
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100831_Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000832{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200833 int recursion_limit = tstate->interp->ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000834
835#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700836 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000837 if (PyOS_CheckStack()) {
838 --tstate->recursion_depth;
Victor Stinner438a12d2019-05-24 17:01:38 +0200839 _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 return -1;
841 }
pdox18967932017-10-25 23:03:01 -0700842#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 if (tstate->overflowed) {
844 if (tstate->recursion_depth > recursion_limit + 50) {
845 /* Overflowing while handling an overflow. Give up. */
846 Py_FatalError("Cannot recover from stack overflow.");
847 }
848 return 0;
849 }
850 if (tstate->recursion_depth > recursion_limit) {
851 --tstate->recursion_depth;
852 tstate->overflowed = 1;
Victor Stinner438a12d2019-05-24 17:01:38 +0200853 _PyErr_Format(tstate, PyExc_RecursionError,
854 "maximum recursion depth exceeded%s",
855 where);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 return -1;
857 }
858 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000859}
860
Victor Stinner09532fe2019-05-10 23:39:09 +0200861static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
Victor Stinner438a12d2019-05-24 17:01:38 +0200862static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000863
Victor Stinnere225beb2019-06-03 18:14:24 +0200864#define _Py_TracingPossible(ceval) ((ceval)->tracing_possible)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000865
Guido van Rossum374a9221991-04-04 10:40:29 +0000866
Guido van Rossumb209a111997-04-29 18:18:01 +0000867PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000868PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000869{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 return PyEval_EvalCodeEx(co,
871 globals, locals,
872 (PyObject **)NULL, 0,
873 (PyObject **)NULL, 0,
874 (PyObject **)NULL, 0,
875 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000876}
877
878
879/* Interpreter main loop */
880
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000881PyObject *
Victor Stinnerb9e68122019-11-14 12:20:46 +0100882PyEval_EvalFrame(PyFrameObject *f)
883{
Victor Stinner0b72b232020-03-12 23:18:39 +0100884 /* Function kept for backward compatibility */
Victor Stinnerb9e68122019-11-14 12:20:46 +0100885 PyThreadState *tstate = _PyThreadState_GET();
886 return _PyEval_EvalFrame(tstate, f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000887}
888
889PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000890PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000891{
Victor Stinnerb9e68122019-11-14 12:20:46 +0100892 PyThreadState *tstate = _PyThreadState_GET();
893 return _PyEval_EvalFrame(tstate, f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700894}
895
Victor Stinnerda2914d2020-03-20 09:29:08 +0100896
897/* Handle signals, pending calls, GIL drop request
898 and asynchronous exception */
899static int
900eval_frame_handle_pending(PyThreadState *tstate)
901{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100902 _PyRuntimeState * const runtime = &_PyRuntime;
903 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200904
905 /* Pending signals */
Victor Stinner299b8c62020-05-05 17:40:18 +0200906 if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
Victor Stinnerda2914d2020-03-20 09:29:08 +0100907 if (handle_signals(tstate) != 0) {
908 return -1;
909 }
910 }
911
912 /* Pending calls */
Victor Stinner299b8c62020-05-05 17:40:18 +0200913 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100914 if (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)) {
915 if (make_pending_calls(tstate) != 0) {
916 return -1;
917 }
918 }
919
920 /* GIL drop request */
Victor Stinner0b1e3302020-05-05 16:14:31 +0200921 if (_Py_atomic_load_relaxed(&ceval2->gil_drop_request)) {
Victor Stinnerda2914d2020-03-20 09:29:08 +0100922 /* Give another thread a chance */
923 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
924 Py_FatalError("tstate mix-up");
925 }
Victor Stinner0b1e3302020-05-05 16:14:31 +0200926 drop_gil(ceval, ceval2, tstate);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100927
928 /* Other threads may run now */
929
930 take_gil(tstate);
931
Victor Stinnere838a932020-05-05 19:56:48 +0200932#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
933 (void)_PyThreadState_Swap(&runtime->gilstate, tstate);
934#else
Victor Stinnerda2914d2020-03-20 09:29:08 +0100935 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
936 Py_FatalError("orphan tstate");
937 }
Victor Stinnere838a932020-05-05 19:56:48 +0200938#endif
Victor Stinnerda2914d2020-03-20 09:29:08 +0100939 }
940
941 /* Check for asynchronous exception. */
942 if (tstate->async_exc != NULL) {
943 PyObject *exc = tstate->async_exc;
944 tstate->async_exc = NULL;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200945 UNSIGNAL_ASYNC_EXC(tstate->interp);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100946 _PyErr_SetNone(tstate, exc);
947 Py_DECREF(exc);
948 return -1;
949 }
950
951 return 0;
952}
953
Victor Stinnerc6944e72016-11-11 02:13:35 +0100954PyObject* _Py_HOT_FUNCTION
Victor Stinner0b72b232020-03-12 23:18:39 +0100955_PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
Brett Cannon3cebf932016-09-05 15:33:46 -0700956{
Victor Stinner3026cad2020-06-01 16:02:40 +0200957 _Py_EnsureTstateNotNULL(tstate);
Victor Stinner0b72b232020-03-12 23:18:39 +0100958
Guido van Rossum950361c1997-01-24 13:49:28 +0000959#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000961#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200962 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300963 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200964 int opcode; /* Current opcode */
965 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200966 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 PyObject *retval = NULL; /* Return value */
Victor Stinnerdab84232020-03-17 18:56:44 +0100968 struct _ceval_state * const ceval2 = &tstate->interp->ceval;
Victor Stinner50e6e992020-03-19 02:41:21 +0100969 _Py_atomic_int * const eval_breaker = &ceval2->eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000971
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000973
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000975
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 is true when the line being executed has changed. The
977 initial values are such as to make this false the first
978 time it is tested. */
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000979
Serhiy Storchakaab874002016-09-11 13:48:15 +0300980 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 PyObject *names;
982 PyObject *consts;
Inada Naoki91234a12019-06-03 21:30:58 +0900983 _PyOpcache *co_opcache;
Guido van Rossum374a9221991-04-04 10:40:29 +0000984
Brett Cannon368b4b72012-04-02 12:17:59 -0400985#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200986 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400987#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200988
Antoine Pitroub52ec782009-01-25 16:34:23 +0000989/* Computed GOTOs, or
990 the-optimization-commonly-but-improperly-known-as-"threaded code"
991 using gcc's labels-as-values extension
992 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
993
994 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000996 combined with a lookup table of jump addresses. However, since the
997 indirect jump instruction is shared by all opcodes, the CPU will have a
998 hard time making the right prediction for where to jump next (actually,
999 it will be always wrong except in the uncommon case of a sequence of
1000 several identical opcodes).
1001
1002 "Threaded code" in contrast, uses an explicit jump table and an explicit
1003 indirect jump instruction at the end of each opcode. Since the jump
1004 instruction is at a different address for each opcode, the CPU will make a
1005 separate prediction for each of these instructions, which is equivalent to
1006 predicting the second opcode of each opcode pair. These predictions have
1007 a much better chance to turn out valid, especially in small bytecode loops.
1008
1009 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +00001011 and potentially many more instructions (depending on the pipeline width).
1012 A correctly predicted branch, however, is nearly free.
1013
1014 At the time of this writing, the "threaded code" version is up to 15-20%
1015 faster than the normal "switch" version, depending on the compiler and the
1016 CPU architecture.
1017
1018 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
1019 because it would render the measurements invalid.
1020
1021
1022 NOTE: care must be taken that the compiler doesn't try to "optimize" the
1023 indirect jumps by sharing them between all opcodes. Such optimizations
1024 can be disabled on gcc by using the -fno-gcse flag (or possibly
1025 -fno-crossjumping).
1026*/
1027
Antoine Pitrou042b1282010-08-13 21:15:58 +00001028#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +00001029#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +00001030#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +00001031#endif
1032
Antoine Pitrou042b1282010-08-13 21:15:58 +00001033#ifdef HAVE_COMPUTED_GOTOS
1034 #ifndef USE_COMPUTED_GOTOS
1035 #define USE_COMPUTED_GOTOS 1
1036 #endif
1037#else
1038 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
1039 #error "Computed gotos are not supported on this compiler."
1040 #endif
1041 #undef USE_COMPUTED_GOTOS
1042 #define USE_COMPUTED_GOTOS 0
1043#endif
1044
1045#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +00001046/* Import the static jump table */
1047#include "opcode_targets.h"
1048
Antoine Pitroub52ec782009-01-25 16:34:23 +00001049#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -07001050 op: \
1051 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +00001052
Antoine Pitroub52ec782009-01-25 16:34:23 +00001053#ifdef LLTRACE
1054#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 { \
Victor Stinnerdab84232020-03-17 18:56:44 +01001056 if (!lltrace && !_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001058 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001059 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 } \
1061 goto fast_next_opcode; \
1062 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001063#else
1064#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 { \
Victor Stinnerdab84232020-03-17 18:56:44 +01001066 if (!_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001068 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001069 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 } \
1071 goto fast_next_opcode; \
1072 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001073#endif
1074
Victor Stinner09532fe2019-05-10 23:39:09 +02001075#define DISPATCH() \
1076 { \
1077 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
1078 FAST_DISPATCH(); \
1079 } \
1080 continue; \
1081 }
1082
Antoine Pitroub52ec782009-01-25 16:34:23 +00001083#else
Benjamin Petersonddd19492018-09-16 22:38:02 -07001084#define TARGET(op) op
Antoine Pitroub52ec782009-01-25 16:34:23 +00001085#define FAST_DISPATCH() goto fast_next_opcode
Victor Stinner09532fe2019-05-10 23:39:09 +02001086#define DISPATCH() continue
Antoine Pitroub52ec782009-01-25 16:34:23 +00001087#endif
1088
1089
Neal Norwitza81d2202002-07-14 00:27:26 +00001090/* Tuple access macros */
1091
1092#ifndef Py_DEBUG
1093#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
1094#else
1095#define GETITEM(v, i) PyTuple_GetItem((v), (i))
1096#endif
1097
Guido van Rossum374a9221991-04-04 10:40:29 +00001098/* Code access macros */
1099
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001100/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001101#define INSTR_OFFSET() \
1102 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001103#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001104 _Py_CODEUNIT word = *next_instr; \
1105 opcode = _Py_OPCODE(word); \
1106 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001107 next_instr++; \
1108 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +03001109#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
1110#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +00001111
Raymond Hettingerf606f872003-03-16 03:11:04 +00001112/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 Some opcodes tend to come in pairs thus making it possible to
1114 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001115 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001116
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 Verifying the prediction costs a single high-speed test of a register
1118 variable against a constant. If the pairing was good, then the
1119 processor's own internal branch predication has a high likelihood of
1120 success, resulting in a nearly zero-overhead transition to the
1121 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001122 including its unpredictable switch-case branch. Combined with the
1123 processor's internal branch prediction, a successful PREDICT has the
1124 effect of making the two opcodes run as if they were a single new opcode
1125 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001126
Georg Brandl86b2fb92008-07-16 03:43:04 +00001127 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001128 predictions turned-on and interpret the results as if some opcodes
1129 had been combined or turn-off predictions so that the opcode frequency
1130 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001131
1132 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 the CPU to record separate branch prediction information for each
1134 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001135
Raymond Hettingerf606f872003-03-16 03:11:04 +00001136*/
1137
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001138#define PREDICT_ID(op) PRED_##op
1139
Antoine Pitrou042b1282010-08-13 21:15:58 +00001140#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001141#define PREDICT(op) if (0) goto PREDICT_ID(op)
Raymond Hettingera7216982004-02-08 19:59:27 +00001142#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001143#define PREDICT(op) \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001144 do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001145 _Py_CODEUNIT word = *next_instr; \
1146 opcode = _Py_OPCODE(word); \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001147 if (opcode == op) { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001148 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001149 next_instr++; \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001150 goto PREDICT_ID(op); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001151 } \
1152 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +00001153#endif
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001154#define PREDICTED(op) PREDICT_ID(op):
Antoine Pitroub52ec782009-01-25 16:34:23 +00001155
Raymond Hettingerf606f872003-03-16 03:11:04 +00001156
Guido van Rossum374a9221991-04-04 10:40:29 +00001157/* Stack manipulation macros */
1158
Martin v. Löwis18e16552006-02-15 17:27:45 +00001159/* The stack can grow at most MAXINT deep, as co_nlocals and
1160 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001161#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1162#define EMPTY() (STACK_LEVEL() == 0)
1163#define TOP() (stack_pointer[-1])
1164#define SECOND() (stack_pointer[-2])
1165#define THIRD() (stack_pointer[-3])
1166#define FOURTH() (stack_pointer[-4])
1167#define PEEK(n) (stack_pointer[-(n)])
1168#define SET_TOP(v) (stack_pointer[-1] = (v))
1169#define SET_SECOND(v) (stack_pointer[-2] = (v))
1170#define SET_THIRD(v) (stack_pointer[-3] = (v))
1171#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Stefan Krahb7e10102010-06-23 18:42:39 +00001172#define BASIC_STACKADJ(n) (stack_pointer += n)
1173#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1174#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001175
Guido van Rossum96a42c81992-01-12 02:29:51 +00001176#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001178 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001179 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +02001180#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001181 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +00001182#define STACK_GROW(n) do { \
1183 assert(n >= 0); \
1184 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001185 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001186 assert(STACK_LEVEL() <= co->co_stacksize); \
1187 } while (0)
1188#define STACK_SHRINK(n) do { \
1189 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +02001190 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001191 (void)(BASIC_STACKADJ(-n)); \
1192 assert(STACK_LEVEL() <= co->co_stacksize); \
1193 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +00001194#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +02001195 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001196 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001197#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001198#define PUSH(v) BASIC_PUSH(v)
1199#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +00001200#define STACK_GROW(n) BASIC_STACKADJ(n)
1201#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001202#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001203#endif
1204
Guido van Rossum681d79a1995-07-18 14:51:37 +00001205/* Local variable macros */
1206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001208
1209/* The SETLOCAL() macro must not DECREF the local variable in-place and
1210 then store the new value; it must copy the old value to a temporary
1211 value, then store the new value, and then DECREF the temporary value.
1212 This is because it is possible that during the DECREF the frame is
1213 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1214 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001216 GETLOCAL(i) = value; \
1217 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001218
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001219
1220#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001221 while (STACK_LEVEL() > (b)->b_level) { \
1222 PyObject *v = POP(); \
1223 Py_XDECREF(v); \
1224 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001225
1226#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001227 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001228 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +01001229 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1231 while (STACK_LEVEL() > (b)->b_level + 3) { \
1232 value = POP(); \
1233 Py_XDECREF(value); \
1234 } \
Mark Shannonae3087c2017-10-22 22:41:51 +01001235 exc_info = tstate->exc_info; \
1236 type = exc_info->exc_type; \
1237 value = exc_info->exc_value; \
1238 traceback = exc_info->exc_traceback; \
1239 exc_info->exc_type = POP(); \
1240 exc_info->exc_value = POP(); \
1241 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 Py_XDECREF(type); \
1243 Py_XDECREF(value); \
1244 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001245 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001246
Inada Naoki91234a12019-06-03 21:30:58 +09001247 /* macros for opcode cache */
1248#define OPCACHE_CHECK() \
1249 do { \
1250 co_opcache = NULL; \
1251 if (co->co_opcache != NULL) { \
Pablo Galindo109826c2020-10-20 06:22:44 +01001252 unsigned char co_opcache_offset = \
Inada Naoki91234a12019-06-03 21:30:58 +09001253 co->co_opcache_map[next_instr - first_instr]; \
Pablo Galindo109826c2020-10-20 06:22:44 +01001254 if (co_opcache_offset > 0) { \
1255 assert(co_opcache_offset <= co->co_opcache_size); \
1256 co_opcache = &co->co_opcache[co_opcache_offset - 1]; \
Inada Naoki91234a12019-06-03 21:30:58 +09001257 assert(co_opcache != NULL); \
Inada Naoki91234a12019-06-03 21:30:58 +09001258 } \
1259 } \
1260 } while (0)
1261
Pablo Galindo109826c2020-10-20 06:22:44 +01001262#define OPCACHE_DEOPT() \
1263 do { \
1264 if (co_opcache != NULL) { \
1265 co_opcache->optimized = -1; \
1266 unsigned char co_opcache_offset = \
1267 co->co_opcache_map[next_instr - first_instr]; \
1268 assert(co_opcache_offset <= co->co_opcache_size); \
1269 co->co_opcache_map[co_opcache_offset] = 0; \
1270 co_opcache = NULL; \
1271 } \
1272 } while (0)
1273
1274#define OPCACHE_DEOPT_LOAD_ATTR() \
1275 do { \
1276 if (co_opcache != NULL) { \
1277 OPCACHE_STAT_ATTR_DEOPT(); \
1278 OPCACHE_DEOPT(); \
1279 } \
1280 } while (0)
1281
1282#define OPCACHE_MAYBE_DEOPT_LOAD_ATTR() \
1283 do { \
1284 if (co_opcache != NULL && --co_opcache->optimized <= 0) { \
1285 OPCACHE_DEOPT_LOAD_ATTR(); \
1286 } \
1287 } while (0)
1288
Inada Naoki91234a12019-06-03 21:30:58 +09001289#if OPCACHE_STATS
1290
1291#define OPCACHE_STAT_GLOBAL_HIT() \
1292 do { \
1293 if (co->co_opcache != NULL) opcache_global_hits++; \
1294 } while (0)
1295
1296#define OPCACHE_STAT_GLOBAL_MISS() \
1297 do { \
1298 if (co->co_opcache != NULL) opcache_global_misses++; \
1299 } while (0)
1300
1301#define OPCACHE_STAT_GLOBAL_OPT() \
1302 do { \
1303 if (co->co_opcache != NULL) opcache_global_opts++; \
1304 } while (0)
1305
Pablo Galindo109826c2020-10-20 06:22:44 +01001306#define OPCACHE_STAT_ATTR_HIT() \
1307 do { \
1308 if (co->co_opcache != NULL) opcache_attr_hits++; \
1309 } while (0)
1310
1311#define OPCACHE_STAT_ATTR_MISS() \
1312 do { \
1313 if (co->co_opcache != NULL) opcache_attr_misses++; \
1314 } while (0)
1315
1316#define OPCACHE_STAT_ATTR_OPT() \
1317 do { \
1318 if (co->co_opcache!= NULL) opcache_attr_opts++; \
1319 } while (0)
1320
1321#define OPCACHE_STAT_ATTR_DEOPT() \
1322 do { \
1323 if (co->co_opcache != NULL) opcache_attr_deopts++; \
1324 } while (0)
1325
1326#define OPCACHE_STAT_ATTR_TOTAL() \
1327 do { \
1328 if (co->co_opcache != NULL) opcache_attr_total++; \
1329 } while (0)
1330
Inada Naoki91234a12019-06-03 21:30:58 +09001331#else /* OPCACHE_STATS */
1332
1333#define OPCACHE_STAT_GLOBAL_HIT()
1334#define OPCACHE_STAT_GLOBAL_MISS()
1335#define OPCACHE_STAT_GLOBAL_OPT()
1336
Pablo Galindo109826c2020-10-20 06:22:44 +01001337#define OPCACHE_STAT_ATTR_HIT()
1338#define OPCACHE_STAT_ATTR_MISS()
1339#define OPCACHE_STAT_ATTR_OPT()
1340#define OPCACHE_STAT_ATTR_DEOPT()
1341#define OPCACHE_STAT_ATTR_TOTAL()
1342
Inada Naoki91234a12019-06-03 21:30:58 +09001343#endif
1344
Guido van Rossuma027efa1997-05-05 20:56:21 +00001345/* Start of code */
1346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001347 /* push frame */
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001348 if (_Py_EnterRecursiveCall(tstate, "")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001350 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 if (tstate->use_tracing) {
1355 if (tstate->c_tracefunc != NULL) {
1356 /* tstate->c_tracefunc, if defined, is a
1357 function that will be called on *every* entry
1358 to a code block. Its return value, if not
1359 None, is a function that will be called at
1360 the start of each executed line of code.
1361 (Actually, the function must return itself
1362 in order to continue tracing.) The trace
1363 functions are called with three arguments:
1364 a pointer to the current frame, a string
1365 indicating why the function is called, and
1366 an argument which depends on the situation.
1367 The global trace function is also called
1368 whenever an exception is detected. */
1369 if (call_trace_protected(tstate->c_tracefunc,
1370 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001371 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 /* Trace function raised an error */
1373 goto exit_eval_frame;
1374 }
1375 }
1376 if (tstate->c_profilefunc != NULL) {
1377 /* Similar for c_profilefunc, except it needn't
1378 return itself and isn't called for "line" events */
1379 if (call_trace_protected(tstate->c_profilefunc,
1380 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001381 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 /* Profile function raised an error */
1383 goto exit_eval_frame;
1384 }
1385 }
1386 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001387
Łukasz Langaa785c872016-09-09 17:37:37 -07001388 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1389 dtrace_function_entry(f);
1390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 co = f->f_code;
Mark Shannon877df852020-11-12 09:43:29 +00001392 PyCodeAddressRange bounds;
1393 _PyCode_InitAddressRange(co, &bounds);
1394 int instr_prev = -1;
1395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 names = co->co_names;
1397 consts = co->co_consts;
1398 fastlocals = f->f_localsplus;
1399 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001400 assert(PyBytes_Check(co->co_code));
1401 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001402 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1403 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1404 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001405 /*
1406 f->f_lasti refers to the index of the last instruction,
1407 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001408
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001409 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001410 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 When the PREDICT() macros are enabled, some opcode pairs follow in
1413 direct succession without updating f->f_lasti. A successful
1414 prediction effectively links the two codes together as if they
1415 were a single new opcode; accordingly,f->f_lasti will point to
1416 the first code in the pair (for instance, GET_ITER followed by
1417 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001418 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001420 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001421 next_instr = first_instr;
1422 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001423 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1424 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001425 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01001426 stack_pointer = f->f_valuestack + f->f_stackdepth;
1427 /* Set f->f_stackdepth to -1.
1428 * Update when returning or calling trace function.
1429 Having f_stackdepth <= 0 ensures that invalid
1430 values are not visible to the cycle GC.
1431 We choose -1 rather than 0 to assist debugging.
1432 */
1433 f->f_stackdepth = -1;
1434 f->f_state = FRAME_EXECUTING;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001435
Inada Naoki91234a12019-06-03 21:30:58 +09001436 if (co->co_opcache_flag < OPCACHE_MIN_RUNS) {
1437 co->co_opcache_flag++;
1438 if (co->co_opcache_flag == OPCACHE_MIN_RUNS) {
1439 if (_PyCode_InitOpcache(co) < 0) {
Victor Stinner25104942020-04-24 02:43:18 +02001440 goto exit_eval_frame;
Inada Naoki91234a12019-06-03 21:30:58 +09001441 }
1442#if OPCACHE_STATS
1443 opcache_code_objects_extra_mem +=
1444 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1445 sizeof(_PyOpcache) * co->co_opcache_size;
1446 opcache_code_objects++;
1447#endif
1448 }
1449 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001450
Tim Peters5ca576e2001-06-18 22:08:13 +00001451#ifdef LLTRACE
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001452 {
1453 int r = _PyDict_ContainsId(f->f_globals, &PyId___ltrace__);
1454 if (r < 0) {
1455 goto exit_eval_frame;
1456 }
1457 lltrace = r;
1458 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001459#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001460
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001461 if (throwflag) { /* support for generator.throw() */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001462 goto error;
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001463 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001464
Victor Stinnerace47d72013-07-18 01:41:08 +02001465#ifdef Py_DEBUG
Victor Stinner0b72b232020-03-12 23:18:39 +01001466 /* _PyEval_EvalFrameDefault() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001467 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001468 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001469 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001470#endif
1471
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001472main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001474 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1475 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001476 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001478 /* Do periodic things. Doing this every time through
1479 the loop would add too much overhead, so we do it
1480 only every Nth instruction. We also do it if
Chris Jerdonek4a12d122020-05-14 19:25:45 -07001481 ``pending.calls_to_do'' is set, i.e. when an asynchronous
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482 event needs attention (e.g. a signal handler or
1483 async I/O handler); see Py_AddPendingCall() and
1484 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001485
Eric Snow7bda9de2019-03-08 17:25:54 -07001486 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001487 opcode = _Py_OPCODE(*next_instr);
1488 if (opcode == SETUP_FINALLY ||
1489 opcode == SETUP_WITH ||
1490 opcode == BEFORE_ASYNC_WITH ||
1491 opcode == YIELD_FROM) {
1492 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001493 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001494 - If we're about to enter the 'with:'. It will prevent
1495 emitting a resource warning in the common idiom
1496 'with open(path) as file:'.
1497 - If we're about to enter the 'async with:'.
1498 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001499 *very* useful, but might help in some cases and it's
1500 traditional)
1501 - If we're resuming a chain of nested 'yield from' or
1502 'await' calls, then each frame is parked with YIELD_FROM
1503 as its next opcode. If the user hit control-C we want to
1504 wait until we've reached the innermost frame before
1505 running the signal handler and raising KeyboardInterrupt
1506 (see bpo-30039).
1507 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001508 goto fast_next_opcode;
1509 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001510
Victor Stinnerda2914d2020-03-20 09:29:08 +01001511 if (eval_frame_handle_pending(tstate) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001512 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001513 }
1514 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001516 fast_next_opcode:
1517 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001518
Łukasz Langaa785c872016-09-09 17:37:37 -07001519 if (PyDTrace_LINE_ENABLED())
Mark Shannon877df852020-11-12 09:43:29 +00001520 maybe_dtrace_line(f, &bounds, &instr_prev);
Łukasz Langaa785c872016-09-09 17:37:37 -07001521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001522 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001523
Victor Stinnerdab84232020-03-17 18:56:44 +01001524 if (_Py_TracingPossible(ceval2) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001525 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001526 int err;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02001527 /* see maybe_call_line_trace()
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528 for expository comments */
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02001529 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001531 err = maybe_call_line_trace(tstate->c_tracefunc,
1532 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001533 tstate, f,
Mark Shannon877df852020-11-12 09:43:29 +00001534 &bounds, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001535 /* Reload possibly changed frame fields */
1536 JUMPTO(f->f_lasti);
Mark Shannoncb9879b2020-07-17 11:44:23 +01001537 stack_pointer = f->f_valuestack+f->f_stackdepth;
1538 f->f_stackdepth = -1;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001539 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001540 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001541 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001542 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001543
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001544 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001545
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001546 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001547 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001548#ifdef DYNAMIC_EXECUTION_PROFILE
1549#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001550 dxpairs[lastopcode][opcode]++;
1551 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001552#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001553 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001554#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001555
Guido van Rossum96a42c81992-01-12 02:29:51 +00001556#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001557 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001558
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001559 if (lltrace) {
1560 if (HAS_ARG(opcode)) {
1561 printf("%d: %d, %d\n",
1562 f->f_lasti, opcode, oparg);
1563 }
1564 else {
1565 printf("%d: %d\n",
1566 f->f_lasti, opcode);
1567 }
1568 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001569#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001571 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001573 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001574 It is essential that any operation that fails must goto error
1575 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001576
Benjamin Petersonddd19492018-09-16 22:38:02 -07001577 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001578 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001579 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001580
Benjamin Petersonddd19492018-09-16 22:38:02 -07001581 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001582 PyObject *value = GETLOCAL(oparg);
1583 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001584 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001585 UNBOUNDLOCAL_ERROR_MSG,
1586 PyTuple_GetItem(co->co_varnames, oparg));
1587 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001589 Py_INCREF(value);
1590 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001592 }
1593
Benjamin Petersonddd19492018-09-16 22:38:02 -07001594 case TARGET(LOAD_CONST): {
1595 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001596 PyObject *value = GETITEM(consts, oparg);
1597 Py_INCREF(value);
1598 PUSH(value);
1599 FAST_DISPATCH();
1600 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001601
Benjamin Petersonddd19492018-09-16 22:38:02 -07001602 case TARGET(STORE_FAST): {
1603 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001604 PyObject *value = POP();
1605 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001606 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001607 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001608
Benjamin Petersonddd19492018-09-16 22:38:02 -07001609 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001610 PyObject *value = POP();
1611 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001613 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001614
Benjamin Petersonddd19492018-09-16 22:38:02 -07001615 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001616 PyObject *top = TOP();
1617 PyObject *second = SECOND();
1618 SET_TOP(second);
1619 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001620 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001621 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001622
Benjamin Petersonddd19492018-09-16 22:38:02 -07001623 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001624 PyObject *top = TOP();
1625 PyObject *second = SECOND();
1626 PyObject *third = THIRD();
1627 SET_TOP(second);
1628 SET_SECOND(third);
1629 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001630 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001631 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001632
Benjamin Petersonddd19492018-09-16 22:38:02 -07001633 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001634 PyObject *top = TOP();
1635 PyObject *second = SECOND();
1636 PyObject *third = THIRD();
1637 PyObject *fourth = FOURTH();
1638 SET_TOP(second);
1639 SET_SECOND(third);
1640 SET_THIRD(fourth);
1641 SET_FOURTH(top);
1642 FAST_DISPATCH();
1643 }
1644
Benjamin Petersonddd19492018-09-16 22:38:02 -07001645 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001646 PyObject *top = TOP();
1647 Py_INCREF(top);
1648 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001650 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001651
Benjamin Petersonddd19492018-09-16 22:38:02 -07001652 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001653 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001654 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001655 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001656 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001657 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001658 SET_TOP(top);
1659 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001660 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001661 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001662
Benjamin Petersonddd19492018-09-16 22:38:02 -07001663 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001664 PyObject *value = TOP();
1665 PyObject *res = PyNumber_Positive(value);
1666 Py_DECREF(value);
1667 SET_TOP(res);
1668 if (res == NULL)
1669 goto error;
1670 DISPATCH();
1671 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001672
Benjamin Petersonddd19492018-09-16 22:38:02 -07001673 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001674 PyObject *value = TOP();
1675 PyObject *res = PyNumber_Negative(value);
1676 Py_DECREF(value);
1677 SET_TOP(res);
1678 if (res == NULL)
1679 goto error;
1680 DISPATCH();
1681 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001682
Benjamin Petersonddd19492018-09-16 22:38:02 -07001683 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001684 PyObject *value = TOP();
1685 int err = PyObject_IsTrue(value);
1686 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001687 if (err == 0) {
1688 Py_INCREF(Py_True);
1689 SET_TOP(Py_True);
1690 DISPATCH();
1691 }
1692 else if (err > 0) {
1693 Py_INCREF(Py_False);
1694 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001695 DISPATCH();
1696 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001697 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001698 goto error;
1699 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001700
Benjamin Petersonddd19492018-09-16 22:38:02 -07001701 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001702 PyObject *value = TOP();
1703 PyObject *res = PyNumber_Invert(value);
1704 Py_DECREF(value);
1705 SET_TOP(res);
1706 if (res == NULL)
1707 goto error;
1708 DISPATCH();
1709 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001710
Benjamin Petersonddd19492018-09-16 22:38:02 -07001711 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001712 PyObject *exp = POP();
1713 PyObject *base = TOP();
1714 PyObject *res = PyNumber_Power(base, exp, Py_None);
1715 Py_DECREF(base);
1716 Py_DECREF(exp);
1717 SET_TOP(res);
1718 if (res == NULL)
1719 goto error;
1720 DISPATCH();
1721 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001722
Benjamin Petersonddd19492018-09-16 22:38:02 -07001723 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001724 PyObject *right = POP();
1725 PyObject *left = TOP();
1726 PyObject *res = PyNumber_Multiply(left, right);
1727 Py_DECREF(left);
1728 Py_DECREF(right);
1729 SET_TOP(res);
1730 if (res == NULL)
1731 goto error;
1732 DISPATCH();
1733 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001734
Benjamin Petersonddd19492018-09-16 22:38:02 -07001735 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001736 PyObject *right = POP();
1737 PyObject *left = TOP();
1738 PyObject *res = PyNumber_MatrixMultiply(left, right);
1739 Py_DECREF(left);
1740 Py_DECREF(right);
1741 SET_TOP(res);
1742 if (res == NULL)
1743 goto error;
1744 DISPATCH();
1745 }
1746
Benjamin Petersonddd19492018-09-16 22:38:02 -07001747 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001748 PyObject *divisor = POP();
1749 PyObject *dividend = TOP();
1750 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1751 Py_DECREF(dividend);
1752 Py_DECREF(divisor);
1753 SET_TOP(quotient);
1754 if (quotient == NULL)
1755 goto error;
1756 DISPATCH();
1757 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001758
Benjamin Petersonddd19492018-09-16 22:38:02 -07001759 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001760 PyObject *divisor = POP();
1761 PyObject *dividend = TOP();
1762 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1763 Py_DECREF(dividend);
1764 Py_DECREF(divisor);
1765 SET_TOP(quotient);
1766 if (quotient == NULL)
1767 goto error;
1768 DISPATCH();
1769 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001770
Benjamin Petersonddd19492018-09-16 22:38:02 -07001771 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001772 PyObject *divisor = POP();
1773 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001774 PyObject *res;
1775 if (PyUnicode_CheckExact(dividend) && (
1776 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1777 // fast path; string formatting, but not if the RHS is a str subclass
1778 // (see issue28598)
1779 res = PyUnicode_Format(dividend, divisor);
1780 } else {
1781 res = PyNumber_Remainder(dividend, divisor);
1782 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001783 Py_DECREF(divisor);
1784 Py_DECREF(dividend);
1785 SET_TOP(res);
1786 if (res == NULL)
1787 goto error;
1788 DISPATCH();
1789 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001790
Benjamin Petersonddd19492018-09-16 22:38:02 -07001791 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001792 PyObject *right = POP();
1793 PyObject *left = TOP();
1794 PyObject *sum;
Victor Stinnerbd0a08e2020-10-01 18:57:37 +02001795 /* NOTE(vstinner): Please don't try to micro-optimize int+int on
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001796 CPython using bytecode, it is simply worthless.
1797 See http://bugs.python.org/issue21955 and
1798 http://bugs.python.org/issue10044 for the discussion. In short,
1799 no patch shown any impact on a realistic benchmark, only a minor
1800 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001801 if (PyUnicode_CheckExact(left) &&
1802 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001803 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001804 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001805 }
1806 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001807 sum = PyNumber_Add(left, right);
1808 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001809 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001810 Py_DECREF(right);
1811 SET_TOP(sum);
1812 if (sum == NULL)
1813 goto error;
1814 DISPATCH();
1815 }
1816
Benjamin Petersonddd19492018-09-16 22:38:02 -07001817 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001818 PyObject *right = POP();
1819 PyObject *left = TOP();
1820 PyObject *diff = PyNumber_Subtract(left, right);
1821 Py_DECREF(right);
1822 Py_DECREF(left);
1823 SET_TOP(diff);
1824 if (diff == NULL)
1825 goto error;
1826 DISPATCH();
1827 }
1828
Benjamin Petersonddd19492018-09-16 22:38:02 -07001829 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001830 PyObject *sub = POP();
1831 PyObject *container = TOP();
1832 PyObject *res = PyObject_GetItem(container, sub);
1833 Py_DECREF(container);
1834 Py_DECREF(sub);
1835 SET_TOP(res);
1836 if (res == NULL)
1837 goto error;
1838 DISPATCH();
1839 }
1840
Benjamin Petersonddd19492018-09-16 22:38:02 -07001841 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001842 PyObject *right = POP();
1843 PyObject *left = TOP();
1844 PyObject *res = PyNumber_Lshift(left, right);
1845 Py_DECREF(left);
1846 Py_DECREF(right);
1847 SET_TOP(res);
1848 if (res == NULL)
1849 goto error;
1850 DISPATCH();
1851 }
1852
Benjamin Petersonddd19492018-09-16 22:38:02 -07001853 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001854 PyObject *right = POP();
1855 PyObject *left = TOP();
1856 PyObject *res = PyNumber_Rshift(left, right);
1857 Py_DECREF(left);
1858 Py_DECREF(right);
1859 SET_TOP(res);
1860 if (res == NULL)
1861 goto error;
1862 DISPATCH();
1863 }
1864
Benjamin Petersonddd19492018-09-16 22:38:02 -07001865 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001866 PyObject *right = POP();
1867 PyObject *left = TOP();
1868 PyObject *res = PyNumber_And(left, right);
1869 Py_DECREF(left);
1870 Py_DECREF(right);
1871 SET_TOP(res);
1872 if (res == NULL)
1873 goto error;
1874 DISPATCH();
1875 }
1876
Benjamin Petersonddd19492018-09-16 22:38:02 -07001877 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001878 PyObject *right = POP();
1879 PyObject *left = TOP();
1880 PyObject *res = PyNumber_Xor(left, right);
1881 Py_DECREF(left);
1882 Py_DECREF(right);
1883 SET_TOP(res);
1884 if (res == NULL)
1885 goto error;
1886 DISPATCH();
1887 }
1888
Benjamin Petersonddd19492018-09-16 22:38:02 -07001889 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001890 PyObject *right = POP();
1891 PyObject *left = TOP();
1892 PyObject *res = PyNumber_Or(left, right);
1893 Py_DECREF(left);
1894 Py_DECREF(right);
1895 SET_TOP(res);
1896 if (res == NULL)
1897 goto error;
1898 DISPATCH();
1899 }
1900
Benjamin Petersonddd19492018-09-16 22:38:02 -07001901 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001902 PyObject *v = POP();
1903 PyObject *list = PEEK(oparg);
1904 int err;
1905 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001906 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001907 if (err != 0)
1908 goto error;
1909 PREDICT(JUMP_ABSOLUTE);
1910 DISPATCH();
1911 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001912
Benjamin Petersonddd19492018-09-16 22:38:02 -07001913 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001914 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001915 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001916 int err;
1917 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001918 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001919 if (err != 0)
1920 goto error;
1921 PREDICT(JUMP_ABSOLUTE);
1922 DISPATCH();
1923 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001924
Benjamin Petersonddd19492018-09-16 22:38:02 -07001925 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001926 PyObject *exp = POP();
1927 PyObject *base = TOP();
1928 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1929 Py_DECREF(base);
1930 Py_DECREF(exp);
1931 SET_TOP(res);
1932 if (res == NULL)
1933 goto error;
1934 DISPATCH();
1935 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001936
Benjamin Petersonddd19492018-09-16 22:38:02 -07001937 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001938 PyObject *right = POP();
1939 PyObject *left = TOP();
1940 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1941 Py_DECREF(left);
1942 Py_DECREF(right);
1943 SET_TOP(res);
1944 if (res == NULL)
1945 goto error;
1946 DISPATCH();
1947 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001948
Benjamin Petersonddd19492018-09-16 22:38:02 -07001949 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001950 PyObject *right = POP();
1951 PyObject *left = TOP();
1952 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1953 Py_DECREF(left);
1954 Py_DECREF(right);
1955 SET_TOP(res);
1956 if (res == NULL)
1957 goto error;
1958 DISPATCH();
1959 }
1960
Benjamin Petersonddd19492018-09-16 22:38:02 -07001961 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001962 PyObject *divisor = POP();
1963 PyObject *dividend = TOP();
1964 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1965 Py_DECREF(dividend);
1966 Py_DECREF(divisor);
1967 SET_TOP(quotient);
1968 if (quotient == NULL)
1969 goto error;
1970 DISPATCH();
1971 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001972
Benjamin Petersonddd19492018-09-16 22:38:02 -07001973 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001974 PyObject *divisor = POP();
1975 PyObject *dividend = TOP();
1976 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1977 Py_DECREF(dividend);
1978 Py_DECREF(divisor);
1979 SET_TOP(quotient);
1980 if (quotient == NULL)
1981 goto error;
1982 DISPATCH();
1983 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001984
Benjamin Petersonddd19492018-09-16 22:38:02 -07001985 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001986 PyObject *right = POP();
1987 PyObject *left = TOP();
1988 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1989 Py_DECREF(left);
1990 Py_DECREF(right);
1991 SET_TOP(mod);
1992 if (mod == NULL)
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(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001998 PyObject *right = POP();
1999 PyObject *left = TOP();
2000 PyObject *sum;
2001 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002002 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00002003 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002004 }
2005 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002006 sum = PyNumber_InPlaceAdd(left, right);
2007 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002008 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002009 Py_DECREF(right);
2010 SET_TOP(sum);
2011 if (sum == NULL)
2012 goto error;
2013 DISPATCH();
2014 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002015
Benjamin Petersonddd19492018-09-16 22:38:02 -07002016 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002017 PyObject *right = POP();
2018 PyObject *left = TOP();
2019 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
2020 Py_DECREF(left);
2021 Py_DECREF(right);
2022 SET_TOP(diff);
2023 if (diff == NULL)
2024 goto error;
2025 DISPATCH();
2026 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002027
Benjamin Petersonddd19492018-09-16 22:38:02 -07002028 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002029 PyObject *right = POP();
2030 PyObject *left = TOP();
2031 PyObject *res = PyNumber_InPlaceLshift(left, right);
2032 Py_DECREF(left);
2033 Py_DECREF(right);
2034 SET_TOP(res);
2035 if (res == NULL)
2036 goto error;
2037 DISPATCH();
2038 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002039
Benjamin Petersonddd19492018-09-16 22:38:02 -07002040 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002041 PyObject *right = POP();
2042 PyObject *left = TOP();
2043 PyObject *res = PyNumber_InPlaceRshift(left, right);
2044 Py_DECREF(left);
2045 Py_DECREF(right);
2046 SET_TOP(res);
2047 if (res == NULL)
2048 goto error;
2049 DISPATCH();
2050 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002051
Benjamin Petersonddd19492018-09-16 22:38:02 -07002052 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002053 PyObject *right = POP();
2054 PyObject *left = TOP();
2055 PyObject *res = PyNumber_InPlaceAnd(left, right);
2056 Py_DECREF(left);
2057 Py_DECREF(right);
2058 SET_TOP(res);
2059 if (res == NULL)
2060 goto error;
2061 DISPATCH();
2062 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002063
Benjamin Petersonddd19492018-09-16 22:38:02 -07002064 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002065 PyObject *right = POP();
2066 PyObject *left = TOP();
2067 PyObject *res = PyNumber_InPlaceXor(left, right);
2068 Py_DECREF(left);
2069 Py_DECREF(right);
2070 SET_TOP(res);
2071 if (res == NULL)
2072 goto error;
2073 DISPATCH();
2074 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002075
Benjamin Petersonddd19492018-09-16 22:38:02 -07002076 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002077 PyObject *right = POP();
2078 PyObject *left = TOP();
2079 PyObject *res = PyNumber_InPlaceOr(left, right);
2080 Py_DECREF(left);
2081 Py_DECREF(right);
2082 SET_TOP(res);
2083 if (res == NULL)
2084 goto error;
2085 DISPATCH();
2086 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002087
Benjamin Petersonddd19492018-09-16 22:38:02 -07002088 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002089 PyObject *sub = TOP();
2090 PyObject *container = SECOND();
2091 PyObject *v = THIRD();
2092 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002093 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00002094 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002095 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002096 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002097 Py_DECREF(container);
2098 Py_DECREF(sub);
2099 if (err != 0)
2100 goto error;
2101 DISPATCH();
2102 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002103
Benjamin Petersonddd19492018-09-16 22:38:02 -07002104 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002105 PyObject *sub = TOP();
2106 PyObject *container = SECOND();
2107 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002108 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00002109 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002110 err = PyObject_DelItem(container, sub);
2111 Py_DECREF(container);
2112 Py_DECREF(sub);
2113 if (err != 0)
2114 goto error;
2115 DISPATCH();
2116 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00002117
Benjamin Petersonddd19492018-09-16 22:38:02 -07002118 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01002119 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002120 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01002121 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002122 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002123 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002124 _PyErr_SetString(tstate, PyExc_RuntimeError,
2125 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002126 Py_DECREF(value);
2127 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002128 }
Petr Viktorinffd97532020-02-11 17:46:57 +01002129 res = PyObject_CallOneArg(hook, value);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002130 Py_DECREF(value);
2131 if (res == NULL)
2132 goto error;
2133 Py_DECREF(res);
2134 DISPATCH();
2135 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00002136
Benjamin Petersonddd19492018-09-16 22:38:02 -07002137 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002138 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002139 switch (oparg) {
2140 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002141 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02002142 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002143 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002144 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02002145 /* fall through */
2146 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02002147 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002148 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002149 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002150 break;
2151 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02002152 _PyErr_SetString(tstate, PyExc_SystemError,
2153 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002154 break;
2155 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002156 goto error;
2157 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002158
Benjamin Petersonddd19492018-09-16 22:38:02 -07002159 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002160 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002161 assert(f->f_iblock == 0);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002162 assert(EMPTY());
Mark Shannoncb9879b2020-07-17 11:44:23 +01002163 f->f_state = FRAME_RETURNED;
2164 f->f_stackdepth = 0;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002165 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002166 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002167
Benjamin Petersonddd19492018-09-16 22:38:02 -07002168 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002169 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002170 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002171 PyObject *obj = TOP();
2172 PyTypeObject *type = Py_TYPE(obj);
2173
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002174 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002175 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002176 }
Yury Selivanov75445082015-05-11 22:57:16 -04002177
2178 if (getter != NULL) {
2179 iter = (*getter)(obj);
2180 Py_DECREF(obj);
2181 if (iter == NULL) {
2182 SET_TOP(NULL);
2183 goto error;
2184 }
2185 }
2186 else {
2187 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002188 _PyErr_Format(tstate, PyExc_TypeError,
2189 "'async for' requires an object with "
2190 "__aiter__ method, got %.100s",
2191 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002192 Py_DECREF(obj);
2193 goto error;
2194 }
2195
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002196 if (Py_TYPE(iter)->tp_as_async == NULL ||
2197 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002198
Yury Selivanov398ff912017-03-02 22:20:00 -05002199 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002200 _PyErr_Format(tstate, PyExc_TypeError,
2201 "'async for' received an object from __aiter__ "
2202 "that does not implement __anext__: %.100s",
2203 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002204 Py_DECREF(iter);
2205 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002206 }
2207
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002208 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04002209 DISPATCH();
2210 }
2211
Benjamin Petersonddd19492018-09-16 22:38:02 -07002212 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002213 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002214 PyObject *next_iter = NULL;
2215 PyObject *awaitable = NULL;
2216 PyObject *aiter = TOP();
2217 PyTypeObject *type = Py_TYPE(aiter);
2218
Yury Selivanoveb636452016-09-08 22:01:51 -07002219 if (PyAsyncGen_CheckExact(aiter)) {
2220 awaitable = type->tp_as_async->am_anext(aiter);
2221 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002222 goto error;
2223 }
Yury Selivanoveb636452016-09-08 22:01:51 -07002224 } else {
2225 if (type->tp_as_async != NULL){
2226 getter = type->tp_as_async->am_anext;
2227 }
Yury Selivanov75445082015-05-11 22:57:16 -04002228
Yury Selivanoveb636452016-09-08 22:01:51 -07002229 if (getter != NULL) {
2230 next_iter = (*getter)(aiter);
2231 if (next_iter == NULL) {
2232 goto error;
2233 }
2234 }
2235 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02002236 _PyErr_Format(tstate, PyExc_TypeError,
2237 "'async for' requires an iterator with "
2238 "__anext__ method, got %.100s",
2239 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07002240 goto error;
2241 }
Yury Selivanov75445082015-05-11 22:57:16 -04002242
Yury Selivanoveb636452016-09-08 22:01:51 -07002243 awaitable = _PyCoro_GetAwaitableIter(next_iter);
2244 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05002245 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07002246 PyExc_TypeError,
2247 "'async for' received an invalid object "
2248 "from __anext__: %.100s",
2249 Py_TYPE(next_iter)->tp_name);
2250
2251 Py_DECREF(next_iter);
2252 goto error;
2253 } else {
2254 Py_DECREF(next_iter);
2255 }
2256 }
Yury Selivanov75445082015-05-11 22:57:16 -04002257
2258 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002259 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002260 DISPATCH();
2261 }
2262
Benjamin Petersonddd19492018-09-16 22:38:02 -07002263 case TARGET(GET_AWAITABLE): {
2264 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002265 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002266 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002267
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002268 if (iter == NULL) {
Mark Shannonfee55262019-11-21 09:11:43 +00002269 int opcode_at_minus_3 = 0;
2270 if ((next_instr - first_instr) > 2) {
2271 opcode_at_minus_3 = _Py_OPCODE(next_instr[-3]);
2272 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002273 format_awaitable_error(tstate, Py_TYPE(iterable),
Mark Shannonfee55262019-11-21 09:11:43 +00002274 opcode_at_minus_3,
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002275 _Py_OPCODE(next_instr[-2]));
2276 }
2277
Yury Selivanov75445082015-05-11 22:57:16 -04002278 Py_DECREF(iterable);
2279
Yury Selivanovc724bae2016-03-02 11:30:46 -05002280 if (iter != NULL && PyCoro_CheckExact(iter)) {
2281 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2282 if (yf != NULL) {
2283 /* `iter` is a coroutine object that is being
2284 awaited, `yf` is a pointer to the current awaitable
2285 being awaited on. */
2286 Py_DECREF(yf);
2287 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002288 _PyErr_SetString(tstate, PyExc_RuntimeError,
2289 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002290 /* The code below jumps to `error` if `iter` is NULL. */
2291 }
2292 }
2293
Yury Selivanov75445082015-05-11 22:57:16 -04002294 SET_TOP(iter); /* Even if it's NULL */
2295
2296 if (iter == NULL) {
2297 goto error;
2298 }
2299
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002300 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002301 DISPATCH();
2302 }
2303
Benjamin Petersonddd19492018-09-16 22:38:02 -07002304 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002305 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002306 PyObject *receiver = TOP();
Vladimir Matveev037245c2020-10-09 17:15:15 -07002307 PySendResult gen_status;
2308 if (tstate->c_tracefunc == NULL) {
2309 gen_status = PyIter_Send(receiver, v, &retval);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002310 } else {
Vladimir Matveev037245c2020-10-09 17:15:15 -07002311 _Py_IDENTIFIER(send);
2312 if (v == Py_None && PyIter_Check(receiver)) {
2313 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002314 }
2315 else {
Vladimir Matveev037245c2020-10-09 17:15:15 -07002316 retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002317 }
Vladimir Matveev2b053612020-09-18 18:38:38 -07002318 if (retval == NULL) {
2319 if (tstate->c_tracefunc != NULL
2320 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
2321 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
2322 if (_PyGen_FetchStopIterationValue(&retval) == 0) {
2323 gen_status = PYGEN_RETURN;
2324 }
2325 else {
2326 gen_status = PYGEN_ERROR;
2327 }
2328 }
2329 else {
2330 gen_status = PYGEN_NEXT;
2331 }
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002332 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002333 Py_DECREF(v);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002334 if (gen_status == PYGEN_ERROR) {
2335 assert (retval == NULL);
2336 goto error;
2337 }
2338 if (gen_status == PYGEN_RETURN) {
2339 assert (retval != NULL);
2340
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002341 Py_DECREF(receiver);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002342 SET_TOP(retval);
2343 retval = NULL;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002344 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002345 }
Vladimir Matveev2b053612020-09-18 18:38:38 -07002346 assert (gen_status == PYGEN_NEXT);
Martin Panter95f53c12016-07-18 08:23:26 +00002347 /* receiver remains on stack, retval is value to be yielded */
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002348 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002349 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002350 f->f_lasti -= sizeof(_Py_CODEUNIT);
Mark Shannoncb9879b2020-07-17 11:44:23 +01002351 f->f_state = FRAME_SUSPENDED;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02002352 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002353 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002354 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002355
Benjamin Petersonddd19492018-09-16 22:38:02 -07002356 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002357 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002358
2359 if (co->co_flags & CO_ASYNC_GENERATOR) {
2360 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2361 Py_DECREF(retval);
2362 if (w == NULL) {
2363 retval = NULL;
2364 goto error;
2365 }
2366 retval = w;
2367 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01002368 f->f_state = FRAME_SUSPENDED;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02002369 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002370 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002371 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002372
Benjamin Petersonddd19492018-09-16 22:38:02 -07002373 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002374 PyObject *type, *value, *traceback;
2375 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002376 PyTryBlock *b = PyFrame_BlockPop(f);
2377 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002378 _PyErr_SetString(tstate, PyExc_SystemError,
2379 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002380 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002381 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002382 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2383 STACK_LEVEL() <= (b)->b_level + 4);
2384 exc_info = tstate->exc_info;
2385 type = exc_info->exc_type;
2386 value = exc_info->exc_value;
2387 traceback = exc_info->exc_traceback;
2388 exc_info->exc_type = POP();
2389 exc_info->exc_value = POP();
2390 exc_info->exc_traceback = POP();
2391 Py_XDECREF(type);
2392 Py_XDECREF(value);
2393 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002394 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002395 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002396
Benjamin Petersonddd19492018-09-16 22:38:02 -07002397 case TARGET(POP_BLOCK): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002398 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002399 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002400 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002401
Mark Shannonfee55262019-11-21 09:11:43 +00002402 case TARGET(RERAISE): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002403 PyObject *exc = POP();
Mark Shannonfee55262019-11-21 09:11:43 +00002404 PyObject *val = POP();
2405 PyObject *tb = POP();
2406 assert(PyExceptionClass_Check(exc));
Victor Stinner61f4db82020-01-28 03:37:45 +01002407 _PyErr_Restore(tstate, exc, val, tb);
Mark Shannonfee55262019-11-21 09:11:43 +00002408 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002409 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002410
Benjamin Petersonddd19492018-09-16 22:38:02 -07002411 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002412 PyObject *exc = POP();
2413 assert(PyExceptionClass_Check(exc));
2414 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2415 PyTryBlock *b = PyFrame_BlockPop(f);
2416 assert(b->b_type == EXCEPT_HANDLER);
2417 Py_DECREF(exc);
2418 UNWIND_EXCEPT_HANDLER(b);
2419 Py_DECREF(POP());
2420 JUMPBY(oparg);
2421 FAST_DISPATCH();
2422 }
2423 else {
2424 PyObject *val = POP();
2425 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002426 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002427 goto exception_unwind;
2428 }
2429 }
2430
Zackery Spytzce6a0702019-08-25 03:44:09 -06002431 case TARGET(LOAD_ASSERTION_ERROR): {
2432 PyObject *value = PyExc_AssertionError;
2433 Py_INCREF(value);
2434 PUSH(value);
2435 FAST_DISPATCH();
2436 }
2437
Benjamin Petersonddd19492018-09-16 22:38:02 -07002438 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002439 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002440
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002441 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002442 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002443 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002444 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002445 if (!_PyErr_Occurred(tstate)) {
2446 _PyErr_SetString(tstate, PyExc_NameError,
2447 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002448 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002449 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002450 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002451 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002452 }
2453 else {
2454 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2455 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002456 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002457 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2458 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002459 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2460 _PyErr_SetString(tstate, PyExc_NameError,
2461 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002462 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002463 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002464 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002465 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002466 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002467 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002468
Benjamin Petersonddd19492018-09-16 22:38:02 -07002469 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002470 PyObject *name = GETITEM(names, oparg);
2471 PyObject *v = POP();
2472 PyObject *ns = f->f_locals;
2473 int err;
2474 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002475 _PyErr_Format(tstate, PyExc_SystemError,
2476 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002477 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002478 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002479 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002480 if (PyDict_CheckExact(ns))
2481 err = PyDict_SetItem(ns, name, v);
2482 else
2483 err = PyObject_SetItem(ns, name, v);
2484 Py_DECREF(v);
2485 if (err != 0)
2486 goto error;
2487 DISPATCH();
2488 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002489
Benjamin Petersonddd19492018-09-16 22:38:02 -07002490 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002491 PyObject *name = GETITEM(names, oparg);
2492 PyObject *ns = f->f_locals;
2493 int err;
2494 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002495 _PyErr_Format(tstate, PyExc_SystemError,
2496 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002497 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002498 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002499 err = PyObject_DelItem(ns, name);
2500 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002501 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002502 NAME_ERROR_MSG,
2503 name);
2504 goto error;
2505 }
2506 DISPATCH();
2507 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002508
Benjamin Petersonddd19492018-09-16 22:38:02 -07002509 case TARGET(UNPACK_SEQUENCE): {
2510 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002511 PyObject *seq = POP(), *item, **items;
2512 if (PyTuple_CheckExact(seq) &&
2513 PyTuple_GET_SIZE(seq) == oparg) {
2514 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002515 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002516 item = items[oparg];
2517 Py_INCREF(item);
2518 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002519 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002520 } else if (PyList_CheckExact(seq) &&
2521 PyList_GET_SIZE(seq) == oparg) {
2522 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002523 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002524 item = items[oparg];
2525 Py_INCREF(item);
2526 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002527 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002528 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002529 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002530 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002531 } else {
2532 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002533 Py_DECREF(seq);
2534 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002535 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002536 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002537 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002538 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002539
Benjamin Petersonddd19492018-09-16 22:38:02 -07002540 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002541 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2542 PyObject *seq = POP();
2543
Victor Stinner438a12d2019-05-24 17:01:38 +02002544 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002545 stack_pointer + totalargs)) {
2546 stack_pointer += totalargs;
2547 } else {
2548 Py_DECREF(seq);
2549 goto error;
2550 }
2551 Py_DECREF(seq);
2552 DISPATCH();
2553 }
2554
Benjamin Petersonddd19492018-09-16 22:38:02 -07002555 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002556 PyObject *name = GETITEM(names, oparg);
2557 PyObject *owner = TOP();
2558 PyObject *v = SECOND();
2559 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002560 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002561 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002562 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002563 Py_DECREF(owner);
2564 if (err != 0)
2565 goto error;
2566 DISPATCH();
2567 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002568
Benjamin Petersonddd19492018-09-16 22:38:02 -07002569 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002570 PyObject *name = GETITEM(names, oparg);
2571 PyObject *owner = POP();
2572 int err;
2573 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2574 Py_DECREF(owner);
2575 if (err != 0)
2576 goto error;
2577 DISPATCH();
2578 }
2579
Benjamin Petersonddd19492018-09-16 22:38:02 -07002580 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002581 PyObject *name = GETITEM(names, oparg);
2582 PyObject *v = POP();
2583 int err;
2584 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002585 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002586 if (err != 0)
2587 goto error;
2588 DISPATCH();
2589 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002590
Benjamin Petersonddd19492018-09-16 22:38:02 -07002591 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002592 PyObject *name = GETITEM(names, oparg);
2593 int err;
2594 err = PyDict_DelItem(f->f_globals, name);
2595 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002596 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2597 format_exc_check_arg(tstate, PyExc_NameError,
2598 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002599 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002600 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002601 }
2602 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002603 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002604
Benjamin Petersonddd19492018-09-16 22:38:02 -07002605 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002606 PyObject *name = GETITEM(names, oparg);
2607 PyObject *locals = f->f_locals;
2608 PyObject *v;
2609 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002610 _PyErr_Format(tstate, PyExc_SystemError,
2611 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002612 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002613 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002614 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002615 v = PyDict_GetItemWithError(locals, name);
2616 if (v != NULL) {
2617 Py_INCREF(v);
2618 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002619 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002620 goto error;
2621 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002622 }
2623 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002624 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002625 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002626 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002627 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002628 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002629 }
2630 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002631 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002632 v = PyDict_GetItemWithError(f->f_globals, name);
2633 if (v != NULL) {
2634 Py_INCREF(v);
2635 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002636 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002637 goto error;
2638 }
2639 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002640 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002641 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002642 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002643 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002644 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002645 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002646 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002647 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002648 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002649 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002650 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002651 }
2652 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002653 v = PyObject_GetItem(f->f_builtins, name);
2654 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002655 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002656 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002657 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002658 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002659 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002660 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002661 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002662 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002663 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002664 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002665 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002666 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002667 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002668
Benjamin Petersonddd19492018-09-16 22:38:02 -07002669 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002670 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002671 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002672 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002673 && PyDict_CheckExact(f->f_builtins))
2674 {
Inada Naoki91234a12019-06-03 21:30:58 +09002675 OPCACHE_CHECK();
2676 if (co_opcache != NULL && co_opcache->optimized > 0) {
2677 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2678
2679 if (lg->globals_ver ==
2680 ((PyDictObject *)f->f_globals)->ma_version_tag
2681 && lg->builtins_ver ==
2682 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2683 {
2684 PyObject *ptr = lg->ptr;
2685 OPCACHE_STAT_GLOBAL_HIT();
2686 assert(ptr != NULL);
2687 Py_INCREF(ptr);
2688 PUSH(ptr);
2689 DISPATCH();
2690 }
2691 }
2692
2693 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002694 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002695 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002696 name);
2697 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002698 if (!_PyErr_OCCURRED()) {
2699 /* _PyDict_LoadGlobal() returns NULL without raising
2700 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002701 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002702 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002703 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002704 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002705 }
Inada Naoki91234a12019-06-03 21:30:58 +09002706
2707 if (co_opcache != NULL) {
2708 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2709
2710 if (co_opcache->optimized == 0) {
2711 /* Wasn't optimized before. */
2712 OPCACHE_STAT_GLOBAL_OPT();
2713 } else {
2714 OPCACHE_STAT_GLOBAL_MISS();
2715 }
2716
2717 co_opcache->optimized = 1;
2718 lg->globals_ver =
2719 ((PyDictObject *)f->f_globals)->ma_version_tag;
2720 lg->builtins_ver =
2721 ((PyDictObject *)f->f_builtins)->ma_version_tag;
2722 lg->ptr = v; /* borrowed */
2723 }
2724
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002725 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002726 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002727 else {
2728 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002729
2730 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09002731 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002732 v = PyObject_GetItem(f->f_globals, name);
2733 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002734 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002735 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002736 }
2737 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002738
Victor Stinnerb4efc962015-11-20 09:24:02 +01002739 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002740 v = PyObject_GetItem(f->f_builtins, name);
2741 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002742 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002743 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002744 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002745 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002746 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002747 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002748 }
2749 }
2750 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002751 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002752 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002753 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002754
Benjamin Petersonddd19492018-09-16 22:38:02 -07002755 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002756 PyObject *v = GETLOCAL(oparg);
2757 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002758 SETLOCAL(oparg, NULL);
2759 DISPATCH();
2760 }
2761 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002762 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002763 UNBOUNDLOCAL_ERROR_MSG,
2764 PyTuple_GetItem(co->co_varnames, oparg)
2765 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002766 goto error;
2767 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002768
Benjamin Petersonddd19492018-09-16 22:38:02 -07002769 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002770 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002771 PyObject *oldobj = PyCell_GET(cell);
2772 if (oldobj != NULL) {
2773 PyCell_SET(cell, NULL);
2774 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002775 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002776 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002777 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002778 goto error;
2779 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002780
Benjamin Petersonddd19492018-09-16 22:38:02 -07002781 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002782 PyObject *cell = freevars[oparg];
2783 Py_INCREF(cell);
2784 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002785 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002786 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002787
Benjamin Petersonddd19492018-09-16 22:38:02 -07002788 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002789 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002790 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002791 assert(locals);
2792 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2793 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2794 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2795 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2796 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002797 value = PyDict_GetItemWithError(locals, name);
2798 if (value != NULL) {
2799 Py_INCREF(value);
2800 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002801 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002802 goto error;
2803 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002804 }
2805 else {
2806 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002807 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002808 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002809 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002810 }
2811 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002812 }
2813 }
2814 if (!value) {
2815 PyObject *cell = freevars[oparg];
2816 value = PyCell_GET(cell);
2817 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002818 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002819 goto error;
2820 }
2821 Py_INCREF(value);
2822 }
2823 PUSH(value);
2824 DISPATCH();
2825 }
2826
Benjamin Petersonddd19492018-09-16 22:38:02 -07002827 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002828 PyObject *cell = freevars[oparg];
2829 PyObject *value = PyCell_GET(cell);
2830 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002831 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002832 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002833 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002834 Py_INCREF(value);
2835 PUSH(value);
2836 DISPATCH();
2837 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002838
Benjamin Petersonddd19492018-09-16 22:38:02 -07002839 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002840 PyObject *v = POP();
2841 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002842 PyObject *oldobj = PyCell_GET(cell);
2843 PyCell_SET(cell, v);
2844 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002845 DISPATCH();
2846 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002847
Benjamin Petersonddd19492018-09-16 22:38:02 -07002848 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002849 PyObject *str;
2850 PyObject *empty = PyUnicode_New(0, 0);
2851 if (empty == NULL) {
2852 goto error;
2853 }
2854 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2855 Py_DECREF(empty);
2856 if (str == NULL)
2857 goto error;
2858 while (--oparg >= 0) {
2859 PyObject *item = POP();
2860 Py_DECREF(item);
2861 }
2862 PUSH(str);
2863 DISPATCH();
2864 }
2865
Benjamin Petersonddd19492018-09-16 22:38:02 -07002866 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002867 PyObject *tup = PyTuple_New(oparg);
2868 if (tup == NULL)
2869 goto error;
2870 while (--oparg >= 0) {
2871 PyObject *item = POP();
2872 PyTuple_SET_ITEM(tup, oparg, item);
2873 }
2874 PUSH(tup);
2875 DISPATCH();
2876 }
2877
Benjamin Petersonddd19492018-09-16 22:38:02 -07002878 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002879 PyObject *list = PyList_New(oparg);
2880 if (list == NULL)
2881 goto error;
2882 while (--oparg >= 0) {
2883 PyObject *item = POP();
2884 PyList_SET_ITEM(list, oparg, item);
2885 }
2886 PUSH(list);
2887 DISPATCH();
2888 }
2889
Mark Shannon13bc1392020-01-23 09:25:17 +00002890 case TARGET(LIST_TO_TUPLE): {
2891 PyObject *list = POP();
2892 PyObject *tuple = PyList_AsTuple(list);
2893 Py_DECREF(list);
2894 if (tuple == NULL) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002895 goto error;
Mark Shannon13bc1392020-01-23 09:25:17 +00002896 }
2897 PUSH(tuple);
2898 DISPATCH();
2899 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002900
Mark Shannon13bc1392020-01-23 09:25:17 +00002901 case TARGET(LIST_EXTEND): {
2902 PyObject *iterable = POP();
2903 PyObject *list = PEEK(oparg);
2904 PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
2905 if (none_val == NULL) {
2906 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01002907 (Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable)))
Mark Shannon13bc1392020-01-23 09:25:17 +00002908 {
Victor Stinner61f4db82020-01-28 03:37:45 +01002909 _PyErr_Clear(tstate);
Mark Shannon13bc1392020-01-23 09:25:17 +00002910 _PyErr_Format(tstate, PyExc_TypeError,
2911 "Value after * must be an iterable, not %.200s",
2912 Py_TYPE(iterable)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002913 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002914 Py_DECREF(iterable);
2915 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002916 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002917 Py_DECREF(none_val);
2918 Py_DECREF(iterable);
2919 DISPATCH();
2920 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002921
Mark Shannon13bc1392020-01-23 09:25:17 +00002922 case TARGET(SET_UPDATE): {
2923 PyObject *iterable = POP();
2924 PyObject *set = PEEK(oparg);
2925 int err = _PySet_Update(set, iterable);
2926 Py_DECREF(iterable);
2927 if (err < 0) {
2928 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002929 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002930 DISPATCH();
2931 }
2932
Benjamin Petersonddd19492018-09-16 22:38:02 -07002933 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002934 PyObject *set = PySet_New(NULL);
2935 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002936 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002937 if (set == NULL)
2938 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002939 for (i = oparg; i > 0; i--) {
2940 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002941 if (err == 0)
2942 err = PySet_Add(set, item);
2943 Py_DECREF(item);
2944 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002945 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002946 if (err != 0) {
2947 Py_DECREF(set);
2948 goto error;
2949 }
2950 PUSH(set);
2951 DISPATCH();
2952 }
2953
Benjamin Petersonddd19492018-09-16 22:38:02 -07002954 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002955 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002956 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2957 if (map == NULL)
2958 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002959 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002960 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002961 PyObject *key = PEEK(2*i);
2962 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002963 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002964 if (err != 0) {
2965 Py_DECREF(map);
2966 goto error;
2967 }
2968 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002969
2970 while (oparg--) {
2971 Py_DECREF(POP());
2972 Py_DECREF(POP());
2973 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002974 PUSH(map);
2975 DISPATCH();
2976 }
2977
Benjamin Petersonddd19492018-09-16 22:38:02 -07002978 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002979 _Py_IDENTIFIER(__annotations__);
2980 int err;
2981 PyObject *ann_dict;
2982 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002983 _PyErr_Format(tstate, PyExc_SystemError,
2984 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002985 goto error;
2986 }
2987 /* check if __annotations__ in locals()... */
2988 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002989 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002990 &PyId___annotations__);
2991 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002992 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002993 goto error;
2994 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002995 /* ...if not, create a new one */
2996 ann_dict = PyDict_New();
2997 if (ann_dict == NULL) {
2998 goto error;
2999 }
3000 err = _PyDict_SetItemId(f->f_locals,
3001 &PyId___annotations__, ann_dict);
3002 Py_DECREF(ann_dict);
3003 if (err != 0) {
3004 goto error;
3005 }
3006 }
3007 }
3008 else {
3009 /* do the same if locals() is not a dict */
3010 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
3011 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02003012 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003013 }
3014 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
3015 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003016 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003017 goto error;
3018 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003019 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003020 ann_dict = PyDict_New();
3021 if (ann_dict == NULL) {
3022 goto error;
3023 }
3024 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
3025 Py_DECREF(ann_dict);
3026 if (err != 0) {
3027 goto error;
3028 }
3029 }
3030 else {
3031 Py_DECREF(ann_dict);
3032 }
3033 }
3034 DISPATCH();
3035 }
3036
Benjamin Petersonddd19492018-09-16 22:38:02 -07003037 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02003038 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003039 PyObject *map;
3040 PyObject *keys = TOP();
3041 if (!PyTuple_CheckExact(keys) ||
3042 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003043 _PyErr_SetString(tstate, PyExc_SystemError,
3044 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003045 goto error;
3046 }
3047 map = _PyDict_NewPresized((Py_ssize_t)oparg);
3048 if (map == NULL) {
3049 goto error;
3050 }
3051 for (i = oparg; i > 0; i--) {
3052 int err;
3053 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
3054 PyObject *value = PEEK(i + 1);
3055 err = PyDict_SetItem(map, key, value);
3056 if (err != 0) {
3057 Py_DECREF(map);
3058 goto error;
3059 }
3060 }
3061
3062 Py_DECREF(POP());
3063 while (oparg--) {
3064 Py_DECREF(POP());
3065 }
3066 PUSH(map);
3067 DISPATCH();
3068 }
3069
Mark Shannon8a4cd702020-01-27 09:57:45 +00003070 case TARGET(DICT_UPDATE): {
3071 PyObject *update = POP();
3072 PyObject *dict = PEEK(oparg);
3073 if (PyDict_Update(dict, update) < 0) {
3074 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
3075 _PyErr_Format(tstate, PyExc_TypeError,
3076 "'%.200s' object is not a mapping",
Victor Stinnera102ed72020-02-07 02:24:48 +01003077 Py_TYPE(update)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003078 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003079 Py_DECREF(update);
3080 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003081 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003082 Py_DECREF(update);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003083 DISPATCH();
3084 }
3085
Mark Shannon8a4cd702020-01-27 09:57:45 +00003086 case TARGET(DICT_MERGE): {
3087 PyObject *update = POP();
3088 PyObject *dict = PEEK(oparg);
3089
3090 if (_PyDict_MergeEx(dict, update, 2) < 0) {
3091 format_kwargs_error(tstate, PEEK(2 + oparg), update);
3092 Py_DECREF(update);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003093 goto error;
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003094 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003095 Py_DECREF(update);
Brandt Bucherf185a732019-09-28 17:12:49 -07003096 PREDICT(CALL_FUNCTION_EX);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003097 DISPATCH();
3098 }
3099
Benjamin Petersonddd19492018-09-16 22:38:02 -07003100 case TARGET(MAP_ADD): {
Jörn Heisslerc8a35412019-06-22 16:40:55 +02003101 PyObject *value = TOP();
3102 PyObject *key = SECOND();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003103 PyObject *map;
3104 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00003105 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07003106 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003107 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00003108 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003109 Py_DECREF(value);
3110 Py_DECREF(key);
3111 if (err != 0)
3112 goto error;
3113 PREDICT(JUMP_ABSOLUTE);
3114 DISPATCH();
3115 }
3116
Benjamin Petersonddd19492018-09-16 22:38:02 -07003117 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003118 PyObject *name = GETITEM(names, oparg);
3119 PyObject *owner = TOP();
Pablo Galindo109826c2020-10-20 06:22:44 +01003120
3121 PyTypeObject *type = Py_TYPE(owner);
3122 PyObject *res;
3123 PyObject **dictptr;
3124 PyObject *dict;
3125 _PyOpCodeOpt_LoadAttr *la;
3126
3127 OPCACHE_STAT_ATTR_TOTAL();
3128
3129 OPCACHE_CHECK();
3130 if (co_opcache != NULL && PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG))
3131 {
3132 if (co_opcache->optimized > 0) {
3133 /* Fast path -- cache hit makes LOAD_ATTR ~30% faster */
3134 la = &co_opcache->u.la;
3135 if (la->type == type && la->tp_version_tag == type->tp_version_tag)
3136 {
3137 assert(type->tp_dict != NULL);
3138 assert(type->tp_dictoffset > 0);
3139
3140 dictptr = (PyObject **) ((char *)owner + type->tp_dictoffset);
3141 dict = *dictptr;
3142 if (dict != NULL && PyDict_CheckExact(dict)) {
3143 Py_ssize_t hint = la->hint;
3144 Py_INCREF(dict);
3145 res = NULL;
3146 la->hint = _PyDict_GetItemHint((PyDictObject*)dict, name, hint, &res);
3147
3148 if (res != NULL) {
3149 if (la->hint == hint && hint >= 0) {
3150 /* Our hint has helped -- cache hit. */
3151 OPCACHE_STAT_ATTR_HIT();
3152 } else {
3153 /* The hint we provided didn't work.
3154 Maybe next time? */
3155 OPCACHE_MAYBE_DEOPT_LOAD_ATTR();
3156 }
3157
3158 Py_INCREF(res);
3159 SET_TOP(res);
3160 Py_DECREF(owner);
3161 Py_DECREF(dict);
3162 DISPATCH();
3163 } else {
3164 // This attribute can be missing sometimes -- we
3165 // don't want to optimize this lookup.
3166 OPCACHE_DEOPT_LOAD_ATTR();
3167 Py_DECREF(dict);
3168 }
3169 } else {
3170 // There is no dict, or __dict__ doesn't satisfy PyDict_CheckExact
3171 OPCACHE_DEOPT_LOAD_ATTR();
3172 }
3173 } else {
3174 // The type of the object has either been updated,
3175 // or is different. Maybe it will stabilize?
3176 OPCACHE_MAYBE_DEOPT_LOAD_ATTR();
3177 }
3178
3179 OPCACHE_STAT_ATTR_MISS();
3180 }
3181
3182 if (co_opcache != NULL && /* co_opcache can be NULL after a DEOPT() call. */
3183 type->tp_getattro == PyObject_GenericGetAttr)
3184 {
Pablo Galindo109826c2020-10-20 06:22:44 +01003185 Py_ssize_t ret;
3186
3187 if (type->tp_dictoffset > 0) {
3188 if (type->tp_dict == NULL) {
3189 if (PyType_Ready(type) < 0) {
3190 Py_DECREF(owner);
3191 SET_TOP(NULL);
3192 goto error;
3193 }
3194 }
Pablo Galindo80449f22020-11-05 09:23:15 +00003195 if (_PyType_Lookup(type, name) == NULL) {
Pablo Galindo109826c2020-10-20 06:22:44 +01003196 dictptr = (PyObject **) ((char *)owner + type->tp_dictoffset);
3197 dict = *dictptr;
3198
3199 if (dict != NULL && PyDict_CheckExact(dict)) {
3200 Py_INCREF(dict);
3201 res = NULL;
3202 ret = _PyDict_GetItemHint((PyDictObject*)dict, name, -1, &res);
3203 if (res != NULL) {
3204 Py_INCREF(res);
3205 Py_DECREF(dict);
3206 Py_DECREF(owner);
3207 SET_TOP(res);
3208
3209 if (co_opcache->optimized == 0) {
3210 // First time we optimize this opcode. */
3211 OPCACHE_STAT_ATTR_OPT();
3212 co_opcache->optimized = OPCODE_CACHE_MAX_TRIES;
3213 }
3214
3215 la = &co_opcache->u.la;
3216 la->type = type;
3217 la->tp_version_tag = type->tp_version_tag;
3218 la->hint = ret;
3219
3220 DISPATCH();
3221 }
3222 Py_DECREF(dict);
3223 } else {
3224 // There is no dict, or __dict__ doesn't satisfy PyDict_CheckExact
3225 OPCACHE_DEOPT_LOAD_ATTR();
3226 }
3227 } else {
3228 // We failed to find an attribute without a data-like descriptor
3229 OPCACHE_DEOPT_LOAD_ATTR();
3230 }
3231 } else {
3232 // The object's class does not have a tp_dictoffset we can use
3233 OPCACHE_DEOPT_LOAD_ATTR();
3234 }
3235 } else if (type->tp_getattro != PyObject_GenericGetAttr) {
3236 OPCACHE_DEOPT_LOAD_ATTR();
3237 }
3238 }
3239
3240 /* slow path */
3241 res = PyObject_GetAttr(owner, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003242 Py_DECREF(owner);
3243 SET_TOP(res);
3244 if (res == NULL)
3245 goto error;
3246 DISPATCH();
3247 }
3248
Benjamin Petersonddd19492018-09-16 22:38:02 -07003249 case TARGET(COMPARE_OP): {
Mark Shannon9af0e472020-01-14 10:12:45 +00003250 assert(oparg <= Py_GE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003251 PyObject *right = POP();
3252 PyObject *left = TOP();
Mark Shannon9af0e472020-01-14 10:12:45 +00003253 PyObject *res = PyObject_RichCompare(left, right, oparg);
3254 SET_TOP(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003255 Py_DECREF(left);
3256 Py_DECREF(right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003257 if (res == NULL)
3258 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003259 PREDICT(POP_JUMP_IF_FALSE);
3260 PREDICT(POP_JUMP_IF_TRUE);
3261 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003262 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003263
Mark Shannon9af0e472020-01-14 10:12:45 +00003264 case TARGET(IS_OP): {
3265 PyObject *right = POP();
3266 PyObject *left = TOP();
3267 int res = (left == right)^oparg;
3268 PyObject *b = res ? Py_True : Py_False;
3269 Py_INCREF(b);
3270 SET_TOP(b);
3271 Py_DECREF(left);
3272 Py_DECREF(right);
3273 PREDICT(POP_JUMP_IF_FALSE);
3274 PREDICT(POP_JUMP_IF_TRUE);
3275 FAST_DISPATCH();
3276 }
3277
3278 case TARGET(CONTAINS_OP): {
3279 PyObject *right = POP();
3280 PyObject *left = POP();
3281 int res = PySequence_Contains(right, left);
3282 Py_DECREF(left);
3283 Py_DECREF(right);
3284 if (res < 0) {
3285 goto error;
3286 }
3287 PyObject *b = (res^oparg) ? Py_True : Py_False;
3288 Py_INCREF(b);
3289 PUSH(b);
3290 PREDICT(POP_JUMP_IF_FALSE);
3291 PREDICT(POP_JUMP_IF_TRUE);
3292 FAST_DISPATCH();
3293 }
3294
3295#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
3296 "BaseException is not allowed"
3297
3298 case TARGET(JUMP_IF_NOT_EXC_MATCH): {
3299 PyObject *right = POP();
3300 PyObject *left = POP();
3301 if (PyTuple_Check(right)) {
3302 Py_ssize_t i, length;
3303 length = PyTuple_GET_SIZE(right);
3304 for (i = 0; i < length; i++) {
3305 PyObject *exc = PyTuple_GET_ITEM(right, i);
3306 if (!PyExceptionClass_Check(exc)) {
3307 _PyErr_SetString(tstate, PyExc_TypeError,
3308 CANNOT_CATCH_MSG);
3309 Py_DECREF(left);
3310 Py_DECREF(right);
3311 goto error;
3312 }
3313 }
3314 }
3315 else {
3316 if (!PyExceptionClass_Check(right)) {
3317 _PyErr_SetString(tstate, PyExc_TypeError,
3318 CANNOT_CATCH_MSG);
3319 Py_DECREF(left);
3320 Py_DECREF(right);
3321 goto error;
3322 }
3323 }
3324 int res = PyErr_GivenExceptionMatches(left, right);
3325 Py_DECREF(left);
3326 Py_DECREF(right);
3327 if (res > 0) {
3328 /* Exception matches -- Do nothing */;
3329 }
3330 else if (res == 0) {
3331 JUMPTO(oparg);
3332 }
3333 else {
3334 goto error;
3335 }
3336 DISPATCH();
3337 }
3338
Benjamin Petersonddd19492018-09-16 22:38:02 -07003339 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003340 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003341 PyObject *fromlist = POP();
3342 PyObject *level = TOP();
3343 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003344 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003345 Py_DECREF(level);
3346 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003347 SET_TOP(res);
3348 if (res == NULL)
3349 goto error;
3350 DISPATCH();
3351 }
3352
Benjamin Petersonddd19492018-09-16 22:38:02 -07003353 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003354 PyObject *from = POP(), *locals;
3355 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003356 if (PyFrame_FastToLocalsWithError(f) < 0) {
3357 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01003358 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003359 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01003360
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003361 locals = f->f_locals;
3362 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003363 _PyErr_SetString(tstate, PyExc_SystemError,
3364 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003365 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003366 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003367 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003368 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003369 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003370 Py_DECREF(from);
3371 if (err != 0)
3372 goto error;
3373 DISPATCH();
3374 }
Guido van Rossum25831651993-05-19 14:50:45 +00003375
Benjamin Petersonddd19492018-09-16 22:38:02 -07003376 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003377 PyObject *name = GETITEM(names, oparg);
3378 PyObject *from = TOP();
3379 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003380 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003381 PUSH(res);
3382 if (res == NULL)
3383 goto error;
3384 DISPATCH();
3385 }
Thomas Wouters52152252000-08-17 22:55:00 +00003386
Benjamin Petersonddd19492018-09-16 22:38:02 -07003387 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003388 JUMPBY(oparg);
3389 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003390 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003391
Benjamin Petersonddd19492018-09-16 22:38:02 -07003392 case TARGET(POP_JUMP_IF_FALSE): {
3393 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003394 PyObject *cond = POP();
3395 int err;
3396 if (cond == Py_True) {
3397 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003398 FAST_DISPATCH();
3399 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003400 if (cond == Py_False) {
3401 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003402 JUMPTO(oparg);
3403 FAST_DISPATCH();
3404 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003405 err = PyObject_IsTrue(cond);
3406 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003407 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003408 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003409 else if (err == 0)
3410 JUMPTO(oparg);
3411 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003412 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003413 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003414 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003415
Benjamin Petersonddd19492018-09-16 22:38:02 -07003416 case TARGET(POP_JUMP_IF_TRUE): {
3417 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003418 PyObject *cond = POP();
3419 int err;
3420 if (cond == Py_False) {
3421 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003422 FAST_DISPATCH();
3423 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003424 if (cond == Py_True) {
3425 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003426 JUMPTO(oparg);
3427 FAST_DISPATCH();
3428 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003429 err = PyObject_IsTrue(cond);
3430 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003431 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003432 JUMPTO(oparg);
3433 }
3434 else if (err == 0)
3435 ;
3436 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003437 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003438 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003439 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003440
Benjamin Petersonddd19492018-09-16 22:38:02 -07003441 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003442 PyObject *cond = TOP();
3443 int err;
3444 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003445 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003446 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003447 FAST_DISPATCH();
3448 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003449 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003450 JUMPTO(oparg);
3451 FAST_DISPATCH();
3452 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003453 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003454 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003455 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003456 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003457 }
3458 else if (err == 0)
3459 JUMPTO(oparg);
3460 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003461 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003462 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003463 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003464
Benjamin Petersonddd19492018-09-16 22:38:02 -07003465 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003466 PyObject *cond = TOP();
3467 int err;
3468 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003469 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003470 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003471 FAST_DISPATCH();
3472 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003473 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003474 JUMPTO(oparg);
3475 FAST_DISPATCH();
3476 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003477 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003478 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003479 JUMPTO(oparg);
3480 }
3481 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003482 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003483 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003484 }
3485 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003486 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003487 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003488 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003489
Benjamin Petersonddd19492018-09-16 22:38:02 -07003490 case TARGET(JUMP_ABSOLUTE): {
3491 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003492 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003493#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003494 /* Enabling this path speeds-up all while and for-loops by bypassing
3495 the per-loop checks for signals. By default, this should be turned-off
3496 because it prevents detection of a control-break in tight loops like
3497 "while 1: pass". Compile with this option turned-on when you need
3498 the speed-up and do not need break checking inside tight loops (ones
3499 that contain only instructions ending with FAST_DISPATCH).
3500 */
3501 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003502#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003503 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003504#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003505 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003506
Benjamin Petersonddd19492018-09-16 22:38:02 -07003507 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003508 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003509 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003510 PyObject *iter = PyObject_GetIter(iterable);
3511 Py_DECREF(iterable);
3512 SET_TOP(iter);
3513 if (iter == NULL)
3514 goto error;
3515 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003516 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003517 DISPATCH();
3518 }
3519
Benjamin Petersonddd19492018-09-16 22:38:02 -07003520 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04003521 /* before: [obj]; after [getiter(obj)] */
3522 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003523 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003524 if (PyCoro_CheckExact(iterable)) {
3525 /* `iterable` is a coroutine */
3526 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3527 /* and it is used in a 'yield from' expression of a
3528 regular generator. */
3529 Py_DECREF(iterable);
3530 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003531 _PyErr_SetString(tstate, PyExc_TypeError,
3532 "cannot 'yield from' a coroutine object "
3533 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04003534 goto error;
3535 }
3536 }
3537 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003538 /* `iterable` is not a generator. */
3539 iter = PyObject_GetIter(iterable);
3540 Py_DECREF(iterable);
3541 SET_TOP(iter);
3542 if (iter == NULL)
3543 goto error;
3544 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003545 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003546 DISPATCH();
3547 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003548
Benjamin Petersonddd19492018-09-16 22:38:02 -07003549 case TARGET(FOR_ITER): {
3550 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003551 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003552 PyObject *iter = TOP();
Victor Stinnera102ed72020-02-07 02:24:48 +01003553 PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003554 if (next != NULL) {
3555 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003556 PREDICT(STORE_FAST);
3557 PREDICT(UNPACK_SEQUENCE);
3558 DISPATCH();
3559 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003560 if (_PyErr_Occurred(tstate)) {
3561 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003562 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003563 }
3564 else if (tstate->c_tracefunc != NULL) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003565 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Victor Stinner438a12d2019-05-24 17:01:38 +02003566 }
3567 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003568 }
3569 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003570 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003571 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003572 JUMPBY(oparg);
3573 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003574 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003575
Benjamin Petersonddd19492018-09-16 22:38:02 -07003576 case TARGET(SETUP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003577 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003578 STACK_LEVEL());
3579 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003580 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003581
Benjamin Petersonddd19492018-09-16 22:38:02 -07003582 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003583 _Py_IDENTIFIER(__aenter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003584 _Py_IDENTIFIER(__aexit__);
Yury Selivanov75445082015-05-11 22:57:16 -04003585 PyObject *mgr = TOP();
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003586 PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04003587 PyObject *res;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003588 if (enter == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003589 goto error;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003590 }
3591 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__);
3592 if (exit == NULL) {
3593 Py_DECREF(enter);
3594 goto error;
3595 }
Yury Selivanov75445082015-05-11 22:57:16 -04003596 SET_TOP(exit);
Yury Selivanov75445082015-05-11 22:57:16 -04003597 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003598 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003599 Py_DECREF(enter);
3600 if (res == NULL)
3601 goto error;
3602 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003603 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003604 DISPATCH();
3605 }
3606
Benjamin Petersonddd19492018-09-16 22:38:02 -07003607 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003608 PyObject *res = POP();
3609 /* Setup the finally block before pushing the result
3610 of __aenter__ on the stack. */
3611 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3612 STACK_LEVEL());
3613 PUSH(res);
3614 DISPATCH();
3615 }
3616
Benjamin Petersonddd19492018-09-16 22:38:02 -07003617 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003618 _Py_IDENTIFIER(__enter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003619 _Py_IDENTIFIER(__exit__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003620 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003621 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003622 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003623 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08003624 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003625 }
3626 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003627 if (exit == NULL) {
3628 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003629 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003630 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003631 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003632 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003633 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003634 Py_DECREF(enter);
3635 if (res == NULL)
3636 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003637 /* Setup the finally block before pushing the result
3638 of __enter__ on the stack. */
3639 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3640 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003641
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003642 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003643 DISPATCH();
3644 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003645
Mark Shannonfee55262019-11-21 09:11:43 +00003646 case TARGET(WITH_EXCEPT_START): {
3647 /* At the top of the stack are 7 values:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003648 - (TOP, SECOND, THIRD) = exc_info()
Mark Shannonfee55262019-11-21 09:11:43 +00003649 - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER
3650 - SEVENTH: the context.__exit__ bound method
3651 We call SEVENTH(TOP, SECOND, THIRD).
3652 Then we push again the TOP exception and the __exit__
3653 return value.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003654 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003655 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003656 PyObject *exc, *val, *tb, *res;
3657
Victor Stinner842cfff2016-12-01 14:45:31 +01003658 exc = TOP();
Mark Shannonfee55262019-11-21 09:11:43 +00003659 val = SECOND();
3660 tb = THIRD();
3661 assert(exc != Py_None);
3662 assert(!PyLong_Check(exc));
3663 exit_func = PEEK(7);
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003664 PyObject *stack[4] = {NULL, exc, val, tb};
Petr Viktorinffd97532020-02-11 17:46:57 +01003665 res = PyObject_Vectorcall(exit_func, stack + 1,
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003666 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003667 if (res == NULL)
3668 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003669
Yury Selivanov75445082015-05-11 22:57:16 -04003670 PUSH(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003671 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003672 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003673
Benjamin Petersonddd19492018-09-16 22:38:02 -07003674 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10003675 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003676 PyObject *name = GETITEM(names, oparg);
3677 PyObject *obj = TOP();
3678 PyObject *meth = NULL;
3679
3680 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3681
Yury Selivanovf2392132016-12-13 19:03:51 -05003682 if (meth == NULL) {
3683 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003684 goto error;
3685 }
3686
3687 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003688 /* We can bypass temporary bound method object.
3689 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003690
INADA Naoki015bce62017-01-16 17:23:30 +09003691 meth | self | arg1 | ... | argN
3692 */
3693 SET_TOP(meth);
3694 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003695 }
3696 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003697 /* meth is not an unbound method (but a regular attr, or
3698 something was returned by a descriptor protocol). Set
3699 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003700 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003701
3702 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003703 */
INADA Naoki015bce62017-01-16 17:23:30 +09003704 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003705 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003706 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003707 }
3708 DISPATCH();
3709 }
3710
Benjamin Petersonddd19492018-09-16 22:38:02 -07003711 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003712 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003713 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003714
3715 sp = stack_pointer;
3716
INADA Naoki015bce62017-01-16 17:23:30 +09003717 meth = PEEK(oparg + 2);
3718 if (meth == NULL) {
3719 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3720 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003721
3722 Stack layout:
3723
INADA Naoki015bce62017-01-16 17:23:30 +09003724 ... | NULL | callable | arg1 | ... | argN
3725 ^- TOP()
3726 ^- (-oparg)
3727 ^- (-oparg-1)
3728 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003729
Ville Skyttä49b27342017-08-03 09:00:59 +03003730 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003731 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003732 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003733 res = call_function(tstate, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003734 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003735 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003736 }
3737 else {
3738 /* This is a method call. Stack layout:
3739
INADA Naoki015bce62017-01-16 17:23:30 +09003740 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003741 ^- TOP()
3742 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003743 ^- (-oparg-1)
3744 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003745
INADA Naoki015bce62017-01-16 17:23:30 +09003746 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003747 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003748 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003749 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003750 res = call_function(tstate, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003751 stack_pointer = sp;
3752 }
3753
3754 PUSH(res);
3755 if (res == NULL)
3756 goto error;
3757 DISPATCH();
3758 }
3759
Benjamin Petersonddd19492018-09-16 22:38:02 -07003760 case TARGET(CALL_FUNCTION): {
3761 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003762 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003763 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003764 res = call_function(tstate, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003765 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003766 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003767 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003768 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003769 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003770 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003771 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003772
Benjamin Petersonddd19492018-09-16 22:38:02 -07003773 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003774 PyObject **sp, *res, *names;
3775
3776 names = POP();
Jeroen Demeyer05677862019-08-16 12:41:27 +02003777 assert(PyTuple_Check(names));
3778 assert(PyTuple_GET_SIZE(names) <= oparg);
3779 /* We assume without checking that names contains only strings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003780 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003781 res = call_function(tstate, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003782 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003783 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003784 Py_DECREF(names);
3785
3786 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003787 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003788 }
3789 DISPATCH();
3790 }
3791
Benjamin Petersonddd19492018-09-16 22:38:02 -07003792 case TARGET(CALL_FUNCTION_EX): {
Brandt Bucherf185a732019-09-28 17:12:49 -07003793 PREDICTED(CALL_FUNCTION_EX);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003794 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003795 if (oparg & 0x01) {
3796 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003797 if (!PyDict_CheckExact(kwargs)) {
3798 PyObject *d = PyDict_New();
3799 if (d == NULL)
3800 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003801 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003802 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02003803 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003804 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003805 goto error;
3806 }
3807 Py_DECREF(kwargs);
3808 kwargs = d;
3809 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003810 assert(PyDict_CheckExact(kwargs));
3811 }
3812 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003813 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003814 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003815 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003816 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003817 goto error;
3818 }
3819 Py_SETREF(callargs, PySequence_Tuple(callargs));
3820 if (callargs == NULL) {
3821 goto error;
3822 }
3823 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003824 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003825
Victor Stinner09532fe2019-05-10 23:39:09 +02003826 result = do_call_core(tstate, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003827 Py_DECREF(func);
3828 Py_DECREF(callargs);
3829 Py_XDECREF(kwargs);
3830
3831 SET_TOP(result);
3832 if (result == NULL) {
3833 goto error;
3834 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003835 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003836 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003837
Benjamin Petersonddd19492018-09-16 22:38:02 -07003838 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003839 PyObject *qualname = POP();
3840 PyObject *codeobj = POP();
3841 PyFunctionObject *func = (PyFunctionObject *)
3842 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003843
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003844 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003845 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003846 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003847 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003848 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003849
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003850 if (oparg & 0x08) {
3851 assert(PyTuple_CheckExact(TOP()));
3852 func ->func_closure = POP();
3853 }
3854 if (oparg & 0x04) {
3855 assert(PyDict_CheckExact(TOP()));
3856 func->func_annotations = POP();
3857 }
3858 if (oparg & 0x02) {
3859 assert(PyDict_CheckExact(TOP()));
3860 func->func_kwdefaults = POP();
3861 }
3862 if (oparg & 0x01) {
3863 assert(PyTuple_CheckExact(TOP()));
3864 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003865 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003866
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003867 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003868 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003869 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003870
Benjamin Petersonddd19492018-09-16 22:38:02 -07003871 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003872 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003873 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003874 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003875 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003876 step = NULL;
3877 stop = POP();
3878 start = TOP();
3879 slice = PySlice_New(start, stop, step);
3880 Py_DECREF(start);
3881 Py_DECREF(stop);
3882 Py_XDECREF(step);
3883 SET_TOP(slice);
3884 if (slice == NULL)
3885 goto error;
3886 DISPATCH();
3887 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003888
Benjamin Petersonddd19492018-09-16 22:38:02 -07003889 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003890 /* Handles f-string value formatting. */
3891 PyObject *result;
3892 PyObject *fmt_spec;
3893 PyObject *value;
3894 PyObject *(*conv_fn)(PyObject *);
3895 int which_conversion = oparg & FVC_MASK;
3896 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3897
3898 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003899 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003900
3901 /* See if any conversion is specified. */
3902 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003903 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003904 case FVC_STR: conv_fn = PyObject_Str; break;
3905 case FVC_REPR: conv_fn = PyObject_Repr; break;
3906 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003907 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02003908 _PyErr_Format(tstate, PyExc_SystemError,
3909 "unexpected conversion flag %d",
3910 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003911 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003912 }
3913
3914 /* If there's a conversion function, call it and replace
3915 value with that result. Otherwise, just use value,
3916 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003917 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003918 result = conv_fn(value);
3919 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003920 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003921 Py_XDECREF(fmt_spec);
3922 goto error;
3923 }
3924 value = result;
3925 }
3926
3927 /* If value is a unicode object, and there's no fmt_spec,
3928 then we know the result of format(value) is value
3929 itself. In that case, skip calling format(). I plan to
3930 move this optimization in to PyObject_Format()
3931 itself. */
3932 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3933 /* Do nothing, just transfer ownership to result. */
3934 result = value;
3935 } else {
3936 /* Actually call format(). */
3937 result = PyObject_Format(value, fmt_spec);
3938 Py_DECREF(value);
3939 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003940 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003941 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003942 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003943 }
3944
Eric V. Smith135d5f42016-02-05 18:23:08 -05003945 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003946 DISPATCH();
3947 }
3948
Benjamin Petersonddd19492018-09-16 22:38:02 -07003949 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003950 int oldoparg = oparg;
3951 NEXTOPARG();
3952 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003953 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003954 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003955
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003956
Antoine Pitrou042b1282010-08-13 21:15:58 +00003957#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003958 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003959#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003960 default:
3961 fprintf(stderr,
3962 "XXX lineno: %d, opcode: %d\n",
3963 PyFrame_GetLineNumber(f),
3964 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02003965 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003966 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003967
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003968 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003969
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003970 /* This should never be reached. Every opcode should end with DISPATCH()
3971 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003972 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003973
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003974error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003975 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003976#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02003977 if (!_PyErr_Occurred(tstate)) {
3978 _PyErr_SetString(tstate, PyExc_SystemError,
3979 "error return without exception set");
3980 }
Victor Stinner365b6932013-07-12 00:11:58 +02003981#else
Victor Stinner438a12d2019-05-24 17:01:38 +02003982 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02003983#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003984
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003985 /* Log traceback info. */
3986 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003987
Mark Shannoncb9879b2020-07-17 11:44:23 +01003988 if (tstate->c_tracefunc != NULL) {
3989 /* Make sure state is set to FRAME_EXECUTING for tracing */
3990 assert(f->f_state == FRAME_EXECUTING);
3991 f->f_state = FRAME_UNWINDING;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003992 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3993 tstate, f);
Mark Shannoncb9879b2020-07-17 11:44:23 +01003994 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003995exception_unwind:
Mark Shannoncb9879b2020-07-17 11:44:23 +01003996 f->f_state = FRAME_UNWINDING;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003997 /* Unwind stacks if an exception occurred */
3998 while (f->f_iblock > 0) {
3999 /* Pop the current block. */
4000 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00004001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004002 if (b->b_type == EXCEPT_HANDLER) {
4003 UNWIND_EXCEPT_HANDLER(b);
4004 continue;
4005 }
4006 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004007 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004008 PyObject *exc, *val, *tb;
4009 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01004010 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004011 /* Beware, this invalidates all b->b_* fields */
4012 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01004013 PUSH(exc_info->exc_traceback);
4014 PUSH(exc_info->exc_value);
4015 if (exc_info->exc_type != NULL) {
4016 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004017 }
4018 else {
4019 Py_INCREF(Py_None);
4020 PUSH(Py_None);
4021 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004022 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004023 /* Make the raw exception data
4024 available to the handler,
4025 so a program can emulate the
4026 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004027 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02004028 if (tb != NULL)
4029 PyException_SetTraceback(val, tb);
4030 else
4031 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004032 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01004033 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004034 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01004035 exc_info->exc_value = val;
4036 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004037 if (tb == NULL)
4038 tb = Py_None;
4039 Py_INCREF(tb);
4040 PUSH(tb);
4041 PUSH(val);
4042 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004043 JUMPTO(handler);
Victor Stinnerdab84232020-03-17 18:56:44 +01004044 if (_Py_TracingPossible(ceval2)) {
Mark Shannon877df852020-11-12 09:43:29 +00004045 instr_prev = INT_MAX;
Mark Shannonfee55262019-11-21 09:11:43 +00004046 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004047 /* Resume normal execution */
Mark Shannoncb9879b2020-07-17 11:44:23 +01004048 f->f_state = FRAME_EXECUTING;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004049 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004050 }
4051 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00004052
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004053 /* End the loop as we still have an error */
4054 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004055 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00004056
Pablo Galindof00828a2019-05-09 16:52:02 +01004057 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02004058 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01004059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004060 /* Pop remaining stack entries. */
4061 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004062 PyObject *o = POP();
4063 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004064 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01004065 f->f_stackdepth = 0;
4066 f->f_state = FRAME_RAISED;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00004067exiting:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004068 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05004069 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004070 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
4071 tstate, f, PyTrace_RETURN, retval)) {
4072 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004073 }
4074 }
4075 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004076 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
4077 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004078 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004079 }
4080 }
4081 }
Guido van Rossuma4240131997-01-21 21:18:36 +00004082
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004083 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00004084exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07004085 if (PyDTrace_FUNCTION_RETURN_ENABLED())
4086 dtrace_function_return(f);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01004087 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004088 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00004089
Victor Stinner0b72b232020-03-12 23:18:39 +01004090 return _Py_CheckFunctionResult(tstate, NULL, retval, __func__);
Guido van Rossum374a9221991-04-04 10:40:29 +00004091}
4092
Benjamin Petersonb204a422011-06-05 22:04:07 -05004093static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004094format_missing(PyThreadState *tstate, const char *kind,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004095 PyCodeObject *co, PyObject *names, PyObject *qualname)
Benjamin Petersone109c702011-06-24 09:37:26 -05004096{
4097 int err;
4098 Py_ssize_t len = PyList_GET_SIZE(names);
4099 PyObject *name_str, *comma, *tail, *tmp;
4100
4101 assert(PyList_CheckExact(names));
4102 assert(len >= 1);
4103 /* Deal with the joys of natural language. */
4104 switch (len) {
4105 case 1:
4106 name_str = PyList_GET_ITEM(names, 0);
4107 Py_INCREF(name_str);
4108 break;
4109 case 2:
4110 name_str = PyUnicode_FromFormat("%U and %U",
4111 PyList_GET_ITEM(names, len - 2),
4112 PyList_GET_ITEM(names, len - 1));
4113 break;
4114 default:
4115 tail = PyUnicode_FromFormat(", %U, and %U",
4116 PyList_GET_ITEM(names, len - 2),
4117 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07004118 if (tail == NULL)
4119 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05004120 /* Chop off the last two objects in the list. This shouldn't actually
4121 fail, but we can't be too careful. */
4122 err = PyList_SetSlice(names, len - 2, len, NULL);
4123 if (err == -1) {
4124 Py_DECREF(tail);
4125 return;
4126 }
4127 /* Stitch everything up into a nice comma-separated list. */
4128 comma = PyUnicode_FromString(", ");
4129 if (comma == NULL) {
4130 Py_DECREF(tail);
4131 return;
4132 }
4133 tmp = PyUnicode_Join(comma, names);
4134 Py_DECREF(comma);
4135 if (tmp == NULL) {
4136 Py_DECREF(tail);
4137 return;
4138 }
4139 name_str = PyUnicode_Concat(tmp, tail);
4140 Py_DECREF(tmp);
4141 Py_DECREF(tail);
4142 break;
4143 }
4144 if (name_str == NULL)
4145 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02004146 _PyErr_Format(tstate, PyExc_TypeError,
4147 "%U() missing %i required %s argument%s: %U",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004148 qualname,
Victor Stinner438a12d2019-05-24 17:01:38 +02004149 len,
4150 kind,
4151 len == 1 ? "" : "s",
4152 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05004153 Py_DECREF(name_str);
4154}
4155
4156static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004157missing_arguments(PyThreadState *tstate, PyCodeObject *co,
4158 Py_ssize_t missing, Py_ssize_t defcount,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004159 PyObject **fastlocals, PyObject *qualname)
Benjamin Petersone109c702011-06-24 09:37:26 -05004160{
Victor Stinner74319ae2016-08-25 00:04:09 +02004161 Py_ssize_t i, j = 0;
4162 Py_ssize_t start, end;
4163 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05004164 const char *kind = positional ? "positional" : "keyword-only";
4165 PyObject *missing_names;
4166
4167 /* Compute the names of the arguments that are missing. */
4168 missing_names = PyList_New(missing);
4169 if (missing_names == NULL)
4170 return;
4171 if (positional) {
4172 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004173 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05004174 }
4175 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01004176 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05004177 end = start + co->co_kwonlyargcount;
4178 }
4179 for (i = start; i < end; i++) {
4180 if (GETLOCAL(i) == NULL) {
4181 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
4182 PyObject *name = PyObject_Repr(raw);
4183 if (name == NULL) {
4184 Py_DECREF(missing_names);
4185 return;
4186 }
4187 PyList_SET_ITEM(missing_names, j++, name);
4188 }
4189 }
4190 assert(j == missing);
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004191 format_missing(tstate, kind, co, missing_names, qualname);
Benjamin Petersone109c702011-06-24 09:37:26 -05004192 Py_DECREF(missing_names);
4193}
4194
4195static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004196too_many_positional(PyThreadState *tstate, PyCodeObject *co,
4197 Py_ssize_t given, Py_ssize_t defcount,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004198 PyObject **fastlocals, PyObject *qualname)
Benjamin Petersonb204a422011-06-05 22:04:07 -05004199{
4200 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02004201 Py_ssize_t kwonly_given = 0;
4202 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004203 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02004204 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004205
Benjamin Petersone109c702011-06-24 09:37:26 -05004206 assert((co->co_flags & CO_VARARGS) == 0);
4207 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01004208 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02004209 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004210 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02004211 }
4212 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004213 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01004214 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004215 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01004216 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004217 }
4218 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01004219 plural = (co_argcount != 1);
4220 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004221 }
4222 if (sig == NULL)
4223 return;
4224 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02004225 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
4226 kwonly_sig = PyUnicode_FromFormat(format,
4227 given != 1 ? "s" : "",
4228 kwonly_given,
4229 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004230 if (kwonly_sig == NULL) {
4231 Py_DECREF(sig);
4232 return;
4233 }
4234 }
4235 else {
4236 /* This will not fail. */
4237 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05004238 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004239 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004240 _PyErr_Format(tstate, PyExc_TypeError,
4241 "%U() takes %U positional argument%s but %zd%U %s given",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004242 qualname,
Victor Stinner438a12d2019-05-24 17:01:38 +02004243 sig,
4244 plural ? "s" : "",
4245 given,
4246 kwonly_sig,
4247 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004248 Py_DECREF(sig);
4249 Py_DECREF(kwonly_sig);
4250}
4251
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004252static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004253positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004254 Py_ssize_t kwcount, PyObject* const* kwnames,
4255 PyObject *qualname)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004256{
4257 int posonly_conflicts = 0;
4258 PyObject* posonly_names = PyList_New(0);
4259
4260 for(int k=0; k < co->co_posonlyargcount; k++){
4261 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
4262
4263 for (int k2=0; k2<kwcount; k2++){
4264 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
4265 PyObject* kwname = kwnames[k2];
4266 if (kwname == posonly_name){
4267 if(PyList_Append(posonly_names, kwname) != 0) {
4268 goto fail;
4269 }
4270 posonly_conflicts++;
4271 continue;
4272 }
4273
4274 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
4275
4276 if ( cmp > 0) {
4277 if(PyList_Append(posonly_names, kwname) != 0) {
4278 goto fail;
4279 }
4280 posonly_conflicts++;
4281 } else if (cmp < 0) {
4282 goto fail;
4283 }
4284
4285 }
4286 }
4287 if (posonly_conflicts) {
4288 PyObject* comma = PyUnicode_FromString(", ");
4289 if (comma == NULL) {
4290 goto fail;
4291 }
4292 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
4293 Py_DECREF(comma);
4294 if (error_names == NULL) {
4295 goto fail;
4296 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004297 _PyErr_Format(tstate, PyExc_TypeError,
4298 "%U() got some positional-only arguments passed"
4299 " as keyword arguments: '%U'",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004300 qualname, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004301 Py_DECREF(error_names);
4302 goto fail;
4303 }
4304
4305 Py_DECREF(posonly_names);
4306 return 0;
4307
4308fail:
4309 Py_XDECREF(posonly_names);
4310 return 1;
4311
4312}
4313
Guido van Rossumc2e20742006-02-27 22:32:47 +00004314/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02004315 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00004316 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00004317
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004318PyObject *
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004319_PyEval_EvalCode(PyThreadState *tstate,
4320 PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004321 PyObject *const *args, Py_ssize_t argcount,
4322 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03004323 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004324 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02004325 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004326 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00004327{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004328 assert(is_tstate_valid(tstate));
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004329
Victor Stinner232dda62020-06-04 15:19:02 +02004330 PyCodeObject *co = (PyCodeObject*)_co;
4331
4332 if (!name) {
4333 name = co->co_name;
4334 }
4335 assert(name != NULL);
4336 assert(PyUnicode_Check(name));
4337
4338 if (!qualname) {
4339 qualname = name;
4340 }
4341 assert(qualname != NULL);
4342 assert(PyUnicode_Check(qualname));
4343
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004344 PyObject *retval = NULL;
Pablo Galindocd74e662019-06-01 18:08:04 +01004345 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00004346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004347 if (globals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004348 _PyErr_SetString(tstate, PyExc_SystemError,
4349 "PyEval_EvalCodeEx: NULL globals");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004350 return NULL;
4351 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004352
Victor Stinnerc7020012016-08-16 23:40:29 +02004353 /* Create the frame */
Victor Stinner232dda62020-06-04 15:19:02 +02004354 PyFrameObject *f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02004355 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004356 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02004357 }
Victor Stinner232dda62020-06-04 15:19:02 +02004358 PyObject **fastlocals = f->f_localsplus;
4359 PyObject **freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00004360
Victor Stinnerc7020012016-08-16 23:40:29 +02004361 /* Create a dictionary for keyword parameters (**kwags) */
Victor Stinner232dda62020-06-04 15:19:02 +02004362 PyObject *kwdict;
4363 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004364 if (co->co_flags & CO_VARKEYWORDS) {
4365 kwdict = PyDict_New();
4366 if (kwdict == NULL)
4367 goto fail;
4368 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02004369 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004370 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02004371 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004372 SETLOCAL(i, kwdict);
4373 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004374 else {
4375 kwdict = NULL;
4376 }
4377
Pablo Galindocd74e662019-06-01 18:08:04 +01004378 /* Copy all positional arguments into local variables */
Victor Stinner232dda62020-06-04 15:19:02 +02004379 Py_ssize_t j, n;
Pablo Galindocd74e662019-06-01 18:08:04 +01004380 if (argcount > co->co_argcount) {
4381 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02004382 }
4383 else {
4384 n = argcount;
4385 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004386 for (j = 0; j < n; j++) {
Victor Stinner232dda62020-06-04 15:19:02 +02004387 PyObject *x = args[j];
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004388 Py_INCREF(x);
4389 SETLOCAL(j, x);
4390 }
4391
Victor Stinnerc7020012016-08-16 23:40:29 +02004392 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004393 if (co->co_flags & CO_VARARGS) {
Victor Stinner232dda62020-06-04 15:19:02 +02004394 PyObject *u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02004395 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004396 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004397 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004398 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004399 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004400
Serhiy Storchakab7281052016-09-12 00:52:40 +03004401 /* Handle keyword arguments passed as two strided arrays */
4402 kwcount *= kwstep;
4403 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004404 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03004405 PyObject *keyword = kwnames[i];
4406 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02004407 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004408
Benjamin Petersonb204a422011-06-05 22:04:07 -05004409 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004410 _PyErr_Format(tstate, PyExc_TypeError,
4411 "%U() keywords must be strings",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004412 qualname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004413 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004414 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004415
Benjamin Petersonb204a422011-06-05 22:04:07 -05004416 /* Speed hack: do raw pointer compares. As names are
4417 normally interned this should almost always hit. */
4418 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004419 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner232dda62020-06-04 15:19:02 +02004420 PyObject *varname = co_varnames[j];
4421 if (varname == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004422 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004423 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004424 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004425
Benjamin Petersonb204a422011-06-05 22:04:07 -05004426 /* Slow fallback, just in case */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004427 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner232dda62020-06-04 15:19:02 +02004428 PyObject *varname = co_varnames[j];
4429 int cmp = PyObject_RichCompareBool( keyword, varname, Py_EQ);
Victor Stinner6fea7f72016-08-22 23:17:30 +02004430 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004431 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004432 }
4433 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004434 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004435 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004436 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004437
Victor Stinner231d1f32017-01-11 02:12:06 +01004438 assert(j >= total_args);
4439 if (kwdict == NULL) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004440
Victor Stinner438a12d2019-05-24 17:01:38 +02004441 if (co->co_posonlyargcount
4442 && positional_only_passed_as_keyword(tstate, co,
Victor Stinner232dda62020-06-04 15:19:02 +02004443 kwcount, kwnames,
4444 qualname))
Victor Stinner438a12d2019-05-24 17:01:38 +02004445 {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004446 goto fail;
4447 }
4448
Victor Stinner438a12d2019-05-24 17:01:38 +02004449 _PyErr_Format(tstate, PyExc_TypeError,
4450 "%U() got an unexpected keyword argument '%S'",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004451 qualname, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004452 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004453 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004454
Christian Heimes0bd447f2013-07-20 14:48:10 +02004455 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4456 goto fail;
4457 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004458 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004459
Benjamin Petersonb204a422011-06-05 22:04:07 -05004460 kw_found:
4461 if (GETLOCAL(j) != NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004462 _PyErr_Format(tstate, PyExc_TypeError,
4463 "%U() got multiple values for argument '%S'",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004464 qualname, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004465 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004466 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004467 Py_INCREF(value);
4468 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004469 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004470
4471 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004472 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Victor Stinner232dda62020-06-04 15:19:02 +02004473 too_many_positional(tstate, co, argcount, defcount, fastlocals,
4474 qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004475 goto fail;
4476 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004477
4478 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004479 if (argcount < co->co_argcount) {
4480 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004481 Py_ssize_t missing = 0;
4482 for (i = argcount; i < m; i++) {
4483 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004484 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004485 }
4486 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004487 if (missing) {
Victor Stinner232dda62020-06-04 15:19:02 +02004488 missing_arguments(tstate, co, missing, defcount, fastlocals,
4489 qualname);
Benjamin Petersone109c702011-06-24 09:37:26 -05004490 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004491 }
4492 if (n > m)
4493 i = n - m;
4494 else
4495 i = 0;
4496 for (; i < defcount; i++) {
4497 if (GETLOCAL(m+i) == NULL) {
4498 PyObject *def = defs[i];
4499 Py_INCREF(def);
4500 SETLOCAL(m+i, def);
4501 }
4502 }
4503 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004504
4505 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004506 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004507 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004508 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004509 if (GETLOCAL(i) != NULL)
4510 continue;
Victor Stinner232dda62020-06-04 15:19:02 +02004511 PyObject *varname = PyTuple_GET_ITEM(co->co_varnames, i);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004512 if (kwdefs != NULL) {
Victor Stinner232dda62020-06-04 15:19:02 +02004513 PyObject *def = PyDict_GetItemWithError(kwdefs, varname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004514 if (def) {
4515 Py_INCREF(def);
4516 SETLOCAL(i, def);
4517 continue;
4518 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004519 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004520 goto fail;
4521 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004522 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004523 missing++;
4524 }
4525 if (missing) {
Victor Stinner232dda62020-06-04 15:19:02 +02004526 missing_arguments(tstate, co, missing, -1, fastlocals,
4527 qualname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004528 goto fail;
4529 }
4530 }
4531
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004532 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004533 vars into frame. */
4534 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004535 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004536 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004537 /* Possibly account for the cell variable being an argument. */
4538 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004539 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004540 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004541 /* Clear the local copy. */
4542 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004543 }
4544 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004545 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004546 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004547 if (c == NULL)
4548 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004549 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004550 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004551
4552 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004553 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4554 PyObject *o = PyTuple_GET_ITEM(closure, i);
4555 Py_INCREF(o);
4556 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004557 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004558
Yury Selivanoveb636452016-09-08 22:01:51 -07004559 /* Handle generator/coroutine/asynchronous generator */
4560 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004561 PyObject *gen;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004562 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004564 /* Don't need to keep the reference to f_back, it will be set
4565 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004566 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004568 /* Create a new generator that owns the ready to run frame
4569 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004570 if (is_coro) {
4571 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004572 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4573 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004574 } else {
4575 gen = PyGen_NewWithQualName(f, name, qualname);
4576 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004577 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004578 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004579 }
INADA Naoki9c157762016-12-26 18:52:46 +09004580
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004581 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004582
Yury Selivanov75445082015-05-11 22:57:16 -04004583 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004584 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004585
Victor Stinnerb9e68122019-11-14 12:20:46 +01004586 retval = _PyEval_EvalFrame(tstate, f, 0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004587
Thomas Woutersce272b62007-09-19 21:19:28 +00004588fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004590 /* decref'ing the frame can cause __del__ methods to get invoked,
4591 which can call back into Python. While we're done with the
4592 current Python frame (f), the associated C stack is still in use,
4593 so recursion_depth must be boosted for the duration.
4594 */
INADA Naoki5a625d02016-12-24 20:19:08 +09004595 if (Py_REFCNT(f) > 1) {
4596 Py_DECREF(f);
4597 _PyObject_GC_TRACK(f);
4598 }
4599 else {
4600 ++tstate->recursion_depth;
4601 Py_DECREF(f);
4602 --tstate->recursion_depth;
4603 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004604 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004605}
4606
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004607
4608PyObject *
4609_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
4610 PyObject *const *args, Py_ssize_t argcount,
4611 PyObject *const *kwnames, PyObject *const *kwargs,
4612 Py_ssize_t kwcount, int kwstep,
4613 PyObject *const *defs, Py_ssize_t defcount,
4614 PyObject *kwdefs, PyObject *closure,
4615 PyObject *name, PyObject *qualname)
4616{
4617 PyThreadState *tstate = _PyThreadState_GET();
4618 return _PyEval_EvalCode(tstate, _co, globals, locals,
4619 args, argcount,
4620 kwnames, kwargs,
4621 kwcount, kwstep,
4622 defs, defcount,
4623 kwdefs, closure,
4624 name, qualname);
4625}
4626
Victor Stinner40ee3012014-06-16 15:59:28 +02004627PyObject *
4628PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004629 PyObject *const *args, int argcount,
4630 PyObject *const *kws, int kwcount,
4631 PyObject *const *defs, int defcount,
4632 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004633{
4634 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004635 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004636 kws, kws != NULL ? kws + 1 : NULL,
4637 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004638 defs, defcount,
4639 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004640 NULL, NULL);
4641}
Tim Peters5ca576e2001-06-18 22:08:13 +00004642
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004643static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004644special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004645{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004646 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004647 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02004648 if (res == NULL && !_PyErr_Occurred(tstate)) {
Victor Stinner4804b5b2020-05-12 01:43:38 +02004649 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(id));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004650 return NULL;
4651 }
4652 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004653}
4654
4655
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004656/* Logic for the raise statement (too complicated for inlining).
4657 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004658static int
Victor Stinner09532fe2019-05-10 23:39:09 +02004659do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004660{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004661 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004662
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004663 if (exc == NULL) {
4664 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01004665 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004666 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004667 type = exc_info->exc_type;
4668 value = exc_info->exc_value;
4669 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004670 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004671 _PyErr_SetString(tstate, PyExc_RuntimeError,
4672 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004673 return 0;
4674 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004675 Py_XINCREF(type);
4676 Py_XINCREF(value);
4677 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02004678 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004679 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004680 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004682 /* We support the following forms of raise:
4683 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004684 raise <instance>
4685 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004686
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004687 if (PyExceptionClass_Check(exc)) {
4688 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004689 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004690 if (value == NULL)
4691 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004692 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004693 _PyErr_Format(tstate, PyExc_TypeError,
4694 "calling %R should have returned an instance of "
4695 "BaseException, not %R",
4696 type, Py_TYPE(value));
4697 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004698 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004699 }
4700 else if (PyExceptionInstance_Check(exc)) {
4701 value = exc;
4702 type = PyExceptionInstance_Class(exc);
4703 Py_INCREF(type);
4704 }
4705 else {
4706 /* Not something you can raise. You get an exception
4707 anyway, just not what you specified :-) */
4708 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02004709 _PyErr_SetString(tstate, PyExc_TypeError,
4710 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004711 goto raise_error;
4712 }
Collin Winter828f04a2007-08-31 00:04:24 +00004713
Serhiy Storchakac0191582016-09-27 11:37:10 +03004714 assert(type != NULL);
4715 assert(value != NULL);
4716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004717 if (cause) {
4718 PyObject *fixed_cause;
4719 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004720 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004721 if (fixed_cause == NULL)
4722 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004723 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004724 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004725 else if (PyExceptionInstance_Check(cause)) {
4726 fixed_cause = cause;
4727 }
4728 else if (cause == Py_None) {
4729 Py_DECREF(cause);
4730 fixed_cause = NULL;
4731 }
4732 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004733 _PyErr_SetString(tstate, PyExc_TypeError,
4734 "exception causes must derive from "
4735 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004736 goto raise_error;
4737 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004738 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004739 }
Collin Winter828f04a2007-08-31 00:04:24 +00004740
Victor Stinner438a12d2019-05-24 17:01:38 +02004741 _PyErr_SetObject(tstate, type, value);
Victor Stinner61f4db82020-01-28 03:37:45 +01004742 /* _PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004743 Py_DECREF(value);
4744 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004745 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004746
4747raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004748 Py_XDECREF(value);
4749 Py_XDECREF(type);
4750 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004751 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004752}
4753
Tim Petersd6d010b2001-06-21 02:49:55 +00004754/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004755 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004756
Guido van Rossum0368b722007-05-11 16:50:42 +00004757 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4758 with a variable target.
4759*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004760
Barry Warsawe42b18f1997-08-25 22:13:04 +00004761static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004762unpack_iterable(PyThreadState *tstate, PyObject *v,
4763 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004764{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004765 int i = 0, j = 0;
4766 Py_ssize_t ll = 0;
4767 PyObject *it; /* iter(v) */
4768 PyObject *w;
4769 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004771 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004773 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004774 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004775 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01004776 Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v))
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004777 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004778 _PyErr_Format(tstate, PyExc_TypeError,
4779 "cannot unpack non-iterable %.200s object",
Victor Stinnera102ed72020-02-07 02:24:48 +01004780 Py_TYPE(v)->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004781 }
4782 return 0;
4783 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004784
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004785 for (; i < argcnt; i++) {
4786 w = PyIter_Next(it);
4787 if (w == NULL) {
4788 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004789 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04004790 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004791 _PyErr_Format(tstate, PyExc_ValueError,
4792 "not enough values to unpack "
4793 "(expected %d, got %d)",
4794 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004795 }
4796 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004797 _PyErr_Format(tstate, PyExc_ValueError,
4798 "not enough values to unpack "
4799 "(expected at least %d, got %d)",
4800 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004801 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004802 }
4803 goto Error;
4804 }
4805 *--sp = w;
4806 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004808 if (argcntafter == -1) {
4809 /* We better have exhausted the iterator now. */
4810 w = PyIter_Next(it);
4811 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004812 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004813 goto Error;
4814 Py_DECREF(it);
4815 return 1;
4816 }
4817 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02004818 _PyErr_Format(tstate, PyExc_ValueError,
4819 "too many values to unpack (expected %d)",
4820 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004821 goto Error;
4822 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004824 l = PySequence_List(it);
4825 if (l == NULL)
4826 goto Error;
4827 *--sp = l;
4828 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004830 ll = PyList_GET_SIZE(l);
4831 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004832 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04004833 "not enough values to unpack (expected at least %d, got %zd)",
4834 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004835 goto Error;
4836 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004838 /* Pop the "after-variable" args off the list. */
4839 for (j = argcntafter; j > 0; j--, i++) {
4840 *--sp = PyList_GET_ITEM(l, ll - j);
4841 }
4842 /* Resize the list. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +01004843 Py_SET_SIZE(l, ll - argcntafter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004844 Py_DECREF(it);
4845 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004846
Tim Petersd6d010b2001-06-21 02:49:55 +00004847Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004848 for (; i > 0; i--, sp++)
4849 Py_DECREF(*sp);
4850 Py_XDECREF(it);
4851 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004852}
4853
4854
Guido van Rossum96a42c81992-01-12 02:29:51 +00004855#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004856static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004857prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004858{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004859 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02004860 if (PyObject_Print(v, stdout, 0) != 0) {
4861 /* Don't know what else to do */
4862 _PyErr_Clear(tstate);
4863 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004864 printf("\n");
4865 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004866}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004867#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004868
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004869static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004870call_exc_trace(Py_tracefunc func, PyObject *self,
4871 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004872{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004873 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004874 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004875 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004876 if (value == NULL) {
4877 value = Py_None;
4878 Py_INCREF(value);
4879 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004880 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01004881 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004882 arg = PyTuple_Pack(3, type, value, traceback);
4883 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004884 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004885 return;
4886 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004887 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004888 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02004889 if (err == 0) {
4890 _PyErr_Restore(tstate, type, value, orig_traceback);
4891 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004892 else {
4893 Py_XDECREF(type);
4894 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004895 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004896 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004897}
4898
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004899static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004900call_trace_protected(Py_tracefunc func, PyObject *obj,
4901 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004902 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004903{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004904 PyObject *type, *value, *traceback;
4905 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004906 _PyErr_Fetch(tstate, &type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004907 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004908 if (err == 0)
4909 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004910 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004911 return 0;
4912 }
4913 else {
4914 Py_XDECREF(type);
4915 Py_XDECREF(value);
4916 Py_XDECREF(traceback);
4917 return -1;
4918 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004919}
4920
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004921static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004922call_trace(Py_tracefunc func, PyObject *obj,
4923 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004924 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004925{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004926 int result;
4927 if (tstate->tracing)
4928 return 0;
4929 tstate->tracing++;
4930 tstate->use_tracing = 0;
4931 result = func(obj, frame, what, arg);
4932 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4933 || (tstate->c_profilefunc != NULL));
4934 tstate->tracing--;
4935 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004936}
4937
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004938PyObject *
4939_PyEval_CallTracing(PyObject *func, PyObject *args)
4940{
Victor Stinner50b48572018-11-01 01:51:40 +01004941 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004942 int save_tracing = tstate->tracing;
4943 int save_use_tracing = tstate->use_tracing;
4944 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004945
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004946 tstate->tracing = 0;
4947 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4948 || (tstate->c_profilefunc != NULL));
4949 result = PyObject_Call(func, args, NULL);
4950 tstate->tracing = save_tracing;
4951 tstate->use_tracing = save_use_tracing;
4952 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004953}
4954
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004955/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004956static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004957maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004958 PyThreadState *tstate, PyFrameObject *frame,
Mark Shannon877df852020-11-12 09:43:29 +00004959 PyCodeAddressRange *bounds, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004960{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004961 int result = 0;
4962 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004963
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004964 /* If the last instruction executed isn't in the current
4965 instruction window, reset the window.
4966 */
Mark Shannon877df852020-11-12 09:43:29 +00004967 line = _PyCode_CheckLineNumber(frame->f_lasti, bounds);
Nick Coghlan5a851672017-09-08 10:14:16 +10004968 /* If the last instruction falls at the start of a line or if it
4969 represents a jump backwards, update the frame's line number and
4970 then call the trace function if we're tracing source lines.
4971 */
Mark Shannon877df852020-11-12 09:43:29 +00004972 if ((line != frame->f_lineno || frame->f_lasti < *instr_prev)) {
4973 if (line != -1) {
4974 frame->f_lineno = line;
4975 if (frame->f_trace_lines) {
4976 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4977 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004978 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004979 }
George King20faa682017-10-18 17:44:22 -07004980 /* Always emit an opcode event if we're tracing all opcodes. */
4981 if (frame->f_trace_opcodes) {
4982 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4983 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004984 *instr_prev = frame->f_lasti;
4985 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004986}
4987
Victor Stinner309d7cc2020-03-13 16:39:12 +01004988int
4989_PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
4990{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004991 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01004992 /* The caller must hold the GIL */
4993 assert(PyGILState_Check());
4994
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004995 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01004996 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004997 PyThreadState *current_tstate = _PyThreadState_GET();
4998 if (_PySys_Audit(current_tstate, "sys.setprofile", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01004999 return -1;
5000 }
5001
5002 PyObject *profileobj = tstate->c_profileobj;
5003
5004 tstate->c_profilefunc = NULL;
5005 tstate->c_profileobj = NULL;
5006 /* Must make sure that tracing is not ignored if 'profileobj' is freed */
5007 tstate->use_tracing = tstate->c_tracefunc != NULL;
5008 Py_XDECREF(profileobj);
5009
5010 Py_XINCREF(arg);
5011 tstate->c_profileobj = arg;
5012 tstate->c_profilefunc = func;
5013
5014 /* Flag that tracing or profiling is turned on */
5015 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
5016 return 0;
5017}
5018
Fred Drake5755ce62001-06-27 19:19:46 +00005019void
5020PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00005021{
Victor Stinner309d7cc2020-03-13 16:39:12 +01005022 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01005023 if (_PyEval_SetProfile(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005024 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01005025 _PyErr_WriteUnraisableMsg("in PyEval_SetProfile", NULL);
5026 }
Victor Stinner309d7cc2020-03-13 16:39:12 +01005027}
5028
5029int
5030_PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
5031{
Victor Stinnerda2914d2020-03-20 09:29:08 +01005032 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01005033 /* The caller must hold the GIL */
5034 assert(PyGILState_Check());
5035
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005036 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01005037 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005038 PyThreadState *current_tstate = _PyThreadState_GET();
5039 if (_PySys_Audit(current_tstate, "sys.settrace", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01005040 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005041 }
5042
Victor Stinnerda2914d2020-03-20 09:29:08 +01005043 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinner309d7cc2020-03-13 16:39:12 +01005044 PyObject *traceobj = tstate->c_traceobj;
Victor Stinnerda2914d2020-03-20 09:29:08 +01005045 ceval2->tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
Victor Stinner309d7cc2020-03-13 16:39:12 +01005046
5047 tstate->c_tracefunc = NULL;
5048 tstate->c_traceobj = NULL;
5049 /* Must make sure that profiling is not ignored if 'traceobj' is freed */
5050 tstate->use_tracing = (tstate->c_profilefunc != NULL);
5051 Py_XDECREF(traceobj);
5052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005053 Py_XINCREF(arg);
Victor Stinner309d7cc2020-03-13 16:39:12 +01005054 tstate->c_traceobj = arg;
5055 tstate->c_tracefunc = func;
5056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005057 /* Flag that tracing or profiling is turned on */
Victor Stinner309d7cc2020-03-13 16:39:12 +01005058 tstate->use_tracing = ((func != NULL)
5059 || (tstate->c_profilefunc != NULL));
5060
5061 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +00005062}
5063
5064void
5065PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
5066{
Victor Stinner309d7cc2020-03-13 16:39:12 +01005067 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01005068 if (_PyEval_SetTrace(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005069 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01005070 _PyErr_WriteUnraisableMsg("in PyEval_SetTrace", NULL);
5071 }
Fred Draked0838392001-06-16 21:02:31 +00005072}
5073
Victor Stinner309d7cc2020-03-13 16:39:12 +01005074
Yury Selivanov75445082015-05-11 22:57:16 -04005075void
Victor Stinner838f2642019-06-13 22:41:23 +02005076_PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005077{
5078 assert(new_depth >= 0);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005079 tstate->coroutine_origin_tracking_depth = new_depth;
5080}
5081
5082int
5083_PyEval_GetCoroutineOriginTrackingDepth(void)
5084{
Victor Stinner50b48572018-11-01 01:51:40 +01005085 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005086 return tstate->coroutine_origin_tracking_depth;
5087}
5088
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005089int
Yury Selivanoveb636452016-09-08 22:01:51 -07005090_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
5091{
Victor Stinner50b48572018-11-01 01:51:40 +01005092 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07005093
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005094 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_firstiter", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005095 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005096 }
5097
Yury Selivanoveb636452016-09-08 22:01:51 -07005098 Py_XINCREF(firstiter);
5099 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005100 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07005101}
5102
5103PyObject *
5104_PyEval_GetAsyncGenFirstiter(void)
5105{
Victor Stinner50b48572018-11-01 01:51:40 +01005106 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07005107 return tstate->async_gen_firstiter;
5108}
5109
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005110int
Yury Selivanoveb636452016-09-08 22:01:51 -07005111_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
5112{
Victor Stinner50b48572018-11-01 01:51:40 +01005113 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07005114
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005115 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_finalizer", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005116 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005117 }
5118
Yury Selivanoveb636452016-09-08 22:01:51 -07005119 Py_XINCREF(finalizer);
5120 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005121 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07005122}
5123
5124PyObject *
5125_PyEval_GetAsyncGenFinalizer(void)
5126{
Victor Stinner50b48572018-11-01 01:51:40 +01005127 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07005128 return tstate->async_gen_finalizer;
5129}
5130
Victor Stinner438a12d2019-05-24 17:01:38 +02005131PyFrameObject *
5132PyEval_GetFrame(void)
5133{
5134 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005135 return tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02005136}
5137
Guido van Rossumb209a111997-04-29 18:18:01 +00005138PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005139PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00005140{
Victor Stinner438a12d2019-05-24 17:01:38 +02005141 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005142 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005143 if (current_frame == NULL)
Victor Stinner438a12d2019-05-24 17:01:38 +02005144 return tstate->interp->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005145 else
5146 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00005147}
5148
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005149/* Convenience function to get a builtin from its name */
5150PyObject *
5151_PyEval_GetBuiltinId(_Py_Identifier *name)
5152{
Victor Stinner438a12d2019-05-24 17:01:38 +02005153 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005154 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
5155 if (attr) {
5156 Py_INCREF(attr);
5157 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005158 else if (!_PyErr_Occurred(tstate)) {
5159 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005160 }
5161 return attr;
5162}
5163
Guido van Rossumb209a111997-04-29 18:18:01 +00005164PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005165PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00005166{
Victor Stinner438a12d2019-05-24 17:01:38 +02005167 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005168 PyFrameObject *current_frame = tstate->frame;
Victor Stinner41bb43a2013-10-29 01:19:37 +01005169 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005170 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005171 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01005172 }
5173
Victor Stinner438a12d2019-05-24 17:01:38 +02005174 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01005175 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02005176 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01005177
5178 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005179 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00005180}
5181
Guido van Rossumb209a111997-04-29 18:18:01 +00005182PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005183PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00005184{
Victor Stinner438a12d2019-05-24 17:01:38 +02005185 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005186 PyFrameObject *current_frame = tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02005187 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005188 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02005189 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01005190
5191 assert(current_frame->f_globals != NULL);
5192 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00005193}
5194
Guido van Rossum6135a871995-01-09 17:53:26 +00005195int
Tim Peters5ba58662001-07-16 02:29:45 +00005196PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00005197{
Victor Stinner438a12d2019-05-24 17:01:38 +02005198 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005199 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005200 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00005201
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005202 if (current_frame != NULL) {
5203 const int codeflags = current_frame->f_code->co_flags;
5204 const int compilerflags = codeflags & PyCF_MASK;
5205 if (compilerflags) {
5206 result = 1;
5207 cf->cf_flags |= compilerflags;
5208 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00005209#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005210 if (codeflags & CO_GENERATOR_ALLOWED) {
5211 result = 1;
5212 cf->cf_flags |= CO_GENERATOR_ALLOWED;
5213 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00005214#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005215 }
5216 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00005217}
5218
Guido van Rossum3f5da241990-12-20 15:06:42 +00005219
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00005220const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00005221PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00005222{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005223 if (PyMethod_Check(func))
5224 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
5225 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02005226 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005227 else if (PyCFunction_Check(func))
5228 return ((PyCFunctionObject*)func)->m_ml->ml_name;
5229 else
Victor Stinnera102ed72020-02-07 02:24:48 +01005230 return Py_TYPE(func)->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00005231}
5232
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00005233const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00005234PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00005235{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005236 if (PyMethod_Check(func))
5237 return "()";
5238 else if (PyFunction_Check(func))
5239 return "()";
5240 else if (PyCFunction_Check(func))
5241 return "()";
5242 else
5243 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00005244}
5245
Armin Rigo1c2d7e52005-09-20 18:34:01 +00005246#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00005247if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005248 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
5249 tstate, tstate->frame, \
5250 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005251 x = NULL; \
5252 } \
5253 else { \
5254 x = call; \
5255 if (tstate->c_profilefunc != NULL) { \
5256 if (x == NULL) { \
5257 call_trace_protected(tstate->c_profilefunc, \
5258 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005259 tstate, tstate->frame, \
5260 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005261 /* XXX should pass (type, value, tb) */ \
5262 } else { \
5263 if (call_trace(tstate->c_profilefunc, \
5264 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005265 tstate, tstate->frame, \
5266 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005267 Py_DECREF(x); \
5268 x = NULL; \
5269 } \
5270 } \
5271 } \
5272 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00005273} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005274 x = call; \
5275 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00005276
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005277
5278static PyObject *
5279trace_call_function(PyThreadState *tstate,
5280 PyObject *func,
5281 PyObject **args, Py_ssize_t nargs,
5282 PyObject *kwnames)
5283{
5284 PyObject *x;
scoder4c9ea092020-05-12 16:12:41 +02005285 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Petr Viktorinffd97532020-02-11 17:46:57 +01005286 C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005287 return x;
5288 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005289 else if (Py_IS_TYPE(func, &PyMethodDescr_Type) && nargs > 0) {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005290 /* We need to create a temporary bound method as argument
5291 for profiling.
5292
5293 If nargs == 0, then this cannot work because we have no
5294 "self". In any case, the call itself would raise
5295 TypeError (foo needs an argument), so we just skip
5296 profiling. */
5297 PyObject *self = args[0];
5298 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5299 if (func == NULL) {
5300 return NULL;
5301 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005302 C_TRACE(x, PyObject_Vectorcall(func,
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02005303 args+1, nargs-1,
5304 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005305 Py_DECREF(func);
5306 return x;
5307 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005308 return PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005309}
5310
Victor Stinner415c5102017-01-11 00:54:57 +01005311/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
5312 to reduce the stack consumption. */
5313Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Victor Stinner09532fe2019-05-10 23:39:09 +02005314call_function(PyThreadState *tstate, PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005315{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005316 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005317 PyObject *func = *pfunc;
5318 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07005319 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
5320 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09005321 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005322
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005323 if (tstate->use_tracing) {
5324 x = trace_call_function(tstate, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09005325 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01005326 else {
Petr Viktorinffd97532020-02-11 17:46:57 +01005327 x = PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005328 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00005329
Victor Stinner438a12d2019-05-24 17:01:38 +02005330 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005331
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01005332 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005333 while ((*pp_stack) > pfunc) {
5334 w = EXT_POP(*pp_stack);
5335 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005336 }
Victor Stinnerace47d72013-07-18 01:41:08 +02005337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005338 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005339}
5340
Jeremy Hylton52820442001-01-03 23:52:36 +00005341static PyObject *
Victor Stinner09532fe2019-05-10 23:39:09 +02005342do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005343{
jdemeyere89de732018-09-19 12:06:20 +02005344 PyObject *result;
5345
scoder4c9ea092020-05-12 16:12:41 +02005346 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +02005347 C_TRACE(result, PyObject_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005348 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005349 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005350 else if (Py_IS_TYPE(func, &PyMethodDescr_Type)) {
jdemeyere89de732018-09-19 12:06:20 +02005351 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
5352 if (nargs > 0 && tstate->use_tracing) {
5353 /* We need to create a temporary bound method as argument
5354 for profiling.
5355
5356 If nargs == 0, then this cannot work because we have no
5357 "self". In any case, the call itself would raise
5358 TypeError (foo needs an argument), so we just skip
5359 profiling. */
5360 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
5361 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5362 if (func == NULL) {
5363 return NULL;
5364 }
5365
Victor Stinner4d231bc2019-11-14 13:36:21 +01005366 C_TRACE(result, _PyObject_FastCallDictTstate(
5367 tstate, func,
5368 &_PyTuple_ITEMS(callargs)[1],
5369 nargs - 1,
5370 kwdict));
jdemeyere89de732018-09-19 12:06:20 +02005371 Py_DECREF(func);
5372 return result;
5373 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005374 }
jdemeyere89de732018-09-19 12:06:20 +02005375 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00005376}
5377
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005378/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005379 nb_index slot defined, and store in *pi.
5380 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08005381 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00005382 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005383*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005384int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005385_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005386{
Victor Stinner438a12d2019-05-24 17:01:38 +02005387 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005388 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005389 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005390 if (_PyIndex_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005391 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005392 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005393 return 0;
5394 }
5395 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005396 _PyErr_SetString(tstate, PyExc_TypeError,
5397 "slice indices must be integers or "
5398 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005399 return 0;
5400 }
5401 *pi = x;
5402 }
5403 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005404}
5405
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005406int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005407_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005408{
Victor Stinner438a12d2019-05-24 17:01:38 +02005409 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005410 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005411 if (_PyIndex_Check(v)) {
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005412 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005413 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005414 return 0;
5415 }
5416 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005417 _PyErr_SetString(tstate, PyExc_TypeError,
5418 "slice indices must be integers or "
5419 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005420 return 0;
5421 }
5422 *pi = x;
5423 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005424}
5425
Thomas Wouters52152252000-08-17 22:55:00 +00005426static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005427import_name(PyThreadState *tstate, PyFrameObject *f,
5428 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005429{
5430 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005431 PyObject *import_func, *res;
5432 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005433
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005434 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005435 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005436 if (!_PyErr_Occurred(tstate)) {
5437 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005438 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005439 return NULL;
5440 }
5441
5442 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005443 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005444 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02005445 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005446 return NULL;
5447 }
5448 res = PyImport_ImportModuleLevelObject(
5449 name,
5450 f->f_globals,
5451 f->f_locals == NULL ? Py_None : f->f_locals,
5452 fromlist,
5453 ilevel);
5454 return res;
5455 }
5456
5457 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005458
5459 stack[0] = name;
5460 stack[1] = f->f_globals;
5461 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5462 stack[3] = fromlist;
5463 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005464 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005465 Py_DECREF(import_func);
5466 return res;
5467}
5468
5469static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005470import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005471{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005472 PyObject *x;
Xiang Zhang4830f582017-03-21 11:13:42 +08005473 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005474
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005475 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005476 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005477 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005478 /* Issue #17636: in case this failed because of a circular relative
5479 import, try to fallback on reading the module directly from
5480 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02005481 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005482 if (pkgname == NULL) {
5483 goto error;
5484 }
Oren Milman6db70332017-09-19 14:23:01 +03005485 if (!PyUnicode_Check(pkgname)) {
5486 Py_CLEAR(pkgname);
5487 goto error;
5488 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005489 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005490 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005491 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005492 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005493 }
Eric Snow3f9eee62017-09-15 16:35:20 -06005494 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005495 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02005496 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07005497 goto error;
5498 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005499 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005500 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005501 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005502 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005503 if (pkgname == NULL) {
5504 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5505 if (pkgname_or_unknown == NULL) {
5506 Py_XDECREF(pkgpath);
5507 return NULL;
5508 }
5509 } else {
5510 pkgname_or_unknown = pkgname;
5511 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005512
5513 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005514 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08005515 errmsg = PyUnicode_FromFormat(
5516 "cannot import name %R from %R (unknown location)",
5517 name, pkgname_or_unknown
5518 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005519 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005520 PyErr_SetImportError(errmsg, pkgname, NULL);
5521 }
5522 else {
Anthony Sottile65366bc2019-09-09 08:17:50 -07005523 _Py_IDENTIFIER(__spec__);
5524 PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
Anthony Sottile65366bc2019-09-09 08:17:50 -07005525 const char *fmt =
5526 _PyModuleSpec_IsInitializing(spec) ?
5527 "cannot import name %R from partially initialized module %R "
5528 "(most likely due to a circular import) (%S)" :
5529 "cannot import name %R from %R (%S)";
5530 Py_XDECREF(spec);
5531
5532 errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
Stefan Krah027b09c2019-03-25 21:50:58 +01005533 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005534 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005535 }
5536
Xiang Zhang4830f582017-03-21 11:13:42 +08005537 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005538 Py_XDECREF(pkgname_or_unknown);
5539 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005540 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005541}
Guido van Rossumac7be682001-01-17 15:42:30 +00005542
Thomas Wouters52152252000-08-17 22:55:00 +00005543static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005544import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00005545{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005546 _Py_IDENTIFIER(__all__);
5547 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005548 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005549 int skip_leading_underscores = 0;
5550 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005551
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005552 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5553 return -1; /* Unexpected error */
5554 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005555 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005556 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5557 return -1;
5558 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005559 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005560 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005561 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005562 return -1;
5563 }
5564 all = PyMapping_Keys(dict);
5565 Py_DECREF(dict);
5566 if (all == NULL)
5567 return -1;
5568 skip_leading_underscores = 1;
5569 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005571 for (pos = 0, err = 0; ; pos++) {
5572 name = PySequence_GetItem(all, pos);
5573 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005574 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005575 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02005576 }
5577 else {
5578 _PyErr_Clear(tstate);
5579 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005580 break;
5581 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005582 if (!PyUnicode_Check(name)) {
5583 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5584 if (modname == NULL) {
5585 Py_DECREF(name);
5586 err = -1;
5587 break;
5588 }
5589 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005590 _PyErr_Format(tstate, PyExc_TypeError,
5591 "module __name__ must be a string, not %.100s",
5592 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005593 }
5594 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005595 _PyErr_Format(tstate, PyExc_TypeError,
5596 "%s in %U.%s must be str, not %.100s",
5597 skip_leading_underscores ? "Key" : "Item",
5598 modname,
5599 skip_leading_underscores ? "__dict__" : "__all__",
5600 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005601 }
5602 Py_DECREF(modname);
5603 Py_DECREF(name);
5604 err = -1;
5605 break;
5606 }
5607 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005608 if (PyUnicode_READY(name) == -1) {
5609 Py_DECREF(name);
5610 err = -1;
5611 break;
5612 }
5613 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5614 Py_DECREF(name);
5615 continue;
5616 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005617 }
5618 value = PyObject_GetAttr(v, name);
5619 if (value == NULL)
5620 err = -1;
5621 else if (PyDict_CheckExact(locals))
5622 err = PyDict_SetItem(locals, name, value);
5623 else
5624 err = PyObject_SetItem(locals, name, value);
5625 Py_DECREF(name);
5626 Py_XDECREF(value);
5627 if (err != 0)
5628 break;
5629 }
5630 Py_DECREF(all);
5631 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005632}
5633
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005634static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005635check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005636{
Victor Stinnera102ed72020-02-07 02:24:48 +01005637 if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005638 /* check_args_iterable() may be called with a live exception:
5639 * clear it to prevent calling _PyObject_FunctionStr() with an
5640 * exception set. */
Victor Stinner61f4db82020-01-28 03:37:45 +01005641 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005642 PyObject *funcstr = _PyObject_FunctionStr(func);
5643 if (funcstr != NULL) {
5644 _PyErr_Format(tstate, PyExc_TypeError,
5645 "%U argument after * must be an iterable, not %.200s",
5646 funcstr, Py_TYPE(args)->tp_name);
5647 Py_DECREF(funcstr);
5648 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005649 return -1;
5650 }
5651 return 0;
5652}
5653
5654static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005655format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005656{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005657 /* _PyDict_MergeEx raises attribute
5658 * error (percolated from an attempt
5659 * to get 'keys' attribute) instead of
5660 * a type error if its second argument
5661 * is not a mapping.
5662 */
Victor Stinner438a12d2019-05-24 17:01:38 +02005663 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005664 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005665 PyObject *funcstr = _PyObject_FunctionStr(func);
5666 if (funcstr != NULL) {
5667 _PyErr_Format(
5668 tstate, PyExc_TypeError,
5669 "%U argument after ** must be a mapping, not %.200s",
5670 funcstr, Py_TYPE(kwargs)->tp_name);
5671 Py_DECREF(funcstr);
5672 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005673 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005674 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005675 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02005676 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005677 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005678 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005679 PyObject *funcstr = _PyObject_FunctionStr(func);
5680 if (funcstr != NULL) {
5681 PyObject *key = PyTuple_GET_ITEM(val, 0);
5682 _PyErr_Format(
5683 tstate, PyExc_TypeError,
5684 "%U got multiple values for keyword argument '%S'",
5685 funcstr, key);
5686 Py_DECREF(funcstr);
5687 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005688 Py_XDECREF(exc);
5689 Py_XDECREF(val);
5690 Py_XDECREF(tb);
5691 }
5692 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005693 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005694 }
5695 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005696}
5697
Guido van Rossumac7be682001-01-17 15:42:30 +00005698static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005699format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
5700 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005701{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005702 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005704 if (!obj)
5705 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005706
Serhiy Storchaka06515832016-11-20 09:13:07 +02005707 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005708 if (!obj_str)
5709 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005710
Victor Stinner438a12d2019-05-24 17:01:38 +02005711 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005712}
Guido van Rossum950361c1997-01-24 13:49:28 +00005713
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005714static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005715format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005716{
5717 PyObject *name;
5718 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02005719 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005720 return;
5721 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5722 name = PyTuple_GET_ITEM(co->co_cellvars,
5723 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005724 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005725 PyExc_UnboundLocalError,
5726 UNBOUNDLOCAL_ERROR_MSG,
5727 name);
5728 } else {
5729 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5730 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02005731 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005732 UNBOUNDFREE_ERROR_MSG, name);
5733 }
5734}
5735
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005736static void
Mark Shannonfee55262019-11-21 09:11:43 +00005737format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevprevopcode, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005738{
5739 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5740 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005741 _PyErr_Format(tstate, PyExc_TypeError,
5742 "'async with' received an object from __aenter__ "
5743 "that does not implement __await__: %.100s",
5744 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005745 }
Mark Shannonfee55262019-11-21 09:11:43 +00005746 else if (prevopcode == WITH_EXCEPT_START || (prevopcode == CALL_FUNCTION && prevprevopcode == DUP_TOP)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005747 _PyErr_Format(tstate, PyExc_TypeError,
5748 "'async with' received an object from __aexit__ "
5749 "that does not implement __await__: %.100s",
5750 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005751 }
5752 }
5753}
5754
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005755static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005756unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005757 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005758{
5759 PyObject *res;
5760 if (Py_REFCNT(v) == 2) {
5761 /* In the common case, there are 2 references to the value
5762 * stored in 'variable' when the += is performed: one on the
5763 * value stack (in 'v') and one still stored in the
5764 * 'variable'. We try to delete the variable now to reduce
5765 * the refcnt to 1.
5766 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005767 int opcode, oparg;
5768 NEXTOPARG();
5769 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005770 case STORE_FAST:
5771 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005772 PyObject **fastlocals = f->f_localsplus;
5773 if (GETLOCAL(oparg) == v)
5774 SETLOCAL(oparg, NULL);
5775 break;
5776 }
5777 case STORE_DEREF:
5778 {
5779 PyObject **freevars = (f->f_localsplus +
5780 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005781 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005782 if (PyCell_GET(c) == v) {
5783 PyCell_SET(c, NULL);
5784 Py_DECREF(v);
5785 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005786 break;
5787 }
5788 case STORE_NAME:
5789 {
5790 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005791 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005792 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005793 if (locals && PyDict_CheckExact(locals)) {
5794 PyObject *w = PyDict_GetItemWithError(locals, name);
5795 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02005796 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005797 {
5798 Py_DECREF(v);
5799 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005800 }
5801 }
5802 break;
5803 }
5804 }
5805 }
5806 res = v;
5807 PyUnicode_Append(&res, w);
5808 return res;
5809}
5810
Guido van Rossum950361c1997-01-24 13:49:28 +00005811#ifdef DYNAMIC_EXECUTION_PROFILE
5812
Skip Montanarof118cb12001-10-15 20:51:38 +00005813static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005814getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005815{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005816 int i;
5817 PyObject *l = PyList_New(256);
5818 if (l == NULL) return NULL;
5819 for (i = 0; i < 256; i++) {
5820 PyObject *x = PyLong_FromLong(a[i]);
5821 if (x == NULL) {
5822 Py_DECREF(l);
5823 return NULL;
5824 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005825 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005826 }
5827 for (i = 0; i < 256; i++)
5828 a[i] = 0;
5829 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005830}
5831
5832PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005833_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005834{
5835#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005836 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005837#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005838 int i;
5839 PyObject *l = PyList_New(257);
5840 if (l == NULL) return NULL;
5841 for (i = 0; i < 257; i++) {
5842 PyObject *x = getarray(dxpairs[i]);
5843 if (x == NULL) {
5844 Py_DECREF(l);
5845 return NULL;
5846 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005847 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005848 }
5849 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005850#endif
5851}
5852
5853#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005854
5855Py_ssize_t
5856_PyEval_RequestCodeExtraIndex(freefunc free)
5857{
Victor Stinner81a7be32020-04-14 15:14:01 +02005858 PyInterpreterState *interp = _PyInterpreterState_GET();
Brett Cannon5c4de282016-09-07 11:16:41 -07005859 Py_ssize_t new_index;
5860
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005861 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005862 return -1;
5863 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005864 new_index = interp->co_extra_user_count++;
5865 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005866 return new_index;
5867}
Łukasz Langaa785c872016-09-09 17:37:37 -07005868
5869static void
5870dtrace_function_entry(PyFrameObject *f)
5871{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005872 const char *filename;
5873 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005874 int lineno;
5875
Victor Stinner6d86a232020-04-29 00:56:58 +02005876 PyCodeObject *code = f->f_code;
5877 filename = PyUnicode_AsUTF8(code->co_filename);
5878 funcname = PyUnicode_AsUTF8(code->co_name);
5879 lineno = PyCode_Addr2Line(code, f->f_lasti);
Łukasz Langaa785c872016-09-09 17:37:37 -07005880
Andy Lestere6be9b52020-02-11 20:28:35 -06005881 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005882}
5883
5884static void
5885dtrace_function_return(PyFrameObject *f)
5886{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005887 const char *filename;
5888 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005889 int lineno;
5890
Victor Stinner6d86a232020-04-29 00:56:58 +02005891 PyCodeObject *code = f->f_code;
5892 filename = PyUnicode_AsUTF8(code->co_filename);
5893 funcname = PyUnicode_AsUTF8(code->co_name);
5894 lineno = PyCode_Addr2Line(code, f->f_lasti);
Łukasz Langaa785c872016-09-09 17:37:37 -07005895
Andy Lestere6be9b52020-02-11 20:28:35 -06005896 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005897}
5898
5899/* DTrace equivalent of maybe_call_line_trace. */
5900static void
5901maybe_dtrace_line(PyFrameObject *frame,
Mark Shannon877df852020-11-12 09:43:29 +00005902 PyCodeAddressRange *bounds, int *instr_prev)
Łukasz Langaa785c872016-09-09 17:37:37 -07005903{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005904 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005905
5906 /* If the last instruction executed isn't in the current
5907 instruction window, reset the window.
5908 */
Mark Shannon877df852020-11-12 09:43:29 +00005909 int line = _PyCode_CheckLineNumber(frame->f_lasti, bounds);
Łukasz Langaa785c872016-09-09 17:37:37 -07005910 /* If the last instruction falls at the start of a line or if
5911 it represents a jump backwards, update the frame's line
5912 number and call the trace function. */
Mark Shannon877df852020-11-12 09:43:29 +00005913 if (line != frame->f_lineno || frame->f_lasti < *instr_prev) {
5914 if (line != -1) {
5915 frame->f_lineno = line;
5916 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5917 if (!co_filename)
5918 co_filename = "?";
5919 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5920 if (!co_name)
5921 co_name = "?";
5922 PyDTrace_LINE(co_filename, co_name, line);
5923 }
Łukasz Langaa785c872016-09-09 17:37:37 -07005924 }
5925 *instr_prev = frame->f_lasti;
5926}
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005927
5928
5929/* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions
5930 for the limited API. */
5931
5932#undef Py_EnterRecursiveCall
5933
5934int Py_EnterRecursiveCall(const char *where)
5935{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005936 return _Py_EnterRecursiveCall_inline(where);
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005937}
5938
5939#undef Py_LeaveRecursiveCall
5940
5941void Py_LeaveRecursiveCall(void)
5942{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005943 _Py_LeaveRecursiveCall_inline();
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005944}