blob: 426d0bbee8901cab1ff1245a8a898e0edef2c938 [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
Antoine Pitrou042b1282010-08-13 21:15:58 +0000924#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000926#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300927#define PREDICT(op) \
928 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300929 _Py_CODEUNIT word = *next_instr; \
930 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300931 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300932 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300933 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300934 goto PRED_##op; \
935 } \
936 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000937#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300938#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000939
Raymond Hettingerf606f872003-03-16 03:11:04 +0000940
Guido van Rossum374a9221991-04-04 10:40:29 +0000941/* Stack manipulation macros */
942
Martin v. Löwis18e16552006-02-15 17:27:45 +0000943/* The stack can grow at most MAXINT deep, as co_nlocals and
944 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000945#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
946#define EMPTY() (STACK_LEVEL() == 0)
947#define TOP() (stack_pointer[-1])
948#define SECOND() (stack_pointer[-2])
949#define THIRD() (stack_pointer[-3])
950#define FOURTH() (stack_pointer[-4])
951#define PEEK(n) (stack_pointer[-(n)])
952#define SET_TOP(v) (stack_pointer[-1] = (v))
953#define SET_SECOND(v) (stack_pointer[-2] = (v))
954#define SET_THIRD(v) (stack_pointer[-3] = (v))
955#define SET_FOURTH(v) (stack_pointer[-4] = (v))
956#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
957#define BASIC_STACKADJ(n) (stack_pointer += n)
958#define BASIC_PUSH(v) (*stack_pointer++ = (v))
959#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000960
Guido van Rossum96a42c81992-01-12 02:29:51 +0000961#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +0200963 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000964 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +0200965#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000966 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +0000967#define STACK_GROW(n) do { \
968 assert(n >= 0); \
969 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +0200970 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +0000971 assert(STACK_LEVEL() <= co->co_stacksize); \
972 } while (0)
973#define STACK_SHRINK(n) do { \
974 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +0200975 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +0000976 (void)(BASIC_STACKADJ(-n)); \
977 assert(STACK_LEVEL() <= co->co_stacksize); \
978 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +0000979#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +0200980 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000981 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000982#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000983#define PUSH(v) BASIC_PUSH(v)
984#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +0000985#define STACK_GROW(n) BASIC_STACKADJ(n)
986#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000987#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000988#endif
989
Guido van Rossum681d79a1995-07-18 14:51:37 +0000990/* Local variable macros */
991
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000993
994/* The SETLOCAL() macro must not DECREF the local variable in-place and
995 then store the new value; it must copy the old value to a temporary
996 value, then store the new value, and then DECREF the temporary value.
997 This is because it is possible that during the DECREF the frame is
998 accessed by other code (e.g. a __del__ method or gc.collect()) and the
999 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001001 GETLOCAL(i) = value; \
1002 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001003
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001004
1005#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 while (STACK_LEVEL() > (b)->b_level) { \
1007 PyObject *v = POP(); \
1008 Py_XDECREF(v); \
1009 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001010
1011#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001012 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +01001014 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1016 while (STACK_LEVEL() > (b)->b_level + 3) { \
1017 value = POP(); \
1018 Py_XDECREF(value); \
1019 } \
Mark Shannonae3087c2017-10-22 22:41:51 +01001020 exc_info = tstate->exc_info; \
1021 type = exc_info->exc_type; \
1022 value = exc_info->exc_value; \
1023 traceback = exc_info->exc_traceback; \
1024 exc_info->exc_type = POP(); \
1025 exc_info->exc_value = POP(); \
1026 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 Py_XDECREF(type); \
1028 Py_XDECREF(value); \
1029 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001030 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001031
Inada Naoki91234a12019-06-03 21:30:58 +09001032 /* macros for opcode cache */
1033#define OPCACHE_CHECK() \
1034 do { \
1035 co_opcache = NULL; \
1036 if (co->co_opcache != NULL) { \
1037 unsigned char co_opt_offset = \
1038 co->co_opcache_map[next_instr - first_instr]; \
1039 if (co_opt_offset > 0) { \
1040 assert(co_opt_offset <= co->co_opcache_size); \
1041 co_opcache = &co->co_opcache[co_opt_offset - 1]; \
1042 assert(co_opcache != NULL); \
Inada Naoki91234a12019-06-03 21:30:58 +09001043 } \
1044 } \
1045 } while (0)
1046
1047#if OPCACHE_STATS
1048
1049#define OPCACHE_STAT_GLOBAL_HIT() \
1050 do { \
1051 if (co->co_opcache != NULL) opcache_global_hits++; \
1052 } while (0)
1053
1054#define OPCACHE_STAT_GLOBAL_MISS() \
1055 do { \
1056 if (co->co_opcache != NULL) opcache_global_misses++; \
1057 } while (0)
1058
1059#define OPCACHE_STAT_GLOBAL_OPT() \
1060 do { \
1061 if (co->co_opcache != NULL) opcache_global_opts++; \
1062 } while (0)
1063
1064#else /* OPCACHE_STATS */
1065
1066#define OPCACHE_STAT_GLOBAL_HIT()
1067#define OPCACHE_STAT_GLOBAL_MISS()
1068#define OPCACHE_STAT_GLOBAL_OPT()
1069
1070#endif
1071
Guido van Rossuma027efa1997-05-05 20:56:21 +00001072/* Start of code */
1073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 /* push frame */
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001075 if (_Py_EnterRecursiveCall(tstate, "")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001077 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001078
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001080
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081 if (tstate->use_tracing) {
1082 if (tstate->c_tracefunc != NULL) {
1083 /* tstate->c_tracefunc, if defined, is a
1084 function that will be called on *every* entry
1085 to a code block. Its return value, if not
1086 None, is a function that will be called at
1087 the start of each executed line of code.
1088 (Actually, the function must return itself
1089 in order to continue tracing.) The trace
1090 functions are called with three arguments:
1091 a pointer to the current frame, a string
1092 indicating why the function is called, and
1093 an argument which depends on the situation.
1094 The global trace function is also called
1095 whenever an exception is detected. */
1096 if (call_trace_protected(tstate->c_tracefunc,
1097 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001098 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 /* Trace function raised an error */
1100 goto exit_eval_frame;
1101 }
1102 }
1103 if (tstate->c_profilefunc != NULL) {
1104 /* Similar for c_profilefunc, except it needn't
1105 return itself and isn't called for "line" events */
1106 if (call_trace_protected(tstate->c_profilefunc,
1107 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001108 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 /* Profile function raised an error */
1110 goto exit_eval_frame;
1111 }
1112 }
1113 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001114
Łukasz Langaa785c872016-09-09 17:37:37 -07001115 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1116 dtrace_function_entry(f);
1117
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 co = f->f_code;
1119 names = co->co_names;
1120 consts = co->co_consts;
1121 fastlocals = f->f_localsplus;
1122 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001123 assert(PyBytes_Check(co->co_code));
1124 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001125 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1126 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1127 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001128 /*
1129 f->f_lasti refers to the index of the last instruction,
1130 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001131
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001132 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001133 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001134
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 When the PREDICT() macros are enabled, some opcode pairs follow in
1136 direct succession without updating f->f_lasti. A successful
1137 prediction effectively links the two codes together as if they
1138 were a single new opcode; accordingly,f->f_lasti will point to
1139 the first code in the pair (for instance, GET_ITER followed by
1140 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001141 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001143 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001144 next_instr = first_instr;
1145 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001146 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1147 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001148 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 stack_pointer = f->f_stacktop;
1150 assert(stack_pointer != NULL);
1151 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001152 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001153
Inada Naoki91234a12019-06-03 21:30:58 +09001154 if (co->co_opcache_flag < OPCACHE_MIN_RUNS) {
1155 co->co_opcache_flag++;
1156 if (co->co_opcache_flag == OPCACHE_MIN_RUNS) {
1157 if (_PyCode_InitOpcache(co) < 0) {
1158 return NULL;
1159 }
1160#if OPCACHE_STATS
1161 opcache_code_objects_extra_mem +=
1162 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1163 sizeof(_PyOpcache) * co->co_opcache_size;
1164 opcache_code_objects++;
1165#endif
1166 }
1167 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001168
Tim Peters5ca576e2001-06-18 22:08:13 +00001169#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001170 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001171#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001172
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001173 if (throwflag) /* support for generator.throw() */
1174 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001175
Victor Stinnerace47d72013-07-18 01:41:08 +02001176#ifdef Py_DEBUG
1177 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001178 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001179 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001180 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001181#endif
1182
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001183main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1186 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001187 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 /* Do periodic things. Doing this every time through
1190 the loop would add too much overhead, so we do it
1191 only every Nth instruction. We also do it if
1192 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1193 event needs attention (e.g. a signal handler or
1194 async I/O handler); see Py_AddPendingCall() and
1195 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001196
Eric Snow7bda9de2019-03-08 17:25:54 -07001197 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001198 opcode = _Py_OPCODE(*next_instr);
1199 if (opcode == SETUP_FINALLY ||
1200 opcode == SETUP_WITH ||
1201 opcode == BEFORE_ASYNC_WITH ||
1202 opcode == YIELD_FROM) {
1203 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001204 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001205 - If we're about to enter the 'with:'. It will prevent
1206 emitting a resource warning in the common idiom
1207 'with open(path) as file:'.
1208 - If we're about to enter the 'async with:'.
1209 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001210 *very* useful, but might help in some cases and it's
1211 traditional)
1212 - If we're resuming a chain of nested 'yield from' or
1213 'await' calls, then each frame is parked with YIELD_FROM
1214 as its next opcode. If the user hit control-C we want to
1215 wait until we've reached the innermost frame before
1216 running the signal handler and raising KeyboardInterrupt
1217 (see bpo-30039).
1218 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 goto fast_next_opcode;
1220 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001221
Victor Stinnere225beb2019-06-03 18:14:24 +02001222 if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
Victor Stinner09532fe2019-05-10 23:39:09 +02001223 if (handle_signals(runtime) != 0) {
Eric Snowfdf282d2019-01-11 14:26:55 -07001224 goto error;
1225 }
1226 }
Victor Stinnere225beb2019-06-03 18:14:24 +02001227 if (_Py_atomic_load_relaxed(&ceval->pending.calls_to_do)) {
1228 if (make_pending_calls(runtime) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001229 goto error;
Eric Snowfdf282d2019-01-11 14:26:55 -07001230 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001231 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001232
Victor Stinnere225beb2019-06-03 18:14:24 +02001233 if (_Py_atomic_load_relaxed(&ceval->gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 /* Give another thread a chance */
Victor Stinner09532fe2019-05-10 23:39:09 +02001235 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 Py_FatalError("ceval: tstate mix-up");
Victor Stinner09532fe2019-05-10 23:39:09 +02001237 }
Victor Stinnere225beb2019-06-03 18:14:24 +02001238 drop_gil(ceval, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239
1240 /* Other threads may run now */
1241
Victor Stinnere225beb2019-06-03 18:14:24 +02001242 take_gil(ceval, tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001243
1244 /* Check if we should make a quick exit. */
Victor Stinner01b1cc12019-11-20 02:27:56 +01001245 exit_thread_if_finalizing(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001246
Victor Stinner09532fe2019-05-10 23:39:09 +02001247 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 Py_FatalError("ceval: orphan tstate");
Victor Stinner09532fe2019-05-10 23:39:09 +02001249 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 }
1251 /* Check for asynchronous exceptions. */
1252 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001253 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 tstate->async_exc = NULL;
Victor Stinnere225beb2019-06-03 18:14:24 +02001255 UNSIGNAL_ASYNC_EXC(ceval);
Victor Stinner438a12d2019-05-24 17:01:38 +02001256 _PyErr_SetNone(tstate, exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001257 Py_DECREF(exc);
1258 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 }
1260 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001261
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001262 fast_next_opcode:
1263 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001264
Łukasz Langaa785c872016-09-09 17:37:37 -07001265 if (PyDTrace_LINE_ENABLED())
1266 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001269
Victor Stinnere225beb2019-06-03 18:14:24 +02001270 if (_Py_TracingPossible(ceval) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001271 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001272 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 /* see maybe_call_line_trace
1274 for expository comments */
1275 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 err = maybe_call_line_trace(tstate->c_tracefunc,
1278 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001279 tstate, f,
1280 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 /* Reload possibly changed frame fields */
1282 JUMPTO(f->f_lasti);
1283 if (f->f_stacktop != NULL) {
1284 stack_pointer = f->f_stacktop;
1285 f->f_stacktop = NULL;
1286 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001287 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001289 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001293
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001294 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001295 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001296#ifdef DYNAMIC_EXECUTION_PROFILE
1297#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 dxpairs[lastopcode][opcode]++;
1299 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001300#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001301 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001302#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001303
Guido van Rossum96a42c81992-01-12 02:29:51 +00001304#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 if (lltrace) {
1308 if (HAS_ARG(opcode)) {
1309 printf("%d: %d, %d\n",
1310 f->f_lasti, opcode, oparg);
1311 }
1312 else {
1313 printf("%d: %d\n",
1314 f->f_lasti, opcode);
1315 }
1316 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001317#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001322 It is essential that any operation that fails must goto error
1323 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001324
Benjamin Petersonddd19492018-09-16 22:38:02 -07001325 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001327 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001328
Benjamin Petersonddd19492018-09-16 22:38:02 -07001329 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001330 PyObject *value = GETLOCAL(oparg);
1331 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001332 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001333 UNBOUNDLOCAL_ERROR_MSG,
1334 PyTuple_GetItem(co->co_varnames, oparg));
1335 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001337 Py_INCREF(value);
1338 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001340 }
1341
Benjamin Petersonddd19492018-09-16 22:38:02 -07001342 case TARGET(LOAD_CONST): {
1343 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001344 PyObject *value = GETITEM(consts, oparg);
1345 Py_INCREF(value);
1346 PUSH(value);
1347 FAST_DISPATCH();
1348 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001349
Benjamin Petersonddd19492018-09-16 22:38:02 -07001350 case TARGET(STORE_FAST): {
1351 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001352 PyObject *value = POP();
1353 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001355 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001356
Benjamin Petersonddd19492018-09-16 22:38:02 -07001357 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001358 PyObject *value = POP();
1359 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001361 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001362
Benjamin Petersonddd19492018-09-16 22:38:02 -07001363 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001364 PyObject *top = TOP();
1365 PyObject *second = SECOND();
1366 SET_TOP(second);
1367 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001368 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001369 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001370
Benjamin Petersonddd19492018-09-16 22:38:02 -07001371 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001372 PyObject *top = TOP();
1373 PyObject *second = SECOND();
1374 PyObject *third = THIRD();
1375 SET_TOP(second);
1376 SET_SECOND(third);
1377 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001379 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001380
Benjamin Petersonddd19492018-09-16 22:38:02 -07001381 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001382 PyObject *top = TOP();
1383 PyObject *second = SECOND();
1384 PyObject *third = THIRD();
1385 PyObject *fourth = FOURTH();
1386 SET_TOP(second);
1387 SET_SECOND(third);
1388 SET_THIRD(fourth);
1389 SET_FOURTH(top);
1390 FAST_DISPATCH();
1391 }
1392
Benjamin Petersonddd19492018-09-16 22:38:02 -07001393 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001394 PyObject *top = TOP();
1395 Py_INCREF(top);
1396 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001398 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001399
Benjamin Petersonddd19492018-09-16 22:38:02 -07001400 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001401 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001402 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001403 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001404 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001405 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001406 SET_TOP(top);
1407 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001408 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001409 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001410
Benjamin Petersonddd19492018-09-16 22:38:02 -07001411 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001412 PyObject *value = TOP();
1413 PyObject *res = PyNumber_Positive(value);
1414 Py_DECREF(value);
1415 SET_TOP(res);
1416 if (res == NULL)
1417 goto error;
1418 DISPATCH();
1419 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001420
Benjamin Petersonddd19492018-09-16 22:38:02 -07001421 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001422 PyObject *value = TOP();
1423 PyObject *res = PyNumber_Negative(value);
1424 Py_DECREF(value);
1425 SET_TOP(res);
1426 if (res == NULL)
1427 goto error;
1428 DISPATCH();
1429 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001430
Benjamin Petersonddd19492018-09-16 22:38:02 -07001431 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001432 PyObject *value = TOP();
1433 int err = PyObject_IsTrue(value);
1434 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 if (err == 0) {
1436 Py_INCREF(Py_True);
1437 SET_TOP(Py_True);
1438 DISPATCH();
1439 }
1440 else if (err > 0) {
1441 Py_INCREF(Py_False);
1442 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 DISPATCH();
1444 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001445 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001446 goto error;
1447 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001448
Benjamin Petersonddd19492018-09-16 22:38:02 -07001449 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001450 PyObject *value = TOP();
1451 PyObject *res = PyNumber_Invert(value);
1452 Py_DECREF(value);
1453 SET_TOP(res);
1454 if (res == NULL)
1455 goto error;
1456 DISPATCH();
1457 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001458
Benjamin Petersonddd19492018-09-16 22:38:02 -07001459 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001460 PyObject *exp = POP();
1461 PyObject *base = TOP();
1462 PyObject *res = PyNumber_Power(base, exp, Py_None);
1463 Py_DECREF(base);
1464 Py_DECREF(exp);
1465 SET_TOP(res);
1466 if (res == NULL)
1467 goto error;
1468 DISPATCH();
1469 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001470
Benjamin Petersonddd19492018-09-16 22:38:02 -07001471 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001472 PyObject *right = POP();
1473 PyObject *left = TOP();
1474 PyObject *res = PyNumber_Multiply(left, right);
1475 Py_DECREF(left);
1476 Py_DECREF(right);
1477 SET_TOP(res);
1478 if (res == NULL)
1479 goto error;
1480 DISPATCH();
1481 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001482
Benjamin Petersonddd19492018-09-16 22:38:02 -07001483 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001484 PyObject *right = POP();
1485 PyObject *left = TOP();
1486 PyObject *res = PyNumber_MatrixMultiply(left, right);
1487 Py_DECREF(left);
1488 Py_DECREF(right);
1489 SET_TOP(res);
1490 if (res == NULL)
1491 goto error;
1492 DISPATCH();
1493 }
1494
Benjamin Petersonddd19492018-09-16 22:38:02 -07001495 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001496 PyObject *divisor = POP();
1497 PyObject *dividend = TOP();
1498 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1499 Py_DECREF(dividend);
1500 Py_DECREF(divisor);
1501 SET_TOP(quotient);
1502 if (quotient == NULL)
1503 goto error;
1504 DISPATCH();
1505 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001506
Benjamin Petersonddd19492018-09-16 22:38:02 -07001507 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001508 PyObject *divisor = POP();
1509 PyObject *dividend = TOP();
1510 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1511 Py_DECREF(dividend);
1512 Py_DECREF(divisor);
1513 SET_TOP(quotient);
1514 if (quotient == NULL)
1515 goto error;
1516 DISPATCH();
1517 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001518
Benjamin Petersonddd19492018-09-16 22:38:02 -07001519 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001520 PyObject *divisor = POP();
1521 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001522 PyObject *res;
1523 if (PyUnicode_CheckExact(dividend) && (
1524 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1525 // fast path; string formatting, but not if the RHS is a str subclass
1526 // (see issue28598)
1527 res = PyUnicode_Format(dividend, divisor);
1528 } else {
1529 res = PyNumber_Remainder(dividend, divisor);
1530 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001531 Py_DECREF(divisor);
1532 Py_DECREF(dividend);
1533 SET_TOP(res);
1534 if (res == NULL)
1535 goto error;
1536 DISPATCH();
1537 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001538
Benjamin Petersonddd19492018-09-16 22:38:02 -07001539 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001540 PyObject *right = POP();
1541 PyObject *left = TOP();
1542 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001543 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1544 CPython using bytecode, it is simply worthless.
1545 See http://bugs.python.org/issue21955 and
1546 http://bugs.python.org/issue10044 for the discussion. In short,
1547 no patch shown any impact on a realistic benchmark, only a minor
1548 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001549 if (PyUnicode_CheckExact(left) &&
1550 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001551 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001552 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001553 }
1554 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001555 sum = PyNumber_Add(left, right);
1556 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001557 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001558 Py_DECREF(right);
1559 SET_TOP(sum);
1560 if (sum == NULL)
1561 goto error;
1562 DISPATCH();
1563 }
1564
Benjamin Petersonddd19492018-09-16 22:38:02 -07001565 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001566 PyObject *right = POP();
1567 PyObject *left = TOP();
1568 PyObject *diff = PyNumber_Subtract(left, right);
1569 Py_DECREF(right);
1570 Py_DECREF(left);
1571 SET_TOP(diff);
1572 if (diff == NULL)
1573 goto error;
1574 DISPATCH();
1575 }
1576
Benjamin Petersonddd19492018-09-16 22:38:02 -07001577 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001578 PyObject *sub = POP();
1579 PyObject *container = TOP();
1580 PyObject *res = PyObject_GetItem(container, sub);
1581 Py_DECREF(container);
1582 Py_DECREF(sub);
1583 SET_TOP(res);
1584 if (res == NULL)
1585 goto error;
1586 DISPATCH();
1587 }
1588
Benjamin Petersonddd19492018-09-16 22:38:02 -07001589 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001590 PyObject *right = POP();
1591 PyObject *left = TOP();
1592 PyObject *res = PyNumber_Lshift(left, right);
1593 Py_DECREF(left);
1594 Py_DECREF(right);
1595 SET_TOP(res);
1596 if (res == NULL)
1597 goto error;
1598 DISPATCH();
1599 }
1600
Benjamin Petersonddd19492018-09-16 22:38:02 -07001601 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001602 PyObject *right = POP();
1603 PyObject *left = TOP();
1604 PyObject *res = PyNumber_Rshift(left, right);
1605 Py_DECREF(left);
1606 Py_DECREF(right);
1607 SET_TOP(res);
1608 if (res == NULL)
1609 goto error;
1610 DISPATCH();
1611 }
1612
Benjamin Petersonddd19492018-09-16 22:38:02 -07001613 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001614 PyObject *right = POP();
1615 PyObject *left = TOP();
1616 PyObject *res = PyNumber_And(left, right);
1617 Py_DECREF(left);
1618 Py_DECREF(right);
1619 SET_TOP(res);
1620 if (res == NULL)
1621 goto error;
1622 DISPATCH();
1623 }
1624
Benjamin Petersonddd19492018-09-16 22:38:02 -07001625 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001626 PyObject *right = POP();
1627 PyObject *left = TOP();
1628 PyObject *res = PyNumber_Xor(left, right);
1629 Py_DECREF(left);
1630 Py_DECREF(right);
1631 SET_TOP(res);
1632 if (res == NULL)
1633 goto error;
1634 DISPATCH();
1635 }
1636
Benjamin Petersonddd19492018-09-16 22:38:02 -07001637 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001638 PyObject *right = POP();
1639 PyObject *left = TOP();
1640 PyObject *res = PyNumber_Or(left, right);
1641 Py_DECREF(left);
1642 Py_DECREF(right);
1643 SET_TOP(res);
1644 if (res == NULL)
1645 goto error;
1646 DISPATCH();
1647 }
1648
Benjamin Petersonddd19492018-09-16 22:38:02 -07001649 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001650 PyObject *v = POP();
1651 PyObject *list = PEEK(oparg);
1652 int err;
1653 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001655 if (err != 0)
1656 goto error;
1657 PREDICT(JUMP_ABSOLUTE);
1658 DISPATCH();
1659 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001660
Benjamin Petersonddd19492018-09-16 22:38:02 -07001661 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001662 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001663 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001664 int err;
1665 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001666 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001667 if (err != 0)
1668 goto error;
1669 PREDICT(JUMP_ABSOLUTE);
1670 DISPATCH();
1671 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001672
Benjamin Petersonddd19492018-09-16 22:38:02 -07001673 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001674 PyObject *exp = POP();
1675 PyObject *base = TOP();
1676 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1677 Py_DECREF(base);
1678 Py_DECREF(exp);
1679 SET_TOP(res);
1680 if (res == NULL)
1681 goto error;
1682 DISPATCH();
1683 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001684
Benjamin Petersonddd19492018-09-16 22:38:02 -07001685 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001686 PyObject *right = POP();
1687 PyObject *left = TOP();
1688 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1689 Py_DECREF(left);
1690 Py_DECREF(right);
1691 SET_TOP(res);
1692 if (res == NULL)
1693 goto error;
1694 DISPATCH();
1695 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001696
Benjamin Petersonddd19492018-09-16 22:38:02 -07001697 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001698 PyObject *right = POP();
1699 PyObject *left = TOP();
1700 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1701 Py_DECREF(left);
1702 Py_DECREF(right);
1703 SET_TOP(res);
1704 if (res == NULL)
1705 goto error;
1706 DISPATCH();
1707 }
1708
Benjamin Petersonddd19492018-09-16 22:38:02 -07001709 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001710 PyObject *divisor = POP();
1711 PyObject *dividend = TOP();
1712 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1713 Py_DECREF(dividend);
1714 Py_DECREF(divisor);
1715 SET_TOP(quotient);
1716 if (quotient == NULL)
1717 goto error;
1718 DISPATCH();
1719 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001720
Benjamin Petersonddd19492018-09-16 22:38:02 -07001721 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001722 PyObject *divisor = POP();
1723 PyObject *dividend = TOP();
1724 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1725 Py_DECREF(dividend);
1726 Py_DECREF(divisor);
1727 SET_TOP(quotient);
1728 if (quotient == NULL)
1729 goto error;
1730 DISPATCH();
1731 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001732
Benjamin Petersonddd19492018-09-16 22:38:02 -07001733 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001734 PyObject *right = POP();
1735 PyObject *left = TOP();
1736 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1737 Py_DECREF(left);
1738 Py_DECREF(right);
1739 SET_TOP(mod);
1740 if (mod == NULL)
1741 goto error;
1742 DISPATCH();
1743 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001744
Benjamin Petersonddd19492018-09-16 22:38:02 -07001745 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001746 PyObject *right = POP();
1747 PyObject *left = TOP();
1748 PyObject *sum;
1749 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001750 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001751 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001752 }
1753 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001754 sum = PyNumber_InPlaceAdd(left, right);
1755 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001756 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001757 Py_DECREF(right);
1758 SET_TOP(sum);
1759 if (sum == NULL)
1760 goto error;
1761 DISPATCH();
1762 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001763
Benjamin Petersonddd19492018-09-16 22:38:02 -07001764 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001765 PyObject *right = POP();
1766 PyObject *left = TOP();
1767 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1768 Py_DECREF(left);
1769 Py_DECREF(right);
1770 SET_TOP(diff);
1771 if (diff == NULL)
1772 goto error;
1773 DISPATCH();
1774 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001775
Benjamin Petersonddd19492018-09-16 22:38:02 -07001776 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001777 PyObject *right = POP();
1778 PyObject *left = TOP();
1779 PyObject *res = PyNumber_InPlaceLshift(left, right);
1780 Py_DECREF(left);
1781 Py_DECREF(right);
1782 SET_TOP(res);
1783 if (res == NULL)
1784 goto error;
1785 DISPATCH();
1786 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001787
Benjamin Petersonddd19492018-09-16 22:38:02 -07001788 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001789 PyObject *right = POP();
1790 PyObject *left = TOP();
1791 PyObject *res = PyNumber_InPlaceRshift(left, right);
1792 Py_DECREF(left);
1793 Py_DECREF(right);
1794 SET_TOP(res);
1795 if (res == NULL)
1796 goto error;
1797 DISPATCH();
1798 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001799
Benjamin Petersonddd19492018-09-16 22:38:02 -07001800 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001801 PyObject *right = POP();
1802 PyObject *left = TOP();
1803 PyObject *res = PyNumber_InPlaceAnd(left, right);
1804 Py_DECREF(left);
1805 Py_DECREF(right);
1806 SET_TOP(res);
1807 if (res == NULL)
1808 goto error;
1809 DISPATCH();
1810 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001811
Benjamin Petersonddd19492018-09-16 22:38:02 -07001812 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001813 PyObject *right = POP();
1814 PyObject *left = TOP();
1815 PyObject *res = PyNumber_InPlaceXor(left, right);
1816 Py_DECREF(left);
1817 Py_DECREF(right);
1818 SET_TOP(res);
1819 if (res == NULL)
1820 goto error;
1821 DISPATCH();
1822 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001823
Benjamin Petersonddd19492018-09-16 22:38:02 -07001824 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001825 PyObject *right = POP();
1826 PyObject *left = TOP();
1827 PyObject *res = PyNumber_InPlaceOr(left, right);
1828 Py_DECREF(left);
1829 Py_DECREF(right);
1830 SET_TOP(res);
1831 if (res == NULL)
1832 goto error;
1833 DISPATCH();
1834 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001835
Benjamin Petersonddd19492018-09-16 22:38:02 -07001836 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001837 PyObject *sub = TOP();
1838 PyObject *container = SECOND();
1839 PyObject *v = THIRD();
1840 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001841 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001842 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001843 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001844 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001845 Py_DECREF(container);
1846 Py_DECREF(sub);
1847 if (err != 0)
1848 goto error;
1849 DISPATCH();
1850 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001851
Benjamin Petersonddd19492018-09-16 22:38:02 -07001852 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001853 PyObject *sub = TOP();
1854 PyObject *container = SECOND();
1855 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001856 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00001857 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001858 err = PyObject_DelItem(container, sub);
1859 Py_DECREF(container);
1860 Py_DECREF(sub);
1861 if (err != 0)
1862 goto error;
1863 DISPATCH();
1864 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001865
Benjamin Petersonddd19492018-09-16 22:38:02 -07001866 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01001867 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001868 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001869 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001870 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001871 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001872 _PyErr_SetString(tstate, PyExc_RuntimeError,
1873 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001874 Py_DECREF(value);
1875 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876 }
Petr Viktorinffd97532020-02-11 17:46:57 +01001877 res = PyObject_CallOneArg(hook, value);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001878 Py_DECREF(value);
1879 if (res == NULL)
1880 goto error;
1881 Py_DECREF(res);
1882 DISPATCH();
1883 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001884
Benjamin Petersonddd19492018-09-16 22:38:02 -07001885 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001886 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001887 switch (oparg) {
1888 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001889 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001890 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001891 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001892 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001893 /* fall through */
1894 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02001895 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001896 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001897 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 break;
1899 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02001900 _PyErr_SetString(tstate, PyExc_SystemError,
1901 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902 break;
1903 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001904 goto error;
1905 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001906
Benjamin Petersonddd19492018-09-16 22:38:02 -07001907 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001908 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001909 assert(f->f_iblock == 0);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00001910 assert(EMPTY());
1911 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001912 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001913
Benjamin Petersonddd19492018-09-16 22:38:02 -07001914 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001915 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001916 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001917 PyObject *obj = TOP();
1918 PyTypeObject *type = Py_TYPE(obj);
1919
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001920 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001921 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001922 }
Yury Selivanov75445082015-05-11 22:57:16 -04001923
1924 if (getter != NULL) {
1925 iter = (*getter)(obj);
1926 Py_DECREF(obj);
1927 if (iter == NULL) {
1928 SET_TOP(NULL);
1929 goto error;
1930 }
1931 }
1932 else {
1933 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02001934 _PyErr_Format(tstate, PyExc_TypeError,
1935 "'async for' requires an object with "
1936 "__aiter__ method, got %.100s",
1937 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001938 Py_DECREF(obj);
1939 goto error;
1940 }
1941
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001942 if (Py_TYPE(iter)->tp_as_async == NULL ||
1943 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001944
Yury Selivanov398ff912017-03-02 22:20:00 -05001945 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02001946 _PyErr_Format(tstate, PyExc_TypeError,
1947 "'async for' received an object from __aiter__ "
1948 "that does not implement __anext__: %.100s",
1949 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001950 Py_DECREF(iter);
1951 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001952 }
1953
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001954 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001955 DISPATCH();
1956 }
1957
Benjamin Petersonddd19492018-09-16 22:38:02 -07001958 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001959 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001960 PyObject *next_iter = NULL;
1961 PyObject *awaitable = NULL;
1962 PyObject *aiter = TOP();
1963 PyTypeObject *type = Py_TYPE(aiter);
1964
Yury Selivanoveb636452016-09-08 22:01:51 -07001965 if (PyAsyncGen_CheckExact(aiter)) {
1966 awaitable = type->tp_as_async->am_anext(aiter);
1967 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001968 goto error;
1969 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001970 } else {
1971 if (type->tp_as_async != NULL){
1972 getter = type->tp_as_async->am_anext;
1973 }
Yury Selivanov75445082015-05-11 22:57:16 -04001974
Yury Selivanoveb636452016-09-08 22:01:51 -07001975 if (getter != NULL) {
1976 next_iter = (*getter)(aiter);
1977 if (next_iter == NULL) {
1978 goto error;
1979 }
1980 }
1981 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02001982 _PyErr_Format(tstate, PyExc_TypeError,
1983 "'async for' requires an iterator with "
1984 "__anext__ method, got %.100s",
1985 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07001986 goto error;
1987 }
Yury Selivanov75445082015-05-11 22:57:16 -04001988
Yury Selivanoveb636452016-09-08 22:01:51 -07001989 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1990 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001991 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001992 PyExc_TypeError,
1993 "'async for' received an invalid object "
1994 "from __anext__: %.100s",
1995 Py_TYPE(next_iter)->tp_name);
1996
1997 Py_DECREF(next_iter);
1998 goto error;
1999 } else {
2000 Py_DECREF(next_iter);
2001 }
2002 }
Yury Selivanov75445082015-05-11 22:57:16 -04002003
2004 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002005 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002006 DISPATCH();
2007 }
2008
Benjamin Petersonddd19492018-09-16 22:38:02 -07002009 case TARGET(GET_AWAITABLE): {
2010 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002011 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002012 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002013
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002014 if (iter == NULL) {
Mark Shannonfee55262019-11-21 09:11:43 +00002015 int opcode_at_minus_3 = 0;
2016 if ((next_instr - first_instr) > 2) {
2017 opcode_at_minus_3 = _Py_OPCODE(next_instr[-3]);
2018 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002019 format_awaitable_error(tstate, Py_TYPE(iterable),
Mark Shannonfee55262019-11-21 09:11:43 +00002020 opcode_at_minus_3,
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002021 _Py_OPCODE(next_instr[-2]));
2022 }
2023
Yury Selivanov75445082015-05-11 22:57:16 -04002024 Py_DECREF(iterable);
2025
Yury Selivanovc724bae2016-03-02 11:30:46 -05002026 if (iter != NULL && PyCoro_CheckExact(iter)) {
2027 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2028 if (yf != NULL) {
2029 /* `iter` is a coroutine object that is being
2030 awaited, `yf` is a pointer to the current awaitable
2031 being awaited on. */
2032 Py_DECREF(yf);
2033 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002034 _PyErr_SetString(tstate, PyExc_RuntimeError,
2035 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002036 /* The code below jumps to `error` if `iter` is NULL. */
2037 }
2038 }
2039
Yury Selivanov75445082015-05-11 22:57:16 -04002040 SET_TOP(iter); /* Even if it's NULL */
2041
2042 if (iter == NULL) {
2043 goto error;
2044 }
2045
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002046 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002047 DISPATCH();
2048 }
2049
Benjamin Petersonddd19492018-09-16 22:38:02 -07002050 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002051 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002052 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002053 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002054 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
2055 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002056 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002057 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002058 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002059 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002060 else
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02002061 retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002062 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002063 Py_DECREF(v);
2064 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002065 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002066 if (tstate->c_tracefunc != NULL
Victor Stinner438a12d2019-05-24 17:01:38 +02002067 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002068 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002069 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002070 if (err < 0)
2071 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002072 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002073 SET_TOP(val);
2074 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002075 }
Martin Panter95f53c12016-07-18 08:23:26 +00002076 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002077 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002078 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002079 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002080 f->f_lasti -= sizeof(_Py_CODEUNIT);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002081 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002082 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002083
Benjamin Petersonddd19492018-09-16 22:38:02 -07002084 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002085 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002086
2087 if (co->co_flags & CO_ASYNC_GENERATOR) {
2088 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2089 Py_DECREF(retval);
2090 if (w == NULL) {
2091 retval = NULL;
2092 goto error;
2093 }
2094 retval = w;
2095 }
2096
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002097 f->f_stacktop = stack_pointer;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002098 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002099 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002100
Benjamin Petersonddd19492018-09-16 22:38:02 -07002101 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002102 PyObject *type, *value, *traceback;
2103 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002104 PyTryBlock *b = PyFrame_BlockPop(f);
2105 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002106 _PyErr_SetString(tstate, PyExc_SystemError,
2107 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002108 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002109 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002110 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2111 STACK_LEVEL() <= (b)->b_level + 4);
2112 exc_info = tstate->exc_info;
2113 type = exc_info->exc_type;
2114 value = exc_info->exc_value;
2115 traceback = exc_info->exc_traceback;
2116 exc_info->exc_type = POP();
2117 exc_info->exc_value = POP();
2118 exc_info->exc_traceback = POP();
2119 Py_XDECREF(type);
2120 Py_XDECREF(value);
2121 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002122 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002123 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002124
Benjamin Petersonddd19492018-09-16 22:38:02 -07002125 case TARGET(POP_BLOCK): {
2126 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002127 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002128 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002129 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002130
Mark Shannonfee55262019-11-21 09:11:43 +00002131 case TARGET(RERAISE): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002132 PyObject *exc = POP();
Mark Shannonfee55262019-11-21 09:11:43 +00002133 PyObject *val = POP();
2134 PyObject *tb = POP();
2135 assert(PyExceptionClass_Check(exc));
Victor Stinner61f4db82020-01-28 03:37:45 +01002136 _PyErr_Restore(tstate, exc, val, tb);
Mark Shannonfee55262019-11-21 09:11:43 +00002137 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002138 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002139
Benjamin Petersonddd19492018-09-16 22:38:02 -07002140 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002141 PyObject *exc = POP();
2142 assert(PyExceptionClass_Check(exc));
2143 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2144 PyTryBlock *b = PyFrame_BlockPop(f);
2145 assert(b->b_type == EXCEPT_HANDLER);
2146 Py_DECREF(exc);
2147 UNWIND_EXCEPT_HANDLER(b);
2148 Py_DECREF(POP());
2149 JUMPBY(oparg);
2150 FAST_DISPATCH();
2151 }
2152 else {
2153 PyObject *val = POP();
2154 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002155 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002156 goto exception_unwind;
2157 }
2158 }
2159
Zackery Spytzce6a0702019-08-25 03:44:09 -06002160 case TARGET(LOAD_ASSERTION_ERROR): {
2161 PyObject *value = PyExc_AssertionError;
2162 Py_INCREF(value);
2163 PUSH(value);
2164 FAST_DISPATCH();
2165 }
2166
Benjamin Petersonddd19492018-09-16 22:38:02 -07002167 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002168 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002169
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002170 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002171 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002172 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002173 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002174 if (!_PyErr_Occurred(tstate)) {
2175 _PyErr_SetString(tstate, PyExc_NameError,
2176 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002177 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002178 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002179 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002180 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002181 }
2182 else {
2183 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2184 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002185 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002186 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2187 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002188 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2189 _PyErr_SetString(tstate, PyExc_NameError,
2190 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002191 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002192 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002193 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002194 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002195 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002196 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002197
Benjamin Petersonddd19492018-09-16 22:38:02 -07002198 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002199 PyObject *name = GETITEM(names, oparg);
2200 PyObject *v = POP();
2201 PyObject *ns = f->f_locals;
2202 int err;
2203 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002204 _PyErr_Format(tstate, PyExc_SystemError,
2205 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002206 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002207 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002208 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002209 if (PyDict_CheckExact(ns))
2210 err = PyDict_SetItem(ns, name, v);
2211 else
2212 err = PyObject_SetItem(ns, name, v);
2213 Py_DECREF(v);
2214 if (err != 0)
2215 goto error;
2216 DISPATCH();
2217 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002218
Benjamin Petersonddd19492018-09-16 22:38:02 -07002219 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002220 PyObject *name = GETITEM(names, oparg);
2221 PyObject *ns = f->f_locals;
2222 int err;
2223 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002224 _PyErr_Format(tstate, PyExc_SystemError,
2225 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002226 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002227 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002228 err = PyObject_DelItem(ns, name);
2229 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002230 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002231 NAME_ERROR_MSG,
2232 name);
2233 goto error;
2234 }
2235 DISPATCH();
2236 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002237
Benjamin Petersonddd19492018-09-16 22:38:02 -07002238 case TARGET(UNPACK_SEQUENCE): {
2239 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002240 PyObject *seq = POP(), *item, **items;
2241 if (PyTuple_CheckExact(seq) &&
2242 PyTuple_GET_SIZE(seq) == oparg) {
2243 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002244 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002245 item = items[oparg];
2246 Py_INCREF(item);
2247 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002248 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002249 } else if (PyList_CheckExact(seq) &&
2250 PyList_GET_SIZE(seq) == oparg) {
2251 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002252 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002253 item = items[oparg];
2254 Py_INCREF(item);
2255 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002256 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002257 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002258 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002259 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002260 } else {
2261 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002262 Py_DECREF(seq);
2263 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002264 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002265 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002266 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002267 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002268
Benjamin Petersonddd19492018-09-16 22:38:02 -07002269 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002270 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2271 PyObject *seq = POP();
2272
Victor Stinner438a12d2019-05-24 17:01:38 +02002273 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002274 stack_pointer + totalargs)) {
2275 stack_pointer += totalargs;
2276 } else {
2277 Py_DECREF(seq);
2278 goto error;
2279 }
2280 Py_DECREF(seq);
2281 DISPATCH();
2282 }
2283
Benjamin Petersonddd19492018-09-16 22:38:02 -07002284 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002285 PyObject *name = GETITEM(names, oparg);
2286 PyObject *owner = TOP();
2287 PyObject *v = SECOND();
2288 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002289 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002290 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002291 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002292 Py_DECREF(owner);
2293 if (err != 0)
2294 goto error;
2295 DISPATCH();
2296 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002297
Benjamin Petersonddd19492018-09-16 22:38:02 -07002298 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002299 PyObject *name = GETITEM(names, oparg);
2300 PyObject *owner = POP();
2301 int err;
2302 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2303 Py_DECREF(owner);
2304 if (err != 0)
2305 goto error;
2306 DISPATCH();
2307 }
2308
Benjamin Petersonddd19492018-09-16 22:38:02 -07002309 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002310 PyObject *name = GETITEM(names, oparg);
2311 PyObject *v = POP();
2312 int err;
2313 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002314 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002315 if (err != 0)
2316 goto error;
2317 DISPATCH();
2318 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002319
Benjamin Petersonddd19492018-09-16 22:38:02 -07002320 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002321 PyObject *name = GETITEM(names, oparg);
2322 int err;
2323 err = PyDict_DelItem(f->f_globals, name);
2324 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002325 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2326 format_exc_check_arg(tstate, PyExc_NameError,
2327 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002328 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002329 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002330 }
2331 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002332 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002333
Benjamin Petersonddd19492018-09-16 22:38:02 -07002334 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002335 PyObject *name = GETITEM(names, oparg);
2336 PyObject *locals = f->f_locals;
2337 PyObject *v;
2338 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002339 _PyErr_Format(tstate, PyExc_SystemError,
2340 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002341 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002342 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002343 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002344 v = PyDict_GetItemWithError(locals, name);
2345 if (v != NULL) {
2346 Py_INCREF(v);
2347 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002348 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002349 goto error;
2350 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002351 }
2352 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002353 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002354 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002355 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002356 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002357 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002358 }
2359 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002360 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002361 v = PyDict_GetItemWithError(f->f_globals, name);
2362 if (v != NULL) {
2363 Py_INCREF(v);
2364 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002365 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002366 goto error;
2367 }
2368 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002369 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002370 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002371 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002372 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002373 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002374 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002375 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002376 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002377 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002378 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002379 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002380 }
2381 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002382 v = PyObject_GetItem(f->f_builtins, name);
2383 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002384 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002385 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002386 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002387 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002388 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002389 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002390 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002391 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002392 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002393 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002394 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002396 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002397
Benjamin Petersonddd19492018-09-16 22:38:02 -07002398 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002399 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002400 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002401 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002402 && PyDict_CheckExact(f->f_builtins))
2403 {
Inada Naoki91234a12019-06-03 21:30:58 +09002404 OPCACHE_CHECK();
2405 if (co_opcache != NULL && co_opcache->optimized > 0) {
2406 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2407
2408 if (lg->globals_ver ==
2409 ((PyDictObject *)f->f_globals)->ma_version_tag
2410 && lg->builtins_ver ==
2411 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2412 {
2413 PyObject *ptr = lg->ptr;
2414 OPCACHE_STAT_GLOBAL_HIT();
2415 assert(ptr != NULL);
2416 Py_INCREF(ptr);
2417 PUSH(ptr);
2418 DISPATCH();
2419 }
2420 }
2421
2422 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002423 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002424 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002425 name);
2426 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002427 if (!_PyErr_OCCURRED()) {
2428 /* _PyDict_LoadGlobal() returns NULL without raising
2429 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002430 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002431 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002432 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002433 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002434 }
Inada Naoki91234a12019-06-03 21:30:58 +09002435
2436 if (co_opcache != NULL) {
2437 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2438
2439 if (co_opcache->optimized == 0) {
2440 /* Wasn't optimized before. */
2441 OPCACHE_STAT_GLOBAL_OPT();
2442 } else {
2443 OPCACHE_STAT_GLOBAL_MISS();
2444 }
2445
2446 co_opcache->optimized = 1;
2447 lg->globals_ver =
2448 ((PyDictObject *)f->f_globals)->ma_version_tag;
2449 lg->builtins_ver =
2450 ((PyDictObject *)f->f_builtins)->ma_version_tag;
2451 lg->ptr = v; /* borrowed */
2452 }
2453
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002454 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002455 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002456 else {
2457 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002458
2459 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09002460 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002461 v = PyObject_GetItem(f->f_globals, name);
2462 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002463 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002464 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002465 }
2466 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002467
Victor Stinnerb4efc962015-11-20 09:24:02 +01002468 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002469 v = PyObject_GetItem(f->f_builtins, name);
2470 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002471 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002472 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002473 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002474 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002475 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002476 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002477 }
2478 }
2479 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002480 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002481 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002482 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002483
Benjamin Petersonddd19492018-09-16 22:38:02 -07002484 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002485 PyObject *v = GETLOCAL(oparg);
2486 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002487 SETLOCAL(oparg, NULL);
2488 DISPATCH();
2489 }
2490 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002491 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002492 UNBOUNDLOCAL_ERROR_MSG,
2493 PyTuple_GetItem(co->co_varnames, oparg)
2494 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002495 goto error;
2496 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002497
Benjamin Petersonddd19492018-09-16 22:38:02 -07002498 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002499 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002500 PyObject *oldobj = PyCell_GET(cell);
2501 if (oldobj != NULL) {
2502 PyCell_SET(cell, NULL);
2503 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002504 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002505 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002506 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002507 goto error;
2508 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002509
Benjamin Petersonddd19492018-09-16 22:38:02 -07002510 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002511 PyObject *cell = freevars[oparg];
2512 Py_INCREF(cell);
2513 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002514 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002515 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002516
Benjamin Petersonddd19492018-09-16 22:38:02 -07002517 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002518 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002519 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002520 assert(locals);
2521 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2522 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2523 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2524 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2525 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002526 value = PyDict_GetItemWithError(locals, name);
2527 if (value != NULL) {
2528 Py_INCREF(value);
2529 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002530 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002531 goto error;
2532 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002533 }
2534 else {
2535 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002536 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002537 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002538 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002539 }
2540 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002541 }
2542 }
2543 if (!value) {
2544 PyObject *cell = freevars[oparg];
2545 value = PyCell_GET(cell);
2546 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002547 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002548 goto error;
2549 }
2550 Py_INCREF(value);
2551 }
2552 PUSH(value);
2553 DISPATCH();
2554 }
2555
Benjamin Petersonddd19492018-09-16 22:38:02 -07002556 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002557 PyObject *cell = freevars[oparg];
2558 PyObject *value = PyCell_GET(cell);
2559 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002560 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002561 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002562 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002563 Py_INCREF(value);
2564 PUSH(value);
2565 DISPATCH();
2566 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002567
Benjamin Petersonddd19492018-09-16 22:38:02 -07002568 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002569 PyObject *v = POP();
2570 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002571 PyObject *oldobj = PyCell_GET(cell);
2572 PyCell_SET(cell, v);
2573 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002574 DISPATCH();
2575 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002576
Benjamin Petersonddd19492018-09-16 22:38:02 -07002577 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002578 PyObject *str;
2579 PyObject *empty = PyUnicode_New(0, 0);
2580 if (empty == NULL) {
2581 goto error;
2582 }
2583 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2584 Py_DECREF(empty);
2585 if (str == NULL)
2586 goto error;
2587 while (--oparg >= 0) {
2588 PyObject *item = POP();
2589 Py_DECREF(item);
2590 }
2591 PUSH(str);
2592 DISPATCH();
2593 }
2594
Benjamin Petersonddd19492018-09-16 22:38:02 -07002595 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002596 PyObject *tup = PyTuple_New(oparg);
2597 if (tup == NULL)
2598 goto error;
2599 while (--oparg >= 0) {
2600 PyObject *item = POP();
2601 PyTuple_SET_ITEM(tup, oparg, item);
2602 }
2603 PUSH(tup);
2604 DISPATCH();
2605 }
2606
Benjamin Petersonddd19492018-09-16 22:38:02 -07002607 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002608 PyObject *list = PyList_New(oparg);
2609 if (list == NULL)
2610 goto error;
2611 while (--oparg >= 0) {
2612 PyObject *item = POP();
2613 PyList_SET_ITEM(list, oparg, item);
2614 }
2615 PUSH(list);
2616 DISPATCH();
2617 }
2618
Mark Shannon13bc1392020-01-23 09:25:17 +00002619 case TARGET(LIST_TO_TUPLE): {
2620 PyObject *list = POP();
2621 PyObject *tuple = PyList_AsTuple(list);
2622 Py_DECREF(list);
2623 if (tuple == NULL) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002624 goto error;
Mark Shannon13bc1392020-01-23 09:25:17 +00002625 }
2626 PUSH(tuple);
2627 DISPATCH();
2628 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002629
Mark Shannon13bc1392020-01-23 09:25:17 +00002630 case TARGET(LIST_EXTEND): {
2631 PyObject *iterable = POP();
2632 PyObject *list = PEEK(oparg);
2633 PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
2634 if (none_val == NULL) {
2635 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01002636 (Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable)))
Mark Shannon13bc1392020-01-23 09:25:17 +00002637 {
Victor Stinner61f4db82020-01-28 03:37:45 +01002638 _PyErr_Clear(tstate);
Mark Shannon13bc1392020-01-23 09:25:17 +00002639 _PyErr_Format(tstate, PyExc_TypeError,
2640 "Value after * must be an iterable, not %.200s",
2641 Py_TYPE(iterable)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002642 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002643 Py_DECREF(iterable);
2644 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002645 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002646 Py_DECREF(none_val);
2647 Py_DECREF(iterable);
2648 DISPATCH();
2649 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002650
Mark Shannon13bc1392020-01-23 09:25:17 +00002651 case TARGET(SET_UPDATE): {
2652 PyObject *iterable = POP();
2653 PyObject *set = PEEK(oparg);
2654 int err = _PySet_Update(set, iterable);
2655 Py_DECREF(iterable);
2656 if (err < 0) {
2657 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002658 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002659 DISPATCH();
2660 }
2661
Benjamin Petersonddd19492018-09-16 22:38:02 -07002662 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002663 PyObject *set = PySet_New(NULL);
2664 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002665 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002666 if (set == NULL)
2667 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002668 for (i = oparg; i > 0; i--) {
2669 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002670 if (err == 0)
2671 err = PySet_Add(set, item);
2672 Py_DECREF(item);
2673 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002674 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002675 if (err != 0) {
2676 Py_DECREF(set);
2677 goto error;
2678 }
2679 PUSH(set);
2680 DISPATCH();
2681 }
2682
Benjamin Petersonddd19492018-09-16 22:38:02 -07002683 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002684 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002685 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2686 if (map == NULL)
2687 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002688 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002689 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002690 PyObject *key = PEEK(2*i);
2691 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002692 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002693 if (err != 0) {
2694 Py_DECREF(map);
2695 goto error;
2696 }
2697 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002698
2699 while (oparg--) {
2700 Py_DECREF(POP());
2701 Py_DECREF(POP());
2702 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002703 PUSH(map);
2704 DISPATCH();
2705 }
2706
Benjamin Petersonddd19492018-09-16 22:38:02 -07002707 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002708 _Py_IDENTIFIER(__annotations__);
2709 int err;
2710 PyObject *ann_dict;
2711 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002712 _PyErr_Format(tstate, PyExc_SystemError,
2713 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002714 goto error;
2715 }
2716 /* check if __annotations__ in locals()... */
2717 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002718 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002719 &PyId___annotations__);
2720 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002721 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002722 goto error;
2723 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002724 /* ...if not, create a new one */
2725 ann_dict = PyDict_New();
2726 if (ann_dict == NULL) {
2727 goto error;
2728 }
2729 err = _PyDict_SetItemId(f->f_locals,
2730 &PyId___annotations__, ann_dict);
2731 Py_DECREF(ann_dict);
2732 if (err != 0) {
2733 goto error;
2734 }
2735 }
2736 }
2737 else {
2738 /* do the same if locals() is not a dict */
2739 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2740 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002741 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002742 }
2743 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2744 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002745 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002746 goto error;
2747 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002748 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002749 ann_dict = PyDict_New();
2750 if (ann_dict == NULL) {
2751 goto error;
2752 }
2753 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2754 Py_DECREF(ann_dict);
2755 if (err != 0) {
2756 goto error;
2757 }
2758 }
2759 else {
2760 Py_DECREF(ann_dict);
2761 }
2762 }
2763 DISPATCH();
2764 }
2765
Benjamin Petersonddd19492018-09-16 22:38:02 -07002766 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002767 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002768 PyObject *map;
2769 PyObject *keys = TOP();
2770 if (!PyTuple_CheckExact(keys) ||
2771 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002772 _PyErr_SetString(tstate, PyExc_SystemError,
2773 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002774 goto error;
2775 }
2776 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2777 if (map == NULL) {
2778 goto error;
2779 }
2780 for (i = oparg; i > 0; i--) {
2781 int err;
2782 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2783 PyObject *value = PEEK(i + 1);
2784 err = PyDict_SetItem(map, key, value);
2785 if (err != 0) {
2786 Py_DECREF(map);
2787 goto error;
2788 }
2789 }
2790
2791 Py_DECREF(POP());
2792 while (oparg--) {
2793 Py_DECREF(POP());
2794 }
2795 PUSH(map);
2796 DISPATCH();
2797 }
2798
Mark Shannon8a4cd702020-01-27 09:57:45 +00002799 case TARGET(DICT_UPDATE): {
2800 PyObject *update = POP();
2801 PyObject *dict = PEEK(oparg);
2802 if (PyDict_Update(dict, update) < 0) {
2803 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
2804 _PyErr_Format(tstate, PyExc_TypeError,
2805 "'%.200s' object is not a mapping",
Victor Stinnera102ed72020-02-07 02:24:48 +01002806 Py_TYPE(update)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002807 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002808 Py_DECREF(update);
2809 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002810 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002811 Py_DECREF(update);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002812 DISPATCH();
2813 }
2814
Mark Shannon8a4cd702020-01-27 09:57:45 +00002815 case TARGET(DICT_MERGE): {
2816 PyObject *update = POP();
2817 PyObject *dict = PEEK(oparg);
2818
2819 if (_PyDict_MergeEx(dict, update, 2) < 0) {
2820 format_kwargs_error(tstate, PEEK(2 + oparg), update);
2821 Py_DECREF(update);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002822 goto error;
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002823 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002824 Py_DECREF(update);
Brandt Bucherf185a732019-09-28 17:12:49 -07002825 PREDICT(CALL_FUNCTION_EX);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002826 DISPATCH();
2827 }
2828
Benjamin Petersonddd19492018-09-16 22:38:02 -07002829 case TARGET(MAP_ADD): {
Jörn Heisslerc8a35412019-06-22 16:40:55 +02002830 PyObject *value = TOP();
2831 PyObject *key = SECOND();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002832 PyObject *map;
2833 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002834 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002835 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002836 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002837 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002838 Py_DECREF(value);
2839 Py_DECREF(key);
2840 if (err != 0)
2841 goto error;
2842 PREDICT(JUMP_ABSOLUTE);
2843 DISPATCH();
2844 }
2845
Benjamin Petersonddd19492018-09-16 22:38:02 -07002846 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002847 PyObject *name = GETITEM(names, oparg);
2848 PyObject *owner = TOP();
2849 PyObject *res = PyObject_GetAttr(owner, name);
2850 Py_DECREF(owner);
2851 SET_TOP(res);
2852 if (res == NULL)
2853 goto error;
2854 DISPATCH();
2855 }
2856
Benjamin Petersonddd19492018-09-16 22:38:02 -07002857 case TARGET(COMPARE_OP): {
Mark Shannon9af0e472020-01-14 10:12:45 +00002858 assert(oparg <= Py_GE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002859 PyObject *right = POP();
2860 PyObject *left = TOP();
Mark Shannon9af0e472020-01-14 10:12:45 +00002861 PyObject *res = PyObject_RichCompare(left, right, oparg);
2862 SET_TOP(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002863 Py_DECREF(left);
2864 Py_DECREF(right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002865 if (res == NULL)
2866 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002867 PREDICT(POP_JUMP_IF_FALSE);
2868 PREDICT(POP_JUMP_IF_TRUE);
2869 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002870 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002871
Mark Shannon9af0e472020-01-14 10:12:45 +00002872 case TARGET(IS_OP): {
2873 PyObject *right = POP();
2874 PyObject *left = TOP();
2875 int res = (left == right)^oparg;
2876 PyObject *b = res ? Py_True : Py_False;
2877 Py_INCREF(b);
2878 SET_TOP(b);
2879 Py_DECREF(left);
2880 Py_DECREF(right);
2881 PREDICT(POP_JUMP_IF_FALSE);
2882 PREDICT(POP_JUMP_IF_TRUE);
2883 FAST_DISPATCH();
2884 }
2885
2886 case TARGET(CONTAINS_OP): {
2887 PyObject *right = POP();
2888 PyObject *left = POP();
2889 int res = PySequence_Contains(right, left);
2890 Py_DECREF(left);
2891 Py_DECREF(right);
2892 if (res < 0) {
2893 goto error;
2894 }
2895 PyObject *b = (res^oparg) ? Py_True : Py_False;
2896 Py_INCREF(b);
2897 PUSH(b);
2898 PREDICT(POP_JUMP_IF_FALSE);
2899 PREDICT(POP_JUMP_IF_TRUE);
2900 FAST_DISPATCH();
2901 }
2902
2903#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
2904 "BaseException is not allowed"
2905
2906 case TARGET(JUMP_IF_NOT_EXC_MATCH): {
2907 PyObject *right = POP();
2908 PyObject *left = POP();
2909 if (PyTuple_Check(right)) {
2910 Py_ssize_t i, length;
2911 length = PyTuple_GET_SIZE(right);
2912 for (i = 0; i < length; i++) {
2913 PyObject *exc = PyTuple_GET_ITEM(right, i);
2914 if (!PyExceptionClass_Check(exc)) {
2915 _PyErr_SetString(tstate, PyExc_TypeError,
2916 CANNOT_CATCH_MSG);
2917 Py_DECREF(left);
2918 Py_DECREF(right);
2919 goto error;
2920 }
2921 }
2922 }
2923 else {
2924 if (!PyExceptionClass_Check(right)) {
2925 _PyErr_SetString(tstate, PyExc_TypeError,
2926 CANNOT_CATCH_MSG);
2927 Py_DECREF(left);
2928 Py_DECREF(right);
2929 goto error;
2930 }
2931 }
2932 int res = PyErr_GivenExceptionMatches(left, right);
2933 Py_DECREF(left);
2934 Py_DECREF(right);
2935 if (res > 0) {
2936 /* Exception matches -- Do nothing */;
2937 }
2938 else if (res == 0) {
2939 JUMPTO(oparg);
2940 }
2941 else {
2942 goto error;
2943 }
2944 DISPATCH();
2945 }
2946
Benjamin Petersonddd19492018-09-16 22:38:02 -07002947 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002948 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002949 PyObject *fromlist = POP();
2950 PyObject *level = TOP();
2951 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02002952 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002953 Py_DECREF(level);
2954 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002955 SET_TOP(res);
2956 if (res == NULL)
2957 goto error;
2958 DISPATCH();
2959 }
2960
Benjamin Petersonddd19492018-09-16 22:38:02 -07002961 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002962 PyObject *from = POP(), *locals;
2963 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002964 if (PyFrame_FastToLocalsWithError(f) < 0) {
2965 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002966 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002967 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002968
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002969 locals = f->f_locals;
2970 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002971 _PyErr_SetString(tstate, PyExc_SystemError,
2972 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002973 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002974 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002975 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002976 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002977 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002978 Py_DECREF(from);
2979 if (err != 0)
2980 goto error;
2981 DISPATCH();
2982 }
Guido van Rossum25831651993-05-19 14:50:45 +00002983
Benjamin Petersonddd19492018-09-16 22:38:02 -07002984 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002985 PyObject *name = GETITEM(names, oparg);
2986 PyObject *from = TOP();
2987 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02002988 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002989 PUSH(res);
2990 if (res == NULL)
2991 goto error;
2992 DISPATCH();
2993 }
Thomas Wouters52152252000-08-17 22:55:00 +00002994
Benjamin Petersonddd19492018-09-16 22:38:02 -07002995 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002996 JUMPBY(oparg);
2997 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002998 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002999
Benjamin Petersonddd19492018-09-16 22:38:02 -07003000 case TARGET(POP_JUMP_IF_FALSE): {
3001 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003002 PyObject *cond = POP();
3003 int err;
3004 if (cond == Py_True) {
3005 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003006 FAST_DISPATCH();
3007 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003008 if (cond == Py_False) {
3009 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003010 JUMPTO(oparg);
3011 FAST_DISPATCH();
3012 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003013 err = PyObject_IsTrue(cond);
3014 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003015 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003016 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003017 else if (err == 0)
3018 JUMPTO(oparg);
3019 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003020 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003021 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003022 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003023
Benjamin Petersonddd19492018-09-16 22:38:02 -07003024 case TARGET(POP_JUMP_IF_TRUE): {
3025 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003026 PyObject *cond = POP();
3027 int err;
3028 if (cond == Py_False) {
3029 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003030 FAST_DISPATCH();
3031 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003032 if (cond == Py_True) {
3033 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003034 JUMPTO(oparg);
3035 FAST_DISPATCH();
3036 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003037 err = PyObject_IsTrue(cond);
3038 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003039 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003040 JUMPTO(oparg);
3041 }
3042 else if (err == 0)
3043 ;
3044 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003045 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003046 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003047 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003048
Benjamin Petersonddd19492018-09-16 22:38:02 -07003049 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003050 PyObject *cond = TOP();
3051 int err;
3052 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003053 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003054 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003055 FAST_DISPATCH();
3056 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003057 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003058 JUMPTO(oparg);
3059 FAST_DISPATCH();
3060 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003061 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003062 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003063 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003064 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003065 }
3066 else if (err == 0)
3067 JUMPTO(oparg);
3068 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003069 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003070 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003071 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003072
Benjamin Petersonddd19492018-09-16 22:38:02 -07003073 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003074 PyObject *cond = TOP();
3075 int err;
3076 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003077 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003078 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003079 FAST_DISPATCH();
3080 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003081 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003082 JUMPTO(oparg);
3083 FAST_DISPATCH();
3084 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003085 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003086 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003087 JUMPTO(oparg);
3088 }
3089 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003090 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003091 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003092 }
3093 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003094 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003095 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003096 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003097
Benjamin Petersonddd19492018-09-16 22:38:02 -07003098 case TARGET(JUMP_ABSOLUTE): {
3099 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003100 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003101#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003102 /* Enabling this path speeds-up all while and for-loops by bypassing
3103 the per-loop checks for signals. By default, this should be turned-off
3104 because it prevents detection of a control-break in tight loops like
3105 "while 1: pass". Compile with this option turned-on when you need
3106 the speed-up and do not need break checking inside tight loops (ones
3107 that contain only instructions ending with FAST_DISPATCH).
3108 */
3109 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003110#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003111 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003112#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003113 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003114
Benjamin Petersonddd19492018-09-16 22:38:02 -07003115 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003116 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003117 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003118 PyObject *iter = PyObject_GetIter(iterable);
3119 Py_DECREF(iterable);
3120 SET_TOP(iter);
3121 if (iter == NULL)
3122 goto error;
3123 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003124 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003125 DISPATCH();
3126 }
3127
Benjamin Petersonddd19492018-09-16 22:38:02 -07003128 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04003129 /* before: [obj]; after [getiter(obj)] */
3130 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003131 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003132 if (PyCoro_CheckExact(iterable)) {
3133 /* `iterable` is a coroutine */
3134 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3135 /* and it is used in a 'yield from' expression of a
3136 regular generator. */
3137 Py_DECREF(iterable);
3138 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003139 _PyErr_SetString(tstate, PyExc_TypeError,
3140 "cannot 'yield from' a coroutine object "
3141 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04003142 goto error;
3143 }
3144 }
3145 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003146 /* `iterable` is not a generator. */
3147 iter = PyObject_GetIter(iterable);
3148 Py_DECREF(iterable);
3149 SET_TOP(iter);
3150 if (iter == NULL)
3151 goto error;
3152 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003153 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003154 DISPATCH();
3155 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003156
Benjamin Petersonddd19492018-09-16 22:38:02 -07003157 case TARGET(FOR_ITER): {
3158 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003159 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003160 PyObject *iter = TOP();
Victor Stinnera102ed72020-02-07 02:24:48 +01003161 PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003162 if (next != NULL) {
3163 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003164 PREDICT(STORE_FAST);
3165 PREDICT(UNPACK_SEQUENCE);
3166 DISPATCH();
3167 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003168 if (_PyErr_Occurred(tstate)) {
3169 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003170 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003171 }
3172 else if (tstate->c_tracefunc != NULL) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003173 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Victor Stinner438a12d2019-05-24 17:01:38 +02003174 }
3175 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003176 }
3177 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003178 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003179 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003180 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003181 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003182 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003183 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003184
Benjamin Petersonddd19492018-09-16 22:38:02 -07003185 case TARGET(SETUP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003186 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003187 STACK_LEVEL());
3188 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003189 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003190
Benjamin Petersonddd19492018-09-16 22:38:02 -07003191 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003192 _Py_IDENTIFIER(__aenter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003193 _Py_IDENTIFIER(__aexit__);
Yury Selivanov75445082015-05-11 22:57:16 -04003194 PyObject *mgr = TOP();
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003195 PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04003196 PyObject *res;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003197 if (enter == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003198 goto error;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003199 }
3200 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__);
3201 if (exit == NULL) {
3202 Py_DECREF(enter);
3203 goto error;
3204 }
Yury Selivanov75445082015-05-11 22:57:16 -04003205 SET_TOP(exit);
Yury Selivanov75445082015-05-11 22:57:16 -04003206 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003207 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003208 Py_DECREF(enter);
3209 if (res == NULL)
3210 goto error;
3211 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003212 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003213 DISPATCH();
3214 }
3215
Benjamin Petersonddd19492018-09-16 22:38:02 -07003216 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003217 PyObject *res = POP();
3218 /* Setup the finally block before pushing the result
3219 of __aenter__ on the stack. */
3220 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3221 STACK_LEVEL());
3222 PUSH(res);
3223 DISPATCH();
3224 }
3225
Benjamin Petersonddd19492018-09-16 22:38:02 -07003226 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003227 _Py_IDENTIFIER(__enter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003228 _Py_IDENTIFIER(__exit__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003229 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003230 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003231 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003232 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08003233 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003234 }
3235 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003236 if (exit == NULL) {
3237 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003238 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003239 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003240 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003241 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003242 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003243 Py_DECREF(enter);
3244 if (res == NULL)
3245 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003246 /* Setup the finally block before pushing the result
3247 of __enter__ on the stack. */
3248 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3249 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003250
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003251 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003252 DISPATCH();
3253 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003254
Mark Shannonfee55262019-11-21 09:11:43 +00003255 case TARGET(WITH_EXCEPT_START): {
3256 /* At the top of the stack are 7 values:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003257 - (TOP, SECOND, THIRD) = exc_info()
Mark Shannonfee55262019-11-21 09:11:43 +00003258 - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER
3259 - SEVENTH: the context.__exit__ bound method
3260 We call SEVENTH(TOP, SECOND, THIRD).
3261 Then we push again the TOP exception and the __exit__
3262 return value.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003263 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003264 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003265 PyObject *exc, *val, *tb, *res;
3266
Victor Stinner842cfff2016-12-01 14:45:31 +01003267 exc = TOP();
Mark Shannonfee55262019-11-21 09:11:43 +00003268 val = SECOND();
3269 tb = THIRD();
3270 assert(exc != Py_None);
3271 assert(!PyLong_Check(exc));
3272 exit_func = PEEK(7);
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003273 PyObject *stack[4] = {NULL, exc, val, tb};
Petr Viktorinffd97532020-02-11 17:46:57 +01003274 res = PyObject_Vectorcall(exit_func, stack + 1,
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003275 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003276 if (res == NULL)
3277 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003278
Yury Selivanov75445082015-05-11 22:57:16 -04003279 PUSH(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003280 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003281 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003282
Benjamin Petersonddd19492018-09-16 22:38:02 -07003283 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10003284 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003285 PyObject *name = GETITEM(names, oparg);
3286 PyObject *obj = TOP();
3287 PyObject *meth = NULL;
3288
3289 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3290
Yury Selivanovf2392132016-12-13 19:03:51 -05003291 if (meth == NULL) {
3292 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003293 goto error;
3294 }
3295
3296 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003297 /* We can bypass temporary bound method object.
3298 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003299
INADA Naoki015bce62017-01-16 17:23:30 +09003300 meth | self | arg1 | ... | argN
3301 */
3302 SET_TOP(meth);
3303 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003304 }
3305 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003306 /* meth is not an unbound method (but a regular attr, or
3307 something was returned by a descriptor protocol). Set
3308 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003309 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003310
3311 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003312 */
INADA Naoki015bce62017-01-16 17:23:30 +09003313 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003314 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003315 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003316 }
3317 DISPATCH();
3318 }
3319
Benjamin Petersonddd19492018-09-16 22:38:02 -07003320 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003321 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003322 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003323
3324 sp = stack_pointer;
3325
INADA Naoki015bce62017-01-16 17:23:30 +09003326 meth = PEEK(oparg + 2);
3327 if (meth == NULL) {
3328 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3329 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003330
3331 Stack layout:
3332
INADA Naoki015bce62017-01-16 17:23:30 +09003333 ... | NULL | callable | arg1 | ... | argN
3334 ^- TOP()
3335 ^- (-oparg)
3336 ^- (-oparg-1)
3337 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003338
Ville Skyttä49b27342017-08-03 09:00:59 +03003339 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003340 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003341 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003342 res = call_function(tstate, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003343 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003344 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003345 }
3346 else {
3347 /* This is a method call. Stack layout:
3348
INADA Naoki015bce62017-01-16 17:23:30 +09003349 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003350 ^- TOP()
3351 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003352 ^- (-oparg-1)
3353 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003354
INADA Naoki015bce62017-01-16 17:23:30 +09003355 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003356 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003357 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003358 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003359 res = call_function(tstate, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003360 stack_pointer = sp;
3361 }
3362
3363 PUSH(res);
3364 if (res == NULL)
3365 goto error;
3366 DISPATCH();
3367 }
3368
Benjamin Petersonddd19492018-09-16 22:38:02 -07003369 case TARGET(CALL_FUNCTION): {
3370 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003371 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003372 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003373 res = call_function(tstate, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003374 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003375 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003376 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003377 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003378 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003379 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003380 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003381
Benjamin Petersonddd19492018-09-16 22:38:02 -07003382 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003383 PyObject **sp, *res, *names;
3384
3385 names = POP();
Jeroen Demeyer05677862019-08-16 12:41:27 +02003386 assert(PyTuple_Check(names));
3387 assert(PyTuple_GET_SIZE(names) <= oparg);
3388 /* We assume without checking that names contains only strings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003389 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003390 res = call_function(tstate, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003391 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003392 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003393 Py_DECREF(names);
3394
3395 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003396 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003397 }
3398 DISPATCH();
3399 }
3400
Benjamin Petersonddd19492018-09-16 22:38:02 -07003401 case TARGET(CALL_FUNCTION_EX): {
Brandt Bucherf185a732019-09-28 17:12:49 -07003402 PREDICTED(CALL_FUNCTION_EX);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003403 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003404 if (oparg & 0x01) {
3405 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003406 if (!PyDict_CheckExact(kwargs)) {
3407 PyObject *d = PyDict_New();
3408 if (d == NULL)
3409 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003410 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003411 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02003412 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003413 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003414 goto error;
3415 }
3416 Py_DECREF(kwargs);
3417 kwargs = d;
3418 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003419 assert(PyDict_CheckExact(kwargs));
3420 }
3421 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003422 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003423 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003424 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003425 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003426 goto error;
3427 }
3428 Py_SETREF(callargs, PySequence_Tuple(callargs));
3429 if (callargs == NULL) {
3430 goto error;
3431 }
3432 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003433 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003434
Victor Stinner09532fe2019-05-10 23:39:09 +02003435 result = do_call_core(tstate, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003436 Py_DECREF(func);
3437 Py_DECREF(callargs);
3438 Py_XDECREF(kwargs);
3439
3440 SET_TOP(result);
3441 if (result == NULL) {
3442 goto error;
3443 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003444 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003445 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003446
Benjamin Petersonddd19492018-09-16 22:38:02 -07003447 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003448 PyObject *qualname = POP();
3449 PyObject *codeobj = POP();
3450 PyFunctionObject *func = (PyFunctionObject *)
3451 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003452
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003453 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003454 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003455 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003456 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003457 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003458
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003459 if (oparg & 0x08) {
3460 assert(PyTuple_CheckExact(TOP()));
3461 func ->func_closure = POP();
3462 }
3463 if (oparg & 0x04) {
3464 assert(PyDict_CheckExact(TOP()));
3465 func->func_annotations = POP();
3466 }
3467 if (oparg & 0x02) {
3468 assert(PyDict_CheckExact(TOP()));
3469 func->func_kwdefaults = POP();
3470 }
3471 if (oparg & 0x01) {
3472 assert(PyTuple_CheckExact(TOP()));
3473 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003474 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003475
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003476 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003477 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003478 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003479
Benjamin Petersonddd19492018-09-16 22:38:02 -07003480 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003481 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003482 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003483 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003484 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003485 step = NULL;
3486 stop = POP();
3487 start = TOP();
3488 slice = PySlice_New(start, stop, step);
3489 Py_DECREF(start);
3490 Py_DECREF(stop);
3491 Py_XDECREF(step);
3492 SET_TOP(slice);
3493 if (slice == NULL)
3494 goto error;
3495 DISPATCH();
3496 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003497
Benjamin Petersonddd19492018-09-16 22:38:02 -07003498 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003499 /* Handles f-string value formatting. */
3500 PyObject *result;
3501 PyObject *fmt_spec;
3502 PyObject *value;
3503 PyObject *(*conv_fn)(PyObject *);
3504 int which_conversion = oparg & FVC_MASK;
3505 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3506
3507 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003508 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003509
3510 /* See if any conversion is specified. */
3511 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003512 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003513 case FVC_STR: conv_fn = PyObject_Str; break;
3514 case FVC_REPR: conv_fn = PyObject_Repr; break;
3515 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003516 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02003517 _PyErr_Format(tstate, PyExc_SystemError,
3518 "unexpected conversion flag %d",
3519 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003520 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003521 }
3522
3523 /* If there's a conversion function, call it and replace
3524 value with that result. Otherwise, just use value,
3525 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003526 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003527 result = conv_fn(value);
3528 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003529 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003530 Py_XDECREF(fmt_spec);
3531 goto error;
3532 }
3533 value = result;
3534 }
3535
3536 /* If value is a unicode object, and there's no fmt_spec,
3537 then we know the result of format(value) is value
3538 itself. In that case, skip calling format(). I plan to
3539 move this optimization in to PyObject_Format()
3540 itself. */
3541 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3542 /* Do nothing, just transfer ownership to result. */
3543 result = value;
3544 } else {
3545 /* Actually call format(). */
3546 result = PyObject_Format(value, fmt_spec);
3547 Py_DECREF(value);
3548 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003549 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003550 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003551 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003552 }
3553
Eric V. Smith135d5f42016-02-05 18:23:08 -05003554 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003555 DISPATCH();
3556 }
3557
Benjamin Petersonddd19492018-09-16 22:38:02 -07003558 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003559 int oldoparg = oparg;
3560 NEXTOPARG();
3561 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003562 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003563 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003564
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003565
Antoine Pitrou042b1282010-08-13 21:15:58 +00003566#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003567 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003568#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003569 default:
3570 fprintf(stderr,
3571 "XXX lineno: %d, opcode: %d\n",
3572 PyFrame_GetLineNumber(f),
3573 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02003574 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003575 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003577 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003578
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003579 /* This should never be reached. Every opcode should end with DISPATCH()
3580 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003581 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003582
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003583error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003584 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003585#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02003586 if (!_PyErr_Occurred(tstate)) {
3587 _PyErr_SetString(tstate, PyExc_SystemError,
3588 "error return without exception set");
3589 }
Victor Stinner365b6932013-07-12 00:11:58 +02003590#else
Victor Stinner438a12d2019-05-24 17:01:38 +02003591 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02003592#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003593
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003594 /* Log traceback info. */
3595 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003596
Benjamin Peterson51f46162013-01-23 08:38:47 -05003597 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003598 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3599 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003600
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003601exception_unwind:
3602 /* Unwind stacks if an exception occurred */
3603 while (f->f_iblock > 0) {
3604 /* Pop the current block. */
3605 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003607 if (b->b_type == EXCEPT_HANDLER) {
3608 UNWIND_EXCEPT_HANDLER(b);
3609 continue;
3610 }
3611 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003612 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003613 PyObject *exc, *val, *tb;
3614 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003615 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003616 /* Beware, this invalidates all b->b_* fields */
3617 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003618 PUSH(exc_info->exc_traceback);
3619 PUSH(exc_info->exc_value);
3620 if (exc_info->exc_type != NULL) {
3621 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003622 }
3623 else {
3624 Py_INCREF(Py_None);
3625 PUSH(Py_None);
3626 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003627 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003628 /* Make the raw exception data
3629 available to the handler,
3630 so a program can emulate the
3631 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02003632 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003633 if (tb != NULL)
3634 PyException_SetTraceback(val, tb);
3635 else
3636 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003637 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003638 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003639 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003640 exc_info->exc_value = val;
3641 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003642 if (tb == NULL)
3643 tb = Py_None;
3644 Py_INCREF(tb);
3645 PUSH(tb);
3646 PUSH(val);
3647 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003648 JUMPTO(handler);
Pablo Galindo4c53e632020-01-10 09:24:22 +00003649 if (_Py_TracingPossible(ceval)) {
3650 int needs_new_execution_window = (f->f_lasti < instr_lb || f->f_lasti >= instr_ub);
3651 int needs_line_update = (f->f_lasti == instr_lb || f->f_lasti < instr_prev);
3652 /* Make sure that we trace line after exception if we are in a new execution
3653 * window or we don't need a line update and we are not in the first instruction
3654 * of the line. */
3655 if (needs_new_execution_window || (!needs_line_update && instr_lb > 0)) {
3656 instr_prev = INT_MAX;
3657 }
Mark Shannonfee55262019-11-21 09:11:43 +00003658 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003659 /* Resume normal execution */
3660 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003661 }
3662 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003663
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003664 /* End the loop as we still have an error */
3665 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003666 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003667
Pablo Galindof00828a2019-05-09 16:52:02 +01003668 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003669 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01003670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003671 /* Pop remaining stack entries. */
3672 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003673 PyObject *o = POP();
3674 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003675 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003676
Mark Shannone7c9f4a2020-01-13 12:51:26 +00003677exiting:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003678 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003679 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003680 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3681 tstate, f, PyTrace_RETURN, retval)) {
3682 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003683 }
3684 }
3685 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003686 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3687 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003688 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003689 }
3690 }
3691 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003693 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003694exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003695 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3696 dtrace_function_return(f);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01003697 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrou58720d62013-08-05 23:26:40 +02003698 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003699 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003700
Victor Stinner17269092019-11-05 01:22:12 +01003701 return _Py_CheckFunctionResult(tstate, NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003702}
3703
Benjamin Petersonb204a422011-06-05 22:04:07 -05003704static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003705format_missing(PyThreadState *tstate, const char *kind,
3706 PyCodeObject *co, PyObject *names)
Benjamin Petersone109c702011-06-24 09:37:26 -05003707{
3708 int err;
3709 Py_ssize_t len = PyList_GET_SIZE(names);
3710 PyObject *name_str, *comma, *tail, *tmp;
3711
3712 assert(PyList_CheckExact(names));
3713 assert(len >= 1);
3714 /* Deal with the joys of natural language. */
3715 switch (len) {
3716 case 1:
3717 name_str = PyList_GET_ITEM(names, 0);
3718 Py_INCREF(name_str);
3719 break;
3720 case 2:
3721 name_str = PyUnicode_FromFormat("%U and %U",
3722 PyList_GET_ITEM(names, len - 2),
3723 PyList_GET_ITEM(names, len - 1));
3724 break;
3725 default:
3726 tail = PyUnicode_FromFormat(", %U, and %U",
3727 PyList_GET_ITEM(names, len - 2),
3728 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003729 if (tail == NULL)
3730 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003731 /* Chop off the last two objects in the list. This shouldn't actually
3732 fail, but we can't be too careful. */
3733 err = PyList_SetSlice(names, len - 2, len, NULL);
3734 if (err == -1) {
3735 Py_DECREF(tail);
3736 return;
3737 }
3738 /* Stitch everything up into a nice comma-separated list. */
3739 comma = PyUnicode_FromString(", ");
3740 if (comma == NULL) {
3741 Py_DECREF(tail);
3742 return;
3743 }
3744 tmp = PyUnicode_Join(comma, names);
3745 Py_DECREF(comma);
3746 if (tmp == NULL) {
3747 Py_DECREF(tail);
3748 return;
3749 }
3750 name_str = PyUnicode_Concat(tmp, tail);
3751 Py_DECREF(tmp);
3752 Py_DECREF(tail);
3753 break;
3754 }
3755 if (name_str == NULL)
3756 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02003757 _PyErr_Format(tstate, PyExc_TypeError,
3758 "%U() missing %i required %s argument%s: %U",
3759 co->co_name,
3760 len,
3761 kind,
3762 len == 1 ? "" : "s",
3763 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05003764 Py_DECREF(name_str);
3765}
3766
3767static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003768missing_arguments(PyThreadState *tstate, PyCodeObject *co,
3769 Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003770 PyObject **fastlocals)
3771{
Victor Stinner74319ae2016-08-25 00:04:09 +02003772 Py_ssize_t i, j = 0;
3773 Py_ssize_t start, end;
3774 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003775 const char *kind = positional ? "positional" : "keyword-only";
3776 PyObject *missing_names;
3777
3778 /* Compute the names of the arguments that are missing. */
3779 missing_names = PyList_New(missing);
3780 if (missing_names == NULL)
3781 return;
3782 if (positional) {
3783 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01003784 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003785 }
3786 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003787 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003788 end = start + co->co_kwonlyargcount;
3789 }
3790 for (i = start; i < end; i++) {
3791 if (GETLOCAL(i) == NULL) {
3792 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3793 PyObject *name = PyObject_Repr(raw);
3794 if (name == NULL) {
3795 Py_DECREF(missing_names);
3796 return;
3797 }
3798 PyList_SET_ITEM(missing_names, j++, name);
3799 }
3800 }
3801 assert(j == missing);
Victor Stinner438a12d2019-05-24 17:01:38 +02003802 format_missing(tstate, kind, co, missing_names);
Benjamin Petersone109c702011-06-24 09:37:26 -05003803 Py_DECREF(missing_names);
3804}
3805
3806static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003807too_many_positional(PyThreadState *tstate, PyCodeObject *co,
3808 Py_ssize_t given, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003809 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003810{
3811 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003812 Py_ssize_t kwonly_given = 0;
3813 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003814 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003815 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003816
Benjamin Petersone109c702011-06-24 09:37:26 -05003817 assert((co->co_flags & CO_VARARGS) == 0);
3818 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01003819 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003820 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003821 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003822 }
3823 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003824 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01003825 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003826 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01003827 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003828 }
3829 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003830 plural = (co_argcount != 1);
3831 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003832 }
3833 if (sig == NULL)
3834 return;
3835 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003836 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3837 kwonly_sig = PyUnicode_FromFormat(format,
3838 given != 1 ? "s" : "",
3839 kwonly_given,
3840 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003841 if (kwonly_sig == NULL) {
3842 Py_DECREF(sig);
3843 return;
3844 }
3845 }
3846 else {
3847 /* This will not fail. */
3848 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003849 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003850 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003851 _PyErr_Format(tstate, PyExc_TypeError,
3852 "%U() takes %U positional argument%s but %zd%U %s given",
3853 co->co_name,
3854 sig,
3855 plural ? "s" : "",
3856 given,
3857 kwonly_sig,
3858 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003859 Py_DECREF(sig);
3860 Py_DECREF(kwonly_sig);
3861}
3862
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003863static int
Victor Stinner438a12d2019-05-24 17:01:38 +02003864positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
3865 Py_ssize_t kwcount, PyObject* const* kwnames)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003866{
3867 int posonly_conflicts = 0;
3868 PyObject* posonly_names = PyList_New(0);
3869
3870 for(int k=0; k < co->co_posonlyargcount; k++){
3871 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
3872
3873 for (int k2=0; k2<kwcount; k2++){
3874 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
3875 PyObject* kwname = kwnames[k2];
3876 if (kwname == posonly_name){
3877 if(PyList_Append(posonly_names, kwname) != 0) {
3878 goto fail;
3879 }
3880 posonly_conflicts++;
3881 continue;
3882 }
3883
3884 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
3885
3886 if ( cmp > 0) {
3887 if(PyList_Append(posonly_names, kwname) != 0) {
3888 goto fail;
3889 }
3890 posonly_conflicts++;
3891 } else if (cmp < 0) {
3892 goto fail;
3893 }
3894
3895 }
3896 }
3897 if (posonly_conflicts) {
3898 PyObject* comma = PyUnicode_FromString(", ");
3899 if (comma == NULL) {
3900 goto fail;
3901 }
3902 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
3903 Py_DECREF(comma);
3904 if (error_names == NULL) {
3905 goto fail;
3906 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003907 _PyErr_Format(tstate, PyExc_TypeError,
3908 "%U() got some positional-only arguments passed"
3909 " as keyword arguments: '%U'",
3910 co->co_name, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003911 Py_DECREF(error_names);
3912 goto fail;
3913 }
3914
3915 Py_DECREF(posonly_names);
3916 return 0;
3917
3918fail:
3919 Py_XDECREF(posonly_names);
3920 return 1;
3921
3922}
3923
Guido van Rossumc2e20742006-02-27 22:32:47 +00003924/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02003925 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003926 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003927
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003928PyObject *
Victor Stinnerb5e170f2019-11-16 01:03:22 +01003929_PyEval_EvalCode(PyThreadState *tstate,
3930 PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003931 PyObject *const *args, Py_ssize_t argcount,
3932 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003933 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003934 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003935 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003936 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003937{
Victor Stinnerb5e170f2019-11-16 01:03:22 +01003938 assert(tstate != NULL);
3939
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003940 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003941 PyFrameObject *f;
3942 PyObject *retval = NULL;
3943 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003944 PyObject *x, *u;
Pablo Galindocd74e662019-06-01 18:08:04 +01003945 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003946 Py_ssize_t i, j, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003947 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003948
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003949 if (globals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003950 _PyErr_SetString(tstate, PyExc_SystemError,
3951 "PyEval_EvalCodeEx: NULL globals");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003952 return NULL;
3953 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003954
Victor Stinnerc7020012016-08-16 23:40:29 +02003955 /* Create the frame */
INADA Naoki5a625d02016-12-24 20:19:08 +09003956 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003957 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003958 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003959 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003960 fastlocals = f->f_localsplus;
3961 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003962
Victor Stinnerc7020012016-08-16 23:40:29 +02003963 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003964 if (co->co_flags & CO_VARKEYWORDS) {
3965 kwdict = PyDict_New();
3966 if (kwdict == NULL)
3967 goto fail;
3968 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003969 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003970 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003971 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003972 SETLOCAL(i, kwdict);
3973 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003974 else {
3975 kwdict = NULL;
3976 }
3977
Pablo Galindocd74e662019-06-01 18:08:04 +01003978 /* Copy all positional arguments into local variables */
3979 if (argcount > co->co_argcount) {
3980 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003981 }
3982 else {
3983 n = argcount;
3984 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003985 for (j = 0; j < n; j++) {
3986 x = args[j];
3987 Py_INCREF(x);
3988 SETLOCAL(j, x);
3989 }
3990
Victor Stinnerc7020012016-08-16 23:40:29 +02003991 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003992 if (co->co_flags & CO_VARARGS) {
Sergey Fedoseev234531b2019-02-25 21:59:12 +05003993 u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003994 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003995 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003996 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003997 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003998 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003999
Serhiy Storchakab7281052016-09-12 00:52:40 +03004000 /* Handle keyword arguments passed as two strided arrays */
4001 kwcount *= kwstep;
4002 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004003 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03004004 PyObject *keyword = kwnames[i];
4005 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02004006 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004007
Benjamin Petersonb204a422011-06-05 22:04:07 -05004008 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004009 _PyErr_Format(tstate, PyExc_TypeError,
4010 "%U() keywords must be strings",
4011 co->co_name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004012 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004013 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004014
Benjamin Petersonb204a422011-06-05 22:04:07 -05004015 /* Speed hack: do raw pointer compares. As names are
4016 normally interned this should almost always hit. */
4017 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004018 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004019 PyObject *name = co_varnames[j];
4020 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004021 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004022 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004023 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004024
Benjamin Petersonb204a422011-06-05 22:04:07 -05004025 /* Slow fallback, just in case */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004026 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004027 PyObject *name = co_varnames[j];
4028 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
4029 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004030 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004031 }
4032 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004033 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004034 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004035 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004036
Victor Stinner231d1f32017-01-11 02:12:06 +01004037 assert(j >= total_args);
4038 if (kwdict == NULL) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004039
Victor Stinner438a12d2019-05-24 17:01:38 +02004040 if (co->co_posonlyargcount
4041 && positional_only_passed_as_keyword(tstate, co,
4042 kwcount, kwnames))
4043 {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004044 goto fail;
4045 }
4046
Victor Stinner438a12d2019-05-24 17:01:38 +02004047 _PyErr_Format(tstate, PyExc_TypeError,
4048 "%U() got an unexpected keyword argument '%S'",
4049 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004050 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004051 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004052
Christian Heimes0bd447f2013-07-20 14:48:10 +02004053 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4054 goto fail;
4055 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004056 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004057
Benjamin Petersonb204a422011-06-05 22:04:07 -05004058 kw_found:
4059 if (GETLOCAL(j) != NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004060 _PyErr_Format(tstate, PyExc_TypeError,
4061 "%U() got multiple values for argument '%S'",
4062 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004063 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004064 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004065 Py_INCREF(value);
4066 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004067 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004068
4069 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004070 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004071 too_many_positional(tstate, co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004072 goto fail;
4073 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004074
4075 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004076 if (argcount < co->co_argcount) {
4077 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004078 Py_ssize_t missing = 0;
4079 for (i = argcount; i < m; i++) {
4080 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004081 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004082 }
4083 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004084 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004085 missing_arguments(tstate, co, missing, defcount, fastlocals);
Benjamin Petersone109c702011-06-24 09:37:26 -05004086 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004087 }
4088 if (n > m)
4089 i = n - m;
4090 else
4091 i = 0;
4092 for (; i < defcount; i++) {
4093 if (GETLOCAL(m+i) == NULL) {
4094 PyObject *def = defs[i];
4095 Py_INCREF(def);
4096 SETLOCAL(m+i, def);
4097 }
4098 }
4099 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004100
4101 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004102 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004103 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004104 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004105 PyObject *name;
4106 if (GETLOCAL(i) != NULL)
4107 continue;
4108 name = PyTuple_GET_ITEM(co->co_varnames, i);
4109 if (kwdefs != NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004110 PyObject *def = PyDict_GetItemWithError(kwdefs, name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004111 if (def) {
4112 Py_INCREF(def);
4113 SETLOCAL(i, def);
4114 continue;
4115 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004116 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004117 goto fail;
4118 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004119 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004120 missing++;
4121 }
4122 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004123 missing_arguments(tstate, co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004124 goto fail;
4125 }
4126 }
4127
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004128 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004129 vars into frame. */
4130 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004131 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004132 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004133 /* Possibly account for the cell variable being an argument. */
4134 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004135 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004136 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004137 /* Clear the local copy. */
4138 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004139 }
4140 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004141 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004142 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004143 if (c == NULL)
4144 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004145 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004146 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004147
4148 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004149 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4150 PyObject *o = PyTuple_GET_ITEM(closure, i);
4151 Py_INCREF(o);
4152 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004153 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004154
Yury Selivanoveb636452016-09-08 22:01:51 -07004155 /* Handle generator/coroutine/asynchronous generator */
4156 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004157 PyObject *gen;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004158 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004159
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004160 /* Don't need to keep the reference to f_back, it will be set
4161 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004162 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004163
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004164 /* Create a new generator that owns the ready to run frame
4165 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004166 if (is_coro) {
4167 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004168 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4169 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004170 } else {
4171 gen = PyGen_NewWithQualName(f, name, qualname);
4172 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004173 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004174 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004175 }
INADA Naoki9c157762016-12-26 18:52:46 +09004176
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004177 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004178
Yury Selivanov75445082015-05-11 22:57:16 -04004179 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004180 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004181
Victor Stinnerb9e68122019-11-14 12:20:46 +01004182 retval = _PyEval_EvalFrame(tstate, f, 0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004183
Thomas Woutersce272b62007-09-19 21:19:28 +00004184fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004186 /* decref'ing the frame can cause __del__ methods to get invoked,
4187 which can call back into Python. While we're done with the
4188 current Python frame (f), the associated C stack is still in use,
4189 so recursion_depth must be boosted for the duration.
4190 */
INADA Naoki5a625d02016-12-24 20:19:08 +09004191 if (Py_REFCNT(f) > 1) {
4192 Py_DECREF(f);
4193 _PyObject_GC_TRACK(f);
4194 }
4195 else {
4196 ++tstate->recursion_depth;
4197 Py_DECREF(f);
4198 --tstate->recursion_depth;
4199 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004200 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004201}
4202
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004203
4204PyObject *
4205_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
4206 PyObject *const *args, Py_ssize_t argcount,
4207 PyObject *const *kwnames, PyObject *const *kwargs,
4208 Py_ssize_t kwcount, int kwstep,
4209 PyObject *const *defs, Py_ssize_t defcount,
4210 PyObject *kwdefs, PyObject *closure,
4211 PyObject *name, PyObject *qualname)
4212{
4213 PyThreadState *tstate = _PyThreadState_GET();
4214 return _PyEval_EvalCode(tstate, _co, globals, locals,
4215 args, argcount,
4216 kwnames, kwargs,
4217 kwcount, kwstep,
4218 defs, defcount,
4219 kwdefs, closure,
4220 name, qualname);
4221}
4222
Victor Stinner40ee3012014-06-16 15:59:28 +02004223PyObject *
4224PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004225 PyObject *const *args, int argcount,
4226 PyObject *const *kws, int kwcount,
4227 PyObject *const *defs, int defcount,
4228 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004229{
4230 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004231 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004232 kws, kws != NULL ? kws + 1 : NULL,
4233 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004234 defs, defcount,
4235 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004236 NULL, NULL);
4237}
Tim Peters5ca576e2001-06-18 22:08:13 +00004238
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004239static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004240special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004241{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004242 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004243 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02004244 if (res == NULL && !_PyErr_Occurred(tstate)) {
4245 _PyErr_SetObject(tstate, PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004246 return NULL;
4247 }
4248 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004249}
4250
4251
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004252/* Logic for the raise statement (too complicated for inlining).
4253 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004254static int
Victor Stinner09532fe2019-05-10 23:39:09 +02004255do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004256{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004257 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004259 if (exc == NULL) {
4260 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01004261 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004262 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004263 type = exc_info->exc_type;
4264 value = exc_info->exc_value;
4265 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004266 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004267 _PyErr_SetString(tstate, PyExc_RuntimeError,
4268 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004269 return 0;
4270 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004271 Py_XINCREF(type);
4272 Py_XINCREF(value);
4273 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02004274 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004275 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004276 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004278 /* We support the following forms of raise:
4279 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004280 raise <instance>
4281 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004283 if (PyExceptionClass_Check(exc)) {
4284 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004285 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004286 if (value == NULL)
4287 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004288 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004289 _PyErr_Format(tstate, PyExc_TypeError,
4290 "calling %R should have returned an instance of "
4291 "BaseException, not %R",
4292 type, Py_TYPE(value));
4293 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004294 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004295 }
4296 else if (PyExceptionInstance_Check(exc)) {
4297 value = exc;
4298 type = PyExceptionInstance_Class(exc);
4299 Py_INCREF(type);
4300 }
4301 else {
4302 /* Not something you can raise. You get an exception
4303 anyway, just not what you specified :-) */
4304 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02004305 _PyErr_SetString(tstate, PyExc_TypeError,
4306 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004307 goto raise_error;
4308 }
Collin Winter828f04a2007-08-31 00:04:24 +00004309
Serhiy Storchakac0191582016-09-27 11:37:10 +03004310 assert(type != NULL);
4311 assert(value != NULL);
4312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004313 if (cause) {
4314 PyObject *fixed_cause;
4315 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004316 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004317 if (fixed_cause == NULL)
4318 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004319 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004320 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004321 else if (PyExceptionInstance_Check(cause)) {
4322 fixed_cause = cause;
4323 }
4324 else if (cause == Py_None) {
4325 Py_DECREF(cause);
4326 fixed_cause = NULL;
4327 }
4328 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004329 _PyErr_SetString(tstate, PyExc_TypeError,
4330 "exception causes must derive from "
4331 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004332 goto raise_error;
4333 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004334 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004335 }
Collin Winter828f04a2007-08-31 00:04:24 +00004336
Victor Stinner438a12d2019-05-24 17:01:38 +02004337 _PyErr_SetObject(tstate, type, value);
Victor Stinner61f4db82020-01-28 03:37:45 +01004338 /* _PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004339 Py_DECREF(value);
4340 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004341 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004342
4343raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004344 Py_XDECREF(value);
4345 Py_XDECREF(type);
4346 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004347 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004348}
4349
Tim Petersd6d010b2001-06-21 02:49:55 +00004350/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004351 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004352
Guido van Rossum0368b722007-05-11 16:50:42 +00004353 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4354 with a variable target.
4355*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004356
Barry Warsawe42b18f1997-08-25 22:13:04 +00004357static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004358unpack_iterable(PyThreadState *tstate, PyObject *v,
4359 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004360{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004361 int i = 0, j = 0;
4362 Py_ssize_t ll = 0;
4363 PyObject *it; /* iter(v) */
4364 PyObject *w;
4365 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004367 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004369 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004370 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004371 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01004372 Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v))
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004373 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004374 _PyErr_Format(tstate, PyExc_TypeError,
4375 "cannot unpack non-iterable %.200s object",
Victor Stinnera102ed72020-02-07 02:24:48 +01004376 Py_TYPE(v)->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004377 }
4378 return 0;
4379 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004381 for (; i < argcnt; i++) {
4382 w = PyIter_Next(it);
4383 if (w == NULL) {
4384 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004385 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04004386 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004387 _PyErr_Format(tstate, PyExc_ValueError,
4388 "not enough values to unpack "
4389 "(expected %d, got %d)",
4390 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004391 }
4392 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004393 _PyErr_Format(tstate, PyExc_ValueError,
4394 "not enough values to unpack "
4395 "(expected at least %d, got %d)",
4396 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004397 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004398 }
4399 goto Error;
4400 }
4401 *--sp = w;
4402 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004404 if (argcntafter == -1) {
4405 /* We better have exhausted the iterator now. */
4406 w = PyIter_Next(it);
4407 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004408 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004409 goto Error;
4410 Py_DECREF(it);
4411 return 1;
4412 }
4413 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02004414 _PyErr_Format(tstate, PyExc_ValueError,
4415 "too many values to unpack (expected %d)",
4416 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004417 goto Error;
4418 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004420 l = PySequence_List(it);
4421 if (l == NULL)
4422 goto Error;
4423 *--sp = l;
4424 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004426 ll = PyList_GET_SIZE(l);
4427 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004428 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04004429 "not enough values to unpack (expected at least %d, got %zd)",
4430 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004431 goto Error;
4432 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004434 /* Pop the "after-variable" args off the list. */
4435 for (j = argcntafter; j > 0; j--, i++) {
4436 *--sp = PyList_GET_ITEM(l, ll - j);
4437 }
4438 /* Resize the list. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +01004439 Py_SET_SIZE(l, ll - argcntafter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004440 Py_DECREF(it);
4441 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004442
Tim Petersd6d010b2001-06-21 02:49:55 +00004443Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004444 for (; i > 0; i--, sp++)
4445 Py_DECREF(*sp);
4446 Py_XDECREF(it);
4447 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004448}
4449
4450
Guido van Rossum96a42c81992-01-12 02:29:51 +00004451#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004452static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004453prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004454{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004455 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02004456 if (PyObject_Print(v, stdout, 0) != 0) {
4457 /* Don't know what else to do */
4458 _PyErr_Clear(tstate);
4459 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004460 printf("\n");
4461 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004462}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004463#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004464
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004465static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004466call_exc_trace(Py_tracefunc func, PyObject *self,
4467 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004468{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004469 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004470 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004471 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004472 if (value == NULL) {
4473 value = Py_None;
4474 Py_INCREF(value);
4475 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004476 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01004477 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004478 arg = PyTuple_Pack(3, type, value, traceback);
4479 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004480 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004481 return;
4482 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004483 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004484 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02004485 if (err == 0) {
4486 _PyErr_Restore(tstate, type, value, orig_traceback);
4487 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004488 else {
4489 Py_XDECREF(type);
4490 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004491 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004492 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004493}
4494
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004495static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004496call_trace_protected(Py_tracefunc func, PyObject *obj,
4497 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004498 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004499{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004500 PyObject *type, *value, *traceback;
4501 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004502 _PyErr_Fetch(tstate, &type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004503 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004504 if (err == 0)
4505 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004506 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004507 return 0;
4508 }
4509 else {
4510 Py_XDECREF(type);
4511 Py_XDECREF(value);
4512 Py_XDECREF(traceback);
4513 return -1;
4514 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004515}
4516
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004517static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004518call_trace(Py_tracefunc func, PyObject *obj,
4519 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004520 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004521{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004522 int result;
4523 if (tstate->tracing)
4524 return 0;
4525 tstate->tracing++;
4526 tstate->use_tracing = 0;
4527 result = func(obj, frame, what, arg);
4528 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4529 || (tstate->c_profilefunc != NULL));
4530 tstate->tracing--;
4531 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004532}
4533
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004534PyObject *
4535_PyEval_CallTracing(PyObject *func, PyObject *args)
4536{
Victor Stinner50b48572018-11-01 01:51:40 +01004537 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004538 int save_tracing = tstate->tracing;
4539 int save_use_tracing = tstate->use_tracing;
4540 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004542 tstate->tracing = 0;
4543 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4544 || (tstate->c_profilefunc != NULL));
4545 result = PyObject_Call(func, args, NULL);
4546 tstate->tracing = save_tracing;
4547 tstate->use_tracing = save_use_tracing;
4548 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004549}
4550
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004551/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004552static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004553maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004554 PyThreadState *tstate, PyFrameObject *frame,
4555 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004556{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004557 int result = 0;
4558 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004560 /* If the last instruction executed isn't in the current
4561 instruction window, reset the window.
4562 */
4563 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4564 PyAddrPair bounds;
4565 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4566 &bounds);
4567 *instr_lb = bounds.ap_lower;
4568 *instr_ub = bounds.ap_upper;
4569 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004570 /* If the last instruction falls at the start of a line or if it
4571 represents a jump backwards, update the frame's line number and
4572 then call the trace function if we're tracing source lines.
4573 */
4574 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004575 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004576 if (frame->f_trace_lines) {
4577 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4578 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004579 }
George King20faa682017-10-18 17:44:22 -07004580 /* Always emit an opcode event if we're tracing all opcodes. */
4581 if (frame->f_trace_opcodes) {
4582 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4583 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004584 *instr_prev = frame->f_lasti;
4585 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004586}
4587
Fred Drake5755ce62001-06-27 19:19:46 +00004588void
4589PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004590{
Steve Dowerb82e17e2019-05-23 08:45:22 -07004591 if (PySys_Audit("sys.setprofile", NULL) < 0) {
4592 return;
4593 }
4594
Victor Stinner50b48572018-11-01 01:51:40 +01004595 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004596 PyObject *temp = tstate->c_profileobj;
4597 Py_XINCREF(arg);
4598 tstate->c_profilefunc = NULL;
4599 tstate->c_profileobj = NULL;
4600 /* Must make sure that tracing is not ignored if 'temp' is freed */
4601 tstate->use_tracing = tstate->c_tracefunc != NULL;
4602 Py_XDECREF(temp);
4603 tstate->c_profilefunc = func;
4604 tstate->c_profileobj = arg;
4605 /* Flag that tracing or profiling is turned on */
4606 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004607}
4608
4609void
4610PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4611{
Steve Dowerb82e17e2019-05-23 08:45:22 -07004612 if (PySys_Audit("sys.settrace", NULL) < 0) {
4613 return;
4614 }
4615
Victor Stinner09532fe2019-05-10 23:39:09 +02004616 _PyRuntimeState *runtime = &_PyRuntime;
4617 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004618 PyObject *temp = tstate->c_traceobj;
Victor Stinner09532fe2019-05-10 23:39:09 +02004619 runtime->ceval.tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004620 Py_XINCREF(arg);
4621 tstate->c_tracefunc = NULL;
4622 tstate->c_traceobj = NULL;
4623 /* Must make sure that profiling is not ignored if 'temp' is freed */
4624 tstate->use_tracing = tstate->c_profilefunc != NULL;
4625 Py_XDECREF(temp);
4626 tstate->c_tracefunc = func;
4627 tstate->c_traceobj = arg;
4628 /* Flag that tracing or profiling is turned on */
4629 tstate->use_tracing = ((func != NULL)
4630 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004631}
4632
Yury Selivanov75445082015-05-11 22:57:16 -04004633void
Victor Stinner838f2642019-06-13 22:41:23 +02004634_PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004635{
4636 assert(new_depth >= 0);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004637 tstate->coroutine_origin_tracking_depth = new_depth;
4638}
4639
4640int
4641_PyEval_GetCoroutineOriginTrackingDepth(void)
4642{
Victor Stinner50b48572018-11-01 01:51:40 +01004643 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004644 return tstate->coroutine_origin_tracking_depth;
4645}
4646
4647void
Yury Selivanoveb636452016-09-08 22:01:51 -07004648_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4649{
Victor Stinner50b48572018-11-01 01:51:40 +01004650 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004651
4652 if (PySys_Audit("sys.set_asyncgen_hook_firstiter", NULL) < 0) {
4653 return;
4654 }
4655
Yury Selivanoveb636452016-09-08 22:01:51 -07004656 Py_XINCREF(firstiter);
4657 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4658}
4659
4660PyObject *
4661_PyEval_GetAsyncGenFirstiter(void)
4662{
Victor Stinner50b48572018-11-01 01:51:40 +01004663 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004664 return tstate->async_gen_firstiter;
4665}
4666
4667void
4668_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4669{
Victor Stinner50b48572018-11-01 01:51:40 +01004670 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004671
4672 if (PySys_Audit("sys.set_asyncgen_hook_finalizer", NULL) < 0) {
4673 return;
4674 }
4675
Yury Selivanoveb636452016-09-08 22:01:51 -07004676 Py_XINCREF(finalizer);
4677 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4678}
4679
4680PyObject *
4681_PyEval_GetAsyncGenFinalizer(void)
4682{
Victor Stinner50b48572018-11-01 01:51:40 +01004683 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004684 return tstate->async_gen_finalizer;
4685}
4686
Victor Stinner438a12d2019-05-24 17:01:38 +02004687static PyFrameObject *
4688_PyEval_GetFrame(PyThreadState *tstate)
4689{
Victor Stinner01b1cc12019-11-20 02:27:56 +01004690 _PyRuntimeState *runtime = tstate->interp->runtime;
4691 return runtime->gilstate.getframe(tstate);
Victor Stinner438a12d2019-05-24 17:01:38 +02004692}
4693
4694PyFrameObject *
4695PyEval_GetFrame(void)
4696{
4697 PyThreadState *tstate = _PyThreadState_GET();
4698 return _PyEval_GetFrame(tstate);
4699}
4700
Guido van Rossumb209a111997-04-29 18:18:01 +00004701PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004702PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004703{
Victor Stinner438a12d2019-05-24 17:01:38 +02004704 PyThreadState *tstate = _PyThreadState_GET();
4705 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004706 if (current_frame == NULL)
Victor Stinner438a12d2019-05-24 17:01:38 +02004707 return tstate->interp->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004708 else
4709 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004710}
4711
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004712/* Convenience function to get a builtin from its name */
4713PyObject *
4714_PyEval_GetBuiltinId(_Py_Identifier *name)
4715{
Victor Stinner438a12d2019-05-24 17:01:38 +02004716 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004717 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4718 if (attr) {
4719 Py_INCREF(attr);
4720 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004721 else if (!_PyErr_Occurred(tstate)) {
4722 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004723 }
4724 return attr;
4725}
4726
Guido van Rossumb209a111997-04-29 18:18:01 +00004727PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004728PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004729{
Victor Stinner438a12d2019-05-24 17:01:38 +02004730 PyThreadState *tstate = _PyThreadState_GET();
4731 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Victor Stinner41bb43a2013-10-29 01:19:37 +01004732 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004733 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004734 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004735 }
4736
Victor Stinner438a12d2019-05-24 17:01:38 +02004737 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01004738 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004739 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004740
4741 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004742 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004743}
4744
Guido van Rossumb209a111997-04-29 18:18:01 +00004745PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004746PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004747{
Victor Stinner438a12d2019-05-24 17:01:38 +02004748 PyThreadState *tstate = _PyThreadState_GET();
4749 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
4750 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004751 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004752 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004753
4754 assert(current_frame->f_globals != NULL);
4755 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004756}
4757
Guido van Rossum6135a871995-01-09 17:53:26 +00004758int
Tim Peters5ba58662001-07-16 02:29:45 +00004759PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004760{
Victor Stinner438a12d2019-05-24 17:01:38 +02004761 PyThreadState *tstate = _PyThreadState_GET();
4762 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004763 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004765 if (current_frame != NULL) {
4766 const int codeflags = current_frame->f_code->co_flags;
4767 const int compilerflags = codeflags & PyCF_MASK;
4768 if (compilerflags) {
4769 result = 1;
4770 cf->cf_flags |= compilerflags;
4771 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004772#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004773 if (codeflags & CO_GENERATOR_ALLOWED) {
4774 result = 1;
4775 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4776 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004777#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004778 }
4779 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004780}
4781
Guido van Rossum3f5da241990-12-20 15:06:42 +00004782
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004783const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004784PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004785{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004786 if (PyMethod_Check(func))
4787 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4788 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004789 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004790 else if (PyCFunction_Check(func))
4791 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4792 else
Victor Stinnera102ed72020-02-07 02:24:48 +01004793 return Py_TYPE(func)->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004794}
4795
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004796const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004797PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004798{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004799 if (PyMethod_Check(func))
4800 return "()";
4801 else if (PyFunction_Check(func))
4802 return "()";
4803 else if (PyCFunction_Check(func))
4804 return "()";
4805 else
4806 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004807}
4808
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004809#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004810if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004811 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4812 tstate, tstate->frame, \
4813 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004814 x = NULL; \
4815 } \
4816 else { \
4817 x = call; \
4818 if (tstate->c_profilefunc != NULL) { \
4819 if (x == NULL) { \
4820 call_trace_protected(tstate->c_profilefunc, \
4821 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004822 tstate, tstate->frame, \
4823 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004824 /* XXX should pass (type, value, tb) */ \
4825 } else { \
4826 if (call_trace(tstate->c_profilefunc, \
4827 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004828 tstate, tstate->frame, \
4829 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004830 Py_DECREF(x); \
4831 x = NULL; \
4832 } \
4833 } \
4834 } \
4835 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004836} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004837 x = call; \
4838 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004839
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004840
4841static PyObject *
4842trace_call_function(PyThreadState *tstate,
4843 PyObject *func,
4844 PyObject **args, Py_ssize_t nargs,
4845 PyObject *kwnames)
4846{
4847 PyObject *x;
4848 if (PyCFunction_Check(func)) {
Petr Viktorinffd97532020-02-11 17:46:57 +01004849 C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004850 return x;
4851 }
4852 else if (Py_TYPE(func) == &PyMethodDescr_Type && nargs > 0) {
4853 /* We need to create a temporary bound method as argument
4854 for profiling.
4855
4856 If nargs == 0, then this cannot work because we have no
4857 "self". In any case, the call itself would raise
4858 TypeError (foo needs an argument), so we just skip
4859 profiling. */
4860 PyObject *self = args[0];
4861 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4862 if (func == NULL) {
4863 return NULL;
4864 }
Petr Viktorinffd97532020-02-11 17:46:57 +01004865 C_TRACE(x, PyObject_Vectorcall(func,
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02004866 args+1, nargs-1,
4867 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004868 Py_DECREF(func);
4869 return x;
4870 }
Petr Viktorinffd97532020-02-11 17:46:57 +01004871 return PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004872}
4873
Victor Stinner415c5102017-01-11 00:54:57 +01004874/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4875 to reduce the stack consumption. */
4876Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Victor Stinner09532fe2019-05-10 23:39:09 +02004877call_function(PyThreadState *tstate, PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004878{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004879 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004880 PyObject *func = *pfunc;
4881 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004882 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4883 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004884 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004885
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004886 if (tstate->use_tracing) {
4887 x = trace_call_function(tstate, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09004888 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004889 else {
Petr Viktorinffd97532020-02-11 17:46:57 +01004890 x = PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004891 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004892
Victor Stinner438a12d2019-05-24 17:01:38 +02004893 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004894
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004895 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004896 while ((*pp_stack) > pfunc) {
4897 w = EXT_POP(*pp_stack);
4898 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004899 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004900
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004901 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004902}
4903
Jeremy Hylton52820442001-01-03 23:52:36 +00004904static PyObject *
Victor Stinner09532fe2019-05-10 23:39:09 +02004905do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004906{
jdemeyere89de732018-09-19 12:06:20 +02004907 PyObject *result;
4908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004909 if (PyCFunction_Check(func)) {
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +02004910 C_TRACE(result, PyObject_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004911 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004912 }
jdemeyere89de732018-09-19 12:06:20 +02004913 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
jdemeyere89de732018-09-19 12:06:20 +02004914 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
4915 if (nargs > 0 && tstate->use_tracing) {
4916 /* We need to create a temporary bound method as argument
4917 for profiling.
4918
4919 If nargs == 0, then this cannot work because we have no
4920 "self". In any case, the call itself would raise
4921 TypeError (foo needs an argument), so we just skip
4922 profiling. */
4923 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
4924 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4925 if (func == NULL) {
4926 return NULL;
4927 }
4928
Victor Stinner4d231bc2019-11-14 13:36:21 +01004929 C_TRACE(result, _PyObject_FastCallDictTstate(
4930 tstate, func,
4931 &_PyTuple_ITEMS(callargs)[1],
4932 nargs - 1,
4933 kwdict));
jdemeyere89de732018-09-19 12:06:20 +02004934 Py_DECREF(func);
4935 return result;
4936 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004937 }
jdemeyere89de732018-09-19 12:06:20 +02004938 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00004939}
4940
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004941/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004942 nb_index slot defined, and store in *pi.
4943 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004944 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004945 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004946*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004947int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004948_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004949{
Victor Stinner438a12d2019-05-24 17:01:38 +02004950 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004951 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004952 Py_ssize_t x;
4953 if (PyIndex_Check(v)) {
4954 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02004955 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004956 return 0;
4957 }
4958 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004959 _PyErr_SetString(tstate, PyExc_TypeError,
4960 "slice indices must be integers or "
4961 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004962 return 0;
4963 }
4964 *pi = x;
4965 }
4966 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004967}
4968
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004969int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004970_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004971{
Victor Stinner438a12d2019-05-24 17:01:38 +02004972 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004973 Py_ssize_t x;
4974 if (PyIndex_Check(v)) {
4975 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02004976 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004977 return 0;
4978 }
4979 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004980 _PyErr_SetString(tstate, PyExc_TypeError,
4981 "slice indices must be integers or "
4982 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004983 return 0;
4984 }
4985 *pi = x;
4986 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004987}
4988
Thomas Wouters52152252000-08-17 22:55:00 +00004989static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004990import_name(PyThreadState *tstate, PyFrameObject *f,
4991 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004992{
4993 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004994 PyObject *import_func, *res;
4995 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004996
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004997 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004998 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004999 if (!_PyErr_Occurred(tstate)) {
5000 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005001 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005002 return NULL;
5003 }
5004
5005 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005006 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005007 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02005008 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005009 return NULL;
5010 }
5011 res = PyImport_ImportModuleLevelObject(
5012 name,
5013 f->f_globals,
5014 f->f_locals == NULL ? Py_None : f->f_locals,
5015 fromlist,
5016 ilevel);
5017 return res;
5018 }
5019
5020 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005021
5022 stack[0] = name;
5023 stack[1] = f->f_globals;
5024 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5025 stack[3] = fromlist;
5026 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005027 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005028 Py_DECREF(import_func);
5029 return res;
5030}
5031
5032static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005033import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005034{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005035 PyObject *x;
Xiang Zhang4830f582017-03-21 11:13:42 +08005036 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005037
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005038 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005039 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005040 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005041 /* Issue #17636: in case this failed because of a circular relative
5042 import, try to fallback on reading the module directly from
5043 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02005044 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005045 if (pkgname == NULL) {
5046 goto error;
5047 }
Oren Milman6db70332017-09-19 14:23:01 +03005048 if (!PyUnicode_Check(pkgname)) {
5049 Py_CLEAR(pkgname);
5050 goto error;
5051 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005052 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005053 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005054 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005055 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005056 }
Eric Snow3f9eee62017-09-15 16:35:20 -06005057 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005058 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02005059 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07005060 goto error;
5061 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005062 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005063 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005064 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005065 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005066 if (pkgname == NULL) {
5067 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5068 if (pkgname_or_unknown == NULL) {
5069 Py_XDECREF(pkgpath);
5070 return NULL;
5071 }
5072 } else {
5073 pkgname_or_unknown = pkgname;
5074 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005075
5076 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005077 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08005078 errmsg = PyUnicode_FromFormat(
5079 "cannot import name %R from %R (unknown location)",
5080 name, pkgname_or_unknown
5081 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005082 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005083 PyErr_SetImportError(errmsg, pkgname, NULL);
5084 }
5085 else {
Anthony Sottile65366bc2019-09-09 08:17:50 -07005086 _Py_IDENTIFIER(__spec__);
5087 PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
Anthony Sottile65366bc2019-09-09 08:17:50 -07005088 const char *fmt =
5089 _PyModuleSpec_IsInitializing(spec) ?
5090 "cannot import name %R from partially initialized module %R "
5091 "(most likely due to a circular import) (%S)" :
5092 "cannot import name %R from %R (%S)";
5093 Py_XDECREF(spec);
5094
5095 errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
Stefan Krah027b09c2019-03-25 21:50:58 +01005096 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005097 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005098 }
5099
Xiang Zhang4830f582017-03-21 11:13:42 +08005100 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005101 Py_XDECREF(pkgname_or_unknown);
5102 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005103 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005104}
Guido van Rossumac7be682001-01-17 15:42:30 +00005105
Thomas Wouters52152252000-08-17 22:55:00 +00005106static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005107import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00005108{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005109 _Py_IDENTIFIER(__all__);
5110 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005111 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005112 int skip_leading_underscores = 0;
5113 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005114
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005115 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5116 return -1; /* Unexpected error */
5117 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005118 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005119 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5120 return -1;
5121 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005122 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005123 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005124 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005125 return -1;
5126 }
5127 all = PyMapping_Keys(dict);
5128 Py_DECREF(dict);
5129 if (all == NULL)
5130 return -1;
5131 skip_leading_underscores = 1;
5132 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005133
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005134 for (pos = 0, err = 0; ; pos++) {
5135 name = PySequence_GetItem(all, pos);
5136 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005137 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005138 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02005139 }
5140 else {
5141 _PyErr_Clear(tstate);
5142 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005143 break;
5144 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005145 if (!PyUnicode_Check(name)) {
5146 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5147 if (modname == NULL) {
5148 Py_DECREF(name);
5149 err = -1;
5150 break;
5151 }
5152 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005153 _PyErr_Format(tstate, PyExc_TypeError,
5154 "module __name__ must be a string, not %.100s",
5155 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005156 }
5157 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005158 _PyErr_Format(tstate, PyExc_TypeError,
5159 "%s in %U.%s must be str, not %.100s",
5160 skip_leading_underscores ? "Key" : "Item",
5161 modname,
5162 skip_leading_underscores ? "__dict__" : "__all__",
5163 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005164 }
5165 Py_DECREF(modname);
5166 Py_DECREF(name);
5167 err = -1;
5168 break;
5169 }
5170 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005171 if (PyUnicode_READY(name) == -1) {
5172 Py_DECREF(name);
5173 err = -1;
5174 break;
5175 }
5176 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5177 Py_DECREF(name);
5178 continue;
5179 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005180 }
5181 value = PyObject_GetAttr(v, name);
5182 if (value == NULL)
5183 err = -1;
5184 else if (PyDict_CheckExact(locals))
5185 err = PyDict_SetItem(locals, name, value);
5186 else
5187 err = PyObject_SetItem(locals, name, value);
5188 Py_DECREF(name);
5189 Py_XDECREF(value);
5190 if (err != 0)
5191 break;
5192 }
5193 Py_DECREF(all);
5194 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005195}
5196
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005197static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005198check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005199{
Victor Stinnera102ed72020-02-07 02:24:48 +01005200 if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005201 /* check_args_iterable() may be called with a live exception:
5202 * clear it to prevent calling _PyObject_FunctionStr() with an
5203 * exception set. */
Victor Stinner61f4db82020-01-28 03:37:45 +01005204 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005205 PyObject *funcstr = _PyObject_FunctionStr(func);
5206 if (funcstr != NULL) {
5207 _PyErr_Format(tstate, PyExc_TypeError,
5208 "%U argument after * must be an iterable, not %.200s",
5209 funcstr, Py_TYPE(args)->tp_name);
5210 Py_DECREF(funcstr);
5211 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005212 return -1;
5213 }
5214 return 0;
5215}
5216
5217static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005218format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005219{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005220 /* _PyDict_MergeEx raises attribute
5221 * error (percolated from an attempt
5222 * to get 'keys' attribute) instead of
5223 * a type error if its second argument
5224 * is not a mapping.
5225 */
Victor Stinner438a12d2019-05-24 17:01:38 +02005226 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005227 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005228 PyObject *funcstr = _PyObject_FunctionStr(func);
5229 if (funcstr != NULL) {
5230 _PyErr_Format(
5231 tstate, PyExc_TypeError,
5232 "%U argument after ** must be a mapping, not %.200s",
5233 funcstr, Py_TYPE(kwargs)->tp_name);
5234 Py_DECREF(funcstr);
5235 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005236 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005237 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005238 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02005239 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005240 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005241 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005242 PyObject *funcstr = _PyObject_FunctionStr(func);
5243 if (funcstr != NULL) {
5244 PyObject *key = PyTuple_GET_ITEM(val, 0);
5245 _PyErr_Format(
5246 tstate, PyExc_TypeError,
5247 "%U got multiple values for keyword argument '%S'",
5248 funcstr, key);
5249 Py_DECREF(funcstr);
5250 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005251 Py_XDECREF(exc);
5252 Py_XDECREF(val);
5253 Py_XDECREF(tb);
5254 }
5255 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005256 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005257 }
5258 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005259}
5260
Guido van Rossumac7be682001-01-17 15:42:30 +00005261static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005262format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
5263 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005264{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005265 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005267 if (!obj)
5268 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005269
Serhiy Storchaka06515832016-11-20 09:13:07 +02005270 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005271 if (!obj_str)
5272 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005273
Victor Stinner438a12d2019-05-24 17:01:38 +02005274 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005275}
Guido van Rossum950361c1997-01-24 13:49:28 +00005276
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005277static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005278format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005279{
5280 PyObject *name;
5281 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02005282 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005283 return;
5284 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5285 name = PyTuple_GET_ITEM(co->co_cellvars,
5286 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005287 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005288 PyExc_UnboundLocalError,
5289 UNBOUNDLOCAL_ERROR_MSG,
5290 name);
5291 } else {
5292 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5293 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02005294 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005295 UNBOUNDFREE_ERROR_MSG, name);
5296 }
5297}
5298
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005299static void
Mark Shannonfee55262019-11-21 09:11:43 +00005300format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevprevopcode, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005301{
5302 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5303 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005304 _PyErr_Format(tstate, PyExc_TypeError,
5305 "'async with' received an object from __aenter__ "
5306 "that does not implement __await__: %.100s",
5307 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005308 }
Mark Shannonfee55262019-11-21 09:11:43 +00005309 else if (prevopcode == WITH_EXCEPT_START || (prevopcode == CALL_FUNCTION && prevprevopcode == DUP_TOP)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005310 _PyErr_Format(tstate, PyExc_TypeError,
5311 "'async with' received an object from __aexit__ "
5312 "that does not implement __await__: %.100s",
5313 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005314 }
5315 }
5316}
5317
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005318static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005319unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005320 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005321{
5322 PyObject *res;
5323 if (Py_REFCNT(v) == 2) {
5324 /* In the common case, there are 2 references to the value
5325 * stored in 'variable' when the += is performed: one on the
5326 * value stack (in 'v') and one still stored in the
5327 * 'variable'. We try to delete the variable now to reduce
5328 * the refcnt to 1.
5329 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005330 int opcode, oparg;
5331 NEXTOPARG();
5332 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005333 case STORE_FAST:
5334 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005335 PyObject **fastlocals = f->f_localsplus;
5336 if (GETLOCAL(oparg) == v)
5337 SETLOCAL(oparg, NULL);
5338 break;
5339 }
5340 case STORE_DEREF:
5341 {
5342 PyObject **freevars = (f->f_localsplus +
5343 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005344 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005345 if (PyCell_GET(c) == v) {
5346 PyCell_SET(c, NULL);
5347 Py_DECREF(v);
5348 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005349 break;
5350 }
5351 case STORE_NAME:
5352 {
5353 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005354 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005355 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005356 if (locals && PyDict_CheckExact(locals)) {
5357 PyObject *w = PyDict_GetItemWithError(locals, name);
5358 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02005359 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005360 {
5361 Py_DECREF(v);
5362 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005363 }
5364 }
5365 break;
5366 }
5367 }
5368 }
5369 res = v;
5370 PyUnicode_Append(&res, w);
5371 return res;
5372}
5373
Guido van Rossum950361c1997-01-24 13:49:28 +00005374#ifdef DYNAMIC_EXECUTION_PROFILE
5375
Skip Montanarof118cb12001-10-15 20:51:38 +00005376static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005377getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005378{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005379 int i;
5380 PyObject *l = PyList_New(256);
5381 if (l == NULL) return NULL;
5382 for (i = 0; i < 256; i++) {
5383 PyObject *x = PyLong_FromLong(a[i]);
5384 if (x == NULL) {
5385 Py_DECREF(l);
5386 return NULL;
5387 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005388 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005389 }
5390 for (i = 0; i < 256; i++)
5391 a[i] = 0;
5392 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005393}
5394
5395PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005396_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005397{
5398#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005399 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005400#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005401 int i;
5402 PyObject *l = PyList_New(257);
5403 if (l == NULL) return NULL;
5404 for (i = 0; i < 257; i++) {
5405 PyObject *x = getarray(dxpairs[i]);
5406 if (x == NULL) {
5407 Py_DECREF(l);
5408 return NULL;
5409 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005410 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005411 }
5412 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005413#endif
5414}
5415
5416#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005417
5418Py_ssize_t
5419_PyEval_RequestCodeExtraIndex(freefunc free)
5420{
Victor Stinnercaba55b2018-08-03 15:33:52 +02005421 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -07005422 Py_ssize_t new_index;
5423
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005424 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005425 return -1;
5426 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005427 new_index = interp->co_extra_user_count++;
5428 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005429 return new_index;
5430}
Łukasz Langaa785c872016-09-09 17:37:37 -07005431
5432static void
5433dtrace_function_entry(PyFrameObject *f)
5434{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005435 const char *filename;
5436 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005437 int lineno;
5438
5439 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5440 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5441 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5442
Andy Lestere6be9b52020-02-11 20:28:35 -06005443 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005444}
5445
5446static void
5447dtrace_function_return(PyFrameObject *f)
5448{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005449 const char *filename;
5450 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005451 int lineno;
5452
5453 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5454 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5455 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5456
Andy Lestere6be9b52020-02-11 20:28:35 -06005457 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005458}
5459
5460/* DTrace equivalent of maybe_call_line_trace. */
5461static void
5462maybe_dtrace_line(PyFrameObject *frame,
5463 int *instr_lb, int *instr_ub, int *instr_prev)
5464{
5465 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005466 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005467
5468 /* If the last instruction executed isn't in the current
5469 instruction window, reset the window.
5470 */
5471 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5472 PyAddrPair bounds;
5473 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5474 &bounds);
5475 *instr_lb = bounds.ap_lower;
5476 *instr_ub = bounds.ap_upper;
5477 }
5478 /* If the last instruction falls at the start of a line or if
5479 it represents a jump backwards, update the frame's line
5480 number and call the trace function. */
5481 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5482 frame->f_lineno = line;
5483 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5484 if (!co_filename)
5485 co_filename = "?";
5486 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5487 if (!co_name)
5488 co_name = "?";
Andy Lestere6be9b52020-02-11 20:28:35 -06005489 PyDTrace_LINE(co_filename, co_name, line);
Łukasz Langaa785c872016-09-09 17:37:37 -07005490 }
5491 *instr_prev = frame->f_lasti;
5492}
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005493
5494
5495/* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions
5496 for the limited API. */
5497
5498#undef Py_EnterRecursiveCall
5499
5500int Py_EnterRecursiveCall(const char *where)
5501{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005502 return _Py_EnterRecursiveCall_inline(where);
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005503}
5504
5505#undef Py_LeaveRecursiveCall
5506
5507void Py_LeaveRecursiveCall(void)
5508{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005509 _Py_LeaveRecursiveCall_inline();
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005510}