blob: 3f65820c25da94a201b229f8f4349b3d3750bd40 [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 Stinner4d231bc2019-11-14 13:36:21 +010013#include "pycore_call.h"
Victor Stinner09532fe2019-05-10 23:39:09 +020014#include "pycore_ceval.h"
Inada Naoki91234a12019-06-03 21:30:58 +090015#include "pycore_code.h"
Victor Stinnerbcda8f12018-11-21 22:27:47 +010016#include "pycore_object.h"
Victor Stinner438a12d2019-05-24 17:01:38 +020017#include "pycore_pyerrors.h"
18#include "pycore_pylifecycle.h"
Victor Stinner621cebe2018-11-12 16:53:38 +010019#include "pycore_pystate.h"
Victor Stinnerec13b932018-11-25 23:56:17 +010020#include "pycore_tupleobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000021
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000022#include "code.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040023#include "dictobject.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000024#include "frameobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000025#include "opcode.h"
Łukasz Langaa785c872016-09-09 17:37:37 -070026#include "pydtrace.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040027#include "setobject.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000028#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000029
Guido van Rossumc6004111993-11-05 10:22:19 +000030#include <ctype.h>
31
Guido van Rossum408027e1996-12-30 16:17:54 +000032#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000033/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000034#define LLTRACE 1 /* Low-level trace feature */
35#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000036#endif
37
Victor Stinner5c75f372019-04-17 23:02:26 +020038#if !defined(Py_BUILD_CORE)
39# error "ceval.c must be build with Py_BUILD_CORE define for best performance"
40#endif
41
Hai Shi46874c22020-01-30 17:20:25 -060042_Py_IDENTIFIER(__name__);
Guido van Rossum5b722181993-03-30 17:46:03 +000043
Guido van Rossum374a9221991-04-04 10:40:29 +000044/* Forward declarations */
Victor Stinner09532fe2019-05-10 23:39:09 +020045Py_LOCAL_INLINE(PyObject *) call_function(
46 PyThreadState *tstate, PyObject ***pp_stack,
47 Py_ssize_t oparg, PyObject *kwnames);
48static PyObject * do_call_core(
49 PyThreadState *tstate, PyObject *func,
50 PyObject *callargs, PyObject *kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +000051
Guido van Rossum0a066c01992-03-27 17:29:15 +000052#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000053static int lltrace;
Victor Stinner438a12d2019-05-24 17:01:38 +020054static int prtrace(PyThreadState *, PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000055#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010056static int call_trace(Py_tracefunc, PyObject *,
57 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000058 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000059static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010060 PyThreadState *, PyFrameObject *,
61 int, PyObject *);
62static void call_exc_trace(Py_tracefunc, PyObject *,
63 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000064static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Eric Snow2ebc5ce2017-09-07 23:51:28 -060065 PyThreadState *, PyFrameObject *,
66 int *, int *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070067static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *);
68static void dtrace_function_entry(PyFrameObject *);
69static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000070
Victor Stinner438a12d2019-05-24 17:01:38 +020071static PyObject * import_name(PyThreadState *, PyFrameObject *,
72 PyObject *, PyObject *, PyObject *);
73static PyObject * import_from(PyThreadState *, PyObject *, PyObject *);
74static int import_all_from(PyThreadState *, PyObject *, PyObject *);
75static void format_exc_check_arg(PyThreadState *, PyObject *, const char *, PyObject *);
76static void format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg);
77static PyObject * unicode_concatenate(PyThreadState *, PyObject *, PyObject *,
Serhiy Storchakaab874002016-09-11 13:48:15 +030078 PyFrameObject *, const _Py_CODEUNIT *);
Victor Stinner438a12d2019-05-24 17:01:38 +020079static PyObject * special_lookup(PyThreadState *, PyObject *, _Py_Identifier *);
80static int check_args_iterable(PyThreadState *, PyObject *func, PyObject *vararg);
81static void format_kwargs_error(PyThreadState *, PyObject *func, PyObject *kwargs);
Mark Shannonfee55262019-11-21 09:11:43 +000082static void format_awaitable_error(PyThreadState *, PyTypeObject *, int, int);
Guido van Rossum374a9221991-04-04 10:40:29 +000083
Paul Prescode68140d2000-08-30 20:25:01 +000084#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000086#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000088#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 "free variable '%.200s' referenced before assignment" \
90 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000091
Guido van Rossum950361c1997-01-24 13:49:28 +000092/* Dynamic execution profile */
93#ifdef DYNAMIC_EXECUTION_PROFILE
94#ifdef DXPAIRS
95static long dxpairs[257][256];
96#define dxp dxpairs[256]
97#else
98static long dxp[256];
99#endif
100#endif
101
Inada Naoki91234a12019-06-03 21:30:58 +0900102/* per opcode cache */
Inada Naokieddef862019-06-04 07:38:10 +0900103#ifdef Py_DEBUG
104// --with-pydebug is used to find memory leak. opcache makes it harder.
105// So we disable opcache when Py_DEBUG is defined.
106// See bpo-37146
107#define OPCACHE_MIN_RUNS 0 /* disable opcache */
108#else
Inada Naoki91234a12019-06-03 21:30:58 +0900109#define OPCACHE_MIN_RUNS 1024 /* create opcache when code executed this time */
Inada Naokieddef862019-06-04 07:38:10 +0900110#endif
Inada Naoki91234a12019-06-03 21:30:58 +0900111#define OPCACHE_STATS 0 /* Enable stats */
112
113#if OPCACHE_STATS
114static size_t opcache_code_objects = 0;
115static size_t opcache_code_objects_extra_mem = 0;
116
117static size_t opcache_global_opts = 0;
118static size_t opcache_global_hits = 0;
119static size_t opcache_global_misses = 0;
120#endif
121
Victor Stinnere225beb2019-06-03 18:14:24 +0200122#define GIL_REQUEST _Py_atomic_load_relaxed(&ceval->gil_drop_request)
Inada Naoki91234a12019-06-03 21:30:58 +0900123
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000124/* This can set eval_breaker to 0 even though gil_drop_request became
125 1. We believe this is all right because the eval loop will release
126 the GIL eventually anyway. */
Victor Stinnere225beb2019-06-03 18:14:24 +0200127#define COMPUTE_EVAL_BREAKER(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 _Py_atomic_store_relaxed( \
Victor Stinnere225beb2019-06-03 18:14:24 +0200129 &(ceval)->eval_breaker, \
130 GIL_REQUEST | \
131 _Py_atomic_load_relaxed(&(ceval)->signals_pending) | \
132 _Py_atomic_load_relaxed(&(ceval)->pending.calls_to_do) | \
133 (ceval)->pending.async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000134
Victor Stinnere225beb2019-06-03 18:14:24 +0200135#define SET_GIL_DROP_REQUEST(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000136 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200137 _Py_atomic_store_relaxed(&(ceval)->gil_drop_request, 1); \
138 _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000140
Victor Stinnere225beb2019-06-03 18:14:24 +0200141#define RESET_GIL_DROP_REQUEST(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200143 _Py_atomic_store_relaxed(&(ceval)->gil_drop_request, 0); \
144 COMPUTE_EVAL_BREAKER(ceval); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000146
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000147/* Pending calls are only modified under pending_lock */
Victor Stinnere225beb2019-06-03 18:14:24 +0200148#define SIGNAL_PENDING_CALLS(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200150 _Py_atomic_store_relaxed(&(ceval)->pending.calls_to_do, 1); \
151 _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000153
Victor Stinnere225beb2019-06-03 18:14:24 +0200154#define UNSIGNAL_PENDING_CALLS(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200156 _Py_atomic_store_relaxed(&(ceval)->pending.calls_to_do, 0); \
157 COMPUTE_EVAL_BREAKER(ceval); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000159
Victor Stinnere225beb2019-06-03 18:14:24 +0200160#define SIGNAL_PENDING_SIGNALS(ceval) \
Eric Snowfdf282d2019-01-11 14:26:55 -0700161 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200162 _Py_atomic_store_relaxed(&(ceval)->signals_pending, 1); \
163 _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \
Eric Snowfdf282d2019-01-11 14:26:55 -0700164 } while (0)
165
Victor Stinnere225beb2019-06-03 18:14:24 +0200166#define UNSIGNAL_PENDING_SIGNALS(ceval) \
Eric Snowfdf282d2019-01-11 14:26:55 -0700167 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200168 _Py_atomic_store_relaxed(&(ceval)->signals_pending, 0); \
169 COMPUTE_EVAL_BREAKER(ceval); \
Eric Snowfdf282d2019-01-11 14:26:55 -0700170 } while (0)
171
Victor Stinnere225beb2019-06-03 18:14:24 +0200172#define SIGNAL_ASYNC_EXC(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200174 (ceval)->pending.async_exc = 1; \
175 _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000177
Victor Stinnere225beb2019-06-03 18:14:24 +0200178#define UNSIGNAL_ASYNC_EXC(ceval) \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600179 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200180 (ceval)->pending.async_exc = 0; \
181 COMPUTE_EVAL_BREAKER(ceval); \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600182 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000183
184
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000185#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000186#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000187#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000188#include "pythread.h"
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000189#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000190
Tim Peters7f468f22004-10-11 02:40:51 +0000191int
192PyEval_ThreadsInitialized(void)
193{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100194 _PyRuntimeState *runtime = &_PyRuntime;
195 return gil_created(&runtime->ceval.gil);
Tim Peters7f468f22004-10-11 02:40:51 +0000196}
197
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000198void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000199PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000200{
Victor Stinner09532fe2019-05-10 23:39:09 +0200201 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200202 struct _ceval_runtime_state *ceval = &runtime->ceval;
203 struct _gil_runtime_state *gil = &ceval->gil;
Victor Stinner09532fe2019-05-10 23:39:09 +0200204 if (gil_created(gil)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 return;
Victor Stinnera7126792019-03-19 14:19:38 +0100206 }
207
Inada Naoki001fee12019-02-20 10:00:09 +0900208 PyThread_init_thread();
Victor Stinner09532fe2019-05-10 23:39:09 +0200209 create_gil(gil);
210 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinnere225beb2019-06-03 18:14:24 +0200211 take_gil(ceval, tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700212
Victor Stinnere225beb2019-06-03 18:14:24 +0200213 struct _pending_calls *pending = &ceval->pending;
214 pending->lock = PyThread_allocate_lock();
215 if (pending->lock == NULL) {
216 Py_FatalError("Can't initialize threads for pending calls");
217 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000218}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000219
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000220void
Victor Stinnere225beb2019-06-03 18:14:24 +0200221_PyEval_FiniThreads(struct _ceval_runtime_state *ceval)
Antoine Pitrou1df15362010-09-13 14:16:46 +0000222{
Victor Stinnere225beb2019-06-03 18:14:24 +0200223 struct _gil_runtime_state *gil = &ceval->gil;
Victor Stinner09532fe2019-05-10 23:39:09 +0200224 if (!gil_created(gil)) {
Antoine Pitrou1df15362010-09-13 14:16:46 +0000225 return;
Victor Stinnera7126792019-03-19 14:19:38 +0100226 }
227
Victor Stinner09532fe2019-05-10 23:39:09 +0200228 destroy_gil(gil);
229 assert(!gil_created(gil));
Victor Stinner99fcc612019-04-29 13:04:07 +0200230
Victor Stinnere225beb2019-06-03 18:14:24 +0200231 struct _pending_calls *pending = &ceval->pending;
232 if (pending->lock != NULL) {
233 PyThread_free_lock(pending->lock);
234 pending->lock = NULL;
235 }
Antoine Pitrou1df15362010-09-13 14:16:46 +0000236}
237
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400238static inline void
Victor Stinner01b1cc12019-11-20 02:27:56 +0100239exit_thread_if_finalizing(PyThreadState *tstate)
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400240{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100241 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200242 /* _Py_Finalizing is protected by the GIL */
Victor Stinner09532fe2019-05-10 23:39:09 +0200243 if (runtime->finalizing != NULL && !_Py_CURRENTLY_FINALIZING(runtime, tstate)) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200244 drop_gil(&runtime->ceval, tstate);
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400245 PyThread_exit_thread();
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400246 }
247}
248
Antoine Pitrou1df15362010-09-13 14:16:46 +0000249void
Inada Naoki91234a12019-06-03 21:30:58 +0900250_PyEval_Fini(void)
251{
252#if OPCACHE_STATS
253 fprintf(stderr, "-- Opcode cache number of objects = %zd\n",
254 opcache_code_objects);
255
256 fprintf(stderr, "-- Opcode cache total extra mem = %zd\n",
257 opcache_code_objects_extra_mem);
258
259 fprintf(stderr, "\n");
260
261 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL hits = %zd (%d%%)\n",
262 opcache_global_hits,
263 (int) (100.0 * opcache_global_hits /
264 (opcache_global_hits + opcache_global_misses)));
265
266 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL misses = %zd (%d%%)\n",
267 opcache_global_misses,
268 (int) (100.0 * opcache_global_misses /
269 (opcache_global_hits + opcache_global_misses)));
270
271 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL opts = %zd\n",
272 opcache_global_opts);
273
274 fprintf(stderr, "\n");
275#endif
276}
277
278void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000279PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000280{
Victor Stinner09532fe2019-05-10 23:39:09 +0200281 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200282 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinner09532fe2019-05-10 23:39:09 +0200283 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
284 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
Victor Stinner09532fe2019-05-10 23:39:09 +0200286 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200287 take_gil(ceval, tstate);
Victor Stinner01b1cc12019-11-20 02:27:56 +0100288 exit_thread_if_finalizing(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000289}
290
291void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000292PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000293{
Victor Stinner09532fe2019-05-10 23:39:09 +0200294 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200295 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100297 We therefore avoid PyThreadState_Get() which dumps a fatal error
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 in debug mode.
299 */
Victor Stinnere225beb2019-06-03 18:14:24 +0200300 drop_gil(&runtime->ceval, tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000301}
302
303void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000304PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000305{
Victor Stinner17c68b82020-01-30 12:20:48 +0100306 assert(tstate != NULL);
Victor Stinnere225beb2019-06-03 18:14:24 +0200307
Victor Stinner01b1cc12019-11-20 02:27:56 +0100308 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200309 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinner09532fe2019-05-10 23:39:09 +0200310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 /* Check someone has called PyEval_InitThreads() to create the lock */
Victor Stinnere225beb2019-06-03 18:14:24 +0200312 assert(gil_created(&ceval->gil));
313 take_gil(ceval, tstate);
Victor Stinner01b1cc12019-11-20 02:27:56 +0100314 exit_thread_if_finalizing(tstate);
Victor Stinner09532fe2019-05-10 23:39:09 +0200315 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
316 Py_FatalError("PyEval_AcquireThread: non-NULL old thread state");
317 }
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000318}
319
320void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000321PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000322{
Victor Stinner17c68b82020-01-30 12:20:48 +0100323 assert(tstate != NULL);
Victor Stinner09532fe2019-05-10 23:39:09 +0200324
Victor Stinner01b1cc12019-11-20 02:27:56 +0100325 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200326 PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
327 if (new_tstate != tstate) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200329 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200330 drop_gil(&runtime->ceval, tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000331}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000332
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200333/* This function is called from PyOS_AfterFork_Child to destroy all threads
334 * which are not running in the child process, and clear internal locks
335 * which might be held by those threads.
336 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000337
338void
Victor Stinnerd5d9e812019-05-13 12:35:37 +0200339_PyEval_ReInitThreads(_PyRuntimeState *runtime)
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000340{
Victor Stinnere225beb2019-06-03 18:14:24 +0200341 struct _ceval_runtime_state *ceval = &runtime->ceval;
342 if (!gil_created(&ceval->gil)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 return;
Victor Stinner09532fe2019-05-10 23:39:09 +0200344 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200345 recreate_gil(&ceval->gil);
Victor Stinner09532fe2019-05-10 23:39:09 +0200346 PyThreadState *current_tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinnere225beb2019-06-03 18:14:24 +0200347 take_gil(ceval, current_tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700348
Victor Stinnere225beb2019-06-03 18:14:24 +0200349 struct _pending_calls *pending = &ceval->pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200350 pending->lock = PyThread_allocate_lock();
351 if (pending->lock == NULL) {
Eric Snow8479a342019-03-08 23:44:33 -0700352 Py_FatalError("Can't initialize threads for pending calls");
353 }
Jesse Nollera8513972008-07-17 16:49:17 +0000354
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200355 /* Destroy all threads except the current one */
Victor Stinner0fd2c302019-06-04 03:15:09 +0200356 _PyThreadState_DeleteExcept(runtime, current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000357}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000358
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000359/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600360 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000361
362void
Victor Stinnere225beb2019-06-03 18:14:24 +0200363_PyEval_SignalAsyncExc(struct _ceval_runtime_state *ceval)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000364{
Victor Stinnere225beb2019-06-03 18:14:24 +0200365 SIGNAL_ASYNC_EXC(ceval);
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000366}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000367
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000368PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000369PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000370{
Victor Stinner09532fe2019-05-10 23:39:09 +0200371 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200372 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinner09532fe2019-05-10 23:39:09 +0200373 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
374 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 Py_FatalError("PyEval_SaveThread: NULL tstate");
Victor Stinner09532fe2019-05-10 23:39:09 +0200376 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200377 assert(gil_created(&ceval->gil));
378 drop_gil(ceval, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000380}
381
382void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000383PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000384{
Victor Stinner17c68b82020-01-30 12:20:48 +0100385 assert(tstate != NULL);
386
Victor Stinner01b1cc12019-11-20 02:27:56 +0100387 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner0fd2c302019-06-04 03:15:09 +0200388 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinnere225beb2019-06-03 18:14:24 +0200389 assert(gil_created(&ceval->gil));
Victor Stinner2914bb32018-01-29 11:57:45 +0100390
391 int err = errno;
Victor Stinnere225beb2019-06-03 18:14:24 +0200392 take_gil(ceval, tstate);
Victor Stinner01b1cc12019-11-20 02:27:56 +0100393 exit_thread_if_finalizing(tstate);
Victor Stinner2914bb32018-01-29 11:57:45 +0100394 errno = err;
395
Victor Stinner09532fe2019-05-10 23:39:09 +0200396 _PyThreadState_Swap(&runtime->gilstate, tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000397}
398
399
Guido van Rossuma9672091994-09-14 13:31:22 +0000400/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
401 signal handlers or Mac I/O completion routines) can schedule calls
402 to a function to be called synchronously.
403 The synchronous function is called with one void* argument.
404 It should return 0 for success or -1 for failure -- failure should
405 be accompanied by an exception.
406
407 If registry succeeds, the registry function returns 0; if it fails
408 (e.g. due to too many pending calls) it returns -1 (without setting
409 an exception condition).
410
411 Note that because registry may occur from within signal handlers,
412 or other asynchronous events, calling malloc() is unsafe!
413
Guido van Rossuma9672091994-09-14 13:31:22 +0000414 Any thread can schedule pending calls, but only the main thread
415 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000416 There is no facility to schedule calls to a particular thread, but
417 that should be easy to change, should that ever be required. In
418 that case, the static variables here should go into the python
419 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000420*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000421
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200422void
Victor Stinnere225beb2019-06-03 18:14:24 +0200423_PyEval_SignalReceived(struct _ceval_runtime_state *ceval)
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200424{
425 /* bpo-30703: Function called when the C signal handler of Python gets a
426 signal. We cannot queue a callback using Py_AddPendingCall() since
427 that function is not async-signal-safe. */
Victor Stinnere225beb2019-06-03 18:14:24 +0200428 SIGNAL_PENDING_SIGNALS(ceval);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200429}
430
Eric Snow5be45a62019-03-08 22:47:07 -0700431/* Push one item onto the queue while holding the lock. */
432static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200433_push_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600434 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700435{
Eric Snow842a2f02019-03-15 15:47:51 -0600436 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700437 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600438 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700439 return -1; /* Queue full */
440 }
Eric Snow842a2f02019-03-15 15:47:51 -0600441 pending->calls[i].func = func;
442 pending->calls[i].arg = arg;
443 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700444 return 0;
445}
446
447/* Pop one item off the queue while holding the lock. */
448static void
Victor Stinnere225beb2019-06-03 18:14:24 +0200449_pop_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600450 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700451{
Eric Snow842a2f02019-03-15 15:47:51 -0600452 int i = pending->first;
453 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700454 return; /* Queue empty */
455 }
456
Eric Snow842a2f02019-03-15 15:47:51 -0600457 *func = pending->calls[i].func;
458 *arg = pending->calls[i].arg;
459 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700460}
461
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200462/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000463 scheduling to be made from any thread, and even from an executing
464 callback.
465 */
466
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000467int
Victor Stinner438a12d2019-05-24 17:01:38 +0200468_PyEval_AddPendingCall(PyThreadState *tstate,
Victor Stinnere225beb2019-06-03 18:14:24 +0200469 struct _ceval_runtime_state *ceval,
Victor Stinner09532fe2019-05-10 23:39:09 +0200470 int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000471{
Victor Stinnere225beb2019-06-03 18:14:24 +0200472 struct _pending_calls *pending = &ceval->pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600473
474 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
475 if (pending->finishing) {
476 PyThread_release_lock(pending->lock);
477
478 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +0200479 _PyErr_Fetch(tstate, &exc, &val, &tb);
480 _PyErr_SetString(tstate, PyExc_SystemError,
Eric Snow842a2f02019-03-15 15:47:51 -0600481 "Py_AddPendingCall: cannot add pending calls "
482 "(Python shutting down)");
Victor Stinner438a12d2019-05-24 17:01:38 +0200483 _PyErr_Print(tstate);
484 _PyErr_Restore(tstate, exc, val, tb);
Eric Snow842a2f02019-03-15 15:47:51 -0600485 return -1;
486 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200487 int result = _push_pending_call(pending, func, arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600488 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700489
Victor Stinnere225beb2019-06-03 18:14:24 +0200490 /* signal main loop */
491 SIGNAL_PENDING_CALLS(ceval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000493}
494
Victor Stinner09532fe2019-05-10 23:39:09 +0200495int
496Py_AddPendingCall(int (*func)(void *), void *arg)
497{
Victor Stinner438a12d2019-05-24 17:01:38 +0200498 _PyRuntimeState *runtime = &_PyRuntime;
499 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinnere225beb2019-06-03 18:14:24 +0200500 return _PyEval_AddPendingCall(tstate, &runtime->ceval, func, arg);
Victor Stinner09532fe2019-05-10 23:39:09 +0200501}
502
Eric Snowfdf282d2019-01-11 14:26:55 -0700503static int
Victor Stinner09532fe2019-05-10 23:39:09 +0200504handle_signals(_PyRuntimeState *runtime)
Eric Snowfdf282d2019-01-11 14:26:55 -0700505{
Eric Snow5be45a62019-03-08 22:47:07 -0700506 /* Only handle signals on main thread. PyEval_InitThreads must
507 * have been called already.
508 */
Victor Stinner09532fe2019-05-10 23:39:09 +0200509 if (PyThread_get_thread_ident() != runtime->main_thread) {
Eric Snowfdf282d2019-01-11 14:26:55 -0700510 return 0;
511 }
Eric Snow64d6cc82019-02-23 15:40:43 -0700512 /*
513 * Ensure that the thread isn't currently running some other
514 * interpreter.
515 */
Victor Stinner09532fe2019-05-10 23:39:09 +0200516 PyInterpreterState *interp = _PyRuntimeState_GetThreadState(runtime)->interp;
517 if (interp != runtime->interpreters.main) {
Eric Snow64d6cc82019-02-23 15:40:43 -0700518 return 0;
519 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700520
Victor Stinnere225beb2019-06-03 18:14:24 +0200521 struct _ceval_runtime_state *ceval = &runtime->ceval;
522 UNSIGNAL_PENDING_SIGNALS(ceval);
Eric Snow64d6cc82019-02-23 15:40:43 -0700523 if (_PyErr_CheckSignals() < 0) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200524 SIGNAL_PENDING_SIGNALS(ceval); /* We're not done yet */
Eric Snowfdf282d2019-01-11 14:26:55 -0700525 return -1;
526 }
527 return 0;
528}
529
530static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200531make_pending_calls(_PyRuntimeState *runtime)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000532{
Eric Snow6a150bc2019-06-01 15:39:46 -0600533 static int busy = 0;
Eric Snowb75b1a352019-04-12 10:20:10 -0600534
Victor Stinnere225beb2019-06-03 18:14:24 +0200535 /* only service pending calls on main thread */
536 if (PyThread_get_thread_ident() != runtime->main_thread) {
537 return 0;
538 }
539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 /* don't perform recursive pending calls */
Eric Snowfdf282d2019-01-11 14:26:55 -0700541 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700543 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200544 busy = 1;
Victor Stinnere225beb2019-06-03 18:14:24 +0200545 struct _ceval_runtime_state *ceval = &runtime->ceval;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200546 /* unsignal before starting to call callbacks, so that any callback
547 added in-between re-signals */
Victor Stinnere225beb2019-06-03 18:14:24 +0200548 UNSIGNAL_PENDING_CALLS(ceval);
Eric Snowfdf282d2019-01-11 14:26:55 -0700549 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 /* perform a bounded number of calls, in case of recursion */
Victor Stinnere225beb2019-06-03 18:14:24 +0200552 struct _pending_calls *pending = &ceval->pending;
Eric Snowfdf282d2019-01-11 14:26:55 -0700553 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700554 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 void *arg = NULL;
556
557 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600558 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200559 _pop_pending_call(pending, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600560 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700561
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100562 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700563 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100564 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700565 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700566 res = func(arg);
567 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200568 goto error;
569 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000570 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200571
Charles-François Natalif23339a2011-07-23 18:15:43 +0200572 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700573 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200574
575error:
576 busy = 0;
Victor Stinnere225beb2019-06-03 18:14:24 +0200577 SIGNAL_PENDING_CALLS(ceval);
Eric Snowfdf282d2019-01-11 14:26:55 -0700578 return res;
579}
580
Eric Snow842a2f02019-03-15 15:47:51 -0600581void
Victor Stinner2b1df452020-01-13 18:46:59 +0100582_Py_FinishPendingCalls(PyThreadState *tstate)
Eric Snow842a2f02019-03-15 15:47:51 -0600583{
Eric Snow842a2f02019-03-15 15:47:51 -0600584 assert(PyGILState_Check());
585
Victor Stinner2b1df452020-01-13 18:46:59 +0100586 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200587 struct _pending_calls *pending = &runtime->ceval.pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200588
Eric Snow842a2f02019-03-15 15:47:51 -0600589 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
590 pending->finishing = 1;
591 PyThread_release_lock(pending->lock);
592
593 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
594 return;
595 }
596
Victor Stinnere225beb2019-06-03 18:14:24 +0200597 if (make_pending_calls(runtime) < 0) {
598 PyObject *exc, *val, *tb;
599 _PyErr_Fetch(tstate, &exc, &val, &tb);
600 PyErr_BadInternalCall();
601 _PyErr_ChainExceptions(exc, val, tb);
602 _PyErr_Print(tstate);
Eric Snow842a2f02019-03-15 15:47:51 -0600603 }
604}
605
Eric Snowfdf282d2019-01-11 14:26:55 -0700606/* Py_MakePendingCalls() is a simple wrapper for the sake
607 of backward-compatibility. */
608int
609Py_MakePendingCalls(void)
610{
611 assert(PyGILState_Check());
612
613 /* Python signal handler doesn't really queue a callback: it only signals
614 that a signal was received, see _PyEval_SignalReceived(). */
Victor Stinner09532fe2019-05-10 23:39:09 +0200615 _PyRuntimeState *runtime = &_PyRuntime;
616 int res = handle_signals(runtime);
Eric Snowfdf282d2019-01-11 14:26:55 -0700617 if (res != 0) {
618 return res;
619 }
620
Victor Stinnere225beb2019-06-03 18:14:24 +0200621 res = make_pending_calls(runtime);
Eric Snowb75b1a352019-04-12 10:20:10 -0600622 if (res != 0) {
623 return res;
624 }
625
626 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000627}
628
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000629/* The interpreter's recursion limit */
630
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000631#ifndef Py_DEFAULT_RECURSION_LIMIT
632#define Py_DEFAULT_RECURSION_LIMIT 1000
633#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600634
Eric Snow05351c12017-09-05 21:43:08 -0700635int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000636
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600637void
Victor Stinnere225beb2019-06-03 18:14:24 +0200638_PyEval_Initialize(struct _ceval_runtime_state *state)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600639{
Victor Stinnere225beb2019-06-03 18:14:24 +0200640 state->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600641 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Victor Stinnere225beb2019-06-03 18:14:24 +0200642 _gil_initialize(&state->gil);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600643}
644
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000645int
646Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000647{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100648 struct _ceval_runtime_state *ceval = &_PyRuntime.ceval;
649 return ceval->recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000650}
651
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000652void
653Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000654{
Victor Stinnere225beb2019-06-03 18:14:24 +0200655 struct _ceval_runtime_state *ceval = &_PyRuntime.ceval;
656 ceval->recursion_limit = new_limit;
657 _Py_CheckRecursionLimit = ceval->recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000658}
659
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100660/* The function _Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
Armin Rigo2b3eb402003-10-28 12:05:48 +0000661 if the recursion_depth reaches _Py_CheckRecursionLimit.
662 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
663 to guarantee that _Py_CheckRecursiveCall() is regularly called.
664 Without USE_STACKCHECK, there is no need for this. */
665int
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100666_Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000667{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100668 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200669 int recursion_limit = runtime->ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000670
671#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700672 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 if (PyOS_CheckStack()) {
674 --tstate->recursion_depth;
Victor Stinner438a12d2019-05-24 17:01:38 +0200675 _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 return -1;
677 }
pdox18967932017-10-25 23:03:01 -0700678 /* Needed for ABI backwards-compatibility (see bpo-31857) */
Eric Snow05351c12017-09-05 21:43:08 -0700679 _Py_CheckRecursionLimit = recursion_limit;
pdox18967932017-10-25 23:03:01 -0700680#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 if (tstate->recursion_critical)
682 /* Somebody asked that we don't check for recursion. */
683 return 0;
684 if (tstate->overflowed) {
685 if (tstate->recursion_depth > recursion_limit + 50) {
686 /* Overflowing while handling an overflow. Give up. */
687 Py_FatalError("Cannot recover from stack overflow.");
688 }
689 return 0;
690 }
691 if (tstate->recursion_depth > recursion_limit) {
692 --tstate->recursion_depth;
693 tstate->overflowed = 1;
Victor Stinner438a12d2019-05-24 17:01:38 +0200694 _PyErr_Format(tstate, PyExc_RecursionError,
695 "maximum recursion depth exceeded%s",
696 where);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 return -1;
698 }
699 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000700}
701
Victor Stinner09532fe2019-05-10 23:39:09 +0200702static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
Victor Stinner438a12d2019-05-24 17:01:38 +0200703static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000704
Victor Stinnere225beb2019-06-03 18:14:24 +0200705#define _Py_TracingPossible(ceval) ((ceval)->tracing_possible)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000706
Guido van Rossum374a9221991-04-04 10:40:29 +0000707
Guido van Rossumb209a111997-04-29 18:18:01 +0000708PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000709PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000710{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 return PyEval_EvalCodeEx(co,
712 globals, locals,
713 (PyObject **)NULL, 0,
714 (PyObject **)NULL, 0,
715 (PyObject **)NULL, 0,
716 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000717}
718
719
720/* Interpreter main loop */
721
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000722PyObject *
Victor Stinnerb9e68122019-11-14 12:20:46 +0100723PyEval_EvalFrame(PyFrameObject *f)
724{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 /* This is for backward compatibility with extension modules that
726 used this API; core interpreter code should call
727 PyEval_EvalFrameEx() */
Victor Stinnerb9e68122019-11-14 12:20:46 +0100728 PyThreadState *tstate = _PyThreadState_GET();
729 return _PyEval_EvalFrame(tstate, f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000730}
731
732PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000733PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000734{
Victor Stinnerb9e68122019-11-14 12:20:46 +0100735 PyThreadState *tstate = _PyThreadState_GET();
736 return _PyEval_EvalFrame(tstate, f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700737}
738
Victor Stinnerc6944e72016-11-11 02:13:35 +0100739PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700740_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
741{
Guido van Rossum950361c1997-01-24 13:49:28 +0000742#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000744#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200745 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300746 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200747 int opcode; /* Current opcode */
748 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200749 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000750 PyObject *retval = NULL; /* Return value */
Victor Stinner09532fe2019-05-10 23:39:09 +0200751 _PyRuntimeState * const runtime = &_PyRuntime;
752 PyThreadState * const tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinnere225beb2019-06-03 18:14:24 +0200753 struct _ceval_runtime_state * const ceval = &runtime->ceval;
754 _Py_atomic_int * const eval_breaker = &ceval->eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000758
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000760
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 is true when the line being executed has changed. The
762 initial values are such as to make this false the first
763 time it is tested. */
764 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000765
Serhiy Storchakaab874002016-09-11 13:48:15 +0300766 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 PyObject *names;
768 PyObject *consts;
Inada Naoki91234a12019-06-03 21:30:58 +0900769 _PyOpcache *co_opcache;
Guido van Rossum374a9221991-04-04 10:40:29 +0000770
Brett Cannon368b4b72012-04-02 12:17:59 -0400771#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200772 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400773#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200774
Antoine Pitroub52ec782009-01-25 16:34:23 +0000775/* Computed GOTOs, or
776 the-optimization-commonly-but-improperly-known-as-"threaded code"
777 using gcc's labels-as-values extension
778 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
779
780 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000782 combined with a lookup table of jump addresses. However, since the
783 indirect jump instruction is shared by all opcodes, the CPU will have a
784 hard time making the right prediction for where to jump next (actually,
785 it will be always wrong except in the uncommon case of a sequence of
786 several identical opcodes).
787
788 "Threaded code" in contrast, uses an explicit jump table and an explicit
789 indirect jump instruction at the end of each opcode. Since the jump
790 instruction is at a different address for each opcode, the CPU will make a
791 separate prediction for each of these instructions, which is equivalent to
792 predicting the second opcode of each opcode pair. These predictions have
793 a much better chance to turn out valid, especially in small bytecode loops.
794
795 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000797 and potentially many more instructions (depending on the pipeline width).
798 A correctly predicted branch, however, is nearly free.
799
800 At the time of this writing, the "threaded code" version is up to 15-20%
801 faster than the normal "switch" version, depending on the compiler and the
802 CPU architecture.
803
804 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
805 because it would render the measurements invalid.
806
807
808 NOTE: care must be taken that the compiler doesn't try to "optimize" the
809 indirect jumps by sharing them between all opcodes. Such optimizations
810 can be disabled on gcc by using the -fno-gcse flag (or possibly
811 -fno-crossjumping).
812*/
813
Antoine Pitrou042b1282010-08-13 21:15:58 +0000814#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000815#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000816#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000817#endif
818
Antoine Pitrou042b1282010-08-13 21:15:58 +0000819#ifdef HAVE_COMPUTED_GOTOS
820 #ifndef USE_COMPUTED_GOTOS
821 #define USE_COMPUTED_GOTOS 1
822 #endif
823#else
824 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
825 #error "Computed gotos are not supported on this compiler."
826 #endif
827 #undef USE_COMPUTED_GOTOS
828 #define USE_COMPUTED_GOTOS 0
829#endif
830
831#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000832/* Import the static jump table */
833#include "opcode_targets.h"
834
Antoine Pitroub52ec782009-01-25 16:34:23 +0000835#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -0700836 op: \
837 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000838
Antoine Pitroub52ec782009-01-25 16:34:23 +0000839#ifdef LLTRACE
840#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200842 if (!lltrace && !_Py_TracingPossible(ceval) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300844 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300845 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 } \
847 goto fast_next_opcode; \
848 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000849#else
850#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200852 if (!_Py_TracingPossible(ceval) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300854 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300855 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 } \
857 goto fast_next_opcode; \
858 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000859#endif
860
Victor Stinner09532fe2019-05-10 23:39:09 +0200861#define DISPATCH() \
862 { \
863 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
864 FAST_DISPATCH(); \
865 } \
866 continue; \
867 }
868
Antoine Pitroub52ec782009-01-25 16:34:23 +0000869#else
Benjamin Petersonddd19492018-09-16 22:38:02 -0700870#define TARGET(op) op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000871#define FAST_DISPATCH() goto fast_next_opcode
Victor Stinner09532fe2019-05-10 23:39:09 +0200872#define DISPATCH() continue
Antoine Pitroub52ec782009-01-25 16:34:23 +0000873#endif
874
875
Neal Norwitza81d2202002-07-14 00:27:26 +0000876/* Tuple access macros */
877
878#ifndef Py_DEBUG
879#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
880#else
881#define GETITEM(v, i) PyTuple_GetItem((v), (i))
882#endif
883
Guido van Rossum374a9221991-04-04 10:40:29 +0000884/* Code access macros */
885
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300886/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600887#define INSTR_OFFSET() \
888 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300889#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300890 _Py_CODEUNIT word = *next_instr; \
891 opcode = _Py_OPCODE(word); \
892 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300893 next_instr++; \
894 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300895#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
896#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000897
Raymond Hettingerf606f872003-03-16 03:11:04 +0000898/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 Some opcodes tend to come in pairs thus making it possible to
900 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300901 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 Verifying the prediction costs a single high-speed test of a register
904 variable against a constant. If the pairing was good, then the
905 processor's own internal branch predication has a high likelihood of
906 success, resulting in a nearly zero-overhead transition to the
907 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300908 including its unpredictable switch-case branch. Combined with the
909 processor's internal branch prediction, a successful PREDICT has the
910 effect of making the two opcodes run as if they were a single new opcode
911 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000912
Georg Brandl86b2fb92008-07-16 03:43:04 +0000913 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 predictions turned-on and interpret the results as if some opcodes
915 had been combined or turn-off predictions so that the opcode frequency
916 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000917
918 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 the CPU to record separate branch prediction information for each
920 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000921
Raymond Hettingerf606f872003-03-16 03:11:04 +0000922*/
923
Denis Chernikovbaf29b22020-02-21 12:17:50 +0300924#define PREDICT_ID(op) PRED_##op
925
Antoine Pitrou042b1282010-08-13 21:15:58 +0000926#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Denis Chernikovbaf29b22020-02-21 12:17:50 +0300927#define PREDICT(op) if (0) goto PREDICT_ID(op)
Raymond Hettingera7216982004-02-08 19:59:27 +0000928#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300929#define PREDICT(op) \
Denis Chernikovbaf29b22020-02-21 12:17:50 +0300930 do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300931 _Py_CODEUNIT word = *next_instr; \
932 opcode = _Py_OPCODE(word); \
Denis Chernikovbaf29b22020-02-21 12:17:50 +0300933 if (opcode == op) { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300934 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300935 next_instr++; \
Denis Chernikovbaf29b22020-02-21 12:17:50 +0300936 goto PREDICT_ID(op); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300937 } \
938 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000939#endif
Denis Chernikovbaf29b22020-02-21 12:17:50 +0300940#define PREDICTED(op) PREDICT_ID(op):
Antoine Pitroub52ec782009-01-25 16:34:23 +0000941
Raymond Hettingerf606f872003-03-16 03:11:04 +0000942
Guido van Rossum374a9221991-04-04 10:40:29 +0000943/* Stack manipulation macros */
944
Martin v. Löwis18e16552006-02-15 17:27:45 +0000945/* The stack can grow at most MAXINT deep, as co_nlocals and
946 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000947#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
948#define EMPTY() (STACK_LEVEL() == 0)
949#define TOP() (stack_pointer[-1])
950#define SECOND() (stack_pointer[-2])
951#define THIRD() (stack_pointer[-3])
952#define FOURTH() (stack_pointer[-4])
953#define PEEK(n) (stack_pointer[-(n)])
954#define SET_TOP(v) (stack_pointer[-1] = (v))
955#define SET_SECOND(v) (stack_pointer[-2] = (v))
956#define SET_THIRD(v) (stack_pointer[-3] = (v))
957#define SET_FOURTH(v) (stack_pointer[-4] = (v))
958#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
959#define BASIC_STACKADJ(n) (stack_pointer += n)
960#define BASIC_PUSH(v) (*stack_pointer++ = (v))
961#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000962
Guido van Rossum96a42c81992-01-12 02:29:51 +0000963#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +0200965 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000966 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +0200967#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000968 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +0000969#define STACK_GROW(n) do { \
970 assert(n >= 0); \
971 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +0200972 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +0000973 assert(STACK_LEVEL() <= co->co_stacksize); \
974 } while (0)
975#define STACK_SHRINK(n) do { \
976 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +0200977 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +0000978 (void)(BASIC_STACKADJ(-n)); \
979 assert(STACK_LEVEL() <= co->co_stacksize); \
980 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +0000981#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +0200982 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000983 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000984#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000985#define PUSH(v) BASIC_PUSH(v)
986#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +0000987#define STACK_GROW(n) BASIC_STACKADJ(n)
988#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000989#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000990#endif
991
Guido van Rossum681d79a1995-07-18 14:51:37 +0000992/* Local variable macros */
993
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000995
996/* The SETLOCAL() macro must not DECREF the local variable in-place and
997 then store the new value; it must copy the old value to a temporary
998 value, then store the new value, and then DECREF the temporary value.
999 This is because it is possible that during the DECREF the frame is
1000 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1001 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001003 GETLOCAL(i) = value; \
1004 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001005
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001006
1007#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 while (STACK_LEVEL() > (b)->b_level) { \
1009 PyObject *v = POP(); \
1010 Py_XDECREF(v); \
1011 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001012
1013#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001014 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +01001016 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1018 while (STACK_LEVEL() > (b)->b_level + 3) { \
1019 value = POP(); \
1020 Py_XDECREF(value); \
1021 } \
Mark Shannonae3087c2017-10-22 22:41:51 +01001022 exc_info = tstate->exc_info; \
1023 type = exc_info->exc_type; \
1024 value = exc_info->exc_value; \
1025 traceback = exc_info->exc_traceback; \
1026 exc_info->exc_type = POP(); \
1027 exc_info->exc_value = POP(); \
1028 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 Py_XDECREF(type); \
1030 Py_XDECREF(value); \
1031 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001032 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001033
Inada Naoki91234a12019-06-03 21:30:58 +09001034 /* macros for opcode cache */
1035#define OPCACHE_CHECK() \
1036 do { \
1037 co_opcache = NULL; \
1038 if (co->co_opcache != NULL) { \
1039 unsigned char co_opt_offset = \
1040 co->co_opcache_map[next_instr - first_instr]; \
1041 if (co_opt_offset > 0) { \
1042 assert(co_opt_offset <= co->co_opcache_size); \
1043 co_opcache = &co->co_opcache[co_opt_offset - 1]; \
1044 assert(co_opcache != NULL); \
Inada Naoki91234a12019-06-03 21:30:58 +09001045 } \
1046 } \
1047 } while (0)
1048
1049#if OPCACHE_STATS
1050
1051#define OPCACHE_STAT_GLOBAL_HIT() \
1052 do { \
1053 if (co->co_opcache != NULL) opcache_global_hits++; \
1054 } while (0)
1055
1056#define OPCACHE_STAT_GLOBAL_MISS() \
1057 do { \
1058 if (co->co_opcache != NULL) opcache_global_misses++; \
1059 } while (0)
1060
1061#define OPCACHE_STAT_GLOBAL_OPT() \
1062 do { \
1063 if (co->co_opcache != NULL) opcache_global_opts++; \
1064 } while (0)
1065
1066#else /* OPCACHE_STATS */
1067
1068#define OPCACHE_STAT_GLOBAL_HIT()
1069#define OPCACHE_STAT_GLOBAL_MISS()
1070#define OPCACHE_STAT_GLOBAL_OPT()
1071
1072#endif
1073
Guido van Rossuma027efa1997-05-05 20:56:21 +00001074/* Start of code */
1075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076 /* push frame */
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001077 if (_Py_EnterRecursiveCall(tstate, "")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001079 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001080
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001082
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 if (tstate->use_tracing) {
1084 if (tstate->c_tracefunc != NULL) {
1085 /* tstate->c_tracefunc, if defined, is a
1086 function that will be called on *every* entry
1087 to a code block. Its return value, if not
1088 None, is a function that will be called at
1089 the start of each executed line of code.
1090 (Actually, the function must return itself
1091 in order to continue tracing.) The trace
1092 functions are called with three arguments:
1093 a pointer to the current frame, a string
1094 indicating why the function is called, and
1095 an argument which depends on the situation.
1096 The global trace function is also called
1097 whenever an exception is detected. */
1098 if (call_trace_protected(tstate->c_tracefunc,
1099 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001100 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 /* Trace function raised an error */
1102 goto exit_eval_frame;
1103 }
1104 }
1105 if (tstate->c_profilefunc != NULL) {
1106 /* Similar for c_profilefunc, except it needn't
1107 return itself and isn't called for "line" events */
1108 if (call_trace_protected(tstate->c_profilefunc,
1109 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001110 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 /* Profile function raised an error */
1112 goto exit_eval_frame;
1113 }
1114 }
1115 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001116
Łukasz Langaa785c872016-09-09 17:37:37 -07001117 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1118 dtrace_function_entry(f);
1119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 co = f->f_code;
1121 names = co->co_names;
1122 consts = co->co_consts;
1123 fastlocals = f->f_localsplus;
1124 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001125 assert(PyBytes_Check(co->co_code));
1126 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001127 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1128 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1129 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001130 /*
1131 f->f_lasti refers to the index of the last instruction,
1132 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001133
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001134 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001135 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001136
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 When the PREDICT() macros are enabled, some opcode pairs follow in
1138 direct succession without updating f->f_lasti. A successful
1139 prediction effectively links the two codes together as if they
1140 were a single new opcode; accordingly,f->f_lasti will point to
1141 the first code in the pair (for instance, GET_ITER followed by
1142 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001143 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001145 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001146 next_instr = first_instr;
1147 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001148 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1149 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001150 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 stack_pointer = f->f_stacktop;
1152 assert(stack_pointer != NULL);
1153 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001154 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001155
Inada Naoki91234a12019-06-03 21:30:58 +09001156 if (co->co_opcache_flag < OPCACHE_MIN_RUNS) {
1157 co->co_opcache_flag++;
1158 if (co->co_opcache_flag == OPCACHE_MIN_RUNS) {
1159 if (_PyCode_InitOpcache(co) < 0) {
1160 return NULL;
1161 }
1162#if OPCACHE_STATS
1163 opcache_code_objects_extra_mem +=
1164 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1165 sizeof(_PyOpcache) * co->co_opcache_size;
1166 opcache_code_objects++;
1167#endif
1168 }
1169 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001170
Tim Peters5ca576e2001-06-18 22:08:13 +00001171#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001172 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001173#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001174
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001175 if (throwflag) /* support for generator.throw() */
1176 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001177
Victor Stinnerace47d72013-07-18 01:41:08 +02001178#ifdef Py_DEBUG
1179 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001180 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001181 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001182 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001183#endif
1184
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001185main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1188 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001189 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 /* Do periodic things. Doing this every time through
1192 the loop would add too much overhead, so we do it
1193 only every Nth instruction. We also do it if
1194 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1195 event needs attention (e.g. a signal handler or
1196 async I/O handler); see Py_AddPendingCall() and
1197 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001198
Eric Snow7bda9de2019-03-08 17:25:54 -07001199 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001200 opcode = _Py_OPCODE(*next_instr);
1201 if (opcode == SETUP_FINALLY ||
1202 opcode == SETUP_WITH ||
1203 opcode == BEFORE_ASYNC_WITH ||
1204 opcode == YIELD_FROM) {
1205 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001206 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001207 - If we're about to enter the 'with:'. It will prevent
1208 emitting a resource warning in the common idiom
1209 'with open(path) as file:'.
1210 - If we're about to enter the 'async with:'.
1211 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001212 *very* useful, but might help in some cases and it's
1213 traditional)
1214 - If we're resuming a chain of nested 'yield from' or
1215 'await' calls, then each frame is parked with YIELD_FROM
1216 as its next opcode. If the user hit control-C we want to
1217 wait until we've reached the innermost frame before
1218 running the signal handler and raising KeyboardInterrupt
1219 (see bpo-30039).
1220 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001221 goto fast_next_opcode;
1222 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001223
Victor Stinnere225beb2019-06-03 18:14:24 +02001224 if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
Victor Stinner09532fe2019-05-10 23:39:09 +02001225 if (handle_signals(runtime) != 0) {
Eric Snowfdf282d2019-01-11 14:26:55 -07001226 goto error;
1227 }
1228 }
Victor Stinnere225beb2019-06-03 18:14:24 +02001229 if (_Py_atomic_load_relaxed(&ceval->pending.calls_to_do)) {
1230 if (make_pending_calls(runtime) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001231 goto error;
Eric Snowfdf282d2019-01-11 14:26:55 -07001232 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001234
Victor Stinnere225beb2019-06-03 18:14:24 +02001235 if (_Py_atomic_load_relaxed(&ceval->gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 /* Give another thread a chance */
Victor Stinner09532fe2019-05-10 23:39:09 +02001237 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001238 Py_FatalError("ceval: tstate mix-up");
Victor Stinner09532fe2019-05-10 23:39:09 +02001239 }
Victor Stinnere225beb2019-06-03 18:14:24 +02001240 drop_gil(ceval, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241
1242 /* Other threads may run now */
1243
Victor Stinnere225beb2019-06-03 18:14:24 +02001244 take_gil(ceval, tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001245
1246 /* Check if we should make a quick exit. */
Victor Stinner01b1cc12019-11-20 02:27:56 +01001247 exit_thread_if_finalizing(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001248
Victor Stinner09532fe2019-05-10 23:39:09 +02001249 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 Py_FatalError("ceval: orphan tstate");
Victor Stinner09532fe2019-05-10 23:39:09 +02001251 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 }
1253 /* Check for asynchronous exceptions. */
1254 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001255 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 tstate->async_exc = NULL;
Victor Stinnere225beb2019-06-03 18:14:24 +02001257 UNSIGNAL_ASYNC_EXC(ceval);
Victor Stinner438a12d2019-05-24 17:01:38 +02001258 _PyErr_SetNone(tstate, exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001259 Py_DECREF(exc);
1260 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 }
1262 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 fast_next_opcode:
1265 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001266
Łukasz Langaa785c872016-09-09 17:37:37 -07001267 if (PyDTrace_LINE_ENABLED())
1268 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001271
Victor Stinnere225beb2019-06-03 18:14:24 +02001272 if (_Py_TracingPossible(ceval) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001273 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001274 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 /* see maybe_call_line_trace
1276 for expository comments */
1277 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 err = maybe_call_line_trace(tstate->c_tracefunc,
1280 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001281 tstate, f,
1282 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 /* Reload possibly changed frame fields */
1284 JUMPTO(f->f_lasti);
1285 if (f->f_stacktop != NULL) {
1286 stack_pointer = f->f_stacktop;
1287 f->f_stacktop = NULL;
1288 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001289 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001291 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001295
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001296 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001297 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001298#ifdef DYNAMIC_EXECUTION_PROFILE
1299#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 dxpairs[lastopcode][opcode]++;
1301 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001302#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001304#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001305
Guido van Rossum96a42c81992-01-12 02:29:51 +00001306#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 if (lltrace) {
1310 if (HAS_ARG(opcode)) {
1311 printf("%d: %d, %d\n",
1312 f->f_lasti, opcode, oparg);
1313 }
1314 else {
1315 printf("%d: %d\n",
1316 f->f_lasti, opcode);
1317 }
1318 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001319#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001324 It is essential that any operation that fails must goto error
1325 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001326
Benjamin Petersonddd19492018-09-16 22:38:02 -07001327 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001329 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001330
Benjamin Petersonddd19492018-09-16 22:38:02 -07001331 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001332 PyObject *value = GETLOCAL(oparg);
1333 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001334 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001335 UNBOUNDLOCAL_ERROR_MSG,
1336 PyTuple_GetItem(co->co_varnames, oparg));
1337 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001339 Py_INCREF(value);
1340 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001342 }
1343
Benjamin Petersonddd19492018-09-16 22:38:02 -07001344 case TARGET(LOAD_CONST): {
1345 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001346 PyObject *value = GETITEM(consts, oparg);
1347 Py_INCREF(value);
1348 PUSH(value);
1349 FAST_DISPATCH();
1350 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001351
Benjamin Petersonddd19492018-09-16 22:38:02 -07001352 case TARGET(STORE_FAST): {
1353 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001354 PyObject *value = POP();
1355 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001357 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001358
Benjamin Petersonddd19492018-09-16 22:38:02 -07001359 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001360 PyObject *value = POP();
1361 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001363 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001364
Benjamin Petersonddd19492018-09-16 22:38:02 -07001365 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001366 PyObject *top = TOP();
1367 PyObject *second = SECOND();
1368 SET_TOP(second);
1369 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001371 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001372
Benjamin Petersonddd19492018-09-16 22:38:02 -07001373 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001374 PyObject *top = TOP();
1375 PyObject *second = SECOND();
1376 PyObject *third = THIRD();
1377 SET_TOP(second);
1378 SET_SECOND(third);
1379 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001381 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001382
Benjamin Petersonddd19492018-09-16 22:38:02 -07001383 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001384 PyObject *top = TOP();
1385 PyObject *second = SECOND();
1386 PyObject *third = THIRD();
1387 PyObject *fourth = FOURTH();
1388 SET_TOP(second);
1389 SET_SECOND(third);
1390 SET_THIRD(fourth);
1391 SET_FOURTH(top);
1392 FAST_DISPATCH();
1393 }
1394
Benjamin Petersonddd19492018-09-16 22:38:02 -07001395 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001396 PyObject *top = TOP();
1397 Py_INCREF(top);
1398 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001400 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001401
Benjamin Petersonddd19492018-09-16 22:38:02 -07001402 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001403 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001404 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001405 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001406 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001407 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001408 SET_TOP(top);
1409 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001410 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001411 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001412
Benjamin Petersonddd19492018-09-16 22:38:02 -07001413 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001414 PyObject *value = TOP();
1415 PyObject *res = PyNumber_Positive(value);
1416 Py_DECREF(value);
1417 SET_TOP(res);
1418 if (res == NULL)
1419 goto error;
1420 DISPATCH();
1421 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001422
Benjamin Petersonddd19492018-09-16 22:38:02 -07001423 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001424 PyObject *value = TOP();
1425 PyObject *res = PyNumber_Negative(value);
1426 Py_DECREF(value);
1427 SET_TOP(res);
1428 if (res == NULL)
1429 goto error;
1430 DISPATCH();
1431 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001432
Benjamin Petersonddd19492018-09-16 22:38:02 -07001433 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001434 PyObject *value = TOP();
1435 int err = PyObject_IsTrue(value);
1436 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 if (err == 0) {
1438 Py_INCREF(Py_True);
1439 SET_TOP(Py_True);
1440 DISPATCH();
1441 }
1442 else if (err > 0) {
1443 Py_INCREF(Py_False);
1444 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001445 DISPATCH();
1446 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001447 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001448 goto error;
1449 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001450
Benjamin Petersonddd19492018-09-16 22:38:02 -07001451 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001452 PyObject *value = TOP();
1453 PyObject *res = PyNumber_Invert(value);
1454 Py_DECREF(value);
1455 SET_TOP(res);
1456 if (res == NULL)
1457 goto error;
1458 DISPATCH();
1459 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001460
Benjamin Petersonddd19492018-09-16 22:38:02 -07001461 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001462 PyObject *exp = POP();
1463 PyObject *base = TOP();
1464 PyObject *res = PyNumber_Power(base, exp, Py_None);
1465 Py_DECREF(base);
1466 Py_DECREF(exp);
1467 SET_TOP(res);
1468 if (res == NULL)
1469 goto error;
1470 DISPATCH();
1471 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001472
Benjamin Petersonddd19492018-09-16 22:38:02 -07001473 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001474 PyObject *right = POP();
1475 PyObject *left = TOP();
1476 PyObject *res = PyNumber_Multiply(left, right);
1477 Py_DECREF(left);
1478 Py_DECREF(right);
1479 SET_TOP(res);
1480 if (res == NULL)
1481 goto error;
1482 DISPATCH();
1483 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001484
Benjamin Petersonddd19492018-09-16 22:38:02 -07001485 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001486 PyObject *right = POP();
1487 PyObject *left = TOP();
1488 PyObject *res = PyNumber_MatrixMultiply(left, right);
1489 Py_DECREF(left);
1490 Py_DECREF(right);
1491 SET_TOP(res);
1492 if (res == NULL)
1493 goto error;
1494 DISPATCH();
1495 }
1496
Benjamin Petersonddd19492018-09-16 22:38:02 -07001497 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001498 PyObject *divisor = POP();
1499 PyObject *dividend = TOP();
1500 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1501 Py_DECREF(dividend);
1502 Py_DECREF(divisor);
1503 SET_TOP(quotient);
1504 if (quotient == NULL)
1505 goto error;
1506 DISPATCH();
1507 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001508
Benjamin Petersonddd19492018-09-16 22:38:02 -07001509 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001510 PyObject *divisor = POP();
1511 PyObject *dividend = TOP();
1512 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1513 Py_DECREF(dividend);
1514 Py_DECREF(divisor);
1515 SET_TOP(quotient);
1516 if (quotient == NULL)
1517 goto error;
1518 DISPATCH();
1519 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001520
Benjamin Petersonddd19492018-09-16 22:38:02 -07001521 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001522 PyObject *divisor = POP();
1523 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001524 PyObject *res;
1525 if (PyUnicode_CheckExact(dividend) && (
1526 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1527 // fast path; string formatting, but not if the RHS is a str subclass
1528 // (see issue28598)
1529 res = PyUnicode_Format(dividend, divisor);
1530 } else {
1531 res = PyNumber_Remainder(dividend, divisor);
1532 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001533 Py_DECREF(divisor);
1534 Py_DECREF(dividend);
1535 SET_TOP(res);
1536 if (res == NULL)
1537 goto error;
1538 DISPATCH();
1539 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001540
Benjamin Petersonddd19492018-09-16 22:38:02 -07001541 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001542 PyObject *right = POP();
1543 PyObject *left = TOP();
1544 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001545 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1546 CPython using bytecode, it is simply worthless.
1547 See http://bugs.python.org/issue21955 and
1548 http://bugs.python.org/issue10044 for the discussion. In short,
1549 no patch shown any impact on a realistic benchmark, only a minor
1550 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001551 if (PyUnicode_CheckExact(left) &&
1552 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001553 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001554 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001555 }
1556 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001557 sum = PyNumber_Add(left, right);
1558 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001559 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001560 Py_DECREF(right);
1561 SET_TOP(sum);
1562 if (sum == NULL)
1563 goto error;
1564 DISPATCH();
1565 }
1566
Benjamin Petersonddd19492018-09-16 22:38:02 -07001567 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001568 PyObject *right = POP();
1569 PyObject *left = TOP();
1570 PyObject *diff = PyNumber_Subtract(left, right);
1571 Py_DECREF(right);
1572 Py_DECREF(left);
1573 SET_TOP(diff);
1574 if (diff == NULL)
1575 goto error;
1576 DISPATCH();
1577 }
1578
Benjamin Petersonddd19492018-09-16 22:38:02 -07001579 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001580 PyObject *sub = POP();
1581 PyObject *container = TOP();
1582 PyObject *res = PyObject_GetItem(container, sub);
1583 Py_DECREF(container);
1584 Py_DECREF(sub);
1585 SET_TOP(res);
1586 if (res == NULL)
1587 goto error;
1588 DISPATCH();
1589 }
1590
Benjamin Petersonddd19492018-09-16 22:38:02 -07001591 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001592 PyObject *right = POP();
1593 PyObject *left = TOP();
1594 PyObject *res = PyNumber_Lshift(left, right);
1595 Py_DECREF(left);
1596 Py_DECREF(right);
1597 SET_TOP(res);
1598 if (res == NULL)
1599 goto error;
1600 DISPATCH();
1601 }
1602
Benjamin Petersonddd19492018-09-16 22:38:02 -07001603 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001604 PyObject *right = POP();
1605 PyObject *left = TOP();
1606 PyObject *res = PyNumber_Rshift(left, right);
1607 Py_DECREF(left);
1608 Py_DECREF(right);
1609 SET_TOP(res);
1610 if (res == NULL)
1611 goto error;
1612 DISPATCH();
1613 }
1614
Benjamin Petersonddd19492018-09-16 22:38:02 -07001615 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001616 PyObject *right = POP();
1617 PyObject *left = TOP();
1618 PyObject *res = PyNumber_And(left, right);
1619 Py_DECREF(left);
1620 Py_DECREF(right);
1621 SET_TOP(res);
1622 if (res == NULL)
1623 goto error;
1624 DISPATCH();
1625 }
1626
Benjamin Petersonddd19492018-09-16 22:38:02 -07001627 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001628 PyObject *right = POP();
1629 PyObject *left = TOP();
1630 PyObject *res = PyNumber_Xor(left, right);
1631 Py_DECREF(left);
1632 Py_DECREF(right);
1633 SET_TOP(res);
1634 if (res == NULL)
1635 goto error;
1636 DISPATCH();
1637 }
1638
Benjamin Petersonddd19492018-09-16 22:38:02 -07001639 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001640 PyObject *right = POP();
1641 PyObject *left = TOP();
1642 PyObject *res = PyNumber_Or(left, right);
1643 Py_DECREF(left);
1644 Py_DECREF(right);
1645 SET_TOP(res);
1646 if (res == NULL)
1647 goto error;
1648 DISPATCH();
1649 }
1650
Benjamin Petersonddd19492018-09-16 22:38:02 -07001651 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001652 PyObject *v = POP();
1653 PyObject *list = PEEK(oparg);
1654 int err;
1655 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001657 if (err != 0)
1658 goto error;
1659 PREDICT(JUMP_ABSOLUTE);
1660 DISPATCH();
1661 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001662
Benjamin Petersonddd19492018-09-16 22:38:02 -07001663 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001664 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001665 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001666 int err;
1667 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001668 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001669 if (err != 0)
1670 goto error;
1671 PREDICT(JUMP_ABSOLUTE);
1672 DISPATCH();
1673 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001674
Benjamin Petersonddd19492018-09-16 22:38:02 -07001675 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001676 PyObject *exp = POP();
1677 PyObject *base = TOP();
1678 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1679 Py_DECREF(base);
1680 Py_DECREF(exp);
1681 SET_TOP(res);
1682 if (res == NULL)
1683 goto error;
1684 DISPATCH();
1685 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001686
Benjamin Petersonddd19492018-09-16 22:38:02 -07001687 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001688 PyObject *right = POP();
1689 PyObject *left = TOP();
1690 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1691 Py_DECREF(left);
1692 Py_DECREF(right);
1693 SET_TOP(res);
1694 if (res == NULL)
1695 goto error;
1696 DISPATCH();
1697 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001698
Benjamin Petersonddd19492018-09-16 22:38:02 -07001699 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001700 PyObject *right = POP();
1701 PyObject *left = TOP();
1702 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1703 Py_DECREF(left);
1704 Py_DECREF(right);
1705 SET_TOP(res);
1706 if (res == NULL)
1707 goto error;
1708 DISPATCH();
1709 }
1710
Benjamin Petersonddd19492018-09-16 22:38:02 -07001711 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001712 PyObject *divisor = POP();
1713 PyObject *dividend = TOP();
1714 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1715 Py_DECREF(dividend);
1716 Py_DECREF(divisor);
1717 SET_TOP(quotient);
1718 if (quotient == 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(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001724 PyObject *divisor = POP();
1725 PyObject *dividend = TOP();
1726 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1727 Py_DECREF(dividend);
1728 Py_DECREF(divisor);
1729 SET_TOP(quotient);
1730 if (quotient == 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(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001736 PyObject *right = POP();
1737 PyObject *left = TOP();
1738 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1739 Py_DECREF(left);
1740 Py_DECREF(right);
1741 SET_TOP(mod);
1742 if (mod == NULL)
1743 goto error;
1744 DISPATCH();
1745 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001746
Benjamin Petersonddd19492018-09-16 22:38:02 -07001747 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001748 PyObject *right = POP();
1749 PyObject *left = TOP();
1750 PyObject *sum;
1751 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001752 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001753 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001754 }
1755 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001756 sum = PyNumber_InPlaceAdd(left, right);
1757 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001758 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001759 Py_DECREF(right);
1760 SET_TOP(sum);
1761 if (sum == NULL)
1762 goto error;
1763 DISPATCH();
1764 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001765
Benjamin Petersonddd19492018-09-16 22:38:02 -07001766 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001767 PyObject *right = POP();
1768 PyObject *left = TOP();
1769 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1770 Py_DECREF(left);
1771 Py_DECREF(right);
1772 SET_TOP(diff);
1773 if (diff == NULL)
1774 goto error;
1775 DISPATCH();
1776 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001777
Benjamin Petersonddd19492018-09-16 22:38:02 -07001778 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001779 PyObject *right = POP();
1780 PyObject *left = TOP();
1781 PyObject *res = PyNumber_InPlaceLshift(left, right);
1782 Py_DECREF(left);
1783 Py_DECREF(right);
1784 SET_TOP(res);
1785 if (res == NULL)
1786 goto error;
1787 DISPATCH();
1788 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001789
Benjamin Petersonddd19492018-09-16 22:38:02 -07001790 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001791 PyObject *right = POP();
1792 PyObject *left = TOP();
1793 PyObject *res = PyNumber_InPlaceRshift(left, right);
1794 Py_DECREF(left);
1795 Py_DECREF(right);
1796 SET_TOP(res);
1797 if (res == NULL)
1798 goto error;
1799 DISPATCH();
1800 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001801
Benjamin Petersonddd19492018-09-16 22:38:02 -07001802 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001803 PyObject *right = POP();
1804 PyObject *left = TOP();
1805 PyObject *res = PyNumber_InPlaceAnd(left, right);
1806 Py_DECREF(left);
1807 Py_DECREF(right);
1808 SET_TOP(res);
1809 if (res == NULL)
1810 goto error;
1811 DISPATCH();
1812 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001813
Benjamin Petersonddd19492018-09-16 22:38:02 -07001814 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001815 PyObject *right = POP();
1816 PyObject *left = TOP();
1817 PyObject *res = PyNumber_InPlaceXor(left, right);
1818 Py_DECREF(left);
1819 Py_DECREF(right);
1820 SET_TOP(res);
1821 if (res == NULL)
1822 goto error;
1823 DISPATCH();
1824 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001825
Benjamin Petersonddd19492018-09-16 22:38:02 -07001826 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001827 PyObject *right = POP();
1828 PyObject *left = TOP();
1829 PyObject *res = PyNumber_InPlaceOr(left, right);
1830 Py_DECREF(left);
1831 Py_DECREF(right);
1832 SET_TOP(res);
1833 if (res == NULL)
1834 goto error;
1835 DISPATCH();
1836 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001837
Benjamin Petersonddd19492018-09-16 22:38:02 -07001838 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001839 PyObject *sub = TOP();
1840 PyObject *container = SECOND();
1841 PyObject *v = THIRD();
1842 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001843 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001844 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001845 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001846 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001847 Py_DECREF(container);
1848 Py_DECREF(sub);
1849 if (err != 0)
1850 goto error;
1851 DISPATCH();
1852 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001853
Benjamin Petersonddd19492018-09-16 22:38:02 -07001854 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001855 PyObject *sub = TOP();
1856 PyObject *container = SECOND();
1857 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001858 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00001859 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001860 err = PyObject_DelItem(container, sub);
1861 Py_DECREF(container);
1862 Py_DECREF(sub);
1863 if (err != 0)
1864 goto error;
1865 DISPATCH();
1866 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001867
Benjamin Petersonddd19492018-09-16 22:38:02 -07001868 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01001869 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001870 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001871 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001872 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001873 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001874 _PyErr_SetString(tstate, PyExc_RuntimeError,
1875 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001876 Py_DECREF(value);
1877 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001878 }
Petr Viktorinffd97532020-02-11 17:46:57 +01001879 res = PyObject_CallOneArg(hook, value);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001880 Py_DECREF(value);
1881 if (res == NULL)
1882 goto error;
1883 Py_DECREF(res);
1884 DISPATCH();
1885 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001886
Benjamin Petersonddd19492018-09-16 22:38:02 -07001887 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001888 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001889 switch (oparg) {
1890 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001891 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001892 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001893 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001894 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001895 /* fall through */
1896 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02001897 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001898 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001899 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900 break;
1901 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02001902 _PyErr_SetString(tstate, PyExc_SystemError,
1903 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001904 break;
1905 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001906 goto error;
1907 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001908
Benjamin Petersonddd19492018-09-16 22:38:02 -07001909 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001910 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001911 assert(f->f_iblock == 0);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00001912 assert(EMPTY());
1913 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001914 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001915
Benjamin Petersonddd19492018-09-16 22:38:02 -07001916 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001917 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001918 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001919 PyObject *obj = TOP();
1920 PyTypeObject *type = Py_TYPE(obj);
1921
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001922 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001923 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001924 }
Yury Selivanov75445082015-05-11 22:57:16 -04001925
1926 if (getter != NULL) {
1927 iter = (*getter)(obj);
1928 Py_DECREF(obj);
1929 if (iter == NULL) {
1930 SET_TOP(NULL);
1931 goto error;
1932 }
1933 }
1934 else {
1935 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02001936 _PyErr_Format(tstate, PyExc_TypeError,
1937 "'async for' requires an object with "
1938 "__aiter__ method, got %.100s",
1939 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001940 Py_DECREF(obj);
1941 goto error;
1942 }
1943
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001944 if (Py_TYPE(iter)->tp_as_async == NULL ||
1945 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001946
Yury Selivanov398ff912017-03-02 22:20:00 -05001947 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02001948 _PyErr_Format(tstate, PyExc_TypeError,
1949 "'async for' received an object from __aiter__ "
1950 "that does not implement __anext__: %.100s",
1951 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001952 Py_DECREF(iter);
1953 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001954 }
1955
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001956 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001957 DISPATCH();
1958 }
1959
Benjamin Petersonddd19492018-09-16 22:38:02 -07001960 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001961 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001962 PyObject *next_iter = NULL;
1963 PyObject *awaitable = NULL;
1964 PyObject *aiter = TOP();
1965 PyTypeObject *type = Py_TYPE(aiter);
1966
Yury Selivanoveb636452016-09-08 22:01:51 -07001967 if (PyAsyncGen_CheckExact(aiter)) {
1968 awaitable = type->tp_as_async->am_anext(aiter);
1969 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001970 goto error;
1971 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001972 } else {
1973 if (type->tp_as_async != NULL){
1974 getter = type->tp_as_async->am_anext;
1975 }
Yury Selivanov75445082015-05-11 22:57:16 -04001976
Yury Selivanoveb636452016-09-08 22:01:51 -07001977 if (getter != NULL) {
1978 next_iter = (*getter)(aiter);
1979 if (next_iter == NULL) {
1980 goto error;
1981 }
1982 }
1983 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02001984 _PyErr_Format(tstate, PyExc_TypeError,
1985 "'async for' requires an iterator with "
1986 "__anext__ method, got %.100s",
1987 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07001988 goto error;
1989 }
Yury Selivanov75445082015-05-11 22:57:16 -04001990
Yury Selivanoveb636452016-09-08 22:01:51 -07001991 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1992 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001993 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001994 PyExc_TypeError,
1995 "'async for' received an invalid object "
1996 "from __anext__: %.100s",
1997 Py_TYPE(next_iter)->tp_name);
1998
1999 Py_DECREF(next_iter);
2000 goto error;
2001 } else {
2002 Py_DECREF(next_iter);
2003 }
2004 }
Yury Selivanov75445082015-05-11 22:57:16 -04002005
2006 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002007 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002008 DISPATCH();
2009 }
2010
Benjamin Petersonddd19492018-09-16 22:38:02 -07002011 case TARGET(GET_AWAITABLE): {
2012 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002013 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002014 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002015
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002016 if (iter == NULL) {
Mark Shannonfee55262019-11-21 09:11:43 +00002017 int opcode_at_minus_3 = 0;
2018 if ((next_instr - first_instr) > 2) {
2019 opcode_at_minus_3 = _Py_OPCODE(next_instr[-3]);
2020 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002021 format_awaitable_error(tstate, Py_TYPE(iterable),
Mark Shannonfee55262019-11-21 09:11:43 +00002022 opcode_at_minus_3,
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002023 _Py_OPCODE(next_instr[-2]));
2024 }
2025
Yury Selivanov75445082015-05-11 22:57:16 -04002026 Py_DECREF(iterable);
2027
Yury Selivanovc724bae2016-03-02 11:30:46 -05002028 if (iter != NULL && PyCoro_CheckExact(iter)) {
2029 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2030 if (yf != NULL) {
2031 /* `iter` is a coroutine object that is being
2032 awaited, `yf` is a pointer to the current awaitable
2033 being awaited on. */
2034 Py_DECREF(yf);
2035 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002036 _PyErr_SetString(tstate, PyExc_RuntimeError,
2037 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002038 /* The code below jumps to `error` if `iter` is NULL. */
2039 }
2040 }
2041
Yury Selivanov75445082015-05-11 22:57:16 -04002042 SET_TOP(iter); /* Even if it's NULL */
2043
2044 if (iter == NULL) {
2045 goto error;
2046 }
2047
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002048 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002049 DISPATCH();
2050 }
2051
Benjamin Petersonddd19492018-09-16 22:38:02 -07002052 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002053 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002054 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002055 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002056 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
2057 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002058 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002059 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002060 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002061 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002062 else
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02002063 retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002064 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002065 Py_DECREF(v);
2066 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002067 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002068 if (tstate->c_tracefunc != NULL
Victor Stinner438a12d2019-05-24 17:01:38 +02002069 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002070 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002071 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002072 if (err < 0)
2073 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002074 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002075 SET_TOP(val);
2076 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002077 }
Martin Panter95f53c12016-07-18 08:23:26 +00002078 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002079 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002080 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002081 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002082 f->f_lasti -= sizeof(_Py_CODEUNIT);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002083 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002084 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002085
Benjamin Petersonddd19492018-09-16 22:38:02 -07002086 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002087 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002088
2089 if (co->co_flags & CO_ASYNC_GENERATOR) {
2090 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2091 Py_DECREF(retval);
2092 if (w == NULL) {
2093 retval = NULL;
2094 goto error;
2095 }
2096 retval = w;
2097 }
2098
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002099 f->f_stacktop = stack_pointer;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002100 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002101 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002102
Benjamin Petersonddd19492018-09-16 22:38:02 -07002103 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002104 PyObject *type, *value, *traceback;
2105 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002106 PyTryBlock *b = PyFrame_BlockPop(f);
2107 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002108 _PyErr_SetString(tstate, PyExc_SystemError,
2109 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002110 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002111 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002112 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2113 STACK_LEVEL() <= (b)->b_level + 4);
2114 exc_info = tstate->exc_info;
2115 type = exc_info->exc_type;
2116 value = exc_info->exc_value;
2117 traceback = exc_info->exc_traceback;
2118 exc_info->exc_type = POP();
2119 exc_info->exc_value = POP();
2120 exc_info->exc_traceback = POP();
2121 Py_XDECREF(type);
2122 Py_XDECREF(value);
2123 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002124 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002125 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002126
Benjamin Petersonddd19492018-09-16 22:38:02 -07002127 case TARGET(POP_BLOCK): {
2128 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002129 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002130 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002131 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002132
Mark Shannonfee55262019-11-21 09:11:43 +00002133 case TARGET(RERAISE): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002134 PyObject *exc = POP();
Mark Shannonfee55262019-11-21 09:11:43 +00002135 PyObject *val = POP();
2136 PyObject *tb = POP();
2137 assert(PyExceptionClass_Check(exc));
Victor Stinner61f4db82020-01-28 03:37:45 +01002138 _PyErr_Restore(tstate, exc, val, tb);
Mark Shannonfee55262019-11-21 09:11:43 +00002139 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002140 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002141
Benjamin Petersonddd19492018-09-16 22:38:02 -07002142 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002143 PyObject *exc = POP();
2144 assert(PyExceptionClass_Check(exc));
2145 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2146 PyTryBlock *b = PyFrame_BlockPop(f);
2147 assert(b->b_type == EXCEPT_HANDLER);
2148 Py_DECREF(exc);
2149 UNWIND_EXCEPT_HANDLER(b);
2150 Py_DECREF(POP());
2151 JUMPBY(oparg);
2152 FAST_DISPATCH();
2153 }
2154 else {
2155 PyObject *val = POP();
2156 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002157 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002158 goto exception_unwind;
2159 }
2160 }
2161
Zackery Spytzce6a0702019-08-25 03:44:09 -06002162 case TARGET(LOAD_ASSERTION_ERROR): {
2163 PyObject *value = PyExc_AssertionError;
2164 Py_INCREF(value);
2165 PUSH(value);
2166 FAST_DISPATCH();
2167 }
2168
Benjamin Petersonddd19492018-09-16 22:38:02 -07002169 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002170 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002171
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002172 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002173 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002174 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002175 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002176 if (!_PyErr_Occurred(tstate)) {
2177 _PyErr_SetString(tstate, PyExc_NameError,
2178 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002179 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002180 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002181 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002182 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002183 }
2184 else {
2185 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2186 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002187 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002188 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2189 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002190 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2191 _PyErr_SetString(tstate, PyExc_NameError,
2192 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002193 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002194 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002195 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002196 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002197 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002198 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002199
Benjamin Petersonddd19492018-09-16 22:38:02 -07002200 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002201 PyObject *name = GETITEM(names, oparg);
2202 PyObject *v = POP();
2203 PyObject *ns = f->f_locals;
2204 int err;
2205 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002206 _PyErr_Format(tstate, PyExc_SystemError,
2207 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002208 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002209 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002210 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002211 if (PyDict_CheckExact(ns))
2212 err = PyDict_SetItem(ns, name, v);
2213 else
2214 err = PyObject_SetItem(ns, name, v);
2215 Py_DECREF(v);
2216 if (err != 0)
2217 goto error;
2218 DISPATCH();
2219 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002220
Benjamin Petersonddd19492018-09-16 22:38:02 -07002221 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002222 PyObject *name = GETITEM(names, oparg);
2223 PyObject *ns = f->f_locals;
2224 int err;
2225 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002226 _PyErr_Format(tstate, PyExc_SystemError,
2227 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002228 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002229 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002230 err = PyObject_DelItem(ns, name);
2231 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002232 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002233 NAME_ERROR_MSG,
2234 name);
2235 goto error;
2236 }
2237 DISPATCH();
2238 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002239
Benjamin Petersonddd19492018-09-16 22:38:02 -07002240 case TARGET(UNPACK_SEQUENCE): {
2241 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002242 PyObject *seq = POP(), *item, **items;
2243 if (PyTuple_CheckExact(seq) &&
2244 PyTuple_GET_SIZE(seq) == oparg) {
2245 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002246 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002247 item = items[oparg];
2248 Py_INCREF(item);
2249 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002250 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002251 } else if (PyList_CheckExact(seq) &&
2252 PyList_GET_SIZE(seq) == oparg) {
2253 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002254 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002255 item = items[oparg];
2256 Py_INCREF(item);
2257 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002258 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002259 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002260 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002261 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002262 } else {
2263 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002264 Py_DECREF(seq);
2265 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002266 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002267 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002268 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002269 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002270
Benjamin Petersonddd19492018-09-16 22:38:02 -07002271 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002272 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2273 PyObject *seq = POP();
2274
Victor Stinner438a12d2019-05-24 17:01:38 +02002275 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002276 stack_pointer + totalargs)) {
2277 stack_pointer += totalargs;
2278 } else {
2279 Py_DECREF(seq);
2280 goto error;
2281 }
2282 Py_DECREF(seq);
2283 DISPATCH();
2284 }
2285
Benjamin Petersonddd19492018-09-16 22:38:02 -07002286 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002287 PyObject *name = GETITEM(names, oparg);
2288 PyObject *owner = TOP();
2289 PyObject *v = SECOND();
2290 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002291 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002292 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002293 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002294 Py_DECREF(owner);
2295 if (err != 0)
2296 goto error;
2297 DISPATCH();
2298 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002299
Benjamin Petersonddd19492018-09-16 22:38:02 -07002300 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002301 PyObject *name = GETITEM(names, oparg);
2302 PyObject *owner = POP();
2303 int err;
2304 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2305 Py_DECREF(owner);
2306 if (err != 0)
2307 goto error;
2308 DISPATCH();
2309 }
2310
Benjamin Petersonddd19492018-09-16 22:38:02 -07002311 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002312 PyObject *name = GETITEM(names, oparg);
2313 PyObject *v = POP();
2314 int err;
2315 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002316 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002317 if (err != 0)
2318 goto error;
2319 DISPATCH();
2320 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002321
Benjamin Petersonddd19492018-09-16 22:38:02 -07002322 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002323 PyObject *name = GETITEM(names, oparg);
2324 int err;
2325 err = PyDict_DelItem(f->f_globals, name);
2326 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002327 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2328 format_exc_check_arg(tstate, PyExc_NameError,
2329 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002330 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002331 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002332 }
2333 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002334 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002335
Benjamin Petersonddd19492018-09-16 22:38:02 -07002336 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002337 PyObject *name = GETITEM(names, oparg);
2338 PyObject *locals = f->f_locals;
2339 PyObject *v;
2340 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002341 _PyErr_Format(tstate, PyExc_SystemError,
2342 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002343 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002344 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002345 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002346 v = PyDict_GetItemWithError(locals, name);
2347 if (v != NULL) {
2348 Py_INCREF(v);
2349 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002350 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002351 goto error;
2352 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002353 }
2354 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002355 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002356 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002357 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002358 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002359 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002360 }
2361 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002362 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002363 v = PyDict_GetItemWithError(f->f_globals, name);
2364 if (v != NULL) {
2365 Py_INCREF(v);
2366 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002367 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002368 goto error;
2369 }
2370 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002371 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002372 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002373 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002374 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002375 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002376 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002377 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002378 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002379 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002380 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002381 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002382 }
2383 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002384 v = PyObject_GetItem(f->f_builtins, name);
2385 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002386 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002387 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002388 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002389 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002390 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002391 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002392 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002393 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002394 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002396 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002397 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002398 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002399
Benjamin Petersonddd19492018-09-16 22:38:02 -07002400 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002401 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002402 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002403 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002404 && PyDict_CheckExact(f->f_builtins))
2405 {
Inada Naoki91234a12019-06-03 21:30:58 +09002406 OPCACHE_CHECK();
2407 if (co_opcache != NULL && co_opcache->optimized > 0) {
2408 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2409
2410 if (lg->globals_ver ==
2411 ((PyDictObject *)f->f_globals)->ma_version_tag
2412 && lg->builtins_ver ==
2413 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2414 {
2415 PyObject *ptr = lg->ptr;
2416 OPCACHE_STAT_GLOBAL_HIT();
2417 assert(ptr != NULL);
2418 Py_INCREF(ptr);
2419 PUSH(ptr);
2420 DISPATCH();
2421 }
2422 }
2423
2424 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002425 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002426 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002427 name);
2428 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002429 if (!_PyErr_OCCURRED()) {
2430 /* _PyDict_LoadGlobal() returns NULL without raising
2431 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002432 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002433 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002434 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002435 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002436 }
Inada Naoki91234a12019-06-03 21:30:58 +09002437
2438 if (co_opcache != NULL) {
2439 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2440
2441 if (co_opcache->optimized == 0) {
2442 /* Wasn't optimized before. */
2443 OPCACHE_STAT_GLOBAL_OPT();
2444 } else {
2445 OPCACHE_STAT_GLOBAL_MISS();
2446 }
2447
2448 co_opcache->optimized = 1;
2449 lg->globals_ver =
2450 ((PyDictObject *)f->f_globals)->ma_version_tag;
2451 lg->builtins_ver =
2452 ((PyDictObject *)f->f_builtins)->ma_version_tag;
2453 lg->ptr = v; /* borrowed */
2454 }
2455
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002456 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002457 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002458 else {
2459 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002460
2461 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09002462 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002463 v = PyObject_GetItem(f->f_globals, name);
2464 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002465 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002466 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002467 }
2468 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002469
Victor Stinnerb4efc962015-11-20 09:24:02 +01002470 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002471 v = PyObject_GetItem(f->f_builtins, name);
2472 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002473 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002474 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002475 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002476 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002477 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002478 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002479 }
2480 }
2481 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002482 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002483 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002484 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002485
Benjamin Petersonddd19492018-09-16 22:38:02 -07002486 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002487 PyObject *v = GETLOCAL(oparg);
2488 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002489 SETLOCAL(oparg, NULL);
2490 DISPATCH();
2491 }
2492 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002493 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002494 UNBOUNDLOCAL_ERROR_MSG,
2495 PyTuple_GetItem(co->co_varnames, oparg)
2496 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002497 goto error;
2498 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002499
Benjamin Petersonddd19492018-09-16 22:38:02 -07002500 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002501 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002502 PyObject *oldobj = PyCell_GET(cell);
2503 if (oldobj != NULL) {
2504 PyCell_SET(cell, NULL);
2505 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002506 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002507 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002508 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002509 goto error;
2510 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002511
Benjamin Petersonddd19492018-09-16 22:38:02 -07002512 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002513 PyObject *cell = freevars[oparg];
2514 Py_INCREF(cell);
2515 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002516 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002517 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002518
Benjamin Petersonddd19492018-09-16 22:38:02 -07002519 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002520 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002521 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002522 assert(locals);
2523 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2524 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2525 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2526 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2527 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002528 value = PyDict_GetItemWithError(locals, name);
2529 if (value != NULL) {
2530 Py_INCREF(value);
2531 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002532 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002533 goto error;
2534 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002535 }
2536 else {
2537 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002538 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002539 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002540 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002541 }
2542 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002543 }
2544 }
2545 if (!value) {
2546 PyObject *cell = freevars[oparg];
2547 value = PyCell_GET(cell);
2548 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002549 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002550 goto error;
2551 }
2552 Py_INCREF(value);
2553 }
2554 PUSH(value);
2555 DISPATCH();
2556 }
2557
Benjamin Petersonddd19492018-09-16 22:38:02 -07002558 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002559 PyObject *cell = freevars[oparg];
2560 PyObject *value = PyCell_GET(cell);
2561 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002562 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002563 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002564 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002565 Py_INCREF(value);
2566 PUSH(value);
2567 DISPATCH();
2568 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002569
Benjamin Petersonddd19492018-09-16 22:38:02 -07002570 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002571 PyObject *v = POP();
2572 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002573 PyObject *oldobj = PyCell_GET(cell);
2574 PyCell_SET(cell, v);
2575 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002576 DISPATCH();
2577 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002578
Benjamin Petersonddd19492018-09-16 22:38:02 -07002579 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002580 PyObject *str;
2581 PyObject *empty = PyUnicode_New(0, 0);
2582 if (empty == NULL) {
2583 goto error;
2584 }
2585 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2586 Py_DECREF(empty);
2587 if (str == NULL)
2588 goto error;
2589 while (--oparg >= 0) {
2590 PyObject *item = POP();
2591 Py_DECREF(item);
2592 }
2593 PUSH(str);
2594 DISPATCH();
2595 }
2596
Benjamin Petersonddd19492018-09-16 22:38:02 -07002597 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002598 PyObject *tup = PyTuple_New(oparg);
2599 if (tup == NULL)
2600 goto error;
2601 while (--oparg >= 0) {
2602 PyObject *item = POP();
2603 PyTuple_SET_ITEM(tup, oparg, item);
2604 }
2605 PUSH(tup);
2606 DISPATCH();
2607 }
2608
Benjamin Petersonddd19492018-09-16 22:38:02 -07002609 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002610 PyObject *list = PyList_New(oparg);
2611 if (list == NULL)
2612 goto error;
2613 while (--oparg >= 0) {
2614 PyObject *item = POP();
2615 PyList_SET_ITEM(list, oparg, item);
2616 }
2617 PUSH(list);
2618 DISPATCH();
2619 }
2620
Mark Shannon13bc1392020-01-23 09:25:17 +00002621 case TARGET(LIST_TO_TUPLE): {
2622 PyObject *list = POP();
2623 PyObject *tuple = PyList_AsTuple(list);
2624 Py_DECREF(list);
2625 if (tuple == NULL) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002626 goto error;
Mark Shannon13bc1392020-01-23 09:25:17 +00002627 }
2628 PUSH(tuple);
2629 DISPATCH();
2630 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002631
Mark Shannon13bc1392020-01-23 09:25:17 +00002632 case TARGET(LIST_EXTEND): {
2633 PyObject *iterable = POP();
2634 PyObject *list = PEEK(oparg);
2635 PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
2636 if (none_val == NULL) {
2637 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01002638 (Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable)))
Mark Shannon13bc1392020-01-23 09:25:17 +00002639 {
Victor Stinner61f4db82020-01-28 03:37:45 +01002640 _PyErr_Clear(tstate);
Mark Shannon13bc1392020-01-23 09:25:17 +00002641 _PyErr_Format(tstate, PyExc_TypeError,
2642 "Value after * must be an iterable, not %.200s",
2643 Py_TYPE(iterable)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002644 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002645 Py_DECREF(iterable);
2646 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002647 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002648 Py_DECREF(none_val);
2649 Py_DECREF(iterable);
2650 DISPATCH();
2651 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002652
Mark Shannon13bc1392020-01-23 09:25:17 +00002653 case TARGET(SET_UPDATE): {
2654 PyObject *iterable = POP();
2655 PyObject *set = PEEK(oparg);
2656 int err = _PySet_Update(set, iterable);
2657 Py_DECREF(iterable);
2658 if (err < 0) {
2659 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002660 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002661 DISPATCH();
2662 }
2663
Benjamin Petersonddd19492018-09-16 22:38:02 -07002664 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002665 PyObject *set = PySet_New(NULL);
2666 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002667 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002668 if (set == NULL)
2669 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002670 for (i = oparg; i > 0; i--) {
2671 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002672 if (err == 0)
2673 err = PySet_Add(set, item);
2674 Py_DECREF(item);
2675 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002676 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002677 if (err != 0) {
2678 Py_DECREF(set);
2679 goto error;
2680 }
2681 PUSH(set);
2682 DISPATCH();
2683 }
2684
Benjamin Petersonddd19492018-09-16 22:38:02 -07002685 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002686 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002687 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2688 if (map == NULL)
2689 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002690 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002691 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002692 PyObject *key = PEEK(2*i);
2693 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002694 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002695 if (err != 0) {
2696 Py_DECREF(map);
2697 goto error;
2698 }
2699 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002700
2701 while (oparg--) {
2702 Py_DECREF(POP());
2703 Py_DECREF(POP());
2704 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002705 PUSH(map);
2706 DISPATCH();
2707 }
2708
Benjamin Petersonddd19492018-09-16 22:38:02 -07002709 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002710 _Py_IDENTIFIER(__annotations__);
2711 int err;
2712 PyObject *ann_dict;
2713 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002714 _PyErr_Format(tstate, PyExc_SystemError,
2715 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002716 goto error;
2717 }
2718 /* check if __annotations__ in locals()... */
2719 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002720 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002721 &PyId___annotations__);
2722 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002723 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002724 goto error;
2725 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002726 /* ...if not, create a new one */
2727 ann_dict = PyDict_New();
2728 if (ann_dict == NULL) {
2729 goto error;
2730 }
2731 err = _PyDict_SetItemId(f->f_locals,
2732 &PyId___annotations__, ann_dict);
2733 Py_DECREF(ann_dict);
2734 if (err != 0) {
2735 goto error;
2736 }
2737 }
2738 }
2739 else {
2740 /* do the same if locals() is not a dict */
2741 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2742 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002743 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002744 }
2745 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2746 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002747 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002748 goto error;
2749 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002750 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002751 ann_dict = PyDict_New();
2752 if (ann_dict == NULL) {
2753 goto error;
2754 }
2755 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2756 Py_DECREF(ann_dict);
2757 if (err != 0) {
2758 goto error;
2759 }
2760 }
2761 else {
2762 Py_DECREF(ann_dict);
2763 }
2764 }
2765 DISPATCH();
2766 }
2767
Benjamin Petersonddd19492018-09-16 22:38:02 -07002768 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002769 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002770 PyObject *map;
2771 PyObject *keys = TOP();
2772 if (!PyTuple_CheckExact(keys) ||
2773 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002774 _PyErr_SetString(tstate, PyExc_SystemError,
2775 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002776 goto error;
2777 }
2778 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2779 if (map == NULL) {
2780 goto error;
2781 }
2782 for (i = oparg; i > 0; i--) {
2783 int err;
2784 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2785 PyObject *value = PEEK(i + 1);
2786 err = PyDict_SetItem(map, key, value);
2787 if (err != 0) {
2788 Py_DECREF(map);
2789 goto error;
2790 }
2791 }
2792
2793 Py_DECREF(POP());
2794 while (oparg--) {
2795 Py_DECREF(POP());
2796 }
2797 PUSH(map);
2798 DISPATCH();
2799 }
2800
Mark Shannon8a4cd702020-01-27 09:57:45 +00002801 case TARGET(DICT_UPDATE): {
2802 PyObject *update = POP();
2803 PyObject *dict = PEEK(oparg);
2804 if (PyDict_Update(dict, update) < 0) {
2805 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
2806 _PyErr_Format(tstate, PyExc_TypeError,
2807 "'%.200s' object is not a mapping",
Victor Stinnera102ed72020-02-07 02:24:48 +01002808 Py_TYPE(update)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002809 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002810 Py_DECREF(update);
2811 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002812 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002813 Py_DECREF(update);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002814 DISPATCH();
2815 }
2816
Mark Shannon8a4cd702020-01-27 09:57:45 +00002817 case TARGET(DICT_MERGE): {
2818 PyObject *update = POP();
2819 PyObject *dict = PEEK(oparg);
2820
2821 if (_PyDict_MergeEx(dict, update, 2) < 0) {
2822 format_kwargs_error(tstate, PEEK(2 + oparg), update);
2823 Py_DECREF(update);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002824 goto error;
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002825 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002826 Py_DECREF(update);
Brandt Bucherf185a732019-09-28 17:12:49 -07002827 PREDICT(CALL_FUNCTION_EX);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002828 DISPATCH();
2829 }
2830
Benjamin Petersonddd19492018-09-16 22:38:02 -07002831 case TARGET(MAP_ADD): {
Jörn Heisslerc8a35412019-06-22 16:40:55 +02002832 PyObject *value = TOP();
2833 PyObject *key = SECOND();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002834 PyObject *map;
2835 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002836 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002837 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002838 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002839 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002840 Py_DECREF(value);
2841 Py_DECREF(key);
2842 if (err != 0)
2843 goto error;
2844 PREDICT(JUMP_ABSOLUTE);
2845 DISPATCH();
2846 }
2847
Benjamin Petersonddd19492018-09-16 22:38:02 -07002848 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002849 PyObject *name = GETITEM(names, oparg);
2850 PyObject *owner = TOP();
2851 PyObject *res = PyObject_GetAttr(owner, name);
2852 Py_DECREF(owner);
2853 SET_TOP(res);
2854 if (res == NULL)
2855 goto error;
2856 DISPATCH();
2857 }
2858
Benjamin Petersonddd19492018-09-16 22:38:02 -07002859 case TARGET(COMPARE_OP): {
Mark Shannon9af0e472020-01-14 10:12:45 +00002860 assert(oparg <= Py_GE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002861 PyObject *right = POP();
2862 PyObject *left = TOP();
Mark Shannon9af0e472020-01-14 10:12:45 +00002863 PyObject *res = PyObject_RichCompare(left, right, oparg);
2864 SET_TOP(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002865 Py_DECREF(left);
2866 Py_DECREF(right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002867 if (res == NULL)
2868 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002869 PREDICT(POP_JUMP_IF_FALSE);
2870 PREDICT(POP_JUMP_IF_TRUE);
2871 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002872 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002873
Mark Shannon9af0e472020-01-14 10:12:45 +00002874 case TARGET(IS_OP): {
2875 PyObject *right = POP();
2876 PyObject *left = TOP();
2877 int res = (left == right)^oparg;
2878 PyObject *b = res ? Py_True : Py_False;
2879 Py_INCREF(b);
2880 SET_TOP(b);
2881 Py_DECREF(left);
2882 Py_DECREF(right);
2883 PREDICT(POP_JUMP_IF_FALSE);
2884 PREDICT(POP_JUMP_IF_TRUE);
2885 FAST_DISPATCH();
2886 }
2887
2888 case TARGET(CONTAINS_OP): {
2889 PyObject *right = POP();
2890 PyObject *left = POP();
2891 int res = PySequence_Contains(right, left);
2892 Py_DECREF(left);
2893 Py_DECREF(right);
2894 if (res < 0) {
2895 goto error;
2896 }
2897 PyObject *b = (res^oparg) ? Py_True : Py_False;
2898 Py_INCREF(b);
2899 PUSH(b);
2900 PREDICT(POP_JUMP_IF_FALSE);
2901 PREDICT(POP_JUMP_IF_TRUE);
2902 FAST_DISPATCH();
2903 }
2904
2905#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
2906 "BaseException is not allowed"
2907
2908 case TARGET(JUMP_IF_NOT_EXC_MATCH): {
2909 PyObject *right = POP();
2910 PyObject *left = POP();
2911 if (PyTuple_Check(right)) {
2912 Py_ssize_t i, length;
2913 length = PyTuple_GET_SIZE(right);
2914 for (i = 0; i < length; i++) {
2915 PyObject *exc = PyTuple_GET_ITEM(right, i);
2916 if (!PyExceptionClass_Check(exc)) {
2917 _PyErr_SetString(tstate, PyExc_TypeError,
2918 CANNOT_CATCH_MSG);
2919 Py_DECREF(left);
2920 Py_DECREF(right);
2921 goto error;
2922 }
2923 }
2924 }
2925 else {
2926 if (!PyExceptionClass_Check(right)) {
2927 _PyErr_SetString(tstate, PyExc_TypeError,
2928 CANNOT_CATCH_MSG);
2929 Py_DECREF(left);
2930 Py_DECREF(right);
2931 goto error;
2932 }
2933 }
2934 int res = PyErr_GivenExceptionMatches(left, right);
2935 Py_DECREF(left);
2936 Py_DECREF(right);
2937 if (res > 0) {
2938 /* Exception matches -- Do nothing */;
2939 }
2940 else if (res == 0) {
2941 JUMPTO(oparg);
2942 }
2943 else {
2944 goto error;
2945 }
2946 DISPATCH();
2947 }
2948
Benjamin Petersonddd19492018-09-16 22:38:02 -07002949 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002950 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002951 PyObject *fromlist = POP();
2952 PyObject *level = TOP();
2953 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02002954 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002955 Py_DECREF(level);
2956 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002957 SET_TOP(res);
2958 if (res == NULL)
2959 goto error;
2960 DISPATCH();
2961 }
2962
Benjamin Petersonddd19492018-09-16 22:38:02 -07002963 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002964 PyObject *from = POP(), *locals;
2965 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002966 if (PyFrame_FastToLocalsWithError(f) < 0) {
2967 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002968 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002969 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002970
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002971 locals = f->f_locals;
2972 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002973 _PyErr_SetString(tstate, PyExc_SystemError,
2974 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002975 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002976 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002977 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002978 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002979 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002980 Py_DECREF(from);
2981 if (err != 0)
2982 goto error;
2983 DISPATCH();
2984 }
Guido van Rossum25831651993-05-19 14:50:45 +00002985
Benjamin Petersonddd19492018-09-16 22:38:02 -07002986 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002987 PyObject *name = GETITEM(names, oparg);
2988 PyObject *from = TOP();
2989 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02002990 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002991 PUSH(res);
2992 if (res == NULL)
2993 goto error;
2994 DISPATCH();
2995 }
Thomas Wouters52152252000-08-17 22:55:00 +00002996
Benjamin Petersonddd19492018-09-16 22:38:02 -07002997 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002998 JUMPBY(oparg);
2999 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003000 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003001
Benjamin Petersonddd19492018-09-16 22:38:02 -07003002 case TARGET(POP_JUMP_IF_FALSE): {
3003 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003004 PyObject *cond = POP();
3005 int err;
3006 if (cond == Py_True) {
3007 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003008 FAST_DISPATCH();
3009 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003010 if (cond == Py_False) {
3011 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003012 JUMPTO(oparg);
3013 FAST_DISPATCH();
3014 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003015 err = PyObject_IsTrue(cond);
3016 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003017 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003018 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003019 else if (err == 0)
3020 JUMPTO(oparg);
3021 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003022 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003023 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003024 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003025
Benjamin Petersonddd19492018-09-16 22:38:02 -07003026 case TARGET(POP_JUMP_IF_TRUE): {
3027 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003028 PyObject *cond = POP();
3029 int err;
3030 if (cond == Py_False) {
3031 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003032 FAST_DISPATCH();
3033 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003034 if (cond == Py_True) {
3035 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003036 JUMPTO(oparg);
3037 FAST_DISPATCH();
3038 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003039 err = PyObject_IsTrue(cond);
3040 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003041 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003042 JUMPTO(oparg);
3043 }
3044 else if (err == 0)
3045 ;
3046 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003047 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003048 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003049 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003050
Benjamin Petersonddd19492018-09-16 22:38:02 -07003051 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003052 PyObject *cond = TOP();
3053 int err;
3054 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003055 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003056 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003057 FAST_DISPATCH();
3058 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003059 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003060 JUMPTO(oparg);
3061 FAST_DISPATCH();
3062 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003063 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003064 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003065 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003066 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003067 }
3068 else if (err == 0)
3069 JUMPTO(oparg);
3070 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003071 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003072 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003073 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003074
Benjamin Petersonddd19492018-09-16 22:38:02 -07003075 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003076 PyObject *cond = TOP();
3077 int err;
3078 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003079 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003080 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003081 FAST_DISPATCH();
3082 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003083 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003084 JUMPTO(oparg);
3085 FAST_DISPATCH();
3086 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003087 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003088 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003089 JUMPTO(oparg);
3090 }
3091 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003092 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003093 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003094 }
3095 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003096 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003097 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003098 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003099
Benjamin Petersonddd19492018-09-16 22:38:02 -07003100 case TARGET(JUMP_ABSOLUTE): {
3101 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003102 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003103#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003104 /* Enabling this path speeds-up all while and for-loops by bypassing
3105 the per-loop checks for signals. By default, this should be turned-off
3106 because it prevents detection of a control-break in tight loops like
3107 "while 1: pass". Compile with this option turned-on when you need
3108 the speed-up and do not need break checking inside tight loops (ones
3109 that contain only instructions ending with FAST_DISPATCH).
3110 */
3111 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003112#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003113 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003114#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003115 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003116
Benjamin Petersonddd19492018-09-16 22:38:02 -07003117 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003118 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003119 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003120 PyObject *iter = PyObject_GetIter(iterable);
3121 Py_DECREF(iterable);
3122 SET_TOP(iter);
3123 if (iter == NULL)
3124 goto error;
3125 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003126 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003127 DISPATCH();
3128 }
3129
Benjamin Petersonddd19492018-09-16 22:38:02 -07003130 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04003131 /* before: [obj]; after [getiter(obj)] */
3132 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003133 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003134 if (PyCoro_CheckExact(iterable)) {
3135 /* `iterable` is a coroutine */
3136 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3137 /* and it is used in a 'yield from' expression of a
3138 regular generator. */
3139 Py_DECREF(iterable);
3140 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003141 _PyErr_SetString(tstate, PyExc_TypeError,
3142 "cannot 'yield from' a coroutine object "
3143 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04003144 goto error;
3145 }
3146 }
3147 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003148 /* `iterable` is not a generator. */
3149 iter = PyObject_GetIter(iterable);
3150 Py_DECREF(iterable);
3151 SET_TOP(iter);
3152 if (iter == NULL)
3153 goto error;
3154 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003155 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003156 DISPATCH();
3157 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003158
Benjamin Petersonddd19492018-09-16 22:38:02 -07003159 case TARGET(FOR_ITER): {
3160 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003161 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003162 PyObject *iter = TOP();
Victor Stinnera102ed72020-02-07 02:24:48 +01003163 PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003164 if (next != NULL) {
3165 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003166 PREDICT(STORE_FAST);
3167 PREDICT(UNPACK_SEQUENCE);
3168 DISPATCH();
3169 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003170 if (_PyErr_Occurred(tstate)) {
3171 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003172 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003173 }
3174 else if (tstate->c_tracefunc != NULL) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003175 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Victor Stinner438a12d2019-05-24 17:01:38 +02003176 }
3177 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003178 }
3179 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003180 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003181 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003182 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003183 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003184 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003185 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003186
Benjamin Petersonddd19492018-09-16 22:38:02 -07003187 case TARGET(SETUP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003188 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003189 STACK_LEVEL());
3190 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003191 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003192
Benjamin Petersonddd19492018-09-16 22:38:02 -07003193 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003194 _Py_IDENTIFIER(__aenter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003195 _Py_IDENTIFIER(__aexit__);
Yury Selivanov75445082015-05-11 22:57:16 -04003196 PyObject *mgr = TOP();
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003197 PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04003198 PyObject *res;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003199 if (enter == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003200 goto error;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003201 }
3202 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__);
3203 if (exit == NULL) {
3204 Py_DECREF(enter);
3205 goto error;
3206 }
Yury Selivanov75445082015-05-11 22:57:16 -04003207 SET_TOP(exit);
Yury Selivanov75445082015-05-11 22:57:16 -04003208 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003209 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003210 Py_DECREF(enter);
3211 if (res == NULL)
3212 goto error;
3213 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003214 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003215 DISPATCH();
3216 }
3217
Benjamin Petersonddd19492018-09-16 22:38:02 -07003218 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003219 PyObject *res = POP();
3220 /* Setup the finally block before pushing the result
3221 of __aenter__ on the stack. */
3222 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3223 STACK_LEVEL());
3224 PUSH(res);
3225 DISPATCH();
3226 }
3227
Benjamin Petersonddd19492018-09-16 22:38:02 -07003228 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003229 _Py_IDENTIFIER(__enter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003230 _Py_IDENTIFIER(__exit__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003231 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003232 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003233 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003234 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08003235 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003236 }
3237 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003238 if (exit == NULL) {
3239 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003240 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003241 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003242 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003243 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003244 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003245 Py_DECREF(enter);
3246 if (res == NULL)
3247 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003248 /* Setup the finally block before pushing the result
3249 of __enter__ on the stack. */
3250 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3251 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003252
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003253 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003254 DISPATCH();
3255 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003256
Mark Shannonfee55262019-11-21 09:11:43 +00003257 case TARGET(WITH_EXCEPT_START): {
3258 /* At the top of the stack are 7 values:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003259 - (TOP, SECOND, THIRD) = exc_info()
Mark Shannonfee55262019-11-21 09:11:43 +00003260 - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER
3261 - SEVENTH: the context.__exit__ bound method
3262 We call SEVENTH(TOP, SECOND, THIRD).
3263 Then we push again the TOP exception and the __exit__
3264 return value.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003265 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003266 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003267 PyObject *exc, *val, *tb, *res;
3268
Victor Stinner842cfff2016-12-01 14:45:31 +01003269 exc = TOP();
Mark Shannonfee55262019-11-21 09:11:43 +00003270 val = SECOND();
3271 tb = THIRD();
3272 assert(exc != Py_None);
3273 assert(!PyLong_Check(exc));
3274 exit_func = PEEK(7);
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003275 PyObject *stack[4] = {NULL, exc, val, tb};
Petr Viktorinffd97532020-02-11 17:46:57 +01003276 res = PyObject_Vectorcall(exit_func, stack + 1,
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003277 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003278 if (res == NULL)
3279 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003280
Yury Selivanov75445082015-05-11 22:57:16 -04003281 PUSH(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003282 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003283 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003284
Benjamin Petersonddd19492018-09-16 22:38:02 -07003285 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10003286 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003287 PyObject *name = GETITEM(names, oparg);
3288 PyObject *obj = TOP();
3289 PyObject *meth = NULL;
3290
3291 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3292
Yury Selivanovf2392132016-12-13 19:03:51 -05003293 if (meth == NULL) {
3294 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003295 goto error;
3296 }
3297
3298 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003299 /* We can bypass temporary bound method object.
3300 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003301
INADA Naoki015bce62017-01-16 17:23:30 +09003302 meth | self | arg1 | ... | argN
3303 */
3304 SET_TOP(meth);
3305 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003306 }
3307 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003308 /* meth is not an unbound method (but a regular attr, or
3309 something was returned by a descriptor protocol). Set
3310 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003311 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003312
3313 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003314 */
INADA Naoki015bce62017-01-16 17:23:30 +09003315 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003316 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003317 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003318 }
3319 DISPATCH();
3320 }
3321
Benjamin Petersonddd19492018-09-16 22:38:02 -07003322 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003323 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003324 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003325
3326 sp = stack_pointer;
3327
INADA Naoki015bce62017-01-16 17:23:30 +09003328 meth = PEEK(oparg + 2);
3329 if (meth == NULL) {
3330 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3331 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003332
3333 Stack layout:
3334
INADA Naoki015bce62017-01-16 17:23:30 +09003335 ... | NULL | callable | arg1 | ... | argN
3336 ^- TOP()
3337 ^- (-oparg)
3338 ^- (-oparg-1)
3339 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003340
Ville Skyttä49b27342017-08-03 09:00:59 +03003341 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003342 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003343 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003344 res = call_function(tstate, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003345 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003346 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003347 }
3348 else {
3349 /* This is a method call. Stack layout:
3350
INADA Naoki015bce62017-01-16 17:23:30 +09003351 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003352 ^- TOP()
3353 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003354 ^- (-oparg-1)
3355 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003356
INADA Naoki015bce62017-01-16 17:23:30 +09003357 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003358 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003359 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003360 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003361 res = call_function(tstate, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003362 stack_pointer = sp;
3363 }
3364
3365 PUSH(res);
3366 if (res == NULL)
3367 goto error;
3368 DISPATCH();
3369 }
3370
Benjamin Petersonddd19492018-09-16 22:38:02 -07003371 case TARGET(CALL_FUNCTION): {
3372 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003373 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003374 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003375 res = call_function(tstate, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003376 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003377 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003378 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003379 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003380 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003381 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003382 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003383
Benjamin Petersonddd19492018-09-16 22:38:02 -07003384 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003385 PyObject **sp, *res, *names;
3386
3387 names = POP();
Jeroen Demeyer05677862019-08-16 12:41:27 +02003388 assert(PyTuple_Check(names));
3389 assert(PyTuple_GET_SIZE(names) <= oparg);
3390 /* We assume without checking that names contains only strings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003391 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003392 res = call_function(tstate, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003393 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003394 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003395 Py_DECREF(names);
3396
3397 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003398 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003399 }
3400 DISPATCH();
3401 }
3402
Benjamin Petersonddd19492018-09-16 22:38:02 -07003403 case TARGET(CALL_FUNCTION_EX): {
Brandt Bucherf185a732019-09-28 17:12:49 -07003404 PREDICTED(CALL_FUNCTION_EX);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003405 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003406 if (oparg & 0x01) {
3407 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003408 if (!PyDict_CheckExact(kwargs)) {
3409 PyObject *d = PyDict_New();
3410 if (d == NULL)
3411 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003412 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003413 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02003414 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003415 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003416 goto error;
3417 }
3418 Py_DECREF(kwargs);
3419 kwargs = d;
3420 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003421 assert(PyDict_CheckExact(kwargs));
3422 }
3423 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003424 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003425 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003426 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003427 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003428 goto error;
3429 }
3430 Py_SETREF(callargs, PySequence_Tuple(callargs));
3431 if (callargs == NULL) {
3432 goto error;
3433 }
3434 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003435 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003436
Victor Stinner09532fe2019-05-10 23:39:09 +02003437 result = do_call_core(tstate, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003438 Py_DECREF(func);
3439 Py_DECREF(callargs);
3440 Py_XDECREF(kwargs);
3441
3442 SET_TOP(result);
3443 if (result == NULL) {
3444 goto error;
3445 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003446 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003447 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003448
Benjamin Petersonddd19492018-09-16 22:38:02 -07003449 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003450 PyObject *qualname = POP();
3451 PyObject *codeobj = POP();
3452 PyFunctionObject *func = (PyFunctionObject *)
3453 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003454
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003455 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003456 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003457 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003458 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003459 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003460
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003461 if (oparg & 0x08) {
3462 assert(PyTuple_CheckExact(TOP()));
3463 func ->func_closure = POP();
3464 }
3465 if (oparg & 0x04) {
3466 assert(PyDict_CheckExact(TOP()));
3467 func->func_annotations = POP();
3468 }
3469 if (oparg & 0x02) {
3470 assert(PyDict_CheckExact(TOP()));
3471 func->func_kwdefaults = POP();
3472 }
3473 if (oparg & 0x01) {
3474 assert(PyTuple_CheckExact(TOP()));
3475 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003476 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003477
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003478 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003479 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003480 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003481
Benjamin Petersonddd19492018-09-16 22:38:02 -07003482 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003483 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003484 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003485 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003486 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003487 step = NULL;
3488 stop = POP();
3489 start = TOP();
3490 slice = PySlice_New(start, stop, step);
3491 Py_DECREF(start);
3492 Py_DECREF(stop);
3493 Py_XDECREF(step);
3494 SET_TOP(slice);
3495 if (slice == NULL)
3496 goto error;
3497 DISPATCH();
3498 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003499
Benjamin Petersonddd19492018-09-16 22:38:02 -07003500 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003501 /* Handles f-string value formatting. */
3502 PyObject *result;
3503 PyObject *fmt_spec;
3504 PyObject *value;
3505 PyObject *(*conv_fn)(PyObject *);
3506 int which_conversion = oparg & FVC_MASK;
3507 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3508
3509 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003510 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003511
3512 /* See if any conversion is specified. */
3513 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003514 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003515 case FVC_STR: conv_fn = PyObject_Str; break;
3516 case FVC_REPR: conv_fn = PyObject_Repr; break;
3517 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003518 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02003519 _PyErr_Format(tstate, PyExc_SystemError,
3520 "unexpected conversion flag %d",
3521 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003522 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003523 }
3524
3525 /* If there's a conversion function, call it and replace
3526 value with that result. Otherwise, just use value,
3527 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003528 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003529 result = conv_fn(value);
3530 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003531 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003532 Py_XDECREF(fmt_spec);
3533 goto error;
3534 }
3535 value = result;
3536 }
3537
3538 /* If value is a unicode object, and there's no fmt_spec,
3539 then we know the result of format(value) is value
3540 itself. In that case, skip calling format(). I plan to
3541 move this optimization in to PyObject_Format()
3542 itself. */
3543 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3544 /* Do nothing, just transfer ownership to result. */
3545 result = value;
3546 } else {
3547 /* Actually call format(). */
3548 result = PyObject_Format(value, fmt_spec);
3549 Py_DECREF(value);
3550 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003551 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003552 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003553 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003554 }
3555
Eric V. Smith135d5f42016-02-05 18:23:08 -05003556 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003557 DISPATCH();
3558 }
3559
Benjamin Petersonddd19492018-09-16 22:38:02 -07003560 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003561 int oldoparg = oparg;
3562 NEXTOPARG();
3563 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003564 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003565 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003566
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003567
Antoine Pitrou042b1282010-08-13 21:15:58 +00003568#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003569 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003570#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003571 default:
3572 fprintf(stderr,
3573 "XXX lineno: %d, opcode: %d\n",
3574 PyFrame_GetLineNumber(f),
3575 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02003576 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003577 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003579 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003580
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003581 /* This should never be reached. Every opcode should end with DISPATCH()
3582 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003583 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003584
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003585error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003586 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003587#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02003588 if (!_PyErr_Occurred(tstate)) {
3589 _PyErr_SetString(tstate, PyExc_SystemError,
3590 "error return without exception set");
3591 }
Victor Stinner365b6932013-07-12 00:11:58 +02003592#else
Victor Stinner438a12d2019-05-24 17:01:38 +02003593 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02003594#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003595
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003596 /* Log traceback info. */
3597 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003598
Benjamin Peterson51f46162013-01-23 08:38:47 -05003599 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003600 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3601 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003602
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003603exception_unwind:
3604 /* Unwind stacks if an exception occurred */
3605 while (f->f_iblock > 0) {
3606 /* Pop the current block. */
3607 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003608
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003609 if (b->b_type == EXCEPT_HANDLER) {
3610 UNWIND_EXCEPT_HANDLER(b);
3611 continue;
3612 }
3613 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003614 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003615 PyObject *exc, *val, *tb;
3616 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003617 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003618 /* Beware, this invalidates all b->b_* fields */
3619 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003620 PUSH(exc_info->exc_traceback);
3621 PUSH(exc_info->exc_value);
3622 if (exc_info->exc_type != NULL) {
3623 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003624 }
3625 else {
3626 Py_INCREF(Py_None);
3627 PUSH(Py_None);
3628 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003629 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003630 /* Make the raw exception data
3631 available to the handler,
3632 so a program can emulate the
3633 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02003634 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003635 if (tb != NULL)
3636 PyException_SetTraceback(val, tb);
3637 else
3638 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003639 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003640 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003641 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003642 exc_info->exc_value = val;
3643 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003644 if (tb == NULL)
3645 tb = Py_None;
3646 Py_INCREF(tb);
3647 PUSH(tb);
3648 PUSH(val);
3649 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003650 JUMPTO(handler);
Pablo Galindo4c53e632020-01-10 09:24:22 +00003651 if (_Py_TracingPossible(ceval)) {
3652 int needs_new_execution_window = (f->f_lasti < instr_lb || f->f_lasti >= instr_ub);
3653 int needs_line_update = (f->f_lasti == instr_lb || f->f_lasti < instr_prev);
3654 /* Make sure that we trace line after exception if we are in a new execution
3655 * window or we don't need a line update and we are not in the first instruction
3656 * of the line. */
3657 if (needs_new_execution_window || (!needs_line_update && instr_lb > 0)) {
3658 instr_prev = INT_MAX;
3659 }
Mark Shannonfee55262019-11-21 09:11:43 +00003660 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003661 /* Resume normal execution */
3662 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003663 }
3664 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003665
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003666 /* End the loop as we still have an error */
3667 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003668 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003669
Pablo Galindof00828a2019-05-09 16:52:02 +01003670 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003671 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01003672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003673 /* Pop remaining stack entries. */
3674 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003675 PyObject *o = POP();
3676 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003677 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003678
Mark Shannone7c9f4a2020-01-13 12:51:26 +00003679exiting:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003680 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003681 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003682 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3683 tstate, f, PyTrace_RETURN, retval)) {
3684 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003685 }
3686 }
3687 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003688 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3689 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003690 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003691 }
3692 }
3693 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003694
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003695 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003696exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003697 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3698 dtrace_function_return(f);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01003699 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrou58720d62013-08-05 23:26:40 +02003700 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003701 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003702
Victor Stinner17269092019-11-05 01:22:12 +01003703 return _Py_CheckFunctionResult(tstate, NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003704}
3705
Benjamin Petersonb204a422011-06-05 22:04:07 -05003706static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003707format_missing(PyThreadState *tstate, const char *kind,
3708 PyCodeObject *co, PyObject *names)
Benjamin Petersone109c702011-06-24 09:37:26 -05003709{
3710 int err;
3711 Py_ssize_t len = PyList_GET_SIZE(names);
3712 PyObject *name_str, *comma, *tail, *tmp;
3713
3714 assert(PyList_CheckExact(names));
3715 assert(len >= 1);
3716 /* Deal with the joys of natural language. */
3717 switch (len) {
3718 case 1:
3719 name_str = PyList_GET_ITEM(names, 0);
3720 Py_INCREF(name_str);
3721 break;
3722 case 2:
3723 name_str = PyUnicode_FromFormat("%U and %U",
3724 PyList_GET_ITEM(names, len - 2),
3725 PyList_GET_ITEM(names, len - 1));
3726 break;
3727 default:
3728 tail = PyUnicode_FromFormat(", %U, and %U",
3729 PyList_GET_ITEM(names, len - 2),
3730 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003731 if (tail == NULL)
3732 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003733 /* Chop off the last two objects in the list. This shouldn't actually
3734 fail, but we can't be too careful. */
3735 err = PyList_SetSlice(names, len - 2, len, NULL);
3736 if (err == -1) {
3737 Py_DECREF(tail);
3738 return;
3739 }
3740 /* Stitch everything up into a nice comma-separated list. */
3741 comma = PyUnicode_FromString(", ");
3742 if (comma == NULL) {
3743 Py_DECREF(tail);
3744 return;
3745 }
3746 tmp = PyUnicode_Join(comma, names);
3747 Py_DECREF(comma);
3748 if (tmp == NULL) {
3749 Py_DECREF(tail);
3750 return;
3751 }
3752 name_str = PyUnicode_Concat(tmp, tail);
3753 Py_DECREF(tmp);
3754 Py_DECREF(tail);
3755 break;
3756 }
3757 if (name_str == NULL)
3758 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02003759 _PyErr_Format(tstate, PyExc_TypeError,
3760 "%U() missing %i required %s argument%s: %U",
3761 co->co_name,
3762 len,
3763 kind,
3764 len == 1 ? "" : "s",
3765 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05003766 Py_DECREF(name_str);
3767}
3768
3769static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003770missing_arguments(PyThreadState *tstate, PyCodeObject *co,
3771 Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003772 PyObject **fastlocals)
3773{
Victor Stinner74319ae2016-08-25 00:04:09 +02003774 Py_ssize_t i, j = 0;
3775 Py_ssize_t start, end;
3776 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003777 const char *kind = positional ? "positional" : "keyword-only";
3778 PyObject *missing_names;
3779
3780 /* Compute the names of the arguments that are missing. */
3781 missing_names = PyList_New(missing);
3782 if (missing_names == NULL)
3783 return;
3784 if (positional) {
3785 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01003786 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003787 }
3788 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003789 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003790 end = start + co->co_kwonlyargcount;
3791 }
3792 for (i = start; i < end; i++) {
3793 if (GETLOCAL(i) == NULL) {
3794 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3795 PyObject *name = PyObject_Repr(raw);
3796 if (name == NULL) {
3797 Py_DECREF(missing_names);
3798 return;
3799 }
3800 PyList_SET_ITEM(missing_names, j++, name);
3801 }
3802 }
3803 assert(j == missing);
Victor Stinner438a12d2019-05-24 17:01:38 +02003804 format_missing(tstate, kind, co, missing_names);
Benjamin Petersone109c702011-06-24 09:37:26 -05003805 Py_DECREF(missing_names);
3806}
3807
3808static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003809too_many_positional(PyThreadState *tstate, PyCodeObject *co,
3810 Py_ssize_t given, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003811 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003812{
3813 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003814 Py_ssize_t kwonly_given = 0;
3815 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003816 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003817 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003818
Benjamin Petersone109c702011-06-24 09:37:26 -05003819 assert((co->co_flags & CO_VARARGS) == 0);
3820 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01003821 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003822 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003823 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003824 }
3825 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003826 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01003827 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003828 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01003829 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003830 }
3831 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003832 plural = (co_argcount != 1);
3833 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003834 }
3835 if (sig == NULL)
3836 return;
3837 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003838 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3839 kwonly_sig = PyUnicode_FromFormat(format,
3840 given != 1 ? "s" : "",
3841 kwonly_given,
3842 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003843 if (kwonly_sig == NULL) {
3844 Py_DECREF(sig);
3845 return;
3846 }
3847 }
3848 else {
3849 /* This will not fail. */
3850 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003851 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003852 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003853 _PyErr_Format(tstate, PyExc_TypeError,
3854 "%U() takes %U positional argument%s but %zd%U %s given",
3855 co->co_name,
3856 sig,
3857 plural ? "s" : "",
3858 given,
3859 kwonly_sig,
3860 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003861 Py_DECREF(sig);
3862 Py_DECREF(kwonly_sig);
3863}
3864
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003865static int
Victor Stinner438a12d2019-05-24 17:01:38 +02003866positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
3867 Py_ssize_t kwcount, PyObject* const* kwnames)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003868{
3869 int posonly_conflicts = 0;
3870 PyObject* posonly_names = PyList_New(0);
3871
3872 for(int k=0; k < co->co_posonlyargcount; k++){
3873 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
3874
3875 for (int k2=0; k2<kwcount; k2++){
3876 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
3877 PyObject* kwname = kwnames[k2];
3878 if (kwname == posonly_name){
3879 if(PyList_Append(posonly_names, kwname) != 0) {
3880 goto fail;
3881 }
3882 posonly_conflicts++;
3883 continue;
3884 }
3885
3886 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
3887
3888 if ( cmp > 0) {
3889 if(PyList_Append(posonly_names, kwname) != 0) {
3890 goto fail;
3891 }
3892 posonly_conflicts++;
3893 } else if (cmp < 0) {
3894 goto fail;
3895 }
3896
3897 }
3898 }
3899 if (posonly_conflicts) {
3900 PyObject* comma = PyUnicode_FromString(", ");
3901 if (comma == NULL) {
3902 goto fail;
3903 }
3904 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
3905 Py_DECREF(comma);
3906 if (error_names == NULL) {
3907 goto fail;
3908 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003909 _PyErr_Format(tstate, PyExc_TypeError,
3910 "%U() got some positional-only arguments passed"
3911 " as keyword arguments: '%U'",
3912 co->co_name, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003913 Py_DECREF(error_names);
3914 goto fail;
3915 }
3916
3917 Py_DECREF(posonly_names);
3918 return 0;
3919
3920fail:
3921 Py_XDECREF(posonly_names);
3922 return 1;
3923
3924}
3925
Guido van Rossumc2e20742006-02-27 22:32:47 +00003926/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02003927 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003928 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003929
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003930PyObject *
Victor Stinnerb5e170f2019-11-16 01:03:22 +01003931_PyEval_EvalCode(PyThreadState *tstate,
3932 PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003933 PyObject *const *args, Py_ssize_t argcount,
3934 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003935 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003936 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003937 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003938 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003939{
Victor Stinnerb5e170f2019-11-16 01:03:22 +01003940 assert(tstate != NULL);
3941
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003942 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003943 PyFrameObject *f;
3944 PyObject *retval = NULL;
3945 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003946 PyObject *x, *u;
Pablo Galindocd74e662019-06-01 18:08:04 +01003947 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003948 Py_ssize_t i, j, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003949 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003951 if (globals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003952 _PyErr_SetString(tstate, PyExc_SystemError,
3953 "PyEval_EvalCodeEx: NULL globals");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003954 return NULL;
3955 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003956
Victor Stinnerc7020012016-08-16 23:40:29 +02003957 /* Create the frame */
INADA Naoki5a625d02016-12-24 20:19:08 +09003958 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003959 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003960 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003961 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003962 fastlocals = f->f_localsplus;
3963 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003964
Victor Stinnerc7020012016-08-16 23:40:29 +02003965 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003966 if (co->co_flags & CO_VARKEYWORDS) {
3967 kwdict = PyDict_New();
3968 if (kwdict == NULL)
3969 goto fail;
3970 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003971 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003972 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003973 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003974 SETLOCAL(i, kwdict);
3975 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003976 else {
3977 kwdict = NULL;
3978 }
3979
Pablo Galindocd74e662019-06-01 18:08:04 +01003980 /* Copy all positional arguments into local variables */
3981 if (argcount > co->co_argcount) {
3982 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003983 }
3984 else {
3985 n = argcount;
3986 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003987 for (j = 0; j < n; j++) {
3988 x = args[j];
3989 Py_INCREF(x);
3990 SETLOCAL(j, x);
3991 }
3992
Victor Stinnerc7020012016-08-16 23:40:29 +02003993 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003994 if (co->co_flags & CO_VARARGS) {
Sergey Fedoseev234531b2019-02-25 21:59:12 +05003995 u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003996 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003997 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003998 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003999 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004000 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004001
Serhiy Storchakab7281052016-09-12 00:52:40 +03004002 /* Handle keyword arguments passed as two strided arrays */
4003 kwcount *= kwstep;
4004 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004005 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03004006 PyObject *keyword = kwnames[i];
4007 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02004008 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004009
Benjamin Petersonb204a422011-06-05 22:04:07 -05004010 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004011 _PyErr_Format(tstate, PyExc_TypeError,
4012 "%U() keywords must be strings",
4013 co->co_name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004014 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004015 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004016
Benjamin Petersonb204a422011-06-05 22:04:07 -05004017 /* Speed hack: do raw pointer compares. As names are
4018 normally interned this should almost always hit. */
4019 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004020 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004021 PyObject *name = co_varnames[j];
4022 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004023 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004024 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004025 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004026
Benjamin Petersonb204a422011-06-05 22:04:07 -05004027 /* Slow fallback, just in case */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004028 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004029 PyObject *name = co_varnames[j];
4030 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
4031 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004032 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004033 }
4034 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004035 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004036 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004037 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004038
Victor Stinner231d1f32017-01-11 02:12:06 +01004039 assert(j >= total_args);
4040 if (kwdict == NULL) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004041
Victor Stinner438a12d2019-05-24 17:01:38 +02004042 if (co->co_posonlyargcount
4043 && positional_only_passed_as_keyword(tstate, co,
4044 kwcount, kwnames))
4045 {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004046 goto fail;
4047 }
4048
Victor Stinner438a12d2019-05-24 17:01:38 +02004049 _PyErr_Format(tstate, PyExc_TypeError,
4050 "%U() got an unexpected keyword argument '%S'",
4051 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004052 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004053 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004054
Christian Heimes0bd447f2013-07-20 14:48:10 +02004055 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4056 goto fail;
4057 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004058 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004059
Benjamin Petersonb204a422011-06-05 22:04:07 -05004060 kw_found:
4061 if (GETLOCAL(j) != NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004062 _PyErr_Format(tstate, PyExc_TypeError,
4063 "%U() got multiple values for argument '%S'",
4064 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004065 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004066 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004067 Py_INCREF(value);
4068 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004069 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004070
4071 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004072 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004073 too_many_positional(tstate, co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004074 goto fail;
4075 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004076
4077 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004078 if (argcount < co->co_argcount) {
4079 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004080 Py_ssize_t missing = 0;
4081 for (i = argcount; i < m; i++) {
4082 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004083 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004084 }
4085 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004086 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004087 missing_arguments(tstate, co, missing, defcount, fastlocals);
Benjamin Petersone109c702011-06-24 09:37:26 -05004088 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004089 }
4090 if (n > m)
4091 i = n - m;
4092 else
4093 i = 0;
4094 for (; i < defcount; i++) {
4095 if (GETLOCAL(m+i) == NULL) {
4096 PyObject *def = defs[i];
4097 Py_INCREF(def);
4098 SETLOCAL(m+i, def);
4099 }
4100 }
4101 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004102
4103 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004104 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004105 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004106 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004107 PyObject *name;
4108 if (GETLOCAL(i) != NULL)
4109 continue;
4110 name = PyTuple_GET_ITEM(co->co_varnames, i);
4111 if (kwdefs != NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004112 PyObject *def = PyDict_GetItemWithError(kwdefs, name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004113 if (def) {
4114 Py_INCREF(def);
4115 SETLOCAL(i, def);
4116 continue;
4117 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004118 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004119 goto fail;
4120 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004121 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004122 missing++;
4123 }
4124 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004125 missing_arguments(tstate, co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004126 goto fail;
4127 }
4128 }
4129
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004130 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004131 vars into frame. */
4132 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004133 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004134 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004135 /* Possibly account for the cell variable being an argument. */
4136 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004137 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004138 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004139 /* Clear the local copy. */
4140 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004141 }
4142 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004143 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004144 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004145 if (c == NULL)
4146 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004147 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004148 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004149
4150 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004151 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4152 PyObject *o = PyTuple_GET_ITEM(closure, i);
4153 Py_INCREF(o);
4154 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004155 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004156
Yury Selivanoveb636452016-09-08 22:01:51 -07004157 /* Handle generator/coroutine/asynchronous generator */
4158 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004159 PyObject *gen;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004160 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004162 /* Don't need to keep the reference to f_back, it will be set
4163 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004164 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004165
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004166 /* Create a new generator that owns the ready to run frame
4167 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004168 if (is_coro) {
4169 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004170 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4171 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004172 } else {
4173 gen = PyGen_NewWithQualName(f, name, qualname);
4174 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004175 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004176 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004177 }
INADA Naoki9c157762016-12-26 18:52:46 +09004178
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004179 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004180
Yury Selivanov75445082015-05-11 22:57:16 -04004181 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004182 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004183
Victor Stinnerb9e68122019-11-14 12:20:46 +01004184 retval = _PyEval_EvalFrame(tstate, f, 0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004185
Thomas Woutersce272b62007-09-19 21:19:28 +00004186fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004187
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004188 /* decref'ing the frame can cause __del__ methods to get invoked,
4189 which can call back into Python. While we're done with the
4190 current Python frame (f), the associated C stack is still in use,
4191 so recursion_depth must be boosted for the duration.
4192 */
INADA Naoki5a625d02016-12-24 20:19:08 +09004193 if (Py_REFCNT(f) > 1) {
4194 Py_DECREF(f);
4195 _PyObject_GC_TRACK(f);
4196 }
4197 else {
4198 ++tstate->recursion_depth;
4199 Py_DECREF(f);
4200 --tstate->recursion_depth;
4201 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004202 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004203}
4204
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004205
4206PyObject *
4207_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
4208 PyObject *const *args, Py_ssize_t argcount,
4209 PyObject *const *kwnames, PyObject *const *kwargs,
4210 Py_ssize_t kwcount, int kwstep,
4211 PyObject *const *defs, Py_ssize_t defcount,
4212 PyObject *kwdefs, PyObject *closure,
4213 PyObject *name, PyObject *qualname)
4214{
4215 PyThreadState *tstate = _PyThreadState_GET();
4216 return _PyEval_EvalCode(tstate, _co, globals, locals,
4217 args, argcount,
4218 kwnames, kwargs,
4219 kwcount, kwstep,
4220 defs, defcount,
4221 kwdefs, closure,
4222 name, qualname);
4223}
4224
Victor Stinner40ee3012014-06-16 15:59:28 +02004225PyObject *
4226PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004227 PyObject *const *args, int argcount,
4228 PyObject *const *kws, int kwcount,
4229 PyObject *const *defs, int defcount,
4230 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004231{
4232 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004233 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004234 kws, kws != NULL ? kws + 1 : NULL,
4235 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004236 defs, defcount,
4237 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004238 NULL, NULL);
4239}
Tim Peters5ca576e2001-06-18 22:08:13 +00004240
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004241static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004242special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004243{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004244 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004245 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02004246 if (res == NULL && !_PyErr_Occurred(tstate)) {
4247 _PyErr_SetObject(tstate, PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004248 return NULL;
4249 }
4250 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004251}
4252
4253
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004254/* Logic for the raise statement (too complicated for inlining).
4255 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004256static int
Victor Stinner09532fe2019-05-10 23:39:09 +02004257do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004258{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004259 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004261 if (exc == NULL) {
4262 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01004263 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004264 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004265 type = exc_info->exc_type;
4266 value = exc_info->exc_value;
4267 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004268 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004269 _PyErr_SetString(tstate, PyExc_RuntimeError,
4270 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004271 return 0;
4272 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004273 Py_XINCREF(type);
4274 Py_XINCREF(value);
4275 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02004276 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004277 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004278 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004280 /* We support the following forms of raise:
4281 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004282 raise <instance>
4283 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004285 if (PyExceptionClass_Check(exc)) {
4286 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004287 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004288 if (value == NULL)
4289 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004290 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004291 _PyErr_Format(tstate, PyExc_TypeError,
4292 "calling %R should have returned an instance of "
4293 "BaseException, not %R",
4294 type, Py_TYPE(value));
4295 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004296 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004297 }
4298 else if (PyExceptionInstance_Check(exc)) {
4299 value = exc;
4300 type = PyExceptionInstance_Class(exc);
4301 Py_INCREF(type);
4302 }
4303 else {
4304 /* Not something you can raise. You get an exception
4305 anyway, just not what you specified :-) */
4306 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02004307 _PyErr_SetString(tstate, PyExc_TypeError,
4308 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004309 goto raise_error;
4310 }
Collin Winter828f04a2007-08-31 00:04:24 +00004311
Serhiy Storchakac0191582016-09-27 11:37:10 +03004312 assert(type != NULL);
4313 assert(value != NULL);
4314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004315 if (cause) {
4316 PyObject *fixed_cause;
4317 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004318 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004319 if (fixed_cause == NULL)
4320 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004321 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004322 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004323 else if (PyExceptionInstance_Check(cause)) {
4324 fixed_cause = cause;
4325 }
4326 else if (cause == Py_None) {
4327 Py_DECREF(cause);
4328 fixed_cause = NULL;
4329 }
4330 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004331 _PyErr_SetString(tstate, PyExc_TypeError,
4332 "exception causes must derive from "
4333 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004334 goto raise_error;
4335 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004336 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004337 }
Collin Winter828f04a2007-08-31 00:04:24 +00004338
Victor Stinner438a12d2019-05-24 17:01:38 +02004339 _PyErr_SetObject(tstate, type, value);
Victor Stinner61f4db82020-01-28 03:37:45 +01004340 /* _PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004341 Py_DECREF(value);
4342 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004343 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004344
4345raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004346 Py_XDECREF(value);
4347 Py_XDECREF(type);
4348 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004349 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004350}
4351
Tim Petersd6d010b2001-06-21 02:49:55 +00004352/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004353 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004354
Guido van Rossum0368b722007-05-11 16:50:42 +00004355 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4356 with a variable target.
4357*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004358
Barry Warsawe42b18f1997-08-25 22:13:04 +00004359static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004360unpack_iterable(PyThreadState *tstate, PyObject *v,
4361 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004362{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004363 int i = 0, j = 0;
4364 Py_ssize_t ll = 0;
4365 PyObject *it; /* iter(v) */
4366 PyObject *w;
4367 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004369 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004371 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004372 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004373 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01004374 Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v))
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004375 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004376 _PyErr_Format(tstate, PyExc_TypeError,
4377 "cannot unpack non-iterable %.200s object",
Victor Stinnera102ed72020-02-07 02:24:48 +01004378 Py_TYPE(v)->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004379 }
4380 return 0;
4381 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004383 for (; i < argcnt; i++) {
4384 w = PyIter_Next(it);
4385 if (w == NULL) {
4386 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004387 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04004388 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004389 _PyErr_Format(tstate, PyExc_ValueError,
4390 "not enough values to unpack "
4391 "(expected %d, got %d)",
4392 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004393 }
4394 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004395 _PyErr_Format(tstate, PyExc_ValueError,
4396 "not enough values to unpack "
4397 "(expected at least %d, got %d)",
4398 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004399 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004400 }
4401 goto Error;
4402 }
4403 *--sp = w;
4404 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004406 if (argcntafter == -1) {
4407 /* We better have exhausted the iterator now. */
4408 w = PyIter_Next(it);
4409 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004410 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004411 goto Error;
4412 Py_DECREF(it);
4413 return 1;
4414 }
4415 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02004416 _PyErr_Format(tstate, PyExc_ValueError,
4417 "too many values to unpack (expected %d)",
4418 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004419 goto Error;
4420 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004422 l = PySequence_List(it);
4423 if (l == NULL)
4424 goto Error;
4425 *--sp = l;
4426 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004428 ll = PyList_GET_SIZE(l);
4429 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004430 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04004431 "not enough values to unpack (expected at least %d, got %zd)",
4432 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004433 goto Error;
4434 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004436 /* Pop the "after-variable" args off the list. */
4437 for (j = argcntafter; j > 0; j--, i++) {
4438 *--sp = PyList_GET_ITEM(l, ll - j);
4439 }
4440 /* Resize the list. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +01004441 Py_SET_SIZE(l, ll - argcntafter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004442 Py_DECREF(it);
4443 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004444
Tim Petersd6d010b2001-06-21 02:49:55 +00004445Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004446 for (; i > 0; i--, sp++)
4447 Py_DECREF(*sp);
4448 Py_XDECREF(it);
4449 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004450}
4451
4452
Guido van Rossum96a42c81992-01-12 02:29:51 +00004453#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004454static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004455prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004456{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004457 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02004458 if (PyObject_Print(v, stdout, 0) != 0) {
4459 /* Don't know what else to do */
4460 _PyErr_Clear(tstate);
4461 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004462 printf("\n");
4463 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004464}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004465#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004466
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004467static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004468call_exc_trace(Py_tracefunc func, PyObject *self,
4469 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004470{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004471 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004472 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004473 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004474 if (value == NULL) {
4475 value = Py_None;
4476 Py_INCREF(value);
4477 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004478 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01004479 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004480 arg = PyTuple_Pack(3, type, value, traceback);
4481 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004482 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004483 return;
4484 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004485 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004486 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02004487 if (err == 0) {
4488 _PyErr_Restore(tstate, type, value, orig_traceback);
4489 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004490 else {
4491 Py_XDECREF(type);
4492 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004493 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004494 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004495}
4496
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004497static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004498call_trace_protected(Py_tracefunc func, PyObject *obj,
4499 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004500 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004501{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004502 PyObject *type, *value, *traceback;
4503 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004504 _PyErr_Fetch(tstate, &type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004505 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004506 if (err == 0)
4507 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004508 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004509 return 0;
4510 }
4511 else {
4512 Py_XDECREF(type);
4513 Py_XDECREF(value);
4514 Py_XDECREF(traceback);
4515 return -1;
4516 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004517}
4518
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004519static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004520call_trace(Py_tracefunc func, PyObject *obj,
4521 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004522 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004523{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004524 int result;
4525 if (tstate->tracing)
4526 return 0;
4527 tstate->tracing++;
4528 tstate->use_tracing = 0;
4529 result = func(obj, frame, what, arg);
4530 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4531 || (tstate->c_profilefunc != NULL));
4532 tstate->tracing--;
4533 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004534}
4535
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004536PyObject *
4537_PyEval_CallTracing(PyObject *func, PyObject *args)
4538{
Victor Stinner50b48572018-11-01 01:51:40 +01004539 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004540 int save_tracing = tstate->tracing;
4541 int save_use_tracing = tstate->use_tracing;
4542 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004543
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004544 tstate->tracing = 0;
4545 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4546 || (tstate->c_profilefunc != NULL));
4547 result = PyObject_Call(func, args, NULL);
4548 tstate->tracing = save_tracing;
4549 tstate->use_tracing = save_use_tracing;
4550 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004551}
4552
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004553/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004554static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004555maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004556 PyThreadState *tstate, PyFrameObject *frame,
4557 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004558{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004559 int result = 0;
4560 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004562 /* If the last instruction executed isn't in the current
4563 instruction window, reset the window.
4564 */
4565 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4566 PyAddrPair bounds;
4567 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4568 &bounds);
4569 *instr_lb = bounds.ap_lower;
4570 *instr_ub = bounds.ap_upper;
4571 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004572 /* If the last instruction falls at the start of a line or if it
4573 represents a jump backwards, update the frame's line number and
4574 then call the trace function if we're tracing source lines.
4575 */
4576 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004577 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004578 if (frame->f_trace_lines) {
4579 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4580 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004581 }
George King20faa682017-10-18 17:44:22 -07004582 /* Always emit an opcode event if we're tracing all opcodes. */
4583 if (frame->f_trace_opcodes) {
4584 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4585 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004586 *instr_prev = frame->f_lasti;
4587 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004588}
4589
Fred Drake5755ce62001-06-27 19:19:46 +00004590void
4591PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004592{
Steve Dowerb82e17e2019-05-23 08:45:22 -07004593 if (PySys_Audit("sys.setprofile", NULL) < 0) {
4594 return;
4595 }
4596
Victor Stinner50b48572018-11-01 01:51:40 +01004597 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004598 PyObject *temp = tstate->c_profileobj;
4599 Py_XINCREF(arg);
4600 tstate->c_profilefunc = NULL;
4601 tstate->c_profileobj = NULL;
4602 /* Must make sure that tracing is not ignored if 'temp' is freed */
4603 tstate->use_tracing = tstate->c_tracefunc != NULL;
4604 Py_XDECREF(temp);
4605 tstate->c_profilefunc = func;
4606 tstate->c_profileobj = arg;
4607 /* Flag that tracing or profiling is turned on */
4608 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004609}
4610
4611void
4612PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4613{
Steve Dowerb82e17e2019-05-23 08:45:22 -07004614 if (PySys_Audit("sys.settrace", NULL) < 0) {
4615 return;
4616 }
4617
Victor Stinner09532fe2019-05-10 23:39:09 +02004618 _PyRuntimeState *runtime = &_PyRuntime;
4619 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004620 PyObject *temp = tstate->c_traceobj;
Victor Stinner09532fe2019-05-10 23:39:09 +02004621 runtime->ceval.tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004622 Py_XINCREF(arg);
4623 tstate->c_tracefunc = NULL;
4624 tstate->c_traceobj = NULL;
4625 /* Must make sure that profiling is not ignored if 'temp' is freed */
4626 tstate->use_tracing = tstate->c_profilefunc != NULL;
4627 Py_XDECREF(temp);
4628 tstate->c_tracefunc = func;
4629 tstate->c_traceobj = arg;
4630 /* Flag that tracing or profiling is turned on */
4631 tstate->use_tracing = ((func != NULL)
4632 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004633}
4634
Yury Selivanov75445082015-05-11 22:57:16 -04004635void
Victor Stinner838f2642019-06-13 22:41:23 +02004636_PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004637{
4638 assert(new_depth >= 0);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004639 tstate->coroutine_origin_tracking_depth = new_depth;
4640}
4641
4642int
4643_PyEval_GetCoroutineOriginTrackingDepth(void)
4644{
Victor Stinner50b48572018-11-01 01:51:40 +01004645 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004646 return tstate->coroutine_origin_tracking_depth;
4647}
4648
4649void
Yury Selivanoveb636452016-09-08 22:01:51 -07004650_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4651{
Victor Stinner50b48572018-11-01 01:51:40 +01004652 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004653
4654 if (PySys_Audit("sys.set_asyncgen_hook_firstiter", NULL) < 0) {
4655 return;
4656 }
4657
Yury Selivanoveb636452016-09-08 22:01:51 -07004658 Py_XINCREF(firstiter);
4659 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4660}
4661
4662PyObject *
4663_PyEval_GetAsyncGenFirstiter(void)
4664{
Victor Stinner50b48572018-11-01 01:51:40 +01004665 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004666 return tstate->async_gen_firstiter;
4667}
4668
4669void
4670_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4671{
Victor Stinner50b48572018-11-01 01:51:40 +01004672 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004673
4674 if (PySys_Audit("sys.set_asyncgen_hook_finalizer", NULL) < 0) {
4675 return;
4676 }
4677
Yury Selivanoveb636452016-09-08 22:01:51 -07004678 Py_XINCREF(finalizer);
4679 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4680}
4681
4682PyObject *
4683_PyEval_GetAsyncGenFinalizer(void)
4684{
Victor Stinner50b48572018-11-01 01:51:40 +01004685 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004686 return tstate->async_gen_finalizer;
4687}
4688
Victor Stinner438a12d2019-05-24 17:01:38 +02004689static PyFrameObject *
4690_PyEval_GetFrame(PyThreadState *tstate)
4691{
Victor Stinner01b1cc12019-11-20 02:27:56 +01004692 _PyRuntimeState *runtime = tstate->interp->runtime;
4693 return runtime->gilstate.getframe(tstate);
Victor Stinner438a12d2019-05-24 17:01:38 +02004694}
4695
4696PyFrameObject *
4697PyEval_GetFrame(void)
4698{
4699 PyThreadState *tstate = _PyThreadState_GET();
4700 return _PyEval_GetFrame(tstate);
4701}
4702
Guido van Rossumb209a111997-04-29 18:18:01 +00004703PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004704PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004705{
Victor Stinner438a12d2019-05-24 17:01:38 +02004706 PyThreadState *tstate = _PyThreadState_GET();
4707 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004708 if (current_frame == NULL)
Victor Stinner438a12d2019-05-24 17:01:38 +02004709 return tstate->interp->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004710 else
4711 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004712}
4713
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004714/* Convenience function to get a builtin from its name */
4715PyObject *
4716_PyEval_GetBuiltinId(_Py_Identifier *name)
4717{
Victor Stinner438a12d2019-05-24 17:01:38 +02004718 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004719 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4720 if (attr) {
4721 Py_INCREF(attr);
4722 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004723 else if (!_PyErr_Occurred(tstate)) {
4724 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004725 }
4726 return attr;
4727}
4728
Guido van Rossumb209a111997-04-29 18:18:01 +00004729PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004730PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004731{
Victor Stinner438a12d2019-05-24 17:01:38 +02004732 PyThreadState *tstate = _PyThreadState_GET();
4733 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Victor Stinner41bb43a2013-10-29 01:19:37 +01004734 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004735 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004736 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004737 }
4738
Victor Stinner438a12d2019-05-24 17:01:38 +02004739 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01004740 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004741 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004742
4743 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004744 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004745}
4746
Guido van Rossumb209a111997-04-29 18:18:01 +00004747PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004748PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004749{
Victor Stinner438a12d2019-05-24 17:01:38 +02004750 PyThreadState *tstate = _PyThreadState_GET();
4751 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
4752 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004753 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004754 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004755
4756 assert(current_frame->f_globals != NULL);
4757 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004758}
4759
Guido van Rossum6135a871995-01-09 17:53:26 +00004760int
Tim Peters5ba58662001-07-16 02:29:45 +00004761PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004762{
Victor Stinner438a12d2019-05-24 17:01:38 +02004763 PyThreadState *tstate = _PyThreadState_GET();
4764 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004765 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004766
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004767 if (current_frame != NULL) {
4768 const int codeflags = current_frame->f_code->co_flags;
4769 const int compilerflags = codeflags & PyCF_MASK;
4770 if (compilerflags) {
4771 result = 1;
4772 cf->cf_flags |= compilerflags;
4773 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004774#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004775 if (codeflags & CO_GENERATOR_ALLOWED) {
4776 result = 1;
4777 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4778 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004779#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004780 }
4781 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004782}
4783
Guido van Rossum3f5da241990-12-20 15:06:42 +00004784
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004785const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004786PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004787{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004788 if (PyMethod_Check(func))
4789 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4790 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004791 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004792 else if (PyCFunction_Check(func))
4793 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4794 else
Victor Stinnera102ed72020-02-07 02:24:48 +01004795 return Py_TYPE(func)->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004796}
4797
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004798const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004799PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004800{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004801 if (PyMethod_Check(func))
4802 return "()";
4803 else if (PyFunction_Check(func))
4804 return "()";
4805 else if (PyCFunction_Check(func))
4806 return "()";
4807 else
4808 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004809}
4810
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004811#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004812if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004813 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4814 tstate, tstate->frame, \
4815 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004816 x = NULL; \
4817 } \
4818 else { \
4819 x = call; \
4820 if (tstate->c_profilefunc != NULL) { \
4821 if (x == NULL) { \
4822 call_trace_protected(tstate->c_profilefunc, \
4823 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004824 tstate, tstate->frame, \
4825 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004826 /* XXX should pass (type, value, tb) */ \
4827 } else { \
4828 if (call_trace(tstate->c_profilefunc, \
4829 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004830 tstate, tstate->frame, \
4831 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004832 Py_DECREF(x); \
4833 x = NULL; \
4834 } \
4835 } \
4836 } \
4837 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004838} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004839 x = call; \
4840 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004841
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004842
4843static PyObject *
4844trace_call_function(PyThreadState *tstate,
4845 PyObject *func,
4846 PyObject **args, Py_ssize_t nargs,
4847 PyObject *kwnames)
4848{
4849 PyObject *x;
4850 if (PyCFunction_Check(func)) {
Petr Viktorinffd97532020-02-11 17:46:57 +01004851 C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004852 return x;
4853 }
4854 else if (Py_TYPE(func) == &PyMethodDescr_Type && nargs > 0) {
4855 /* We need to create a temporary bound method as argument
4856 for profiling.
4857
4858 If nargs == 0, then this cannot work because we have no
4859 "self". In any case, the call itself would raise
4860 TypeError (foo needs an argument), so we just skip
4861 profiling. */
4862 PyObject *self = args[0];
4863 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4864 if (func == NULL) {
4865 return NULL;
4866 }
Petr Viktorinffd97532020-02-11 17:46:57 +01004867 C_TRACE(x, PyObject_Vectorcall(func,
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02004868 args+1, nargs-1,
4869 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004870 Py_DECREF(func);
4871 return x;
4872 }
Petr Viktorinffd97532020-02-11 17:46:57 +01004873 return PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004874}
4875
Victor Stinner415c5102017-01-11 00:54:57 +01004876/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4877 to reduce the stack consumption. */
4878Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Victor Stinner09532fe2019-05-10 23:39:09 +02004879call_function(PyThreadState *tstate, PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004880{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004881 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004882 PyObject *func = *pfunc;
4883 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004884 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4885 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004886 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004887
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004888 if (tstate->use_tracing) {
4889 x = trace_call_function(tstate, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09004890 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004891 else {
Petr Viktorinffd97532020-02-11 17:46:57 +01004892 x = PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004893 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004894
Victor Stinner438a12d2019-05-24 17:01:38 +02004895 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004896
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004897 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004898 while ((*pp_stack) > pfunc) {
4899 w = EXT_POP(*pp_stack);
4900 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004901 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004903 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004904}
4905
Jeremy Hylton52820442001-01-03 23:52:36 +00004906static PyObject *
Victor Stinner09532fe2019-05-10 23:39:09 +02004907do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004908{
jdemeyere89de732018-09-19 12:06:20 +02004909 PyObject *result;
4910
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004911 if (PyCFunction_Check(func)) {
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +02004912 C_TRACE(result, PyObject_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004913 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004914 }
jdemeyere89de732018-09-19 12:06:20 +02004915 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
jdemeyere89de732018-09-19 12:06:20 +02004916 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
4917 if (nargs > 0 && tstate->use_tracing) {
4918 /* We need to create a temporary bound method as argument
4919 for profiling.
4920
4921 If nargs == 0, then this cannot work because we have no
4922 "self". In any case, the call itself would raise
4923 TypeError (foo needs an argument), so we just skip
4924 profiling. */
4925 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
4926 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4927 if (func == NULL) {
4928 return NULL;
4929 }
4930
Victor Stinner4d231bc2019-11-14 13:36:21 +01004931 C_TRACE(result, _PyObject_FastCallDictTstate(
4932 tstate, func,
4933 &_PyTuple_ITEMS(callargs)[1],
4934 nargs - 1,
4935 kwdict));
jdemeyere89de732018-09-19 12:06:20 +02004936 Py_DECREF(func);
4937 return result;
4938 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004939 }
jdemeyere89de732018-09-19 12:06:20 +02004940 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00004941}
4942
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004943/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004944 nb_index slot defined, and store in *pi.
4945 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004946 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004947 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004948*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004949int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004950_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004951{
Victor Stinner438a12d2019-05-24 17:01:38 +02004952 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004953 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004954 Py_ssize_t x;
4955 if (PyIndex_Check(v)) {
4956 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02004957 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004958 return 0;
4959 }
4960 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004961 _PyErr_SetString(tstate, PyExc_TypeError,
4962 "slice indices must be integers or "
4963 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004964 return 0;
4965 }
4966 *pi = x;
4967 }
4968 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004969}
4970
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004971int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004972_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004973{
Victor Stinner438a12d2019-05-24 17:01:38 +02004974 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004975 Py_ssize_t x;
4976 if (PyIndex_Check(v)) {
4977 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02004978 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004979 return 0;
4980 }
4981 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004982 _PyErr_SetString(tstate, PyExc_TypeError,
4983 "slice indices must be integers or "
4984 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004985 return 0;
4986 }
4987 *pi = x;
4988 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004989}
4990
Thomas Wouters52152252000-08-17 22:55:00 +00004991static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004992import_name(PyThreadState *tstate, PyFrameObject *f,
4993 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004994{
4995 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004996 PyObject *import_func, *res;
4997 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004998
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004999 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005000 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005001 if (!_PyErr_Occurred(tstate)) {
5002 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005003 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005004 return NULL;
5005 }
5006
5007 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005008 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005009 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02005010 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005011 return NULL;
5012 }
5013 res = PyImport_ImportModuleLevelObject(
5014 name,
5015 f->f_globals,
5016 f->f_locals == NULL ? Py_None : f->f_locals,
5017 fromlist,
5018 ilevel);
5019 return res;
5020 }
5021
5022 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005023
5024 stack[0] = name;
5025 stack[1] = f->f_globals;
5026 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5027 stack[3] = fromlist;
5028 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005029 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005030 Py_DECREF(import_func);
5031 return res;
5032}
5033
5034static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005035import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005036{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005037 PyObject *x;
Xiang Zhang4830f582017-03-21 11:13:42 +08005038 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005039
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005040 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005041 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005042 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005043 /* Issue #17636: in case this failed because of a circular relative
5044 import, try to fallback on reading the module directly from
5045 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02005046 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005047 if (pkgname == NULL) {
5048 goto error;
5049 }
Oren Milman6db70332017-09-19 14:23:01 +03005050 if (!PyUnicode_Check(pkgname)) {
5051 Py_CLEAR(pkgname);
5052 goto error;
5053 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005054 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005055 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005056 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005057 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005058 }
Eric Snow3f9eee62017-09-15 16:35:20 -06005059 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005060 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02005061 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07005062 goto error;
5063 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005064 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005065 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005066 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005067 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005068 if (pkgname == NULL) {
5069 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5070 if (pkgname_or_unknown == NULL) {
5071 Py_XDECREF(pkgpath);
5072 return NULL;
5073 }
5074 } else {
5075 pkgname_or_unknown = pkgname;
5076 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005077
5078 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005079 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08005080 errmsg = PyUnicode_FromFormat(
5081 "cannot import name %R from %R (unknown location)",
5082 name, pkgname_or_unknown
5083 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005084 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005085 PyErr_SetImportError(errmsg, pkgname, NULL);
5086 }
5087 else {
Anthony Sottile65366bc2019-09-09 08:17:50 -07005088 _Py_IDENTIFIER(__spec__);
5089 PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
Anthony Sottile65366bc2019-09-09 08:17:50 -07005090 const char *fmt =
5091 _PyModuleSpec_IsInitializing(spec) ?
5092 "cannot import name %R from partially initialized module %R "
5093 "(most likely due to a circular import) (%S)" :
5094 "cannot import name %R from %R (%S)";
5095 Py_XDECREF(spec);
5096
5097 errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
Stefan Krah027b09c2019-03-25 21:50:58 +01005098 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005099 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005100 }
5101
Xiang Zhang4830f582017-03-21 11:13:42 +08005102 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005103 Py_XDECREF(pkgname_or_unknown);
5104 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005105 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005106}
Guido van Rossumac7be682001-01-17 15:42:30 +00005107
Thomas Wouters52152252000-08-17 22:55:00 +00005108static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005109import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00005110{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005111 _Py_IDENTIFIER(__all__);
5112 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005113 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005114 int skip_leading_underscores = 0;
5115 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005116
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005117 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5118 return -1; /* Unexpected error */
5119 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005120 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005121 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5122 return -1;
5123 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005124 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005125 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005126 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005127 return -1;
5128 }
5129 all = PyMapping_Keys(dict);
5130 Py_DECREF(dict);
5131 if (all == NULL)
5132 return -1;
5133 skip_leading_underscores = 1;
5134 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005135
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005136 for (pos = 0, err = 0; ; pos++) {
5137 name = PySequence_GetItem(all, pos);
5138 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005139 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005140 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02005141 }
5142 else {
5143 _PyErr_Clear(tstate);
5144 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005145 break;
5146 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005147 if (!PyUnicode_Check(name)) {
5148 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5149 if (modname == NULL) {
5150 Py_DECREF(name);
5151 err = -1;
5152 break;
5153 }
5154 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005155 _PyErr_Format(tstate, PyExc_TypeError,
5156 "module __name__ must be a string, not %.100s",
5157 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005158 }
5159 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005160 _PyErr_Format(tstate, PyExc_TypeError,
5161 "%s in %U.%s must be str, not %.100s",
5162 skip_leading_underscores ? "Key" : "Item",
5163 modname,
5164 skip_leading_underscores ? "__dict__" : "__all__",
5165 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005166 }
5167 Py_DECREF(modname);
5168 Py_DECREF(name);
5169 err = -1;
5170 break;
5171 }
5172 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005173 if (PyUnicode_READY(name) == -1) {
5174 Py_DECREF(name);
5175 err = -1;
5176 break;
5177 }
5178 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5179 Py_DECREF(name);
5180 continue;
5181 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005182 }
5183 value = PyObject_GetAttr(v, name);
5184 if (value == NULL)
5185 err = -1;
5186 else if (PyDict_CheckExact(locals))
5187 err = PyDict_SetItem(locals, name, value);
5188 else
5189 err = PyObject_SetItem(locals, name, value);
5190 Py_DECREF(name);
5191 Py_XDECREF(value);
5192 if (err != 0)
5193 break;
5194 }
5195 Py_DECREF(all);
5196 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005197}
5198
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005199static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005200check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005201{
Victor Stinnera102ed72020-02-07 02:24:48 +01005202 if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005203 /* check_args_iterable() may be called with a live exception:
5204 * clear it to prevent calling _PyObject_FunctionStr() with an
5205 * exception set. */
Victor Stinner61f4db82020-01-28 03:37:45 +01005206 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005207 PyObject *funcstr = _PyObject_FunctionStr(func);
5208 if (funcstr != NULL) {
5209 _PyErr_Format(tstate, PyExc_TypeError,
5210 "%U argument after * must be an iterable, not %.200s",
5211 funcstr, Py_TYPE(args)->tp_name);
5212 Py_DECREF(funcstr);
5213 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005214 return -1;
5215 }
5216 return 0;
5217}
5218
5219static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005220format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005221{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005222 /* _PyDict_MergeEx raises attribute
5223 * error (percolated from an attempt
5224 * to get 'keys' attribute) instead of
5225 * a type error if its second argument
5226 * is not a mapping.
5227 */
Victor Stinner438a12d2019-05-24 17:01:38 +02005228 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005229 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005230 PyObject *funcstr = _PyObject_FunctionStr(func);
5231 if (funcstr != NULL) {
5232 _PyErr_Format(
5233 tstate, PyExc_TypeError,
5234 "%U argument after ** must be a mapping, not %.200s",
5235 funcstr, Py_TYPE(kwargs)->tp_name);
5236 Py_DECREF(funcstr);
5237 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005238 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005239 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005240 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02005241 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005242 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005243 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005244 PyObject *funcstr = _PyObject_FunctionStr(func);
5245 if (funcstr != NULL) {
5246 PyObject *key = PyTuple_GET_ITEM(val, 0);
5247 _PyErr_Format(
5248 tstate, PyExc_TypeError,
5249 "%U got multiple values for keyword argument '%S'",
5250 funcstr, key);
5251 Py_DECREF(funcstr);
5252 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005253 Py_XDECREF(exc);
5254 Py_XDECREF(val);
5255 Py_XDECREF(tb);
5256 }
5257 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005258 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005259 }
5260 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005261}
5262
Guido van Rossumac7be682001-01-17 15:42:30 +00005263static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005264format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
5265 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005266{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005267 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005269 if (!obj)
5270 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005271
Serhiy Storchaka06515832016-11-20 09:13:07 +02005272 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005273 if (!obj_str)
5274 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005275
Victor Stinner438a12d2019-05-24 17:01:38 +02005276 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005277}
Guido van Rossum950361c1997-01-24 13:49:28 +00005278
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005279static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005280format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005281{
5282 PyObject *name;
5283 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02005284 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005285 return;
5286 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5287 name = PyTuple_GET_ITEM(co->co_cellvars,
5288 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005289 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005290 PyExc_UnboundLocalError,
5291 UNBOUNDLOCAL_ERROR_MSG,
5292 name);
5293 } else {
5294 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5295 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02005296 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005297 UNBOUNDFREE_ERROR_MSG, name);
5298 }
5299}
5300
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005301static void
Mark Shannonfee55262019-11-21 09:11:43 +00005302format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevprevopcode, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005303{
5304 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5305 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005306 _PyErr_Format(tstate, PyExc_TypeError,
5307 "'async with' received an object from __aenter__ "
5308 "that does not implement __await__: %.100s",
5309 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005310 }
Mark Shannonfee55262019-11-21 09:11:43 +00005311 else if (prevopcode == WITH_EXCEPT_START || (prevopcode == CALL_FUNCTION && prevprevopcode == DUP_TOP)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005312 _PyErr_Format(tstate, PyExc_TypeError,
5313 "'async with' received an object from __aexit__ "
5314 "that does not implement __await__: %.100s",
5315 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005316 }
5317 }
5318}
5319
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005320static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005321unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005322 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005323{
5324 PyObject *res;
5325 if (Py_REFCNT(v) == 2) {
5326 /* In the common case, there are 2 references to the value
5327 * stored in 'variable' when the += is performed: one on the
5328 * value stack (in 'v') and one still stored in the
5329 * 'variable'. We try to delete the variable now to reduce
5330 * the refcnt to 1.
5331 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005332 int opcode, oparg;
5333 NEXTOPARG();
5334 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005335 case STORE_FAST:
5336 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005337 PyObject **fastlocals = f->f_localsplus;
5338 if (GETLOCAL(oparg) == v)
5339 SETLOCAL(oparg, NULL);
5340 break;
5341 }
5342 case STORE_DEREF:
5343 {
5344 PyObject **freevars = (f->f_localsplus +
5345 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005346 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005347 if (PyCell_GET(c) == v) {
5348 PyCell_SET(c, NULL);
5349 Py_DECREF(v);
5350 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005351 break;
5352 }
5353 case STORE_NAME:
5354 {
5355 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005356 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005357 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005358 if (locals && PyDict_CheckExact(locals)) {
5359 PyObject *w = PyDict_GetItemWithError(locals, name);
5360 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02005361 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005362 {
5363 Py_DECREF(v);
5364 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005365 }
5366 }
5367 break;
5368 }
5369 }
5370 }
5371 res = v;
5372 PyUnicode_Append(&res, w);
5373 return res;
5374}
5375
Guido van Rossum950361c1997-01-24 13:49:28 +00005376#ifdef DYNAMIC_EXECUTION_PROFILE
5377
Skip Montanarof118cb12001-10-15 20:51:38 +00005378static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005379getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005380{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005381 int i;
5382 PyObject *l = PyList_New(256);
5383 if (l == NULL) return NULL;
5384 for (i = 0; i < 256; i++) {
5385 PyObject *x = PyLong_FromLong(a[i]);
5386 if (x == NULL) {
5387 Py_DECREF(l);
5388 return NULL;
5389 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005390 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005391 }
5392 for (i = 0; i < 256; i++)
5393 a[i] = 0;
5394 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005395}
5396
5397PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005398_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005399{
5400#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005401 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005402#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005403 int i;
5404 PyObject *l = PyList_New(257);
5405 if (l == NULL) return NULL;
5406 for (i = 0; i < 257; i++) {
5407 PyObject *x = getarray(dxpairs[i]);
5408 if (x == NULL) {
5409 Py_DECREF(l);
5410 return NULL;
5411 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005412 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005413 }
5414 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005415#endif
5416}
5417
5418#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005419
5420Py_ssize_t
5421_PyEval_RequestCodeExtraIndex(freefunc free)
5422{
Victor Stinnercaba55b2018-08-03 15:33:52 +02005423 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -07005424 Py_ssize_t new_index;
5425
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005426 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005427 return -1;
5428 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005429 new_index = interp->co_extra_user_count++;
5430 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005431 return new_index;
5432}
Łukasz Langaa785c872016-09-09 17:37:37 -07005433
5434static void
5435dtrace_function_entry(PyFrameObject *f)
5436{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005437 const char *filename;
5438 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005439 int lineno;
5440
5441 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5442 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5443 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5444
Andy Lestere6be9b52020-02-11 20:28:35 -06005445 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005446}
5447
5448static void
5449dtrace_function_return(PyFrameObject *f)
5450{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005451 const char *filename;
5452 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005453 int lineno;
5454
5455 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5456 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5457 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5458
Andy Lestere6be9b52020-02-11 20:28:35 -06005459 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005460}
5461
5462/* DTrace equivalent of maybe_call_line_trace. */
5463static void
5464maybe_dtrace_line(PyFrameObject *frame,
5465 int *instr_lb, int *instr_ub, int *instr_prev)
5466{
5467 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005468 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005469
5470 /* If the last instruction executed isn't in the current
5471 instruction window, reset the window.
5472 */
5473 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5474 PyAddrPair bounds;
5475 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5476 &bounds);
5477 *instr_lb = bounds.ap_lower;
5478 *instr_ub = bounds.ap_upper;
5479 }
5480 /* If the last instruction falls at the start of a line or if
5481 it represents a jump backwards, update the frame's line
5482 number and call the trace function. */
5483 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5484 frame->f_lineno = line;
5485 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5486 if (!co_filename)
5487 co_filename = "?";
5488 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5489 if (!co_name)
5490 co_name = "?";
Andy Lestere6be9b52020-02-11 20:28:35 -06005491 PyDTrace_LINE(co_filename, co_name, line);
Łukasz Langaa785c872016-09-09 17:37:37 -07005492 }
5493 *instr_prev = frame->f_lasti;
5494}
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005495
5496
5497/* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions
5498 for the limited API. */
5499
5500#undef Py_EnterRecursiveCall
5501
5502int Py_EnterRecursiveCall(const char *where)
5503{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005504 return _Py_EnterRecursiveCall_inline(where);
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005505}
5506
5507#undef Py_LeaveRecursiveCall
5508
5509void Py_LeaveRecursiveCall(void)
5510{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005511 _Py_LeaveRecursiveCall_inline();
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005512}