blob: 9f4b43615e28f2f509dad54557765108e31f2af9 [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 Stinner09532fe2019-05-10 23:39:09 +0200194 return gil_created(&_PyRuntime.ceval.gil);
Tim Peters7f468f22004-10-11 02:40:51 +0000195}
196
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000197void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000198PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000199{
Victor Stinner09532fe2019-05-10 23:39:09 +0200200 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200201 struct _ceval_runtime_state *ceval = &runtime->ceval;
202 struct _gil_runtime_state *gil = &ceval->gil;
Victor Stinner09532fe2019-05-10 23:39:09 +0200203 if (gil_created(gil)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 return;
Victor Stinnera7126792019-03-19 14:19:38 +0100205 }
206
Inada Naoki001fee12019-02-20 10:00:09 +0900207 PyThread_init_thread();
Victor Stinner09532fe2019-05-10 23:39:09 +0200208 create_gil(gil);
209 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinnere225beb2019-06-03 18:14:24 +0200210 take_gil(ceval, tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700211
Victor Stinnere225beb2019-06-03 18:14:24 +0200212 struct _pending_calls *pending = &ceval->pending;
213 pending->lock = PyThread_allocate_lock();
214 if (pending->lock == NULL) {
215 Py_FatalError("Can't initialize threads for pending calls");
216 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000217}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000218
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000219void
Victor Stinnere225beb2019-06-03 18:14:24 +0200220_PyEval_FiniThreads(struct _ceval_runtime_state *ceval)
Antoine Pitrou1df15362010-09-13 14:16:46 +0000221{
Victor Stinnere225beb2019-06-03 18:14:24 +0200222 struct _gil_runtime_state *gil = &ceval->gil;
Victor Stinner09532fe2019-05-10 23:39:09 +0200223 if (!gil_created(gil)) {
Antoine Pitrou1df15362010-09-13 14:16:46 +0000224 return;
Victor Stinnera7126792019-03-19 14:19:38 +0100225 }
226
Victor Stinner09532fe2019-05-10 23:39:09 +0200227 destroy_gil(gil);
228 assert(!gil_created(gil));
Victor Stinner99fcc612019-04-29 13:04:07 +0200229
Victor Stinnere225beb2019-06-03 18:14:24 +0200230 struct _pending_calls *pending = &ceval->pending;
231 if (pending->lock != NULL) {
232 PyThread_free_lock(pending->lock);
233 pending->lock = NULL;
234 }
Antoine Pitrou1df15362010-09-13 14:16:46 +0000235}
236
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400237static inline void
Victor Stinner0fd2c302019-06-04 03:15:09 +0200238exit_thread_if_finalizing(_PyRuntimeState *runtime, PyThreadState *tstate)
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400239{
Victor Stinnere225beb2019-06-03 18:14:24 +0200240 /* _Py_Finalizing is protected by the GIL */
Victor Stinner09532fe2019-05-10 23:39:09 +0200241 if (runtime->finalizing != NULL && !_Py_CURRENTLY_FINALIZING(runtime, tstate)) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200242 drop_gil(&runtime->ceval, tstate);
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400243 PyThread_exit_thread();
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400244 }
245}
246
Antoine Pitrou1df15362010-09-13 14:16:46 +0000247void
Inada Naoki91234a12019-06-03 21:30:58 +0900248_PyEval_Fini(void)
249{
250#if OPCACHE_STATS
251 fprintf(stderr, "-- Opcode cache number of objects = %zd\n",
252 opcache_code_objects);
253
254 fprintf(stderr, "-- Opcode cache total extra mem = %zd\n",
255 opcache_code_objects_extra_mem);
256
257 fprintf(stderr, "\n");
258
259 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL hits = %zd (%d%%)\n",
260 opcache_global_hits,
261 (int) (100.0 * opcache_global_hits /
262 (opcache_global_hits + opcache_global_misses)));
263
264 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL misses = %zd (%d%%)\n",
265 opcache_global_misses,
266 (int) (100.0 * opcache_global_misses /
267 (opcache_global_hits + opcache_global_misses)));
268
269 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL opts = %zd\n",
270 opcache_global_opts);
271
272 fprintf(stderr, "\n");
273#endif
274}
275
276void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000277PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000278{
Victor Stinner09532fe2019-05-10 23:39:09 +0200279 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200280 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinner09532fe2019-05-10 23:39:09 +0200281 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
282 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
Victor Stinner09532fe2019-05-10 23:39:09 +0200284 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200285 take_gil(ceval, tstate);
Victor Stinner0fd2c302019-06-04 03:15:09 +0200286 exit_thread_if_finalizing(runtime, tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000287}
288
289void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000290PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000291{
Victor Stinner09532fe2019-05-10 23:39:09 +0200292 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200293 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000294 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100295 We therefore avoid PyThreadState_Get() which dumps a fatal error
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 in debug mode.
297 */
Victor Stinnere225beb2019-06-03 18:14:24 +0200298 drop_gil(&runtime->ceval, tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000299}
300
301void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000302PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000303{
Victor Stinner09532fe2019-05-10 23:39:09 +0200304 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200306 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200307
Victor Stinner0fd2c302019-06-04 03:15:09 +0200308 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200309 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinner09532fe2019-05-10 23:39:09 +0200310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 /* Check someone has called PyEval_InitThreads() to create the lock */
Victor Stinnere225beb2019-06-03 18:14:24 +0200312 assert(gil_created(&ceval->gil));
313 take_gil(ceval, tstate);
Victor Stinner0fd2c302019-06-04 03:15:09 +0200314 exit_thread_if_finalizing(runtime, tstate);
Victor Stinner09532fe2019-05-10 23:39:09 +0200315 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
316 Py_FatalError("PyEval_AcquireThread: non-NULL old thread state");
317 }
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000318}
319
320void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000321PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000322{
Victor Stinner09532fe2019-05-10 23:39:09 +0200323 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200325 }
326
Victor Stinner0fd2c302019-06-04 03:15:09 +0200327 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200328 PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
329 if (new_tstate != tstate) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200331 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200332 drop_gil(&runtime->ceval, tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000333}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000334
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200335/* This function is called from PyOS_AfterFork_Child to destroy all threads
336 * which are not running in the child process, and clear internal locks
337 * which might be held by those threads.
338 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000339
340void
Victor Stinnerd5d9e812019-05-13 12:35:37 +0200341_PyEval_ReInitThreads(_PyRuntimeState *runtime)
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000342{
Victor Stinnere225beb2019-06-03 18:14:24 +0200343 struct _ceval_runtime_state *ceval = &runtime->ceval;
344 if (!gil_created(&ceval->gil)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 return;
Victor Stinner09532fe2019-05-10 23:39:09 +0200346 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200347 recreate_gil(&ceval->gil);
Victor Stinner09532fe2019-05-10 23:39:09 +0200348 PyThreadState *current_tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinnere225beb2019-06-03 18:14:24 +0200349 take_gil(ceval, current_tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700350
Victor Stinnere225beb2019-06-03 18:14:24 +0200351 struct _pending_calls *pending = &ceval->pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200352 pending->lock = PyThread_allocate_lock();
353 if (pending->lock == NULL) {
Eric Snow8479a342019-03-08 23:44:33 -0700354 Py_FatalError("Can't initialize threads for pending calls");
355 }
Jesse Nollera8513972008-07-17 16:49:17 +0000356
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200357 /* Destroy all threads except the current one */
Victor Stinner0fd2c302019-06-04 03:15:09 +0200358 _PyThreadState_DeleteExcept(runtime, current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000359}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000360
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000361/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600362 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000363
364void
Victor Stinnere225beb2019-06-03 18:14:24 +0200365_PyEval_SignalAsyncExc(struct _ceval_runtime_state *ceval)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000366{
Victor Stinnere225beb2019-06-03 18:14:24 +0200367 SIGNAL_ASYNC_EXC(ceval);
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000368}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000369
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000370PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000371PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000372{
Victor Stinner09532fe2019-05-10 23:39:09 +0200373 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200374 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinner09532fe2019-05-10 23:39:09 +0200375 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
376 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 Py_FatalError("PyEval_SaveThread: NULL tstate");
Victor Stinner09532fe2019-05-10 23:39:09 +0200378 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200379 assert(gil_created(&ceval->gil));
380 drop_gil(ceval, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000381 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000382}
383
384void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000385PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000386{
Victor Stinner0fd2c302019-06-04 03:15:09 +0200387 _PyRuntimeState *runtime = &_PyRuntime;
388 struct _ceval_runtime_state *ceval = &runtime->ceval;
389
Victor Stinner09532fe2019-05-10 23:39:09 +0200390 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Victor Stinner09532fe2019-05-10 23:39:09 +0200392 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200393 assert(gil_created(&ceval->gil));
Victor Stinner2914bb32018-01-29 11:57:45 +0100394
395 int err = errno;
Victor Stinnere225beb2019-06-03 18:14:24 +0200396 take_gil(ceval, tstate);
Victor Stinner0fd2c302019-06-04 03:15:09 +0200397 exit_thread_if_finalizing(runtime, tstate);
Victor Stinner2914bb32018-01-29 11:57:45 +0100398 errno = err;
399
Victor Stinner09532fe2019-05-10 23:39:09 +0200400 _PyThreadState_Swap(&runtime->gilstate, tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000401}
402
403
Guido van Rossuma9672091994-09-14 13:31:22 +0000404/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
405 signal handlers or Mac I/O completion routines) can schedule calls
406 to a function to be called synchronously.
407 The synchronous function is called with one void* argument.
408 It should return 0 for success or -1 for failure -- failure should
409 be accompanied by an exception.
410
411 If registry succeeds, the registry function returns 0; if it fails
412 (e.g. due to too many pending calls) it returns -1 (without setting
413 an exception condition).
414
415 Note that because registry may occur from within signal handlers,
416 or other asynchronous events, calling malloc() is unsafe!
417
Guido van Rossuma9672091994-09-14 13:31:22 +0000418 Any thread can schedule pending calls, but only the main thread
419 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000420 There is no facility to schedule calls to a particular thread, but
421 that should be easy to change, should that ever be required. In
422 that case, the static variables here should go into the python
423 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000424*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000425
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200426void
Victor Stinnere225beb2019-06-03 18:14:24 +0200427_PyEval_SignalReceived(struct _ceval_runtime_state *ceval)
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200428{
429 /* bpo-30703: Function called when the C signal handler of Python gets a
430 signal. We cannot queue a callback using Py_AddPendingCall() since
431 that function is not async-signal-safe. */
Victor Stinnere225beb2019-06-03 18:14:24 +0200432 SIGNAL_PENDING_SIGNALS(ceval);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200433}
434
Eric Snow5be45a62019-03-08 22:47:07 -0700435/* Push one item onto the queue while holding the lock. */
436static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200437_push_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600438 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700439{
Eric Snow842a2f02019-03-15 15:47:51 -0600440 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700441 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600442 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700443 return -1; /* Queue full */
444 }
Eric Snow842a2f02019-03-15 15:47:51 -0600445 pending->calls[i].func = func;
446 pending->calls[i].arg = arg;
447 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700448 return 0;
449}
450
451/* Pop one item off the queue while holding the lock. */
452static void
Victor Stinnere225beb2019-06-03 18:14:24 +0200453_pop_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600454 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700455{
Eric Snow842a2f02019-03-15 15:47:51 -0600456 int i = pending->first;
457 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700458 return; /* Queue empty */
459 }
460
Eric Snow842a2f02019-03-15 15:47:51 -0600461 *func = pending->calls[i].func;
462 *arg = pending->calls[i].arg;
463 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700464}
465
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200466/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000467 scheduling to be made from any thread, and even from an executing
468 callback.
469 */
470
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000471int
Victor Stinner438a12d2019-05-24 17:01:38 +0200472_PyEval_AddPendingCall(PyThreadState *tstate,
Victor Stinnere225beb2019-06-03 18:14:24 +0200473 struct _ceval_runtime_state *ceval,
Victor Stinner09532fe2019-05-10 23:39:09 +0200474 int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000475{
Victor Stinnere225beb2019-06-03 18:14:24 +0200476 struct _pending_calls *pending = &ceval->pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600477
478 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
479 if (pending->finishing) {
480 PyThread_release_lock(pending->lock);
481
482 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +0200483 _PyErr_Fetch(tstate, &exc, &val, &tb);
484 _PyErr_SetString(tstate, PyExc_SystemError,
Eric Snow842a2f02019-03-15 15:47:51 -0600485 "Py_AddPendingCall: cannot add pending calls "
486 "(Python shutting down)");
Victor Stinner438a12d2019-05-24 17:01:38 +0200487 _PyErr_Print(tstate);
488 _PyErr_Restore(tstate, exc, val, tb);
Eric Snow842a2f02019-03-15 15:47:51 -0600489 return -1;
490 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200491 int result = _push_pending_call(pending, func, arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600492 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700493
Victor Stinnere225beb2019-06-03 18:14:24 +0200494 /* signal main loop */
495 SIGNAL_PENDING_CALLS(ceval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000497}
498
Victor Stinner09532fe2019-05-10 23:39:09 +0200499int
500Py_AddPendingCall(int (*func)(void *), void *arg)
501{
Victor Stinner438a12d2019-05-24 17:01:38 +0200502 _PyRuntimeState *runtime = &_PyRuntime;
503 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinnere225beb2019-06-03 18:14:24 +0200504 return _PyEval_AddPendingCall(tstate, &runtime->ceval, func, arg);
Victor Stinner09532fe2019-05-10 23:39:09 +0200505}
506
Eric Snowfdf282d2019-01-11 14:26:55 -0700507static int
Victor Stinner09532fe2019-05-10 23:39:09 +0200508handle_signals(_PyRuntimeState *runtime)
Eric Snowfdf282d2019-01-11 14:26:55 -0700509{
Eric Snow5be45a62019-03-08 22:47:07 -0700510 /* Only handle signals on main thread. PyEval_InitThreads must
511 * have been called already.
512 */
Victor Stinner09532fe2019-05-10 23:39:09 +0200513 if (PyThread_get_thread_ident() != runtime->main_thread) {
Eric Snowfdf282d2019-01-11 14:26:55 -0700514 return 0;
515 }
Eric Snow64d6cc82019-02-23 15:40:43 -0700516 /*
517 * Ensure that the thread isn't currently running some other
518 * interpreter.
519 */
Victor Stinner09532fe2019-05-10 23:39:09 +0200520 PyInterpreterState *interp = _PyRuntimeState_GetThreadState(runtime)->interp;
521 if (interp != runtime->interpreters.main) {
Eric Snow64d6cc82019-02-23 15:40:43 -0700522 return 0;
523 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700524
Victor Stinnere225beb2019-06-03 18:14:24 +0200525 struct _ceval_runtime_state *ceval = &runtime->ceval;
526 UNSIGNAL_PENDING_SIGNALS(ceval);
Eric Snow64d6cc82019-02-23 15:40:43 -0700527 if (_PyErr_CheckSignals() < 0) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200528 SIGNAL_PENDING_SIGNALS(ceval); /* We're not done yet */
Eric Snowfdf282d2019-01-11 14:26:55 -0700529 return -1;
530 }
531 return 0;
532}
533
534static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200535make_pending_calls(_PyRuntimeState *runtime)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000536{
Eric Snow6a150bc2019-06-01 15:39:46 -0600537 static int busy = 0;
Eric Snowb75b1a352019-04-12 10:20:10 -0600538
Victor Stinnere225beb2019-06-03 18:14:24 +0200539 /* only service pending calls on main thread */
540 if (PyThread_get_thread_ident() != runtime->main_thread) {
541 return 0;
542 }
543
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 /* don't perform recursive pending calls */
Eric Snowfdf282d2019-01-11 14:26:55 -0700545 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700547 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200548 busy = 1;
Victor Stinnere225beb2019-06-03 18:14:24 +0200549 struct _ceval_runtime_state *ceval = &runtime->ceval;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200550 /* unsignal before starting to call callbacks, so that any callback
551 added in-between re-signals */
Victor Stinnere225beb2019-06-03 18:14:24 +0200552 UNSIGNAL_PENDING_CALLS(ceval);
Eric Snowfdf282d2019-01-11 14:26:55 -0700553 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 /* perform a bounded number of calls, in case of recursion */
Victor Stinnere225beb2019-06-03 18:14:24 +0200556 struct _pending_calls *pending = &ceval->pending;
Eric Snowfdf282d2019-01-11 14:26:55 -0700557 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700558 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 void *arg = NULL;
560
561 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600562 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200563 _pop_pending_call(pending, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600564 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700565
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100566 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700567 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100568 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700569 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700570 res = func(arg);
571 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200572 goto error;
573 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200575
Charles-François Natalif23339a2011-07-23 18:15:43 +0200576 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700577 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200578
579error:
580 busy = 0;
Victor Stinnere225beb2019-06-03 18:14:24 +0200581 SIGNAL_PENDING_CALLS(ceval);
Eric Snowfdf282d2019-01-11 14:26:55 -0700582 return res;
583}
584
Eric Snow842a2f02019-03-15 15:47:51 -0600585void
Victor Stinnere225beb2019-06-03 18:14:24 +0200586_Py_FinishPendingCalls(_PyRuntimeState *runtime)
Eric Snow842a2f02019-03-15 15:47:51 -0600587{
Eric Snow842a2f02019-03-15 15:47:51 -0600588 assert(PyGILState_Check());
589
Victor Stinnere225beb2019-06-03 18:14:24 +0200590 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
591 struct _pending_calls *pending = &runtime->ceval.pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200592
Eric Snow842a2f02019-03-15 15:47:51 -0600593 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
594 pending->finishing = 1;
595 PyThread_release_lock(pending->lock);
596
597 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
598 return;
599 }
600
Victor Stinnere225beb2019-06-03 18:14:24 +0200601 if (make_pending_calls(runtime) < 0) {
602 PyObject *exc, *val, *tb;
603 _PyErr_Fetch(tstate, &exc, &val, &tb);
604 PyErr_BadInternalCall();
605 _PyErr_ChainExceptions(exc, val, tb);
606 _PyErr_Print(tstate);
Eric Snow842a2f02019-03-15 15:47:51 -0600607 }
608}
609
Eric Snowfdf282d2019-01-11 14:26:55 -0700610/* Py_MakePendingCalls() is a simple wrapper for the sake
611 of backward-compatibility. */
612int
613Py_MakePendingCalls(void)
614{
615 assert(PyGILState_Check());
616
617 /* Python signal handler doesn't really queue a callback: it only signals
618 that a signal was received, see _PyEval_SignalReceived(). */
Victor Stinner09532fe2019-05-10 23:39:09 +0200619 _PyRuntimeState *runtime = &_PyRuntime;
620 int res = handle_signals(runtime);
Eric Snowfdf282d2019-01-11 14:26:55 -0700621 if (res != 0) {
622 return res;
623 }
624
Victor Stinnere225beb2019-06-03 18:14:24 +0200625 res = make_pending_calls(runtime);
Eric Snowb75b1a352019-04-12 10:20:10 -0600626 if (res != 0) {
627 return res;
628 }
629
630 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000631}
632
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000633/* The interpreter's recursion limit */
634
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000635#ifndef Py_DEFAULT_RECURSION_LIMIT
636#define Py_DEFAULT_RECURSION_LIMIT 1000
637#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600638
Eric Snow05351c12017-09-05 21:43:08 -0700639int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000640
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600641void
Victor Stinnere225beb2019-06-03 18:14:24 +0200642_PyEval_Initialize(struct _ceval_runtime_state *state)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600643{
Victor Stinnere225beb2019-06-03 18:14:24 +0200644 state->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600645 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Victor Stinnere225beb2019-06-03 18:14:24 +0200646 _gil_initialize(&state->gil);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600647}
648
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000649int
650Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000651{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600652 return _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000653}
654
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000655void
656Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000657{
Victor Stinnere225beb2019-06-03 18:14:24 +0200658 struct _ceval_runtime_state *ceval = &_PyRuntime.ceval;
659 ceval->recursion_limit = new_limit;
660 _Py_CheckRecursionLimit = ceval->recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000661}
662
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100663/* The function _Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
Armin Rigo2b3eb402003-10-28 12:05:48 +0000664 if the recursion_depth reaches _Py_CheckRecursionLimit.
665 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
666 to guarantee that _Py_CheckRecursiveCall() is regularly called.
667 Without USE_STACKCHECK, there is no need for this. */
668int
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100669_Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000670{
Victor Stinner09532fe2019-05-10 23:39:09 +0200671 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200672 int recursion_limit = runtime->ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000673
674#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700675 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 if (PyOS_CheckStack()) {
677 --tstate->recursion_depth;
Victor Stinner438a12d2019-05-24 17:01:38 +0200678 _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 return -1;
680 }
pdox18967932017-10-25 23:03:01 -0700681 /* Needed for ABI backwards-compatibility (see bpo-31857) */
Eric Snow05351c12017-09-05 21:43:08 -0700682 _Py_CheckRecursionLimit = recursion_limit;
pdox18967932017-10-25 23:03:01 -0700683#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 if (tstate->recursion_critical)
685 /* Somebody asked that we don't check for recursion. */
686 return 0;
687 if (tstate->overflowed) {
688 if (tstate->recursion_depth > recursion_limit + 50) {
689 /* Overflowing while handling an overflow. Give up. */
690 Py_FatalError("Cannot recover from stack overflow.");
691 }
692 return 0;
693 }
694 if (tstate->recursion_depth > recursion_limit) {
695 --tstate->recursion_depth;
696 tstate->overflowed = 1;
Victor Stinner438a12d2019-05-24 17:01:38 +0200697 _PyErr_Format(tstate, PyExc_RecursionError,
698 "maximum recursion depth exceeded%s",
699 where);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 return -1;
701 }
702 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000703}
704
Victor Stinner09532fe2019-05-10 23:39:09 +0200705static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
Victor Stinner438a12d2019-05-24 17:01:38 +0200706static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000707
Victor Stinnere225beb2019-06-03 18:14:24 +0200708#define _Py_TracingPossible(ceval) ((ceval)->tracing_possible)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000709
Guido van Rossum374a9221991-04-04 10:40:29 +0000710
Guido van Rossumb209a111997-04-29 18:18:01 +0000711PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000712PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000713{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 return PyEval_EvalCodeEx(co,
715 globals, locals,
716 (PyObject **)NULL, 0,
717 (PyObject **)NULL, 0,
718 (PyObject **)NULL, 0,
719 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000720}
721
722
723/* Interpreter main loop */
724
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000725PyObject *
Victor Stinnerb9e68122019-11-14 12:20:46 +0100726PyEval_EvalFrame(PyFrameObject *f)
727{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 /* This is for backward compatibility with extension modules that
729 used this API; core interpreter code should call
730 PyEval_EvalFrameEx() */
Victor Stinnerb9e68122019-11-14 12:20:46 +0100731 PyThreadState *tstate = _PyThreadState_GET();
732 return _PyEval_EvalFrame(tstate, f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000733}
734
735PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000736PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000737{
Victor Stinnerb9e68122019-11-14 12:20:46 +0100738 PyThreadState *tstate = _PyThreadState_GET();
739 return _PyEval_EvalFrame(tstate, f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700740}
741
Victor Stinnerc6944e72016-11-11 02:13:35 +0100742PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700743_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
744{
Guido van Rossum950361c1997-01-24 13:49:28 +0000745#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000746 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000747#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200748 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300749 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200750 int opcode; /* Current opcode */
751 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200752 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 PyObject *retval = NULL; /* Return value */
Victor Stinner09532fe2019-05-10 23:39:09 +0200754 _PyRuntimeState * const runtime = &_PyRuntime;
755 PyThreadState * const tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinnere225beb2019-06-03 18:14:24 +0200756 struct _ceval_runtime_state * const ceval = &runtime->ceval;
757 _Py_atomic_int * const eval_breaker = &ceval->eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000759
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 is true when the line being executed has changed. The
765 initial values are such as to make this false the first
766 time it is tested. */
767 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000768
Serhiy Storchakaab874002016-09-11 13:48:15 +0300769 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 PyObject *names;
771 PyObject *consts;
Inada Naoki91234a12019-06-03 21:30:58 +0900772 _PyOpcache *co_opcache;
Guido van Rossum374a9221991-04-04 10:40:29 +0000773
Brett Cannon368b4b72012-04-02 12:17:59 -0400774#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200775 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400776#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200777
Antoine Pitroub52ec782009-01-25 16:34:23 +0000778/* Computed GOTOs, or
779 the-optimization-commonly-but-improperly-known-as-"threaded code"
780 using gcc's labels-as-values extension
781 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
782
783 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000785 combined with a lookup table of jump addresses. However, since the
786 indirect jump instruction is shared by all opcodes, the CPU will have a
787 hard time making the right prediction for where to jump next (actually,
788 it will be always wrong except in the uncommon case of a sequence of
789 several identical opcodes).
790
791 "Threaded code" in contrast, uses an explicit jump table and an explicit
792 indirect jump instruction at the end of each opcode. Since the jump
793 instruction is at a different address for each opcode, the CPU will make a
794 separate prediction for each of these instructions, which is equivalent to
795 predicting the second opcode of each opcode pair. These predictions have
796 a much better chance to turn out valid, especially in small bytecode loops.
797
798 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000800 and potentially many more instructions (depending on the pipeline width).
801 A correctly predicted branch, however, is nearly free.
802
803 At the time of this writing, the "threaded code" version is up to 15-20%
804 faster than the normal "switch" version, depending on the compiler and the
805 CPU architecture.
806
807 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
808 because it would render the measurements invalid.
809
810
811 NOTE: care must be taken that the compiler doesn't try to "optimize" the
812 indirect jumps by sharing them between all opcodes. Such optimizations
813 can be disabled on gcc by using the -fno-gcse flag (or possibly
814 -fno-crossjumping).
815*/
816
Antoine Pitrou042b1282010-08-13 21:15:58 +0000817#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000818#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000819#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000820#endif
821
Antoine Pitrou042b1282010-08-13 21:15:58 +0000822#ifdef HAVE_COMPUTED_GOTOS
823 #ifndef USE_COMPUTED_GOTOS
824 #define USE_COMPUTED_GOTOS 1
825 #endif
826#else
827 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
828 #error "Computed gotos are not supported on this compiler."
829 #endif
830 #undef USE_COMPUTED_GOTOS
831 #define USE_COMPUTED_GOTOS 0
832#endif
833
834#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000835/* Import the static jump table */
836#include "opcode_targets.h"
837
Antoine Pitroub52ec782009-01-25 16:34:23 +0000838#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -0700839 op: \
840 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000841
Antoine Pitroub52ec782009-01-25 16:34:23 +0000842#ifdef LLTRACE
843#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200845 if (!lltrace && !_Py_TracingPossible(ceval) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300847 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300848 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 } \
850 goto fast_next_opcode; \
851 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000852#else
853#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200855 if (!_Py_TracingPossible(ceval) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300857 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300858 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 } \
860 goto fast_next_opcode; \
861 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000862#endif
863
Victor Stinner09532fe2019-05-10 23:39:09 +0200864#define DISPATCH() \
865 { \
866 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
867 FAST_DISPATCH(); \
868 } \
869 continue; \
870 }
871
Antoine Pitroub52ec782009-01-25 16:34:23 +0000872#else
Benjamin Petersonddd19492018-09-16 22:38:02 -0700873#define TARGET(op) op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000874#define FAST_DISPATCH() goto fast_next_opcode
Victor Stinner09532fe2019-05-10 23:39:09 +0200875#define DISPATCH() continue
Antoine Pitroub52ec782009-01-25 16:34:23 +0000876#endif
877
878
Neal Norwitza81d2202002-07-14 00:27:26 +0000879/* Tuple access macros */
880
881#ifndef Py_DEBUG
882#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
883#else
884#define GETITEM(v, i) PyTuple_GetItem((v), (i))
885#endif
886
Guido van Rossum374a9221991-04-04 10:40:29 +0000887/* Code access macros */
888
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300889/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600890#define INSTR_OFFSET() \
891 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300892#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300893 _Py_CODEUNIT word = *next_instr; \
894 opcode = _Py_OPCODE(word); \
895 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300896 next_instr++; \
897 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300898#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
899#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000900
Raymond Hettingerf606f872003-03-16 03:11:04 +0000901/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 Some opcodes tend to come in pairs thus making it possible to
903 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300904 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000905
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 Verifying the prediction costs a single high-speed test of a register
907 variable against a constant. If the pairing was good, then the
908 processor's own internal branch predication has a high likelihood of
909 success, resulting in a nearly zero-overhead transition to the
910 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300911 including its unpredictable switch-case branch. Combined with the
912 processor's internal branch prediction, a successful PREDICT has the
913 effect of making the two opcodes run as if they were a single new opcode
914 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000915
Georg Brandl86b2fb92008-07-16 03:43:04 +0000916 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 predictions turned-on and interpret the results as if some opcodes
918 had been combined or turn-off predictions so that the opcode frequency
919 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000920
921 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 the CPU to record separate branch prediction information for each
923 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000924
Raymond Hettingerf606f872003-03-16 03:11:04 +0000925*/
926
Antoine Pitrou042b1282010-08-13 21:15:58 +0000927#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000929#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300930#define PREDICT(op) \
931 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300932 _Py_CODEUNIT word = *next_instr; \
933 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300934 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300935 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300936 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300937 goto PRED_##op; \
938 } \
939 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000940#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300941#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000942
Raymond Hettingerf606f872003-03-16 03:11:04 +0000943
Guido van Rossum374a9221991-04-04 10:40:29 +0000944/* Stack manipulation macros */
945
Martin v. Löwis18e16552006-02-15 17:27:45 +0000946/* The stack can grow at most MAXINT deep, as co_nlocals and
947 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000948#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
949#define EMPTY() (STACK_LEVEL() == 0)
950#define TOP() (stack_pointer[-1])
951#define SECOND() (stack_pointer[-2])
952#define THIRD() (stack_pointer[-3])
953#define FOURTH() (stack_pointer[-4])
954#define PEEK(n) (stack_pointer[-(n)])
955#define SET_TOP(v) (stack_pointer[-1] = (v))
956#define SET_SECOND(v) (stack_pointer[-2] = (v))
957#define SET_THIRD(v) (stack_pointer[-3] = (v))
958#define SET_FOURTH(v) (stack_pointer[-4] = (v))
959#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
960#define BASIC_STACKADJ(n) (stack_pointer += n)
961#define BASIC_PUSH(v) (*stack_pointer++ = (v))
962#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000963
Guido van Rossum96a42c81992-01-12 02:29:51 +0000964#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +0200966 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000967 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +0200968#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000969 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +0000970#define STACK_GROW(n) do { \
971 assert(n >= 0); \
972 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +0200973 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +0000974 assert(STACK_LEVEL() <= co->co_stacksize); \
975 } while (0)
976#define STACK_SHRINK(n) do { \
977 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +0200978 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +0000979 (void)(BASIC_STACKADJ(-n)); \
980 assert(STACK_LEVEL() <= co->co_stacksize); \
981 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +0000982#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +0200983 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000984 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000985#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000986#define PUSH(v) BASIC_PUSH(v)
987#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +0000988#define STACK_GROW(n) BASIC_STACKADJ(n)
989#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000990#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000991#endif
992
Guido van Rossum681d79a1995-07-18 14:51:37 +0000993/* Local variable macros */
994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000996
997/* The SETLOCAL() macro must not DECREF the local variable in-place and
998 then store the new value; it must copy the old value to a temporary
999 value, then store the new value, and then DECREF the temporary value.
1000 This is because it is possible that during the DECREF the frame is
1001 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1002 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001004 GETLOCAL(i) = value; \
1005 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001006
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001007
1008#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 while (STACK_LEVEL() > (b)->b_level) { \
1010 PyObject *v = POP(); \
1011 Py_XDECREF(v); \
1012 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001013
1014#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001015 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +01001017 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1019 while (STACK_LEVEL() > (b)->b_level + 3) { \
1020 value = POP(); \
1021 Py_XDECREF(value); \
1022 } \
Mark Shannonae3087c2017-10-22 22:41:51 +01001023 exc_info = tstate->exc_info; \
1024 type = exc_info->exc_type; \
1025 value = exc_info->exc_value; \
1026 traceback = exc_info->exc_traceback; \
1027 exc_info->exc_type = POP(); \
1028 exc_info->exc_value = POP(); \
1029 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 Py_XDECREF(type); \
1031 Py_XDECREF(value); \
1032 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001033 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001034
Inada Naoki91234a12019-06-03 21:30:58 +09001035 /* macros for opcode cache */
1036#define OPCACHE_CHECK() \
1037 do { \
1038 co_opcache = NULL; \
1039 if (co->co_opcache != NULL) { \
1040 unsigned char co_opt_offset = \
1041 co->co_opcache_map[next_instr - first_instr]; \
1042 if (co_opt_offset > 0) { \
1043 assert(co_opt_offset <= co->co_opcache_size); \
1044 co_opcache = &co->co_opcache[co_opt_offset - 1]; \
1045 assert(co_opcache != NULL); \
Inada Naoki91234a12019-06-03 21:30:58 +09001046 } \
1047 } \
1048 } while (0)
1049
1050#if OPCACHE_STATS
1051
1052#define OPCACHE_STAT_GLOBAL_HIT() \
1053 do { \
1054 if (co->co_opcache != NULL) opcache_global_hits++; \
1055 } while (0)
1056
1057#define OPCACHE_STAT_GLOBAL_MISS() \
1058 do { \
1059 if (co->co_opcache != NULL) opcache_global_misses++; \
1060 } while (0)
1061
1062#define OPCACHE_STAT_GLOBAL_OPT() \
1063 do { \
1064 if (co->co_opcache != NULL) opcache_global_opts++; \
1065 } while (0)
1066
1067#else /* OPCACHE_STATS */
1068
1069#define OPCACHE_STAT_GLOBAL_HIT()
1070#define OPCACHE_STAT_GLOBAL_MISS()
1071#define OPCACHE_STAT_GLOBAL_OPT()
1072
1073#endif
1074
Guido van Rossuma027efa1997-05-05 20:56:21 +00001075/* Start of code */
1076
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 /* push frame */
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001078 if (_Py_EnterRecursiveCall(tstate, "")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001080 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001081
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001083
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 if (tstate->use_tracing) {
1085 if (tstate->c_tracefunc != NULL) {
1086 /* tstate->c_tracefunc, if defined, is a
1087 function that will be called on *every* entry
1088 to a code block. Its return value, if not
1089 None, is a function that will be called at
1090 the start of each executed line of code.
1091 (Actually, the function must return itself
1092 in order to continue tracing.) The trace
1093 functions are called with three arguments:
1094 a pointer to the current frame, a string
1095 indicating why the function is called, and
1096 an argument which depends on the situation.
1097 The global trace function is also called
1098 whenever an exception is detected. */
1099 if (call_trace_protected(tstate->c_tracefunc,
1100 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001101 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001102 /* Trace function raised an error */
1103 goto exit_eval_frame;
1104 }
1105 }
1106 if (tstate->c_profilefunc != NULL) {
1107 /* Similar for c_profilefunc, except it needn't
1108 return itself and isn't called for "line" events */
1109 if (call_trace_protected(tstate->c_profilefunc,
1110 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001111 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001112 /* Profile function raised an error */
1113 goto exit_eval_frame;
1114 }
1115 }
1116 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001117
Łukasz Langaa785c872016-09-09 17:37:37 -07001118 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1119 dtrace_function_entry(f);
1120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 co = f->f_code;
1122 names = co->co_names;
1123 consts = co->co_consts;
1124 fastlocals = f->f_localsplus;
1125 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001126 assert(PyBytes_Check(co->co_code));
1127 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001128 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1129 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1130 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001131 /*
1132 f->f_lasti refers to the index of the last instruction,
1133 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001134
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001135 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001136 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001137
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 When the PREDICT() macros are enabled, some opcode pairs follow in
1139 direct succession without updating f->f_lasti. A successful
1140 prediction effectively links the two codes together as if they
1141 were a single new opcode; accordingly,f->f_lasti will point to
1142 the first code in the pair (for instance, GET_ITER followed by
1143 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001144 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001146 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001147 next_instr = first_instr;
1148 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001149 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1150 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001151 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 stack_pointer = f->f_stacktop;
1153 assert(stack_pointer != NULL);
1154 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001155 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001156
Inada Naoki91234a12019-06-03 21:30:58 +09001157 if (co->co_opcache_flag < OPCACHE_MIN_RUNS) {
1158 co->co_opcache_flag++;
1159 if (co->co_opcache_flag == OPCACHE_MIN_RUNS) {
1160 if (_PyCode_InitOpcache(co) < 0) {
1161 return NULL;
1162 }
1163#if OPCACHE_STATS
1164 opcache_code_objects_extra_mem +=
1165 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1166 sizeof(_PyOpcache) * co->co_opcache_size;
1167 opcache_code_objects++;
1168#endif
1169 }
1170 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001171
Tim Peters5ca576e2001-06-18 22:08:13 +00001172#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001173 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001174#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001175
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001176 if (throwflag) /* support for generator.throw() */
1177 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001178
Victor Stinnerace47d72013-07-18 01:41:08 +02001179#ifdef Py_DEBUG
1180 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001181 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001182 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001183 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001184#endif
1185
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001186main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1189 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001190 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 /* Do periodic things. Doing this every time through
1193 the loop would add too much overhead, so we do it
1194 only every Nth instruction. We also do it if
1195 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1196 event needs attention (e.g. a signal handler or
1197 async I/O handler); see Py_AddPendingCall() and
1198 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001199
Eric Snow7bda9de2019-03-08 17:25:54 -07001200 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001201 opcode = _Py_OPCODE(*next_instr);
1202 if (opcode == SETUP_FINALLY ||
1203 opcode == SETUP_WITH ||
1204 opcode == BEFORE_ASYNC_WITH ||
1205 opcode == YIELD_FROM) {
1206 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001207 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001208 - If we're about to enter the 'with:'. It will prevent
1209 emitting a resource warning in the common idiom
1210 'with open(path) as file:'.
1211 - If we're about to enter the 'async with:'.
1212 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001213 *very* useful, but might help in some cases and it's
1214 traditional)
1215 - If we're resuming a chain of nested 'yield from' or
1216 'await' calls, then each frame is parked with YIELD_FROM
1217 as its next opcode. If the user hit control-C we want to
1218 wait until we've reached the innermost frame before
1219 running the signal handler and raising KeyboardInterrupt
1220 (see bpo-30039).
1221 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 goto fast_next_opcode;
1223 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001224
Victor Stinnere225beb2019-06-03 18:14:24 +02001225 if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
Victor Stinner09532fe2019-05-10 23:39:09 +02001226 if (handle_signals(runtime) != 0) {
Eric Snowfdf282d2019-01-11 14:26:55 -07001227 goto error;
1228 }
1229 }
Victor Stinnere225beb2019-06-03 18:14:24 +02001230 if (_Py_atomic_load_relaxed(&ceval->pending.calls_to_do)) {
1231 if (make_pending_calls(runtime) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001232 goto error;
Eric Snowfdf282d2019-01-11 14:26:55 -07001233 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001235
Victor Stinnere225beb2019-06-03 18:14:24 +02001236 if (_Py_atomic_load_relaxed(&ceval->gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 /* Give another thread a chance */
Victor Stinner09532fe2019-05-10 23:39:09 +02001238 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 Py_FatalError("ceval: tstate mix-up");
Victor Stinner09532fe2019-05-10 23:39:09 +02001240 }
Victor Stinnere225beb2019-06-03 18:14:24 +02001241 drop_gil(ceval, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242
1243 /* Other threads may run now */
1244
Victor Stinnere225beb2019-06-03 18:14:24 +02001245 take_gil(ceval, tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001246
1247 /* Check if we should make a quick exit. */
Victor Stinner0fd2c302019-06-04 03:15:09 +02001248 exit_thread_if_finalizing(runtime, tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001249
Victor Stinner09532fe2019-05-10 23:39:09 +02001250 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 Py_FatalError("ceval: orphan tstate");
Victor Stinner09532fe2019-05-10 23:39:09 +02001252 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 }
1254 /* Check for asynchronous exceptions. */
1255 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001256 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 tstate->async_exc = NULL;
Victor Stinnere225beb2019-06-03 18:14:24 +02001258 UNSIGNAL_ASYNC_EXC(ceval);
Victor Stinner438a12d2019-05-24 17:01:38 +02001259 _PyErr_SetNone(tstate, exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001260 Py_DECREF(exc);
1261 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001262 }
1263 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 fast_next_opcode:
1266 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001267
Łukasz Langaa785c872016-09-09 17:37:37 -07001268 if (PyDTrace_LINE_ENABLED())
1269 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001272
Victor Stinnere225beb2019-06-03 18:14:24 +02001273 if (_Py_TracingPossible(ceval) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001274 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001275 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 /* see maybe_call_line_trace
1277 for expository comments */
1278 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 err = maybe_call_line_trace(tstate->c_tracefunc,
1281 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001282 tstate, f,
1283 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 /* Reload possibly changed frame fields */
1285 JUMPTO(f->f_lasti);
1286 if (f->f_stacktop != NULL) {
1287 stack_pointer = f->f_stacktop;
1288 f->f_stacktop = NULL;
1289 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001290 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001292 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001295 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001296
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001297 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001298 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001299#ifdef DYNAMIC_EXECUTION_PROFILE
1300#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001301 dxpairs[lastopcode][opcode]++;
1302 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001303#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001305#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001306
Guido van Rossum96a42c81992-01-12 02:29:51 +00001307#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 if (lltrace) {
1311 if (HAS_ARG(opcode)) {
1312 printf("%d: %d, %d\n",
1313 f->f_lasti, opcode, oparg);
1314 }
1315 else {
1316 printf("%d: %d\n",
1317 f->f_lasti, opcode);
1318 }
1319 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001320#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001325 It is essential that any operation that fails must goto error
1326 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001327
Benjamin Petersonddd19492018-09-16 22:38:02 -07001328 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001330 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001331
Benjamin Petersonddd19492018-09-16 22:38:02 -07001332 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001333 PyObject *value = GETLOCAL(oparg);
1334 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001335 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001336 UNBOUNDLOCAL_ERROR_MSG,
1337 PyTuple_GetItem(co->co_varnames, oparg));
1338 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001340 Py_INCREF(value);
1341 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001343 }
1344
Benjamin Petersonddd19492018-09-16 22:38:02 -07001345 case TARGET(LOAD_CONST): {
1346 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001347 PyObject *value = GETITEM(consts, oparg);
1348 Py_INCREF(value);
1349 PUSH(value);
1350 FAST_DISPATCH();
1351 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001352
Benjamin Petersonddd19492018-09-16 22:38:02 -07001353 case TARGET(STORE_FAST): {
1354 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001355 PyObject *value = POP();
1356 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001358 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001359
Benjamin Petersonddd19492018-09-16 22:38:02 -07001360 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001361 PyObject *value = POP();
1362 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001364 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001365
Benjamin Petersonddd19492018-09-16 22:38:02 -07001366 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001367 PyObject *top = TOP();
1368 PyObject *second = SECOND();
1369 SET_TOP(second);
1370 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001372 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001373
Benjamin Petersonddd19492018-09-16 22:38:02 -07001374 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001375 PyObject *top = TOP();
1376 PyObject *second = SECOND();
1377 PyObject *third = THIRD();
1378 SET_TOP(second);
1379 SET_SECOND(third);
1380 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001382 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001383
Benjamin Petersonddd19492018-09-16 22:38:02 -07001384 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001385 PyObject *top = TOP();
1386 PyObject *second = SECOND();
1387 PyObject *third = THIRD();
1388 PyObject *fourth = FOURTH();
1389 SET_TOP(second);
1390 SET_SECOND(third);
1391 SET_THIRD(fourth);
1392 SET_FOURTH(top);
1393 FAST_DISPATCH();
1394 }
1395
Benjamin Petersonddd19492018-09-16 22:38:02 -07001396 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001397 PyObject *top = TOP();
1398 Py_INCREF(top);
1399 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001401 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001402
Benjamin Petersonddd19492018-09-16 22:38:02 -07001403 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001404 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001405 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001406 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001407 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001408 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001409 SET_TOP(top);
1410 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001411 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001412 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001413
Benjamin Petersonddd19492018-09-16 22:38:02 -07001414 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001415 PyObject *value = TOP();
1416 PyObject *res = PyNumber_Positive(value);
1417 Py_DECREF(value);
1418 SET_TOP(res);
1419 if (res == NULL)
1420 goto error;
1421 DISPATCH();
1422 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001423
Benjamin Petersonddd19492018-09-16 22:38:02 -07001424 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001425 PyObject *value = TOP();
1426 PyObject *res = PyNumber_Negative(value);
1427 Py_DECREF(value);
1428 SET_TOP(res);
1429 if (res == NULL)
1430 goto error;
1431 DISPATCH();
1432 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001433
Benjamin Petersonddd19492018-09-16 22:38:02 -07001434 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001435 PyObject *value = TOP();
1436 int err = PyObject_IsTrue(value);
1437 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438 if (err == 0) {
1439 Py_INCREF(Py_True);
1440 SET_TOP(Py_True);
1441 DISPATCH();
1442 }
1443 else if (err > 0) {
1444 Py_INCREF(Py_False);
1445 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001446 DISPATCH();
1447 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001448 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001449 goto error;
1450 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001451
Benjamin Petersonddd19492018-09-16 22:38:02 -07001452 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001453 PyObject *value = TOP();
1454 PyObject *res = PyNumber_Invert(value);
1455 Py_DECREF(value);
1456 SET_TOP(res);
1457 if (res == NULL)
1458 goto error;
1459 DISPATCH();
1460 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001461
Benjamin Petersonddd19492018-09-16 22:38:02 -07001462 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001463 PyObject *exp = POP();
1464 PyObject *base = TOP();
1465 PyObject *res = PyNumber_Power(base, exp, Py_None);
1466 Py_DECREF(base);
1467 Py_DECREF(exp);
1468 SET_TOP(res);
1469 if (res == NULL)
1470 goto error;
1471 DISPATCH();
1472 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001473
Benjamin Petersonddd19492018-09-16 22:38:02 -07001474 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001475 PyObject *right = POP();
1476 PyObject *left = TOP();
1477 PyObject *res = PyNumber_Multiply(left, right);
1478 Py_DECREF(left);
1479 Py_DECREF(right);
1480 SET_TOP(res);
1481 if (res == NULL)
1482 goto error;
1483 DISPATCH();
1484 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001485
Benjamin Petersonddd19492018-09-16 22:38:02 -07001486 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001487 PyObject *right = POP();
1488 PyObject *left = TOP();
1489 PyObject *res = PyNumber_MatrixMultiply(left, right);
1490 Py_DECREF(left);
1491 Py_DECREF(right);
1492 SET_TOP(res);
1493 if (res == NULL)
1494 goto error;
1495 DISPATCH();
1496 }
1497
Benjamin Petersonddd19492018-09-16 22:38:02 -07001498 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001499 PyObject *divisor = POP();
1500 PyObject *dividend = TOP();
1501 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1502 Py_DECREF(dividend);
1503 Py_DECREF(divisor);
1504 SET_TOP(quotient);
1505 if (quotient == NULL)
1506 goto error;
1507 DISPATCH();
1508 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001509
Benjamin Petersonddd19492018-09-16 22:38:02 -07001510 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001511 PyObject *divisor = POP();
1512 PyObject *dividend = TOP();
1513 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1514 Py_DECREF(dividend);
1515 Py_DECREF(divisor);
1516 SET_TOP(quotient);
1517 if (quotient == NULL)
1518 goto error;
1519 DISPATCH();
1520 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001521
Benjamin Petersonddd19492018-09-16 22:38:02 -07001522 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001523 PyObject *divisor = POP();
1524 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001525 PyObject *res;
1526 if (PyUnicode_CheckExact(dividend) && (
1527 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1528 // fast path; string formatting, but not if the RHS is a str subclass
1529 // (see issue28598)
1530 res = PyUnicode_Format(dividend, divisor);
1531 } else {
1532 res = PyNumber_Remainder(dividend, divisor);
1533 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001534 Py_DECREF(divisor);
1535 Py_DECREF(dividend);
1536 SET_TOP(res);
1537 if (res == NULL)
1538 goto error;
1539 DISPATCH();
1540 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001541
Benjamin Petersonddd19492018-09-16 22:38:02 -07001542 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001543 PyObject *right = POP();
1544 PyObject *left = TOP();
1545 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001546 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1547 CPython using bytecode, it is simply worthless.
1548 See http://bugs.python.org/issue21955 and
1549 http://bugs.python.org/issue10044 for the discussion. In short,
1550 no patch shown any impact on a realistic benchmark, only a minor
1551 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001552 if (PyUnicode_CheckExact(left) &&
1553 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001554 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001555 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001556 }
1557 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001558 sum = PyNumber_Add(left, right);
1559 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001560 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001561 Py_DECREF(right);
1562 SET_TOP(sum);
1563 if (sum == NULL)
1564 goto error;
1565 DISPATCH();
1566 }
1567
Benjamin Petersonddd19492018-09-16 22:38:02 -07001568 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001569 PyObject *right = POP();
1570 PyObject *left = TOP();
1571 PyObject *diff = PyNumber_Subtract(left, right);
1572 Py_DECREF(right);
1573 Py_DECREF(left);
1574 SET_TOP(diff);
1575 if (diff == NULL)
1576 goto error;
1577 DISPATCH();
1578 }
1579
Benjamin Petersonddd19492018-09-16 22:38:02 -07001580 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001581 PyObject *sub = POP();
1582 PyObject *container = TOP();
1583 PyObject *res = PyObject_GetItem(container, sub);
1584 Py_DECREF(container);
1585 Py_DECREF(sub);
1586 SET_TOP(res);
1587 if (res == NULL)
1588 goto error;
1589 DISPATCH();
1590 }
1591
Benjamin Petersonddd19492018-09-16 22:38:02 -07001592 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001593 PyObject *right = POP();
1594 PyObject *left = TOP();
1595 PyObject *res = PyNumber_Lshift(left, right);
1596 Py_DECREF(left);
1597 Py_DECREF(right);
1598 SET_TOP(res);
1599 if (res == NULL)
1600 goto error;
1601 DISPATCH();
1602 }
1603
Benjamin Petersonddd19492018-09-16 22:38:02 -07001604 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001605 PyObject *right = POP();
1606 PyObject *left = TOP();
1607 PyObject *res = PyNumber_Rshift(left, right);
1608 Py_DECREF(left);
1609 Py_DECREF(right);
1610 SET_TOP(res);
1611 if (res == NULL)
1612 goto error;
1613 DISPATCH();
1614 }
1615
Benjamin Petersonddd19492018-09-16 22:38:02 -07001616 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001617 PyObject *right = POP();
1618 PyObject *left = TOP();
1619 PyObject *res = PyNumber_And(left, right);
1620 Py_DECREF(left);
1621 Py_DECREF(right);
1622 SET_TOP(res);
1623 if (res == NULL)
1624 goto error;
1625 DISPATCH();
1626 }
1627
Benjamin Petersonddd19492018-09-16 22:38:02 -07001628 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001629 PyObject *right = POP();
1630 PyObject *left = TOP();
1631 PyObject *res = PyNumber_Xor(left, right);
1632 Py_DECREF(left);
1633 Py_DECREF(right);
1634 SET_TOP(res);
1635 if (res == NULL)
1636 goto error;
1637 DISPATCH();
1638 }
1639
Benjamin Petersonddd19492018-09-16 22:38:02 -07001640 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001641 PyObject *right = POP();
1642 PyObject *left = TOP();
1643 PyObject *res = PyNumber_Or(left, right);
1644 Py_DECREF(left);
1645 Py_DECREF(right);
1646 SET_TOP(res);
1647 if (res == NULL)
1648 goto error;
1649 DISPATCH();
1650 }
1651
Benjamin Petersonddd19492018-09-16 22:38:02 -07001652 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001653 PyObject *v = POP();
1654 PyObject *list = PEEK(oparg);
1655 int err;
1656 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001658 if (err != 0)
1659 goto error;
1660 PREDICT(JUMP_ABSOLUTE);
1661 DISPATCH();
1662 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001663
Benjamin Petersonddd19492018-09-16 22:38:02 -07001664 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001665 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001666 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001667 int err;
1668 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001670 if (err != 0)
1671 goto error;
1672 PREDICT(JUMP_ABSOLUTE);
1673 DISPATCH();
1674 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001675
Benjamin Petersonddd19492018-09-16 22:38:02 -07001676 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001677 PyObject *exp = POP();
1678 PyObject *base = TOP();
1679 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1680 Py_DECREF(base);
1681 Py_DECREF(exp);
1682 SET_TOP(res);
1683 if (res == NULL)
1684 goto error;
1685 DISPATCH();
1686 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001687
Benjamin Petersonddd19492018-09-16 22:38:02 -07001688 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001689 PyObject *right = POP();
1690 PyObject *left = TOP();
1691 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1692 Py_DECREF(left);
1693 Py_DECREF(right);
1694 SET_TOP(res);
1695 if (res == NULL)
1696 goto error;
1697 DISPATCH();
1698 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001699
Benjamin Petersonddd19492018-09-16 22:38:02 -07001700 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001701 PyObject *right = POP();
1702 PyObject *left = TOP();
1703 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1704 Py_DECREF(left);
1705 Py_DECREF(right);
1706 SET_TOP(res);
1707 if (res == NULL)
1708 goto error;
1709 DISPATCH();
1710 }
1711
Benjamin Petersonddd19492018-09-16 22:38:02 -07001712 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001713 PyObject *divisor = POP();
1714 PyObject *dividend = TOP();
1715 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1716 Py_DECREF(dividend);
1717 Py_DECREF(divisor);
1718 SET_TOP(quotient);
1719 if (quotient == NULL)
1720 goto error;
1721 DISPATCH();
1722 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001723
Benjamin Petersonddd19492018-09-16 22:38:02 -07001724 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001725 PyObject *divisor = POP();
1726 PyObject *dividend = TOP();
1727 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1728 Py_DECREF(dividend);
1729 Py_DECREF(divisor);
1730 SET_TOP(quotient);
1731 if (quotient == NULL)
1732 goto error;
1733 DISPATCH();
1734 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001735
Benjamin Petersonddd19492018-09-16 22:38:02 -07001736 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001737 PyObject *right = POP();
1738 PyObject *left = TOP();
1739 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1740 Py_DECREF(left);
1741 Py_DECREF(right);
1742 SET_TOP(mod);
1743 if (mod == NULL)
1744 goto error;
1745 DISPATCH();
1746 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001747
Benjamin Petersonddd19492018-09-16 22:38:02 -07001748 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001749 PyObject *right = POP();
1750 PyObject *left = TOP();
1751 PyObject *sum;
1752 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001753 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001754 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001755 }
1756 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001757 sum = PyNumber_InPlaceAdd(left, right);
1758 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001759 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001760 Py_DECREF(right);
1761 SET_TOP(sum);
1762 if (sum == NULL)
1763 goto error;
1764 DISPATCH();
1765 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001766
Benjamin Petersonddd19492018-09-16 22:38:02 -07001767 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001768 PyObject *right = POP();
1769 PyObject *left = TOP();
1770 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1771 Py_DECREF(left);
1772 Py_DECREF(right);
1773 SET_TOP(diff);
1774 if (diff == NULL)
1775 goto error;
1776 DISPATCH();
1777 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001778
Benjamin Petersonddd19492018-09-16 22:38:02 -07001779 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001780 PyObject *right = POP();
1781 PyObject *left = TOP();
1782 PyObject *res = PyNumber_InPlaceLshift(left, right);
1783 Py_DECREF(left);
1784 Py_DECREF(right);
1785 SET_TOP(res);
1786 if (res == NULL)
1787 goto error;
1788 DISPATCH();
1789 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001790
Benjamin Petersonddd19492018-09-16 22:38:02 -07001791 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001792 PyObject *right = POP();
1793 PyObject *left = TOP();
1794 PyObject *res = PyNumber_InPlaceRshift(left, right);
1795 Py_DECREF(left);
1796 Py_DECREF(right);
1797 SET_TOP(res);
1798 if (res == NULL)
1799 goto error;
1800 DISPATCH();
1801 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001802
Benjamin Petersonddd19492018-09-16 22:38:02 -07001803 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001804 PyObject *right = POP();
1805 PyObject *left = TOP();
1806 PyObject *res = PyNumber_InPlaceAnd(left, right);
1807 Py_DECREF(left);
1808 Py_DECREF(right);
1809 SET_TOP(res);
1810 if (res == NULL)
1811 goto error;
1812 DISPATCH();
1813 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001814
Benjamin Petersonddd19492018-09-16 22:38:02 -07001815 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001816 PyObject *right = POP();
1817 PyObject *left = TOP();
1818 PyObject *res = PyNumber_InPlaceXor(left, right);
1819 Py_DECREF(left);
1820 Py_DECREF(right);
1821 SET_TOP(res);
1822 if (res == NULL)
1823 goto error;
1824 DISPATCH();
1825 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001826
Benjamin Petersonddd19492018-09-16 22:38:02 -07001827 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001828 PyObject *right = POP();
1829 PyObject *left = TOP();
1830 PyObject *res = PyNumber_InPlaceOr(left, right);
1831 Py_DECREF(left);
1832 Py_DECREF(right);
1833 SET_TOP(res);
1834 if (res == NULL)
1835 goto error;
1836 DISPATCH();
1837 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001838
Benjamin Petersonddd19492018-09-16 22:38:02 -07001839 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001840 PyObject *sub = TOP();
1841 PyObject *container = SECOND();
1842 PyObject *v = THIRD();
1843 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001844 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001845 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001846 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001847 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001848 Py_DECREF(container);
1849 Py_DECREF(sub);
1850 if (err != 0)
1851 goto error;
1852 DISPATCH();
1853 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001854
Benjamin Petersonddd19492018-09-16 22:38:02 -07001855 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001856 PyObject *sub = TOP();
1857 PyObject *container = SECOND();
1858 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001859 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00001860 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001861 err = PyObject_DelItem(container, sub);
1862 Py_DECREF(container);
1863 Py_DECREF(sub);
1864 if (err != 0)
1865 goto error;
1866 DISPATCH();
1867 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001868
Benjamin Petersonddd19492018-09-16 22:38:02 -07001869 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01001870 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001871 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001872 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001873 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001874 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001875 _PyErr_SetString(tstate, PyExc_RuntimeError,
1876 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001877 Py_DECREF(value);
1878 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001879 }
Jeroen Demeyer196a5302019-07-04 12:31:34 +02001880 res = _PyObject_CallOneArg(hook, value);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001881 Py_DECREF(value);
1882 if (res == NULL)
1883 goto error;
1884 Py_DECREF(res);
1885 DISPATCH();
1886 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001887
Benjamin Petersonddd19492018-09-16 22:38:02 -07001888 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001889 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001890 switch (oparg) {
1891 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001892 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001893 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001894 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001895 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001896 /* fall through */
1897 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02001898 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001899 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001900 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001901 break;
1902 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02001903 _PyErr_SetString(tstate, PyExc_SystemError,
1904 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905 break;
1906 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001907 goto error;
1908 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001909
Benjamin Petersonddd19492018-09-16 22:38:02 -07001910 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001911 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001912 assert(f->f_iblock == 0);
Pablo Galindof00828a2019-05-09 16:52:02 +01001913 goto exit_returning;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001914 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001915
Benjamin Petersonddd19492018-09-16 22:38:02 -07001916 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001917 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001918 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001919 PyObject *obj = TOP();
1920 PyTypeObject *type = Py_TYPE(obj);
1921
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001922 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001923 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001924 }
Yury Selivanov75445082015-05-11 22:57:16 -04001925
1926 if (getter != NULL) {
1927 iter = (*getter)(obj);
1928 Py_DECREF(obj);
1929 if (iter == NULL) {
1930 SET_TOP(NULL);
1931 goto error;
1932 }
1933 }
1934 else {
1935 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02001936 _PyErr_Format(tstate, PyExc_TypeError,
1937 "'async for' requires an object with "
1938 "__aiter__ method, got %.100s",
1939 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001940 Py_DECREF(obj);
1941 goto error;
1942 }
1943
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001944 if (Py_TYPE(iter)->tp_as_async == NULL ||
1945 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001946
Yury Selivanov398ff912017-03-02 22:20:00 -05001947 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02001948 _PyErr_Format(tstate, PyExc_TypeError,
1949 "'async for' received an object from __aiter__ "
1950 "that does not implement __anext__: %.100s",
1951 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001952 Py_DECREF(iter);
1953 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001954 }
1955
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001956 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001957 DISPATCH();
1958 }
1959
Benjamin Petersonddd19492018-09-16 22:38:02 -07001960 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001961 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001962 PyObject *next_iter = NULL;
1963 PyObject *awaitable = NULL;
1964 PyObject *aiter = TOP();
1965 PyTypeObject *type = Py_TYPE(aiter);
1966
Yury Selivanoveb636452016-09-08 22:01:51 -07001967 if (PyAsyncGen_CheckExact(aiter)) {
1968 awaitable = type->tp_as_async->am_anext(aiter);
1969 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001970 goto error;
1971 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001972 } else {
1973 if (type->tp_as_async != NULL){
1974 getter = type->tp_as_async->am_anext;
1975 }
Yury Selivanov75445082015-05-11 22:57:16 -04001976
Yury Selivanoveb636452016-09-08 22:01:51 -07001977 if (getter != NULL) {
1978 next_iter = (*getter)(aiter);
1979 if (next_iter == NULL) {
1980 goto error;
1981 }
1982 }
1983 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02001984 _PyErr_Format(tstate, PyExc_TypeError,
1985 "'async for' requires an iterator with "
1986 "__anext__ method, got %.100s",
1987 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07001988 goto error;
1989 }
Yury Selivanov75445082015-05-11 22:57:16 -04001990
Yury Selivanoveb636452016-09-08 22:01:51 -07001991 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1992 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001993 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001994 PyExc_TypeError,
1995 "'async for' received an invalid object "
1996 "from __anext__: %.100s",
1997 Py_TYPE(next_iter)->tp_name);
1998
1999 Py_DECREF(next_iter);
2000 goto error;
2001 } else {
2002 Py_DECREF(next_iter);
2003 }
2004 }
Yury Selivanov75445082015-05-11 22:57:16 -04002005
2006 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002007 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002008 DISPATCH();
2009 }
2010
Benjamin Petersonddd19492018-09-16 22:38:02 -07002011 case TARGET(GET_AWAITABLE): {
2012 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002013 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002014 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002015
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002016 if (iter == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002017 format_awaitable_error(tstate, Py_TYPE(iterable),
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002018 _Py_OPCODE(next_instr[-2]));
2019 }
2020
Yury Selivanov75445082015-05-11 22:57:16 -04002021 Py_DECREF(iterable);
2022
Yury Selivanovc724bae2016-03-02 11:30:46 -05002023 if (iter != NULL && PyCoro_CheckExact(iter)) {
2024 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2025 if (yf != NULL) {
2026 /* `iter` is a coroutine object that is being
2027 awaited, `yf` is a pointer to the current awaitable
2028 being awaited on. */
2029 Py_DECREF(yf);
2030 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002031 _PyErr_SetString(tstate, PyExc_RuntimeError,
2032 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002033 /* The code below jumps to `error` if `iter` is NULL. */
2034 }
2035 }
2036
Yury Selivanov75445082015-05-11 22:57:16 -04002037 SET_TOP(iter); /* Even if it's NULL */
2038
2039 if (iter == NULL) {
2040 goto error;
2041 }
2042
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002043 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002044 DISPATCH();
2045 }
2046
Benjamin Petersonddd19492018-09-16 22:38:02 -07002047 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002048 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002049 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002050 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002051 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
2052 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002053 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002054 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002055 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002056 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002057 else
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02002058 retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002059 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002060 Py_DECREF(v);
2061 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002062 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002063 if (tstate->c_tracefunc != NULL
Victor Stinner438a12d2019-05-24 17:01:38 +02002064 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002065 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002066 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002067 if (err < 0)
2068 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002069 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002070 SET_TOP(val);
2071 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002072 }
Martin Panter95f53c12016-07-18 08:23:26 +00002073 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002074 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002075 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002076 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002077 f->f_lasti -= sizeof(_Py_CODEUNIT);
Pablo Galindof00828a2019-05-09 16:52:02 +01002078 goto exit_yielding;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002079 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002080
Benjamin Petersonddd19492018-09-16 22:38:02 -07002081 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002082 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002083
2084 if (co->co_flags & CO_ASYNC_GENERATOR) {
2085 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2086 Py_DECREF(retval);
2087 if (w == NULL) {
2088 retval = NULL;
2089 goto error;
2090 }
2091 retval = w;
2092 }
2093
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002094 f->f_stacktop = stack_pointer;
Pablo Galindof00828a2019-05-09 16:52:02 +01002095 goto exit_yielding;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002096 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002097
Benjamin Petersonddd19492018-09-16 22:38:02 -07002098 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002099 PyObject *type, *value, *traceback;
2100 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002101 PyTryBlock *b = PyFrame_BlockPop(f);
2102 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002103 _PyErr_SetString(tstate, PyExc_SystemError,
2104 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002105 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002106 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002107 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2108 STACK_LEVEL() <= (b)->b_level + 4);
2109 exc_info = tstate->exc_info;
2110 type = exc_info->exc_type;
2111 value = exc_info->exc_value;
2112 traceback = exc_info->exc_traceback;
2113 exc_info->exc_type = POP();
2114 exc_info->exc_value = POP();
2115 exc_info->exc_traceback = POP();
2116 Py_XDECREF(type);
2117 Py_XDECREF(value);
2118 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002119 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002120 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002121
Benjamin Petersonddd19492018-09-16 22:38:02 -07002122 case TARGET(POP_BLOCK): {
2123 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002124 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002125 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002126 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002127
Benjamin Petersonddd19492018-09-16 22:38:02 -07002128 case TARGET(POP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002129 /* If oparg is 0 at the top of the stack are 1 or 6 values:
2130 Either:
2131 - TOP = NULL or an integer
2132 or:
2133 - (TOP, SECOND, THIRD) = exc_info()
2134 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2135
2136 If oparg is 1 the value for 'return' was additionally pushed
2137 at the top of the stack.
2138 */
2139 PyObject *res = NULL;
2140 if (oparg) {
2141 res = POP();
2142 }
2143 PyObject *exc = POP();
2144 if (exc == NULL || PyLong_CheckExact(exc)) {
2145 Py_XDECREF(exc);
2146 }
2147 else {
2148 Py_DECREF(exc);
2149 Py_DECREF(POP());
2150 Py_DECREF(POP());
2151
2152 PyObject *type, *value, *traceback;
2153 _PyErr_StackItem *exc_info;
2154 PyTryBlock *b = PyFrame_BlockPop(f);
2155 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002156 _PyErr_SetString(tstate, PyExc_SystemError,
2157 "popped block is not an except handler");
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002158 Py_XDECREF(res);
2159 goto error;
2160 }
2161 assert(STACK_LEVEL() == (b)->b_level + 3);
2162 exc_info = tstate->exc_info;
2163 type = exc_info->exc_type;
2164 value = exc_info->exc_value;
2165 traceback = exc_info->exc_traceback;
2166 exc_info->exc_type = POP();
2167 exc_info->exc_value = POP();
2168 exc_info->exc_traceback = POP();
2169 Py_XDECREF(type);
2170 Py_XDECREF(value);
2171 Py_XDECREF(traceback);
2172 }
2173 if (oparg) {
2174 PUSH(res);
2175 }
2176 DISPATCH();
2177 }
2178
Benjamin Petersonddd19492018-09-16 22:38:02 -07002179 case TARGET(CALL_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002180 PyObject *ret = PyLong_FromLong(INSTR_OFFSET());
2181 if (ret == NULL) {
2182 goto error;
2183 }
2184 PUSH(ret);
2185 JUMPBY(oparg);
2186 FAST_DISPATCH();
2187 }
2188
Benjamin Petersonddd19492018-09-16 22:38:02 -07002189 case TARGET(BEGIN_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002190 /* Push NULL onto the stack for using it in END_FINALLY,
2191 POP_FINALLY, WITH_CLEANUP_START and WITH_CLEANUP_FINISH.
2192 */
2193 PUSH(NULL);
2194 FAST_DISPATCH();
2195 }
2196
Benjamin Petersonddd19492018-09-16 22:38:02 -07002197 case TARGET(END_FINALLY): {
2198 PREDICTED(END_FINALLY);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002199 /* At the top of the stack are 1 or 6 values:
2200 Either:
2201 - TOP = NULL or an integer
2202 or:
2203 - (TOP, SECOND, THIRD) = exc_info()
2204 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2205 */
2206 PyObject *exc = POP();
2207 if (exc == NULL) {
2208 FAST_DISPATCH();
2209 }
2210 else if (PyLong_CheckExact(exc)) {
2211 int ret = _PyLong_AsInt(exc);
2212 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02002213 if (ret == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002214 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002215 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002216 JUMPTO(ret);
2217 FAST_DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002218 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002219 else {
2220 assert(PyExceptionClass_Check(exc));
2221 PyObject *val = POP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002222 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002223 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002224 goto exception_unwind;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002225 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002226 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002227
Benjamin Petersonddd19492018-09-16 22:38:02 -07002228 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002229 PyObject *exc = POP();
2230 assert(PyExceptionClass_Check(exc));
2231 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2232 PyTryBlock *b = PyFrame_BlockPop(f);
2233 assert(b->b_type == EXCEPT_HANDLER);
2234 Py_DECREF(exc);
2235 UNWIND_EXCEPT_HANDLER(b);
2236 Py_DECREF(POP());
2237 JUMPBY(oparg);
2238 FAST_DISPATCH();
2239 }
2240 else {
2241 PyObject *val = POP();
2242 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002243 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002244 goto exception_unwind;
2245 }
2246 }
2247
Zackery Spytzce6a0702019-08-25 03:44:09 -06002248 case TARGET(LOAD_ASSERTION_ERROR): {
2249 PyObject *value = PyExc_AssertionError;
2250 Py_INCREF(value);
2251 PUSH(value);
2252 FAST_DISPATCH();
2253 }
2254
Benjamin Petersonddd19492018-09-16 22:38:02 -07002255 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002256 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002257
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002258 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002259 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002260 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002261 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002262 if (!_PyErr_Occurred(tstate)) {
2263 _PyErr_SetString(tstate, PyExc_NameError,
2264 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002265 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002266 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002267 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002268 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002269 }
2270 else {
2271 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2272 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002273 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002274 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2275 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002276 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2277 _PyErr_SetString(tstate, PyExc_NameError,
2278 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002279 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002280 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002281 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002282 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002283 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002284 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002285
Benjamin Petersonddd19492018-09-16 22:38:02 -07002286 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002287 PyObject *name = GETITEM(names, oparg);
2288 PyObject *v = POP();
2289 PyObject *ns = f->f_locals;
2290 int err;
2291 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002292 _PyErr_Format(tstate, PyExc_SystemError,
2293 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002294 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002295 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002296 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002297 if (PyDict_CheckExact(ns))
2298 err = PyDict_SetItem(ns, name, v);
2299 else
2300 err = PyObject_SetItem(ns, name, v);
2301 Py_DECREF(v);
2302 if (err != 0)
2303 goto error;
2304 DISPATCH();
2305 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002306
Benjamin Petersonddd19492018-09-16 22:38:02 -07002307 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002308 PyObject *name = GETITEM(names, oparg);
2309 PyObject *ns = f->f_locals;
2310 int err;
2311 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002312 _PyErr_Format(tstate, PyExc_SystemError,
2313 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002314 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002315 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002316 err = PyObject_DelItem(ns, name);
2317 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002318 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002319 NAME_ERROR_MSG,
2320 name);
2321 goto error;
2322 }
2323 DISPATCH();
2324 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002325
Benjamin Petersonddd19492018-09-16 22:38:02 -07002326 case TARGET(UNPACK_SEQUENCE): {
2327 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002328 PyObject *seq = POP(), *item, **items;
2329 if (PyTuple_CheckExact(seq) &&
2330 PyTuple_GET_SIZE(seq) == oparg) {
2331 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002332 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002333 item = items[oparg];
2334 Py_INCREF(item);
2335 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002336 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002337 } else if (PyList_CheckExact(seq) &&
2338 PyList_GET_SIZE(seq) == oparg) {
2339 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002340 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002341 item = items[oparg];
2342 Py_INCREF(item);
2343 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002344 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002345 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002346 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002347 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002348 } else {
2349 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002350 Py_DECREF(seq);
2351 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002352 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002353 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002354 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002355 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002356
Benjamin Petersonddd19492018-09-16 22:38:02 -07002357 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002358 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2359 PyObject *seq = POP();
2360
Victor Stinner438a12d2019-05-24 17:01:38 +02002361 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002362 stack_pointer + totalargs)) {
2363 stack_pointer += totalargs;
2364 } else {
2365 Py_DECREF(seq);
2366 goto error;
2367 }
2368 Py_DECREF(seq);
2369 DISPATCH();
2370 }
2371
Benjamin Petersonddd19492018-09-16 22:38:02 -07002372 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002373 PyObject *name = GETITEM(names, oparg);
2374 PyObject *owner = TOP();
2375 PyObject *v = SECOND();
2376 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002377 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002378 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002379 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002380 Py_DECREF(owner);
2381 if (err != 0)
2382 goto error;
2383 DISPATCH();
2384 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002385
Benjamin Petersonddd19492018-09-16 22:38:02 -07002386 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002387 PyObject *name = GETITEM(names, oparg);
2388 PyObject *owner = POP();
2389 int err;
2390 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2391 Py_DECREF(owner);
2392 if (err != 0)
2393 goto error;
2394 DISPATCH();
2395 }
2396
Benjamin Petersonddd19492018-09-16 22:38:02 -07002397 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002398 PyObject *name = GETITEM(names, oparg);
2399 PyObject *v = POP();
2400 int err;
2401 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002402 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002403 if (err != 0)
2404 goto error;
2405 DISPATCH();
2406 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002407
Benjamin Petersonddd19492018-09-16 22:38:02 -07002408 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002409 PyObject *name = GETITEM(names, oparg);
2410 int err;
2411 err = PyDict_DelItem(f->f_globals, name);
2412 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002413 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2414 format_exc_check_arg(tstate, PyExc_NameError,
2415 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002416 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002417 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002418 }
2419 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002420 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002421
Benjamin Petersonddd19492018-09-16 22:38:02 -07002422 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002423 PyObject *name = GETITEM(names, oparg);
2424 PyObject *locals = f->f_locals;
2425 PyObject *v;
2426 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002427 _PyErr_Format(tstate, PyExc_SystemError,
2428 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002429 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002430 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002431 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002432 v = PyDict_GetItemWithError(locals, name);
2433 if (v != NULL) {
2434 Py_INCREF(v);
2435 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002436 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002437 goto error;
2438 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002439 }
2440 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002441 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002442 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002443 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002444 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002445 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002446 }
2447 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002448 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002449 v = PyDict_GetItemWithError(f->f_globals, name);
2450 if (v != NULL) {
2451 Py_INCREF(v);
2452 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002453 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002454 goto error;
2455 }
2456 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002457 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002458 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002459 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002460 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002461 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002462 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002463 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002464 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002465 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002466 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002467 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002468 }
2469 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002470 v = PyObject_GetItem(f->f_builtins, name);
2471 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002472 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002473 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002474 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002475 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002476 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002477 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002478 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002479 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002480 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002481 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002482 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002483 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002484 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002485
Benjamin Petersonddd19492018-09-16 22:38:02 -07002486 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002487 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002488 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002489 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002490 && PyDict_CheckExact(f->f_builtins))
2491 {
Inada Naoki91234a12019-06-03 21:30:58 +09002492 OPCACHE_CHECK();
2493 if (co_opcache != NULL && co_opcache->optimized > 0) {
2494 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2495
2496 if (lg->globals_ver ==
2497 ((PyDictObject *)f->f_globals)->ma_version_tag
2498 && lg->builtins_ver ==
2499 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2500 {
2501 PyObject *ptr = lg->ptr;
2502 OPCACHE_STAT_GLOBAL_HIT();
2503 assert(ptr != NULL);
2504 Py_INCREF(ptr);
2505 PUSH(ptr);
2506 DISPATCH();
2507 }
2508 }
2509
2510 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002511 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002512 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002513 name);
2514 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002515 if (!_PyErr_OCCURRED()) {
2516 /* _PyDict_LoadGlobal() returns NULL without raising
2517 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002518 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002519 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002520 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002521 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002522 }
Inada Naoki91234a12019-06-03 21:30:58 +09002523
2524 if (co_opcache != NULL) {
2525 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2526
2527 if (co_opcache->optimized == 0) {
2528 /* Wasn't optimized before. */
2529 OPCACHE_STAT_GLOBAL_OPT();
2530 } else {
2531 OPCACHE_STAT_GLOBAL_MISS();
2532 }
2533
2534 co_opcache->optimized = 1;
2535 lg->globals_ver =
2536 ((PyDictObject *)f->f_globals)->ma_version_tag;
2537 lg->builtins_ver =
2538 ((PyDictObject *)f->f_builtins)->ma_version_tag;
2539 lg->ptr = v; /* borrowed */
2540 }
2541
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002542 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002543 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002544 else {
2545 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002546
2547 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09002548 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002549 v = PyObject_GetItem(f->f_globals, name);
2550 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002551 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002552 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002553 }
2554 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002555
Victor Stinnerb4efc962015-11-20 09:24:02 +01002556 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002557 v = PyObject_GetItem(f->f_builtins, name);
2558 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002559 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002560 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002561 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002562 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002563 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002564 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002565 }
2566 }
2567 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002568 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002569 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002570 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002571
Benjamin Petersonddd19492018-09-16 22:38:02 -07002572 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002573 PyObject *v = GETLOCAL(oparg);
2574 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002575 SETLOCAL(oparg, NULL);
2576 DISPATCH();
2577 }
2578 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002579 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002580 UNBOUNDLOCAL_ERROR_MSG,
2581 PyTuple_GetItem(co->co_varnames, oparg)
2582 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002583 goto error;
2584 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002585
Benjamin Petersonddd19492018-09-16 22:38:02 -07002586 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002587 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002588 PyObject *oldobj = PyCell_GET(cell);
2589 if (oldobj != NULL) {
2590 PyCell_SET(cell, NULL);
2591 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002592 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002593 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002594 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002595 goto error;
2596 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002597
Benjamin Petersonddd19492018-09-16 22:38:02 -07002598 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002599 PyObject *cell = freevars[oparg];
2600 Py_INCREF(cell);
2601 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002602 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002603 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002604
Benjamin Petersonddd19492018-09-16 22:38:02 -07002605 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002606 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002607 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002608 assert(locals);
2609 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2610 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2611 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2612 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2613 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002614 value = PyDict_GetItemWithError(locals, name);
2615 if (value != NULL) {
2616 Py_INCREF(value);
2617 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002618 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002619 goto error;
2620 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002621 }
2622 else {
2623 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002624 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002625 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002626 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002627 }
2628 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002629 }
2630 }
2631 if (!value) {
2632 PyObject *cell = freevars[oparg];
2633 value = PyCell_GET(cell);
2634 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002635 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002636 goto error;
2637 }
2638 Py_INCREF(value);
2639 }
2640 PUSH(value);
2641 DISPATCH();
2642 }
2643
Benjamin Petersonddd19492018-09-16 22:38:02 -07002644 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002645 PyObject *cell = freevars[oparg];
2646 PyObject *value = PyCell_GET(cell);
2647 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002648 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002649 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002650 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002651 Py_INCREF(value);
2652 PUSH(value);
2653 DISPATCH();
2654 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002655
Benjamin Petersonddd19492018-09-16 22:38:02 -07002656 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002657 PyObject *v = POP();
2658 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002659 PyObject *oldobj = PyCell_GET(cell);
2660 PyCell_SET(cell, v);
2661 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002662 DISPATCH();
2663 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002664
Benjamin Petersonddd19492018-09-16 22:38:02 -07002665 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002666 PyObject *str;
2667 PyObject *empty = PyUnicode_New(0, 0);
2668 if (empty == NULL) {
2669 goto error;
2670 }
2671 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2672 Py_DECREF(empty);
2673 if (str == NULL)
2674 goto error;
2675 while (--oparg >= 0) {
2676 PyObject *item = POP();
2677 Py_DECREF(item);
2678 }
2679 PUSH(str);
2680 DISPATCH();
2681 }
2682
Benjamin Petersonddd19492018-09-16 22:38:02 -07002683 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002684 PyObject *tup = PyTuple_New(oparg);
2685 if (tup == NULL)
2686 goto error;
2687 while (--oparg >= 0) {
2688 PyObject *item = POP();
2689 PyTuple_SET_ITEM(tup, oparg, item);
2690 }
2691 PUSH(tup);
2692 DISPATCH();
2693 }
2694
Benjamin Petersonddd19492018-09-16 22:38:02 -07002695 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002696 PyObject *list = PyList_New(oparg);
2697 if (list == NULL)
2698 goto error;
2699 while (--oparg >= 0) {
2700 PyObject *item = POP();
2701 PyList_SET_ITEM(list, oparg, item);
2702 }
2703 PUSH(list);
2704 DISPATCH();
2705 }
2706
Benjamin Petersonddd19492018-09-16 22:38:02 -07002707 case TARGET(BUILD_TUPLE_UNPACK_WITH_CALL):
2708 case TARGET(BUILD_TUPLE_UNPACK):
2709 case TARGET(BUILD_LIST_UNPACK): {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002710 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002711 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002712 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002713 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002714
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002715 if (sum == NULL)
2716 goto error;
2717
2718 for (i = oparg; i > 0; i--) {
2719 PyObject *none_val;
2720
2721 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2722 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002723 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
Victor Stinner438a12d2019-05-24 17:01:38 +02002724 _PyErr_ExceptionMatches(tstate, PyExc_TypeError))
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002725 {
Victor Stinner438a12d2019-05-24 17:01:38 +02002726 check_args_iterable(tstate, PEEK(1 + oparg), PEEK(i));
Serhiy Storchaka73442852016-10-02 10:33:46 +03002727 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002728 Py_DECREF(sum);
2729 goto error;
2730 }
2731 Py_DECREF(none_val);
2732 }
2733
2734 if (convert_to_tuple) {
2735 return_value = PyList_AsTuple(sum);
2736 Py_DECREF(sum);
2737 if (return_value == NULL)
2738 goto error;
2739 }
2740 else {
2741 return_value = sum;
2742 }
2743
2744 while (oparg--)
2745 Py_DECREF(POP());
2746 PUSH(return_value);
2747 DISPATCH();
2748 }
2749
Benjamin Petersonddd19492018-09-16 22:38:02 -07002750 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002751 PyObject *set = PySet_New(NULL);
2752 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002753 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002754 if (set == NULL)
2755 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002756 for (i = oparg; i > 0; i--) {
2757 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002758 if (err == 0)
2759 err = PySet_Add(set, item);
2760 Py_DECREF(item);
2761 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002762 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002763 if (err != 0) {
2764 Py_DECREF(set);
2765 goto error;
2766 }
2767 PUSH(set);
2768 DISPATCH();
2769 }
2770
Benjamin Petersonddd19492018-09-16 22:38:02 -07002771 case TARGET(BUILD_SET_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002772 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002773 PyObject *sum = PySet_New(NULL);
2774 if (sum == NULL)
2775 goto error;
2776
2777 for (i = oparg; i > 0; i--) {
2778 if (_PySet_Update(sum, PEEK(i)) < 0) {
2779 Py_DECREF(sum);
2780 goto error;
2781 }
2782 }
2783
2784 while (oparg--)
2785 Py_DECREF(POP());
2786 PUSH(sum);
2787 DISPATCH();
2788 }
2789
Benjamin Petersonddd19492018-09-16 22:38:02 -07002790 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002791 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002792 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2793 if (map == NULL)
2794 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002795 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002796 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002797 PyObject *key = PEEK(2*i);
2798 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002799 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002800 if (err != 0) {
2801 Py_DECREF(map);
2802 goto error;
2803 }
2804 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002805
2806 while (oparg--) {
2807 Py_DECREF(POP());
2808 Py_DECREF(POP());
2809 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002810 PUSH(map);
2811 DISPATCH();
2812 }
2813
Benjamin Petersonddd19492018-09-16 22:38:02 -07002814 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002815 _Py_IDENTIFIER(__annotations__);
2816 int err;
2817 PyObject *ann_dict;
2818 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002819 _PyErr_Format(tstate, PyExc_SystemError,
2820 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002821 goto error;
2822 }
2823 /* check if __annotations__ in locals()... */
2824 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002825 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002826 &PyId___annotations__);
2827 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002828 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002829 goto error;
2830 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002831 /* ...if not, create a new one */
2832 ann_dict = PyDict_New();
2833 if (ann_dict == NULL) {
2834 goto error;
2835 }
2836 err = _PyDict_SetItemId(f->f_locals,
2837 &PyId___annotations__, ann_dict);
2838 Py_DECREF(ann_dict);
2839 if (err != 0) {
2840 goto error;
2841 }
2842 }
2843 }
2844 else {
2845 /* do the same if locals() is not a dict */
2846 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2847 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002848 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002849 }
2850 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2851 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002852 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002853 goto error;
2854 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002855 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002856 ann_dict = PyDict_New();
2857 if (ann_dict == NULL) {
2858 goto error;
2859 }
2860 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2861 Py_DECREF(ann_dict);
2862 if (err != 0) {
2863 goto error;
2864 }
2865 }
2866 else {
2867 Py_DECREF(ann_dict);
2868 }
2869 }
2870 DISPATCH();
2871 }
2872
Benjamin Petersonddd19492018-09-16 22:38:02 -07002873 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002874 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002875 PyObject *map;
2876 PyObject *keys = TOP();
2877 if (!PyTuple_CheckExact(keys) ||
2878 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002879 _PyErr_SetString(tstate, PyExc_SystemError,
2880 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002881 goto error;
2882 }
2883 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2884 if (map == NULL) {
2885 goto error;
2886 }
2887 for (i = oparg; i > 0; i--) {
2888 int err;
2889 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2890 PyObject *value = PEEK(i + 1);
2891 err = PyDict_SetItem(map, key, value);
2892 if (err != 0) {
2893 Py_DECREF(map);
2894 goto error;
2895 }
2896 }
2897
2898 Py_DECREF(POP());
2899 while (oparg--) {
2900 Py_DECREF(POP());
2901 }
2902 PUSH(map);
2903 DISPATCH();
2904 }
2905
Benjamin Petersonddd19492018-09-16 22:38:02 -07002906 case TARGET(BUILD_MAP_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002907 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002908 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002909 if (sum == NULL)
2910 goto error;
2911
2912 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002913 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002914 if (PyDict_Update(sum, arg) < 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002915 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
2916 _PyErr_Format(tstate, PyExc_TypeError,
2917 "'%.200s' object is not a mapping",
2918 arg->ob_type->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002919 }
2920 Py_DECREF(sum);
2921 goto error;
2922 }
2923 }
2924
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002925 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002926 Py_DECREF(POP());
2927 PUSH(sum);
2928 DISPATCH();
2929 }
2930
Benjamin Petersonddd19492018-09-16 22:38:02 -07002931 case TARGET(BUILD_MAP_UNPACK_WITH_CALL): {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002932 Py_ssize_t i;
2933 PyObject *sum = PyDict_New();
2934 if (sum == NULL)
2935 goto error;
2936
2937 for (i = oparg; i > 0; i--) {
2938 PyObject *arg = PEEK(i);
2939 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002940 Py_DECREF(sum);
Victor Stinner438a12d2019-05-24 17:01:38 +02002941 format_kwargs_error(tstate, PEEK(2 + oparg), arg);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002942 goto error;
2943 }
2944 }
2945
2946 while (oparg--)
2947 Py_DECREF(POP());
2948 PUSH(sum);
Brandt Bucherf185a732019-09-28 17:12:49 -07002949 PREDICT(CALL_FUNCTION_EX);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002950 DISPATCH();
2951 }
2952
Benjamin Petersonddd19492018-09-16 22:38:02 -07002953 case TARGET(MAP_ADD): {
Jörn Heisslerc8a35412019-06-22 16:40:55 +02002954 PyObject *value = TOP();
2955 PyObject *key = SECOND();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002956 PyObject *map;
2957 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002958 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002959 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002960 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002961 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002962 Py_DECREF(value);
2963 Py_DECREF(key);
2964 if (err != 0)
2965 goto error;
2966 PREDICT(JUMP_ABSOLUTE);
2967 DISPATCH();
2968 }
2969
Benjamin Petersonddd19492018-09-16 22:38:02 -07002970 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002971 PyObject *name = GETITEM(names, oparg);
2972 PyObject *owner = TOP();
2973 PyObject *res = PyObject_GetAttr(owner, name);
2974 Py_DECREF(owner);
2975 SET_TOP(res);
2976 if (res == NULL)
2977 goto error;
2978 DISPATCH();
2979 }
2980
Benjamin Petersonddd19492018-09-16 22:38:02 -07002981 case TARGET(COMPARE_OP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002982 PyObject *right = POP();
2983 PyObject *left = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002984 PyObject *res = cmp_outcome(tstate, oparg, left, right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002985 Py_DECREF(left);
2986 Py_DECREF(right);
2987 SET_TOP(res);
2988 if (res == NULL)
2989 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002990 PREDICT(POP_JUMP_IF_FALSE);
2991 PREDICT(POP_JUMP_IF_TRUE);
2992 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002993 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002994
Benjamin Petersonddd19492018-09-16 22:38:02 -07002995 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002996 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002997 PyObject *fromlist = POP();
2998 PyObject *level = TOP();
2999 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003000 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003001 Py_DECREF(level);
3002 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003003 SET_TOP(res);
3004 if (res == NULL)
3005 goto error;
3006 DISPATCH();
3007 }
3008
Benjamin Petersonddd19492018-09-16 22:38:02 -07003009 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003010 PyObject *from = POP(), *locals;
3011 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003012 if (PyFrame_FastToLocalsWithError(f) < 0) {
3013 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01003014 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003015 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01003016
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003017 locals = f->f_locals;
3018 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003019 _PyErr_SetString(tstate, PyExc_SystemError,
3020 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003021 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003022 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003023 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003024 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003025 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003026 Py_DECREF(from);
3027 if (err != 0)
3028 goto error;
3029 DISPATCH();
3030 }
Guido van Rossum25831651993-05-19 14:50:45 +00003031
Benjamin Petersonddd19492018-09-16 22:38:02 -07003032 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003033 PyObject *name = GETITEM(names, oparg);
3034 PyObject *from = TOP();
3035 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003036 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003037 PUSH(res);
3038 if (res == NULL)
3039 goto error;
3040 DISPATCH();
3041 }
Thomas Wouters52152252000-08-17 22:55:00 +00003042
Benjamin Petersonddd19492018-09-16 22:38:02 -07003043 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003044 JUMPBY(oparg);
3045 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003046 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003047
Benjamin Petersonddd19492018-09-16 22:38:02 -07003048 case TARGET(POP_JUMP_IF_FALSE): {
3049 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003050 PyObject *cond = POP();
3051 int err;
3052 if (cond == Py_True) {
3053 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003054 FAST_DISPATCH();
3055 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003056 if (cond == Py_False) {
3057 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003058 JUMPTO(oparg);
3059 FAST_DISPATCH();
3060 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003061 err = PyObject_IsTrue(cond);
3062 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003063 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003064 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003065 else if (err == 0)
3066 JUMPTO(oparg);
3067 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003068 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003069 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003070 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003071
Benjamin Petersonddd19492018-09-16 22:38:02 -07003072 case TARGET(POP_JUMP_IF_TRUE): {
3073 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003074 PyObject *cond = POP();
3075 int err;
3076 if (cond == Py_False) {
3077 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003078 FAST_DISPATCH();
3079 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003080 if (cond == Py_True) {
3081 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003082 JUMPTO(oparg);
3083 FAST_DISPATCH();
3084 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003085 err = PyObject_IsTrue(cond);
3086 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003087 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003088 JUMPTO(oparg);
3089 }
3090 else if (err == 0)
3091 ;
3092 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003093 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003094 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003095 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003096
Benjamin Petersonddd19492018-09-16 22:38:02 -07003097 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003098 PyObject *cond = TOP();
3099 int err;
3100 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003101 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003102 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003103 FAST_DISPATCH();
3104 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003105 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003106 JUMPTO(oparg);
3107 FAST_DISPATCH();
3108 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003109 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003110 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003111 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003112 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003113 }
3114 else if (err == 0)
3115 JUMPTO(oparg);
3116 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003117 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003118 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003119 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003120
Benjamin Petersonddd19492018-09-16 22:38:02 -07003121 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003122 PyObject *cond = TOP();
3123 int err;
3124 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003125 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003126 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003127 FAST_DISPATCH();
3128 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003129 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003130 JUMPTO(oparg);
3131 FAST_DISPATCH();
3132 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003133 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003134 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003135 JUMPTO(oparg);
3136 }
3137 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003138 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003139 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003140 }
3141 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003142 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003143 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003144 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003145
Benjamin Petersonddd19492018-09-16 22:38:02 -07003146 case TARGET(JUMP_ABSOLUTE): {
3147 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003148 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003149#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003150 /* Enabling this path speeds-up all while and for-loops by bypassing
3151 the per-loop checks for signals. By default, this should be turned-off
3152 because it prevents detection of a control-break in tight loops like
3153 "while 1: pass". Compile with this option turned-on when you need
3154 the speed-up and do not need break checking inside tight loops (ones
3155 that contain only instructions ending with FAST_DISPATCH).
3156 */
3157 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003158#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003159 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003160#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003161 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003162
Benjamin Petersonddd19492018-09-16 22:38:02 -07003163 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003164 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003165 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003166 PyObject *iter = PyObject_GetIter(iterable);
3167 Py_DECREF(iterable);
3168 SET_TOP(iter);
3169 if (iter == NULL)
3170 goto error;
3171 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003172 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003173 DISPATCH();
3174 }
3175
Benjamin Petersonddd19492018-09-16 22:38:02 -07003176 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04003177 /* before: [obj]; after [getiter(obj)] */
3178 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003179 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003180 if (PyCoro_CheckExact(iterable)) {
3181 /* `iterable` is a coroutine */
3182 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3183 /* and it is used in a 'yield from' expression of a
3184 regular generator. */
3185 Py_DECREF(iterable);
3186 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003187 _PyErr_SetString(tstate, PyExc_TypeError,
3188 "cannot 'yield from' a coroutine object "
3189 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04003190 goto error;
3191 }
3192 }
3193 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003194 /* `iterable` is not a generator. */
3195 iter = PyObject_GetIter(iterable);
3196 Py_DECREF(iterable);
3197 SET_TOP(iter);
3198 if (iter == NULL)
3199 goto error;
3200 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003201 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003202 DISPATCH();
3203 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003204
Benjamin Petersonddd19492018-09-16 22:38:02 -07003205 case TARGET(FOR_ITER): {
3206 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003207 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003208 PyObject *iter = TOP();
3209 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
3210 if (next != NULL) {
3211 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003212 PREDICT(STORE_FAST);
3213 PREDICT(UNPACK_SEQUENCE);
3214 DISPATCH();
3215 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003216 if (_PyErr_Occurred(tstate)) {
3217 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003218 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003219 }
3220 else if (tstate->c_tracefunc != NULL) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003221 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Victor Stinner438a12d2019-05-24 17:01:38 +02003222 }
3223 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003224 }
3225 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003226 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003227 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003228 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003229 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003230 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003231 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003232
Benjamin Petersonddd19492018-09-16 22:38:02 -07003233 case TARGET(SETUP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003234 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003235 STACK_LEVEL());
3236 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003237 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003238
Benjamin Petersonddd19492018-09-16 22:38:02 -07003239 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003240 _Py_IDENTIFIER(__aexit__);
3241 _Py_IDENTIFIER(__aenter__);
3242
3243 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003244 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__),
Yury Selivanov75445082015-05-11 22:57:16 -04003245 *enter;
3246 PyObject *res;
3247 if (exit == NULL)
3248 goto error;
3249 SET_TOP(exit);
Victor Stinner438a12d2019-05-24 17:01:38 +02003250 enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04003251 Py_DECREF(mgr);
3252 if (enter == NULL)
3253 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003254 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003255 Py_DECREF(enter);
3256 if (res == NULL)
3257 goto error;
3258 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003259 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003260 DISPATCH();
3261 }
3262
Benjamin Petersonddd19492018-09-16 22:38:02 -07003263 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003264 PyObject *res = POP();
3265 /* Setup the finally block before pushing the result
3266 of __aenter__ on the stack. */
3267 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3268 STACK_LEVEL());
3269 PUSH(res);
3270 DISPATCH();
3271 }
3272
Benjamin Petersonddd19492018-09-16 22:38:02 -07003273 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003274 _Py_IDENTIFIER(__exit__);
3275 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003276 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003277 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003278 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003279 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08003280 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003281 }
3282 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003283 if (exit == NULL) {
3284 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003285 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003286 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003287 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003288 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003289 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003290 Py_DECREF(enter);
3291 if (res == NULL)
3292 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003293 /* Setup the finally block before pushing the result
3294 of __enter__ on the stack. */
3295 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3296 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003297
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003298 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003299 DISPATCH();
3300 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003301
Benjamin Petersonddd19492018-09-16 22:38:02 -07003302 case TARGET(WITH_CLEANUP_START): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003303 /* At the top of the stack are 1 or 6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003304 how/why we entered the finally clause:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003305 - TOP = NULL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003306 - (TOP, SECOND, THIRD) = exc_info()
3307 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003308 Below them is EXIT, the context.__exit__ or context.__aexit__
3309 bound method.
3310 In the first case, we must call
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003311 EXIT(None, None, None)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003312 otherwise we must call
3313 EXIT(TOP, SECOND, THIRD)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003314
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003315 In the first case, we remove EXIT from the
3316 stack, leaving TOP, and push TOP on the stack.
3317 Otherwise we shift the bottom 3 values of the
3318 stack down, replace the empty spot with NULL, and push
3319 None on the stack.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003320
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003321 Finally we push the result of the call.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003322 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003323 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003324 PyObject *exc, *val, *tb, *res;
3325
3326 val = tb = Py_None;
3327 exc = TOP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003328 if (exc == NULL) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003329 STACK_SHRINK(1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003330 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003331 SET_TOP(exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003332 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003333 }
3334 else {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003335 assert(PyExceptionClass_Check(exc));
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003336 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003337 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003338 val = SECOND();
3339 tb = THIRD();
3340 tp2 = FOURTH();
3341 exc2 = PEEK(5);
3342 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003343 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003344 SET_VALUE(7, tb2);
3345 SET_VALUE(6, exc2);
3346 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003347 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3348 SET_FOURTH(NULL);
3349 /* We just shifted the stack down, so we have
3350 to tell the except handler block that the
3351 values are lower than it expects. */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003352 assert(f->f_iblock > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003353 block = &f->f_blockstack[f->f_iblock - 1];
3354 assert(block->b_type == EXCEPT_HANDLER);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003355 assert(block->b_level > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003356 block->b_level--;
3357 }
Victor Stinner842cfff2016-12-01 14:45:31 +01003358
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003359 PyObject *stack[4] = {NULL, exc, val, tb};
3360 res = _PyObject_Vectorcall(exit_func, stack + 1,
3361 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003362 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003363 if (res == NULL)
3364 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003365
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003366 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003367 PUSH(exc);
3368 PUSH(res);
3369 PREDICT(WITH_CLEANUP_FINISH);
3370 DISPATCH();
3371 }
3372
Benjamin Petersonddd19492018-09-16 22:38:02 -07003373 case TARGET(WITH_CLEANUP_FINISH): {
3374 PREDICTED(WITH_CLEANUP_FINISH);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003375 /* TOP = the result of calling the context.__exit__ bound method
3376 SECOND = either None or exception type
3377
3378 If SECOND is None below is NULL or the return address,
3379 otherwise below are 7 values representing an exception.
3380 */
Yury Selivanov75445082015-05-11 22:57:16 -04003381 PyObject *res = POP();
3382 PyObject *exc = POP();
3383 int err;
3384
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003385 if (exc != Py_None)
3386 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003387 else
3388 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003389
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003390 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003391 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003393 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003394 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003395 else if (err > 0) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003396 /* There was an exception and a True return.
3397 * We must manually unwind the EXCEPT_HANDLER block
3398 * which was created when the exception was caught,
Quan Tian3bd0d622018-10-20 05:30:03 +08003399 * otherwise the stack will be in an inconsistent state.
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003400 */
3401 PyTryBlock *b = PyFrame_BlockPop(f);
3402 assert(b->b_type == EXCEPT_HANDLER);
3403 UNWIND_EXCEPT_HANDLER(b);
3404 PUSH(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003405 }
3406 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003407 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003408 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003409
Benjamin Petersonddd19492018-09-16 22:38:02 -07003410 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10003411 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003412 PyObject *name = GETITEM(names, oparg);
3413 PyObject *obj = TOP();
3414 PyObject *meth = NULL;
3415
3416 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3417
Yury Selivanovf2392132016-12-13 19:03:51 -05003418 if (meth == NULL) {
3419 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003420 goto error;
3421 }
3422
3423 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003424 /* We can bypass temporary bound method object.
3425 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003426
INADA Naoki015bce62017-01-16 17:23:30 +09003427 meth | self | arg1 | ... | argN
3428 */
3429 SET_TOP(meth);
3430 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003431 }
3432 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003433 /* meth is not an unbound method (but a regular attr, or
3434 something was returned by a descriptor protocol). Set
3435 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003436 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003437
3438 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003439 */
INADA Naoki015bce62017-01-16 17:23:30 +09003440 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003441 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003442 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003443 }
3444 DISPATCH();
3445 }
3446
Benjamin Petersonddd19492018-09-16 22:38:02 -07003447 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003448 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003449 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003450
3451 sp = stack_pointer;
3452
INADA Naoki015bce62017-01-16 17:23:30 +09003453 meth = PEEK(oparg + 2);
3454 if (meth == NULL) {
3455 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3456 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003457
3458 Stack layout:
3459
INADA Naoki015bce62017-01-16 17:23:30 +09003460 ... | NULL | callable | arg1 | ... | argN
3461 ^- TOP()
3462 ^- (-oparg)
3463 ^- (-oparg-1)
3464 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003465
Ville Skyttä49b27342017-08-03 09:00:59 +03003466 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003467 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003468 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003469 res = call_function(tstate, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003470 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003471 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003472 }
3473 else {
3474 /* This is a method call. Stack layout:
3475
INADA Naoki015bce62017-01-16 17:23:30 +09003476 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003477 ^- TOP()
3478 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003479 ^- (-oparg-1)
3480 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003481
INADA Naoki015bce62017-01-16 17:23:30 +09003482 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003483 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003484 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003485 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003486 res = call_function(tstate, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003487 stack_pointer = sp;
3488 }
3489
3490 PUSH(res);
3491 if (res == NULL)
3492 goto error;
3493 DISPATCH();
3494 }
3495
Benjamin Petersonddd19492018-09-16 22:38:02 -07003496 case TARGET(CALL_FUNCTION): {
3497 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003498 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003499 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003500 res = call_function(tstate, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003501 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003502 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003503 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003504 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003505 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003506 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003507 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003508
Benjamin Petersonddd19492018-09-16 22:38:02 -07003509 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003510 PyObject **sp, *res, *names;
3511
3512 names = POP();
Jeroen Demeyer05677862019-08-16 12:41:27 +02003513 assert(PyTuple_Check(names));
3514 assert(PyTuple_GET_SIZE(names) <= oparg);
3515 /* We assume without checking that names contains only strings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003516 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003517 res = call_function(tstate, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003518 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003519 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003520 Py_DECREF(names);
3521
3522 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003523 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003524 }
3525 DISPATCH();
3526 }
3527
Benjamin Petersonddd19492018-09-16 22:38:02 -07003528 case TARGET(CALL_FUNCTION_EX): {
Brandt Bucherf185a732019-09-28 17:12:49 -07003529 PREDICTED(CALL_FUNCTION_EX);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003530 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003531 if (oparg & 0x01) {
3532 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003533 if (!PyDict_CheckExact(kwargs)) {
3534 PyObject *d = PyDict_New();
3535 if (d == NULL)
3536 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003537 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003538 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02003539 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003540 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003541 goto error;
3542 }
3543 Py_DECREF(kwargs);
3544 kwargs = d;
3545 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003546 assert(PyDict_CheckExact(kwargs));
3547 }
3548 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003549 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003550 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003551 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003552 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003553 goto error;
3554 }
3555 Py_SETREF(callargs, PySequence_Tuple(callargs));
3556 if (callargs == NULL) {
3557 goto error;
3558 }
3559 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003560 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003561
Victor Stinner09532fe2019-05-10 23:39:09 +02003562 result = do_call_core(tstate, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003563 Py_DECREF(func);
3564 Py_DECREF(callargs);
3565 Py_XDECREF(kwargs);
3566
3567 SET_TOP(result);
3568 if (result == NULL) {
3569 goto error;
3570 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003571 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003572 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003573
Benjamin Petersonddd19492018-09-16 22:38:02 -07003574 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003575 PyObject *qualname = POP();
3576 PyObject *codeobj = POP();
3577 PyFunctionObject *func = (PyFunctionObject *)
3578 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003579
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003580 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003581 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003582 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003583 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003584 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003585
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003586 if (oparg & 0x08) {
3587 assert(PyTuple_CheckExact(TOP()));
3588 func ->func_closure = POP();
3589 }
3590 if (oparg & 0x04) {
3591 assert(PyDict_CheckExact(TOP()));
3592 func->func_annotations = POP();
3593 }
3594 if (oparg & 0x02) {
3595 assert(PyDict_CheckExact(TOP()));
3596 func->func_kwdefaults = POP();
3597 }
3598 if (oparg & 0x01) {
3599 assert(PyTuple_CheckExact(TOP()));
3600 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003601 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003602
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003603 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003604 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003605 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003606
Benjamin Petersonddd19492018-09-16 22:38:02 -07003607 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003608 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003609 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003610 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003611 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003612 step = NULL;
3613 stop = POP();
3614 start = TOP();
3615 slice = PySlice_New(start, stop, step);
3616 Py_DECREF(start);
3617 Py_DECREF(stop);
3618 Py_XDECREF(step);
3619 SET_TOP(slice);
3620 if (slice == NULL)
3621 goto error;
3622 DISPATCH();
3623 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003624
Benjamin Petersonddd19492018-09-16 22:38:02 -07003625 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003626 /* Handles f-string value formatting. */
3627 PyObject *result;
3628 PyObject *fmt_spec;
3629 PyObject *value;
3630 PyObject *(*conv_fn)(PyObject *);
3631 int which_conversion = oparg & FVC_MASK;
3632 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3633
3634 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003635 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003636
3637 /* See if any conversion is specified. */
3638 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003639 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003640 case FVC_STR: conv_fn = PyObject_Str; break;
3641 case FVC_REPR: conv_fn = PyObject_Repr; break;
3642 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003643 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02003644 _PyErr_Format(tstate, PyExc_SystemError,
3645 "unexpected conversion flag %d",
3646 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003647 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003648 }
3649
3650 /* If there's a conversion function, call it and replace
3651 value with that result. Otherwise, just use value,
3652 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003653 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003654 result = conv_fn(value);
3655 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003656 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003657 Py_XDECREF(fmt_spec);
3658 goto error;
3659 }
3660 value = result;
3661 }
3662
3663 /* If value is a unicode object, and there's no fmt_spec,
3664 then we know the result of format(value) is value
3665 itself. In that case, skip calling format(). I plan to
3666 move this optimization in to PyObject_Format()
3667 itself. */
3668 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3669 /* Do nothing, just transfer ownership to result. */
3670 result = value;
3671 } else {
3672 /* Actually call format(). */
3673 result = PyObject_Format(value, fmt_spec);
3674 Py_DECREF(value);
3675 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003676 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003677 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003678 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003679 }
3680
Eric V. Smith135d5f42016-02-05 18:23:08 -05003681 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003682 DISPATCH();
3683 }
3684
Benjamin Petersonddd19492018-09-16 22:38:02 -07003685 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003686 int oldoparg = oparg;
3687 NEXTOPARG();
3688 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003689 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003690 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003691
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003692
Antoine Pitrou042b1282010-08-13 21:15:58 +00003693#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003694 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003695#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003696 default:
3697 fprintf(stderr,
3698 "XXX lineno: %d, opcode: %d\n",
3699 PyFrame_GetLineNumber(f),
3700 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02003701 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003702 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003704 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003705
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003706 /* This should never be reached. Every opcode should end with DISPATCH()
3707 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003708 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003709
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003710error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003711 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003712#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02003713 if (!_PyErr_Occurred(tstate)) {
3714 _PyErr_SetString(tstate, PyExc_SystemError,
3715 "error return without exception set");
3716 }
Victor Stinner365b6932013-07-12 00:11:58 +02003717#else
Victor Stinner438a12d2019-05-24 17:01:38 +02003718 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02003719#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003720
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003721 /* Log traceback info. */
3722 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003723
Benjamin Peterson51f46162013-01-23 08:38:47 -05003724 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003725 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3726 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003727
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003728exception_unwind:
3729 /* Unwind stacks if an exception occurred */
3730 while (f->f_iblock > 0) {
3731 /* Pop the current block. */
3732 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003733
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003734 if (b->b_type == EXCEPT_HANDLER) {
3735 UNWIND_EXCEPT_HANDLER(b);
3736 continue;
3737 }
3738 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003739 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003740 PyObject *exc, *val, *tb;
3741 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003742 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003743 /* Beware, this invalidates all b->b_* fields */
3744 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003745 PUSH(exc_info->exc_traceback);
3746 PUSH(exc_info->exc_value);
3747 if (exc_info->exc_type != NULL) {
3748 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003749 }
3750 else {
3751 Py_INCREF(Py_None);
3752 PUSH(Py_None);
3753 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003754 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003755 /* Make the raw exception data
3756 available to the handler,
3757 so a program can emulate the
3758 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02003759 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003760 if (tb != NULL)
3761 PyException_SetTraceback(val, tb);
3762 else
3763 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003764 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003765 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003766 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003767 exc_info->exc_value = val;
3768 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003769 if (tb == NULL)
3770 tb = Py_None;
3771 Py_INCREF(tb);
3772 PUSH(tb);
3773 PUSH(val);
3774 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003775 JUMPTO(handler);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003776 /* Resume normal execution */
3777 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003778 }
3779 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003780
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003781 /* End the loop as we still have an error */
3782 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003783 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003784
Pablo Galindof00828a2019-05-09 16:52:02 +01003785 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003786 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01003787
3788exit_returning:
3789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003790 /* Pop remaining stack entries. */
3791 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003792 PyObject *o = POP();
3793 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003794 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003795
Pablo Galindof00828a2019-05-09 16:52:02 +01003796exit_yielding:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003797 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003798 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003799 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3800 tstate, f, PyTrace_RETURN, retval)) {
3801 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003802 }
3803 }
3804 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003805 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3806 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003807 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003808 }
3809 }
3810 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003811
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003812 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003813exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003814 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3815 dtrace_function_return(f);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01003816 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrou58720d62013-08-05 23:26:40 +02003817 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003818 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003819
Victor Stinner17269092019-11-05 01:22:12 +01003820 return _Py_CheckFunctionResult(tstate, NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003821}
3822
Benjamin Petersonb204a422011-06-05 22:04:07 -05003823static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003824format_missing(PyThreadState *tstate, const char *kind,
3825 PyCodeObject *co, PyObject *names)
Benjamin Petersone109c702011-06-24 09:37:26 -05003826{
3827 int err;
3828 Py_ssize_t len = PyList_GET_SIZE(names);
3829 PyObject *name_str, *comma, *tail, *tmp;
3830
3831 assert(PyList_CheckExact(names));
3832 assert(len >= 1);
3833 /* Deal with the joys of natural language. */
3834 switch (len) {
3835 case 1:
3836 name_str = PyList_GET_ITEM(names, 0);
3837 Py_INCREF(name_str);
3838 break;
3839 case 2:
3840 name_str = PyUnicode_FromFormat("%U and %U",
3841 PyList_GET_ITEM(names, len - 2),
3842 PyList_GET_ITEM(names, len - 1));
3843 break;
3844 default:
3845 tail = PyUnicode_FromFormat(", %U, and %U",
3846 PyList_GET_ITEM(names, len - 2),
3847 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003848 if (tail == NULL)
3849 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003850 /* Chop off the last two objects in the list. This shouldn't actually
3851 fail, but we can't be too careful. */
3852 err = PyList_SetSlice(names, len - 2, len, NULL);
3853 if (err == -1) {
3854 Py_DECREF(tail);
3855 return;
3856 }
3857 /* Stitch everything up into a nice comma-separated list. */
3858 comma = PyUnicode_FromString(", ");
3859 if (comma == NULL) {
3860 Py_DECREF(tail);
3861 return;
3862 }
3863 tmp = PyUnicode_Join(comma, names);
3864 Py_DECREF(comma);
3865 if (tmp == NULL) {
3866 Py_DECREF(tail);
3867 return;
3868 }
3869 name_str = PyUnicode_Concat(tmp, tail);
3870 Py_DECREF(tmp);
3871 Py_DECREF(tail);
3872 break;
3873 }
3874 if (name_str == NULL)
3875 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02003876 _PyErr_Format(tstate, PyExc_TypeError,
3877 "%U() missing %i required %s argument%s: %U",
3878 co->co_name,
3879 len,
3880 kind,
3881 len == 1 ? "" : "s",
3882 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05003883 Py_DECREF(name_str);
3884}
3885
3886static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003887missing_arguments(PyThreadState *tstate, PyCodeObject *co,
3888 Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003889 PyObject **fastlocals)
3890{
Victor Stinner74319ae2016-08-25 00:04:09 +02003891 Py_ssize_t i, j = 0;
3892 Py_ssize_t start, end;
3893 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003894 const char *kind = positional ? "positional" : "keyword-only";
3895 PyObject *missing_names;
3896
3897 /* Compute the names of the arguments that are missing. */
3898 missing_names = PyList_New(missing);
3899 if (missing_names == NULL)
3900 return;
3901 if (positional) {
3902 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01003903 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003904 }
3905 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003906 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003907 end = start + co->co_kwonlyargcount;
3908 }
3909 for (i = start; i < end; i++) {
3910 if (GETLOCAL(i) == NULL) {
3911 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3912 PyObject *name = PyObject_Repr(raw);
3913 if (name == NULL) {
3914 Py_DECREF(missing_names);
3915 return;
3916 }
3917 PyList_SET_ITEM(missing_names, j++, name);
3918 }
3919 }
3920 assert(j == missing);
Victor Stinner438a12d2019-05-24 17:01:38 +02003921 format_missing(tstate, kind, co, missing_names);
Benjamin Petersone109c702011-06-24 09:37:26 -05003922 Py_DECREF(missing_names);
3923}
3924
3925static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003926too_many_positional(PyThreadState *tstate, PyCodeObject *co,
3927 Py_ssize_t given, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003928 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003929{
3930 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003931 Py_ssize_t kwonly_given = 0;
3932 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003933 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003934 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003935
Benjamin Petersone109c702011-06-24 09:37:26 -05003936 assert((co->co_flags & CO_VARARGS) == 0);
3937 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01003938 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003939 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003940 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003941 }
3942 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003943 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01003944 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003945 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01003946 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003947 }
3948 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003949 plural = (co_argcount != 1);
3950 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003951 }
3952 if (sig == NULL)
3953 return;
3954 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003955 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3956 kwonly_sig = PyUnicode_FromFormat(format,
3957 given != 1 ? "s" : "",
3958 kwonly_given,
3959 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003960 if (kwonly_sig == NULL) {
3961 Py_DECREF(sig);
3962 return;
3963 }
3964 }
3965 else {
3966 /* This will not fail. */
3967 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003968 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003969 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003970 _PyErr_Format(tstate, PyExc_TypeError,
3971 "%U() takes %U positional argument%s but %zd%U %s given",
3972 co->co_name,
3973 sig,
3974 plural ? "s" : "",
3975 given,
3976 kwonly_sig,
3977 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003978 Py_DECREF(sig);
3979 Py_DECREF(kwonly_sig);
3980}
3981
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003982static int
Victor Stinner438a12d2019-05-24 17:01:38 +02003983positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
3984 Py_ssize_t kwcount, PyObject* const* kwnames)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003985{
3986 int posonly_conflicts = 0;
3987 PyObject* posonly_names = PyList_New(0);
3988
3989 for(int k=0; k < co->co_posonlyargcount; k++){
3990 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
3991
3992 for (int k2=0; k2<kwcount; k2++){
3993 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
3994 PyObject* kwname = kwnames[k2];
3995 if (kwname == posonly_name){
3996 if(PyList_Append(posonly_names, kwname) != 0) {
3997 goto fail;
3998 }
3999 posonly_conflicts++;
4000 continue;
4001 }
4002
4003 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
4004
4005 if ( cmp > 0) {
4006 if(PyList_Append(posonly_names, kwname) != 0) {
4007 goto fail;
4008 }
4009 posonly_conflicts++;
4010 } else if (cmp < 0) {
4011 goto fail;
4012 }
4013
4014 }
4015 }
4016 if (posonly_conflicts) {
4017 PyObject* comma = PyUnicode_FromString(", ");
4018 if (comma == NULL) {
4019 goto fail;
4020 }
4021 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
4022 Py_DECREF(comma);
4023 if (error_names == NULL) {
4024 goto fail;
4025 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004026 _PyErr_Format(tstate, PyExc_TypeError,
4027 "%U() got some positional-only arguments passed"
4028 " as keyword arguments: '%U'",
4029 co->co_name, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004030 Py_DECREF(error_names);
4031 goto fail;
4032 }
4033
4034 Py_DECREF(posonly_names);
4035 return 0;
4036
4037fail:
4038 Py_XDECREF(posonly_names);
4039 return 1;
4040
4041}
4042
Guido van Rossumc2e20742006-02-27 22:32:47 +00004043/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02004044 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00004045 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00004046
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004047PyObject *
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004048_PyEval_EvalCode(PyThreadState *tstate,
4049 PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004050 PyObject *const *args, Py_ssize_t argcount,
4051 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03004052 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004053 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02004054 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004055 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00004056{
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004057 assert(tstate != NULL);
4058
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00004059 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004060 PyFrameObject *f;
4061 PyObject *retval = NULL;
4062 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004063 PyObject *x, *u;
Pablo Galindocd74e662019-06-01 18:08:04 +01004064 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004065 Py_ssize_t i, j, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02004066 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00004067
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004068 if (globals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004069 _PyErr_SetString(tstate, PyExc_SystemError,
4070 "PyEval_EvalCodeEx: NULL globals");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004071 return NULL;
4072 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004073
Victor Stinnerc7020012016-08-16 23:40:29 +02004074 /* Create the frame */
INADA Naoki5a625d02016-12-24 20:19:08 +09004075 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02004076 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004077 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02004078 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004079 fastlocals = f->f_localsplus;
4080 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00004081
Victor Stinnerc7020012016-08-16 23:40:29 +02004082 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004083 if (co->co_flags & CO_VARKEYWORDS) {
4084 kwdict = PyDict_New();
4085 if (kwdict == NULL)
4086 goto fail;
4087 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02004088 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004089 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02004090 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004091 SETLOCAL(i, kwdict);
4092 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004093 else {
4094 kwdict = NULL;
4095 }
4096
Pablo Galindocd74e662019-06-01 18:08:04 +01004097 /* Copy all positional arguments into local variables */
4098 if (argcount > co->co_argcount) {
4099 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02004100 }
4101 else {
4102 n = argcount;
4103 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004104 for (j = 0; j < n; j++) {
4105 x = args[j];
4106 Py_INCREF(x);
4107 SETLOCAL(j, x);
4108 }
4109
Victor Stinnerc7020012016-08-16 23:40:29 +02004110 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004111 if (co->co_flags & CO_VARARGS) {
Sergey Fedoseev234531b2019-02-25 21:59:12 +05004112 u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02004113 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004114 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004115 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004116 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004117 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004118
Serhiy Storchakab7281052016-09-12 00:52:40 +03004119 /* Handle keyword arguments passed as two strided arrays */
4120 kwcount *= kwstep;
4121 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004122 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03004123 PyObject *keyword = kwnames[i];
4124 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02004125 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004126
Benjamin Petersonb204a422011-06-05 22:04:07 -05004127 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004128 _PyErr_Format(tstate, PyExc_TypeError,
4129 "%U() keywords must be strings",
4130 co->co_name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004131 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004132 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004133
Benjamin Petersonb204a422011-06-05 22:04:07 -05004134 /* Speed hack: do raw pointer compares. As names are
4135 normally interned this should almost always hit. */
4136 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004137 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004138 PyObject *name = co_varnames[j];
4139 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004140 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004141 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004142 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004143
Benjamin Petersonb204a422011-06-05 22:04:07 -05004144 /* Slow fallback, just in case */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004145 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004146 PyObject *name = co_varnames[j];
4147 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
4148 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004149 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004150 }
4151 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004152 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004153 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004154 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004155
Victor Stinner231d1f32017-01-11 02:12:06 +01004156 assert(j >= total_args);
4157 if (kwdict == NULL) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004158
Victor Stinner438a12d2019-05-24 17:01:38 +02004159 if (co->co_posonlyargcount
4160 && positional_only_passed_as_keyword(tstate, co,
4161 kwcount, kwnames))
4162 {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004163 goto fail;
4164 }
4165
Victor Stinner438a12d2019-05-24 17:01:38 +02004166 _PyErr_Format(tstate, PyExc_TypeError,
4167 "%U() got an unexpected keyword argument '%S'",
4168 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004169 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004170 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004171
Christian Heimes0bd447f2013-07-20 14:48:10 +02004172 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4173 goto fail;
4174 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004175 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004176
Benjamin Petersonb204a422011-06-05 22:04:07 -05004177 kw_found:
4178 if (GETLOCAL(j) != NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004179 _PyErr_Format(tstate, PyExc_TypeError,
4180 "%U() got multiple values for argument '%S'",
4181 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004182 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004183 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004184 Py_INCREF(value);
4185 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004186 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004187
4188 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004189 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004190 too_many_positional(tstate, co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004191 goto fail;
4192 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004193
4194 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004195 if (argcount < co->co_argcount) {
4196 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004197 Py_ssize_t missing = 0;
4198 for (i = argcount; i < m; i++) {
4199 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004200 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004201 }
4202 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004203 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004204 missing_arguments(tstate, co, missing, defcount, fastlocals);
Benjamin Petersone109c702011-06-24 09:37:26 -05004205 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004206 }
4207 if (n > m)
4208 i = n - m;
4209 else
4210 i = 0;
4211 for (; i < defcount; i++) {
4212 if (GETLOCAL(m+i) == NULL) {
4213 PyObject *def = defs[i];
4214 Py_INCREF(def);
4215 SETLOCAL(m+i, def);
4216 }
4217 }
4218 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004219
4220 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004221 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004222 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004223 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004224 PyObject *name;
4225 if (GETLOCAL(i) != NULL)
4226 continue;
4227 name = PyTuple_GET_ITEM(co->co_varnames, i);
4228 if (kwdefs != NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004229 PyObject *def = PyDict_GetItemWithError(kwdefs, name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004230 if (def) {
4231 Py_INCREF(def);
4232 SETLOCAL(i, def);
4233 continue;
4234 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004235 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004236 goto fail;
4237 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004238 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004239 missing++;
4240 }
4241 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004242 missing_arguments(tstate, co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004243 goto fail;
4244 }
4245 }
4246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004247 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004248 vars into frame. */
4249 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004250 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004251 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004252 /* Possibly account for the cell variable being an argument. */
4253 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004254 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004255 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004256 /* Clear the local copy. */
4257 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004258 }
4259 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004260 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004261 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004262 if (c == NULL)
4263 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004264 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004265 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004266
4267 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004268 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4269 PyObject *o = PyTuple_GET_ITEM(closure, i);
4270 Py_INCREF(o);
4271 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004272 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004273
Yury Selivanoveb636452016-09-08 22:01:51 -07004274 /* Handle generator/coroutine/asynchronous generator */
4275 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004276 PyObject *gen;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004277 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004279 /* Don't need to keep the reference to f_back, it will be set
4280 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004281 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004283 /* Create a new generator that owns the ready to run frame
4284 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004285 if (is_coro) {
4286 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004287 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4288 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004289 } else {
4290 gen = PyGen_NewWithQualName(f, name, qualname);
4291 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004292 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004293 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004294 }
INADA Naoki9c157762016-12-26 18:52:46 +09004295
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004296 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004297
Yury Selivanov75445082015-05-11 22:57:16 -04004298 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004299 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004300
Victor Stinnerb9e68122019-11-14 12:20:46 +01004301 retval = _PyEval_EvalFrame(tstate, f, 0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004302
Thomas Woutersce272b62007-09-19 21:19:28 +00004303fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004305 /* decref'ing the frame can cause __del__ methods to get invoked,
4306 which can call back into Python. While we're done with the
4307 current Python frame (f), the associated C stack is still in use,
4308 so recursion_depth must be boosted for the duration.
4309 */
INADA Naoki5a625d02016-12-24 20:19:08 +09004310 if (Py_REFCNT(f) > 1) {
4311 Py_DECREF(f);
4312 _PyObject_GC_TRACK(f);
4313 }
4314 else {
4315 ++tstate->recursion_depth;
4316 Py_DECREF(f);
4317 --tstate->recursion_depth;
4318 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004319 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004320}
4321
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004322
4323PyObject *
4324_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
4325 PyObject *const *args, Py_ssize_t argcount,
4326 PyObject *const *kwnames, PyObject *const *kwargs,
4327 Py_ssize_t kwcount, int kwstep,
4328 PyObject *const *defs, Py_ssize_t defcount,
4329 PyObject *kwdefs, PyObject *closure,
4330 PyObject *name, PyObject *qualname)
4331{
4332 PyThreadState *tstate = _PyThreadState_GET();
4333 return _PyEval_EvalCode(tstate, _co, globals, locals,
4334 args, argcount,
4335 kwnames, kwargs,
4336 kwcount, kwstep,
4337 defs, defcount,
4338 kwdefs, closure,
4339 name, qualname);
4340}
4341
Victor Stinner40ee3012014-06-16 15:59:28 +02004342PyObject *
4343PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004344 PyObject *const *args, int argcount,
4345 PyObject *const *kws, int kwcount,
4346 PyObject *const *defs, int defcount,
4347 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004348{
4349 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004350 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004351 kws, kws != NULL ? kws + 1 : NULL,
4352 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004353 defs, defcount,
4354 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004355 NULL, NULL);
4356}
Tim Peters5ca576e2001-06-18 22:08:13 +00004357
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004358static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004359special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004360{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004361 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004362 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02004363 if (res == NULL && !_PyErr_Occurred(tstate)) {
4364 _PyErr_SetObject(tstate, PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004365 return NULL;
4366 }
4367 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004368}
4369
4370
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004371/* Logic for the raise statement (too complicated for inlining).
4372 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004373static int
Victor Stinner09532fe2019-05-10 23:39:09 +02004374do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004375{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004376 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004378 if (exc == NULL) {
4379 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01004380 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004381 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004382 type = exc_info->exc_type;
4383 value = exc_info->exc_value;
4384 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004385 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004386 _PyErr_SetString(tstate, PyExc_RuntimeError,
4387 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004388 return 0;
4389 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004390 Py_XINCREF(type);
4391 Py_XINCREF(value);
4392 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02004393 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004394 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004395 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004397 /* We support the following forms of raise:
4398 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004399 raise <instance>
4400 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004402 if (PyExceptionClass_Check(exc)) {
4403 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004404 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004405 if (value == NULL)
4406 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004407 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004408 _PyErr_Format(tstate, PyExc_TypeError,
4409 "calling %R should have returned an instance of "
4410 "BaseException, not %R",
4411 type, Py_TYPE(value));
4412 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004413 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004414 }
4415 else if (PyExceptionInstance_Check(exc)) {
4416 value = exc;
4417 type = PyExceptionInstance_Class(exc);
4418 Py_INCREF(type);
4419 }
4420 else {
4421 /* Not something you can raise. You get an exception
4422 anyway, just not what you specified :-) */
4423 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02004424 _PyErr_SetString(tstate, PyExc_TypeError,
4425 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004426 goto raise_error;
4427 }
Collin Winter828f04a2007-08-31 00:04:24 +00004428
Serhiy Storchakac0191582016-09-27 11:37:10 +03004429 assert(type != NULL);
4430 assert(value != NULL);
4431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004432 if (cause) {
4433 PyObject *fixed_cause;
4434 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004435 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004436 if (fixed_cause == NULL)
4437 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004438 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004439 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004440 else if (PyExceptionInstance_Check(cause)) {
4441 fixed_cause = cause;
4442 }
4443 else if (cause == Py_None) {
4444 Py_DECREF(cause);
4445 fixed_cause = NULL;
4446 }
4447 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004448 _PyErr_SetString(tstate, PyExc_TypeError,
4449 "exception causes must derive from "
4450 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004451 goto raise_error;
4452 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004453 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004454 }
Collin Winter828f04a2007-08-31 00:04:24 +00004455
Victor Stinner438a12d2019-05-24 17:01:38 +02004456 _PyErr_SetObject(tstate, type, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004457 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004458 Py_DECREF(value);
4459 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004460 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004461
4462raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004463 Py_XDECREF(value);
4464 Py_XDECREF(type);
4465 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004466 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004467}
4468
Tim Petersd6d010b2001-06-21 02:49:55 +00004469/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004470 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004471
Guido van Rossum0368b722007-05-11 16:50:42 +00004472 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4473 with a variable target.
4474*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004475
Barry Warsawe42b18f1997-08-25 22:13:04 +00004476static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004477unpack_iterable(PyThreadState *tstate, PyObject *v,
4478 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004479{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004480 int i = 0, j = 0;
4481 Py_ssize_t ll = 0;
4482 PyObject *it; /* iter(v) */
4483 PyObject *w;
4484 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004486 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004488 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004489 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004490 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004491 v->ob_type->tp_iter == NULL && !PySequence_Check(v))
4492 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004493 _PyErr_Format(tstate, PyExc_TypeError,
4494 "cannot unpack non-iterable %.200s object",
4495 v->ob_type->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004496 }
4497 return 0;
4498 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004500 for (; i < argcnt; i++) {
4501 w = PyIter_Next(it);
4502 if (w == NULL) {
4503 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004504 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04004505 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004506 _PyErr_Format(tstate, PyExc_ValueError,
4507 "not enough values to unpack "
4508 "(expected %d, got %d)",
4509 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004510 }
4511 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004512 _PyErr_Format(tstate, PyExc_ValueError,
4513 "not enough values to unpack "
4514 "(expected at least %d, got %d)",
4515 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004516 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004517 }
4518 goto Error;
4519 }
4520 *--sp = w;
4521 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004523 if (argcntafter == -1) {
4524 /* We better have exhausted the iterator now. */
4525 w = PyIter_Next(it);
4526 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004527 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004528 goto Error;
4529 Py_DECREF(it);
4530 return 1;
4531 }
4532 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02004533 _PyErr_Format(tstate, PyExc_ValueError,
4534 "too many values to unpack (expected %d)",
4535 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004536 goto Error;
4537 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004539 l = PySequence_List(it);
4540 if (l == NULL)
4541 goto Error;
4542 *--sp = l;
4543 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004545 ll = PyList_GET_SIZE(l);
4546 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004547 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04004548 "not enough values to unpack (expected at least %d, got %zd)",
4549 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004550 goto Error;
4551 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004553 /* Pop the "after-variable" args off the list. */
4554 for (j = argcntafter; j > 0; j--, i++) {
4555 *--sp = PyList_GET_ITEM(l, ll - j);
4556 }
4557 /* Resize the list. */
4558 Py_SIZE(l) = ll - argcntafter;
4559 Py_DECREF(it);
4560 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004561
Tim Petersd6d010b2001-06-21 02:49:55 +00004562Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004563 for (; i > 0; i--, sp++)
4564 Py_DECREF(*sp);
4565 Py_XDECREF(it);
4566 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004567}
4568
4569
Guido van Rossum96a42c81992-01-12 02:29:51 +00004570#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004571static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004572prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004573{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004574 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02004575 if (PyObject_Print(v, stdout, 0) != 0) {
4576 /* Don't know what else to do */
4577 _PyErr_Clear(tstate);
4578 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004579 printf("\n");
4580 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004581}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004582#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004583
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004584static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004585call_exc_trace(Py_tracefunc func, PyObject *self,
4586 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004587{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004588 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004589 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004590 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004591 if (value == NULL) {
4592 value = Py_None;
4593 Py_INCREF(value);
4594 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004595 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01004596 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004597 arg = PyTuple_Pack(3, type, value, traceback);
4598 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004599 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004600 return;
4601 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004602 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004603 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02004604 if (err == 0) {
4605 _PyErr_Restore(tstate, type, value, orig_traceback);
4606 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004607 else {
4608 Py_XDECREF(type);
4609 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004610 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004611 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004612}
4613
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004614static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004615call_trace_protected(Py_tracefunc func, PyObject *obj,
4616 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004617 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004618{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004619 PyObject *type, *value, *traceback;
4620 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004621 _PyErr_Fetch(tstate, &type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004622 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004623 if (err == 0)
4624 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004625 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004626 return 0;
4627 }
4628 else {
4629 Py_XDECREF(type);
4630 Py_XDECREF(value);
4631 Py_XDECREF(traceback);
4632 return -1;
4633 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004634}
4635
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004636static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004637call_trace(Py_tracefunc func, PyObject *obj,
4638 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004639 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004640{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004641 int result;
4642 if (tstate->tracing)
4643 return 0;
4644 tstate->tracing++;
4645 tstate->use_tracing = 0;
4646 result = func(obj, frame, what, arg);
4647 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4648 || (tstate->c_profilefunc != NULL));
4649 tstate->tracing--;
4650 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004651}
4652
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004653PyObject *
4654_PyEval_CallTracing(PyObject *func, PyObject *args)
4655{
Victor Stinner50b48572018-11-01 01:51:40 +01004656 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004657 int save_tracing = tstate->tracing;
4658 int save_use_tracing = tstate->use_tracing;
4659 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004661 tstate->tracing = 0;
4662 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4663 || (tstate->c_profilefunc != NULL));
4664 result = PyObject_Call(func, args, NULL);
4665 tstate->tracing = save_tracing;
4666 tstate->use_tracing = save_use_tracing;
4667 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004668}
4669
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004670/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004671static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004672maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004673 PyThreadState *tstate, PyFrameObject *frame,
4674 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004675{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004676 int result = 0;
4677 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004679 /* If the last instruction executed isn't in the current
4680 instruction window, reset the window.
4681 */
4682 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4683 PyAddrPair bounds;
4684 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4685 &bounds);
4686 *instr_lb = bounds.ap_lower;
4687 *instr_ub = bounds.ap_upper;
4688 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004689 /* If the last instruction falls at the start of a line or if it
4690 represents a jump backwards, update the frame's line number and
4691 then call the trace function if we're tracing source lines.
4692 */
4693 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004694 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004695 if (frame->f_trace_lines) {
4696 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4697 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004698 }
George King20faa682017-10-18 17:44:22 -07004699 /* Always emit an opcode event if we're tracing all opcodes. */
4700 if (frame->f_trace_opcodes) {
4701 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4702 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004703 *instr_prev = frame->f_lasti;
4704 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004705}
4706
Fred Drake5755ce62001-06-27 19:19:46 +00004707void
4708PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004709{
Steve Dowerb82e17e2019-05-23 08:45:22 -07004710 if (PySys_Audit("sys.setprofile", NULL) < 0) {
4711 return;
4712 }
4713
Victor Stinner50b48572018-11-01 01:51:40 +01004714 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004715 PyObject *temp = tstate->c_profileobj;
4716 Py_XINCREF(arg);
4717 tstate->c_profilefunc = NULL;
4718 tstate->c_profileobj = NULL;
4719 /* Must make sure that tracing is not ignored if 'temp' is freed */
4720 tstate->use_tracing = tstate->c_tracefunc != NULL;
4721 Py_XDECREF(temp);
4722 tstate->c_profilefunc = func;
4723 tstate->c_profileobj = arg;
4724 /* Flag that tracing or profiling is turned on */
4725 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004726}
4727
4728void
4729PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4730{
Steve Dowerb82e17e2019-05-23 08:45:22 -07004731 if (PySys_Audit("sys.settrace", NULL) < 0) {
4732 return;
4733 }
4734
Victor Stinner09532fe2019-05-10 23:39:09 +02004735 _PyRuntimeState *runtime = &_PyRuntime;
4736 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004737 PyObject *temp = tstate->c_traceobj;
Victor Stinner09532fe2019-05-10 23:39:09 +02004738 runtime->ceval.tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004739 Py_XINCREF(arg);
4740 tstate->c_tracefunc = NULL;
4741 tstate->c_traceobj = NULL;
4742 /* Must make sure that profiling is not ignored if 'temp' is freed */
4743 tstate->use_tracing = tstate->c_profilefunc != NULL;
4744 Py_XDECREF(temp);
4745 tstate->c_tracefunc = func;
4746 tstate->c_traceobj = arg;
4747 /* Flag that tracing or profiling is turned on */
4748 tstate->use_tracing = ((func != NULL)
4749 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004750}
4751
Yury Selivanov75445082015-05-11 22:57:16 -04004752void
Victor Stinner838f2642019-06-13 22:41:23 +02004753_PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004754{
4755 assert(new_depth >= 0);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004756 tstate->coroutine_origin_tracking_depth = new_depth;
4757}
4758
4759int
4760_PyEval_GetCoroutineOriginTrackingDepth(void)
4761{
Victor Stinner50b48572018-11-01 01:51:40 +01004762 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004763 return tstate->coroutine_origin_tracking_depth;
4764}
4765
4766void
Yury Selivanoveb636452016-09-08 22:01:51 -07004767_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4768{
Victor Stinner50b48572018-11-01 01:51:40 +01004769 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004770
4771 if (PySys_Audit("sys.set_asyncgen_hook_firstiter", NULL) < 0) {
4772 return;
4773 }
4774
Yury Selivanoveb636452016-09-08 22:01:51 -07004775 Py_XINCREF(firstiter);
4776 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4777}
4778
4779PyObject *
4780_PyEval_GetAsyncGenFirstiter(void)
4781{
Victor Stinner50b48572018-11-01 01:51:40 +01004782 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004783 return tstate->async_gen_firstiter;
4784}
4785
4786void
4787_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4788{
Victor Stinner50b48572018-11-01 01:51:40 +01004789 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004790
4791 if (PySys_Audit("sys.set_asyncgen_hook_finalizer", NULL) < 0) {
4792 return;
4793 }
4794
Yury Selivanoveb636452016-09-08 22:01:51 -07004795 Py_XINCREF(finalizer);
4796 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4797}
4798
4799PyObject *
4800_PyEval_GetAsyncGenFinalizer(void)
4801{
Victor Stinner50b48572018-11-01 01:51:40 +01004802 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004803 return tstate->async_gen_finalizer;
4804}
4805
Victor Stinner438a12d2019-05-24 17:01:38 +02004806static PyFrameObject *
4807_PyEval_GetFrame(PyThreadState *tstate)
4808{
4809 return _PyRuntime.gilstate.getframe(tstate);
4810}
4811
4812PyFrameObject *
4813PyEval_GetFrame(void)
4814{
4815 PyThreadState *tstate = _PyThreadState_GET();
4816 return _PyEval_GetFrame(tstate);
4817}
4818
Guido van Rossumb209a111997-04-29 18:18:01 +00004819PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004820PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004821{
Victor Stinner438a12d2019-05-24 17:01:38 +02004822 PyThreadState *tstate = _PyThreadState_GET();
4823 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004824 if (current_frame == NULL)
Victor Stinner438a12d2019-05-24 17:01:38 +02004825 return tstate->interp->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004826 else
4827 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004828}
4829
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004830/* Convenience function to get a builtin from its name */
4831PyObject *
4832_PyEval_GetBuiltinId(_Py_Identifier *name)
4833{
Victor Stinner438a12d2019-05-24 17:01:38 +02004834 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004835 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4836 if (attr) {
4837 Py_INCREF(attr);
4838 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004839 else if (!_PyErr_Occurred(tstate)) {
4840 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004841 }
4842 return attr;
4843}
4844
Guido van Rossumb209a111997-04-29 18:18:01 +00004845PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004846PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004847{
Victor Stinner438a12d2019-05-24 17:01:38 +02004848 PyThreadState *tstate = _PyThreadState_GET();
4849 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Victor Stinner41bb43a2013-10-29 01:19:37 +01004850 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004851 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004852 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004853 }
4854
Victor Stinner438a12d2019-05-24 17:01:38 +02004855 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01004856 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004857 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004858
4859 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004860 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004861}
4862
Guido van Rossumb209a111997-04-29 18:18:01 +00004863PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004864PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004865{
Victor Stinner438a12d2019-05-24 17:01:38 +02004866 PyThreadState *tstate = _PyThreadState_GET();
4867 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
4868 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004869 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004870 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004871
4872 assert(current_frame->f_globals != NULL);
4873 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004874}
4875
Guido van Rossum6135a871995-01-09 17:53:26 +00004876int
Tim Peters5ba58662001-07-16 02:29:45 +00004877PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004878{
Victor Stinner438a12d2019-05-24 17:01:38 +02004879 PyThreadState *tstate = _PyThreadState_GET();
4880 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004881 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004882
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004883 if (current_frame != NULL) {
4884 const int codeflags = current_frame->f_code->co_flags;
4885 const int compilerflags = codeflags & PyCF_MASK;
4886 if (compilerflags) {
4887 result = 1;
4888 cf->cf_flags |= compilerflags;
4889 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004890#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004891 if (codeflags & CO_GENERATOR_ALLOWED) {
4892 result = 1;
4893 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4894 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004895#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004896 }
4897 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004898}
4899
Guido van Rossum3f5da241990-12-20 15:06:42 +00004900
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004901const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004902PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004903{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004904 if (PyMethod_Check(func))
4905 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4906 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004907 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004908 else if (PyCFunction_Check(func))
4909 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4910 else
4911 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004912}
4913
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004914const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004915PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004916{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004917 if (PyMethod_Check(func))
4918 return "()";
4919 else if (PyFunction_Check(func))
4920 return "()";
4921 else if (PyCFunction_Check(func))
4922 return "()";
4923 else
4924 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004925}
4926
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004927#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004928if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004929 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4930 tstate, tstate->frame, \
4931 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004932 x = NULL; \
4933 } \
4934 else { \
4935 x = call; \
4936 if (tstate->c_profilefunc != NULL) { \
4937 if (x == NULL) { \
4938 call_trace_protected(tstate->c_profilefunc, \
4939 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004940 tstate, tstate->frame, \
4941 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004942 /* XXX should pass (type, value, tb) */ \
4943 } else { \
4944 if (call_trace(tstate->c_profilefunc, \
4945 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004946 tstate, tstate->frame, \
4947 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004948 Py_DECREF(x); \
4949 x = NULL; \
4950 } \
4951 } \
4952 } \
4953 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004954} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004955 x = call; \
4956 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004957
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004958
4959static PyObject *
4960trace_call_function(PyThreadState *tstate,
4961 PyObject *func,
4962 PyObject **args, Py_ssize_t nargs,
4963 PyObject *kwnames)
4964{
4965 PyObject *x;
4966 if (PyCFunction_Check(func)) {
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02004967 C_TRACE(x, _PyObject_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004968 return x;
4969 }
4970 else if (Py_TYPE(func) == &PyMethodDescr_Type && nargs > 0) {
4971 /* We need to create a temporary bound method as argument
4972 for profiling.
4973
4974 If nargs == 0, then this cannot work because we have no
4975 "self". In any case, the call itself would raise
4976 TypeError (foo needs an argument), so we just skip
4977 profiling. */
4978 PyObject *self = args[0];
4979 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4980 if (func == NULL) {
4981 return NULL;
4982 }
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02004983 C_TRACE(x, _PyObject_Vectorcall(func,
4984 args+1, nargs-1,
4985 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004986 Py_DECREF(func);
4987 return x;
4988 }
4989 return _PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
4990}
4991
Victor Stinner415c5102017-01-11 00:54:57 +01004992/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4993 to reduce the stack consumption. */
4994Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Victor Stinner09532fe2019-05-10 23:39:09 +02004995call_function(PyThreadState *tstate, PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004996{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004997 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004998 PyObject *func = *pfunc;
4999 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07005000 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
5001 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09005002 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005003
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005004 if (tstate->use_tracing) {
5005 x = trace_call_function(tstate, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09005006 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01005007 else {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005008 x = _PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005009 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00005010
Victor Stinner438a12d2019-05-24 17:01:38 +02005011 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005012
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01005013 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005014 while ((*pp_stack) > pfunc) {
5015 w = EXT_POP(*pp_stack);
5016 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005017 }
Victor Stinnerace47d72013-07-18 01:41:08 +02005018
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005019 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005020}
5021
Jeremy Hylton52820442001-01-03 23:52:36 +00005022static PyObject *
Victor Stinner09532fe2019-05-10 23:39:09 +02005023do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005024{
jdemeyere89de732018-09-19 12:06:20 +02005025 PyObject *result;
5026
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005027 if (PyCFunction_Check(func)) {
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +02005028 C_TRACE(result, PyObject_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005029 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005030 }
jdemeyere89de732018-09-19 12:06:20 +02005031 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
jdemeyere89de732018-09-19 12:06:20 +02005032 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
5033 if (nargs > 0 && tstate->use_tracing) {
5034 /* We need to create a temporary bound method as argument
5035 for profiling.
5036
5037 If nargs == 0, then this cannot work because we have no
5038 "self". In any case, the call itself would raise
5039 TypeError (foo needs an argument), so we just skip
5040 profiling. */
5041 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
5042 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5043 if (func == NULL) {
5044 return NULL;
5045 }
5046
Victor Stinner4d231bc2019-11-14 13:36:21 +01005047 C_TRACE(result, _PyObject_FastCallDictTstate(
5048 tstate, func,
5049 &_PyTuple_ITEMS(callargs)[1],
5050 nargs - 1,
5051 kwdict));
jdemeyere89de732018-09-19 12:06:20 +02005052 Py_DECREF(func);
5053 return result;
5054 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005055 }
jdemeyere89de732018-09-19 12:06:20 +02005056 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00005057}
5058
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005059/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005060 nb_index slot defined, and store in *pi.
5061 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08005062 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00005063 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005064*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005065int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005066_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005067{
Victor Stinner438a12d2019-05-24 17:01:38 +02005068 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005069 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005070 Py_ssize_t x;
5071 if (PyIndex_Check(v)) {
5072 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005073 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005074 return 0;
5075 }
5076 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005077 _PyErr_SetString(tstate, PyExc_TypeError,
5078 "slice indices must be integers or "
5079 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005080 return 0;
5081 }
5082 *pi = x;
5083 }
5084 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005085}
5086
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005087int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005088_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005089{
Victor Stinner438a12d2019-05-24 17:01:38 +02005090 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005091 Py_ssize_t x;
5092 if (PyIndex_Check(v)) {
5093 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005094 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005095 return 0;
5096 }
5097 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005098 _PyErr_SetString(tstate, PyExc_TypeError,
5099 "slice indices must be integers or "
5100 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005101 return 0;
5102 }
5103 *pi = x;
5104 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005105}
5106
5107
Guido van Rossum486364b2007-06-30 05:01:58 +00005108#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005109 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00005110
Guido van Rossumb209a111997-04-29 18:18:01 +00005111static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005112cmp_outcome(PyThreadState *tstate, int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005113{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005114 int res = 0;
5115 switch (op) {
5116 case PyCmp_IS:
5117 res = (v == w);
5118 break;
5119 case PyCmp_IS_NOT:
5120 res = (v != w);
5121 break;
5122 case PyCmp_IN:
5123 res = PySequence_Contains(w, v);
5124 if (res < 0)
5125 return NULL;
5126 break;
5127 case PyCmp_NOT_IN:
5128 res = PySequence_Contains(w, v);
5129 if (res < 0)
5130 return NULL;
5131 res = !res;
5132 break;
5133 case PyCmp_EXC_MATCH:
5134 if (PyTuple_Check(w)) {
5135 Py_ssize_t i, length;
5136 length = PyTuple_Size(w);
5137 for (i = 0; i < length; i += 1) {
5138 PyObject *exc = PyTuple_GET_ITEM(w, i);
5139 if (!PyExceptionClass_Check(exc)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005140 _PyErr_SetString(tstate, PyExc_TypeError,
5141 CANNOT_CATCH_MSG);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005142 return NULL;
5143 }
5144 }
5145 }
5146 else {
5147 if (!PyExceptionClass_Check(w)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005148 _PyErr_SetString(tstate, PyExc_TypeError,
5149 CANNOT_CATCH_MSG);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005150 return NULL;
5151 }
5152 }
5153 res = PyErr_GivenExceptionMatches(v, w);
5154 break;
5155 default:
5156 return PyObject_RichCompare(v, w, op);
5157 }
5158 v = res ? Py_True : Py_False;
5159 Py_INCREF(v);
5160 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005161}
5162
Thomas Wouters52152252000-08-17 22:55:00 +00005163static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005164import_name(PyThreadState *tstate, PyFrameObject *f,
5165 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005166{
5167 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005168 PyObject *import_func, *res;
5169 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005170
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005171 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005172 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005173 if (!_PyErr_Occurred(tstate)) {
5174 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005175 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005176 return NULL;
5177 }
5178
5179 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005180 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005181 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02005182 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005183 return NULL;
5184 }
5185 res = PyImport_ImportModuleLevelObject(
5186 name,
5187 f->f_globals,
5188 f->f_locals == NULL ? Py_None : f->f_locals,
5189 fromlist,
5190 ilevel);
5191 return res;
5192 }
5193
5194 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005195
5196 stack[0] = name;
5197 stack[1] = f->f_globals;
5198 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5199 stack[3] = fromlist;
5200 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005201 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005202 Py_DECREF(import_func);
5203 return res;
5204}
5205
5206static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005207import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005208{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005209 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02005210 _Py_IDENTIFIER(__name__);
Xiang Zhang4830f582017-03-21 11:13:42 +08005211 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005212
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005213 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005214 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005215 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005216 /* Issue #17636: in case this failed because of a circular relative
5217 import, try to fallback on reading the module directly from
5218 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02005219 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005220 if (pkgname == NULL) {
5221 goto error;
5222 }
Oren Milman6db70332017-09-19 14:23:01 +03005223 if (!PyUnicode_Check(pkgname)) {
5224 Py_CLEAR(pkgname);
5225 goto error;
5226 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005227 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005228 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005229 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005230 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005231 }
Eric Snow3f9eee62017-09-15 16:35:20 -06005232 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005233 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02005234 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07005235 goto error;
5236 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005237 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005238 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005239 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005240 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005241 if (pkgname == NULL) {
5242 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5243 if (pkgname_or_unknown == NULL) {
5244 Py_XDECREF(pkgpath);
5245 return NULL;
5246 }
5247 } else {
5248 pkgname_or_unknown = pkgname;
5249 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005250
5251 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005252 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08005253 errmsg = PyUnicode_FromFormat(
5254 "cannot import name %R from %R (unknown location)",
5255 name, pkgname_or_unknown
5256 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005257 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005258 PyErr_SetImportError(errmsg, pkgname, NULL);
5259 }
5260 else {
Anthony Sottile65366bc2019-09-09 08:17:50 -07005261 _Py_IDENTIFIER(__spec__);
5262 PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
Anthony Sottile65366bc2019-09-09 08:17:50 -07005263 const char *fmt =
5264 _PyModuleSpec_IsInitializing(spec) ?
5265 "cannot import name %R from partially initialized module %R "
5266 "(most likely due to a circular import) (%S)" :
5267 "cannot import name %R from %R (%S)";
5268 Py_XDECREF(spec);
5269
5270 errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
Stefan Krah027b09c2019-03-25 21:50:58 +01005271 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005272 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005273 }
5274
Xiang Zhang4830f582017-03-21 11:13:42 +08005275 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005276 Py_XDECREF(pkgname_or_unknown);
5277 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005278 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005279}
Guido van Rossumac7be682001-01-17 15:42:30 +00005280
Thomas Wouters52152252000-08-17 22:55:00 +00005281static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005282import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00005283{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005284 _Py_IDENTIFIER(__all__);
5285 _Py_IDENTIFIER(__dict__);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005286 _Py_IDENTIFIER(__name__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005287 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005288 int skip_leading_underscores = 0;
5289 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005290
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005291 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5292 return -1; /* Unexpected error */
5293 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005294 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005295 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5296 return -1;
5297 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005298 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005299 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005300 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005301 return -1;
5302 }
5303 all = PyMapping_Keys(dict);
5304 Py_DECREF(dict);
5305 if (all == NULL)
5306 return -1;
5307 skip_leading_underscores = 1;
5308 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005310 for (pos = 0, err = 0; ; pos++) {
5311 name = PySequence_GetItem(all, pos);
5312 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005313 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005314 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02005315 }
5316 else {
5317 _PyErr_Clear(tstate);
5318 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005319 break;
5320 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005321 if (!PyUnicode_Check(name)) {
5322 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5323 if (modname == NULL) {
5324 Py_DECREF(name);
5325 err = -1;
5326 break;
5327 }
5328 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005329 _PyErr_Format(tstate, PyExc_TypeError,
5330 "module __name__ must be a string, not %.100s",
5331 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005332 }
5333 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005334 _PyErr_Format(tstate, PyExc_TypeError,
5335 "%s in %U.%s must be str, not %.100s",
5336 skip_leading_underscores ? "Key" : "Item",
5337 modname,
5338 skip_leading_underscores ? "__dict__" : "__all__",
5339 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005340 }
5341 Py_DECREF(modname);
5342 Py_DECREF(name);
5343 err = -1;
5344 break;
5345 }
5346 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005347 if (PyUnicode_READY(name) == -1) {
5348 Py_DECREF(name);
5349 err = -1;
5350 break;
5351 }
5352 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5353 Py_DECREF(name);
5354 continue;
5355 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005356 }
5357 value = PyObject_GetAttr(v, name);
5358 if (value == NULL)
5359 err = -1;
5360 else if (PyDict_CheckExact(locals))
5361 err = PyDict_SetItem(locals, name, value);
5362 else
5363 err = PyObject_SetItem(locals, name, value);
5364 Py_DECREF(name);
5365 Py_XDECREF(value);
5366 if (err != 0)
5367 break;
5368 }
5369 Py_DECREF(all);
5370 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005371}
5372
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005373static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005374check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005375{
5376 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005377 /* check_args_iterable() may be called with a live exception:
5378 * clear it to prevent calling _PyObject_FunctionStr() with an
5379 * exception set. */
5380 PyErr_Clear();
5381 PyObject *funcstr = _PyObject_FunctionStr(func);
5382 if (funcstr != NULL) {
5383 _PyErr_Format(tstate, PyExc_TypeError,
5384 "%U argument after * must be an iterable, not %.200s",
5385 funcstr, Py_TYPE(args)->tp_name);
5386 Py_DECREF(funcstr);
5387 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005388 return -1;
5389 }
5390 return 0;
5391}
5392
5393static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005394format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005395{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005396 /* _PyDict_MergeEx raises attribute
5397 * error (percolated from an attempt
5398 * to get 'keys' attribute) instead of
5399 * a type error if its second argument
5400 * is not a mapping.
5401 */
Victor Stinner438a12d2019-05-24 17:01:38 +02005402 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005403 PyErr_Clear();
5404 PyObject *funcstr = _PyObject_FunctionStr(func);
5405 if (funcstr != NULL) {
5406 _PyErr_Format(
5407 tstate, PyExc_TypeError,
5408 "%U argument after ** must be a mapping, not %.200s",
5409 funcstr, Py_TYPE(kwargs)->tp_name);
5410 Py_DECREF(funcstr);
5411 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005412 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005413 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005414 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02005415 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005416 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005417 PyErr_Clear();
5418 PyObject *funcstr = _PyObject_FunctionStr(func);
5419 if (funcstr != NULL) {
5420 PyObject *key = PyTuple_GET_ITEM(val, 0);
5421 _PyErr_Format(
5422 tstate, PyExc_TypeError,
5423 "%U got multiple values for keyword argument '%S'",
5424 funcstr, key);
5425 Py_DECREF(funcstr);
5426 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005427 Py_XDECREF(exc);
5428 Py_XDECREF(val);
5429 Py_XDECREF(tb);
5430 }
5431 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005432 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005433 }
5434 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005435}
5436
Guido van Rossumac7be682001-01-17 15:42:30 +00005437static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005438format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
5439 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005440{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005441 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005442
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005443 if (!obj)
5444 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005445
Serhiy Storchaka06515832016-11-20 09:13:07 +02005446 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005447 if (!obj_str)
5448 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005449
Victor Stinner438a12d2019-05-24 17:01:38 +02005450 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005451}
Guido van Rossum950361c1997-01-24 13:49:28 +00005452
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005453static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005454format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005455{
5456 PyObject *name;
5457 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02005458 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005459 return;
5460 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5461 name = PyTuple_GET_ITEM(co->co_cellvars,
5462 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005463 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005464 PyExc_UnboundLocalError,
5465 UNBOUNDLOCAL_ERROR_MSG,
5466 name);
5467 } else {
5468 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5469 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02005470 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005471 UNBOUNDFREE_ERROR_MSG, name);
5472 }
5473}
5474
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005475static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005476format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005477{
5478 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5479 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005480 _PyErr_Format(tstate, PyExc_TypeError,
5481 "'async with' received an object from __aenter__ "
5482 "that does not implement __await__: %.100s",
5483 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005484 }
5485 else if (prevopcode == WITH_CLEANUP_START) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005486 _PyErr_Format(tstate, PyExc_TypeError,
5487 "'async with' received an object from __aexit__ "
5488 "that does not implement __await__: %.100s",
5489 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005490 }
5491 }
5492}
5493
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005494static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005495unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005496 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005497{
5498 PyObject *res;
5499 if (Py_REFCNT(v) == 2) {
5500 /* In the common case, there are 2 references to the value
5501 * stored in 'variable' when the += is performed: one on the
5502 * value stack (in 'v') and one still stored in the
5503 * 'variable'. We try to delete the variable now to reduce
5504 * the refcnt to 1.
5505 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005506 int opcode, oparg;
5507 NEXTOPARG();
5508 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005509 case STORE_FAST:
5510 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005511 PyObject **fastlocals = f->f_localsplus;
5512 if (GETLOCAL(oparg) == v)
5513 SETLOCAL(oparg, NULL);
5514 break;
5515 }
5516 case STORE_DEREF:
5517 {
5518 PyObject **freevars = (f->f_localsplus +
5519 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005520 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005521 if (PyCell_GET(c) == v) {
5522 PyCell_SET(c, NULL);
5523 Py_DECREF(v);
5524 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005525 break;
5526 }
5527 case STORE_NAME:
5528 {
5529 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005530 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005531 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005532 if (locals && PyDict_CheckExact(locals)) {
5533 PyObject *w = PyDict_GetItemWithError(locals, name);
5534 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02005535 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005536 {
5537 Py_DECREF(v);
5538 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005539 }
5540 }
5541 break;
5542 }
5543 }
5544 }
5545 res = v;
5546 PyUnicode_Append(&res, w);
5547 return res;
5548}
5549
Guido van Rossum950361c1997-01-24 13:49:28 +00005550#ifdef DYNAMIC_EXECUTION_PROFILE
5551
Skip Montanarof118cb12001-10-15 20:51:38 +00005552static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005553getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005554{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005555 int i;
5556 PyObject *l = PyList_New(256);
5557 if (l == NULL) return NULL;
5558 for (i = 0; i < 256; i++) {
5559 PyObject *x = PyLong_FromLong(a[i]);
5560 if (x == NULL) {
5561 Py_DECREF(l);
5562 return NULL;
5563 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005564 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005565 }
5566 for (i = 0; i < 256; i++)
5567 a[i] = 0;
5568 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005569}
5570
5571PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005572_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005573{
5574#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005575 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005576#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005577 int i;
5578 PyObject *l = PyList_New(257);
5579 if (l == NULL) return NULL;
5580 for (i = 0; i < 257; i++) {
5581 PyObject *x = getarray(dxpairs[i]);
5582 if (x == NULL) {
5583 Py_DECREF(l);
5584 return NULL;
5585 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005586 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005587 }
5588 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005589#endif
5590}
5591
5592#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005593
5594Py_ssize_t
5595_PyEval_RequestCodeExtraIndex(freefunc free)
5596{
Victor Stinnercaba55b2018-08-03 15:33:52 +02005597 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -07005598 Py_ssize_t new_index;
5599
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005600 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005601 return -1;
5602 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005603 new_index = interp->co_extra_user_count++;
5604 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005605 return new_index;
5606}
Łukasz Langaa785c872016-09-09 17:37:37 -07005607
5608static void
5609dtrace_function_entry(PyFrameObject *f)
5610{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005611 const char *filename;
5612 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005613 int lineno;
5614
5615 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5616 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5617 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5618
5619 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5620}
5621
5622static void
5623dtrace_function_return(PyFrameObject *f)
5624{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005625 const char *filename;
5626 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005627 int lineno;
5628
5629 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5630 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5631 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5632
5633 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5634}
5635
5636/* DTrace equivalent of maybe_call_line_trace. */
5637static void
5638maybe_dtrace_line(PyFrameObject *frame,
5639 int *instr_lb, int *instr_ub, int *instr_prev)
5640{
5641 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005642 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005643
5644 /* If the last instruction executed isn't in the current
5645 instruction window, reset the window.
5646 */
5647 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5648 PyAddrPair bounds;
5649 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5650 &bounds);
5651 *instr_lb = bounds.ap_lower;
5652 *instr_ub = bounds.ap_upper;
5653 }
5654 /* If the last instruction falls at the start of a line or if
5655 it represents a jump backwards, update the frame's line
5656 number and call the trace function. */
5657 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5658 frame->f_lineno = line;
5659 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5660 if (!co_filename)
5661 co_filename = "?";
5662 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5663 if (!co_name)
5664 co_name = "?";
5665 PyDTrace_LINE(co_filename, co_name, line);
5666 }
5667 *instr_prev = frame->f_lasti;
5668}
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005669
5670
5671/* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions
5672 for the limited API. */
5673
5674#undef Py_EnterRecursiveCall
5675
5676int Py_EnterRecursiveCall(const char *where)
5677{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005678 return _Py_EnterRecursiveCall_inline(where);
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005679}
5680
5681#undef Py_LeaveRecursiveCall
5682
5683void Py_LeaveRecursiveCall(void)
5684{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005685 _Py_LeaveRecursiveCall_inline();
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005686}