blob: ee13fd1ad704311d0be20992e1a261de8b1eef43 [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
Guido van Rossum5b722181993-03-30 17:46:03 +000042
Guido van Rossum374a9221991-04-04 10:40:29 +000043/* Forward declarations */
Victor Stinner09532fe2019-05-10 23:39:09 +020044Py_LOCAL_INLINE(PyObject *) call_function(
45 PyThreadState *tstate, PyObject ***pp_stack,
46 Py_ssize_t oparg, PyObject *kwnames);
47static PyObject * do_call_core(
48 PyThreadState *tstate, PyObject *func,
49 PyObject *callargs, PyObject *kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +000050
Guido van Rossum0a066c01992-03-27 17:29:15 +000051#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000052static int lltrace;
Victor Stinner438a12d2019-05-24 17:01:38 +020053static int prtrace(PyThreadState *, PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000054#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010055static int call_trace(Py_tracefunc, PyObject *,
56 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000057 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000058static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010059 PyThreadState *, PyFrameObject *,
60 int, PyObject *);
61static void call_exc_trace(Py_tracefunc, PyObject *,
62 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000063static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Eric Snow2ebc5ce2017-09-07 23:51:28 -060064 PyThreadState *, PyFrameObject *,
65 int *, int *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070066static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *);
67static void dtrace_function_entry(PyFrameObject *);
68static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000069
Victor Stinner438a12d2019-05-24 17:01:38 +020070static PyObject * cmp_outcome(PyThreadState *, int, PyObject *, PyObject *);
71static 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);
82static void format_awaitable_error(PyThreadState *, PyTypeObject *, 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 Stinner09532fe2019-05-10 23:39:09 +0200306 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200308 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200309
Victor Stinner01b1cc12019-11-20 02:27:56 +0100310 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200311 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinner09532fe2019-05-10 23:39:09 +0200312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 /* Check someone has called PyEval_InitThreads() to create the lock */
Victor Stinnere225beb2019-06-03 18:14:24 +0200314 assert(gil_created(&ceval->gil));
315 take_gil(ceval, tstate);
Victor Stinner01b1cc12019-11-20 02:27:56 +0100316 exit_thread_if_finalizing(tstate);
Victor Stinner09532fe2019-05-10 23:39:09 +0200317 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
318 Py_FatalError("PyEval_AcquireThread: non-NULL old thread state");
319 }
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000320}
321
322void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000323PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000324{
Victor Stinner09532fe2019-05-10 23:39:09 +0200325 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200327 }
328
Victor Stinner01b1cc12019-11-20 02:27:56 +0100329 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200330 PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
331 if (new_tstate != tstate) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200333 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200334 drop_gil(&runtime->ceval, tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000335}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000336
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200337/* This function is called from PyOS_AfterFork_Child to destroy all threads
338 * which are not running in the child process, and clear internal locks
339 * which might be held by those threads.
340 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000341
342void
Victor Stinnerd5d9e812019-05-13 12:35:37 +0200343_PyEval_ReInitThreads(_PyRuntimeState *runtime)
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000344{
Victor Stinnere225beb2019-06-03 18:14:24 +0200345 struct _ceval_runtime_state *ceval = &runtime->ceval;
346 if (!gil_created(&ceval->gil)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 return;
Victor Stinner09532fe2019-05-10 23:39:09 +0200348 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200349 recreate_gil(&ceval->gil);
Victor Stinner09532fe2019-05-10 23:39:09 +0200350 PyThreadState *current_tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinnere225beb2019-06-03 18:14:24 +0200351 take_gil(ceval, current_tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700352
Victor Stinnere225beb2019-06-03 18:14:24 +0200353 struct _pending_calls *pending = &ceval->pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200354 pending->lock = PyThread_allocate_lock();
355 if (pending->lock == NULL) {
Eric Snow8479a342019-03-08 23:44:33 -0700356 Py_FatalError("Can't initialize threads for pending calls");
357 }
Jesse Nollera8513972008-07-17 16:49:17 +0000358
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200359 /* Destroy all threads except the current one */
Victor Stinner0fd2c302019-06-04 03:15:09 +0200360 _PyThreadState_DeleteExcept(runtime, current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000361}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000362
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000363/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600364 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000365
366void
Victor Stinnere225beb2019-06-03 18:14:24 +0200367_PyEval_SignalAsyncExc(struct _ceval_runtime_state *ceval)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000368{
Victor Stinnere225beb2019-06-03 18:14:24 +0200369 SIGNAL_ASYNC_EXC(ceval);
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000370}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000371
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000372PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000373PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000374{
Victor Stinner09532fe2019-05-10 23:39:09 +0200375 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200376 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinner09532fe2019-05-10 23:39:09 +0200377 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
378 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 Py_FatalError("PyEval_SaveThread: NULL tstate");
Victor Stinner09532fe2019-05-10 23:39:09 +0200380 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200381 assert(gil_created(&ceval->gil));
382 drop_gil(ceval, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000384}
385
386void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000387PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000388{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100389 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner0fd2c302019-06-04 03:15:09 +0200390 struct _ceval_runtime_state *ceval = &runtime->ceval;
391
Victor Stinner09532fe2019-05-10 23:39:09 +0200392 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Victor Stinner09532fe2019-05-10 23:39:09 +0200394 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200395 assert(gil_created(&ceval->gil));
Victor Stinner2914bb32018-01-29 11:57:45 +0100396
397 int err = errno;
Victor Stinnere225beb2019-06-03 18:14:24 +0200398 take_gil(ceval, tstate);
Victor Stinner01b1cc12019-11-20 02:27:56 +0100399 exit_thread_if_finalizing(tstate);
Victor Stinner2914bb32018-01-29 11:57:45 +0100400 errno = err;
401
Victor Stinner09532fe2019-05-10 23:39:09 +0200402 _PyThreadState_Swap(&runtime->gilstate, tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000403}
404
405
Guido van Rossuma9672091994-09-14 13:31:22 +0000406/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
407 signal handlers or Mac I/O completion routines) can schedule calls
408 to a function to be called synchronously.
409 The synchronous function is called with one void* argument.
410 It should return 0 for success or -1 for failure -- failure should
411 be accompanied by an exception.
412
413 If registry succeeds, the registry function returns 0; if it fails
414 (e.g. due to too many pending calls) it returns -1 (without setting
415 an exception condition).
416
417 Note that because registry may occur from within signal handlers,
418 or other asynchronous events, calling malloc() is unsafe!
419
Guido van Rossuma9672091994-09-14 13:31:22 +0000420 Any thread can schedule pending calls, but only the main thread
421 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000422 There is no facility to schedule calls to a particular thread, but
423 that should be easy to change, should that ever be required. In
424 that case, the static variables here should go into the python
425 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000426*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000427
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200428void
Victor Stinnere225beb2019-06-03 18:14:24 +0200429_PyEval_SignalReceived(struct _ceval_runtime_state *ceval)
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200430{
431 /* bpo-30703: Function called when the C signal handler of Python gets a
432 signal. We cannot queue a callback using Py_AddPendingCall() since
433 that function is not async-signal-safe. */
Victor Stinnere225beb2019-06-03 18:14:24 +0200434 SIGNAL_PENDING_SIGNALS(ceval);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200435}
436
Eric Snow5be45a62019-03-08 22:47:07 -0700437/* Push one item onto the queue while holding the lock. */
438static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200439_push_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600440 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700441{
Eric Snow842a2f02019-03-15 15:47:51 -0600442 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700443 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600444 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700445 return -1; /* Queue full */
446 }
Eric Snow842a2f02019-03-15 15:47:51 -0600447 pending->calls[i].func = func;
448 pending->calls[i].arg = arg;
449 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700450 return 0;
451}
452
453/* Pop one item off the queue while holding the lock. */
454static void
Victor Stinnere225beb2019-06-03 18:14:24 +0200455_pop_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600456 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700457{
Eric Snow842a2f02019-03-15 15:47:51 -0600458 int i = pending->first;
459 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700460 return; /* Queue empty */
461 }
462
Eric Snow842a2f02019-03-15 15:47:51 -0600463 *func = pending->calls[i].func;
464 *arg = pending->calls[i].arg;
465 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700466}
467
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200468/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000469 scheduling to be made from any thread, and even from an executing
470 callback.
471 */
472
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000473int
Victor Stinner438a12d2019-05-24 17:01:38 +0200474_PyEval_AddPendingCall(PyThreadState *tstate,
Victor Stinnere225beb2019-06-03 18:14:24 +0200475 struct _ceval_runtime_state *ceval,
Victor Stinner09532fe2019-05-10 23:39:09 +0200476 int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000477{
Victor Stinnere225beb2019-06-03 18:14:24 +0200478 struct _pending_calls *pending = &ceval->pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600479
480 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
481 if (pending->finishing) {
482 PyThread_release_lock(pending->lock);
483
484 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +0200485 _PyErr_Fetch(tstate, &exc, &val, &tb);
486 _PyErr_SetString(tstate, PyExc_SystemError,
Eric Snow842a2f02019-03-15 15:47:51 -0600487 "Py_AddPendingCall: cannot add pending calls "
488 "(Python shutting down)");
Victor Stinner438a12d2019-05-24 17:01:38 +0200489 _PyErr_Print(tstate);
490 _PyErr_Restore(tstate, exc, val, tb);
Eric Snow842a2f02019-03-15 15:47:51 -0600491 return -1;
492 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200493 int result = _push_pending_call(pending, func, arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600494 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700495
Victor Stinnere225beb2019-06-03 18:14:24 +0200496 /* signal main loop */
497 SIGNAL_PENDING_CALLS(ceval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000499}
500
Victor Stinner09532fe2019-05-10 23:39:09 +0200501int
502Py_AddPendingCall(int (*func)(void *), void *arg)
503{
Victor Stinner438a12d2019-05-24 17:01:38 +0200504 _PyRuntimeState *runtime = &_PyRuntime;
505 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinnere225beb2019-06-03 18:14:24 +0200506 return _PyEval_AddPendingCall(tstate, &runtime->ceval, func, arg);
Victor Stinner09532fe2019-05-10 23:39:09 +0200507}
508
Eric Snowfdf282d2019-01-11 14:26:55 -0700509static int
Victor Stinner09532fe2019-05-10 23:39:09 +0200510handle_signals(_PyRuntimeState *runtime)
Eric Snowfdf282d2019-01-11 14:26:55 -0700511{
Eric Snow5be45a62019-03-08 22:47:07 -0700512 /* Only handle signals on main thread. PyEval_InitThreads must
513 * have been called already.
514 */
Victor Stinner09532fe2019-05-10 23:39:09 +0200515 if (PyThread_get_thread_ident() != runtime->main_thread) {
Eric Snowfdf282d2019-01-11 14:26:55 -0700516 return 0;
517 }
Eric Snow64d6cc82019-02-23 15:40:43 -0700518 /*
519 * Ensure that the thread isn't currently running some other
520 * interpreter.
521 */
Victor Stinner09532fe2019-05-10 23:39:09 +0200522 PyInterpreterState *interp = _PyRuntimeState_GetThreadState(runtime)->interp;
523 if (interp != runtime->interpreters.main) {
Eric Snow64d6cc82019-02-23 15:40:43 -0700524 return 0;
525 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700526
Victor Stinnere225beb2019-06-03 18:14:24 +0200527 struct _ceval_runtime_state *ceval = &runtime->ceval;
528 UNSIGNAL_PENDING_SIGNALS(ceval);
Eric Snow64d6cc82019-02-23 15:40:43 -0700529 if (_PyErr_CheckSignals() < 0) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200530 SIGNAL_PENDING_SIGNALS(ceval); /* We're not done yet */
Eric Snowfdf282d2019-01-11 14:26:55 -0700531 return -1;
532 }
533 return 0;
534}
535
536static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200537make_pending_calls(_PyRuntimeState *runtime)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000538{
Eric Snow6a150bc2019-06-01 15:39:46 -0600539 static int busy = 0;
Eric Snowb75b1a352019-04-12 10:20:10 -0600540
Victor Stinnere225beb2019-06-03 18:14:24 +0200541 /* only service pending calls on main thread */
542 if (PyThread_get_thread_ident() != runtime->main_thread) {
543 return 0;
544 }
545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 /* don't perform recursive pending calls */
Eric Snowfdf282d2019-01-11 14:26:55 -0700547 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700549 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200550 busy = 1;
Victor Stinnere225beb2019-06-03 18:14:24 +0200551 struct _ceval_runtime_state *ceval = &runtime->ceval;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200552 /* unsignal before starting to call callbacks, so that any callback
553 added in-between re-signals */
Victor Stinnere225beb2019-06-03 18:14:24 +0200554 UNSIGNAL_PENDING_CALLS(ceval);
Eric Snowfdf282d2019-01-11 14:26:55 -0700555 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 /* perform a bounded number of calls, in case of recursion */
Victor Stinnere225beb2019-06-03 18:14:24 +0200558 struct _pending_calls *pending = &ceval->pending;
Eric Snowfdf282d2019-01-11 14:26:55 -0700559 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700560 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561 void *arg = NULL;
562
563 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600564 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200565 _pop_pending_call(pending, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600566 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700567
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100568 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700569 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100570 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700571 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700572 res = func(arg);
573 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200574 goto error;
575 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200577
Charles-François Natalif23339a2011-07-23 18:15:43 +0200578 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700579 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200580
581error:
582 busy = 0;
Victor Stinnere225beb2019-06-03 18:14:24 +0200583 SIGNAL_PENDING_CALLS(ceval);
Eric Snowfdf282d2019-01-11 14:26:55 -0700584 return res;
585}
586
Eric Snow842a2f02019-03-15 15:47:51 -0600587void
Victor Stinnere225beb2019-06-03 18:14:24 +0200588_Py_FinishPendingCalls(_PyRuntimeState *runtime)
Eric Snow842a2f02019-03-15 15:47:51 -0600589{
Eric Snow842a2f02019-03-15 15:47:51 -0600590 assert(PyGILState_Check());
591
Victor Stinnere225beb2019-06-03 18:14:24 +0200592 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
593 struct _pending_calls *pending = &runtime->ceval.pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200594
Eric Snow842a2f02019-03-15 15:47:51 -0600595 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
596 pending->finishing = 1;
597 PyThread_release_lock(pending->lock);
598
599 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
600 return;
601 }
602
Victor Stinnere225beb2019-06-03 18:14:24 +0200603 if (make_pending_calls(runtime) < 0) {
604 PyObject *exc, *val, *tb;
605 _PyErr_Fetch(tstate, &exc, &val, &tb);
606 PyErr_BadInternalCall();
607 _PyErr_ChainExceptions(exc, val, tb);
608 _PyErr_Print(tstate);
Eric Snow842a2f02019-03-15 15:47:51 -0600609 }
610}
611
Eric Snowfdf282d2019-01-11 14:26:55 -0700612/* Py_MakePendingCalls() is a simple wrapper for the sake
613 of backward-compatibility. */
614int
615Py_MakePendingCalls(void)
616{
617 assert(PyGILState_Check());
618
619 /* Python signal handler doesn't really queue a callback: it only signals
620 that a signal was received, see _PyEval_SignalReceived(). */
Victor Stinner09532fe2019-05-10 23:39:09 +0200621 _PyRuntimeState *runtime = &_PyRuntime;
622 int res = handle_signals(runtime);
Eric Snowfdf282d2019-01-11 14:26:55 -0700623 if (res != 0) {
624 return res;
625 }
626
Victor Stinnere225beb2019-06-03 18:14:24 +0200627 res = make_pending_calls(runtime);
Eric Snowb75b1a352019-04-12 10:20:10 -0600628 if (res != 0) {
629 return res;
630 }
631
632 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000633}
634
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000635/* The interpreter's recursion limit */
636
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000637#ifndef Py_DEFAULT_RECURSION_LIMIT
638#define Py_DEFAULT_RECURSION_LIMIT 1000
639#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600640
Eric Snow05351c12017-09-05 21:43:08 -0700641int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000642
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600643void
Victor Stinnere225beb2019-06-03 18:14:24 +0200644_PyEval_Initialize(struct _ceval_runtime_state *state)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600645{
Victor Stinnere225beb2019-06-03 18:14:24 +0200646 state->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600647 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Victor Stinnere225beb2019-06-03 18:14:24 +0200648 _gil_initialize(&state->gil);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600649}
650
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000651int
652Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000653{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100654 struct _ceval_runtime_state *ceval = &_PyRuntime.ceval;
655 return ceval->recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000656}
657
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000658void
659Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000660{
Victor Stinnere225beb2019-06-03 18:14:24 +0200661 struct _ceval_runtime_state *ceval = &_PyRuntime.ceval;
662 ceval->recursion_limit = new_limit;
663 _Py_CheckRecursionLimit = ceval->recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000664}
665
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100666/* The function _Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
Armin Rigo2b3eb402003-10-28 12:05:48 +0000667 if the recursion_depth reaches _Py_CheckRecursionLimit.
668 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
669 to guarantee that _Py_CheckRecursiveCall() is regularly called.
670 Without USE_STACKCHECK, there is no need for this. */
671int
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100672_Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000673{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100674 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200675 int recursion_limit = runtime->ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000676
677#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700678 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 if (PyOS_CheckStack()) {
680 --tstate->recursion_depth;
Victor Stinner438a12d2019-05-24 17:01:38 +0200681 _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 return -1;
683 }
pdox18967932017-10-25 23:03:01 -0700684 /* Needed for ABI backwards-compatibility (see bpo-31857) */
Eric Snow05351c12017-09-05 21:43:08 -0700685 _Py_CheckRecursionLimit = recursion_limit;
pdox18967932017-10-25 23:03:01 -0700686#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000687 if (tstate->recursion_critical)
688 /* Somebody asked that we don't check for recursion. */
689 return 0;
690 if (tstate->overflowed) {
691 if (tstate->recursion_depth > recursion_limit + 50) {
692 /* Overflowing while handling an overflow. Give up. */
693 Py_FatalError("Cannot recover from stack overflow.");
694 }
695 return 0;
696 }
697 if (tstate->recursion_depth > recursion_limit) {
698 --tstate->recursion_depth;
699 tstate->overflowed = 1;
Victor Stinner438a12d2019-05-24 17:01:38 +0200700 _PyErr_Format(tstate, PyExc_RecursionError,
701 "maximum recursion depth exceeded%s",
702 where);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 return -1;
704 }
705 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000706}
707
Victor Stinner09532fe2019-05-10 23:39:09 +0200708static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
Victor Stinner438a12d2019-05-24 17:01:38 +0200709static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000710
Victor Stinnere225beb2019-06-03 18:14:24 +0200711#define _Py_TracingPossible(ceval) ((ceval)->tracing_possible)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000712
Guido van Rossum374a9221991-04-04 10:40:29 +0000713
Guido van Rossumb209a111997-04-29 18:18:01 +0000714PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000715PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000716{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 return PyEval_EvalCodeEx(co,
718 globals, locals,
719 (PyObject **)NULL, 0,
720 (PyObject **)NULL, 0,
721 (PyObject **)NULL, 0,
722 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000723}
724
725
726/* Interpreter main loop */
727
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000728PyObject *
Victor Stinnerb9e68122019-11-14 12:20:46 +0100729PyEval_EvalFrame(PyFrameObject *f)
730{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 /* This is for backward compatibility with extension modules that
732 used this API; core interpreter code should call
733 PyEval_EvalFrameEx() */
Victor Stinnerb9e68122019-11-14 12:20:46 +0100734 PyThreadState *tstate = _PyThreadState_GET();
735 return _PyEval_EvalFrame(tstate, f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000736}
737
738PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000739PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000740{
Victor Stinnerb9e68122019-11-14 12:20:46 +0100741 PyThreadState *tstate = _PyThreadState_GET();
742 return _PyEval_EvalFrame(tstate, f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700743}
744
Victor Stinnerc6944e72016-11-11 02:13:35 +0100745PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700746_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
747{
Guido van Rossum950361c1997-01-24 13:49:28 +0000748#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000750#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200751 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300752 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200753 int opcode; /* Current opcode */
754 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200755 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 PyObject *retval = NULL; /* Return value */
Victor Stinner09532fe2019-05-10 23:39:09 +0200757 _PyRuntimeState * const runtime = &_PyRuntime;
758 PyThreadState * const tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinnere225beb2019-06-03 18:14:24 +0200759 struct _ceval_runtime_state * const ceval = &runtime->ceval;
760 _Py_atomic_int * const eval_breaker = &ceval->eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000766
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 is true when the line being executed has changed. The
768 initial values are such as to make this false the first
769 time it is tested. */
770 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000771
Serhiy Storchakaab874002016-09-11 13:48:15 +0300772 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 PyObject *names;
774 PyObject *consts;
Inada Naoki91234a12019-06-03 21:30:58 +0900775 _PyOpcache *co_opcache;
Guido van Rossum374a9221991-04-04 10:40:29 +0000776
Brett Cannon368b4b72012-04-02 12:17:59 -0400777#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200778 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400779#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200780
Antoine Pitroub52ec782009-01-25 16:34:23 +0000781/* Computed GOTOs, or
782 the-optimization-commonly-but-improperly-known-as-"threaded code"
783 using gcc's labels-as-values extension
784 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
785
786 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000788 combined with a lookup table of jump addresses. However, since the
789 indirect jump instruction is shared by all opcodes, the CPU will have a
790 hard time making the right prediction for where to jump next (actually,
791 it will be always wrong except in the uncommon case of a sequence of
792 several identical opcodes).
793
794 "Threaded code" in contrast, uses an explicit jump table and an explicit
795 indirect jump instruction at the end of each opcode. Since the jump
796 instruction is at a different address for each opcode, the CPU will make a
797 separate prediction for each of these instructions, which is equivalent to
798 predicting the second opcode of each opcode pair. These predictions have
799 a much better chance to turn out valid, especially in small bytecode loops.
800
801 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000803 and potentially many more instructions (depending on the pipeline width).
804 A correctly predicted branch, however, is nearly free.
805
806 At the time of this writing, the "threaded code" version is up to 15-20%
807 faster than the normal "switch" version, depending on the compiler and the
808 CPU architecture.
809
810 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
811 because it would render the measurements invalid.
812
813
814 NOTE: care must be taken that the compiler doesn't try to "optimize" the
815 indirect jumps by sharing them between all opcodes. Such optimizations
816 can be disabled on gcc by using the -fno-gcse flag (or possibly
817 -fno-crossjumping).
818*/
819
Antoine Pitrou042b1282010-08-13 21:15:58 +0000820#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000821#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000822#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000823#endif
824
Antoine Pitrou042b1282010-08-13 21:15:58 +0000825#ifdef HAVE_COMPUTED_GOTOS
826 #ifndef USE_COMPUTED_GOTOS
827 #define USE_COMPUTED_GOTOS 1
828 #endif
829#else
830 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
831 #error "Computed gotos are not supported on this compiler."
832 #endif
833 #undef USE_COMPUTED_GOTOS
834 #define USE_COMPUTED_GOTOS 0
835#endif
836
837#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000838/* Import the static jump table */
839#include "opcode_targets.h"
840
Antoine Pitroub52ec782009-01-25 16:34:23 +0000841#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -0700842 op: \
843 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000844
Antoine Pitroub52ec782009-01-25 16:34:23 +0000845#ifdef LLTRACE
846#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200848 if (!lltrace && !_Py_TracingPossible(ceval) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300850 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300851 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 } \
853 goto fast_next_opcode; \
854 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000855#else
856#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200858 if (!_Py_TracingPossible(ceval) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300860 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300861 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 } \
863 goto fast_next_opcode; \
864 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000865#endif
866
Victor Stinner09532fe2019-05-10 23:39:09 +0200867#define DISPATCH() \
868 { \
869 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
870 FAST_DISPATCH(); \
871 } \
872 continue; \
873 }
874
Antoine Pitroub52ec782009-01-25 16:34:23 +0000875#else
Benjamin Petersonddd19492018-09-16 22:38:02 -0700876#define TARGET(op) op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000877#define FAST_DISPATCH() goto fast_next_opcode
Victor Stinner09532fe2019-05-10 23:39:09 +0200878#define DISPATCH() continue
Antoine Pitroub52ec782009-01-25 16:34:23 +0000879#endif
880
881
Neal Norwitza81d2202002-07-14 00:27:26 +0000882/* Tuple access macros */
883
884#ifndef Py_DEBUG
885#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
886#else
887#define GETITEM(v, i) PyTuple_GetItem((v), (i))
888#endif
889
Guido van Rossum374a9221991-04-04 10:40:29 +0000890/* Code access macros */
891
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300892/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600893#define INSTR_OFFSET() \
894 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300895#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300896 _Py_CODEUNIT word = *next_instr; \
897 opcode = _Py_OPCODE(word); \
898 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300899 next_instr++; \
900 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300901#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
902#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000903
Raymond Hettingerf606f872003-03-16 03:11:04 +0000904/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 Some opcodes tend to come in pairs thus making it possible to
906 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300907 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 Verifying the prediction costs a single high-speed test of a register
910 variable against a constant. If the pairing was good, then the
911 processor's own internal branch predication has a high likelihood of
912 success, resulting in a nearly zero-overhead transition to the
913 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300914 including its unpredictable switch-case branch. Combined with the
915 processor's internal branch prediction, a successful PREDICT has the
916 effect of making the two opcodes run as if they were a single new opcode
917 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000918
Georg Brandl86b2fb92008-07-16 03:43:04 +0000919 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 predictions turned-on and interpret the results as if some opcodes
921 had been combined or turn-off predictions so that the opcode frequency
922 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000923
924 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 the CPU to record separate branch prediction information for each
926 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000927
Raymond Hettingerf606f872003-03-16 03:11:04 +0000928*/
929
Antoine Pitrou042b1282010-08-13 21:15:58 +0000930#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000932#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300933#define PREDICT(op) \
934 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300935 _Py_CODEUNIT word = *next_instr; \
936 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300937 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300938 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300939 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300940 goto PRED_##op; \
941 } \
942 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000943#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300944#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000945
Raymond Hettingerf606f872003-03-16 03:11:04 +0000946
Guido van Rossum374a9221991-04-04 10:40:29 +0000947/* Stack manipulation macros */
948
Martin v. Löwis18e16552006-02-15 17:27:45 +0000949/* The stack can grow at most MAXINT deep, as co_nlocals and
950 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000951#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
952#define EMPTY() (STACK_LEVEL() == 0)
953#define TOP() (stack_pointer[-1])
954#define SECOND() (stack_pointer[-2])
955#define THIRD() (stack_pointer[-3])
956#define FOURTH() (stack_pointer[-4])
957#define PEEK(n) (stack_pointer[-(n)])
958#define SET_TOP(v) (stack_pointer[-1] = (v))
959#define SET_SECOND(v) (stack_pointer[-2] = (v))
960#define SET_THIRD(v) (stack_pointer[-3] = (v))
961#define SET_FOURTH(v) (stack_pointer[-4] = (v))
962#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
963#define BASIC_STACKADJ(n) (stack_pointer += n)
964#define BASIC_PUSH(v) (*stack_pointer++ = (v))
965#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000966
Guido van Rossum96a42c81992-01-12 02:29:51 +0000967#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +0200969 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000970 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +0200971#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000972 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +0000973#define STACK_GROW(n) do { \
974 assert(n >= 0); \
975 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +0200976 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +0000977 assert(STACK_LEVEL() <= co->co_stacksize); \
978 } while (0)
979#define STACK_SHRINK(n) do { \
980 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +0200981 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +0000982 (void)(BASIC_STACKADJ(-n)); \
983 assert(STACK_LEVEL() <= co->co_stacksize); \
984 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +0000985#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +0200986 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000987 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000988#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000989#define PUSH(v) BASIC_PUSH(v)
990#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +0000991#define STACK_GROW(n) BASIC_STACKADJ(n)
992#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000993#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000994#endif
995
Guido van Rossum681d79a1995-07-18 14:51:37 +0000996/* Local variable macros */
997
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000999
1000/* The SETLOCAL() macro must not DECREF the local variable in-place and
1001 then store the new value; it must copy the old value to a temporary
1002 value, then store the new value, and then DECREF the temporary value.
1003 This is because it is possible that during the DECREF the frame is
1004 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1005 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001007 GETLOCAL(i) = value; \
1008 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001009
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001010
1011#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 while (STACK_LEVEL() > (b)->b_level) { \
1013 PyObject *v = POP(); \
1014 Py_XDECREF(v); \
1015 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001016
1017#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001018 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +01001020 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1022 while (STACK_LEVEL() > (b)->b_level + 3) { \
1023 value = POP(); \
1024 Py_XDECREF(value); \
1025 } \
Mark Shannonae3087c2017-10-22 22:41:51 +01001026 exc_info = tstate->exc_info; \
1027 type = exc_info->exc_type; \
1028 value = exc_info->exc_value; \
1029 traceback = exc_info->exc_traceback; \
1030 exc_info->exc_type = POP(); \
1031 exc_info->exc_value = POP(); \
1032 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 Py_XDECREF(type); \
1034 Py_XDECREF(value); \
1035 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001036 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001037
Inada Naoki91234a12019-06-03 21:30:58 +09001038 /* macros for opcode cache */
1039#define OPCACHE_CHECK() \
1040 do { \
1041 co_opcache = NULL; \
1042 if (co->co_opcache != NULL) { \
1043 unsigned char co_opt_offset = \
1044 co->co_opcache_map[next_instr - first_instr]; \
1045 if (co_opt_offset > 0) { \
1046 assert(co_opt_offset <= co->co_opcache_size); \
1047 co_opcache = &co->co_opcache[co_opt_offset - 1]; \
1048 assert(co_opcache != NULL); \
Inada Naoki91234a12019-06-03 21:30:58 +09001049 } \
1050 } \
1051 } while (0)
1052
1053#if OPCACHE_STATS
1054
1055#define OPCACHE_STAT_GLOBAL_HIT() \
1056 do { \
1057 if (co->co_opcache != NULL) opcache_global_hits++; \
1058 } while (0)
1059
1060#define OPCACHE_STAT_GLOBAL_MISS() \
1061 do { \
1062 if (co->co_opcache != NULL) opcache_global_misses++; \
1063 } while (0)
1064
1065#define OPCACHE_STAT_GLOBAL_OPT() \
1066 do { \
1067 if (co->co_opcache != NULL) opcache_global_opts++; \
1068 } while (0)
1069
1070#else /* OPCACHE_STATS */
1071
1072#define OPCACHE_STAT_GLOBAL_HIT()
1073#define OPCACHE_STAT_GLOBAL_MISS()
1074#define OPCACHE_STAT_GLOBAL_OPT()
1075
1076#endif
1077
Guido van Rossuma027efa1997-05-05 20:56:21 +00001078/* Start of code */
1079
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 /* push frame */
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001081 if (_Py_EnterRecursiveCall(tstate, "")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001083 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001086
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 if (tstate->use_tracing) {
1088 if (tstate->c_tracefunc != NULL) {
1089 /* tstate->c_tracefunc, if defined, is a
1090 function that will be called on *every* entry
1091 to a code block. Its return value, if not
1092 None, is a function that will be called at
1093 the start of each executed line of code.
1094 (Actually, the function must return itself
1095 in order to continue tracing.) The trace
1096 functions are called with three arguments:
1097 a pointer to the current frame, a string
1098 indicating why the function is called, and
1099 an argument which depends on the situation.
1100 The global trace function is also called
1101 whenever an exception is detected. */
1102 if (call_trace_protected(tstate->c_tracefunc,
1103 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001104 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 /* Trace function raised an error */
1106 goto exit_eval_frame;
1107 }
1108 }
1109 if (tstate->c_profilefunc != NULL) {
1110 /* Similar for c_profilefunc, except it needn't
1111 return itself and isn't called for "line" events */
1112 if (call_trace_protected(tstate->c_profilefunc,
1113 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001114 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 /* Profile function raised an error */
1116 goto exit_eval_frame;
1117 }
1118 }
1119 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001120
Łukasz Langaa785c872016-09-09 17:37:37 -07001121 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1122 dtrace_function_entry(f);
1123
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 co = f->f_code;
1125 names = co->co_names;
1126 consts = co->co_consts;
1127 fastlocals = f->f_localsplus;
1128 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001129 assert(PyBytes_Check(co->co_code));
1130 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001131 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1132 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1133 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001134 /*
1135 f->f_lasti refers to the index of the last instruction,
1136 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001137
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001138 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001139 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001140
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001141 When the PREDICT() macros are enabled, some opcode pairs follow in
1142 direct succession without updating f->f_lasti. A successful
1143 prediction effectively links the two codes together as if they
1144 were a single new opcode; accordingly,f->f_lasti will point to
1145 the first code in the pair (for instance, GET_ITER followed by
1146 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001147 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001149 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001150 next_instr = first_instr;
1151 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001152 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1153 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001154 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 stack_pointer = f->f_stacktop;
1156 assert(stack_pointer != NULL);
1157 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001158 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001159
Inada Naoki91234a12019-06-03 21:30:58 +09001160 if (co->co_opcache_flag < OPCACHE_MIN_RUNS) {
1161 co->co_opcache_flag++;
1162 if (co->co_opcache_flag == OPCACHE_MIN_RUNS) {
1163 if (_PyCode_InitOpcache(co) < 0) {
1164 return NULL;
1165 }
1166#if OPCACHE_STATS
1167 opcache_code_objects_extra_mem +=
1168 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1169 sizeof(_PyOpcache) * co->co_opcache_size;
1170 opcache_code_objects++;
1171#endif
1172 }
1173 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001174
Tim Peters5ca576e2001-06-18 22:08:13 +00001175#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001176 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001177#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001178
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001179 if (throwflag) /* support for generator.throw() */
1180 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001181
Victor Stinnerace47d72013-07-18 01:41:08 +02001182#ifdef Py_DEBUG
1183 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001184 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001185 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001186 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001187#endif
1188
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001189main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1192 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001193 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 /* Do periodic things. Doing this every time through
1196 the loop would add too much overhead, so we do it
1197 only every Nth instruction. We also do it if
1198 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1199 event needs attention (e.g. a signal handler or
1200 async I/O handler); see Py_AddPendingCall() and
1201 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001202
Eric Snow7bda9de2019-03-08 17:25:54 -07001203 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001204 opcode = _Py_OPCODE(*next_instr);
1205 if (opcode == SETUP_FINALLY ||
1206 opcode == SETUP_WITH ||
1207 opcode == BEFORE_ASYNC_WITH ||
1208 opcode == YIELD_FROM) {
1209 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001210 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001211 - If we're about to enter the 'with:'. It will prevent
1212 emitting a resource warning in the common idiom
1213 'with open(path) as file:'.
1214 - If we're about to enter the 'async with:'.
1215 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001216 *very* useful, but might help in some cases and it's
1217 traditional)
1218 - If we're resuming a chain of nested 'yield from' or
1219 'await' calls, then each frame is parked with YIELD_FROM
1220 as its next opcode. If the user hit control-C we want to
1221 wait until we've reached the innermost frame before
1222 running the signal handler and raising KeyboardInterrupt
1223 (see bpo-30039).
1224 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 goto fast_next_opcode;
1226 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001227
Victor Stinnere225beb2019-06-03 18:14:24 +02001228 if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
Victor Stinner09532fe2019-05-10 23:39:09 +02001229 if (handle_signals(runtime) != 0) {
Eric Snowfdf282d2019-01-11 14:26:55 -07001230 goto error;
1231 }
1232 }
Victor Stinnere225beb2019-06-03 18:14:24 +02001233 if (_Py_atomic_load_relaxed(&ceval->pending.calls_to_do)) {
1234 if (make_pending_calls(runtime) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001235 goto error;
Eric Snowfdf282d2019-01-11 14:26:55 -07001236 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001238
Victor Stinnere225beb2019-06-03 18:14:24 +02001239 if (_Py_atomic_load_relaxed(&ceval->gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 /* Give another thread a chance */
Victor Stinner09532fe2019-05-10 23:39:09 +02001241 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 Py_FatalError("ceval: tstate mix-up");
Victor Stinner09532fe2019-05-10 23:39:09 +02001243 }
Victor Stinnere225beb2019-06-03 18:14:24 +02001244 drop_gil(ceval, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245
1246 /* Other threads may run now */
1247
Victor Stinnere225beb2019-06-03 18:14:24 +02001248 take_gil(ceval, tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001249
1250 /* Check if we should make a quick exit. */
Victor Stinner01b1cc12019-11-20 02:27:56 +01001251 exit_thread_if_finalizing(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001252
Victor Stinner09532fe2019-05-10 23:39:09 +02001253 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 Py_FatalError("ceval: orphan tstate");
Victor Stinner09532fe2019-05-10 23:39:09 +02001255 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 }
1257 /* Check for asynchronous exceptions. */
1258 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001259 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 tstate->async_exc = NULL;
Victor Stinnere225beb2019-06-03 18:14:24 +02001261 UNSIGNAL_ASYNC_EXC(ceval);
Victor Stinner438a12d2019-05-24 17:01:38 +02001262 _PyErr_SetNone(tstate, exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001263 Py_DECREF(exc);
1264 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 }
1266 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 fast_next_opcode:
1269 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001270
Łukasz Langaa785c872016-09-09 17:37:37 -07001271 if (PyDTrace_LINE_ENABLED())
1272 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001275
Victor Stinnere225beb2019-06-03 18:14:24 +02001276 if (_Py_TracingPossible(ceval) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001277 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001278 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 /* see maybe_call_line_trace
1280 for expository comments */
1281 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 err = maybe_call_line_trace(tstate->c_tracefunc,
1284 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001285 tstate, f,
1286 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 /* Reload possibly changed frame fields */
1288 JUMPTO(f->f_lasti);
1289 if (f->f_stacktop != NULL) {
1290 stack_pointer = f->f_stacktop;
1291 f->f_stacktop = NULL;
1292 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001293 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001295 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001299
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001300 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001301 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001302#ifdef DYNAMIC_EXECUTION_PROFILE
1303#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 dxpairs[lastopcode][opcode]++;
1305 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001306#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001308#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001309
Guido van Rossum96a42c81992-01-12 02:29:51 +00001310#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 if (lltrace) {
1314 if (HAS_ARG(opcode)) {
1315 printf("%d: %d, %d\n",
1316 f->f_lasti, opcode, oparg);
1317 }
1318 else {
1319 printf("%d: %d\n",
1320 f->f_lasti, opcode);
1321 }
1322 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001323#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001328 It is essential that any operation that fails must goto error
1329 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001330
Benjamin Petersonddd19492018-09-16 22:38:02 -07001331 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001333 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001334
Benjamin Petersonddd19492018-09-16 22:38:02 -07001335 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001336 PyObject *value = GETLOCAL(oparg);
1337 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001338 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001339 UNBOUNDLOCAL_ERROR_MSG,
1340 PyTuple_GetItem(co->co_varnames, oparg));
1341 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001343 Py_INCREF(value);
1344 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001346 }
1347
Benjamin Petersonddd19492018-09-16 22:38:02 -07001348 case TARGET(LOAD_CONST): {
1349 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001350 PyObject *value = GETITEM(consts, oparg);
1351 Py_INCREF(value);
1352 PUSH(value);
1353 FAST_DISPATCH();
1354 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001355
Benjamin Petersonddd19492018-09-16 22:38:02 -07001356 case TARGET(STORE_FAST): {
1357 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001358 PyObject *value = POP();
1359 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001361 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001362
Benjamin Petersonddd19492018-09-16 22:38:02 -07001363 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001364 PyObject *value = POP();
1365 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001367 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001368
Benjamin Petersonddd19492018-09-16 22:38:02 -07001369 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001370 PyObject *top = TOP();
1371 PyObject *second = SECOND();
1372 SET_TOP(second);
1373 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001375 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001376
Benjamin Petersonddd19492018-09-16 22:38:02 -07001377 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001378 PyObject *top = TOP();
1379 PyObject *second = SECOND();
1380 PyObject *third = THIRD();
1381 SET_TOP(second);
1382 SET_SECOND(third);
1383 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001385 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001386
Benjamin Petersonddd19492018-09-16 22:38:02 -07001387 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001388 PyObject *top = TOP();
1389 PyObject *second = SECOND();
1390 PyObject *third = THIRD();
1391 PyObject *fourth = FOURTH();
1392 SET_TOP(second);
1393 SET_SECOND(third);
1394 SET_THIRD(fourth);
1395 SET_FOURTH(top);
1396 FAST_DISPATCH();
1397 }
1398
Benjamin Petersonddd19492018-09-16 22:38:02 -07001399 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001400 PyObject *top = TOP();
1401 Py_INCREF(top);
1402 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001404 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001405
Benjamin Petersonddd19492018-09-16 22:38:02 -07001406 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001407 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001408 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001409 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001410 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001411 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001412 SET_TOP(top);
1413 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001414 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001415 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001416
Benjamin Petersonddd19492018-09-16 22:38:02 -07001417 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001418 PyObject *value = TOP();
1419 PyObject *res = PyNumber_Positive(value);
1420 Py_DECREF(value);
1421 SET_TOP(res);
1422 if (res == NULL)
1423 goto error;
1424 DISPATCH();
1425 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001426
Benjamin Petersonddd19492018-09-16 22:38:02 -07001427 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001428 PyObject *value = TOP();
1429 PyObject *res = PyNumber_Negative(value);
1430 Py_DECREF(value);
1431 SET_TOP(res);
1432 if (res == NULL)
1433 goto error;
1434 DISPATCH();
1435 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001436
Benjamin Petersonddd19492018-09-16 22:38:02 -07001437 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001438 PyObject *value = TOP();
1439 int err = PyObject_IsTrue(value);
1440 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001441 if (err == 0) {
1442 Py_INCREF(Py_True);
1443 SET_TOP(Py_True);
1444 DISPATCH();
1445 }
1446 else if (err > 0) {
1447 Py_INCREF(Py_False);
1448 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 DISPATCH();
1450 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001451 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001452 goto error;
1453 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001454
Benjamin Petersonddd19492018-09-16 22:38:02 -07001455 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001456 PyObject *value = TOP();
1457 PyObject *res = PyNumber_Invert(value);
1458 Py_DECREF(value);
1459 SET_TOP(res);
1460 if (res == NULL)
1461 goto error;
1462 DISPATCH();
1463 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001464
Benjamin Petersonddd19492018-09-16 22:38:02 -07001465 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001466 PyObject *exp = POP();
1467 PyObject *base = TOP();
1468 PyObject *res = PyNumber_Power(base, exp, Py_None);
1469 Py_DECREF(base);
1470 Py_DECREF(exp);
1471 SET_TOP(res);
1472 if (res == NULL)
1473 goto error;
1474 DISPATCH();
1475 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001476
Benjamin Petersonddd19492018-09-16 22:38:02 -07001477 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001478 PyObject *right = POP();
1479 PyObject *left = TOP();
1480 PyObject *res = PyNumber_Multiply(left, right);
1481 Py_DECREF(left);
1482 Py_DECREF(right);
1483 SET_TOP(res);
1484 if (res == NULL)
1485 goto error;
1486 DISPATCH();
1487 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001488
Benjamin Petersonddd19492018-09-16 22:38:02 -07001489 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001490 PyObject *right = POP();
1491 PyObject *left = TOP();
1492 PyObject *res = PyNumber_MatrixMultiply(left, right);
1493 Py_DECREF(left);
1494 Py_DECREF(right);
1495 SET_TOP(res);
1496 if (res == NULL)
1497 goto error;
1498 DISPATCH();
1499 }
1500
Benjamin Petersonddd19492018-09-16 22:38:02 -07001501 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001502 PyObject *divisor = POP();
1503 PyObject *dividend = TOP();
1504 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1505 Py_DECREF(dividend);
1506 Py_DECREF(divisor);
1507 SET_TOP(quotient);
1508 if (quotient == NULL)
1509 goto error;
1510 DISPATCH();
1511 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001512
Benjamin Petersonddd19492018-09-16 22:38:02 -07001513 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001514 PyObject *divisor = POP();
1515 PyObject *dividend = TOP();
1516 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1517 Py_DECREF(dividend);
1518 Py_DECREF(divisor);
1519 SET_TOP(quotient);
1520 if (quotient == NULL)
1521 goto error;
1522 DISPATCH();
1523 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001524
Benjamin Petersonddd19492018-09-16 22:38:02 -07001525 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001526 PyObject *divisor = POP();
1527 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001528 PyObject *res;
1529 if (PyUnicode_CheckExact(dividend) && (
1530 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1531 // fast path; string formatting, but not if the RHS is a str subclass
1532 // (see issue28598)
1533 res = PyUnicode_Format(dividend, divisor);
1534 } else {
1535 res = PyNumber_Remainder(dividend, divisor);
1536 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001537 Py_DECREF(divisor);
1538 Py_DECREF(dividend);
1539 SET_TOP(res);
1540 if (res == NULL)
1541 goto error;
1542 DISPATCH();
1543 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001544
Benjamin Petersonddd19492018-09-16 22:38:02 -07001545 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001546 PyObject *right = POP();
1547 PyObject *left = TOP();
1548 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001549 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1550 CPython using bytecode, it is simply worthless.
1551 See http://bugs.python.org/issue21955 and
1552 http://bugs.python.org/issue10044 for the discussion. In short,
1553 no patch shown any impact on a realistic benchmark, only a minor
1554 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001555 if (PyUnicode_CheckExact(left) &&
1556 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001557 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001558 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001559 }
1560 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001561 sum = PyNumber_Add(left, right);
1562 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001563 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001564 Py_DECREF(right);
1565 SET_TOP(sum);
1566 if (sum == NULL)
1567 goto error;
1568 DISPATCH();
1569 }
1570
Benjamin Petersonddd19492018-09-16 22:38:02 -07001571 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001572 PyObject *right = POP();
1573 PyObject *left = TOP();
1574 PyObject *diff = PyNumber_Subtract(left, right);
1575 Py_DECREF(right);
1576 Py_DECREF(left);
1577 SET_TOP(diff);
1578 if (diff == NULL)
1579 goto error;
1580 DISPATCH();
1581 }
1582
Benjamin Petersonddd19492018-09-16 22:38:02 -07001583 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001584 PyObject *sub = POP();
1585 PyObject *container = TOP();
1586 PyObject *res = PyObject_GetItem(container, sub);
1587 Py_DECREF(container);
1588 Py_DECREF(sub);
1589 SET_TOP(res);
1590 if (res == NULL)
1591 goto error;
1592 DISPATCH();
1593 }
1594
Benjamin Petersonddd19492018-09-16 22:38:02 -07001595 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001596 PyObject *right = POP();
1597 PyObject *left = TOP();
1598 PyObject *res = PyNumber_Lshift(left, right);
1599 Py_DECREF(left);
1600 Py_DECREF(right);
1601 SET_TOP(res);
1602 if (res == NULL)
1603 goto error;
1604 DISPATCH();
1605 }
1606
Benjamin Petersonddd19492018-09-16 22:38:02 -07001607 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001608 PyObject *right = POP();
1609 PyObject *left = TOP();
1610 PyObject *res = PyNumber_Rshift(left, right);
1611 Py_DECREF(left);
1612 Py_DECREF(right);
1613 SET_TOP(res);
1614 if (res == NULL)
1615 goto error;
1616 DISPATCH();
1617 }
1618
Benjamin Petersonddd19492018-09-16 22:38:02 -07001619 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001620 PyObject *right = POP();
1621 PyObject *left = TOP();
1622 PyObject *res = PyNumber_And(left, right);
1623 Py_DECREF(left);
1624 Py_DECREF(right);
1625 SET_TOP(res);
1626 if (res == NULL)
1627 goto error;
1628 DISPATCH();
1629 }
1630
Benjamin Petersonddd19492018-09-16 22:38:02 -07001631 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001632 PyObject *right = POP();
1633 PyObject *left = TOP();
1634 PyObject *res = PyNumber_Xor(left, right);
1635 Py_DECREF(left);
1636 Py_DECREF(right);
1637 SET_TOP(res);
1638 if (res == NULL)
1639 goto error;
1640 DISPATCH();
1641 }
1642
Benjamin Petersonddd19492018-09-16 22:38:02 -07001643 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001644 PyObject *right = POP();
1645 PyObject *left = TOP();
1646 PyObject *res = PyNumber_Or(left, right);
1647 Py_DECREF(left);
1648 Py_DECREF(right);
1649 SET_TOP(res);
1650 if (res == NULL)
1651 goto error;
1652 DISPATCH();
1653 }
1654
Benjamin Petersonddd19492018-09-16 22:38:02 -07001655 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001656 PyObject *v = POP();
1657 PyObject *list = PEEK(oparg);
1658 int err;
1659 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001661 if (err != 0)
1662 goto error;
1663 PREDICT(JUMP_ABSOLUTE);
1664 DISPATCH();
1665 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001666
Benjamin Petersonddd19492018-09-16 22:38:02 -07001667 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001668 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001669 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001670 int err;
1671 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001672 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001673 if (err != 0)
1674 goto error;
1675 PREDICT(JUMP_ABSOLUTE);
1676 DISPATCH();
1677 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001678
Benjamin Petersonddd19492018-09-16 22:38:02 -07001679 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001680 PyObject *exp = POP();
1681 PyObject *base = TOP();
1682 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1683 Py_DECREF(base);
1684 Py_DECREF(exp);
1685 SET_TOP(res);
1686 if (res == NULL)
1687 goto error;
1688 DISPATCH();
1689 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001690
Benjamin Petersonddd19492018-09-16 22:38:02 -07001691 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001692 PyObject *right = POP();
1693 PyObject *left = TOP();
1694 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1695 Py_DECREF(left);
1696 Py_DECREF(right);
1697 SET_TOP(res);
1698 if (res == NULL)
1699 goto error;
1700 DISPATCH();
1701 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001702
Benjamin Petersonddd19492018-09-16 22:38:02 -07001703 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001704 PyObject *right = POP();
1705 PyObject *left = TOP();
1706 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1707 Py_DECREF(left);
1708 Py_DECREF(right);
1709 SET_TOP(res);
1710 if (res == NULL)
1711 goto error;
1712 DISPATCH();
1713 }
1714
Benjamin Petersonddd19492018-09-16 22:38:02 -07001715 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001716 PyObject *divisor = POP();
1717 PyObject *dividend = TOP();
1718 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1719 Py_DECREF(dividend);
1720 Py_DECREF(divisor);
1721 SET_TOP(quotient);
1722 if (quotient == NULL)
1723 goto error;
1724 DISPATCH();
1725 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001726
Benjamin Petersonddd19492018-09-16 22:38:02 -07001727 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001728 PyObject *divisor = POP();
1729 PyObject *dividend = TOP();
1730 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1731 Py_DECREF(dividend);
1732 Py_DECREF(divisor);
1733 SET_TOP(quotient);
1734 if (quotient == NULL)
1735 goto error;
1736 DISPATCH();
1737 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001738
Benjamin Petersonddd19492018-09-16 22:38:02 -07001739 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001740 PyObject *right = POP();
1741 PyObject *left = TOP();
1742 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1743 Py_DECREF(left);
1744 Py_DECREF(right);
1745 SET_TOP(mod);
1746 if (mod == NULL)
1747 goto error;
1748 DISPATCH();
1749 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001750
Benjamin Petersonddd19492018-09-16 22:38:02 -07001751 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001752 PyObject *right = POP();
1753 PyObject *left = TOP();
1754 PyObject *sum;
1755 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001756 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001757 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001758 }
1759 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001760 sum = PyNumber_InPlaceAdd(left, right);
1761 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001762 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001763 Py_DECREF(right);
1764 SET_TOP(sum);
1765 if (sum == NULL)
1766 goto error;
1767 DISPATCH();
1768 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001769
Benjamin Petersonddd19492018-09-16 22:38:02 -07001770 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001771 PyObject *right = POP();
1772 PyObject *left = TOP();
1773 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1774 Py_DECREF(left);
1775 Py_DECREF(right);
1776 SET_TOP(diff);
1777 if (diff == NULL)
1778 goto error;
1779 DISPATCH();
1780 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001781
Benjamin Petersonddd19492018-09-16 22:38:02 -07001782 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001783 PyObject *right = POP();
1784 PyObject *left = TOP();
1785 PyObject *res = PyNumber_InPlaceLshift(left, right);
1786 Py_DECREF(left);
1787 Py_DECREF(right);
1788 SET_TOP(res);
1789 if (res == NULL)
1790 goto error;
1791 DISPATCH();
1792 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001793
Benjamin Petersonddd19492018-09-16 22:38:02 -07001794 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001795 PyObject *right = POP();
1796 PyObject *left = TOP();
1797 PyObject *res = PyNumber_InPlaceRshift(left, right);
1798 Py_DECREF(left);
1799 Py_DECREF(right);
1800 SET_TOP(res);
1801 if (res == NULL)
1802 goto error;
1803 DISPATCH();
1804 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001805
Benjamin Petersonddd19492018-09-16 22:38:02 -07001806 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001807 PyObject *right = POP();
1808 PyObject *left = TOP();
1809 PyObject *res = PyNumber_InPlaceAnd(left, right);
1810 Py_DECREF(left);
1811 Py_DECREF(right);
1812 SET_TOP(res);
1813 if (res == NULL)
1814 goto error;
1815 DISPATCH();
1816 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001817
Benjamin Petersonddd19492018-09-16 22:38:02 -07001818 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001819 PyObject *right = POP();
1820 PyObject *left = TOP();
1821 PyObject *res = PyNumber_InPlaceXor(left, right);
1822 Py_DECREF(left);
1823 Py_DECREF(right);
1824 SET_TOP(res);
1825 if (res == NULL)
1826 goto error;
1827 DISPATCH();
1828 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001829
Benjamin Petersonddd19492018-09-16 22:38:02 -07001830 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001831 PyObject *right = POP();
1832 PyObject *left = TOP();
1833 PyObject *res = PyNumber_InPlaceOr(left, right);
1834 Py_DECREF(left);
1835 Py_DECREF(right);
1836 SET_TOP(res);
1837 if (res == NULL)
1838 goto error;
1839 DISPATCH();
1840 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001841
Benjamin Petersonddd19492018-09-16 22:38:02 -07001842 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001843 PyObject *sub = TOP();
1844 PyObject *container = SECOND();
1845 PyObject *v = THIRD();
1846 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001847 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001848 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001849 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001850 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001851 Py_DECREF(container);
1852 Py_DECREF(sub);
1853 if (err != 0)
1854 goto error;
1855 DISPATCH();
1856 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001857
Benjamin Petersonddd19492018-09-16 22:38:02 -07001858 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001859 PyObject *sub = TOP();
1860 PyObject *container = SECOND();
1861 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001862 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00001863 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001864 err = PyObject_DelItem(container, sub);
1865 Py_DECREF(container);
1866 Py_DECREF(sub);
1867 if (err != 0)
1868 goto error;
1869 DISPATCH();
1870 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001871
Benjamin Petersonddd19492018-09-16 22:38:02 -07001872 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01001873 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001874 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001875 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001876 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001877 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001878 _PyErr_SetString(tstate, PyExc_RuntimeError,
1879 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001880 Py_DECREF(value);
1881 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001882 }
Jeroen Demeyer196a5302019-07-04 12:31:34 +02001883 res = _PyObject_CallOneArg(hook, value);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001884 Py_DECREF(value);
1885 if (res == NULL)
1886 goto error;
1887 Py_DECREF(res);
1888 DISPATCH();
1889 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001890
Benjamin Petersonddd19492018-09-16 22:38:02 -07001891 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001892 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001893 switch (oparg) {
1894 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001895 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001896 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001897 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001898 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001899 /* fall through */
1900 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02001901 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001902 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001903 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001904 break;
1905 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02001906 _PyErr_SetString(tstate, PyExc_SystemError,
1907 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001908 break;
1909 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001910 goto error;
1911 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001912
Benjamin Petersonddd19492018-09-16 22:38:02 -07001913 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001914 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001915 assert(f->f_iblock == 0);
Pablo Galindof00828a2019-05-09 16:52:02 +01001916 goto exit_returning;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001917 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001918
Benjamin Petersonddd19492018-09-16 22:38:02 -07001919 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001920 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001921 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001922 PyObject *obj = TOP();
1923 PyTypeObject *type = Py_TYPE(obj);
1924
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001925 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001926 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001927 }
Yury Selivanov75445082015-05-11 22:57:16 -04001928
1929 if (getter != NULL) {
1930 iter = (*getter)(obj);
1931 Py_DECREF(obj);
1932 if (iter == NULL) {
1933 SET_TOP(NULL);
1934 goto error;
1935 }
1936 }
1937 else {
1938 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02001939 _PyErr_Format(tstate, PyExc_TypeError,
1940 "'async for' requires an object with "
1941 "__aiter__ method, got %.100s",
1942 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001943 Py_DECREF(obj);
1944 goto error;
1945 }
1946
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001947 if (Py_TYPE(iter)->tp_as_async == NULL ||
1948 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001949
Yury Selivanov398ff912017-03-02 22:20:00 -05001950 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02001951 _PyErr_Format(tstate, PyExc_TypeError,
1952 "'async for' received an object from __aiter__ "
1953 "that does not implement __anext__: %.100s",
1954 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001955 Py_DECREF(iter);
1956 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001957 }
1958
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001959 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001960 DISPATCH();
1961 }
1962
Benjamin Petersonddd19492018-09-16 22:38:02 -07001963 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001964 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001965 PyObject *next_iter = NULL;
1966 PyObject *awaitable = NULL;
1967 PyObject *aiter = TOP();
1968 PyTypeObject *type = Py_TYPE(aiter);
1969
Yury Selivanoveb636452016-09-08 22:01:51 -07001970 if (PyAsyncGen_CheckExact(aiter)) {
1971 awaitable = type->tp_as_async->am_anext(aiter);
1972 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001973 goto error;
1974 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001975 } else {
1976 if (type->tp_as_async != NULL){
1977 getter = type->tp_as_async->am_anext;
1978 }
Yury Selivanov75445082015-05-11 22:57:16 -04001979
Yury Selivanoveb636452016-09-08 22:01:51 -07001980 if (getter != NULL) {
1981 next_iter = (*getter)(aiter);
1982 if (next_iter == NULL) {
1983 goto error;
1984 }
1985 }
1986 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02001987 _PyErr_Format(tstate, PyExc_TypeError,
1988 "'async for' requires an iterator with "
1989 "__anext__ method, got %.100s",
1990 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07001991 goto error;
1992 }
Yury Selivanov75445082015-05-11 22:57:16 -04001993
Yury Selivanoveb636452016-09-08 22:01:51 -07001994 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1995 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001996 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001997 PyExc_TypeError,
1998 "'async for' received an invalid object "
1999 "from __anext__: %.100s",
2000 Py_TYPE(next_iter)->tp_name);
2001
2002 Py_DECREF(next_iter);
2003 goto error;
2004 } else {
2005 Py_DECREF(next_iter);
2006 }
2007 }
Yury Selivanov75445082015-05-11 22:57:16 -04002008
2009 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002010 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002011 DISPATCH();
2012 }
2013
Benjamin Petersonddd19492018-09-16 22:38:02 -07002014 case TARGET(GET_AWAITABLE): {
2015 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002016 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002017 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002018
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002019 if (iter == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002020 format_awaitable_error(tstate, Py_TYPE(iterable),
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);
Pablo Galindof00828a2019-05-09 16:52:02 +01002081 goto exit_yielding;
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;
Pablo Galindof00828a2019-05-09 16:52:02 +01002098 goto exit_yielding;
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
Benjamin Petersonddd19492018-09-16 22:38:02 -07002131 case TARGET(POP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002132 /* If oparg is 0 at the top of the stack are 1 or 6 values:
2133 Either:
2134 - TOP = NULL or an integer
2135 or:
2136 - (TOP, SECOND, THIRD) = exc_info()
2137 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2138
2139 If oparg is 1 the value for 'return' was additionally pushed
2140 at the top of the stack.
2141 */
2142 PyObject *res = NULL;
2143 if (oparg) {
2144 res = POP();
2145 }
2146 PyObject *exc = POP();
2147 if (exc == NULL || PyLong_CheckExact(exc)) {
2148 Py_XDECREF(exc);
2149 }
2150 else {
2151 Py_DECREF(exc);
2152 Py_DECREF(POP());
2153 Py_DECREF(POP());
2154
2155 PyObject *type, *value, *traceback;
2156 _PyErr_StackItem *exc_info;
2157 PyTryBlock *b = PyFrame_BlockPop(f);
2158 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002159 _PyErr_SetString(tstate, PyExc_SystemError,
2160 "popped block is not an except handler");
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002161 Py_XDECREF(res);
2162 goto error;
2163 }
2164 assert(STACK_LEVEL() == (b)->b_level + 3);
2165 exc_info = tstate->exc_info;
2166 type = exc_info->exc_type;
2167 value = exc_info->exc_value;
2168 traceback = exc_info->exc_traceback;
2169 exc_info->exc_type = POP();
2170 exc_info->exc_value = POP();
2171 exc_info->exc_traceback = POP();
2172 Py_XDECREF(type);
2173 Py_XDECREF(value);
2174 Py_XDECREF(traceback);
2175 }
2176 if (oparg) {
2177 PUSH(res);
2178 }
2179 DISPATCH();
2180 }
2181
Benjamin Petersonddd19492018-09-16 22:38:02 -07002182 case TARGET(CALL_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002183 PyObject *ret = PyLong_FromLong(INSTR_OFFSET());
2184 if (ret == NULL) {
2185 goto error;
2186 }
2187 PUSH(ret);
2188 JUMPBY(oparg);
2189 FAST_DISPATCH();
2190 }
2191
Benjamin Petersonddd19492018-09-16 22:38:02 -07002192 case TARGET(BEGIN_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002193 /* Push NULL onto the stack for using it in END_FINALLY,
2194 POP_FINALLY, WITH_CLEANUP_START and WITH_CLEANUP_FINISH.
2195 */
2196 PUSH(NULL);
2197 FAST_DISPATCH();
2198 }
2199
Benjamin Petersonddd19492018-09-16 22:38:02 -07002200 case TARGET(END_FINALLY): {
2201 PREDICTED(END_FINALLY);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002202 /* At the top of the stack are 1 or 6 values:
2203 Either:
2204 - TOP = NULL or an integer
2205 or:
2206 - (TOP, SECOND, THIRD) = exc_info()
2207 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2208 */
2209 PyObject *exc = POP();
2210 if (exc == NULL) {
2211 FAST_DISPATCH();
2212 }
2213 else if (PyLong_CheckExact(exc)) {
2214 int ret = _PyLong_AsInt(exc);
2215 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02002216 if (ret == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002217 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002218 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002219 JUMPTO(ret);
2220 FAST_DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002221 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002222 else {
2223 assert(PyExceptionClass_Check(exc));
2224 PyObject *val = POP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002225 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002226 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002227 goto exception_unwind;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002228 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002229 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002230
Benjamin Petersonddd19492018-09-16 22:38:02 -07002231 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002232 PyObject *exc = POP();
2233 assert(PyExceptionClass_Check(exc));
2234 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2235 PyTryBlock *b = PyFrame_BlockPop(f);
2236 assert(b->b_type == EXCEPT_HANDLER);
2237 Py_DECREF(exc);
2238 UNWIND_EXCEPT_HANDLER(b);
2239 Py_DECREF(POP());
2240 JUMPBY(oparg);
2241 FAST_DISPATCH();
2242 }
2243 else {
2244 PyObject *val = POP();
2245 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002246 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002247 goto exception_unwind;
2248 }
2249 }
2250
Zackery Spytzce6a0702019-08-25 03:44:09 -06002251 case TARGET(LOAD_ASSERTION_ERROR): {
2252 PyObject *value = PyExc_AssertionError;
2253 Py_INCREF(value);
2254 PUSH(value);
2255 FAST_DISPATCH();
2256 }
2257
Benjamin Petersonddd19492018-09-16 22:38:02 -07002258 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002259 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002260
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002261 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002262 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002263 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002264 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002265 if (!_PyErr_Occurred(tstate)) {
2266 _PyErr_SetString(tstate, PyExc_NameError,
2267 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002268 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002269 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002270 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002271 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002272 }
2273 else {
2274 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2275 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002276 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002277 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2278 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002279 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2280 _PyErr_SetString(tstate, PyExc_NameError,
2281 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002282 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002283 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002284 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002285 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002286 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002287 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002288
Benjamin Petersonddd19492018-09-16 22:38:02 -07002289 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002290 PyObject *name = GETITEM(names, oparg);
2291 PyObject *v = POP();
2292 PyObject *ns = f->f_locals;
2293 int err;
2294 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002295 _PyErr_Format(tstate, PyExc_SystemError,
2296 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002297 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002298 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002299 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002300 if (PyDict_CheckExact(ns))
2301 err = PyDict_SetItem(ns, name, v);
2302 else
2303 err = PyObject_SetItem(ns, name, v);
2304 Py_DECREF(v);
2305 if (err != 0)
2306 goto error;
2307 DISPATCH();
2308 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002309
Benjamin Petersonddd19492018-09-16 22:38:02 -07002310 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002311 PyObject *name = GETITEM(names, oparg);
2312 PyObject *ns = f->f_locals;
2313 int err;
2314 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002315 _PyErr_Format(tstate, PyExc_SystemError,
2316 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002317 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002318 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002319 err = PyObject_DelItem(ns, name);
2320 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002321 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002322 NAME_ERROR_MSG,
2323 name);
2324 goto error;
2325 }
2326 DISPATCH();
2327 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002328
Benjamin Petersonddd19492018-09-16 22:38:02 -07002329 case TARGET(UNPACK_SEQUENCE): {
2330 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002331 PyObject *seq = POP(), *item, **items;
2332 if (PyTuple_CheckExact(seq) &&
2333 PyTuple_GET_SIZE(seq) == oparg) {
2334 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002335 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002336 item = items[oparg];
2337 Py_INCREF(item);
2338 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002339 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002340 } else if (PyList_CheckExact(seq) &&
2341 PyList_GET_SIZE(seq) == oparg) {
2342 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002343 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002344 item = items[oparg];
2345 Py_INCREF(item);
2346 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002347 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002348 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002349 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002350 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002351 } else {
2352 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002353 Py_DECREF(seq);
2354 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002355 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002356 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002357 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002358 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002359
Benjamin Petersonddd19492018-09-16 22:38:02 -07002360 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002361 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2362 PyObject *seq = POP();
2363
Victor Stinner438a12d2019-05-24 17:01:38 +02002364 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002365 stack_pointer + totalargs)) {
2366 stack_pointer += totalargs;
2367 } else {
2368 Py_DECREF(seq);
2369 goto error;
2370 }
2371 Py_DECREF(seq);
2372 DISPATCH();
2373 }
2374
Benjamin Petersonddd19492018-09-16 22:38:02 -07002375 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002376 PyObject *name = GETITEM(names, oparg);
2377 PyObject *owner = TOP();
2378 PyObject *v = SECOND();
2379 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002380 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002381 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002382 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002383 Py_DECREF(owner);
2384 if (err != 0)
2385 goto error;
2386 DISPATCH();
2387 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002388
Benjamin Petersonddd19492018-09-16 22:38:02 -07002389 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002390 PyObject *name = GETITEM(names, oparg);
2391 PyObject *owner = POP();
2392 int err;
2393 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2394 Py_DECREF(owner);
2395 if (err != 0)
2396 goto error;
2397 DISPATCH();
2398 }
2399
Benjamin Petersonddd19492018-09-16 22:38:02 -07002400 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002401 PyObject *name = GETITEM(names, oparg);
2402 PyObject *v = POP();
2403 int err;
2404 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002405 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002406 if (err != 0)
2407 goto error;
2408 DISPATCH();
2409 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002410
Benjamin Petersonddd19492018-09-16 22:38:02 -07002411 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002412 PyObject *name = GETITEM(names, oparg);
2413 int err;
2414 err = PyDict_DelItem(f->f_globals, name);
2415 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002416 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2417 format_exc_check_arg(tstate, PyExc_NameError,
2418 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002419 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002420 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002421 }
2422 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002423 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002424
Benjamin Petersonddd19492018-09-16 22:38:02 -07002425 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002426 PyObject *name = GETITEM(names, oparg);
2427 PyObject *locals = f->f_locals;
2428 PyObject *v;
2429 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002430 _PyErr_Format(tstate, PyExc_SystemError,
2431 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002432 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002433 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002434 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002435 v = PyDict_GetItemWithError(locals, name);
2436 if (v != NULL) {
2437 Py_INCREF(v);
2438 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002439 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002440 goto error;
2441 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002442 }
2443 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002444 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002445 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002446 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002447 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002448 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002449 }
2450 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002451 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002452 v = PyDict_GetItemWithError(f->f_globals, name);
2453 if (v != NULL) {
2454 Py_INCREF(v);
2455 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002456 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002457 goto error;
2458 }
2459 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002460 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002461 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002462 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002463 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002464 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002465 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002466 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002467 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002468 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002469 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002470 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002471 }
2472 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002473 v = PyObject_GetItem(f->f_builtins, name);
2474 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002475 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002476 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002477 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002478 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002479 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002480 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002481 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002482 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002483 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002484 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002485 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002486 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002487 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002488
Benjamin Petersonddd19492018-09-16 22:38:02 -07002489 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002490 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002491 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002492 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002493 && PyDict_CheckExact(f->f_builtins))
2494 {
Inada Naoki91234a12019-06-03 21:30:58 +09002495 OPCACHE_CHECK();
2496 if (co_opcache != NULL && co_opcache->optimized > 0) {
2497 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2498
2499 if (lg->globals_ver ==
2500 ((PyDictObject *)f->f_globals)->ma_version_tag
2501 && lg->builtins_ver ==
2502 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2503 {
2504 PyObject *ptr = lg->ptr;
2505 OPCACHE_STAT_GLOBAL_HIT();
2506 assert(ptr != NULL);
2507 Py_INCREF(ptr);
2508 PUSH(ptr);
2509 DISPATCH();
2510 }
2511 }
2512
2513 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002514 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002515 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002516 name);
2517 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002518 if (!_PyErr_OCCURRED()) {
2519 /* _PyDict_LoadGlobal() returns NULL without raising
2520 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002521 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002522 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002523 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002524 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002525 }
Inada Naoki91234a12019-06-03 21:30:58 +09002526
2527 if (co_opcache != NULL) {
2528 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2529
2530 if (co_opcache->optimized == 0) {
2531 /* Wasn't optimized before. */
2532 OPCACHE_STAT_GLOBAL_OPT();
2533 } else {
2534 OPCACHE_STAT_GLOBAL_MISS();
2535 }
2536
2537 co_opcache->optimized = 1;
2538 lg->globals_ver =
2539 ((PyDictObject *)f->f_globals)->ma_version_tag;
2540 lg->builtins_ver =
2541 ((PyDictObject *)f->f_builtins)->ma_version_tag;
2542 lg->ptr = v; /* borrowed */
2543 }
2544
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002545 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002546 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002547 else {
2548 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002549
2550 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09002551 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002552 v = PyObject_GetItem(f->f_globals, name);
2553 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002554 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002555 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002556 }
2557 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002558
Victor Stinnerb4efc962015-11-20 09:24:02 +01002559 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002560 v = PyObject_GetItem(f->f_builtins, name);
2561 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002562 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002563 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002564 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002565 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002566 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002567 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002568 }
2569 }
2570 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002571 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002572 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002573 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002574
Benjamin Petersonddd19492018-09-16 22:38:02 -07002575 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002576 PyObject *v = GETLOCAL(oparg);
2577 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002578 SETLOCAL(oparg, NULL);
2579 DISPATCH();
2580 }
2581 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002582 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002583 UNBOUNDLOCAL_ERROR_MSG,
2584 PyTuple_GetItem(co->co_varnames, oparg)
2585 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002586 goto error;
2587 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002588
Benjamin Petersonddd19492018-09-16 22:38:02 -07002589 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002590 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002591 PyObject *oldobj = PyCell_GET(cell);
2592 if (oldobj != NULL) {
2593 PyCell_SET(cell, NULL);
2594 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002595 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002596 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002597 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002598 goto error;
2599 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002600
Benjamin Petersonddd19492018-09-16 22:38:02 -07002601 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002602 PyObject *cell = freevars[oparg];
2603 Py_INCREF(cell);
2604 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002605 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002606 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002607
Benjamin Petersonddd19492018-09-16 22:38:02 -07002608 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002609 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002610 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002611 assert(locals);
2612 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2613 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2614 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2615 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2616 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002617 value = PyDict_GetItemWithError(locals, name);
2618 if (value != NULL) {
2619 Py_INCREF(value);
2620 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002621 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002622 goto error;
2623 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002624 }
2625 else {
2626 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002627 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002628 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002629 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002630 }
2631 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002632 }
2633 }
2634 if (!value) {
2635 PyObject *cell = freevars[oparg];
2636 value = PyCell_GET(cell);
2637 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002638 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002639 goto error;
2640 }
2641 Py_INCREF(value);
2642 }
2643 PUSH(value);
2644 DISPATCH();
2645 }
2646
Benjamin Petersonddd19492018-09-16 22:38:02 -07002647 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002648 PyObject *cell = freevars[oparg];
2649 PyObject *value = PyCell_GET(cell);
2650 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002651 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002652 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002653 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002654 Py_INCREF(value);
2655 PUSH(value);
2656 DISPATCH();
2657 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002658
Benjamin Petersonddd19492018-09-16 22:38:02 -07002659 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002660 PyObject *v = POP();
2661 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002662 PyObject *oldobj = PyCell_GET(cell);
2663 PyCell_SET(cell, v);
2664 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002665 DISPATCH();
2666 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002667
Benjamin Petersonddd19492018-09-16 22:38:02 -07002668 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002669 PyObject *str;
2670 PyObject *empty = PyUnicode_New(0, 0);
2671 if (empty == NULL) {
2672 goto error;
2673 }
2674 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2675 Py_DECREF(empty);
2676 if (str == NULL)
2677 goto error;
2678 while (--oparg >= 0) {
2679 PyObject *item = POP();
2680 Py_DECREF(item);
2681 }
2682 PUSH(str);
2683 DISPATCH();
2684 }
2685
Benjamin Petersonddd19492018-09-16 22:38:02 -07002686 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002687 PyObject *tup = PyTuple_New(oparg);
2688 if (tup == NULL)
2689 goto error;
2690 while (--oparg >= 0) {
2691 PyObject *item = POP();
2692 PyTuple_SET_ITEM(tup, oparg, item);
2693 }
2694 PUSH(tup);
2695 DISPATCH();
2696 }
2697
Benjamin Petersonddd19492018-09-16 22:38:02 -07002698 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002699 PyObject *list = PyList_New(oparg);
2700 if (list == NULL)
2701 goto error;
2702 while (--oparg >= 0) {
2703 PyObject *item = POP();
2704 PyList_SET_ITEM(list, oparg, item);
2705 }
2706 PUSH(list);
2707 DISPATCH();
2708 }
2709
Benjamin Petersonddd19492018-09-16 22:38:02 -07002710 case TARGET(BUILD_TUPLE_UNPACK_WITH_CALL):
2711 case TARGET(BUILD_TUPLE_UNPACK):
2712 case TARGET(BUILD_LIST_UNPACK): {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002713 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002714 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002715 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002716 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002717
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002718 if (sum == NULL)
2719 goto error;
2720
2721 for (i = oparg; i > 0; i--) {
2722 PyObject *none_val;
2723
2724 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2725 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002726 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
Victor Stinner438a12d2019-05-24 17:01:38 +02002727 _PyErr_ExceptionMatches(tstate, PyExc_TypeError))
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002728 {
Victor Stinner438a12d2019-05-24 17:01:38 +02002729 check_args_iterable(tstate, PEEK(1 + oparg), PEEK(i));
Serhiy Storchaka73442852016-10-02 10:33:46 +03002730 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002731 Py_DECREF(sum);
2732 goto error;
2733 }
2734 Py_DECREF(none_val);
2735 }
2736
2737 if (convert_to_tuple) {
2738 return_value = PyList_AsTuple(sum);
2739 Py_DECREF(sum);
2740 if (return_value == NULL)
2741 goto error;
2742 }
2743 else {
2744 return_value = sum;
2745 }
2746
2747 while (oparg--)
2748 Py_DECREF(POP());
2749 PUSH(return_value);
2750 DISPATCH();
2751 }
2752
Benjamin Petersonddd19492018-09-16 22:38:02 -07002753 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002754 PyObject *set = PySet_New(NULL);
2755 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002756 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002757 if (set == NULL)
2758 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002759 for (i = oparg; i > 0; i--) {
2760 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002761 if (err == 0)
2762 err = PySet_Add(set, item);
2763 Py_DECREF(item);
2764 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002765 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002766 if (err != 0) {
2767 Py_DECREF(set);
2768 goto error;
2769 }
2770 PUSH(set);
2771 DISPATCH();
2772 }
2773
Benjamin Petersonddd19492018-09-16 22:38:02 -07002774 case TARGET(BUILD_SET_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002775 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002776 PyObject *sum = PySet_New(NULL);
2777 if (sum == NULL)
2778 goto error;
2779
2780 for (i = oparg; i > 0; i--) {
2781 if (_PySet_Update(sum, PEEK(i)) < 0) {
2782 Py_DECREF(sum);
2783 goto error;
2784 }
2785 }
2786
2787 while (oparg--)
2788 Py_DECREF(POP());
2789 PUSH(sum);
2790 DISPATCH();
2791 }
2792
Benjamin Petersonddd19492018-09-16 22:38:02 -07002793 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002794 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002795 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2796 if (map == NULL)
2797 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002798 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002799 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002800 PyObject *key = PEEK(2*i);
2801 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002802 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002803 if (err != 0) {
2804 Py_DECREF(map);
2805 goto error;
2806 }
2807 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002808
2809 while (oparg--) {
2810 Py_DECREF(POP());
2811 Py_DECREF(POP());
2812 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002813 PUSH(map);
2814 DISPATCH();
2815 }
2816
Benjamin Petersonddd19492018-09-16 22:38:02 -07002817 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002818 _Py_IDENTIFIER(__annotations__);
2819 int err;
2820 PyObject *ann_dict;
2821 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002822 _PyErr_Format(tstate, PyExc_SystemError,
2823 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002824 goto error;
2825 }
2826 /* check if __annotations__ in locals()... */
2827 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002828 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002829 &PyId___annotations__);
2830 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002831 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002832 goto error;
2833 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002834 /* ...if not, create a new one */
2835 ann_dict = PyDict_New();
2836 if (ann_dict == NULL) {
2837 goto error;
2838 }
2839 err = _PyDict_SetItemId(f->f_locals,
2840 &PyId___annotations__, ann_dict);
2841 Py_DECREF(ann_dict);
2842 if (err != 0) {
2843 goto error;
2844 }
2845 }
2846 }
2847 else {
2848 /* do the same if locals() is not a dict */
2849 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2850 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002851 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002852 }
2853 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2854 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002855 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002856 goto error;
2857 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002858 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002859 ann_dict = PyDict_New();
2860 if (ann_dict == NULL) {
2861 goto error;
2862 }
2863 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2864 Py_DECREF(ann_dict);
2865 if (err != 0) {
2866 goto error;
2867 }
2868 }
2869 else {
2870 Py_DECREF(ann_dict);
2871 }
2872 }
2873 DISPATCH();
2874 }
2875
Benjamin Petersonddd19492018-09-16 22:38:02 -07002876 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002877 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002878 PyObject *map;
2879 PyObject *keys = TOP();
2880 if (!PyTuple_CheckExact(keys) ||
2881 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002882 _PyErr_SetString(tstate, PyExc_SystemError,
2883 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002884 goto error;
2885 }
2886 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2887 if (map == NULL) {
2888 goto error;
2889 }
2890 for (i = oparg; i > 0; i--) {
2891 int err;
2892 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2893 PyObject *value = PEEK(i + 1);
2894 err = PyDict_SetItem(map, key, value);
2895 if (err != 0) {
2896 Py_DECREF(map);
2897 goto error;
2898 }
2899 }
2900
2901 Py_DECREF(POP());
2902 while (oparg--) {
2903 Py_DECREF(POP());
2904 }
2905 PUSH(map);
2906 DISPATCH();
2907 }
2908
Benjamin Petersonddd19492018-09-16 22:38:02 -07002909 case TARGET(BUILD_MAP_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002910 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002911 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002912 if (sum == NULL)
2913 goto error;
2914
2915 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002916 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002917 if (PyDict_Update(sum, arg) < 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002918 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
2919 _PyErr_Format(tstate, PyExc_TypeError,
2920 "'%.200s' object is not a mapping",
2921 arg->ob_type->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002922 }
2923 Py_DECREF(sum);
2924 goto error;
2925 }
2926 }
2927
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002928 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002929 Py_DECREF(POP());
2930 PUSH(sum);
2931 DISPATCH();
2932 }
2933
Benjamin Petersonddd19492018-09-16 22:38:02 -07002934 case TARGET(BUILD_MAP_UNPACK_WITH_CALL): {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002935 Py_ssize_t i;
2936 PyObject *sum = PyDict_New();
2937 if (sum == NULL)
2938 goto error;
2939
2940 for (i = oparg; i > 0; i--) {
2941 PyObject *arg = PEEK(i);
2942 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002943 Py_DECREF(sum);
Victor Stinner438a12d2019-05-24 17:01:38 +02002944 format_kwargs_error(tstate, PEEK(2 + oparg), arg);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002945 goto error;
2946 }
2947 }
2948
2949 while (oparg--)
2950 Py_DECREF(POP());
2951 PUSH(sum);
Brandt Bucherf185a732019-09-28 17:12:49 -07002952 PREDICT(CALL_FUNCTION_EX);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002953 DISPATCH();
2954 }
2955
Benjamin Petersonddd19492018-09-16 22:38:02 -07002956 case TARGET(MAP_ADD): {
Jörn Heisslerc8a35412019-06-22 16:40:55 +02002957 PyObject *value = TOP();
2958 PyObject *key = SECOND();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002959 PyObject *map;
2960 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002961 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002962 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002963 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002964 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002965 Py_DECREF(value);
2966 Py_DECREF(key);
2967 if (err != 0)
2968 goto error;
2969 PREDICT(JUMP_ABSOLUTE);
2970 DISPATCH();
2971 }
2972
Benjamin Petersonddd19492018-09-16 22:38:02 -07002973 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002974 PyObject *name = GETITEM(names, oparg);
2975 PyObject *owner = TOP();
2976 PyObject *res = PyObject_GetAttr(owner, name);
2977 Py_DECREF(owner);
2978 SET_TOP(res);
2979 if (res == NULL)
2980 goto error;
2981 DISPATCH();
2982 }
2983
Benjamin Petersonddd19492018-09-16 22:38:02 -07002984 case TARGET(COMPARE_OP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002985 PyObject *right = POP();
2986 PyObject *left = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002987 PyObject *res = cmp_outcome(tstate, oparg, left, right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002988 Py_DECREF(left);
2989 Py_DECREF(right);
2990 SET_TOP(res);
2991 if (res == NULL)
2992 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002993 PREDICT(POP_JUMP_IF_FALSE);
2994 PREDICT(POP_JUMP_IF_TRUE);
2995 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002996 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002997
Benjamin Petersonddd19492018-09-16 22:38:02 -07002998 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002999 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003000 PyObject *fromlist = POP();
3001 PyObject *level = TOP();
3002 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003003 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003004 Py_DECREF(level);
3005 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003006 SET_TOP(res);
3007 if (res == NULL)
3008 goto error;
3009 DISPATCH();
3010 }
3011
Benjamin Petersonddd19492018-09-16 22:38:02 -07003012 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003013 PyObject *from = POP(), *locals;
3014 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003015 if (PyFrame_FastToLocalsWithError(f) < 0) {
3016 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01003017 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003018 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01003019
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003020 locals = f->f_locals;
3021 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003022 _PyErr_SetString(tstate, PyExc_SystemError,
3023 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003024 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003025 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003026 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003027 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003028 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003029 Py_DECREF(from);
3030 if (err != 0)
3031 goto error;
3032 DISPATCH();
3033 }
Guido van Rossum25831651993-05-19 14:50:45 +00003034
Benjamin Petersonddd19492018-09-16 22:38:02 -07003035 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003036 PyObject *name = GETITEM(names, oparg);
3037 PyObject *from = TOP();
3038 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003039 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003040 PUSH(res);
3041 if (res == NULL)
3042 goto error;
3043 DISPATCH();
3044 }
Thomas Wouters52152252000-08-17 22:55:00 +00003045
Benjamin Petersonddd19492018-09-16 22:38:02 -07003046 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003047 JUMPBY(oparg);
3048 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003049 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003050
Benjamin Petersonddd19492018-09-16 22:38:02 -07003051 case TARGET(POP_JUMP_IF_FALSE): {
3052 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003053 PyObject *cond = POP();
3054 int err;
3055 if (cond == Py_True) {
3056 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003057 FAST_DISPATCH();
3058 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003059 if (cond == Py_False) {
3060 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003061 JUMPTO(oparg);
3062 FAST_DISPATCH();
3063 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003064 err = PyObject_IsTrue(cond);
3065 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003066 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003067 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003068 else if (err == 0)
3069 JUMPTO(oparg);
3070 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003071 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003072 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003073 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003074
Benjamin Petersonddd19492018-09-16 22:38:02 -07003075 case TARGET(POP_JUMP_IF_TRUE): {
3076 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003077 PyObject *cond = POP();
3078 int err;
3079 if (cond == Py_False) {
3080 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003081 FAST_DISPATCH();
3082 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003083 if (cond == Py_True) {
3084 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003085 JUMPTO(oparg);
3086 FAST_DISPATCH();
3087 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003088 err = PyObject_IsTrue(cond);
3089 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003090 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003091 JUMPTO(oparg);
3092 }
3093 else if (err == 0)
3094 ;
3095 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003096 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003097 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003098 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003099
Benjamin Petersonddd19492018-09-16 22:38:02 -07003100 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003101 PyObject *cond = TOP();
3102 int err;
3103 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003104 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003105 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003106 FAST_DISPATCH();
3107 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003108 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003109 JUMPTO(oparg);
3110 FAST_DISPATCH();
3111 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003112 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003113 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003114 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003115 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003116 }
3117 else if (err == 0)
3118 JUMPTO(oparg);
3119 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003120 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003121 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003122 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003123
Benjamin Petersonddd19492018-09-16 22:38:02 -07003124 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003125 PyObject *cond = TOP();
3126 int err;
3127 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003128 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003129 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003130 FAST_DISPATCH();
3131 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003132 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003133 JUMPTO(oparg);
3134 FAST_DISPATCH();
3135 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003136 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003137 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003138 JUMPTO(oparg);
3139 }
3140 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003141 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003142 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003143 }
3144 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003145 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003146 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003147 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003148
Benjamin Petersonddd19492018-09-16 22:38:02 -07003149 case TARGET(JUMP_ABSOLUTE): {
3150 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003151 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003152#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003153 /* Enabling this path speeds-up all while and for-loops by bypassing
3154 the per-loop checks for signals. By default, this should be turned-off
3155 because it prevents detection of a control-break in tight loops like
3156 "while 1: pass". Compile with this option turned-on when you need
3157 the speed-up and do not need break checking inside tight loops (ones
3158 that contain only instructions ending with FAST_DISPATCH).
3159 */
3160 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003161#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003162 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003163#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003164 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003165
Benjamin Petersonddd19492018-09-16 22:38:02 -07003166 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003167 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003168 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003169 PyObject *iter = PyObject_GetIter(iterable);
3170 Py_DECREF(iterable);
3171 SET_TOP(iter);
3172 if (iter == NULL)
3173 goto error;
3174 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003175 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003176 DISPATCH();
3177 }
3178
Benjamin Petersonddd19492018-09-16 22:38:02 -07003179 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04003180 /* before: [obj]; after [getiter(obj)] */
3181 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003182 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003183 if (PyCoro_CheckExact(iterable)) {
3184 /* `iterable` is a coroutine */
3185 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3186 /* and it is used in a 'yield from' expression of a
3187 regular generator. */
3188 Py_DECREF(iterable);
3189 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003190 _PyErr_SetString(tstate, PyExc_TypeError,
3191 "cannot 'yield from' a coroutine object "
3192 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04003193 goto error;
3194 }
3195 }
3196 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003197 /* `iterable` is not a generator. */
3198 iter = PyObject_GetIter(iterable);
3199 Py_DECREF(iterable);
3200 SET_TOP(iter);
3201 if (iter == NULL)
3202 goto error;
3203 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003204 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003205 DISPATCH();
3206 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003207
Benjamin Petersonddd19492018-09-16 22:38:02 -07003208 case TARGET(FOR_ITER): {
3209 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003210 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003211 PyObject *iter = TOP();
3212 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
3213 if (next != NULL) {
3214 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003215 PREDICT(STORE_FAST);
3216 PREDICT(UNPACK_SEQUENCE);
3217 DISPATCH();
3218 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003219 if (_PyErr_Occurred(tstate)) {
3220 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003221 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003222 }
3223 else if (tstate->c_tracefunc != NULL) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003224 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Victor Stinner438a12d2019-05-24 17:01:38 +02003225 }
3226 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003227 }
3228 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003229 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003230 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003231 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003232 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003233 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003234 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003235
Benjamin Petersonddd19492018-09-16 22:38:02 -07003236 case TARGET(SETUP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003237 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003238 STACK_LEVEL());
3239 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003240 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003241
Benjamin Petersonddd19492018-09-16 22:38:02 -07003242 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003243 _Py_IDENTIFIER(__aexit__);
3244 _Py_IDENTIFIER(__aenter__);
3245
3246 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003247 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__),
Yury Selivanov75445082015-05-11 22:57:16 -04003248 *enter;
3249 PyObject *res;
3250 if (exit == NULL)
3251 goto error;
3252 SET_TOP(exit);
Victor Stinner438a12d2019-05-24 17:01:38 +02003253 enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04003254 Py_DECREF(mgr);
3255 if (enter == NULL)
3256 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003257 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003258 Py_DECREF(enter);
3259 if (res == NULL)
3260 goto error;
3261 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003262 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003263 DISPATCH();
3264 }
3265
Benjamin Petersonddd19492018-09-16 22:38:02 -07003266 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003267 PyObject *res = POP();
3268 /* Setup the finally block before pushing the result
3269 of __aenter__ on the stack. */
3270 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3271 STACK_LEVEL());
3272 PUSH(res);
3273 DISPATCH();
3274 }
3275
Benjamin Petersonddd19492018-09-16 22:38:02 -07003276 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003277 _Py_IDENTIFIER(__exit__);
3278 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003279 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003280 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003281 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003282 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08003283 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003284 }
3285 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003286 if (exit == NULL) {
3287 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003288 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003289 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003290 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003291 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003292 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003293 Py_DECREF(enter);
3294 if (res == NULL)
3295 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003296 /* Setup the finally block before pushing the result
3297 of __enter__ on the stack. */
3298 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3299 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003300
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003301 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003302 DISPATCH();
3303 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003304
Benjamin Petersonddd19492018-09-16 22:38:02 -07003305 case TARGET(WITH_CLEANUP_START): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003306 /* At the top of the stack are 1 or 6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003307 how/why we entered the finally clause:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003308 - TOP = NULL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003309 - (TOP, SECOND, THIRD) = exc_info()
3310 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003311 Below them is EXIT, the context.__exit__ or context.__aexit__
3312 bound method.
3313 In the first case, we must call
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003314 EXIT(None, None, None)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003315 otherwise we must call
3316 EXIT(TOP, SECOND, THIRD)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003317
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003318 In the first case, we remove EXIT from the
3319 stack, leaving TOP, and push TOP on the stack.
3320 Otherwise we shift the bottom 3 values of the
3321 stack down, replace the empty spot with NULL, and push
3322 None on the stack.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003323
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003324 Finally we push the result of the call.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003325 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003326 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003327 PyObject *exc, *val, *tb, *res;
3328
3329 val = tb = Py_None;
3330 exc = TOP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003331 if (exc == NULL) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003332 STACK_SHRINK(1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003333 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003334 SET_TOP(exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003335 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003336 }
3337 else {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003338 assert(PyExceptionClass_Check(exc));
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003339 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003340 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003341 val = SECOND();
3342 tb = THIRD();
3343 tp2 = FOURTH();
3344 exc2 = PEEK(5);
3345 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003346 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003347 SET_VALUE(7, tb2);
3348 SET_VALUE(6, exc2);
3349 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003350 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3351 SET_FOURTH(NULL);
3352 /* We just shifted the stack down, so we have
3353 to tell the except handler block that the
3354 values are lower than it expects. */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003355 assert(f->f_iblock > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003356 block = &f->f_blockstack[f->f_iblock - 1];
3357 assert(block->b_type == EXCEPT_HANDLER);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003358 assert(block->b_level > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003359 block->b_level--;
3360 }
Victor Stinner842cfff2016-12-01 14:45:31 +01003361
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003362 PyObject *stack[4] = {NULL, exc, val, tb};
3363 res = _PyObject_Vectorcall(exit_func, stack + 1,
3364 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003365 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003366 if (res == NULL)
3367 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003368
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003369 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003370 PUSH(exc);
3371 PUSH(res);
3372 PREDICT(WITH_CLEANUP_FINISH);
3373 DISPATCH();
3374 }
3375
Benjamin Petersonddd19492018-09-16 22:38:02 -07003376 case TARGET(WITH_CLEANUP_FINISH): {
3377 PREDICTED(WITH_CLEANUP_FINISH);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003378 /* TOP = the result of calling the context.__exit__ bound method
3379 SECOND = either None or exception type
3380
3381 If SECOND is None below is NULL or the return address,
3382 otherwise below are 7 values representing an exception.
3383 */
Yury Selivanov75445082015-05-11 22:57:16 -04003384 PyObject *res = POP();
3385 PyObject *exc = POP();
3386 int err;
3387
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003388 if (exc != Py_None)
3389 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003390 else
3391 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003392
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003393 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003394 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003396 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003397 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003398 else if (err > 0) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003399 /* There was an exception and a True return.
3400 * We must manually unwind the EXCEPT_HANDLER block
3401 * which was created when the exception was caught,
Quan Tian3bd0d622018-10-20 05:30:03 +08003402 * otherwise the stack will be in an inconsistent state.
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003403 */
3404 PyTryBlock *b = PyFrame_BlockPop(f);
3405 assert(b->b_type == EXCEPT_HANDLER);
3406 UNWIND_EXCEPT_HANDLER(b);
3407 PUSH(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003408 }
3409 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003410 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003411 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003412
Benjamin Petersonddd19492018-09-16 22:38:02 -07003413 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10003414 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003415 PyObject *name = GETITEM(names, oparg);
3416 PyObject *obj = TOP();
3417 PyObject *meth = NULL;
3418
3419 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3420
Yury Selivanovf2392132016-12-13 19:03:51 -05003421 if (meth == NULL) {
3422 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003423 goto error;
3424 }
3425
3426 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003427 /* We can bypass temporary bound method object.
3428 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003429
INADA Naoki015bce62017-01-16 17:23:30 +09003430 meth | self | arg1 | ... | argN
3431 */
3432 SET_TOP(meth);
3433 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003434 }
3435 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003436 /* meth is not an unbound method (but a regular attr, or
3437 something was returned by a descriptor protocol). Set
3438 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003439 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003440
3441 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003442 */
INADA Naoki015bce62017-01-16 17:23:30 +09003443 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003444 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003445 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003446 }
3447 DISPATCH();
3448 }
3449
Benjamin Petersonddd19492018-09-16 22:38:02 -07003450 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003451 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003452 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003453
3454 sp = stack_pointer;
3455
INADA Naoki015bce62017-01-16 17:23:30 +09003456 meth = PEEK(oparg + 2);
3457 if (meth == NULL) {
3458 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3459 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003460
3461 Stack layout:
3462
INADA Naoki015bce62017-01-16 17:23:30 +09003463 ... | NULL | callable | arg1 | ... | argN
3464 ^- TOP()
3465 ^- (-oparg)
3466 ^- (-oparg-1)
3467 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003468
Ville Skyttä49b27342017-08-03 09:00:59 +03003469 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003470 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003471 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003472 res = call_function(tstate, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003473 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003474 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003475 }
3476 else {
3477 /* This is a method call. Stack layout:
3478
INADA Naoki015bce62017-01-16 17:23:30 +09003479 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003480 ^- TOP()
3481 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003482 ^- (-oparg-1)
3483 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003484
INADA Naoki015bce62017-01-16 17:23:30 +09003485 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003486 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003487 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003488 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003489 res = call_function(tstate, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003490 stack_pointer = sp;
3491 }
3492
3493 PUSH(res);
3494 if (res == NULL)
3495 goto error;
3496 DISPATCH();
3497 }
3498
Benjamin Petersonddd19492018-09-16 22:38:02 -07003499 case TARGET(CALL_FUNCTION): {
3500 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003501 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003502 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003503 res = call_function(tstate, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003504 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003505 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003506 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003507 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003508 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003509 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003510 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003511
Benjamin Petersonddd19492018-09-16 22:38:02 -07003512 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003513 PyObject **sp, *res, *names;
3514
3515 names = POP();
Jeroen Demeyer05677862019-08-16 12:41:27 +02003516 assert(PyTuple_Check(names));
3517 assert(PyTuple_GET_SIZE(names) <= oparg);
3518 /* We assume without checking that names contains only strings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003519 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003520 res = call_function(tstate, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003521 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003522 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003523 Py_DECREF(names);
3524
3525 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003526 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003527 }
3528 DISPATCH();
3529 }
3530
Benjamin Petersonddd19492018-09-16 22:38:02 -07003531 case TARGET(CALL_FUNCTION_EX): {
Brandt Bucherf185a732019-09-28 17:12:49 -07003532 PREDICTED(CALL_FUNCTION_EX);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003533 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003534 if (oparg & 0x01) {
3535 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003536 if (!PyDict_CheckExact(kwargs)) {
3537 PyObject *d = PyDict_New();
3538 if (d == NULL)
3539 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003540 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003541 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02003542 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003543 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003544 goto error;
3545 }
3546 Py_DECREF(kwargs);
3547 kwargs = d;
3548 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003549 assert(PyDict_CheckExact(kwargs));
3550 }
3551 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003552 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003553 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003554 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003555 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003556 goto error;
3557 }
3558 Py_SETREF(callargs, PySequence_Tuple(callargs));
3559 if (callargs == NULL) {
3560 goto error;
3561 }
3562 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003563 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003564
Victor Stinner09532fe2019-05-10 23:39:09 +02003565 result = do_call_core(tstate, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003566 Py_DECREF(func);
3567 Py_DECREF(callargs);
3568 Py_XDECREF(kwargs);
3569
3570 SET_TOP(result);
3571 if (result == NULL) {
3572 goto error;
3573 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003574 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003575 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003576
Benjamin Petersonddd19492018-09-16 22:38:02 -07003577 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003578 PyObject *qualname = POP();
3579 PyObject *codeobj = POP();
3580 PyFunctionObject *func = (PyFunctionObject *)
3581 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003582
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003583 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003584 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003585 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003586 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003587 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003588
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003589 if (oparg & 0x08) {
3590 assert(PyTuple_CheckExact(TOP()));
3591 func ->func_closure = POP();
3592 }
3593 if (oparg & 0x04) {
3594 assert(PyDict_CheckExact(TOP()));
3595 func->func_annotations = POP();
3596 }
3597 if (oparg & 0x02) {
3598 assert(PyDict_CheckExact(TOP()));
3599 func->func_kwdefaults = POP();
3600 }
3601 if (oparg & 0x01) {
3602 assert(PyTuple_CheckExact(TOP()));
3603 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003604 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003605
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003606 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003607 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003608 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003609
Benjamin Petersonddd19492018-09-16 22:38:02 -07003610 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003611 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003612 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003613 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003614 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003615 step = NULL;
3616 stop = POP();
3617 start = TOP();
3618 slice = PySlice_New(start, stop, step);
3619 Py_DECREF(start);
3620 Py_DECREF(stop);
3621 Py_XDECREF(step);
3622 SET_TOP(slice);
3623 if (slice == NULL)
3624 goto error;
3625 DISPATCH();
3626 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003627
Benjamin Petersonddd19492018-09-16 22:38:02 -07003628 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003629 /* Handles f-string value formatting. */
3630 PyObject *result;
3631 PyObject *fmt_spec;
3632 PyObject *value;
3633 PyObject *(*conv_fn)(PyObject *);
3634 int which_conversion = oparg & FVC_MASK;
3635 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3636
3637 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003638 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003639
3640 /* See if any conversion is specified. */
3641 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003642 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003643 case FVC_STR: conv_fn = PyObject_Str; break;
3644 case FVC_REPR: conv_fn = PyObject_Repr; break;
3645 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003646 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02003647 _PyErr_Format(tstate, PyExc_SystemError,
3648 "unexpected conversion flag %d",
3649 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003650 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003651 }
3652
3653 /* If there's a conversion function, call it and replace
3654 value with that result. Otherwise, just use value,
3655 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003656 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003657 result = conv_fn(value);
3658 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003659 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003660 Py_XDECREF(fmt_spec);
3661 goto error;
3662 }
3663 value = result;
3664 }
3665
3666 /* If value is a unicode object, and there's no fmt_spec,
3667 then we know the result of format(value) is value
3668 itself. In that case, skip calling format(). I plan to
3669 move this optimization in to PyObject_Format()
3670 itself. */
3671 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3672 /* Do nothing, just transfer ownership to result. */
3673 result = value;
3674 } else {
3675 /* Actually call format(). */
3676 result = PyObject_Format(value, fmt_spec);
3677 Py_DECREF(value);
3678 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003679 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003680 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003681 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003682 }
3683
Eric V. Smith135d5f42016-02-05 18:23:08 -05003684 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003685 DISPATCH();
3686 }
3687
Benjamin Petersonddd19492018-09-16 22:38:02 -07003688 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003689 int oldoparg = oparg;
3690 NEXTOPARG();
3691 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003692 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003693 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003694
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003695
Antoine Pitrou042b1282010-08-13 21:15:58 +00003696#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003697 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003698#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003699 default:
3700 fprintf(stderr,
3701 "XXX lineno: %d, opcode: %d\n",
3702 PyFrame_GetLineNumber(f),
3703 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02003704 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003705 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003706
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003707 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003708
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003709 /* This should never be reached. Every opcode should end with DISPATCH()
3710 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003711 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003712
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003713error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003714 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003715#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02003716 if (!_PyErr_Occurred(tstate)) {
3717 _PyErr_SetString(tstate, PyExc_SystemError,
3718 "error return without exception set");
3719 }
Victor Stinner365b6932013-07-12 00:11:58 +02003720#else
Victor Stinner438a12d2019-05-24 17:01:38 +02003721 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02003722#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003723
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003724 /* Log traceback info. */
3725 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003726
Benjamin Peterson51f46162013-01-23 08:38:47 -05003727 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003728 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3729 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003730
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003731exception_unwind:
3732 /* Unwind stacks if an exception occurred */
3733 while (f->f_iblock > 0) {
3734 /* Pop the current block. */
3735 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003736
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003737 if (b->b_type == EXCEPT_HANDLER) {
3738 UNWIND_EXCEPT_HANDLER(b);
3739 continue;
3740 }
3741 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003742 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003743 PyObject *exc, *val, *tb;
3744 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003745 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003746 /* Beware, this invalidates all b->b_* fields */
3747 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003748 PUSH(exc_info->exc_traceback);
3749 PUSH(exc_info->exc_value);
3750 if (exc_info->exc_type != NULL) {
3751 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003752 }
3753 else {
3754 Py_INCREF(Py_None);
3755 PUSH(Py_None);
3756 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003757 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003758 /* Make the raw exception data
3759 available to the handler,
3760 so a program can emulate the
3761 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02003762 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003763 if (tb != NULL)
3764 PyException_SetTraceback(val, tb);
3765 else
3766 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003767 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003768 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003769 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003770 exc_info->exc_value = val;
3771 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003772 if (tb == NULL)
3773 tb = Py_None;
3774 Py_INCREF(tb);
3775 PUSH(tb);
3776 PUSH(val);
3777 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003778 JUMPTO(handler);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003779 /* Resume normal execution */
3780 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003781 }
3782 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003783
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003784 /* End the loop as we still have an error */
3785 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003786 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003787
Pablo Galindof00828a2019-05-09 16:52:02 +01003788 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003789 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01003790
3791exit_returning:
3792
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003793 /* Pop remaining stack entries. */
3794 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003795 PyObject *o = POP();
3796 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003797 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003798
Pablo Galindof00828a2019-05-09 16:52:02 +01003799exit_yielding:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003800 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003801 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003802 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3803 tstate, f, PyTrace_RETURN, retval)) {
3804 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003805 }
3806 }
3807 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003808 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3809 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003810 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003811 }
3812 }
3813 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003814
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003815 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003816exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003817 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3818 dtrace_function_return(f);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01003819 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrou58720d62013-08-05 23:26:40 +02003820 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003821 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003822
Victor Stinner17269092019-11-05 01:22:12 +01003823 return _Py_CheckFunctionResult(tstate, NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003824}
3825
Benjamin Petersonb204a422011-06-05 22:04:07 -05003826static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003827format_missing(PyThreadState *tstate, const char *kind,
3828 PyCodeObject *co, PyObject *names)
Benjamin Petersone109c702011-06-24 09:37:26 -05003829{
3830 int err;
3831 Py_ssize_t len = PyList_GET_SIZE(names);
3832 PyObject *name_str, *comma, *tail, *tmp;
3833
3834 assert(PyList_CheckExact(names));
3835 assert(len >= 1);
3836 /* Deal with the joys of natural language. */
3837 switch (len) {
3838 case 1:
3839 name_str = PyList_GET_ITEM(names, 0);
3840 Py_INCREF(name_str);
3841 break;
3842 case 2:
3843 name_str = PyUnicode_FromFormat("%U and %U",
3844 PyList_GET_ITEM(names, len - 2),
3845 PyList_GET_ITEM(names, len - 1));
3846 break;
3847 default:
3848 tail = PyUnicode_FromFormat(", %U, and %U",
3849 PyList_GET_ITEM(names, len - 2),
3850 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003851 if (tail == NULL)
3852 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003853 /* Chop off the last two objects in the list. This shouldn't actually
3854 fail, but we can't be too careful. */
3855 err = PyList_SetSlice(names, len - 2, len, NULL);
3856 if (err == -1) {
3857 Py_DECREF(tail);
3858 return;
3859 }
3860 /* Stitch everything up into a nice comma-separated list. */
3861 comma = PyUnicode_FromString(", ");
3862 if (comma == NULL) {
3863 Py_DECREF(tail);
3864 return;
3865 }
3866 tmp = PyUnicode_Join(comma, names);
3867 Py_DECREF(comma);
3868 if (tmp == NULL) {
3869 Py_DECREF(tail);
3870 return;
3871 }
3872 name_str = PyUnicode_Concat(tmp, tail);
3873 Py_DECREF(tmp);
3874 Py_DECREF(tail);
3875 break;
3876 }
3877 if (name_str == NULL)
3878 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02003879 _PyErr_Format(tstate, PyExc_TypeError,
3880 "%U() missing %i required %s argument%s: %U",
3881 co->co_name,
3882 len,
3883 kind,
3884 len == 1 ? "" : "s",
3885 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05003886 Py_DECREF(name_str);
3887}
3888
3889static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003890missing_arguments(PyThreadState *tstate, PyCodeObject *co,
3891 Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003892 PyObject **fastlocals)
3893{
Victor Stinner74319ae2016-08-25 00:04:09 +02003894 Py_ssize_t i, j = 0;
3895 Py_ssize_t start, end;
3896 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003897 const char *kind = positional ? "positional" : "keyword-only";
3898 PyObject *missing_names;
3899
3900 /* Compute the names of the arguments that are missing. */
3901 missing_names = PyList_New(missing);
3902 if (missing_names == NULL)
3903 return;
3904 if (positional) {
3905 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01003906 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003907 }
3908 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003909 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003910 end = start + co->co_kwonlyargcount;
3911 }
3912 for (i = start; i < end; i++) {
3913 if (GETLOCAL(i) == NULL) {
3914 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3915 PyObject *name = PyObject_Repr(raw);
3916 if (name == NULL) {
3917 Py_DECREF(missing_names);
3918 return;
3919 }
3920 PyList_SET_ITEM(missing_names, j++, name);
3921 }
3922 }
3923 assert(j == missing);
Victor Stinner438a12d2019-05-24 17:01:38 +02003924 format_missing(tstate, kind, co, missing_names);
Benjamin Petersone109c702011-06-24 09:37:26 -05003925 Py_DECREF(missing_names);
3926}
3927
3928static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003929too_many_positional(PyThreadState *tstate, PyCodeObject *co,
3930 Py_ssize_t given, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003931 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003932{
3933 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003934 Py_ssize_t kwonly_given = 0;
3935 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003936 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003937 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003938
Benjamin Petersone109c702011-06-24 09:37:26 -05003939 assert((co->co_flags & CO_VARARGS) == 0);
3940 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01003941 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003942 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003943 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003944 }
3945 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003946 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01003947 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003948 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01003949 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003950 }
3951 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003952 plural = (co_argcount != 1);
3953 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003954 }
3955 if (sig == NULL)
3956 return;
3957 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003958 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3959 kwonly_sig = PyUnicode_FromFormat(format,
3960 given != 1 ? "s" : "",
3961 kwonly_given,
3962 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003963 if (kwonly_sig == NULL) {
3964 Py_DECREF(sig);
3965 return;
3966 }
3967 }
3968 else {
3969 /* This will not fail. */
3970 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003971 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003972 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003973 _PyErr_Format(tstate, PyExc_TypeError,
3974 "%U() takes %U positional argument%s but %zd%U %s given",
3975 co->co_name,
3976 sig,
3977 plural ? "s" : "",
3978 given,
3979 kwonly_sig,
3980 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003981 Py_DECREF(sig);
3982 Py_DECREF(kwonly_sig);
3983}
3984
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003985static int
Victor Stinner438a12d2019-05-24 17:01:38 +02003986positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
3987 Py_ssize_t kwcount, PyObject* const* kwnames)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003988{
3989 int posonly_conflicts = 0;
3990 PyObject* posonly_names = PyList_New(0);
3991
3992 for(int k=0; k < co->co_posonlyargcount; k++){
3993 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
3994
3995 for (int k2=0; k2<kwcount; k2++){
3996 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
3997 PyObject* kwname = kwnames[k2];
3998 if (kwname == posonly_name){
3999 if(PyList_Append(posonly_names, kwname) != 0) {
4000 goto fail;
4001 }
4002 posonly_conflicts++;
4003 continue;
4004 }
4005
4006 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
4007
4008 if ( cmp > 0) {
4009 if(PyList_Append(posonly_names, kwname) != 0) {
4010 goto fail;
4011 }
4012 posonly_conflicts++;
4013 } else if (cmp < 0) {
4014 goto fail;
4015 }
4016
4017 }
4018 }
4019 if (posonly_conflicts) {
4020 PyObject* comma = PyUnicode_FromString(", ");
4021 if (comma == NULL) {
4022 goto fail;
4023 }
4024 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
4025 Py_DECREF(comma);
4026 if (error_names == NULL) {
4027 goto fail;
4028 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004029 _PyErr_Format(tstate, PyExc_TypeError,
4030 "%U() got some positional-only arguments passed"
4031 " as keyword arguments: '%U'",
4032 co->co_name, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004033 Py_DECREF(error_names);
4034 goto fail;
4035 }
4036
4037 Py_DECREF(posonly_names);
4038 return 0;
4039
4040fail:
4041 Py_XDECREF(posonly_names);
4042 return 1;
4043
4044}
4045
Guido van Rossumc2e20742006-02-27 22:32:47 +00004046/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02004047 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00004048 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00004049
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004050PyObject *
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004051_PyEval_EvalCode(PyThreadState *tstate,
4052 PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004053 PyObject *const *args, Py_ssize_t argcount,
4054 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03004055 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004056 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02004057 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004058 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00004059{
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004060 assert(tstate != NULL);
4061
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00004062 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004063 PyFrameObject *f;
4064 PyObject *retval = NULL;
4065 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004066 PyObject *x, *u;
Pablo Galindocd74e662019-06-01 18:08:04 +01004067 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004068 Py_ssize_t i, j, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02004069 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00004070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004071 if (globals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004072 _PyErr_SetString(tstate, PyExc_SystemError,
4073 "PyEval_EvalCodeEx: NULL globals");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004074 return NULL;
4075 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004076
Victor Stinnerc7020012016-08-16 23:40:29 +02004077 /* Create the frame */
INADA Naoki5a625d02016-12-24 20:19:08 +09004078 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02004079 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004080 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02004081 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004082 fastlocals = f->f_localsplus;
4083 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00004084
Victor Stinnerc7020012016-08-16 23:40:29 +02004085 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004086 if (co->co_flags & CO_VARKEYWORDS) {
4087 kwdict = PyDict_New();
4088 if (kwdict == NULL)
4089 goto fail;
4090 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02004091 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004092 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02004093 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004094 SETLOCAL(i, kwdict);
4095 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004096 else {
4097 kwdict = NULL;
4098 }
4099
Pablo Galindocd74e662019-06-01 18:08:04 +01004100 /* Copy all positional arguments into local variables */
4101 if (argcount > co->co_argcount) {
4102 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02004103 }
4104 else {
4105 n = argcount;
4106 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004107 for (j = 0; j < n; j++) {
4108 x = args[j];
4109 Py_INCREF(x);
4110 SETLOCAL(j, x);
4111 }
4112
Victor Stinnerc7020012016-08-16 23:40:29 +02004113 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004114 if (co->co_flags & CO_VARARGS) {
Sergey Fedoseev234531b2019-02-25 21:59:12 +05004115 u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02004116 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004117 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004118 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004119 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004120 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004121
Serhiy Storchakab7281052016-09-12 00:52:40 +03004122 /* Handle keyword arguments passed as two strided arrays */
4123 kwcount *= kwstep;
4124 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004125 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03004126 PyObject *keyword = kwnames[i];
4127 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02004128 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004129
Benjamin Petersonb204a422011-06-05 22:04:07 -05004130 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004131 _PyErr_Format(tstate, PyExc_TypeError,
4132 "%U() keywords must be strings",
4133 co->co_name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004134 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004135 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004136
Benjamin Petersonb204a422011-06-05 22:04:07 -05004137 /* Speed hack: do raw pointer compares. As names are
4138 normally interned this should almost always hit. */
4139 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004140 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004141 PyObject *name = co_varnames[j];
4142 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004143 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004144 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004145 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004146
Benjamin Petersonb204a422011-06-05 22:04:07 -05004147 /* Slow fallback, just in case */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004148 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004149 PyObject *name = co_varnames[j];
4150 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
4151 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004152 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004153 }
4154 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004155 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004156 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004157 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004158
Victor Stinner231d1f32017-01-11 02:12:06 +01004159 assert(j >= total_args);
4160 if (kwdict == NULL) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004161
Victor Stinner438a12d2019-05-24 17:01:38 +02004162 if (co->co_posonlyargcount
4163 && positional_only_passed_as_keyword(tstate, co,
4164 kwcount, kwnames))
4165 {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004166 goto fail;
4167 }
4168
Victor Stinner438a12d2019-05-24 17:01:38 +02004169 _PyErr_Format(tstate, PyExc_TypeError,
4170 "%U() got an unexpected keyword argument '%S'",
4171 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004172 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004173 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004174
Christian Heimes0bd447f2013-07-20 14:48:10 +02004175 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4176 goto fail;
4177 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004178 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004179
Benjamin Petersonb204a422011-06-05 22:04:07 -05004180 kw_found:
4181 if (GETLOCAL(j) != NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004182 _PyErr_Format(tstate, PyExc_TypeError,
4183 "%U() got multiple values for argument '%S'",
4184 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004185 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004186 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004187 Py_INCREF(value);
4188 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004189 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004190
4191 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004192 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004193 too_many_positional(tstate, co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004194 goto fail;
4195 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004196
4197 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004198 if (argcount < co->co_argcount) {
4199 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004200 Py_ssize_t missing = 0;
4201 for (i = argcount; i < m; i++) {
4202 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004203 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004204 }
4205 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004206 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004207 missing_arguments(tstate, co, missing, defcount, fastlocals);
Benjamin Petersone109c702011-06-24 09:37:26 -05004208 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004209 }
4210 if (n > m)
4211 i = n - m;
4212 else
4213 i = 0;
4214 for (; i < defcount; i++) {
4215 if (GETLOCAL(m+i) == NULL) {
4216 PyObject *def = defs[i];
4217 Py_INCREF(def);
4218 SETLOCAL(m+i, def);
4219 }
4220 }
4221 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004222
4223 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004224 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004225 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004226 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004227 PyObject *name;
4228 if (GETLOCAL(i) != NULL)
4229 continue;
4230 name = PyTuple_GET_ITEM(co->co_varnames, i);
4231 if (kwdefs != NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004232 PyObject *def = PyDict_GetItemWithError(kwdefs, name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004233 if (def) {
4234 Py_INCREF(def);
4235 SETLOCAL(i, def);
4236 continue;
4237 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004238 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004239 goto fail;
4240 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004241 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004242 missing++;
4243 }
4244 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004245 missing_arguments(tstate, co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004246 goto fail;
4247 }
4248 }
4249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004250 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004251 vars into frame. */
4252 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004253 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004254 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004255 /* Possibly account for the cell variable being an argument. */
4256 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004257 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004258 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004259 /* Clear the local copy. */
4260 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004261 }
4262 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004263 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004264 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004265 if (c == NULL)
4266 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004267 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004268 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004269
4270 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004271 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4272 PyObject *o = PyTuple_GET_ITEM(closure, i);
4273 Py_INCREF(o);
4274 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004275 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004276
Yury Selivanoveb636452016-09-08 22:01:51 -07004277 /* Handle generator/coroutine/asynchronous generator */
4278 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004279 PyObject *gen;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004280 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004282 /* Don't need to keep the reference to f_back, it will be set
4283 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004284 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004285
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004286 /* Create a new generator that owns the ready to run frame
4287 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004288 if (is_coro) {
4289 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004290 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4291 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004292 } else {
4293 gen = PyGen_NewWithQualName(f, name, qualname);
4294 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004295 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004296 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004297 }
INADA Naoki9c157762016-12-26 18:52:46 +09004298
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004299 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004300
Yury Selivanov75445082015-05-11 22:57:16 -04004301 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004302 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004303
Victor Stinnerb9e68122019-11-14 12:20:46 +01004304 retval = _PyEval_EvalFrame(tstate, f, 0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004305
Thomas Woutersce272b62007-09-19 21:19:28 +00004306fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004308 /* decref'ing the frame can cause __del__ methods to get invoked,
4309 which can call back into Python. While we're done with the
4310 current Python frame (f), the associated C stack is still in use,
4311 so recursion_depth must be boosted for the duration.
4312 */
INADA Naoki5a625d02016-12-24 20:19:08 +09004313 if (Py_REFCNT(f) > 1) {
4314 Py_DECREF(f);
4315 _PyObject_GC_TRACK(f);
4316 }
4317 else {
4318 ++tstate->recursion_depth;
4319 Py_DECREF(f);
4320 --tstate->recursion_depth;
4321 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004322 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004323}
4324
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004325
4326PyObject *
4327_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
4328 PyObject *const *args, Py_ssize_t argcount,
4329 PyObject *const *kwnames, PyObject *const *kwargs,
4330 Py_ssize_t kwcount, int kwstep,
4331 PyObject *const *defs, Py_ssize_t defcount,
4332 PyObject *kwdefs, PyObject *closure,
4333 PyObject *name, PyObject *qualname)
4334{
4335 PyThreadState *tstate = _PyThreadState_GET();
4336 return _PyEval_EvalCode(tstate, _co, globals, locals,
4337 args, argcount,
4338 kwnames, kwargs,
4339 kwcount, kwstep,
4340 defs, defcount,
4341 kwdefs, closure,
4342 name, qualname);
4343}
4344
Victor Stinner40ee3012014-06-16 15:59:28 +02004345PyObject *
4346PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004347 PyObject *const *args, int argcount,
4348 PyObject *const *kws, int kwcount,
4349 PyObject *const *defs, int defcount,
4350 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004351{
4352 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004353 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004354 kws, kws != NULL ? kws + 1 : NULL,
4355 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004356 defs, defcount,
4357 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004358 NULL, NULL);
4359}
Tim Peters5ca576e2001-06-18 22:08:13 +00004360
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004361static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004362special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004363{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004364 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004365 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02004366 if (res == NULL && !_PyErr_Occurred(tstate)) {
4367 _PyErr_SetObject(tstate, PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004368 return NULL;
4369 }
4370 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004371}
4372
4373
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004374/* Logic for the raise statement (too complicated for inlining).
4375 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004376static int
Victor Stinner09532fe2019-05-10 23:39:09 +02004377do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004378{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004379 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004381 if (exc == NULL) {
4382 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01004383 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004384 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004385 type = exc_info->exc_type;
4386 value = exc_info->exc_value;
4387 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004388 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004389 _PyErr_SetString(tstate, PyExc_RuntimeError,
4390 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004391 return 0;
4392 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004393 Py_XINCREF(type);
4394 Py_XINCREF(value);
4395 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02004396 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004397 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004398 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004400 /* We support the following forms of raise:
4401 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004402 raise <instance>
4403 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004405 if (PyExceptionClass_Check(exc)) {
4406 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004407 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004408 if (value == NULL)
4409 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004410 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004411 _PyErr_Format(tstate, PyExc_TypeError,
4412 "calling %R should have returned an instance of "
4413 "BaseException, not %R",
4414 type, Py_TYPE(value));
4415 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004416 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004417 }
4418 else if (PyExceptionInstance_Check(exc)) {
4419 value = exc;
4420 type = PyExceptionInstance_Class(exc);
4421 Py_INCREF(type);
4422 }
4423 else {
4424 /* Not something you can raise. You get an exception
4425 anyway, just not what you specified :-) */
4426 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02004427 _PyErr_SetString(tstate, PyExc_TypeError,
4428 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004429 goto raise_error;
4430 }
Collin Winter828f04a2007-08-31 00:04:24 +00004431
Serhiy Storchakac0191582016-09-27 11:37:10 +03004432 assert(type != NULL);
4433 assert(value != NULL);
4434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004435 if (cause) {
4436 PyObject *fixed_cause;
4437 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004438 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004439 if (fixed_cause == NULL)
4440 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004441 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004442 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004443 else if (PyExceptionInstance_Check(cause)) {
4444 fixed_cause = cause;
4445 }
4446 else if (cause == Py_None) {
4447 Py_DECREF(cause);
4448 fixed_cause = NULL;
4449 }
4450 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004451 _PyErr_SetString(tstate, PyExc_TypeError,
4452 "exception causes must derive from "
4453 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004454 goto raise_error;
4455 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004456 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004457 }
Collin Winter828f04a2007-08-31 00:04:24 +00004458
Victor Stinner438a12d2019-05-24 17:01:38 +02004459 _PyErr_SetObject(tstate, type, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004460 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004461 Py_DECREF(value);
4462 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004463 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004464
4465raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004466 Py_XDECREF(value);
4467 Py_XDECREF(type);
4468 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004469 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004470}
4471
Tim Petersd6d010b2001-06-21 02:49:55 +00004472/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004473 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004474
Guido van Rossum0368b722007-05-11 16:50:42 +00004475 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4476 with a variable target.
4477*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004478
Barry Warsawe42b18f1997-08-25 22:13:04 +00004479static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004480unpack_iterable(PyThreadState *tstate, PyObject *v,
4481 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004482{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004483 int i = 0, j = 0;
4484 Py_ssize_t ll = 0;
4485 PyObject *it; /* iter(v) */
4486 PyObject *w;
4487 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004489 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004491 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004492 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004493 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004494 v->ob_type->tp_iter == NULL && !PySequence_Check(v))
4495 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004496 _PyErr_Format(tstate, PyExc_TypeError,
4497 "cannot unpack non-iterable %.200s object",
4498 v->ob_type->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004499 }
4500 return 0;
4501 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004503 for (; i < argcnt; i++) {
4504 w = PyIter_Next(it);
4505 if (w == NULL) {
4506 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004507 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04004508 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004509 _PyErr_Format(tstate, PyExc_ValueError,
4510 "not enough values to unpack "
4511 "(expected %d, got %d)",
4512 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004513 }
4514 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004515 _PyErr_Format(tstate, PyExc_ValueError,
4516 "not enough values to unpack "
4517 "(expected at least %d, got %d)",
4518 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004519 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004520 }
4521 goto Error;
4522 }
4523 *--sp = w;
4524 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004526 if (argcntafter == -1) {
4527 /* We better have exhausted the iterator now. */
4528 w = PyIter_Next(it);
4529 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004530 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004531 goto Error;
4532 Py_DECREF(it);
4533 return 1;
4534 }
4535 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02004536 _PyErr_Format(tstate, PyExc_ValueError,
4537 "too many values to unpack (expected %d)",
4538 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004539 goto Error;
4540 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004542 l = PySequence_List(it);
4543 if (l == NULL)
4544 goto Error;
4545 *--sp = l;
4546 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004548 ll = PyList_GET_SIZE(l);
4549 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004550 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04004551 "not enough values to unpack (expected at least %d, got %zd)",
4552 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004553 goto Error;
4554 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004556 /* Pop the "after-variable" args off the list. */
4557 for (j = argcntafter; j > 0; j--, i++) {
4558 *--sp = PyList_GET_ITEM(l, ll - j);
4559 }
4560 /* Resize the list. */
4561 Py_SIZE(l) = ll - argcntafter;
4562 Py_DECREF(it);
4563 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004564
Tim Petersd6d010b2001-06-21 02:49:55 +00004565Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004566 for (; i > 0; i--, sp++)
4567 Py_DECREF(*sp);
4568 Py_XDECREF(it);
4569 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004570}
4571
4572
Guido van Rossum96a42c81992-01-12 02:29:51 +00004573#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004574static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004575prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004576{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004577 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02004578 if (PyObject_Print(v, stdout, 0) != 0) {
4579 /* Don't know what else to do */
4580 _PyErr_Clear(tstate);
4581 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004582 printf("\n");
4583 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004584}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004585#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004586
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004587static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004588call_exc_trace(Py_tracefunc func, PyObject *self,
4589 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004590{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004591 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004592 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004593 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004594 if (value == NULL) {
4595 value = Py_None;
4596 Py_INCREF(value);
4597 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004598 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01004599 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004600 arg = PyTuple_Pack(3, type, value, traceback);
4601 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004602 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004603 return;
4604 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004605 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004606 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02004607 if (err == 0) {
4608 _PyErr_Restore(tstate, type, value, orig_traceback);
4609 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004610 else {
4611 Py_XDECREF(type);
4612 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004613 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004614 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004615}
4616
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004617static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004618call_trace_protected(Py_tracefunc func, PyObject *obj,
4619 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004620 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004621{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004622 PyObject *type, *value, *traceback;
4623 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004624 _PyErr_Fetch(tstate, &type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004625 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004626 if (err == 0)
4627 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004628 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004629 return 0;
4630 }
4631 else {
4632 Py_XDECREF(type);
4633 Py_XDECREF(value);
4634 Py_XDECREF(traceback);
4635 return -1;
4636 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004637}
4638
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004639static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004640call_trace(Py_tracefunc func, PyObject *obj,
4641 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004642 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004643{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004644 int result;
4645 if (tstate->tracing)
4646 return 0;
4647 tstate->tracing++;
4648 tstate->use_tracing = 0;
4649 result = func(obj, frame, what, arg);
4650 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4651 || (tstate->c_profilefunc != NULL));
4652 tstate->tracing--;
4653 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004654}
4655
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004656PyObject *
4657_PyEval_CallTracing(PyObject *func, PyObject *args)
4658{
Victor Stinner50b48572018-11-01 01:51:40 +01004659 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004660 int save_tracing = tstate->tracing;
4661 int save_use_tracing = tstate->use_tracing;
4662 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004663
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004664 tstate->tracing = 0;
4665 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4666 || (tstate->c_profilefunc != NULL));
4667 result = PyObject_Call(func, args, NULL);
4668 tstate->tracing = save_tracing;
4669 tstate->use_tracing = save_use_tracing;
4670 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004671}
4672
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004673/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004674static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004675maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004676 PyThreadState *tstate, PyFrameObject *frame,
4677 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004678{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004679 int result = 0;
4680 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004682 /* If the last instruction executed isn't in the current
4683 instruction window, reset the window.
4684 */
4685 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4686 PyAddrPair bounds;
4687 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4688 &bounds);
4689 *instr_lb = bounds.ap_lower;
4690 *instr_ub = bounds.ap_upper;
4691 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004692 /* If the last instruction falls at the start of a line or if it
4693 represents a jump backwards, update the frame's line number and
4694 then call the trace function if we're tracing source lines.
4695 */
4696 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004697 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004698 if (frame->f_trace_lines) {
4699 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4700 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004701 }
George King20faa682017-10-18 17:44:22 -07004702 /* Always emit an opcode event if we're tracing all opcodes. */
4703 if (frame->f_trace_opcodes) {
4704 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4705 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004706 *instr_prev = frame->f_lasti;
4707 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004708}
4709
Fred Drake5755ce62001-06-27 19:19:46 +00004710void
4711PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004712{
Steve Dowerb82e17e2019-05-23 08:45:22 -07004713 if (PySys_Audit("sys.setprofile", NULL) < 0) {
4714 return;
4715 }
4716
Victor Stinner50b48572018-11-01 01:51:40 +01004717 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004718 PyObject *temp = tstate->c_profileobj;
4719 Py_XINCREF(arg);
4720 tstate->c_profilefunc = NULL;
4721 tstate->c_profileobj = NULL;
4722 /* Must make sure that tracing is not ignored if 'temp' is freed */
4723 tstate->use_tracing = tstate->c_tracefunc != NULL;
4724 Py_XDECREF(temp);
4725 tstate->c_profilefunc = func;
4726 tstate->c_profileobj = arg;
4727 /* Flag that tracing or profiling is turned on */
4728 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004729}
4730
4731void
4732PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4733{
Steve Dowerb82e17e2019-05-23 08:45:22 -07004734 if (PySys_Audit("sys.settrace", NULL) < 0) {
4735 return;
4736 }
4737
Victor Stinner09532fe2019-05-10 23:39:09 +02004738 _PyRuntimeState *runtime = &_PyRuntime;
4739 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004740 PyObject *temp = tstate->c_traceobj;
Victor Stinner09532fe2019-05-10 23:39:09 +02004741 runtime->ceval.tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004742 Py_XINCREF(arg);
4743 tstate->c_tracefunc = NULL;
4744 tstate->c_traceobj = NULL;
4745 /* Must make sure that profiling is not ignored if 'temp' is freed */
4746 tstate->use_tracing = tstate->c_profilefunc != NULL;
4747 Py_XDECREF(temp);
4748 tstate->c_tracefunc = func;
4749 tstate->c_traceobj = arg;
4750 /* Flag that tracing or profiling is turned on */
4751 tstate->use_tracing = ((func != NULL)
4752 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004753}
4754
Yury Selivanov75445082015-05-11 22:57:16 -04004755void
Victor Stinner838f2642019-06-13 22:41:23 +02004756_PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004757{
4758 assert(new_depth >= 0);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004759 tstate->coroutine_origin_tracking_depth = new_depth;
4760}
4761
4762int
4763_PyEval_GetCoroutineOriginTrackingDepth(void)
4764{
Victor Stinner50b48572018-11-01 01:51:40 +01004765 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004766 return tstate->coroutine_origin_tracking_depth;
4767}
4768
4769void
Yury Selivanoveb636452016-09-08 22:01:51 -07004770_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4771{
Victor Stinner50b48572018-11-01 01:51:40 +01004772 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004773
4774 if (PySys_Audit("sys.set_asyncgen_hook_firstiter", NULL) < 0) {
4775 return;
4776 }
4777
Yury Selivanoveb636452016-09-08 22:01:51 -07004778 Py_XINCREF(firstiter);
4779 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4780}
4781
4782PyObject *
4783_PyEval_GetAsyncGenFirstiter(void)
4784{
Victor Stinner50b48572018-11-01 01:51:40 +01004785 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004786 return tstate->async_gen_firstiter;
4787}
4788
4789void
4790_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4791{
Victor Stinner50b48572018-11-01 01:51:40 +01004792 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004793
4794 if (PySys_Audit("sys.set_asyncgen_hook_finalizer", NULL) < 0) {
4795 return;
4796 }
4797
Yury Selivanoveb636452016-09-08 22:01:51 -07004798 Py_XINCREF(finalizer);
4799 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4800}
4801
4802PyObject *
4803_PyEval_GetAsyncGenFinalizer(void)
4804{
Victor Stinner50b48572018-11-01 01:51:40 +01004805 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004806 return tstate->async_gen_finalizer;
4807}
4808
Victor Stinner438a12d2019-05-24 17:01:38 +02004809static PyFrameObject *
4810_PyEval_GetFrame(PyThreadState *tstate)
4811{
Victor Stinner01b1cc12019-11-20 02:27:56 +01004812 _PyRuntimeState *runtime = tstate->interp->runtime;
4813 return runtime->gilstate.getframe(tstate);
Victor Stinner438a12d2019-05-24 17:01:38 +02004814}
4815
4816PyFrameObject *
4817PyEval_GetFrame(void)
4818{
4819 PyThreadState *tstate = _PyThreadState_GET();
4820 return _PyEval_GetFrame(tstate);
4821}
4822
Guido van Rossumb209a111997-04-29 18:18:01 +00004823PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004824PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004825{
Victor Stinner438a12d2019-05-24 17:01:38 +02004826 PyThreadState *tstate = _PyThreadState_GET();
4827 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004828 if (current_frame == NULL)
Victor Stinner438a12d2019-05-24 17:01:38 +02004829 return tstate->interp->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004830 else
4831 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004832}
4833
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004834/* Convenience function to get a builtin from its name */
4835PyObject *
4836_PyEval_GetBuiltinId(_Py_Identifier *name)
4837{
Victor Stinner438a12d2019-05-24 17:01:38 +02004838 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004839 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4840 if (attr) {
4841 Py_INCREF(attr);
4842 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004843 else if (!_PyErr_Occurred(tstate)) {
4844 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004845 }
4846 return attr;
4847}
4848
Guido van Rossumb209a111997-04-29 18:18:01 +00004849PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004850PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004851{
Victor Stinner438a12d2019-05-24 17:01:38 +02004852 PyThreadState *tstate = _PyThreadState_GET();
4853 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Victor Stinner41bb43a2013-10-29 01:19:37 +01004854 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004855 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004856 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004857 }
4858
Victor Stinner438a12d2019-05-24 17:01:38 +02004859 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01004860 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004861 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004862
4863 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004864 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004865}
4866
Guido van Rossumb209a111997-04-29 18:18:01 +00004867PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004868PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004869{
Victor Stinner438a12d2019-05-24 17:01:38 +02004870 PyThreadState *tstate = _PyThreadState_GET();
4871 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
4872 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004873 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004874 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004875
4876 assert(current_frame->f_globals != NULL);
4877 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004878}
4879
Guido van Rossum6135a871995-01-09 17:53:26 +00004880int
Tim Peters5ba58662001-07-16 02:29:45 +00004881PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004882{
Victor Stinner438a12d2019-05-24 17:01:38 +02004883 PyThreadState *tstate = _PyThreadState_GET();
4884 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004885 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004886
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004887 if (current_frame != NULL) {
4888 const int codeflags = current_frame->f_code->co_flags;
4889 const int compilerflags = codeflags & PyCF_MASK;
4890 if (compilerflags) {
4891 result = 1;
4892 cf->cf_flags |= compilerflags;
4893 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004894#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004895 if (codeflags & CO_GENERATOR_ALLOWED) {
4896 result = 1;
4897 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4898 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004899#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004900 }
4901 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004902}
4903
Guido van Rossum3f5da241990-12-20 15:06:42 +00004904
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004905const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004906PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004907{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004908 if (PyMethod_Check(func))
4909 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4910 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004911 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004912 else if (PyCFunction_Check(func))
4913 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4914 else
4915 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004916}
4917
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004918const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004919PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004920{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004921 if (PyMethod_Check(func))
4922 return "()";
4923 else if (PyFunction_Check(func))
4924 return "()";
4925 else if (PyCFunction_Check(func))
4926 return "()";
4927 else
4928 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004929}
4930
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004931#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004932if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004933 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4934 tstate, tstate->frame, \
4935 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004936 x = NULL; \
4937 } \
4938 else { \
4939 x = call; \
4940 if (tstate->c_profilefunc != NULL) { \
4941 if (x == NULL) { \
4942 call_trace_protected(tstate->c_profilefunc, \
4943 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004944 tstate, tstate->frame, \
4945 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004946 /* XXX should pass (type, value, tb) */ \
4947 } else { \
4948 if (call_trace(tstate->c_profilefunc, \
4949 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004950 tstate, tstate->frame, \
4951 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004952 Py_DECREF(x); \
4953 x = NULL; \
4954 } \
4955 } \
4956 } \
4957 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004958} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004959 x = call; \
4960 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004961
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004962
4963static PyObject *
4964trace_call_function(PyThreadState *tstate,
4965 PyObject *func,
4966 PyObject **args, Py_ssize_t nargs,
4967 PyObject *kwnames)
4968{
4969 PyObject *x;
4970 if (PyCFunction_Check(func)) {
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02004971 C_TRACE(x, _PyObject_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004972 return x;
4973 }
4974 else if (Py_TYPE(func) == &PyMethodDescr_Type && nargs > 0) {
4975 /* We need to create a temporary bound method as argument
4976 for profiling.
4977
4978 If nargs == 0, then this cannot work because we have no
4979 "self". In any case, the call itself would raise
4980 TypeError (foo needs an argument), so we just skip
4981 profiling. */
4982 PyObject *self = args[0];
4983 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4984 if (func == NULL) {
4985 return NULL;
4986 }
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02004987 C_TRACE(x, _PyObject_Vectorcall(func,
4988 args+1, nargs-1,
4989 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004990 Py_DECREF(func);
4991 return x;
4992 }
4993 return _PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
4994}
4995
Victor Stinner415c5102017-01-11 00:54:57 +01004996/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4997 to reduce the stack consumption. */
4998Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Victor Stinner09532fe2019-05-10 23:39:09 +02004999call_function(PyThreadState *tstate, PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005000{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005001 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005002 PyObject *func = *pfunc;
5003 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07005004 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
5005 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09005006 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005007
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005008 if (tstate->use_tracing) {
5009 x = trace_call_function(tstate, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09005010 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01005011 else {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005012 x = _PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005013 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00005014
Victor Stinner438a12d2019-05-24 17:01:38 +02005015 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005016
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01005017 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005018 while ((*pp_stack) > pfunc) {
5019 w = EXT_POP(*pp_stack);
5020 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005021 }
Victor Stinnerace47d72013-07-18 01:41:08 +02005022
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005023 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005024}
5025
Jeremy Hylton52820442001-01-03 23:52:36 +00005026static PyObject *
Victor Stinner09532fe2019-05-10 23:39:09 +02005027do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005028{
jdemeyere89de732018-09-19 12:06:20 +02005029 PyObject *result;
5030
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005031 if (PyCFunction_Check(func)) {
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +02005032 C_TRACE(result, PyObject_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005033 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005034 }
jdemeyere89de732018-09-19 12:06:20 +02005035 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
jdemeyere89de732018-09-19 12:06:20 +02005036 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
5037 if (nargs > 0 && tstate->use_tracing) {
5038 /* We need to create a temporary bound method as argument
5039 for profiling.
5040
5041 If nargs == 0, then this cannot work because we have no
5042 "self". In any case, the call itself would raise
5043 TypeError (foo needs an argument), so we just skip
5044 profiling. */
5045 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
5046 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5047 if (func == NULL) {
5048 return NULL;
5049 }
5050
Victor Stinner4d231bc2019-11-14 13:36:21 +01005051 C_TRACE(result, _PyObject_FastCallDictTstate(
5052 tstate, func,
5053 &_PyTuple_ITEMS(callargs)[1],
5054 nargs - 1,
5055 kwdict));
jdemeyere89de732018-09-19 12:06:20 +02005056 Py_DECREF(func);
5057 return result;
5058 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005059 }
jdemeyere89de732018-09-19 12:06:20 +02005060 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00005061}
5062
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005063/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005064 nb_index slot defined, and store in *pi.
5065 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08005066 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00005067 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005068*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005069int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005070_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005071{
Victor Stinner438a12d2019-05-24 17:01:38 +02005072 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005073 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005074 Py_ssize_t x;
5075 if (PyIndex_Check(v)) {
5076 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005077 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005078 return 0;
5079 }
5080 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005081 _PyErr_SetString(tstate, PyExc_TypeError,
5082 "slice indices must be integers or "
5083 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005084 return 0;
5085 }
5086 *pi = x;
5087 }
5088 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005089}
5090
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005091int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005092_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005093{
Victor Stinner438a12d2019-05-24 17:01:38 +02005094 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005095 Py_ssize_t x;
5096 if (PyIndex_Check(v)) {
5097 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005098 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005099 return 0;
5100 }
5101 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005102 _PyErr_SetString(tstate, PyExc_TypeError,
5103 "slice indices must be integers or "
5104 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005105 return 0;
5106 }
5107 *pi = x;
5108 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005109}
5110
5111
Guido van Rossum486364b2007-06-30 05:01:58 +00005112#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005113 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00005114
Guido van Rossumb209a111997-04-29 18:18:01 +00005115static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005116cmp_outcome(PyThreadState *tstate, int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005117{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005118 int res = 0;
5119 switch (op) {
5120 case PyCmp_IS:
5121 res = (v == w);
5122 break;
5123 case PyCmp_IS_NOT:
5124 res = (v != w);
5125 break;
5126 case PyCmp_IN:
5127 res = PySequence_Contains(w, v);
5128 if (res < 0)
5129 return NULL;
5130 break;
5131 case PyCmp_NOT_IN:
5132 res = PySequence_Contains(w, v);
5133 if (res < 0)
5134 return NULL;
5135 res = !res;
5136 break;
5137 case PyCmp_EXC_MATCH:
5138 if (PyTuple_Check(w)) {
5139 Py_ssize_t i, length;
5140 length = PyTuple_Size(w);
5141 for (i = 0; i < length; i += 1) {
5142 PyObject *exc = PyTuple_GET_ITEM(w, i);
5143 if (!PyExceptionClass_Check(exc)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005144 _PyErr_SetString(tstate, PyExc_TypeError,
5145 CANNOT_CATCH_MSG);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005146 return NULL;
5147 }
5148 }
5149 }
5150 else {
5151 if (!PyExceptionClass_Check(w)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005152 _PyErr_SetString(tstate, PyExc_TypeError,
5153 CANNOT_CATCH_MSG);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005154 return NULL;
5155 }
5156 }
5157 res = PyErr_GivenExceptionMatches(v, w);
5158 break;
5159 default:
5160 return PyObject_RichCompare(v, w, op);
5161 }
5162 v = res ? Py_True : Py_False;
5163 Py_INCREF(v);
5164 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005165}
5166
Thomas Wouters52152252000-08-17 22:55:00 +00005167static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005168import_name(PyThreadState *tstate, PyFrameObject *f,
5169 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005170{
5171 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005172 PyObject *import_func, *res;
5173 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005174
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005175 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005176 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005177 if (!_PyErr_Occurred(tstate)) {
5178 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005179 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005180 return NULL;
5181 }
5182
5183 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005184 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005185 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02005186 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005187 return NULL;
5188 }
5189 res = PyImport_ImportModuleLevelObject(
5190 name,
5191 f->f_globals,
5192 f->f_locals == NULL ? Py_None : f->f_locals,
5193 fromlist,
5194 ilevel);
5195 return res;
5196 }
5197
5198 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005199
5200 stack[0] = name;
5201 stack[1] = f->f_globals;
5202 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5203 stack[3] = fromlist;
5204 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005205 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005206 Py_DECREF(import_func);
5207 return res;
5208}
5209
5210static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005211import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005212{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005213 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02005214 _Py_IDENTIFIER(__name__);
Xiang Zhang4830f582017-03-21 11:13:42 +08005215 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005216
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005217 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005218 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005219 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005220 /* Issue #17636: in case this failed because of a circular relative
5221 import, try to fallback on reading the module directly from
5222 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02005223 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005224 if (pkgname == NULL) {
5225 goto error;
5226 }
Oren Milman6db70332017-09-19 14:23:01 +03005227 if (!PyUnicode_Check(pkgname)) {
5228 Py_CLEAR(pkgname);
5229 goto error;
5230 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005231 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005232 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005233 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005234 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005235 }
Eric Snow3f9eee62017-09-15 16:35:20 -06005236 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005237 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02005238 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07005239 goto error;
5240 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005241 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005242 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005243 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005244 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005245 if (pkgname == NULL) {
5246 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5247 if (pkgname_or_unknown == NULL) {
5248 Py_XDECREF(pkgpath);
5249 return NULL;
5250 }
5251 } else {
5252 pkgname_or_unknown = pkgname;
5253 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005254
5255 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005256 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08005257 errmsg = PyUnicode_FromFormat(
5258 "cannot import name %R from %R (unknown location)",
5259 name, pkgname_or_unknown
5260 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005261 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005262 PyErr_SetImportError(errmsg, pkgname, NULL);
5263 }
5264 else {
Anthony Sottile65366bc2019-09-09 08:17:50 -07005265 _Py_IDENTIFIER(__spec__);
5266 PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
Anthony Sottile65366bc2019-09-09 08:17:50 -07005267 const char *fmt =
5268 _PyModuleSpec_IsInitializing(spec) ?
5269 "cannot import name %R from partially initialized module %R "
5270 "(most likely due to a circular import) (%S)" :
5271 "cannot import name %R from %R (%S)";
5272 Py_XDECREF(spec);
5273
5274 errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
Stefan Krah027b09c2019-03-25 21:50:58 +01005275 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005276 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005277 }
5278
Xiang Zhang4830f582017-03-21 11:13:42 +08005279 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005280 Py_XDECREF(pkgname_or_unknown);
5281 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005282 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005283}
Guido van Rossumac7be682001-01-17 15:42:30 +00005284
Thomas Wouters52152252000-08-17 22:55:00 +00005285static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005286import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00005287{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005288 _Py_IDENTIFIER(__all__);
5289 _Py_IDENTIFIER(__dict__);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005290 _Py_IDENTIFIER(__name__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005291 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005292 int skip_leading_underscores = 0;
5293 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005294
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005295 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5296 return -1; /* Unexpected error */
5297 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005298 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005299 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5300 return -1;
5301 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005302 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005303 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005304 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005305 return -1;
5306 }
5307 all = PyMapping_Keys(dict);
5308 Py_DECREF(dict);
5309 if (all == NULL)
5310 return -1;
5311 skip_leading_underscores = 1;
5312 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005314 for (pos = 0, err = 0; ; pos++) {
5315 name = PySequence_GetItem(all, pos);
5316 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005317 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005318 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02005319 }
5320 else {
5321 _PyErr_Clear(tstate);
5322 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005323 break;
5324 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005325 if (!PyUnicode_Check(name)) {
5326 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5327 if (modname == NULL) {
5328 Py_DECREF(name);
5329 err = -1;
5330 break;
5331 }
5332 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005333 _PyErr_Format(tstate, PyExc_TypeError,
5334 "module __name__ must be a string, not %.100s",
5335 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005336 }
5337 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005338 _PyErr_Format(tstate, PyExc_TypeError,
5339 "%s in %U.%s must be str, not %.100s",
5340 skip_leading_underscores ? "Key" : "Item",
5341 modname,
5342 skip_leading_underscores ? "__dict__" : "__all__",
5343 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005344 }
5345 Py_DECREF(modname);
5346 Py_DECREF(name);
5347 err = -1;
5348 break;
5349 }
5350 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005351 if (PyUnicode_READY(name) == -1) {
5352 Py_DECREF(name);
5353 err = -1;
5354 break;
5355 }
5356 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5357 Py_DECREF(name);
5358 continue;
5359 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005360 }
5361 value = PyObject_GetAttr(v, name);
5362 if (value == NULL)
5363 err = -1;
5364 else if (PyDict_CheckExact(locals))
5365 err = PyDict_SetItem(locals, name, value);
5366 else
5367 err = PyObject_SetItem(locals, name, value);
5368 Py_DECREF(name);
5369 Py_XDECREF(value);
5370 if (err != 0)
5371 break;
5372 }
5373 Py_DECREF(all);
5374 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005375}
5376
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005377static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005378check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005379{
5380 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005381 /* check_args_iterable() may be called with a live exception:
5382 * clear it to prevent calling _PyObject_FunctionStr() with an
5383 * exception set. */
5384 PyErr_Clear();
5385 PyObject *funcstr = _PyObject_FunctionStr(func);
5386 if (funcstr != NULL) {
5387 _PyErr_Format(tstate, PyExc_TypeError,
5388 "%U argument after * must be an iterable, not %.200s",
5389 funcstr, Py_TYPE(args)->tp_name);
5390 Py_DECREF(funcstr);
5391 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005392 return -1;
5393 }
5394 return 0;
5395}
5396
5397static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005398format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005399{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005400 /* _PyDict_MergeEx raises attribute
5401 * error (percolated from an attempt
5402 * to get 'keys' attribute) instead of
5403 * a type error if its second argument
5404 * is not a mapping.
5405 */
Victor Stinner438a12d2019-05-24 17:01:38 +02005406 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005407 PyErr_Clear();
5408 PyObject *funcstr = _PyObject_FunctionStr(func);
5409 if (funcstr != NULL) {
5410 _PyErr_Format(
5411 tstate, PyExc_TypeError,
5412 "%U argument after ** must be a mapping, not %.200s",
5413 funcstr, Py_TYPE(kwargs)->tp_name);
5414 Py_DECREF(funcstr);
5415 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005416 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005417 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005418 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02005419 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005420 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005421 PyErr_Clear();
5422 PyObject *funcstr = _PyObject_FunctionStr(func);
5423 if (funcstr != NULL) {
5424 PyObject *key = PyTuple_GET_ITEM(val, 0);
5425 _PyErr_Format(
5426 tstate, PyExc_TypeError,
5427 "%U got multiple values for keyword argument '%S'",
5428 funcstr, key);
5429 Py_DECREF(funcstr);
5430 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005431 Py_XDECREF(exc);
5432 Py_XDECREF(val);
5433 Py_XDECREF(tb);
5434 }
5435 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005436 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005437 }
5438 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005439}
5440
Guido van Rossumac7be682001-01-17 15:42:30 +00005441static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005442format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
5443 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005444{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005445 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005447 if (!obj)
5448 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005449
Serhiy Storchaka06515832016-11-20 09:13:07 +02005450 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005451 if (!obj_str)
5452 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005453
Victor Stinner438a12d2019-05-24 17:01:38 +02005454 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005455}
Guido van Rossum950361c1997-01-24 13:49:28 +00005456
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005457static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005458format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005459{
5460 PyObject *name;
5461 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02005462 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005463 return;
5464 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5465 name = PyTuple_GET_ITEM(co->co_cellvars,
5466 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005467 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005468 PyExc_UnboundLocalError,
5469 UNBOUNDLOCAL_ERROR_MSG,
5470 name);
5471 } else {
5472 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5473 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02005474 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005475 UNBOUNDFREE_ERROR_MSG, name);
5476 }
5477}
5478
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005479static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005480format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005481{
5482 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5483 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005484 _PyErr_Format(tstate, PyExc_TypeError,
5485 "'async with' received an object from __aenter__ "
5486 "that does not implement __await__: %.100s",
5487 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005488 }
5489 else if (prevopcode == WITH_CLEANUP_START) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005490 _PyErr_Format(tstate, PyExc_TypeError,
5491 "'async with' received an object from __aexit__ "
5492 "that does not implement __await__: %.100s",
5493 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005494 }
5495 }
5496}
5497
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005498static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005499unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005500 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005501{
5502 PyObject *res;
5503 if (Py_REFCNT(v) == 2) {
5504 /* In the common case, there are 2 references to the value
5505 * stored in 'variable' when the += is performed: one on the
5506 * value stack (in 'v') and one still stored in the
5507 * 'variable'. We try to delete the variable now to reduce
5508 * the refcnt to 1.
5509 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005510 int opcode, oparg;
5511 NEXTOPARG();
5512 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005513 case STORE_FAST:
5514 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005515 PyObject **fastlocals = f->f_localsplus;
5516 if (GETLOCAL(oparg) == v)
5517 SETLOCAL(oparg, NULL);
5518 break;
5519 }
5520 case STORE_DEREF:
5521 {
5522 PyObject **freevars = (f->f_localsplus +
5523 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005524 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005525 if (PyCell_GET(c) == v) {
5526 PyCell_SET(c, NULL);
5527 Py_DECREF(v);
5528 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005529 break;
5530 }
5531 case STORE_NAME:
5532 {
5533 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005534 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005535 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005536 if (locals && PyDict_CheckExact(locals)) {
5537 PyObject *w = PyDict_GetItemWithError(locals, name);
5538 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02005539 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005540 {
5541 Py_DECREF(v);
5542 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005543 }
5544 }
5545 break;
5546 }
5547 }
5548 }
5549 res = v;
5550 PyUnicode_Append(&res, w);
5551 return res;
5552}
5553
Guido van Rossum950361c1997-01-24 13:49:28 +00005554#ifdef DYNAMIC_EXECUTION_PROFILE
5555
Skip Montanarof118cb12001-10-15 20:51:38 +00005556static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005557getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005558{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005559 int i;
5560 PyObject *l = PyList_New(256);
5561 if (l == NULL) return NULL;
5562 for (i = 0; i < 256; i++) {
5563 PyObject *x = PyLong_FromLong(a[i]);
5564 if (x == NULL) {
5565 Py_DECREF(l);
5566 return NULL;
5567 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005568 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005569 }
5570 for (i = 0; i < 256; i++)
5571 a[i] = 0;
5572 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005573}
5574
5575PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005576_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005577{
5578#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005579 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005580#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005581 int i;
5582 PyObject *l = PyList_New(257);
5583 if (l == NULL) return NULL;
5584 for (i = 0; i < 257; i++) {
5585 PyObject *x = getarray(dxpairs[i]);
5586 if (x == NULL) {
5587 Py_DECREF(l);
5588 return NULL;
5589 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005590 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005591 }
5592 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005593#endif
5594}
5595
5596#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005597
5598Py_ssize_t
5599_PyEval_RequestCodeExtraIndex(freefunc free)
5600{
Victor Stinnercaba55b2018-08-03 15:33:52 +02005601 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -07005602 Py_ssize_t new_index;
5603
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005604 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005605 return -1;
5606 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005607 new_index = interp->co_extra_user_count++;
5608 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005609 return new_index;
5610}
Łukasz Langaa785c872016-09-09 17:37:37 -07005611
5612static void
5613dtrace_function_entry(PyFrameObject *f)
5614{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005615 const char *filename;
5616 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005617 int lineno;
5618
5619 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5620 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5621 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5622
5623 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5624}
5625
5626static void
5627dtrace_function_return(PyFrameObject *f)
5628{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005629 const char *filename;
5630 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005631 int lineno;
5632
5633 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5634 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5635 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5636
5637 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5638}
5639
5640/* DTrace equivalent of maybe_call_line_trace. */
5641static void
5642maybe_dtrace_line(PyFrameObject *frame,
5643 int *instr_lb, int *instr_ub, int *instr_prev)
5644{
5645 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005646 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005647
5648 /* If the last instruction executed isn't in the current
5649 instruction window, reset the window.
5650 */
5651 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5652 PyAddrPair bounds;
5653 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5654 &bounds);
5655 *instr_lb = bounds.ap_lower;
5656 *instr_ub = bounds.ap_upper;
5657 }
5658 /* If the last instruction falls at the start of a line or if
5659 it represents a jump backwards, update the frame's line
5660 number and call the trace function. */
5661 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5662 frame->f_lineno = line;
5663 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5664 if (!co_filename)
5665 co_filename = "?";
5666 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5667 if (!co_name)
5668 co_name = "?";
5669 PyDTrace_LINE(co_filename, co_name, line);
5670 }
5671 *instr_prev = frame->f_lasti;
5672}
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005673
5674
5675/* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions
5676 for the limited API. */
5677
5678#undef Py_EnterRecursiveCall
5679
5680int Py_EnterRecursiveCall(const char *where)
5681{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005682 return _Py_EnterRecursiveCall_inline(where);
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005683}
5684
5685#undef Py_LeaveRecursiveCall
5686
5687void Py_LeaveRecursiveCall(void)
5688{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005689 _Py_LeaveRecursiveCall_inline();
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005690}