blob: 964ce1333694e23533aff1b79e77e35d5c77a5b2 [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 Stinner09532fe2019-05-10 23:39:09 +020013#include "pycore_ceval.h"
Inada Naoki91234a12019-06-03 21:30:58 +090014#include "pycore_code.h"
Victor Stinnerbcda8f12018-11-21 22:27:47 +010015#include "pycore_object.h"
Victor Stinner438a12d2019-05-24 17:01:38 +020016#include "pycore_pyerrors.h"
17#include "pycore_pylifecycle.h"
Victor Stinner621cebe2018-11-12 16:53:38 +010018#include "pycore_pystate.h"
Victor Stinnerec13b932018-11-25 23:56:17 +010019#include "pycore_tupleobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000020
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000021#include "code.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040022#include "dictobject.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000023#include "frameobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000024#include "opcode.h"
Łukasz Langaa785c872016-09-09 17:37:37 -070025#include "pydtrace.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040026#include "setobject.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000027#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000028
Guido van Rossumc6004111993-11-05 10:22:19 +000029#include <ctype.h>
30
Guido van Rossum408027e1996-12-30 16:17:54 +000031#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000032/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000033#define LLTRACE 1 /* Low-level trace feature */
34#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000035#endif
36
Victor Stinner5c75f372019-04-17 23:02:26 +020037#if !defined(Py_BUILD_CORE)
38# error "ceval.c must be build with Py_BUILD_CORE define for best performance"
39#endif
40
Guido van Rossum5b722181993-03-30 17:46:03 +000041
Guido van Rossum374a9221991-04-04 10:40:29 +000042/* Forward declarations */
Victor Stinner09532fe2019-05-10 23:39:09 +020043Py_LOCAL_INLINE(PyObject *) call_function(
44 PyThreadState *tstate, PyObject ***pp_stack,
45 Py_ssize_t oparg, PyObject *kwnames);
46static PyObject * do_call_core(
47 PyThreadState *tstate, PyObject *func,
48 PyObject *callargs, PyObject *kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +000049
Guido van Rossum0a066c01992-03-27 17:29:15 +000050#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000051static int lltrace;
Victor Stinner438a12d2019-05-24 17:01:38 +020052static int prtrace(PyThreadState *, PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000053#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010054static int call_trace(Py_tracefunc, PyObject *,
55 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000056 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000057static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010058 PyThreadState *, PyFrameObject *,
59 int, PyObject *);
60static void call_exc_trace(Py_tracefunc, PyObject *,
61 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000062static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Eric Snow2ebc5ce2017-09-07 23:51:28 -060063 PyThreadState *, PyFrameObject *,
64 int *, int *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070065static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *);
66static void dtrace_function_entry(PyFrameObject *);
67static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000068
Victor Stinner438a12d2019-05-24 17:01:38 +020069static PyObject * cmp_outcome(PyThreadState *, int, PyObject *, PyObject *);
70static PyObject * import_name(PyThreadState *, PyFrameObject *,
71 PyObject *, PyObject *, PyObject *);
72static PyObject * import_from(PyThreadState *, PyObject *, PyObject *);
73static int import_all_from(PyThreadState *, PyObject *, PyObject *);
74static void format_exc_check_arg(PyThreadState *, PyObject *, const char *, PyObject *);
75static void format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg);
76static PyObject * unicode_concatenate(PyThreadState *, PyObject *, PyObject *,
Serhiy Storchakaab874002016-09-11 13:48:15 +030077 PyFrameObject *, const _Py_CODEUNIT *);
Victor Stinner438a12d2019-05-24 17:01:38 +020078static PyObject * special_lookup(PyThreadState *, PyObject *, _Py_Identifier *);
79static int check_args_iterable(PyThreadState *, PyObject *func, PyObject *vararg);
80static void format_kwargs_error(PyThreadState *, PyObject *func, PyObject *kwargs);
81static void format_awaitable_error(PyThreadState *, PyTypeObject *, int);
Guido van Rossum374a9221991-04-04 10:40:29 +000082
Paul Prescode68140d2000-08-30 20:25:01 +000083#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000085#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000087#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 "free variable '%.200s' referenced before assignment" \
89 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000090
Guido van Rossum950361c1997-01-24 13:49:28 +000091/* Dynamic execution profile */
92#ifdef DYNAMIC_EXECUTION_PROFILE
93#ifdef DXPAIRS
94static long dxpairs[257][256];
95#define dxp dxpairs[256]
96#else
97static long dxp[256];
98#endif
99#endif
100
Inada Naoki91234a12019-06-03 21:30:58 +0900101/* per opcode cache */
Inada Naokieddef862019-06-04 07:38:10 +0900102#ifdef Py_DEBUG
103// --with-pydebug is used to find memory leak. opcache makes it harder.
104// So we disable opcache when Py_DEBUG is defined.
105// See bpo-37146
106#define OPCACHE_MIN_RUNS 0 /* disable opcache */
107#else
Inada Naoki91234a12019-06-03 21:30:58 +0900108#define OPCACHE_MIN_RUNS 1024 /* create opcache when code executed this time */
Inada Naokieddef862019-06-04 07:38:10 +0900109#endif
Inada Naoki91234a12019-06-03 21:30:58 +0900110#define OPCACHE_STATS 0 /* Enable stats */
111
112#if OPCACHE_STATS
113static size_t opcache_code_objects = 0;
114static size_t opcache_code_objects_extra_mem = 0;
115
116static size_t opcache_global_opts = 0;
117static size_t opcache_global_hits = 0;
118static size_t opcache_global_misses = 0;
119#endif
120
Victor Stinnere225beb2019-06-03 18:14:24 +0200121#define GIL_REQUEST _Py_atomic_load_relaxed(&ceval->gil_drop_request)
Inada Naoki91234a12019-06-03 21:30:58 +0900122
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000123/* This can set eval_breaker to 0 even though gil_drop_request became
124 1. We believe this is all right because the eval loop will release
125 the GIL eventually anyway. */
Victor Stinnere225beb2019-06-03 18:14:24 +0200126#define COMPUTE_EVAL_BREAKER(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 _Py_atomic_store_relaxed( \
Victor Stinnere225beb2019-06-03 18:14:24 +0200128 &(ceval)->eval_breaker, \
129 GIL_REQUEST | \
130 _Py_atomic_load_relaxed(&(ceval)->signals_pending) | \
131 _Py_atomic_load_relaxed(&(ceval)->pending.calls_to_do) | \
132 (ceval)->pending.async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000133
Victor Stinnere225beb2019-06-03 18:14:24 +0200134#define SET_GIL_DROP_REQUEST(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200136 _Py_atomic_store_relaxed(&(ceval)->gil_drop_request, 1); \
137 _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000138 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000139
Victor Stinnere225beb2019-06-03 18:14:24 +0200140#define RESET_GIL_DROP_REQUEST(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000141 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200142 _Py_atomic_store_relaxed(&(ceval)->gil_drop_request, 0); \
143 COMPUTE_EVAL_BREAKER(ceval); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000145
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000146/* Pending calls are only modified under pending_lock */
Victor Stinnere225beb2019-06-03 18:14:24 +0200147#define SIGNAL_PENDING_CALLS(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200149 _Py_atomic_store_relaxed(&(ceval)->pending.calls_to_do, 1); \
150 _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000152
Victor Stinnere225beb2019-06-03 18:14:24 +0200153#define UNSIGNAL_PENDING_CALLS(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000154 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200155 _Py_atomic_store_relaxed(&(ceval)->pending.calls_to_do, 0); \
156 COMPUTE_EVAL_BREAKER(ceval); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000157 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000158
Victor Stinnere225beb2019-06-03 18:14:24 +0200159#define SIGNAL_PENDING_SIGNALS(ceval) \
Eric Snowfdf282d2019-01-11 14:26:55 -0700160 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200161 _Py_atomic_store_relaxed(&(ceval)->signals_pending, 1); \
162 _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \
Eric Snowfdf282d2019-01-11 14:26:55 -0700163 } while (0)
164
Victor Stinnere225beb2019-06-03 18:14:24 +0200165#define UNSIGNAL_PENDING_SIGNALS(ceval) \
Eric Snowfdf282d2019-01-11 14:26:55 -0700166 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200167 _Py_atomic_store_relaxed(&(ceval)->signals_pending, 0); \
168 COMPUTE_EVAL_BREAKER(ceval); \
Eric Snowfdf282d2019-01-11 14:26:55 -0700169 } while (0)
170
Victor Stinnere225beb2019-06-03 18:14:24 +0200171#define SIGNAL_ASYNC_EXC(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200173 (ceval)->pending.async_exc = 1; \
174 _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000176
Victor Stinnere225beb2019-06-03 18:14:24 +0200177#define UNSIGNAL_ASYNC_EXC(ceval) \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600178 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200179 (ceval)->pending.async_exc = 0; \
180 COMPUTE_EVAL_BREAKER(ceval); \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600181 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000182
183
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000184#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000185#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000186#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000187#include "pythread.h"
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000188#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000189
Tim Peters7f468f22004-10-11 02:40:51 +0000190int
191PyEval_ThreadsInitialized(void)
192{
Victor Stinner09532fe2019-05-10 23:39:09 +0200193 return gil_created(&_PyRuntime.ceval.gil);
Tim Peters7f468f22004-10-11 02:40:51 +0000194}
195
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000196void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000197PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000198{
Victor Stinner09532fe2019-05-10 23:39:09 +0200199 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200200 struct _ceval_runtime_state *ceval = &runtime->ceval;
201 struct _gil_runtime_state *gil = &ceval->gil;
Victor Stinner09532fe2019-05-10 23:39:09 +0200202 if (gil_created(gil)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 return;
Victor Stinnera7126792019-03-19 14:19:38 +0100204 }
205
Inada Naoki001fee12019-02-20 10:00:09 +0900206 PyThread_init_thread();
Victor Stinner09532fe2019-05-10 23:39:09 +0200207 create_gil(gil);
208 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinnere225beb2019-06-03 18:14:24 +0200209 take_gil(ceval, tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700210
Victor Stinnere225beb2019-06-03 18:14:24 +0200211 struct _pending_calls *pending = &ceval->pending;
212 pending->lock = PyThread_allocate_lock();
213 if (pending->lock == NULL) {
214 Py_FatalError("Can't initialize threads for pending calls");
215 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000216}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000217
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000218void
Victor Stinnere225beb2019-06-03 18:14:24 +0200219_PyEval_FiniThreads(struct _ceval_runtime_state *ceval)
Antoine Pitrou1df15362010-09-13 14:16:46 +0000220{
Victor Stinnere225beb2019-06-03 18:14:24 +0200221 struct _gil_runtime_state *gil = &ceval->gil;
Victor Stinner09532fe2019-05-10 23:39:09 +0200222 if (!gil_created(gil)) {
Antoine Pitrou1df15362010-09-13 14:16:46 +0000223 return;
Victor Stinnera7126792019-03-19 14:19:38 +0100224 }
225
Victor Stinner09532fe2019-05-10 23:39:09 +0200226 destroy_gil(gil);
227 assert(!gil_created(gil));
Victor Stinner99fcc612019-04-29 13:04:07 +0200228
Victor Stinnere225beb2019-06-03 18:14:24 +0200229 struct _pending_calls *pending = &ceval->pending;
230 if (pending->lock != NULL) {
231 PyThread_free_lock(pending->lock);
232 pending->lock = NULL;
233 }
Antoine Pitrou1df15362010-09-13 14:16:46 +0000234}
235
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400236static inline void
Victor Stinner0fd2c302019-06-04 03:15:09 +0200237exit_thread_if_finalizing(_PyRuntimeState *runtime, PyThreadState *tstate)
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400238{
Victor Stinnere225beb2019-06-03 18:14:24 +0200239 /* _Py_Finalizing is protected by the GIL */
Victor Stinner09532fe2019-05-10 23:39:09 +0200240 if (runtime->finalizing != NULL && !_Py_CURRENTLY_FINALIZING(runtime, tstate)) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200241 drop_gil(&runtime->ceval, tstate);
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400242 PyThread_exit_thread();
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400243 }
244}
245
Antoine Pitrou1df15362010-09-13 14:16:46 +0000246void
Inada Naoki91234a12019-06-03 21:30:58 +0900247_PyEval_Fini(void)
248{
249#if OPCACHE_STATS
250 fprintf(stderr, "-- Opcode cache number of objects = %zd\n",
251 opcache_code_objects);
252
253 fprintf(stderr, "-- Opcode cache total extra mem = %zd\n",
254 opcache_code_objects_extra_mem);
255
256 fprintf(stderr, "\n");
257
258 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL hits = %zd (%d%%)\n",
259 opcache_global_hits,
260 (int) (100.0 * opcache_global_hits /
261 (opcache_global_hits + opcache_global_misses)));
262
263 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL misses = %zd (%d%%)\n",
264 opcache_global_misses,
265 (int) (100.0 * opcache_global_misses /
266 (opcache_global_hits + opcache_global_misses)));
267
268 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL opts = %zd\n",
269 opcache_global_opts);
270
271 fprintf(stderr, "\n");
272#endif
273}
274
275void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000276PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000277{
Victor Stinner09532fe2019-05-10 23:39:09 +0200278 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200279 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinner09532fe2019-05-10 23:39:09 +0200280 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
281 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
Victor Stinner09532fe2019-05-10 23:39:09 +0200283 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200284 take_gil(ceval, tstate);
Victor Stinner0fd2c302019-06-04 03:15:09 +0200285 exit_thread_if_finalizing(runtime, tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000286}
287
288void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000289PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000290{
Victor Stinner09532fe2019-05-10 23:39:09 +0200291 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200292 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100294 We therefore avoid PyThreadState_Get() which dumps a fatal error
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 in debug mode.
296 */
Victor Stinnere225beb2019-06-03 18:14:24 +0200297 drop_gil(&runtime->ceval, tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000298}
299
300void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000301PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000302{
Victor Stinner09532fe2019-05-10 23:39:09 +0200303 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200305 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200306
Victor Stinner0fd2c302019-06-04 03:15:09 +0200307 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200308 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinner09532fe2019-05-10 23:39:09 +0200309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 /* Check someone has called PyEval_InitThreads() to create the lock */
Victor Stinnere225beb2019-06-03 18:14:24 +0200311 assert(gil_created(&ceval->gil));
312 take_gil(ceval, tstate);
Victor Stinner0fd2c302019-06-04 03:15:09 +0200313 exit_thread_if_finalizing(runtime, tstate);
Victor Stinner09532fe2019-05-10 23:39:09 +0200314 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
315 Py_FatalError("PyEval_AcquireThread: non-NULL old thread state");
316 }
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000317}
318
319void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000320PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000321{
Victor Stinner09532fe2019-05-10 23:39:09 +0200322 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200324 }
325
Victor Stinner0fd2c302019-06-04 03:15:09 +0200326 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200327 PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
328 if (new_tstate != tstate) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200330 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200331 drop_gil(&runtime->ceval, tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000332}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000333
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200334/* This function is called from PyOS_AfterFork_Child to destroy all threads
335 * which are not running in the child process, and clear internal locks
336 * which might be held by those threads.
337 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000338
339void
Victor Stinnerd5d9e812019-05-13 12:35:37 +0200340_PyEval_ReInitThreads(_PyRuntimeState *runtime)
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000341{
Victor Stinnere225beb2019-06-03 18:14:24 +0200342 struct _ceval_runtime_state *ceval = &runtime->ceval;
343 if (!gil_created(&ceval->gil)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 return;
Victor Stinner09532fe2019-05-10 23:39:09 +0200345 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200346 recreate_gil(&ceval->gil);
Victor Stinner09532fe2019-05-10 23:39:09 +0200347 PyThreadState *current_tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinnere225beb2019-06-03 18:14:24 +0200348 take_gil(ceval, current_tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700349
Victor Stinnere225beb2019-06-03 18:14:24 +0200350 struct _pending_calls *pending = &ceval->pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200351 pending->lock = PyThread_allocate_lock();
352 if (pending->lock == NULL) {
Eric Snow8479a342019-03-08 23:44:33 -0700353 Py_FatalError("Can't initialize threads for pending calls");
354 }
Jesse Nollera8513972008-07-17 16:49:17 +0000355
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200356 /* Destroy all threads except the current one */
Victor Stinner0fd2c302019-06-04 03:15:09 +0200357 _PyThreadState_DeleteExcept(runtime, current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000358}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000359
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000360/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600361 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000362
363void
Victor Stinnere225beb2019-06-03 18:14:24 +0200364_PyEval_SignalAsyncExc(struct _ceval_runtime_state *ceval)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000365{
Victor Stinnere225beb2019-06-03 18:14:24 +0200366 SIGNAL_ASYNC_EXC(ceval);
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000367}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000368
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000369PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000370PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000371{
Victor Stinner09532fe2019-05-10 23:39:09 +0200372 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200373 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinner09532fe2019-05-10 23:39:09 +0200374 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
375 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 Py_FatalError("PyEval_SaveThread: NULL tstate");
Victor Stinner09532fe2019-05-10 23:39:09 +0200377 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200378 assert(gil_created(&ceval->gil));
379 drop_gil(ceval, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000381}
382
383void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000384PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000385{
Victor Stinner0fd2c302019-06-04 03:15:09 +0200386 _PyRuntimeState *runtime = &_PyRuntime;
387 struct _ceval_runtime_state *ceval = &runtime->ceval;
388
Victor Stinner09532fe2019-05-10 23:39:09 +0200389 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Victor Stinner09532fe2019-05-10 23:39:09 +0200391 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200392 assert(gil_created(&ceval->gil));
Victor Stinner2914bb32018-01-29 11:57:45 +0100393
394 int err = errno;
Victor Stinnere225beb2019-06-03 18:14:24 +0200395 take_gil(ceval, tstate);
Victor Stinner0fd2c302019-06-04 03:15:09 +0200396 exit_thread_if_finalizing(runtime, tstate);
Victor Stinner2914bb32018-01-29 11:57:45 +0100397 errno = err;
398
Victor Stinner09532fe2019-05-10 23:39:09 +0200399 _PyThreadState_Swap(&runtime->gilstate, tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000400}
401
402
Guido van Rossuma9672091994-09-14 13:31:22 +0000403/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
404 signal handlers or Mac I/O completion routines) can schedule calls
405 to a function to be called synchronously.
406 The synchronous function is called with one void* argument.
407 It should return 0 for success or -1 for failure -- failure should
408 be accompanied by an exception.
409
410 If registry succeeds, the registry function returns 0; if it fails
411 (e.g. due to too many pending calls) it returns -1 (without setting
412 an exception condition).
413
414 Note that because registry may occur from within signal handlers,
415 or other asynchronous events, calling malloc() is unsafe!
416
Guido van Rossuma9672091994-09-14 13:31:22 +0000417 Any thread can schedule pending calls, but only the main thread
418 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000419 There is no facility to schedule calls to a particular thread, but
420 that should be easy to change, should that ever be required. In
421 that case, the static variables here should go into the python
422 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000423*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000424
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200425void
Victor Stinnere225beb2019-06-03 18:14:24 +0200426_PyEval_SignalReceived(struct _ceval_runtime_state *ceval)
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200427{
428 /* bpo-30703: Function called when the C signal handler of Python gets a
429 signal. We cannot queue a callback using Py_AddPendingCall() since
430 that function is not async-signal-safe. */
Victor Stinnere225beb2019-06-03 18:14:24 +0200431 SIGNAL_PENDING_SIGNALS(ceval);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200432}
433
Eric Snow5be45a62019-03-08 22:47:07 -0700434/* Push one item onto the queue while holding the lock. */
435static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200436_push_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600437 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700438{
Eric Snow842a2f02019-03-15 15:47:51 -0600439 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700440 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600441 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700442 return -1; /* Queue full */
443 }
Eric Snow842a2f02019-03-15 15:47:51 -0600444 pending->calls[i].func = func;
445 pending->calls[i].arg = arg;
446 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700447 return 0;
448}
449
450/* Pop one item off the queue while holding the lock. */
451static void
Victor Stinnere225beb2019-06-03 18:14:24 +0200452_pop_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600453 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700454{
Eric Snow842a2f02019-03-15 15:47:51 -0600455 int i = pending->first;
456 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700457 return; /* Queue empty */
458 }
459
Eric Snow842a2f02019-03-15 15:47:51 -0600460 *func = pending->calls[i].func;
461 *arg = pending->calls[i].arg;
462 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700463}
464
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200465/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000466 scheduling to be made from any thread, and even from an executing
467 callback.
468 */
469
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000470int
Victor Stinner438a12d2019-05-24 17:01:38 +0200471_PyEval_AddPendingCall(PyThreadState *tstate,
Victor Stinnere225beb2019-06-03 18:14:24 +0200472 struct _ceval_runtime_state *ceval,
Victor Stinner09532fe2019-05-10 23:39:09 +0200473 int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000474{
Victor Stinnere225beb2019-06-03 18:14:24 +0200475 struct _pending_calls *pending = &ceval->pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600476
477 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
478 if (pending->finishing) {
479 PyThread_release_lock(pending->lock);
480
481 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +0200482 _PyErr_Fetch(tstate, &exc, &val, &tb);
483 _PyErr_SetString(tstate, PyExc_SystemError,
Eric Snow842a2f02019-03-15 15:47:51 -0600484 "Py_AddPendingCall: cannot add pending calls "
485 "(Python shutting down)");
Victor Stinner438a12d2019-05-24 17:01:38 +0200486 _PyErr_Print(tstate);
487 _PyErr_Restore(tstate, exc, val, tb);
Eric Snow842a2f02019-03-15 15:47:51 -0600488 return -1;
489 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200490 int result = _push_pending_call(pending, func, arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600491 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700492
Victor Stinnere225beb2019-06-03 18:14:24 +0200493 /* signal main loop */
494 SIGNAL_PENDING_CALLS(ceval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000496}
497
Victor Stinner09532fe2019-05-10 23:39:09 +0200498int
499Py_AddPendingCall(int (*func)(void *), void *arg)
500{
Victor Stinner438a12d2019-05-24 17:01:38 +0200501 _PyRuntimeState *runtime = &_PyRuntime;
502 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinnere225beb2019-06-03 18:14:24 +0200503 return _PyEval_AddPendingCall(tstate, &runtime->ceval, func, arg);
Victor Stinner09532fe2019-05-10 23:39:09 +0200504}
505
Eric Snowfdf282d2019-01-11 14:26:55 -0700506static int
Victor Stinner09532fe2019-05-10 23:39:09 +0200507handle_signals(_PyRuntimeState *runtime)
Eric Snowfdf282d2019-01-11 14:26:55 -0700508{
Eric Snow5be45a62019-03-08 22:47:07 -0700509 /* Only handle signals on main thread. PyEval_InitThreads must
510 * have been called already.
511 */
Victor Stinner09532fe2019-05-10 23:39:09 +0200512 if (PyThread_get_thread_ident() != runtime->main_thread) {
Eric Snowfdf282d2019-01-11 14:26:55 -0700513 return 0;
514 }
Eric Snow64d6cc82019-02-23 15:40:43 -0700515 /*
516 * Ensure that the thread isn't currently running some other
517 * interpreter.
518 */
Victor Stinner09532fe2019-05-10 23:39:09 +0200519 PyInterpreterState *interp = _PyRuntimeState_GetThreadState(runtime)->interp;
520 if (interp != runtime->interpreters.main) {
Eric Snow64d6cc82019-02-23 15:40:43 -0700521 return 0;
522 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700523
Victor Stinnere225beb2019-06-03 18:14:24 +0200524 struct _ceval_runtime_state *ceval = &runtime->ceval;
525 UNSIGNAL_PENDING_SIGNALS(ceval);
Eric Snow64d6cc82019-02-23 15:40:43 -0700526 if (_PyErr_CheckSignals() < 0) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200527 SIGNAL_PENDING_SIGNALS(ceval); /* We're not done yet */
Eric Snowfdf282d2019-01-11 14:26:55 -0700528 return -1;
529 }
530 return 0;
531}
532
533static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200534make_pending_calls(_PyRuntimeState *runtime)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000535{
Eric Snow6a150bc2019-06-01 15:39:46 -0600536 static int busy = 0;
Eric Snowb75b1a352019-04-12 10:20:10 -0600537
Victor Stinnere225beb2019-06-03 18:14:24 +0200538 /* only service pending calls on main thread */
539 if (PyThread_get_thread_ident() != runtime->main_thread) {
540 return 0;
541 }
542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 /* don't perform recursive pending calls */
Eric Snowfdf282d2019-01-11 14:26:55 -0700544 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700546 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200547 busy = 1;
Victor Stinnere225beb2019-06-03 18:14:24 +0200548 struct _ceval_runtime_state *ceval = &runtime->ceval;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200549 /* unsignal before starting to call callbacks, so that any callback
550 added in-between re-signals */
Victor Stinnere225beb2019-06-03 18:14:24 +0200551 UNSIGNAL_PENDING_CALLS(ceval);
Eric Snowfdf282d2019-01-11 14:26:55 -0700552 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 /* perform a bounded number of calls, in case of recursion */
Victor Stinnere225beb2019-06-03 18:14:24 +0200555 struct _pending_calls *pending = &ceval->pending;
Eric Snowfdf282d2019-01-11 14:26:55 -0700556 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700557 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 void *arg = NULL;
559
560 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600561 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200562 _pop_pending_call(pending, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600563 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700564
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100565 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700566 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100567 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700568 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700569 res = func(arg);
570 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200571 goto error;
572 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200574
Charles-François Natalif23339a2011-07-23 18:15:43 +0200575 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700576 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200577
578error:
579 busy = 0;
Victor Stinnere225beb2019-06-03 18:14:24 +0200580 SIGNAL_PENDING_CALLS(ceval);
Eric Snowfdf282d2019-01-11 14:26:55 -0700581 return res;
582}
583
Eric Snow842a2f02019-03-15 15:47:51 -0600584void
Victor Stinnere225beb2019-06-03 18:14:24 +0200585_Py_FinishPendingCalls(_PyRuntimeState *runtime)
Eric Snow842a2f02019-03-15 15:47:51 -0600586{
Eric Snow842a2f02019-03-15 15:47:51 -0600587 assert(PyGILState_Check());
588
Victor Stinnere225beb2019-06-03 18:14:24 +0200589 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
590 struct _pending_calls *pending = &runtime->ceval.pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200591
Eric Snow842a2f02019-03-15 15:47:51 -0600592 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
593 pending->finishing = 1;
594 PyThread_release_lock(pending->lock);
595
596 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
597 return;
598 }
599
Victor Stinnere225beb2019-06-03 18:14:24 +0200600 if (make_pending_calls(runtime) < 0) {
601 PyObject *exc, *val, *tb;
602 _PyErr_Fetch(tstate, &exc, &val, &tb);
603 PyErr_BadInternalCall();
604 _PyErr_ChainExceptions(exc, val, tb);
605 _PyErr_Print(tstate);
Eric Snow842a2f02019-03-15 15:47:51 -0600606 }
607}
608
Eric Snowfdf282d2019-01-11 14:26:55 -0700609/* Py_MakePendingCalls() is a simple wrapper for the sake
610 of backward-compatibility. */
611int
612Py_MakePendingCalls(void)
613{
614 assert(PyGILState_Check());
615
616 /* Python signal handler doesn't really queue a callback: it only signals
617 that a signal was received, see _PyEval_SignalReceived(). */
Victor Stinner09532fe2019-05-10 23:39:09 +0200618 _PyRuntimeState *runtime = &_PyRuntime;
619 int res = handle_signals(runtime);
Eric Snowfdf282d2019-01-11 14:26:55 -0700620 if (res != 0) {
621 return res;
622 }
623
Victor Stinnere225beb2019-06-03 18:14:24 +0200624 res = make_pending_calls(runtime);
Eric Snowb75b1a352019-04-12 10:20:10 -0600625 if (res != 0) {
626 return res;
627 }
628
629 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000630}
631
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000632/* The interpreter's recursion limit */
633
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000634#ifndef Py_DEFAULT_RECURSION_LIMIT
635#define Py_DEFAULT_RECURSION_LIMIT 1000
636#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600637
Eric Snow05351c12017-09-05 21:43:08 -0700638int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000639
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600640void
Victor Stinnere225beb2019-06-03 18:14:24 +0200641_PyEval_Initialize(struct _ceval_runtime_state *state)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600642{
Victor Stinnere225beb2019-06-03 18:14:24 +0200643 state->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600644 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Victor Stinnere225beb2019-06-03 18:14:24 +0200645 _gil_initialize(&state->gil);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600646}
647
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000648int
649Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000650{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600651 return _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000652}
653
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000654void
655Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000656{
Victor Stinnere225beb2019-06-03 18:14:24 +0200657 struct _ceval_runtime_state *ceval = &_PyRuntime.ceval;
658 ceval->recursion_limit = new_limit;
659 _Py_CheckRecursionLimit = ceval->recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000660}
661
Armin Rigo2b3eb402003-10-28 12:05:48 +0000662/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
663 if the recursion_depth reaches _Py_CheckRecursionLimit.
664 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
665 to guarantee that _Py_CheckRecursiveCall() is regularly called.
666 Without USE_STACKCHECK, there is no need for this. */
667int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300668_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000669{
Victor Stinner09532fe2019-05-10 23:39:09 +0200670 _PyRuntimeState *runtime = &_PyRuntime;
671 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
672 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 *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000726PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 /* This is for backward compatibility with extension modules that
728 used this API; core interpreter code should call
729 PyEval_EvalFrameEx() */
730 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000731}
732
733PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000734PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000735{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200736 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
737 return interp->eval_frame(f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700738}
739
Victor Stinnerc6944e72016-11-11 02:13:35 +0100740PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700741_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
742{
Guido van Rossum950361c1997-01-24 13:49:28 +0000743#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000745#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200746 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300747 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200748 int opcode; /* Current opcode */
749 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200750 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751 PyObject *retval = NULL; /* Return value */
Victor Stinner09532fe2019-05-10 23:39:09 +0200752 _PyRuntimeState * const runtime = &_PyRuntime;
753 PyThreadState * const tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinnere225beb2019-06-03 18:14:24 +0200754 struct _ceval_runtime_state * const ceval = &runtime->ceval;
755 _Py_atomic_int * const eval_breaker = &ceval->eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000757
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000759
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 is true when the line being executed has changed. The
763 initial values are such as to make this false the first
764 time it is tested. */
765 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000766
Serhiy Storchakaab874002016-09-11 13:48:15 +0300767 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 PyObject *names;
769 PyObject *consts;
Inada Naoki91234a12019-06-03 21:30:58 +0900770 _PyOpcache *co_opcache;
Guido van Rossum374a9221991-04-04 10:40:29 +0000771
Brett Cannon368b4b72012-04-02 12:17:59 -0400772#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200773 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400774#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200775
Antoine Pitroub52ec782009-01-25 16:34:23 +0000776/* Computed GOTOs, or
777 the-optimization-commonly-but-improperly-known-as-"threaded code"
778 using gcc's labels-as-values extension
779 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
780
781 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000783 combined with a lookup table of jump addresses. However, since the
784 indirect jump instruction is shared by all opcodes, the CPU will have a
785 hard time making the right prediction for where to jump next (actually,
786 it will be always wrong except in the uncommon case of a sequence of
787 several identical opcodes).
788
789 "Threaded code" in contrast, uses an explicit jump table and an explicit
790 indirect jump instruction at the end of each opcode. Since the jump
791 instruction is at a different address for each opcode, the CPU will make a
792 separate prediction for each of these instructions, which is equivalent to
793 predicting the second opcode of each opcode pair. These predictions have
794 a much better chance to turn out valid, especially in small bytecode loops.
795
796 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000798 and potentially many more instructions (depending on the pipeline width).
799 A correctly predicted branch, however, is nearly free.
800
801 At the time of this writing, the "threaded code" version is up to 15-20%
802 faster than the normal "switch" version, depending on the compiler and the
803 CPU architecture.
804
805 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
806 because it would render the measurements invalid.
807
808
809 NOTE: care must be taken that the compiler doesn't try to "optimize" the
810 indirect jumps by sharing them between all opcodes. Such optimizations
811 can be disabled on gcc by using the -fno-gcse flag (or possibly
812 -fno-crossjumping).
813*/
814
Antoine Pitrou042b1282010-08-13 21:15:58 +0000815#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000816#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000817#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000818#endif
819
Antoine Pitrou042b1282010-08-13 21:15:58 +0000820#ifdef HAVE_COMPUTED_GOTOS
821 #ifndef USE_COMPUTED_GOTOS
822 #define USE_COMPUTED_GOTOS 1
823 #endif
824#else
825 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
826 #error "Computed gotos are not supported on this compiler."
827 #endif
828 #undef USE_COMPUTED_GOTOS
829 #define USE_COMPUTED_GOTOS 0
830#endif
831
832#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000833/* Import the static jump table */
834#include "opcode_targets.h"
835
Antoine Pitroub52ec782009-01-25 16:34:23 +0000836#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -0700837 op: \
838 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000839
Antoine Pitroub52ec782009-01-25 16:34:23 +0000840#ifdef LLTRACE
841#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200843 if (!lltrace && !_Py_TracingPossible(ceval) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300845 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300846 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 } \
848 goto fast_next_opcode; \
849 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000850#else
851#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200853 if (!_Py_TracingPossible(ceval) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300855 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300856 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 } \
858 goto fast_next_opcode; \
859 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000860#endif
861
Victor Stinner09532fe2019-05-10 23:39:09 +0200862#define DISPATCH() \
863 { \
864 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
865 FAST_DISPATCH(); \
866 } \
867 continue; \
868 }
869
Antoine Pitroub52ec782009-01-25 16:34:23 +0000870#else
Benjamin Petersonddd19492018-09-16 22:38:02 -0700871#define TARGET(op) op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000872#define FAST_DISPATCH() goto fast_next_opcode
Victor Stinner09532fe2019-05-10 23:39:09 +0200873#define DISPATCH() continue
Antoine Pitroub52ec782009-01-25 16:34:23 +0000874#endif
875
876
Neal Norwitza81d2202002-07-14 00:27:26 +0000877/* Tuple access macros */
878
879#ifndef Py_DEBUG
880#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
881#else
882#define GETITEM(v, i) PyTuple_GetItem((v), (i))
883#endif
884
Guido van Rossum374a9221991-04-04 10:40:29 +0000885/* Code access macros */
886
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300887/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600888#define INSTR_OFFSET() \
889 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300890#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300891 _Py_CODEUNIT word = *next_instr; \
892 opcode = _Py_OPCODE(word); \
893 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300894 next_instr++; \
895 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300896#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
897#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000898
Raymond Hettingerf606f872003-03-16 03:11:04 +0000899/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 Some opcodes tend to come in pairs thus making it possible to
901 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300902 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000903
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 Verifying the prediction costs a single high-speed test of a register
905 variable against a constant. If the pairing was good, then the
906 processor's own internal branch predication has a high likelihood of
907 success, resulting in a nearly zero-overhead transition to the
908 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300909 including its unpredictable switch-case branch. Combined with the
910 processor's internal branch prediction, a successful PREDICT has the
911 effect of making the two opcodes run as if they were a single new opcode
912 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000913
Georg Brandl86b2fb92008-07-16 03:43:04 +0000914 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 predictions turned-on and interpret the results as if some opcodes
916 had been combined or turn-off predictions so that the opcode frequency
917 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000918
919 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 the CPU to record separate branch prediction information for each
921 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000922
Raymond Hettingerf606f872003-03-16 03:11:04 +0000923*/
924
Antoine Pitrou042b1282010-08-13 21:15:58 +0000925#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000927#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300928#define PREDICT(op) \
929 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300930 _Py_CODEUNIT word = *next_instr; \
931 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300932 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300933 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300934 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300935 goto PRED_##op; \
936 } \
937 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000938#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300939#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000940
Raymond Hettingerf606f872003-03-16 03:11:04 +0000941
Guido van Rossum374a9221991-04-04 10:40:29 +0000942/* Stack manipulation macros */
943
Martin v. Löwis18e16552006-02-15 17:27:45 +0000944/* The stack can grow at most MAXINT deep, as co_nlocals and
945 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000946#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
947#define EMPTY() (STACK_LEVEL() == 0)
948#define TOP() (stack_pointer[-1])
949#define SECOND() (stack_pointer[-2])
950#define THIRD() (stack_pointer[-3])
951#define FOURTH() (stack_pointer[-4])
952#define PEEK(n) (stack_pointer[-(n)])
953#define SET_TOP(v) (stack_pointer[-1] = (v))
954#define SET_SECOND(v) (stack_pointer[-2] = (v))
955#define SET_THIRD(v) (stack_pointer[-3] = (v))
956#define SET_FOURTH(v) (stack_pointer[-4] = (v))
957#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
958#define BASIC_STACKADJ(n) (stack_pointer += n)
959#define BASIC_PUSH(v) (*stack_pointer++ = (v))
960#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000961
Guido van Rossum96a42c81992-01-12 02:29:51 +0000962#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +0200964 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000965 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +0200966#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000967 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +0000968#define STACK_GROW(n) do { \
969 assert(n >= 0); \
970 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +0200971 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +0000972 assert(STACK_LEVEL() <= co->co_stacksize); \
973 } while (0)
974#define STACK_SHRINK(n) do { \
975 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +0200976 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +0000977 (void)(BASIC_STACKADJ(-n)); \
978 assert(STACK_LEVEL() <= co->co_stacksize); \
979 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +0000980#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +0200981 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000982 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000983#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000984#define PUSH(v) BASIC_PUSH(v)
985#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +0000986#define STACK_GROW(n) BASIC_STACKADJ(n)
987#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000988#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000989#endif
990
Guido van Rossum681d79a1995-07-18 14:51:37 +0000991/* Local variable macros */
992
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000994
995/* The SETLOCAL() macro must not DECREF the local variable in-place and
996 then store the new value; it must copy the old value to a temporary
997 value, then store the new value, and then DECREF the temporary value.
998 This is because it is possible that during the DECREF the frame is
999 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1000 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001002 GETLOCAL(i) = value; \
1003 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001004
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001005
1006#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 while (STACK_LEVEL() > (b)->b_level) { \
1008 PyObject *v = POP(); \
1009 Py_XDECREF(v); \
1010 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001011
1012#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001013 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +01001015 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1017 while (STACK_LEVEL() > (b)->b_level + 3) { \
1018 value = POP(); \
1019 Py_XDECREF(value); \
1020 } \
Mark Shannonae3087c2017-10-22 22:41:51 +01001021 exc_info = tstate->exc_info; \
1022 type = exc_info->exc_type; \
1023 value = exc_info->exc_value; \
1024 traceback = exc_info->exc_traceback; \
1025 exc_info->exc_type = POP(); \
1026 exc_info->exc_value = POP(); \
1027 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 Py_XDECREF(type); \
1029 Py_XDECREF(value); \
1030 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001031 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001032
Inada Naoki91234a12019-06-03 21:30:58 +09001033 /* macros for opcode cache */
1034#define OPCACHE_CHECK() \
1035 do { \
1036 co_opcache = NULL; \
1037 if (co->co_opcache != NULL) { \
1038 unsigned char co_opt_offset = \
1039 co->co_opcache_map[next_instr - first_instr]; \
1040 if (co_opt_offset > 0) { \
1041 assert(co_opt_offset <= co->co_opcache_size); \
1042 co_opcache = &co->co_opcache[co_opt_offset - 1]; \
1043 assert(co_opcache != NULL); \
Inada Naoki91234a12019-06-03 21:30:58 +09001044 } \
1045 } \
1046 } while (0)
1047
1048#if OPCACHE_STATS
1049
1050#define OPCACHE_STAT_GLOBAL_HIT() \
1051 do { \
1052 if (co->co_opcache != NULL) opcache_global_hits++; \
1053 } while (0)
1054
1055#define OPCACHE_STAT_GLOBAL_MISS() \
1056 do { \
1057 if (co->co_opcache != NULL) opcache_global_misses++; \
1058 } while (0)
1059
1060#define OPCACHE_STAT_GLOBAL_OPT() \
1061 do { \
1062 if (co->co_opcache != NULL) opcache_global_opts++; \
1063 } while (0)
1064
1065#else /* OPCACHE_STATS */
1066
1067#define OPCACHE_STAT_GLOBAL_HIT()
1068#define OPCACHE_STAT_GLOBAL_MISS()
1069#define OPCACHE_STAT_GLOBAL_OPT()
1070
1071#endif
1072
Guido van Rossuma027efa1997-05-05 20:56:21 +00001073/* Start of code */
1074
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 /* push frame */
1076 if (Py_EnterRecursiveCall(""))
1077 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001078
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001080
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081 if (tstate->use_tracing) {
1082 if (tstate->c_tracefunc != NULL) {
1083 /* tstate->c_tracefunc, if defined, is a
1084 function that will be called on *every* entry
1085 to a code block. Its return value, if not
1086 None, is a function that will be called at
1087 the start of each executed line of code.
1088 (Actually, the function must return itself
1089 in order to continue tracing.) The trace
1090 functions are called with three arguments:
1091 a pointer to the current frame, a string
1092 indicating why the function is called, and
1093 an argument which depends on the situation.
1094 The global trace function is also called
1095 whenever an exception is detected. */
1096 if (call_trace_protected(tstate->c_tracefunc,
1097 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001098 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 /* Trace function raised an error */
1100 goto exit_eval_frame;
1101 }
1102 }
1103 if (tstate->c_profilefunc != NULL) {
1104 /* Similar for c_profilefunc, except it needn't
1105 return itself and isn't called for "line" events */
1106 if (call_trace_protected(tstate->c_profilefunc,
1107 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001108 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 /* Profile function raised an error */
1110 goto exit_eval_frame;
1111 }
1112 }
1113 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001114
Łukasz Langaa785c872016-09-09 17:37:37 -07001115 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1116 dtrace_function_entry(f);
1117
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 co = f->f_code;
1119 names = co->co_names;
1120 consts = co->co_consts;
1121 fastlocals = f->f_localsplus;
1122 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001123 assert(PyBytes_Check(co->co_code));
1124 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001125 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1126 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1127 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001128 /*
1129 f->f_lasti refers to the index of the last instruction,
1130 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001131
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001132 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001133 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001134
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 When the PREDICT() macros are enabled, some opcode pairs follow in
1136 direct succession without updating f->f_lasti. A successful
1137 prediction effectively links the two codes together as if they
1138 were a single new opcode; accordingly,f->f_lasti will point to
1139 the first code in the pair (for instance, GET_ITER followed by
1140 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001141 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001143 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001144 next_instr = first_instr;
1145 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001146 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1147 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001148 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 stack_pointer = f->f_stacktop;
1150 assert(stack_pointer != NULL);
1151 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001152 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001153
Inada Naoki91234a12019-06-03 21:30:58 +09001154 if (co->co_opcache_flag < OPCACHE_MIN_RUNS) {
1155 co->co_opcache_flag++;
1156 if (co->co_opcache_flag == OPCACHE_MIN_RUNS) {
1157 if (_PyCode_InitOpcache(co) < 0) {
1158 return NULL;
1159 }
1160#if OPCACHE_STATS
1161 opcache_code_objects_extra_mem +=
1162 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1163 sizeof(_PyOpcache) * co->co_opcache_size;
1164 opcache_code_objects++;
1165#endif
1166 }
1167 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001168
Tim Peters5ca576e2001-06-18 22:08:13 +00001169#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001170 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001171#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001172
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001173 if (throwflag) /* support for generator.throw() */
1174 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001175
Victor Stinnerace47d72013-07-18 01:41:08 +02001176#ifdef Py_DEBUG
1177 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001178 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001179 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001180 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001181#endif
1182
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001183main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1186 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001187 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 /* Do periodic things. Doing this every time through
1190 the loop would add too much overhead, so we do it
1191 only every Nth instruction. We also do it if
1192 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1193 event needs attention (e.g. a signal handler or
1194 async I/O handler); see Py_AddPendingCall() and
1195 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001196
Eric Snow7bda9de2019-03-08 17:25:54 -07001197 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001198 opcode = _Py_OPCODE(*next_instr);
1199 if (opcode == SETUP_FINALLY ||
1200 opcode == SETUP_WITH ||
1201 opcode == BEFORE_ASYNC_WITH ||
1202 opcode == YIELD_FROM) {
1203 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001204 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001205 - If we're about to enter the 'with:'. It will prevent
1206 emitting a resource warning in the common idiom
1207 'with open(path) as file:'.
1208 - If we're about to enter the 'async with:'.
1209 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001210 *very* useful, but might help in some cases and it's
1211 traditional)
1212 - If we're resuming a chain of nested 'yield from' or
1213 'await' calls, then each frame is parked with YIELD_FROM
1214 as its next opcode. If the user hit control-C we want to
1215 wait until we've reached the innermost frame before
1216 running the signal handler and raising KeyboardInterrupt
1217 (see bpo-30039).
1218 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 goto fast_next_opcode;
1220 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001221
Victor Stinnere225beb2019-06-03 18:14:24 +02001222 if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
Victor Stinner09532fe2019-05-10 23:39:09 +02001223 if (handle_signals(runtime) != 0) {
Eric Snowfdf282d2019-01-11 14:26:55 -07001224 goto error;
1225 }
1226 }
Victor Stinnere225beb2019-06-03 18:14:24 +02001227 if (_Py_atomic_load_relaxed(&ceval->pending.calls_to_do)) {
1228 if (make_pending_calls(runtime) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001229 goto error;
Eric Snowfdf282d2019-01-11 14:26:55 -07001230 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001231 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001232
Victor Stinnere225beb2019-06-03 18:14:24 +02001233 if (_Py_atomic_load_relaxed(&ceval->gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 /* Give another thread a chance */
Victor Stinner09532fe2019-05-10 23:39:09 +02001235 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 Py_FatalError("ceval: tstate mix-up");
Victor Stinner09532fe2019-05-10 23:39:09 +02001237 }
Victor Stinnere225beb2019-06-03 18:14:24 +02001238 drop_gil(ceval, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239
1240 /* Other threads may run now */
1241
Victor Stinnere225beb2019-06-03 18:14:24 +02001242 take_gil(ceval, tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001243
1244 /* Check if we should make a quick exit. */
Victor Stinner0fd2c302019-06-04 03:15:09 +02001245 exit_thread_if_finalizing(runtime, tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001246
Victor Stinner09532fe2019-05-10 23:39:09 +02001247 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 Py_FatalError("ceval: orphan tstate");
Victor Stinner09532fe2019-05-10 23:39:09 +02001249 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 }
1251 /* Check for asynchronous exceptions. */
1252 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001253 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 tstate->async_exc = NULL;
Victor Stinnere225beb2019-06-03 18:14:24 +02001255 UNSIGNAL_ASYNC_EXC(ceval);
Victor Stinner438a12d2019-05-24 17:01:38 +02001256 _PyErr_SetNone(tstate, exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001257 Py_DECREF(exc);
1258 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 }
1260 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001261
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001262 fast_next_opcode:
1263 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001264
Łukasz Langaa785c872016-09-09 17:37:37 -07001265 if (PyDTrace_LINE_ENABLED())
1266 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001269
Victor Stinnere225beb2019-06-03 18:14:24 +02001270 if (_Py_TracingPossible(ceval) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001271 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001272 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 /* see maybe_call_line_trace
1274 for expository comments */
1275 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 err = maybe_call_line_trace(tstate->c_tracefunc,
1278 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001279 tstate, f,
1280 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 /* Reload possibly changed frame fields */
1282 JUMPTO(f->f_lasti);
1283 if (f->f_stacktop != NULL) {
1284 stack_pointer = f->f_stacktop;
1285 f->f_stacktop = NULL;
1286 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001287 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001289 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001293
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001294 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001295 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001296#ifdef DYNAMIC_EXECUTION_PROFILE
1297#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 dxpairs[lastopcode][opcode]++;
1299 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001300#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001301 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001302#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001303
Guido van Rossum96a42c81992-01-12 02:29:51 +00001304#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 if (lltrace) {
1308 if (HAS_ARG(opcode)) {
1309 printf("%d: %d, %d\n",
1310 f->f_lasti, opcode, oparg);
1311 }
1312 else {
1313 printf("%d: %d\n",
1314 f->f_lasti, opcode);
1315 }
1316 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001317#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001322 It is essential that any operation that fails must goto error
1323 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001324
Benjamin Petersonddd19492018-09-16 22:38:02 -07001325 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001327 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001328
Benjamin Petersonddd19492018-09-16 22:38:02 -07001329 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001330 PyObject *value = GETLOCAL(oparg);
1331 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001332 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001333 UNBOUNDLOCAL_ERROR_MSG,
1334 PyTuple_GetItem(co->co_varnames, oparg));
1335 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001337 Py_INCREF(value);
1338 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001340 }
1341
Benjamin Petersonddd19492018-09-16 22:38:02 -07001342 case TARGET(LOAD_CONST): {
1343 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001344 PyObject *value = GETITEM(consts, oparg);
1345 Py_INCREF(value);
1346 PUSH(value);
1347 FAST_DISPATCH();
1348 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001349
Benjamin Petersonddd19492018-09-16 22:38:02 -07001350 case TARGET(STORE_FAST): {
1351 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001352 PyObject *value = POP();
1353 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001355 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001356
Benjamin Petersonddd19492018-09-16 22:38:02 -07001357 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001358 PyObject *value = POP();
1359 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001361 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001362
Benjamin Petersonddd19492018-09-16 22:38:02 -07001363 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001364 PyObject *top = TOP();
1365 PyObject *second = SECOND();
1366 SET_TOP(second);
1367 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001368 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001369 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001370
Benjamin Petersonddd19492018-09-16 22:38:02 -07001371 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001372 PyObject *top = TOP();
1373 PyObject *second = SECOND();
1374 PyObject *third = THIRD();
1375 SET_TOP(second);
1376 SET_SECOND(third);
1377 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001379 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001380
Benjamin Petersonddd19492018-09-16 22:38:02 -07001381 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001382 PyObject *top = TOP();
1383 PyObject *second = SECOND();
1384 PyObject *third = THIRD();
1385 PyObject *fourth = FOURTH();
1386 SET_TOP(second);
1387 SET_SECOND(third);
1388 SET_THIRD(fourth);
1389 SET_FOURTH(top);
1390 FAST_DISPATCH();
1391 }
1392
Benjamin Petersonddd19492018-09-16 22:38:02 -07001393 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001394 PyObject *top = TOP();
1395 Py_INCREF(top);
1396 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001398 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001399
Benjamin Petersonddd19492018-09-16 22:38:02 -07001400 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001401 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001402 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001403 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001404 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001405 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001406 SET_TOP(top);
1407 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001408 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001409 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001410
Benjamin Petersonddd19492018-09-16 22:38:02 -07001411 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001412 PyObject *value = TOP();
1413 PyObject *res = PyNumber_Positive(value);
1414 Py_DECREF(value);
1415 SET_TOP(res);
1416 if (res == NULL)
1417 goto error;
1418 DISPATCH();
1419 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001420
Benjamin Petersonddd19492018-09-16 22:38:02 -07001421 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001422 PyObject *value = TOP();
1423 PyObject *res = PyNumber_Negative(value);
1424 Py_DECREF(value);
1425 SET_TOP(res);
1426 if (res == NULL)
1427 goto error;
1428 DISPATCH();
1429 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001430
Benjamin Petersonddd19492018-09-16 22:38:02 -07001431 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001432 PyObject *value = TOP();
1433 int err = PyObject_IsTrue(value);
1434 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 if (err == 0) {
1436 Py_INCREF(Py_True);
1437 SET_TOP(Py_True);
1438 DISPATCH();
1439 }
1440 else if (err > 0) {
1441 Py_INCREF(Py_False);
1442 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 DISPATCH();
1444 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001445 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001446 goto error;
1447 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001448
Benjamin Petersonddd19492018-09-16 22:38:02 -07001449 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001450 PyObject *value = TOP();
1451 PyObject *res = PyNumber_Invert(value);
1452 Py_DECREF(value);
1453 SET_TOP(res);
1454 if (res == NULL)
1455 goto error;
1456 DISPATCH();
1457 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001458
Benjamin Petersonddd19492018-09-16 22:38:02 -07001459 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001460 PyObject *exp = POP();
1461 PyObject *base = TOP();
1462 PyObject *res = PyNumber_Power(base, exp, Py_None);
1463 Py_DECREF(base);
1464 Py_DECREF(exp);
1465 SET_TOP(res);
1466 if (res == NULL)
1467 goto error;
1468 DISPATCH();
1469 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001470
Benjamin Petersonddd19492018-09-16 22:38:02 -07001471 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001472 PyObject *right = POP();
1473 PyObject *left = TOP();
1474 PyObject *res = PyNumber_Multiply(left, right);
1475 Py_DECREF(left);
1476 Py_DECREF(right);
1477 SET_TOP(res);
1478 if (res == NULL)
1479 goto error;
1480 DISPATCH();
1481 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001482
Benjamin Petersonddd19492018-09-16 22:38:02 -07001483 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001484 PyObject *right = POP();
1485 PyObject *left = TOP();
1486 PyObject *res = PyNumber_MatrixMultiply(left, right);
1487 Py_DECREF(left);
1488 Py_DECREF(right);
1489 SET_TOP(res);
1490 if (res == NULL)
1491 goto error;
1492 DISPATCH();
1493 }
1494
Benjamin Petersonddd19492018-09-16 22:38:02 -07001495 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001496 PyObject *divisor = POP();
1497 PyObject *dividend = TOP();
1498 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1499 Py_DECREF(dividend);
1500 Py_DECREF(divisor);
1501 SET_TOP(quotient);
1502 if (quotient == NULL)
1503 goto error;
1504 DISPATCH();
1505 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001506
Benjamin Petersonddd19492018-09-16 22:38:02 -07001507 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001508 PyObject *divisor = POP();
1509 PyObject *dividend = TOP();
1510 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1511 Py_DECREF(dividend);
1512 Py_DECREF(divisor);
1513 SET_TOP(quotient);
1514 if (quotient == NULL)
1515 goto error;
1516 DISPATCH();
1517 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001518
Benjamin Petersonddd19492018-09-16 22:38:02 -07001519 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001520 PyObject *divisor = POP();
1521 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001522 PyObject *res;
1523 if (PyUnicode_CheckExact(dividend) && (
1524 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1525 // fast path; string formatting, but not if the RHS is a str subclass
1526 // (see issue28598)
1527 res = PyUnicode_Format(dividend, divisor);
1528 } else {
1529 res = PyNumber_Remainder(dividend, divisor);
1530 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001531 Py_DECREF(divisor);
1532 Py_DECREF(dividend);
1533 SET_TOP(res);
1534 if (res == NULL)
1535 goto error;
1536 DISPATCH();
1537 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001538
Benjamin Petersonddd19492018-09-16 22:38:02 -07001539 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001540 PyObject *right = POP();
1541 PyObject *left = TOP();
1542 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001543 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1544 CPython using bytecode, it is simply worthless.
1545 See http://bugs.python.org/issue21955 and
1546 http://bugs.python.org/issue10044 for the discussion. In short,
1547 no patch shown any impact on a realistic benchmark, only a minor
1548 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001549 if (PyUnicode_CheckExact(left) &&
1550 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001551 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001552 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001553 }
1554 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001555 sum = PyNumber_Add(left, right);
1556 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001557 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001558 Py_DECREF(right);
1559 SET_TOP(sum);
1560 if (sum == NULL)
1561 goto error;
1562 DISPATCH();
1563 }
1564
Benjamin Petersonddd19492018-09-16 22:38:02 -07001565 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001566 PyObject *right = POP();
1567 PyObject *left = TOP();
1568 PyObject *diff = PyNumber_Subtract(left, right);
1569 Py_DECREF(right);
1570 Py_DECREF(left);
1571 SET_TOP(diff);
1572 if (diff == NULL)
1573 goto error;
1574 DISPATCH();
1575 }
1576
Benjamin Petersonddd19492018-09-16 22:38:02 -07001577 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001578 PyObject *sub = POP();
1579 PyObject *container = TOP();
1580 PyObject *res = PyObject_GetItem(container, sub);
1581 Py_DECREF(container);
1582 Py_DECREF(sub);
1583 SET_TOP(res);
1584 if (res == NULL)
1585 goto error;
1586 DISPATCH();
1587 }
1588
Benjamin Petersonddd19492018-09-16 22:38:02 -07001589 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001590 PyObject *right = POP();
1591 PyObject *left = TOP();
1592 PyObject *res = PyNumber_Lshift(left, right);
1593 Py_DECREF(left);
1594 Py_DECREF(right);
1595 SET_TOP(res);
1596 if (res == NULL)
1597 goto error;
1598 DISPATCH();
1599 }
1600
Benjamin Petersonddd19492018-09-16 22:38:02 -07001601 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001602 PyObject *right = POP();
1603 PyObject *left = TOP();
1604 PyObject *res = PyNumber_Rshift(left, right);
1605 Py_DECREF(left);
1606 Py_DECREF(right);
1607 SET_TOP(res);
1608 if (res == NULL)
1609 goto error;
1610 DISPATCH();
1611 }
1612
Benjamin Petersonddd19492018-09-16 22:38:02 -07001613 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001614 PyObject *right = POP();
1615 PyObject *left = TOP();
1616 PyObject *res = PyNumber_And(left, right);
1617 Py_DECREF(left);
1618 Py_DECREF(right);
1619 SET_TOP(res);
1620 if (res == NULL)
1621 goto error;
1622 DISPATCH();
1623 }
1624
Benjamin Petersonddd19492018-09-16 22:38:02 -07001625 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001626 PyObject *right = POP();
1627 PyObject *left = TOP();
1628 PyObject *res = PyNumber_Xor(left, right);
1629 Py_DECREF(left);
1630 Py_DECREF(right);
1631 SET_TOP(res);
1632 if (res == NULL)
1633 goto error;
1634 DISPATCH();
1635 }
1636
Benjamin Petersonddd19492018-09-16 22:38:02 -07001637 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001638 PyObject *right = POP();
1639 PyObject *left = TOP();
1640 PyObject *res = PyNumber_Or(left, right);
1641 Py_DECREF(left);
1642 Py_DECREF(right);
1643 SET_TOP(res);
1644 if (res == NULL)
1645 goto error;
1646 DISPATCH();
1647 }
1648
Benjamin Petersonddd19492018-09-16 22:38:02 -07001649 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001650 PyObject *v = POP();
1651 PyObject *list = PEEK(oparg);
1652 int err;
1653 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001655 if (err != 0)
1656 goto error;
1657 PREDICT(JUMP_ABSOLUTE);
1658 DISPATCH();
1659 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001660
Benjamin Petersonddd19492018-09-16 22:38:02 -07001661 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001662 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001663 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001664 int err;
1665 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001666 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001667 if (err != 0)
1668 goto error;
1669 PREDICT(JUMP_ABSOLUTE);
1670 DISPATCH();
1671 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001672
Benjamin Petersonddd19492018-09-16 22:38:02 -07001673 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001674 PyObject *exp = POP();
1675 PyObject *base = TOP();
1676 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1677 Py_DECREF(base);
1678 Py_DECREF(exp);
1679 SET_TOP(res);
1680 if (res == NULL)
1681 goto error;
1682 DISPATCH();
1683 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001684
Benjamin Petersonddd19492018-09-16 22:38:02 -07001685 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001686 PyObject *right = POP();
1687 PyObject *left = TOP();
1688 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1689 Py_DECREF(left);
1690 Py_DECREF(right);
1691 SET_TOP(res);
1692 if (res == NULL)
1693 goto error;
1694 DISPATCH();
1695 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001696
Benjamin Petersonddd19492018-09-16 22:38:02 -07001697 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001698 PyObject *right = POP();
1699 PyObject *left = TOP();
1700 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1701 Py_DECREF(left);
1702 Py_DECREF(right);
1703 SET_TOP(res);
1704 if (res == NULL)
1705 goto error;
1706 DISPATCH();
1707 }
1708
Benjamin Petersonddd19492018-09-16 22:38:02 -07001709 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001710 PyObject *divisor = POP();
1711 PyObject *dividend = TOP();
1712 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1713 Py_DECREF(dividend);
1714 Py_DECREF(divisor);
1715 SET_TOP(quotient);
1716 if (quotient == NULL)
1717 goto error;
1718 DISPATCH();
1719 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001720
Benjamin Petersonddd19492018-09-16 22:38:02 -07001721 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001722 PyObject *divisor = POP();
1723 PyObject *dividend = TOP();
1724 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1725 Py_DECREF(dividend);
1726 Py_DECREF(divisor);
1727 SET_TOP(quotient);
1728 if (quotient == NULL)
1729 goto error;
1730 DISPATCH();
1731 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001732
Benjamin Petersonddd19492018-09-16 22:38:02 -07001733 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001734 PyObject *right = POP();
1735 PyObject *left = TOP();
1736 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1737 Py_DECREF(left);
1738 Py_DECREF(right);
1739 SET_TOP(mod);
1740 if (mod == NULL)
1741 goto error;
1742 DISPATCH();
1743 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001744
Benjamin Petersonddd19492018-09-16 22:38:02 -07001745 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001746 PyObject *right = POP();
1747 PyObject *left = TOP();
1748 PyObject *sum;
1749 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001750 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001751 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001752 }
1753 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001754 sum = PyNumber_InPlaceAdd(left, right);
1755 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001756 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001757 Py_DECREF(right);
1758 SET_TOP(sum);
1759 if (sum == NULL)
1760 goto error;
1761 DISPATCH();
1762 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001763
Benjamin Petersonddd19492018-09-16 22:38:02 -07001764 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001765 PyObject *right = POP();
1766 PyObject *left = TOP();
1767 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1768 Py_DECREF(left);
1769 Py_DECREF(right);
1770 SET_TOP(diff);
1771 if (diff == NULL)
1772 goto error;
1773 DISPATCH();
1774 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001775
Benjamin Petersonddd19492018-09-16 22:38:02 -07001776 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001777 PyObject *right = POP();
1778 PyObject *left = TOP();
1779 PyObject *res = PyNumber_InPlaceLshift(left, right);
1780 Py_DECREF(left);
1781 Py_DECREF(right);
1782 SET_TOP(res);
1783 if (res == NULL)
1784 goto error;
1785 DISPATCH();
1786 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001787
Benjamin Petersonddd19492018-09-16 22:38:02 -07001788 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001789 PyObject *right = POP();
1790 PyObject *left = TOP();
1791 PyObject *res = PyNumber_InPlaceRshift(left, right);
1792 Py_DECREF(left);
1793 Py_DECREF(right);
1794 SET_TOP(res);
1795 if (res == NULL)
1796 goto error;
1797 DISPATCH();
1798 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001799
Benjamin Petersonddd19492018-09-16 22:38:02 -07001800 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001801 PyObject *right = POP();
1802 PyObject *left = TOP();
1803 PyObject *res = PyNumber_InPlaceAnd(left, right);
1804 Py_DECREF(left);
1805 Py_DECREF(right);
1806 SET_TOP(res);
1807 if (res == NULL)
1808 goto error;
1809 DISPATCH();
1810 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001811
Benjamin Petersonddd19492018-09-16 22:38:02 -07001812 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001813 PyObject *right = POP();
1814 PyObject *left = TOP();
1815 PyObject *res = PyNumber_InPlaceXor(left, right);
1816 Py_DECREF(left);
1817 Py_DECREF(right);
1818 SET_TOP(res);
1819 if (res == NULL)
1820 goto error;
1821 DISPATCH();
1822 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001823
Benjamin Petersonddd19492018-09-16 22:38:02 -07001824 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001825 PyObject *right = POP();
1826 PyObject *left = TOP();
1827 PyObject *res = PyNumber_InPlaceOr(left, right);
1828 Py_DECREF(left);
1829 Py_DECREF(right);
1830 SET_TOP(res);
1831 if (res == NULL)
1832 goto error;
1833 DISPATCH();
1834 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001835
Benjamin Petersonddd19492018-09-16 22:38:02 -07001836 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001837 PyObject *sub = TOP();
1838 PyObject *container = SECOND();
1839 PyObject *v = THIRD();
1840 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001841 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001842 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001843 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001844 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001845 Py_DECREF(container);
1846 Py_DECREF(sub);
1847 if (err != 0)
1848 goto error;
1849 DISPATCH();
1850 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001851
Benjamin Petersonddd19492018-09-16 22:38:02 -07001852 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001853 PyObject *sub = TOP();
1854 PyObject *container = SECOND();
1855 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001856 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00001857 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001858 err = PyObject_DelItem(container, sub);
1859 Py_DECREF(container);
1860 Py_DECREF(sub);
1861 if (err != 0)
1862 goto error;
1863 DISPATCH();
1864 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001865
Benjamin Petersonddd19492018-09-16 22:38:02 -07001866 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01001867 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001868 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001869 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001870 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001871 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001872 _PyErr_SetString(tstate, PyExc_RuntimeError,
1873 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001874 Py_DECREF(value);
1875 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876 }
Jeroen Demeyer196a5302019-07-04 12:31:34 +02001877 res = _PyObject_CallOneArg(hook, value);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001878 Py_DECREF(value);
1879 if (res == NULL)
1880 goto error;
1881 Py_DECREF(res);
1882 DISPATCH();
1883 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001884
Benjamin Petersonddd19492018-09-16 22:38:02 -07001885 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001886 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001887 switch (oparg) {
1888 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001889 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001890 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001891 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001892 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001893 /* fall through */
1894 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02001895 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001896 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001897 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 break;
1899 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02001900 _PyErr_SetString(tstate, PyExc_SystemError,
1901 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902 break;
1903 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001904 goto error;
1905 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001906
Benjamin Petersonddd19492018-09-16 22:38:02 -07001907 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001908 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001909 assert(f->f_iblock == 0);
Pablo Galindof00828a2019-05-09 16:52:02 +01001910 goto exit_returning;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001911 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001912
Benjamin Petersonddd19492018-09-16 22:38:02 -07001913 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001914 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001915 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001916 PyObject *obj = TOP();
1917 PyTypeObject *type = Py_TYPE(obj);
1918
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001919 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001920 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001921 }
Yury Selivanov75445082015-05-11 22:57:16 -04001922
1923 if (getter != NULL) {
1924 iter = (*getter)(obj);
1925 Py_DECREF(obj);
1926 if (iter == NULL) {
1927 SET_TOP(NULL);
1928 goto error;
1929 }
1930 }
1931 else {
1932 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02001933 _PyErr_Format(tstate, PyExc_TypeError,
1934 "'async for' requires an object with "
1935 "__aiter__ method, got %.100s",
1936 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001937 Py_DECREF(obj);
1938 goto error;
1939 }
1940
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001941 if (Py_TYPE(iter)->tp_as_async == NULL ||
1942 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001943
Yury Selivanov398ff912017-03-02 22:20:00 -05001944 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02001945 _PyErr_Format(tstate, PyExc_TypeError,
1946 "'async for' received an object from __aiter__ "
1947 "that does not implement __anext__: %.100s",
1948 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001949 Py_DECREF(iter);
1950 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001951 }
1952
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001953 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001954 DISPATCH();
1955 }
1956
Benjamin Petersonddd19492018-09-16 22:38:02 -07001957 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001958 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001959 PyObject *next_iter = NULL;
1960 PyObject *awaitable = NULL;
1961 PyObject *aiter = TOP();
1962 PyTypeObject *type = Py_TYPE(aiter);
1963
Yury Selivanoveb636452016-09-08 22:01:51 -07001964 if (PyAsyncGen_CheckExact(aiter)) {
1965 awaitable = type->tp_as_async->am_anext(aiter);
1966 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001967 goto error;
1968 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001969 } else {
1970 if (type->tp_as_async != NULL){
1971 getter = type->tp_as_async->am_anext;
1972 }
Yury Selivanov75445082015-05-11 22:57:16 -04001973
Yury Selivanoveb636452016-09-08 22:01:51 -07001974 if (getter != NULL) {
1975 next_iter = (*getter)(aiter);
1976 if (next_iter == NULL) {
1977 goto error;
1978 }
1979 }
1980 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02001981 _PyErr_Format(tstate, PyExc_TypeError,
1982 "'async for' requires an iterator with "
1983 "__anext__ method, got %.100s",
1984 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07001985 goto error;
1986 }
Yury Selivanov75445082015-05-11 22:57:16 -04001987
Yury Selivanoveb636452016-09-08 22:01:51 -07001988 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1989 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001990 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001991 PyExc_TypeError,
1992 "'async for' received an invalid object "
1993 "from __anext__: %.100s",
1994 Py_TYPE(next_iter)->tp_name);
1995
1996 Py_DECREF(next_iter);
1997 goto error;
1998 } else {
1999 Py_DECREF(next_iter);
2000 }
2001 }
Yury Selivanov75445082015-05-11 22:57:16 -04002002
2003 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002004 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002005 DISPATCH();
2006 }
2007
Benjamin Petersonddd19492018-09-16 22:38:02 -07002008 case TARGET(GET_AWAITABLE): {
2009 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002010 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002011 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002012
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002013 if (iter == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002014 format_awaitable_error(tstate, Py_TYPE(iterable),
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002015 _Py_OPCODE(next_instr[-2]));
2016 }
2017
Yury Selivanov75445082015-05-11 22:57:16 -04002018 Py_DECREF(iterable);
2019
Yury Selivanovc724bae2016-03-02 11:30:46 -05002020 if (iter != NULL && PyCoro_CheckExact(iter)) {
2021 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2022 if (yf != NULL) {
2023 /* `iter` is a coroutine object that is being
2024 awaited, `yf` is a pointer to the current awaitable
2025 being awaited on. */
2026 Py_DECREF(yf);
2027 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002028 _PyErr_SetString(tstate, PyExc_RuntimeError,
2029 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002030 /* The code below jumps to `error` if `iter` is NULL. */
2031 }
2032 }
2033
Yury Selivanov75445082015-05-11 22:57:16 -04002034 SET_TOP(iter); /* Even if it's NULL */
2035
2036 if (iter == NULL) {
2037 goto error;
2038 }
2039
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002040 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002041 DISPATCH();
2042 }
2043
Benjamin Petersonddd19492018-09-16 22:38:02 -07002044 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002045 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002046 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002047 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002048 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
2049 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002050 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002051 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002052 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002053 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002054 else
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02002055 retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002056 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002057 Py_DECREF(v);
2058 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002059 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002060 if (tstate->c_tracefunc != NULL
Victor Stinner438a12d2019-05-24 17:01:38 +02002061 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002062 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002063 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002064 if (err < 0)
2065 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002066 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002067 SET_TOP(val);
2068 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002069 }
Martin Panter95f53c12016-07-18 08:23:26 +00002070 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002071 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002072 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002073 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002074 f->f_lasti -= sizeof(_Py_CODEUNIT);
Pablo Galindof00828a2019-05-09 16:52:02 +01002075 goto exit_yielding;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002076 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002077
Benjamin Petersonddd19492018-09-16 22:38:02 -07002078 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002079 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002080
2081 if (co->co_flags & CO_ASYNC_GENERATOR) {
2082 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2083 Py_DECREF(retval);
2084 if (w == NULL) {
2085 retval = NULL;
2086 goto error;
2087 }
2088 retval = w;
2089 }
2090
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002091 f->f_stacktop = stack_pointer;
Pablo Galindof00828a2019-05-09 16:52:02 +01002092 goto exit_yielding;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002093 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002094
Benjamin Petersonddd19492018-09-16 22:38:02 -07002095 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002096 PyObject *type, *value, *traceback;
2097 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002098 PyTryBlock *b = PyFrame_BlockPop(f);
2099 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002100 _PyErr_SetString(tstate, PyExc_SystemError,
2101 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002102 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002103 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002104 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2105 STACK_LEVEL() <= (b)->b_level + 4);
2106 exc_info = tstate->exc_info;
2107 type = exc_info->exc_type;
2108 value = exc_info->exc_value;
2109 traceback = exc_info->exc_traceback;
2110 exc_info->exc_type = POP();
2111 exc_info->exc_value = POP();
2112 exc_info->exc_traceback = POP();
2113 Py_XDECREF(type);
2114 Py_XDECREF(value);
2115 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002116 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002117 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002118
Benjamin Petersonddd19492018-09-16 22:38:02 -07002119 case TARGET(POP_BLOCK): {
2120 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002121 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002122 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002123 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002124
Benjamin Petersonddd19492018-09-16 22:38:02 -07002125 case TARGET(POP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002126 /* If oparg is 0 at the top of the stack are 1 or 6 values:
2127 Either:
2128 - TOP = NULL or an integer
2129 or:
2130 - (TOP, SECOND, THIRD) = exc_info()
2131 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2132
2133 If oparg is 1 the value for 'return' was additionally pushed
2134 at the top of the stack.
2135 */
2136 PyObject *res = NULL;
2137 if (oparg) {
2138 res = POP();
2139 }
2140 PyObject *exc = POP();
2141 if (exc == NULL || PyLong_CheckExact(exc)) {
2142 Py_XDECREF(exc);
2143 }
2144 else {
2145 Py_DECREF(exc);
2146 Py_DECREF(POP());
2147 Py_DECREF(POP());
2148
2149 PyObject *type, *value, *traceback;
2150 _PyErr_StackItem *exc_info;
2151 PyTryBlock *b = PyFrame_BlockPop(f);
2152 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002153 _PyErr_SetString(tstate, PyExc_SystemError,
2154 "popped block is not an except handler");
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002155 Py_XDECREF(res);
2156 goto error;
2157 }
2158 assert(STACK_LEVEL() == (b)->b_level + 3);
2159 exc_info = tstate->exc_info;
2160 type = exc_info->exc_type;
2161 value = exc_info->exc_value;
2162 traceback = exc_info->exc_traceback;
2163 exc_info->exc_type = POP();
2164 exc_info->exc_value = POP();
2165 exc_info->exc_traceback = POP();
2166 Py_XDECREF(type);
2167 Py_XDECREF(value);
2168 Py_XDECREF(traceback);
2169 }
2170 if (oparg) {
2171 PUSH(res);
2172 }
2173 DISPATCH();
2174 }
2175
Benjamin Petersonddd19492018-09-16 22:38:02 -07002176 case TARGET(CALL_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002177 PyObject *ret = PyLong_FromLong(INSTR_OFFSET());
2178 if (ret == NULL) {
2179 goto error;
2180 }
2181 PUSH(ret);
2182 JUMPBY(oparg);
2183 FAST_DISPATCH();
2184 }
2185
Benjamin Petersonddd19492018-09-16 22:38:02 -07002186 case TARGET(BEGIN_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002187 /* Push NULL onto the stack for using it in END_FINALLY,
2188 POP_FINALLY, WITH_CLEANUP_START and WITH_CLEANUP_FINISH.
2189 */
2190 PUSH(NULL);
2191 FAST_DISPATCH();
2192 }
2193
Benjamin Petersonddd19492018-09-16 22:38:02 -07002194 case TARGET(END_FINALLY): {
2195 PREDICTED(END_FINALLY);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002196 /* At the top of the stack are 1 or 6 values:
2197 Either:
2198 - TOP = NULL or an integer
2199 or:
2200 - (TOP, SECOND, THIRD) = exc_info()
2201 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2202 */
2203 PyObject *exc = POP();
2204 if (exc == NULL) {
2205 FAST_DISPATCH();
2206 }
2207 else if (PyLong_CheckExact(exc)) {
2208 int ret = _PyLong_AsInt(exc);
2209 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02002210 if (ret == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002211 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002212 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002213 JUMPTO(ret);
2214 FAST_DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002215 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002216 else {
2217 assert(PyExceptionClass_Check(exc));
2218 PyObject *val = POP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002219 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002220 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002221 goto exception_unwind;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002222 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002223 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002224
Benjamin Petersonddd19492018-09-16 22:38:02 -07002225 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002226 PyObject *exc = POP();
2227 assert(PyExceptionClass_Check(exc));
2228 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2229 PyTryBlock *b = PyFrame_BlockPop(f);
2230 assert(b->b_type == EXCEPT_HANDLER);
2231 Py_DECREF(exc);
2232 UNWIND_EXCEPT_HANDLER(b);
2233 Py_DECREF(POP());
2234 JUMPBY(oparg);
2235 FAST_DISPATCH();
2236 }
2237 else {
2238 PyObject *val = POP();
2239 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002240 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002241 goto exception_unwind;
2242 }
2243 }
2244
Zackery Spytzce6a0702019-08-25 03:44:09 -06002245 case TARGET(LOAD_ASSERTION_ERROR): {
2246 PyObject *value = PyExc_AssertionError;
2247 Py_INCREF(value);
2248 PUSH(value);
2249 FAST_DISPATCH();
2250 }
2251
Benjamin Petersonddd19492018-09-16 22:38:02 -07002252 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002253 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002254
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002255 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002256 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002257 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002258 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002259 if (!_PyErr_Occurred(tstate)) {
2260 _PyErr_SetString(tstate, PyExc_NameError,
2261 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002262 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002263 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002264 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002265 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002266 }
2267 else {
2268 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2269 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002270 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002271 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2272 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002273 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2274 _PyErr_SetString(tstate, PyExc_NameError,
2275 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002276 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002277 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002279 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002280 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002281 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002282
Benjamin Petersonddd19492018-09-16 22:38:02 -07002283 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002284 PyObject *name = GETITEM(names, oparg);
2285 PyObject *v = POP();
2286 PyObject *ns = f->f_locals;
2287 int err;
2288 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002289 _PyErr_Format(tstate, PyExc_SystemError,
2290 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002291 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002292 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002293 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002294 if (PyDict_CheckExact(ns))
2295 err = PyDict_SetItem(ns, name, v);
2296 else
2297 err = PyObject_SetItem(ns, name, v);
2298 Py_DECREF(v);
2299 if (err != 0)
2300 goto error;
2301 DISPATCH();
2302 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002303
Benjamin Petersonddd19492018-09-16 22:38:02 -07002304 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002305 PyObject *name = GETITEM(names, oparg);
2306 PyObject *ns = f->f_locals;
2307 int err;
2308 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002309 _PyErr_Format(tstate, PyExc_SystemError,
2310 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002311 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002312 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002313 err = PyObject_DelItem(ns, name);
2314 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002315 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002316 NAME_ERROR_MSG,
2317 name);
2318 goto error;
2319 }
2320 DISPATCH();
2321 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002322
Benjamin Petersonddd19492018-09-16 22:38:02 -07002323 case TARGET(UNPACK_SEQUENCE): {
2324 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002325 PyObject *seq = POP(), *item, **items;
2326 if (PyTuple_CheckExact(seq) &&
2327 PyTuple_GET_SIZE(seq) == oparg) {
2328 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002329 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002330 item = items[oparg];
2331 Py_INCREF(item);
2332 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002333 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002334 } else if (PyList_CheckExact(seq) &&
2335 PyList_GET_SIZE(seq) == oparg) {
2336 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002337 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002338 item = items[oparg];
2339 Py_INCREF(item);
2340 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002341 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002342 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002343 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002344 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002345 } else {
2346 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002347 Py_DECREF(seq);
2348 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002349 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002350 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002351 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002352 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002353
Benjamin Petersonddd19492018-09-16 22:38:02 -07002354 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002355 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2356 PyObject *seq = POP();
2357
Victor Stinner438a12d2019-05-24 17:01:38 +02002358 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002359 stack_pointer + totalargs)) {
2360 stack_pointer += totalargs;
2361 } else {
2362 Py_DECREF(seq);
2363 goto error;
2364 }
2365 Py_DECREF(seq);
2366 DISPATCH();
2367 }
2368
Benjamin Petersonddd19492018-09-16 22:38:02 -07002369 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002370 PyObject *name = GETITEM(names, oparg);
2371 PyObject *owner = TOP();
2372 PyObject *v = SECOND();
2373 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002374 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002375 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002376 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002377 Py_DECREF(owner);
2378 if (err != 0)
2379 goto error;
2380 DISPATCH();
2381 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002382
Benjamin Petersonddd19492018-09-16 22:38:02 -07002383 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002384 PyObject *name = GETITEM(names, oparg);
2385 PyObject *owner = POP();
2386 int err;
2387 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2388 Py_DECREF(owner);
2389 if (err != 0)
2390 goto error;
2391 DISPATCH();
2392 }
2393
Benjamin Petersonddd19492018-09-16 22:38:02 -07002394 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002395 PyObject *name = GETITEM(names, oparg);
2396 PyObject *v = POP();
2397 int err;
2398 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002399 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002400 if (err != 0)
2401 goto error;
2402 DISPATCH();
2403 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002404
Benjamin Petersonddd19492018-09-16 22:38:02 -07002405 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002406 PyObject *name = GETITEM(names, oparg);
2407 int err;
2408 err = PyDict_DelItem(f->f_globals, name);
2409 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002410 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2411 format_exc_check_arg(tstate, PyExc_NameError,
2412 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002413 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002414 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002415 }
2416 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002417 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002418
Benjamin Petersonddd19492018-09-16 22:38:02 -07002419 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002420 PyObject *name = GETITEM(names, oparg);
2421 PyObject *locals = f->f_locals;
2422 PyObject *v;
2423 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002424 _PyErr_Format(tstate, PyExc_SystemError,
2425 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002426 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002427 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002428 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002429 v = PyDict_GetItemWithError(locals, name);
2430 if (v != NULL) {
2431 Py_INCREF(v);
2432 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002433 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002434 goto error;
2435 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002436 }
2437 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002438 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002439 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002440 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002441 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002442 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002443 }
2444 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002445 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002446 v = PyDict_GetItemWithError(f->f_globals, name);
2447 if (v != NULL) {
2448 Py_INCREF(v);
2449 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002450 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002451 goto error;
2452 }
2453 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002454 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002455 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002456 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002457 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002458 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002459 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002460 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002461 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002462 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002463 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002464 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002465 }
2466 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002467 v = PyObject_GetItem(f->f_builtins, name);
2468 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002469 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002470 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002471 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002472 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002473 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002474 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002475 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002476 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002477 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002478 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002479 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002480 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002481 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002482
Benjamin Petersonddd19492018-09-16 22:38:02 -07002483 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002484 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002485 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002486 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002487 && PyDict_CheckExact(f->f_builtins))
2488 {
Inada Naoki91234a12019-06-03 21:30:58 +09002489 OPCACHE_CHECK();
2490 if (co_opcache != NULL && co_opcache->optimized > 0) {
2491 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2492
2493 if (lg->globals_ver ==
2494 ((PyDictObject *)f->f_globals)->ma_version_tag
2495 && lg->builtins_ver ==
2496 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2497 {
2498 PyObject *ptr = lg->ptr;
2499 OPCACHE_STAT_GLOBAL_HIT();
2500 assert(ptr != NULL);
2501 Py_INCREF(ptr);
2502 PUSH(ptr);
2503 DISPATCH();
2504 }
2505 }
2506
2507 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002508 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002509 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002510 name);
2511 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002512 if (!_PyErr_OCCURRED()) {
2513 /* _PyDict_LoadGlobal() returns NULL without raising
2514 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002515 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002516 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002517 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002518 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002519 }
Inada Naoki91234a12019-06-03 21:30:58 +09002520
2521 if (co_opcache != NULL) {
2522 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2523
2524 if (co_opcache->optimized == 0) {
2525 /* Wasn't optimized before. */
2526 OPCACHE_STAT_GLOBAL_OPT();
2527 } else {
2528 OPCACHE_STAT_GLOBAL_MISS();
2529 }
2530
2531 co_opcache->optimized = 1;
2532 lg->globals_ver =
2533 ((PyDictObject *)f->f_globals)->ma_version_tag;
2534 lg->builtins_ver =
2535 ((PyDictObject *)f->f_builtins)->ma_version_tag;
2536 lg->ptr = v; /* borrowed */
2537 }
2538
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002539 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002540 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002541 else {
2542 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002543
2544 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09002545 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002546 v = PyObject_GetItem(f->f_globals, name);
2547 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002548 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002549 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002550 }
2551 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002552
Victor Stinnerb4efc962015-11-20 09:24:02 +01002553 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002554 v = PyObject_GetItem(f->f_builtins, name);
2555 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002556 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002557 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002558 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002559 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002560 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002561 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002562 }
2563 }
2564 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002565 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002566 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002567 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002568
Benjamin Petersonddd19492018-09-16 22:38:02 -07002569 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002570 PyObject *v = GETLOCAL(oparg);
2571 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002572 SETLOCAL(oparg, NULL);
2573 DISPATCH();
2574 }
2575 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002576 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002577 UNBOUNDLOCAL_ERROR_MSG,
2578 PyTuple_GetItem(co->co_varnames, oparg)
2579 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002580 goto error;
2581 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002582
Benjamin Petersonddd19492018-09-16 22:38:02 -07002583 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002584 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002585 PyObject *oldobj = PyCell_GET(cell);
2586 if (oldobj != NULL) {
2587 PyCell_SET(cell, NULL);
2588 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002589 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002590 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002591 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002592 goto error;
2593 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002594
Benjamin Petersonddd19492018-09-16 22:38:02 -07002595 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002596 PyObject *cell = freevars[oparg];
2597 Py_INCREF(cell);
2598 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002599 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002600 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002601
Benjamin Petersonddd19492018-09-16 22:38:02 -07002602 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002603 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002604 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002605 assert(locals);
2606 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2607 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2608 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2609 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2610 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002611 value = PyDict_GetItemWithError(locals, name);
2612 if (value != NULL) {
2613 Py_INCREF(value);
2614 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002615 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002616 goto error;
2617 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002618 }
2619 else {
2620 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002621 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002622 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002623 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002624 }
2625 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002626 }
2627 }
2628 if (!value) {
2629 PyObject *cell = freevars[oparg];
2630 value = PyCell_GET(cell);
2631 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002632 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002633 goto error;
2634 }
2635 Py_INCREF(value);
2636 }
2637 PUSH(value);
2638 DISPATCH();
2639 }
2640
Benjamin Petersonddd19492018-09-16 22:38:02 -07002641 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002642 PyObject *cell = freevars[oparg];
2643 PyObject *value = PyCell_GET(cell);
2644 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002645 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002646 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002647 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002648 Py_INCREF(value);
2649 PUSH(value);
2650 DISPATCH();
2651 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002652
Benjamin Petersonddd19492018-09-16 22:38:02 -07002653 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002654 PyObject *v = POP();
2655 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002656 PyObject *oldobj = PyCell_GET(cell);
2657 PyCell_SET(cell, v);
2658 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002659 DISPATCH();
2660 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002661
Benjamin Petersonddd19492018-09-16 22:38:02 -07002662 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002663 PyObject *str;
2664 PyObject *empty = PyUnicode_New(0, 0);
2665 if (empty == NULL) {
2666 goto error;
2667 }
2668 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2669 Py_DECREF(empty);
2670 if (str == NULL)
2671 goto error;
2672 while (--oparg >= 0) {
2673 PyObject *item = POP();
2674 Py_DECREF(item);
2675 }
2676 PUSH(str);
2677 DISPATCH();
2678 }
2679
Benjamin Petersonddd19492018-09-16 22:38:02 -07002680 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002681 PyObject *tup = PyTuple_New(oparg);
2682 if (tup == NULL)
2683 goto error;
2684 while (--oparg >= 0) {
2685 PyObject *item = POP();
2686 PyTuple_SET_ITEM(tup, oparg, item);
2687 }
2688 PUSH(tup);
2689 DISPATCH();
2690 }
2691
Benjamin Petersonddd19492018-09-16 22:38:02 -07002692 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002693 PyObject *list = PyList_New(oparg);
2694 if (list == NULL)
2695 goto error;
2696 while (--oparg >= 0) {
2697 PyObject *item = POP();
2698 PyList_SET_ITEM(list, oparg, item);
2699 }
2700 PUSH(list);
2701 DISPATCH();
2702 }
2703
Benjamin Petersonddd19492018-09-16 22:38:02 -07002704 case TARGET(BUILD_TUPLE_UNPACK_WITH_CALL):
2705 case TARGET(BUILD_TUPLE_UNPACK):
2706 case TARGET(BUILD_LIST_UNPACK): {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002707 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002708 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002709 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002710 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002711
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002712 if (sum == NULL)
2713 goto error;
2714
2715 for (i = oparg; i > 0; i--) {
2716 PyObject *none_val;
2717
2718 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2719 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002720 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
Victor Stinner438a12d2019-05-24 17:01:38 +02002721 _PyErr_ExceptionMatches(tstate, PyExc_TypeError))
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002722 {
Victor Stinner438a12d2019-05-24 17:01:38 +02002723 check_args_iterable(tstate, PEEK(1 + oparg), PEEK(i));
Serhiy Storchaka73442852016-10-02 10:33:46 +03002724 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002725 Py_DECREF(sum);
2726 goto error;
2727 }
2728 Py_DECREF(none_val);
2729 }
2730
2731 if (convert_to_tuple) {
2732 return_value = PyList_AsTuple(sum);
2733 Py_DECREF(sum);
2734 if (return_value == NULL)
2735 goto error;
2736 }
2737 else {
2738 return_value = sum;
2739 }
2740
2741 while (oparg--)
2742 Py_DECREF(POP());
2743 PUSH(return_value);
2744 DISPATCH();
2745 }
2746
Benjamin Petersonddd19492018-09-16 22:38:02 -07002747 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002748 PyObject *set = PySet_New(NULL);
2749 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002750 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002751 if (set == NULL)
2752 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002753 for (i = oparg; i > 0; i--) {
2754 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002755 if (err == 0)
2756 err = PySet_Add(set, item);
2757 Py_DECREF(item);
2758 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002759 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002760 if (err != 0) {
2761 Py_DECREF(set);
2762 goto error;
2763 }
2764 PUSH(set);
2765 DISPATCH();
2766 }
2767
Benjamin Petersonddd19492018-09-16 22:38:02 -07002768 case TARGET(BUILD_SET_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002769 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002770 PyObject *sum = PySet_New(NULL);
2771 if (sum == NULL)
2772 goto error;
2773
2774 for (i = oparg; i > 0; i--) {
2775 if (_PySet_Update(sum, PEEK(i)) < 0) {
2776 Py_DECREF(sum);
2777 goto error;
2778 }
2779 }
2780
2781 while (oparg--)
2782 Py_DECREF(POP());
2783 PUSH(sum);
2784 DISPATCH();
2785 }
2786
Benjamin Petersonddd19492018-09-16 22:38:02 -07002787 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002788 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002789 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2790 if (map == NULL)
2791 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002792 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002793 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002794 PyObject *key = PEEK(2*i);
2795 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002796 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002797 if (err != 0) {
2798 Py_DECREF(map);
2799 goto error;
2800 }
2801 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002802
2803 while (oparg--) {
2804 Py_DECREF(POP());
2805 Py_DECREF(POP());
2806 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002807 PUSH(map);
2808 DISPATCH();
2809 }
2810
Benjamin Petersonddd19492018-09-16 22:38:02 -07002811 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002812 _Py_IDENTIFIER(__annotations__);
2813 int err;
2814 PyObject *ann_dict;
2815 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002816 _PyErr_Format(tstate, PyExc_SystemError,
2817 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002818 goto error;
2819 }
2820 /* check if __annotations__ in locals()... */
2821 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002822 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002823 &PyId___annotations__);
2824 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002825 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002826 goto error;
2827 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002828 /* ...if not, create a new one */
2829 ann_dict = PyDict_New();
2830 if (ann_dict == NULL) {
2831 goto error;
2832 }
2833 err = _PyDict_SetItemId(f->f_locals,
2834 &PyId___annotations__, ann_dict);
2835 Py_DECREF(ann_dict);
2836 if (err != 0) {
2837 goto error;
2838 }
2839 }
2840 }
2841 else {
2842 /* do the same if locals() is not a dict */
2843 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2844 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002845 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002846 }
2847 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2848 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002849 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002850 goto error;
2851 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002852 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002853 ann_dict = PyDict_New();
2854 if (ann_dict == NULL) {
2855 goto error;
2856 }
2857 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2858 Py_DECREF(ann_dict);
2859 if (err != 0) {
2860 goto error;
2861 }
2862 }
2863 else {
2864 Py_DECREF(ann_dict);
2865 }
2866 }
2867 DISPATCH();
2868 }
2869
Benjamin Petersonddd19492018-09-16 22:38:02 -07002870 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002871 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002872 PyObject *map;
2873 PyObject *keys = TOP();
2874 if (!PyTuple_CheckExact(keys) ||
2875 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002876 _PyErr_SetString(tstate, PyExc_SystemError,
2877 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002878 goto error;
2879 }
2880 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2881 if (map == NULL) {
2882 goto error;
2883 }
2884 for (i = oparg; i > 0; i--) {
2885 int err;
2886 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2887 PyObject *value = PEEK(i + 1);
2888 err = PyDict_SetItem(map, key, value);
2889 if (err != 0) {
2890 Py_DECREF(map);
2891 goto error;
2892 }
2893 }
2894
2895 Py_DECREF(POP());
2896 while (oparg--) {
2897 Py_DECREF(POP());
2898 }
2899 PUSH(map);
2900 DISPATCH();
2901 }
2902
Benjamin Petersonddd19492018-09-16 22:38:02 -07002903 case TARGET(BUILD_MAP_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002904 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002905 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002906 if (sum == NULL)
2907 goto error;
2908
2909 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002910 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002911 if (PyDict_Update(sum, arg) < 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002912 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
2913 _PyErr_Format(tstate, PyExc_TypeError,
2914 "'%.200s' object is not a mapping",
2915 arg->ob_type->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002916 }
2917 Py_DECREF(sum);
2918 goto error;
2919 }
2920 }
2921
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002922 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002923 Py_DECREF(POP());
2924 PUSH(sum);
2925 DISPATCH();
2926 }
2927
Benjamin Petersonddd19492018-09-16 22:38:02 -07002928 case TARGET(BUILD_MAP_UNPACK_WITH_CALL): {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002929 Py_ssize_t i;
2930 PyObject *sum = PyDict_New();
2931 if (sum == NULL)
2932 goto error;
2933
2934 for (i = oparg; i > 0; i--) {
2935 PyObject *arg = PEEK(i);
2936 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002937 Py_DECREF(sum);
Victor Stinner438a12d2019-05-24 17:01:38 +02002938 format_kwargs_error(tstate, PEEK(2 + oparg), arg);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002939 goto error;
2940 }
2941 }
2942
2943 while (oparg--)
2944 Py_DECREF(POP());
2945 PUSH(sum);
2946 DISPATCH();
2947 }
2948
Benjamin Petersonddd19492018-09-16 22:38:02 -07002949 case TARGET(MAP_ADD): {
Jörn Heisslerc8a35412019-06-22 16:40:55 +02002950 PyObject *value = TOP();
2951 PyObject *key = SECOND();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002952 PyObject *map;
2953 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002954 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002955 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002956 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002957 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002958 Py_DECREF(value);
2959 Py_DECREF(key);
2960 if (err != 0)
2961 goto error;
2962 PREDICT(JUMP_ABSOLUTE);
2963 DISPATCH();
2964 }
2965
Benjamin Petersonddd19492018-09-16 22:38:02 -07002966 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002967 PyObject *name = GETITEM(names, oparg);
2968 PyObject *owner = TOP();
2969 PyObject *res = PyObject_GetAttr(owner, name);
2970 Py_DECREF(owner);
2971 SET_TOP(res);
2972 if (res == NULL)
2973 goto error;
2974 DISPATCH();
2975 }
2976
Benjamin Petersonddd19492018-09-16 22:38:02 -07002977 case TARGET(COMPARE_OP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002978 PyObject *right = POP();
2979 PyObject *left = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002980 PyObject *res = cmp_outcome(tstate, oparg, left, right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002981 Py_DECREF(left);
2982 Py_DECREF(right);
2983 SET_TOP(res);
2984 if (res == NULL)
2985 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002986 PREDICT(POP_JUMP_IF_FALSE);
2987 PREDICT(POP_JUMP_IF_TRUE);
2988 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002989 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002990
Benjamin Petersonddd19492018-09-16 22:38:02 -07002991 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002992 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002993 PyObject *fromlist = POP();
2994 PyObject *level = TOP();
2995 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02002996 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002997 Py_DECREF(level);
2998 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002999 SET_TOP(res);
3000 if (res == NULL)
3001 goto error;
3002 DISPATCH();
3003 }
3004
Benjamin Petersonddd19492018-09-16 22:38:02 -07003005 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003006 PyObject *from = POP(), *locals;
3007 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003008 if (PyFrame_FastToLocalsWithError(f) < 0) {
3009 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01003010 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003011 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01003012
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003013 locals = f->f_locals;
3014 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003015 _PyErr_SetString(tstate, PyExc_SystemError,
3016 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003017 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003018 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003019 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003020 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003021 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003022 Py_DECREF(from);
3023 if (err != 0)
3024 goto error;
3025 DISPATCH();
3026 }
Guido van Rossum25831651993-05-19 14:50:45 +00003027
Benjamin Petersonddd19492018-09-16 22:38:02 -07003028 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003029 PyObject *name = GETITEM(names, oparg);
3030 PyObject *from = TOP();
3031 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003032 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003033 PUSH(res);
3034 if (res == NULL)
3035 goto error;
3036 DISPATCH();
3037 }
Thomas Wouters52152252000-08-17 22:55:00 +00003038
Benjamin Petersonddd19492018-09-16 22:38:02 -07003039 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003040 JUMPBY(oparg);
3041 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003042 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003043
Benjamin Petersonddd19492018-09-16 22:38:02 -07003044 case TARGET(POP_JUMP_IF_FALSE): {
3045 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003046 PyObject *cond = POP();
3047 int err;
3048 if (cond == Py_True) {
3049 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003050 FAST_DISPATCH();
3051 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003052 if (cond == Py_False) {
3053 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003054 JUMPTO(oparg);
3055 FAST_DISPATCH();
3056 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003057 err = PyObject_IsTrue(cond);
3058 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003059 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003060 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003061 else if (err == 0)
3062 JUMPTO(oparg);
3063 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003064 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003065 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003066 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003067
Benjamin Petersonddd19492018-09-16 22:38:02 -07003068 case TARGET(POP_JUMP_IF_TRUE): {
3069 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003070 PyObject *cond = POP();
3071 int err;
3072 if (cond == Py_False) {
3073 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003074 FAST_DISPATCH();
3075 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003076 if (cond == Py_True) {
3077 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003078 JUMPTO(oparg);
3079 FAST_DISPATCH();
3080 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003081 err = PyObject_IsTrue(cond);
3082 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003083 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003084 JUMPTO(oparg);
3085 }
3086 else if (err == 0)
3087 ;
3088 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003089 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003090 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003091 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003092
Benjamin Petersonddd19492018-09-16 22:38:02 -07003093 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003094 PyObject *cond = TOP();
3095 int err;
3096 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003097 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003098 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003099 FAST_DISPATCH();
3100 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003101 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003102 JUMPTO(oparg);
3103 FAST_DISPATCH();
3104 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003105 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003106 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003107 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003108 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003109 }
3110 else if (err == 0)
3111 JUMPTO(oparg);
3112 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003113 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003114 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003115 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003116
Benjamin Petersonddd19492018-09-16 22:38:02 -07003117 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003118 PyObject *cond = TOP();
3119 int err;
3120 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003121 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003122 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003123 FAST_DISPATCH();
3124 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003125 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003126 JUMPTO(oparg);
3127 FAST_DISPATCH();
3128 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003129 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003130 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003131 JUMPTO(oparg);
3132 }
3133 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003134 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003135 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003136 }
3137 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003138 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003139 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003140 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003141
Benjamin Petersonddd19492018-09-16 22:38:02 -07003142 case TARGET(JUMP_ABSOLUTE): {
3143 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003144 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003145#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003146 /* Enabling this path speeds-up all while and for-loops by bypassing
3147 the per-loop checks for signals. By default, this should be turned-off
3148 because it prevents detection of a control-break in tight loops like
3149 "while 1: pass". Compile with this option turned-on when you need
3150 the speed-up and do not need break checking inside tight loops (ones
3151 that contain only instructions ending with FAST_DISPATCH).
3152 */
3153 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003154#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003155 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003156#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003157 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003158
Benjamin Petersonddd19492018-09-16 22:38:02 -07003159 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003160 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003161 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003162 PyObject *iter = PyObject_GetIter(iterable);
3163 Py_DECREF(iterable);
3164 SET_TOP(iter);
3165 if (iter == NULL)
3166 goto error;
3167 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003168 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003169 DISPATCH();
3170 }
3171
Benjamin Petersonddd19492018-09-16 22:38:02 -07003172 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04003173 /* before: [obj]; after [getiter(obj)] */
3174 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003175 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003176 if (PyCoro_CheckExact(iterable)) {
3177 /* `iterable` is a coroutine */
3178 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3179 /* and it is used in a 'yield from' expression of a
3180 regular generator. */
3181 Py_DECREF(iterable);
3182 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003183 _PyErr_SetString(tstate, PyExc_TypeError,
3184 "cannot 'yield from' a coroutine object "
3185 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04003186 goto error;
3187 }
3188 }
3189 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003190 /* `iterable` is not a generator. */
3191 iter = PyObject_GetIter(iterable);
3192 Py_DECREF(iterable);
3193 SET_TOP(iter);
3194 if (iter == NULL)
3195 goto error;
3196 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003197 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003198 DISPATCH();
3199 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003200
Benjamin Petersonddd19492018-09-16 22:38:02 -07003201 case TARGET(FOR_ITER): {
3202 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003203 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003204 PyObject *iter = TOP();
3205 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
3206 if (next != NULL) {
3207 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003208 PREDICT(STORE_FAST);
3209 PREDICT(UNPACK_SEQUENCE);
3210 DISPATCH();
3211 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003212 if (_PyErr_Occurred(tstate)) {
3213 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003214 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003215 }
3216 else if (tstate->c_tracefunc != NULL) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003217 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Victor Stinner438a12d2019-05-24 17:01:38 +02003218 }
3219 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003220 }
3221 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003222 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003223 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003224 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003225 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003226 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003227 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003228
Benjamin Petersonddd19492018-09-16 22:38:02 -07003229 case TARGET(SETUP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003230 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003231 STACK_LEVEL());
3232 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003233 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003234
Benjamin Petersonddd19492018-09-16 22:38:02 -07003235 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003236 _Py_IDENTIFIER(__aexit__);
3237 _Py_IDENTIFIER(__aenter__);
3238
3239 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003240 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__),
Yury Selivanov75445082015-05-11 22:57:16 -04003241 *enter;
3242 PyObject *res;
3243 if (exit == NULL)
3244 goto error;
3245 SET_TOP(exit);
Victor Stinner438a12d2019-05-24 17:01:38 +02003246 enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04003247 Py_DECREF(mgr);
3248 if (enter == NULL)
3249 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003250 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003251 Py_DECREF(enter);
3252 if (res == NULL)
3253 goto error;
3254 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003255 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003256 DISPATCH();
3257 }
3258
Benjamin Petersonddd19492018-09-16 22:38:02 -07003259 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003260 PyObject *res = POP();
3261 /* Setup the finally block before pushing the result
3262 of __aenter__ on the stack. */
3263 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3264 STACK_LEVEL());
3265 PUSH(res);
3266 DISPATCH();
3267 }
3268
Benjamin Petersonddd19492018-09-16 22:38:02 -07003269 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003270 _Py_IDENTIFIER(__exit__);
3271 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003272 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003273 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003274 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003275 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08003276 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003277 }
3278 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003279 if (exit == NULL) {
3280 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003281 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003282 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003283 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003284 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003285 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003286 Py_DECREF(enter);
3287 if (res == NULL)
3288 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003289 /* Setup the finally block before pushing the result
3290 of __enter__ on the stack. */
3291 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3292 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003293
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003294 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003295 DISPATCH();
3296 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003297
Benjamin Petersonddd19492018-09-16 22:38:02 -07003298 case TARGET(WITH_CLEANUP_START): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003299 /* At the top of the stack are 1 or 6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003300 how/why we entered the finally clause:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003301 - TOP = NULL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003302 - (TOP, SECOND, THIRD) = exc_info()
3303 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003304 Below them is EXIT, the context.__exit__ or context.__aexit__
3305 bound method.
3306 In the first case, we must call
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003307 EXIT(None, None, None)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003308 otherwise we must call
3309 EXIT(TOP, SECOND, THIRD)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003310
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003311 In the first case, we remove EXIT from the
3312 stack, leaving TOP, and push TOP on the stack.
3313 Otherwise we shift the bottom 3 values of the
3314 stack down, replace the empty spot with NULL, and push
3315 None on the stack.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003316
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003317 Finally we push the result of the call.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003318 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003319 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003320 PyObject *exc, *val, *tb, *res;
3321
3322 val = tb = Py_None;
3323 exc = TOP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003324 if (exc == NULL) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003325 STACK_SHRINK(1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003326 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003327 SET_TOP(exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003328 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003329 }
3330 else {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003331 assert(PyExceptionClass_Check(exc));
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003332 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003333 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003334 val = SECOND();
3335 tb = THIRD();
3336 tp2 = FOURTH();
3337 exc2 = PEEK(5);
3338 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003339 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003340 SET_VALUE(7, tb2);
3341 SET_VALUE(6, exc2);
3342 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003343 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3344 SET_FOURTH(NULL);
3345 /* We just shifted the stack down, so we have
3346 to tell the except handler block that the
3347 values are lower than it expects. */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003348 assert(f->f_iblock > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003349 block = &f->f_blockstack[f->f_iblock - 1];
3350 assert(block->b_type == EXCEPT_HANDLER);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003351 assert(block->b_level > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003352 block->b_level--;
3353 }
Victor Stinner842cfff2016-12-01 14:45:31 +01003354
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003355 PyObject *stack[4] = {NULL, exc, val, tb};
3356 res = _PyObject_Vectorcall(exit_func, stack + 1,
3357 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003358 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003359 if (res == NULL)
3360 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003361
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003362 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003363 PUSH(exc);
3364 PUSH(res);
3365 PREDICT(WITH_CLEANUP_FINISH);
3366 DISPATCH();
3367 }
3368
Benjamin Petersonddd19492018-09-16 22:38:02 -07003369 case TARGET(WITH_CLEANUP_FINISH): {
3370 PREDICTED(WITH_CLEANUP_FINISH);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003371 /* TOP = the result of calling the context.__exit__ bound method
3372 SECOND = either None or exception type
3373
3374 If SECOND is None below is NULL or the return address,
3375 otherwise below are 7 values representing an exception.
3376 */
Yury Selivanov75445082015-05-11 22:57:16 -04003377 PyObject *res = POP();
3378 PyObject *exc = POP();
3379 int err;
3380
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003381 if (exc != Py_None)
3382 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003383 else
3384 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003385
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003386 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003387 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003389 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003390 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003391 else if (err > 0) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003392 /* There was an exception and a True return.
3393 * We must manually unwind the EXCEPT_HANDLER block
3394 * which was created when the exception was caught,
Quan Tian3bd0d622018-10-20 05:30:03 +08003395 * otherwise the stack will be in an inconsistent state.
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003396 */
3397 PyTryBlock *b = PyFrame_BlockPop(f);
3398 assert(b->b_type == EXCEPT_HANDLER);
3399 UNWIND_EXCEPT_HANDLER(b);
3400 PUSH(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003401 }
3402 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003403 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003404 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003405
Benjamin Petersonddd19492018-09-16 22:38:02 -07003406 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10003407 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003408 PyObject *name = GETITEM(names, oparg);
3409 PyObject *obj = TOP();
3410 PyObject *meth = NULL;
3411
3412 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3413
Yury Selivanovf2392132016-12-13 19:03:51 -05003414 if (meth == NULL) {
3415 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003416 goto error;
3417 }
3418
3419 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003420 /* We can bypass temporary bound method object.
3421 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003422
INADA Naoki015bce62017-01-16 17:23:30 +09003423 meth | self | arg1 | ... | argN
3424 */
3425 SET_TOP(meth);
3426 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003427 }
3428 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003429 /* meth is not an unbound method (but a regular attr, or
3430 something was returned by a descriptor protocol). Set
3431 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003432 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003433
3434 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003435 */
INADA Naoki015bce62017-01-16 17:23:30 +09003436 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003437 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003438 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003439 }
3440 DISPATCH();
3441 }
3442
Benjamin Petersonddd19492018-09-16 22:38:02 -07003443 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003444 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003445 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003446
3447 sp = stack_pointer;
3448
INADA Naoki015bce62017-01-16 17:23:30 +09003449 meth = PEEK(oparg + 2);
3450 if (meth == NULL) {
3451 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3452 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003453
3454 Stack layout:
3455
INADA Naoki015bce62017-01-16 17:23:30 +09003456 ... | NULL | callable | arg1 | ... | argN
3457 ^- TOP()
3458 ^- (-oparg)
3459 ^- (-oparg-1)
3460 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003461
Ville Skyttä49b27342017-08-03 09:00:59 +03003462 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003463 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003464 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003465 res = call_function(tstate, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003466 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003467 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003468 }
3469 else {
3470 /* This is a method call. Stack layout:
3471
INADA Naoki015bce62017-01-16 17:23:30 +09003472 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003473 ^- TOP()
3474 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003475 ^- (-oparg-1)
3476 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003477
INADA Naoki015bce62017-01-16 17:23:30 +09003478 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003479 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003480 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003481 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003482 res = call_function(tstate, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003483 stack_pointer = sp;
3484 }
3485
3486 PUSH(res);
3487 if (res == NULL)
3488 goto error;
3489 DISPATCH();
3490 }
3491
Benjamin Petersonddd19492018-09-16 22:38:02 -07003492 case TARGET(CALL_FUNCTION): {
3493 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003494 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003495 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003496 res = call_function(tstate, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003497 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003498 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003499 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003500 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003501 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003502 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003503 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003504
Benjamin Petersonddd19492018-09-16 22:38:02 -07003505 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003506 PyObject **sp, *res, *names;
3507
3508 names = POP();
Jeroen Demeyer05677862019-08-16 12:41:27 +02003509 assert(PyTuple_Check(names));
3510 assert(PyTuple_GET_SIZE(names) <= oparg);
3511 /* We assume without checking that names contains only strings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003512 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003513 res = call_function(tstate, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003514 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003515 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003516 Py_DECREF(names);
3517
3518 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003519 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003520 }
3521 DISPATCH();
3522 }
3523
Benjamin Petersonddd19492018-09-16 22:38:02 -07003524 case TARGET(CALL_FUNCTION_EX): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003525 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003526 if (oparg & 0x01) {
3527 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003528 if (!PyDict_CheckExact(kwargs)) {
3529 PyObject *d = PyDict_New();
3530 if (d == NULL)
3531 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003532 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003533 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02003534 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003535 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003536 goto error;
3537 }
3538 Py_DECREF(kwargs);
3539 kwargs = d;
3540 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003541 assert(PyDict_CheckExact(kwargs));
3542 }
3543 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003544 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003545 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003546 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003547 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003548 goto error;
3549 }
3550 Py_SETREF(callargs, PySequence_Tuple(callargs));
3551 if (callargs == NULL) {
3552 goto error;
3553 }
3554 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003555 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003556
Victor Stinner09532fe2019-05-10 23:39:09 +02003557 result = do_call_core(tstate, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003558 Py_DECREF(func);
3559 Py_DECREF(callargs);
3560 Py_XDECREF(kwargs);
3561
3562 SET_TOP(result);
3563 if (result == NULL) {
3564 goto error;
3565 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003566 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003567 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003568
Benjamin Petersonddd19492018-09-16 22:38:02 -07003569 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003570 PyObject *qualname = POP();
3571 PyObject *codeobj = POP();
3572 PyFunctionObject *func = (PyFunctionObject *)
3573 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003574
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003575 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003576 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003577 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003578 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003579 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003580
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003581 if (oparg & 0x08) {
3582 assert(PyTuple_CheckExact(TOP()));
3583 func ->func_closure = POP();
3584 }
3585 if (oparg & 0x04) {
3586 assert(PyDict_CheckExact(TOP()));
3587 func->func_annotations = POP();
3588 }
3589 if (oparg & 0x02) {
3590 assert(PyDict_CheckExact(TOP()));
3591 func->func_kwdefaults = POP();
3592 }
3593 if (oparg & 0x01) {
3594 assert(PyTuple_CheckExact(TOP()));
3595 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003596 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003597
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003598 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003599 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003600 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003601
Benjamin Petersonddd19492018-09-16 22:38:02 -07003602 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003603 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003604 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003605 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003606 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003607 step = NULL;
3608 stop = POP();
3609 start = TOP();
3610 slice = PySlice_New(start, stop, step);
3611 Py_DECREF(start);
3612 Py_DECREF(stop);
3613 Py_XDECREF(step);
3614 SET_TOP(slice);
3615 if (slice == NULL)
3616 goto error;
3617 DISPATCH();
3618 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003619
Benjamin Petersonddd19492018-09-16 22:38:02 -07003620 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003621 /* Handles f-string value formatting. */
3622 PyObject *result;
3623 PyObject *fmt_spec;
3624 PyObject *value;
3625 PyObject *(*conv_fn)(PyObject *);
3626 int which_conversion = oparg & FVC_MASK;
3627 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3628
3629 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003630 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003631
3632 /* See if any conversion is specified. */
3633 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003634 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003635 case FVC_STR: conv_fn = PyObject_Str; break;
3636 case FVC_REPR: conv_fn = PyObject_Repr; break;
3637 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003638 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02003639 _PyErr_Format(tstate, PyExc_SystemError,
3640 "unexpected conversion flag %d",
3641 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003642 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003643 }
3644
3645 /* If there's a conversion function, call it and replace
3646 value with that result. Otherwise, just use value,
3647 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003648 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003649 result = conv_fn(value);
3650 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003651 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003652 Py_XDECREF(fmt_spec);
3653 goto error;
3654 }
3655 value = result;
3656 }
3657
3658 /* If value is a unicode object, and there's no fmt_spec,
3659 then we know the result of format(value) is value
3660 itself. In that case, skip calling format(). I plan to
3661 move this optimization in to PyObject_Format()
3662 itself. */
3663 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3664 /* Do nothing, just transfer ownership to result. */
3665 result = value;
3666 } else {
3667 /* Actually call format(). */
3668 result = PyObject_Format(value, fmt_spec);
3669 Py_DECREF(value);
3670 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003671 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003672 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003673 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003674 }
3675
Eric V. Smith135d5f42016-02-05 18:23:08 -05003676 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003677 DISPATCH();
3678 }
3679
Benjamin Petersonddd19492018-09-16 22:38:02 -07003680 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003681 int oldoparg = oparg;
3682 NEXTOPARG();
3683 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003684 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003685 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003686
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003687
Antoine Pitrou042b1282010-08-13 21:15:58 +00003688#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003689 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003690#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003691 default:
3692 fprintf(stderr,
3693 "XXX lineno: %d, opcode: %d\n",
3694 PyFrame_GetLineNumber(f),
3695 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02003696 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003697 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003699 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003700
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003701 /* This should never be reached. Every opcode should end with DISPATCH()
3702 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003703 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003704
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003705error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003706 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003707#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02003708 if (!_PyErr_Occurred(tstate)) {
3709 _PyErr_SetString(tstate, PyExc_SystemError,
3710 "error return without exception set");
3711 }
Victor Stinner365b6932013-07-12 00:11:58 +02003712#else
Victor Stinner438a12d2019-05-24 17:01:38 +02003713 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02003714#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003715
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003716 /* Log traceback info. */
3717 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003718
Benjamin Peterson51f46162013-01-23 08:38:47 -05003719 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003720 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3721 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003722
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003723exception_unwind:
3724 /* Unwind stacks if an exception occurred */
3725 while (f->f_iblock > 0) {
3726 /* Pop the current block. */
3727 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003729 if (b->b_type == EXCEPT_HANDLER) {
3730 UNWIND_EXCEPT_HANDLER(b);
3731 continue;
3732 }
3733 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003734 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003735 PyObject *exc, *val, *tb;
3736 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003737 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003738 /* Beware, this invalidates all b->b_* fields */
3739 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003740 PUSH(exc_info->exc_traceback);
3741 PUSH(exc_info->exc_value);
3742 if (exc_info->exc_type != NULL) {
3743 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003744 }
3745 else {
3746 Py_INCREF(Py_None);
3747 PUSH(Py_None);
3748 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003749 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003750 /* Make the raw exception data
3751 available to the handler,
3752 so a program can emulate the
3753 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02003754 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003755 if (tb != NULL)
3756 PyException_SetTraceback(val, tb);
3757 else
3758 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003759 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003760 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003761 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003762 exc_info->exc_value = val;
3763 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003764 if (tb == NULL)
3765 tb = Py_None;
3766 Py_INCREF(tb);
3767 PUSH(tb);
3768 PUSH(val);
3769 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003770 JUMPTO(handler);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003771 /* Resume normal execution */
3772 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003773 }
3774 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003775
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003776 /* End the loop as we still have an error */
3777 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003778 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003779
Pablo Galindof00828a2019-05-09 16:52:02 +01003780 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003781 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01003782
3783exit_returning:
3784
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003785 /* Pop remaining stack entries. */
3786 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003787 PyObject *o = POP();
3788 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003789 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003790
Pablo Galindof00828a2019-05-09 16:52:02 +01003791exit_yielding:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003792 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003793 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003794 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3795 tstate, f, PyTrace_RETURN, retval)) {
3796 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003797 }
3798 }
3799 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003800 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3801 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003802 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003803 }
3804 }
3805 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003806
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003807 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003808exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003809 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3810 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003811 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003812 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003813 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003814
Victor Stinnerefde1462015-03-21 15:04:43 +01003815 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003816}
3817
Benjamin Petersonb204a422011-06-05 22:04:07 -05003818static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003819format_missing(PyThreadState *tstate, const char *kind,
3820 PyCodeObject *co, PyObject *names)
Benjamin Petersone109c702011-06-24 09:37:26 -05003821{
3822 int err;
3823 Py_ssize_t len = PyList_GET_SIZE(names);
3824 PyObject *name_str, *comma, *tail, *tmp;
3825
3826 assert(PyList_CheckExact(names));
3827 assert(len >= 1);
3828 /* Deal with the joys of natural language. */
3829 switch (len) {
3830 case 1:
3831 name_str = PyList_GET_ITEM(names, 0);
3832 Py_INCREF(name_str);
3833 break;
3834 case 2:
3835 name_str = PyUnicode_FromFormat("%U and %U",
3836 PyList_GET_ITEM(names, len - 2),
3837 PyList_GET_ITEM(names, len - 1));
3838 break;
3839 default:
3840 tail = PyUnicode_FromFormat(", %U, and %U",
3841 PyList_GET_ITEM(names, len - 2),
3842 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003843 if (tail == NULL)
3844 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003845 /* Chop off the last two objects in the list. This shouldn't actually
3846 fail, but we can't be too careful. */
3847 err = PyList_SetSlice(names, len - 2, len, NULL);
3848 if (err == -1) {
3849 Py_DECREF(tail);
3850 return;
3851 }
3852 /* Stitch everything up into a nice comma-separated list. */
3853 comma = PyUnicode_FromString(", ");
3854 if (comma == NULL) {
3855 Py_DECREF(tail);
3856 return;
3857 }
3858 tmp = PyUnicode_Join(comma, names);
3859 Py_DECREF(comma);
3860 if (tmp == NULL) {
3861 Py_DECREF(tail);
3862 return;
3863 }
3864 name_str = PyUnicode_Concat(tmp, tail);
3865 Py_DECREF(tmp);
3866 Py_DECREF(tail);
3867 break;
3868 }
3869 if (name_str == NULL)
3870 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02003871 _PyErr_Format(tstate, PyExc_TypeError,
3872 "%U() missing %i required %s argument%s: %U",
3873 co->co_name,
3874 len,
3875 kind,
3876 len == 1 ? "" : "s",
3877 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05003878 Py_DECREF(name_str);
3879}
3880
3881static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003882missing_arguments(PyThreadState *tstate, PyCodeObject *co,
3883 Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003884 PyObject **fastlocals)
3885{
Victor Stinner74319ae2016-08-25 00:04:09 +02003886 Py_ssize_t i, j = 0;
3887 Py_ssize_t start, end;
3888 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003889 const char *kind = positional ? "positional" : "keyword-only";
3890 PyObject *missing_names;
3891
3892 /* Compute the names of the arguments that are missing. */
3893 missing_names = PyList_New(missing);
3894 if (missing_names == NULL)
3895 return;
3896 if (positional) {
3897 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01003898 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003899 }
3900 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003901 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003902 end = start + co->co_kwonlyargcount;
3903 }
3904 for (i = start; i < end; i++) {
3905 if (GETLOCAL(i) == NULL) {
3906 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3907 PyObject *name = PyObject_Repr(raw);
3908 if (name == NULL) {
3909 Py_DECREF(missing_names);
3910 return;
3911 }
3912 PyList_SET_ITEM(missing_names, j++, name);
3913 }
3914 }
3915 assert(j == missing);
Victor Stinner438a12d2019-05-24 17:01:38 +02003916 format_missing(tstate, kind, co, missing_names);
Benjamin Petersone109c702011-06-24 09:37:26 -05003917 Py_DECREF(missing_names);
3918}
3919
3920static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003921too_many_positional(PyThreadState *tstate, PyCodeObject *co,
3922 Py_ssize_t given, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003923 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003924{
3925 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003926 Py_ssize_t kwonly_given = 0;
3927 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003928 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003929 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003930
Benjamin Petersone109c702011-06-24 09:37:26 -05003931 assert((co->co_flags & CO_VARARGS) == 0);
3932 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01003933 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003934 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003935 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003936 }
3937 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003938 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01003939 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003940 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01003941 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003942 }
3943 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003944 plural = (co_argcount != 1);
3945 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003946 }
3947 if (sig == NULL)
3948 return;
3949 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003950 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3951 kwonly_sig = PyUnicode_FromFormat(format,
3952 given != 1 ? "s" : "",
3953 kwonly_given,
3954 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003955 if (kwonly_sig == NULL) {
3956 Py_DECREF(sig);
3957 return;
3958 }
3959 }
3960 else {
3961 /* This will not fail. */
3962 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003963 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003964 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003965 _PyErr_Format(tstate, PyExc_TypeError,
3966 "%U() takes %U positional argument%s but %zd%U %s given",
3967 co->co_name,
3968 sig,
3969 plural ? "s" : "",
3970 given,
3971 kwonly_sig,
3972 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003973 Py_DECREF(sig);
3974 Py_DECREF(kwonly_sig);
3975}
3976
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003977static int
Victor Stinner438a12d2019-05-24 17:01:38 +02003978positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
3979 Py_ssize_t kwcount, PyObject* const* kwnames)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003980{
3981 int posonly_conflicts = 0;
3982 PyObject* posonly_names = PyList_New(0);
3983
3984 for(int k=0; k < co->co_posonlyargcount; k++){
3985 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
3986
3987 for (int k2=0; k2<kwcount; k2++){
3988 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
3989 PyObject* kwname = kwnames[k2];
3990 if (kwname == posonly_name){
3991 if(PyList_Append(posonly_names, kwname) != 0) {
3992 goto fail;
3993 }
3994 posonly_conflicts++;
3995 continue;
3996 }
3997
3998 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
3999
4000 if ( cmp > 0) {
4001 if(PyList_Append(posonly_names, kwname) != 0) {
4002 goto fail;
4003 }
4004 posonly_conflicts++;
4005 } else if (cmp < 0) {
4006 goto fail;
4007 }
4008
4009 }
4010 }
4011 if (posonly_conflicts) {
4012 PyObject* comma = PyUnicode_FromString(", ");
4013 if (comma == NULL) {
4014 goto fail;
4015 }
4016 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
4017 Py_DECREF(comma);
4018 if (error_names == NULL) {
4019 goto fail;
4020 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004021 _PyErr_Format(tstate, PyExc_TypeError,
4022 "%U() got some positional-only arguments passed"
4023 " as keyword arguments: '%U'",
4024 co->co_name, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004025 Py_DECREF(error_names);
4026 goto fail;
4027 }
4028
4029 Py_DECREF(posonly_names);
4030 return 0;
4031
4032fail:
4033 Py_XDECREF(posonly_names);
4034 return 1;
4035
4036}
4037
Guido van Rossumc2e20742006-02-27 22:32:47 +00004038/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02004039 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00004040 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00004041
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004042PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +02004043_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004044 PyObject *const *args, Py_ssize_t argcount,
4045 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03004046 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004047 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02004048 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004049 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00004050{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00004051 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004052 PyFrameObject *f;
4053 PyObject *retval = NULL;
4054 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004055 PyObject *x, *u;
Pablo Galindocd74e662019-06-01 18:08:04 +01004056 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004057 Py_ssize_t i, j, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02004058 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00004059
Victor Stinner438a12d2019-05-24 17:01:38 +02004060 PyThreadState *tstate = _PyThreadState_GET();
4061 assert(tstate != NULL);
4062
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004063 if (globals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004064 _PyErr_SetString(tstate, PyExc_SystemError,
4065 "PyEval_EvalCodeEx: NULL globals");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004066 return NULL;
4067 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004068
Victor Stinnerc7020012016-08-16 23:40:29 +02004069 /* Create the frame */
INADA Naoki5a625d02016-12-24 20:19:08 +09004070 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02004071 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004072 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02004073 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004074 fastlocals = f->f_localsplus;
4075 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00004076
Victor Stinnerc7020012016-08-16 23:40:29 +02004077 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004078 if (co->co_flags & CO_VARKEYWORDS) {
4079 kwdict = PyDict_New();
4080 if (kwdict == NULL)
4081 goto fail;
4082 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02004083 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004084 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02004085 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004086 SETLOCAL(i, kwdict);
4087 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004088 else {
4089 kwdict = NULL;
4090 }
4091
Pablo Galindocd74e662019-06-01 18:08:04 +01004092 /* Copy all positional arguments into local variables */
4093 if (argcount > co->co_argcount) {
4094 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02004095 }
4096 else {
4097 n = argcount;
4098 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004099 for (j = 0; j < n; j++) {
4100 x = args[j];
4101 Py_INCREF(x);
4102 SETLOCAL(j, x);
4103 }
4104
Victor Stinnerc7020012016-08-16 23:40:29 +02004105 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004106 if (co->co_flags & CO_VARARGS) {
Sergey Fedoseev234531b2019-02-25 21:59:12 +05004107 u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02004108 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004109 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004110 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004111 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004112 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004113
Serhiy Storchakab7281052016-09-12 00:52:40 +03004114 /* Handle keyword arguments passed as two strided arrays */
4115 kwcount *= kwstep;
4116 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004117 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03004118 PyObject *keyword = kwnames[i];
4119 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02004120 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004121
Benjamin Petersonb204a422011-06-05 22:04:07 -05004122 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004123 _PyErr_Format(tstate, PyExc_TypeError,
4124 "%U() keywords must be strings",
4125 co->co_name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004126 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004127 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004128
Benjamin Petersonb204a422011-06-05 22:04:07 -05004129 /* Speed hack: do raw pointer compares. As names are
4130 normally interned this should almost always hit. */
4131 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004132 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004133 PyObject *name = co_varnames[j];
4134 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004135 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004136 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004137 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004138
Benjamin Petersonb204a422011-06-05 22:04:07 -05004139 /* Slow fallback, just in case */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004140 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004141 PyObject *name = co_varnames[j];
4142 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
4143 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004144 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004145 }
4146 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004147 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004148 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004149 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004150
Victor Stinner231d1f32017-01-11 02:12:06 +01004151 assert(j >= total_args);
4152 if (kwdict == NULL) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004153
Victor Stinner438a12d2019-05-24 17:01:38 +02004154 if (co->co_posonlyargcount
4155 && positional_only_passed_as_keyword(tstate, co,
4156 kwcount, kwnames))
4157 {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004158 goto fail;
4159 }
4160
Victor Stinner438a12d2019-05-24 17:01:38 +02004161 _PyErr_Format(tstate, PyExc_TypeError,
4162 "%U() got an unexpected keyword argument '%S'",
4163 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004164 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004165 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004166
Christian Heimes0bd447f2013-07-20 14:48:10 +02004167 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4168 goto fail;
4169 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004170 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004171
Benjamin Petersonb204a422011-06-05 22:04:07 -05004172 kw_found:
4173 if (GETLOCAL(j) != NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004174 _PyErr_Format(tstate, PyExc_TypeError,
4175 "%U() got multiple values for argument '%S'",
4176 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004177 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004178 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004179 Py_INCREF(value);
4180 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004181 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004182
4183 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004184 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004185 too_many_positional(tstate, co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004186 goto fail;
4187 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004188
4189 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004190 if (argcount < co->co_argcount) {
4191 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004192 Py_ssize_t missing = 0;
4193 for (i = argcount; i < m; i++) {
4194 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004195 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004196 }
4197 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004198 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004199 missing_arguments(tstate, co, missing, defcount, fastlocals);
Benjamin Petersone109c702011-06-24 09:37:26 -05004200 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004201 }
4202 if (n > m)
4203 i = n - m;
4204 else
4205 i = 0;
4206 for (; i < defcount; i++) {
4207 if (GETLOCAL(m+i) == NULL) {
4208 PyObject *def = defs[i];
4209 Py_INCREF(def);
4210 SETLOCAL(m+i, def);
4211 }
4212 }
4213 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004214
4215 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004216 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004217 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004218 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004219 PyObject *name;
4220 if (GETLOCAL(i) != NULL)
4221 continue;
4222 name = PyTuple_GET_ITEM(co->co_varnames, i);
4223 if (kwdefs != NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004224 PyObject *def = PyDict_GetItemWithError(kwdefs, name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004225 if (def) {
4226 Py_INCREF(def);
4227 SETLOCAL(i, def);
4228 continue;
4229 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004230 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004231 goto fail;
4232 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004233 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004234 missing++;
4235 }
4236 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004237 missing_arguments(tstate, co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004238 goto fail;
4239 }
4240 }
4241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004242 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004243 vars into frame. */
4244 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004245 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004246 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004247 /* Possibly account for the cell variable being an argument. */
4248 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004249 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004250 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004251 /* Clear the local copy. */
4252 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004253 }
4254 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004255 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004256 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004257 if (c == NULL)
4258 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004259 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004260 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004261
4262 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004263 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4264 PyObject *o = PyTuple_GET_ITEM(closure, i);
4265 Py_INCREF(o);
4266 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004267 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004268
Yury Selivanoveb636452016-09-08 22:01:51 -07004269 /* Handle generator/coroutine/asynchronous generator */
4270 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004271 PyObject *gen;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004272 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004274 /* Don't need to keep the reference to f_back, it will be set
4275 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004276 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004278 /* Create a new generator that owns the ready to run frame
4279 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004280 if (is_coro) {
4281 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004282 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4283 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004284 } else {
4285 gen = PyGen_NewWithQualName(f, name, qualname);
4286 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004287 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004288 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004289 }
INADA Naoki9c157762016-12-26 18:52:46 +09004290
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004291 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004292
Yury Selivanov75445082015-05-11 22:57:16 -04004293 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004294 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004295
Victor Stinner59a73272016-12-09 18:51:13 +01004296 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004297
Thomas Woutersce272b62007-09-19 21:19:28 +00004298fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004300 /* decref'ing the frame can cause __del__ methods to get invoked,
4301 which can call back into Python. While we're done with the
4302 current Python frame (f), the associated C stack is still in use,
4303 so recursion_depth must be boosted for the duration.
4304 */
4305 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09004306 if (Py_REFCNT(f) > 1) {
4307 Py_DECREF(f);
4308 _PyObject_GC_TRACK(f);
4309 }
4310 else {
4311 ++tstate->recursion_depth;
4312 Py_DECREF(f);
4313 --tstate->recursion_depth;
4314 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004315 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004316}
4317
Victor Stinner40ee3012014-06-16 15:59:28 +02004318PyObject *
4319PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004320 PyObject *const *args, int argcount,
4321 PyObject *const *kws, int kwcount,
4322 PyObject *const *defs, int defcount,
4323 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004324{
4325 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004326 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004327 kws, kws != NULL ? kws + 1 : NULL,
4328 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004329 defs, defcount,
4330 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004331 NULL, NULL);
4332}
Tim Peters5ca576e2001-06-18 22:08:13 +00004333
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004334static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004335special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004336{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004337 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004338 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02004339 if (res == NULL && !_PyErr_Occurred(tstate)) {
4340 _PyErr_SetObject(tstate, PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004341 return NULL;
4342 }
4343 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004344}
4345
4346
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004347/* Logic for the raise statement (too complicated for inlining).
4348 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004349static int
Victor Stinner09532fe2019-05-10 23:39:09 +02004350do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004351{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004352 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004354 if (exc == NULL) {
4355 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01004356 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004357 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004358 type = exc_info->exc_type;
4359 value = exc_info->exc_value;
4360 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004361 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004362 _PyErr_SetString(tstate, PyExc_RuntimeError,
4363 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004364 return 0;
4365 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004366 Py_XINCREF(type);
4367 Py_XINCREF(value);
4368 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02004369 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004370 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004371 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004373 /* We support the following forms of raise:
4374 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004375 raise <instance>
4376 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004378 if (PyExceptionClass_Check(exc)) {
4379 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004380 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004381 if (value == NULL)
4382 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004383 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004384 _PyErr_Format(tstate, PyExc_TypeError,
4385 "calling %R should have returned an instance of "
4386 "BaseException, not %R",
4387 type, Py_TYPE(value));
4388 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004389 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004390 }
4391 else if (PyExceptionInstance_Check(exc)) {
4392 value = exc;
4393 type = PyExceptionInstance_Class(exc);
4394 Py_INCREF(type);
4395 }
4396 else {
4397 /* Not something you can raise. You get an exception
4398 anyway, just not what you specified :-) */
4399 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02004400 _PyErr_SetString(tstate, PyExc_TypeError,
4401 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004402 goto raise_error;
4403 }
Collin Winter828f04a2007-08-31 00:04:24 +00004404
Serhiy Storchakac0191582016-09-27 11:37:10 +03004405 assert(type != NULL);
4406 assert(value != NULL);
4407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004408 if (cause) {
4409 PyObject *fixed_cause;
4410 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004411 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004412 if (fixed_cause == NULL)
4413 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004414 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004415 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004416 else if (PyExceptionInstance_Check(cause)) {
4417 fixed_cause = cause;
4418 }
4419 else if (cause == Py_None) {
4420 Py_DECREF(cause);
4421 fixed_cause = NULL;
4422 }
4423 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004424 _PyErr_SetString(tstate, PyExc_TypeError,
4425 "exception causes must derive from "
4426 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004427 goto raise_error;
4428 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004429 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004430 }
Collin Winter828f04a2007-08-31 00:04:24 +00004431
Victor Stinner438a12d2019-05-24 17:01:38 +02004432 _PyErr_SetObject(tstate, type, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004433 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004434 Py_DECREF(value);
4435 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004436 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004437
4438raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004439 Py_XDECREF(value);
4440 Py_XDECREF(type);
4441 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004442 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004443}
4444
Tim Petersd6d010b2001-06-21 02:49:55 +00004445/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004446 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004447
Guido van Rossum0368b722007-05-11 16:50:42 +00004448 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4449 with a variable target.
4450*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004451
Barry Warsawe42b18f1997-08-25 22:13:04 +00004452static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004453unpack_iterable(PyThreadState *tstate, PyObject *v,
4454 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004455{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004456 int i = 0, j = 0;
4457 Py_ssize_t ll = 0;
4458 PyObject *it; /* iter(v) */
4459 PyObject *w;
4460 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004462 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004464 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004465 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004466 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004467 v->ob_type->tp_iter == NULL && !PySequence_Check(v))
4468 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004469 _PyErr_Format(tstate, PyExc_TypeError,
4470 "cannot unpack non-iterable %.200s object",
4471 v->ob_type->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004472 }
4473 return 0;
4474 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004476 for (; i < argcnt; i++) {
4477 w = PyIter_Next(it);
4478 if (w == NULL) {
4479 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004480 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04004481 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004482 _PyErr_Format(tstate, PyExc_ValueError,
4483 "not enough values to unpack "
4484 "(expected %d, got %d)",
4485 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004486 }
4487 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004488 _PyErr_Format(tstate, PyExc_ValueError,
4489 "not enough values to unpack "
4490 "(expected at least %d, got %d)",
4491 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004492 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004493 }
4494 goto Error;
4495 }
4496 *--sp = w;
4497 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004499 if (argcntafter == -1) {
4500 /* We better have exhausted the iterator now. */
4501 w = PyIter_Next(it);
4502 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004503 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004504 goto Error;
4505 Py_DECREF(it);
4506 return 1;
4507 }
4508 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02004509 _PyErr_Format(tstate, PyExc_ValueError,
4510 "too many values to unpack (expected %d)",
4511 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004512 goto Error;
4513 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004515 l = PySequence_List(it);
4516 if (l == NULL)
4517 goto Error;
4518 *--sp = l;
4519 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004521 ll = PyList_GET_SIZE(l);
4522 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004523 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04004524 "not enough values to unpack (expected at least %d, got %zd)",
4525 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004526 goto Error;
4527 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004528
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004529 /* Pop the "after-variable" args off the list. */
4530 for (j = argcntafter; j > 0; j--, i++) {
4531 *--sp = PyList_GET_ITEM(l, ll - j);
4532 }
4533 /* Resize the list. */
4534 Py_SIZE(l) = ll - argcntafter;
4535 Py_DECREF(it);
4536 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004537
Tim Petersd6d010b2001-06-21 02:49:55 +00004538Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004539 for (; i > 0; i--, sp++)
4540 Py_DECREF(*sp);
4541 Py_XDECREF(it);
4542 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004543}
4544
4545
Guido van Rossum96a42c81992-01-12 02:29:51 +00004546#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004547static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004548prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004549{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004550 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02004551 if (PyObject_Print(v, stdout, 0) != 0) {
4552 /* Don't know what else to do */
4553 _PyErr_Clear(tstate);
4554 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004555 printf("\n");
4556 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004557}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004558#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004559
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004560static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004561call_exc_trace(Py_tracefunc func, PyObject *self,
4562 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004563{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004564 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004565 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004566 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004567 if (value == NULL) {
4568 value = Py_None;
4569 Py_INCREF(value);
4570 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004571 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01004572 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004573 arg = PyTuple_Pack(3, type, value, traceback);
4574 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004575 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004576 return;
4577 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004578 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004579 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02004580 if (err == 0) {
4581 _PyErr_Restore(tstate, type, value, orig_traceback);
4582 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004583 else {
4584 Py_XDECREF(type);
4585 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004586 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004587 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004588}
4589
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004590static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004591call_trace_protected(Py_tracefunc func, PyObject *obj,
4592 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004593 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004594{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004595 PyObject *type, *value, *traceback;
4596 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004597 _PyErr_Fetch(tstate, &type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004598 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004599 if (err == 0)
4600 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004601 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004602 return 0;
4603 }
4604 else {
4605 Py_XDECREF(type);
4606 Py_XDECREF(value);
4607 Py_XDECREF(traceback);
4608 return -1;
4609 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004610}
4611
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004612static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004613call_trace(Py_tracefunc func, PyObject *obj,
4614 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004615 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004616{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004617 int result;
4618 if (tstate->tracing)
4619 return 0;
4620 tstate->tracing++;
4621 tstate->use_tracing = 0;
4622 result = func(obj, frame, what, arg);
4623 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4624 || (tstate->c_profilefunc != NULL));
4625 tstate->tracing--;
4626 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004627}
4628
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004629PyObject *
4630_PyEval_CallTracing(PyObject *func, PyObject *args)
4631{
Victor Stinner50b48572018-11-01 01:51:40 +01004632 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004633 int save_tracing = tstate->tracing;
4634 int save_use_tracing = tstate->use_tracing;
4635 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004637 tstate->tracing = 0;
4638 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4639 || (tstate->c_profilefunc != NULL));
4640 result = PyObject_Call(func, args, NULL);
4641 tstate->tracing = save_tracing;
4642 tstate->use_tracing = save_use_tracing;
4643 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004644}
4645
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004646/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004647static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004648maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004649 PyThreadState *tstate, PyFrameObject *frame,
4650 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004651{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004652 int result = 0;
4653 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004655 /* If the last instruction executed isn't in the current
4656 instruction window, reset the window.
4657 */
4658 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4659 PyAddrPair bounds;
4660 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4661 &bounds);
4662 *instr_lb = bounds.ap_lower;
4663 *instr_ub = bounds.ap_upper;
4664 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004665 /* If the last instruction falls at the start of a line or if it
4666 represents a jump backwards, update the frame's line number and
4667 then call the trace function if we're tracing source lines.
4668 */
4669 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004670 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004671 if (frame->f_trace_lines) {
4672 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4673 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004674 }
George King20faa682017-10-18 17:44:22 -07004675 /* Always emit an opcode event if we're tracing all opcodes. */
4676 if (frame->f_trace_opcodes) {
4677 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4678 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004679 *instr_prev = frame->f_lasti;
4680 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004681}
4682
Fred Drake5755ce62001-06-27 19:19:46 +00004683void
4684PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004685{
Steve Dowerb82e17e2019-05-23 08:45:22 -07004686 if (PySys_Audit("sys.setprofile", NULL) < 0) {
4687 return;
4688 }
4689
Victor Stinner50b48572018-11-01 01:51:40 +01004690 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004691 PyObject *temp = tstate->c_profileobj;
4692 Py_XINCREF(arg);
4693 tstate->c_profilefunc = NULL;
4694 tstate->c_profileobj = NULL;
4695 /* Must make sure that tracing is not ignored if 'temp' is freed */
4696 tstate->use_tracing = tstate->c_tracefunc != NULL;
4697 Py_XDECREF(temp);
4698 tstate->c_profilefunc = func;
4699 tstate->c_profileobj = arg;
4700 /* Flag that tracing or profiling is turned on */
4701 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004702}
4703
4704void
4705PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4706{
Steve Dowerb82e17e2019-05-23 08:45:22 -07004707 if (PySys_Audit("sys.settrace", NULL) < 0) {
4708 return;
4709 }
4710
Victor Stinner09532fe2019-05-10 23:39:09 +02004711 _PyRuntimeState *runtime = &_PyRuntime;
4712 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004713 PyObject *temp = tstate->c_traceobj;
Victor Stinner09532fe2019-05-10 23:39:09 +02004714 runtime->ceval.tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004715 Py_XINCREF(arg);
4716 tstate->c_tracefunc = NULL;
4717 tstate->c_traceobj = NULL;
4718 /* Must make sure that profiling is not ignored if 'temp' is freed */
4719 tstate->use_tracing = tstate->c_profilefunc != NULL;
4720 Py_XDECREF(temp);
4721 tstate->c_tracefunc = func;
4722 tstate->c_traceobj = arg;
4723 /* Flag that tracing or profiling is turned on */
4724 tstate->use_tracing = ((func != NULL)
4725 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004726}
4727
Yury Selivanov75445082015-05-11 22:57:16 -04004728void
Victor Stinner838f2642019-06-13 22:41:23 +02004729_PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004730{
4731 assert(new_depth >= 0);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004732 tstate->coroutine_origin_tracking_depth = new_depth;
4733}
4734
4735int
4736_PyEval_GetCoroutineOriginTrackingDepth(void)
4737{
Victor Stinner50b48572018-11-01 01:51:40 +01004738 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004739 return tstate->coroutine_origin_tracking_depth;
4740}
4741
4742void
Yury Selivanoveb636452016-09-08 22:01:51 -07004743_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4744{
Victor Stinner50b48572018-11-01 01:51:40 +01004745 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004746
4747 if (PySys_Audit("sys.set_asyncgen_hook_firstiter", NULL) < 0) {
4748 return;
4749 }
4750
Yury Selivanoveb636452016-09-08 22:01:51 -07004751 Py_XINCREF(firstiter);
4752 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4753}
4754
4755PyObject *
4756_PyEval_GetAsyncGenFirstiter(void)
4757{
Victor Stinner50b48572018-11-01 01:51:40 +01004758 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004759 return tstate->async_gen_firstiter;
4760}
4761
4762void
4763_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4764{
Victor Stinner50b48572018-11-01 01:51:40 +01004765 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004766
4767 if (PySys_Audit("sys.set_asyncgen_hook_finalizer", NULL) < 0) {
4768 return;
4769 }
4770
Yury Selivanoveb636452016-09-08 22:01:51 -07004771 Py_XINCREF(finalizer);
4772 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4773}
4774
4775PyObject *
4776_PyEval_GetAsyncGenFinalizer(void)
4777{
Victor Stinner50b48572018-11-01 01:51:40 +01004778 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004779 return tstate->async_gen_finalizer;
4780}
4781
Victor Stinner438a12d2019-05-24 17:01:38 +02004782static PyFrameObject *
4783_PyEval_GetFrame(PyThreadState *tstate)
4784{
4785 return _PyRuntime.gilstate.getframe(tstate);
4786}
4787
4788PyFrameObject *
4789PyEval_GetFrame(void)
4790{
4791 PyThreadState *tstate = _PyThreadState_GET();
4792 return _PyEval_GetFrame(tstate);
4793}
4794
Guido van Rossumb209a111997-04-29 18:18:01 +00004795PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004796PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004797{
Victor Stinner438a12d2019-05-24 17:01:38 +02004798 PyThreadState *tstate = _PyThreadState_GET();
4799 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004800 if (current_frame == NULL)
Victor Stinner438a12d2019-05-24 17:01:38 +02004801 return tstate->interp->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004802 else
4803 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004804}
4805
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004806/* Convenience function to get a builtin from its name */
4807PyObject *
4808_PyEval_GetBuiltinId(_Py_Identifier *name)
4809{
Victor Stinner438a12d2019-05-24 17:01:38 +02004810 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004811 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4812 if (attr) {
4813 Py_INCREF(attr);
4814 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004815 else if (!_PyErr_Occurred(tstate)) {
4816 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004817 }
4818 return attr;
4819}
4820
Guido van Rossumb209a111997-04-29 18:18:01 +00004821PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004822PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004823{
Victor Stinner438a12d2019-05-24 17:01:38 +02004824 PyThreadState *tstate = _PyThreadState_GET();
4825 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Victor Stinner41bb43a2013-10-29 01:19:37 +01004826 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004827 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004828 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004829 }
4830
Victor Stinner438a12d2019-05-24 17:01:38 +02004831 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01004832 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004833 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004834
4835 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004836 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004837}
4838
Guido van Rossumb209a111997-04-29 18:18:01 +00004839PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004840PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004841{
Victor Stinner438a12d2019-05-24 17:01:38 +02004842 PyThreadState *tstate = _PyThreadState_GET();
4843 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
4844 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004845 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004846 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004847
4848 assert(current_frame->f_globals != NULL);
4849 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004850}
4851
Guido van Rossum6135a871995-01-09 17:53:26 +00004852int
Tim Peters5ba58662001-07-16 02:29:45 +00004853PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004854{
Victor Stinner438a12d2019-05-24 17:01:38 +02004855 PyThreadState *tstate = _PyThreadState_GET();
4856 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004857 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004859 if (current_frame != NULL) {
4860 const int codeflags = current_frame->f_code->co_flags;
4861 const int compilerflags = codeflags & PyCF_MASK;
4862 if (compilerflags) {
4863 result = 1;
4864 cf->cf_flags |= compilerflags;
4865 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004866#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004867 if (codeflags & CO_GENERATOR_ALLOWED) {
4868 result = 1;
4869 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4870 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004871#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004872 }
4873 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004874}
4875
Guido van Rossum3f5da241990-12-20 15:06:42 +00004876
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004877const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004878PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004879{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004880 if (PyMethod_Check(func))
4881 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4882 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004883 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004884 else if (PyCFunction_Check(func))
4885 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4886 else
4887 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004888}
4889
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004890const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004891PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004892{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004893 if (PyMethod_Check(func))
4894 return "()";
4895 else if (PyFunction_Check(func))
4896 return "()";
4897 else if (PyCFunction_Check(func))
4898 return "()";
4899 else
4900 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004901}
4902
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004903#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004904if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004905 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4906 tstate, tstate->frame, \
4907 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004908 x = NULL; \
4909 } \
4910 else { \
4911 x = call; \
4912 if (tstate->c_profilefunc != NULL) { \
4913 if (x == NULL) { \
4914 call_trace_protected(tstate->c_profilefunc, \
4915 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004916 tstate, tstate->frame, \
4917 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004918 /* XXX should pass (type, value, tb) */ \
4919 } else { \
4920 if (call_trace(tstate->c_profilefunc, \
4921 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004922 tstate, tstate->frame, \
4923 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004924 Py_DECREF(x); \
4925 x = NULL; \
4926 } \
4927 } \
4928 } \
4929 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004930} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004931 x = call; \
4932 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004933
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004934
4935static PyObject *
4936trace_call_function(PyThreadState *tstate,
4937 PyObject *func,
4938 PyObject **args, Py_ssize_t nargs,
4939 PyObject *kwnames)
4940{
4941 PyObject *x;
4942 if (PyCFunction_Check(func)) {
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02004943 C_TRACE(x, _PyObject_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004944 return x;
4945 }
4946 else if (Py_TYPE(func) == &PyMethodDescr_Type && nargs > 0) {
4947 /* We need to create a temporary bound method as argument
4948 for profiling.
4949
4950 If nargs == 0, then this cannot work because we have no
4951 "self". In any case, the call itself would raise
4952 TypeError (foo needs an argument), so we just skip
4953 profiling. */
4954 PyObject *self = args[0];
4955 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4956 if (func == NULL) {
4957 return NULL;
4958 }
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02004959 C_TRACE(x, _PyObject_Vectorcall(func,
4960 args+1, nargs-1,
4961 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004962 Py_DECREF(func);
4963 return x;
4964 }
4965 return _PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
4966}
4967
Victor Stinner415c5102017-01-11 00:54:57 +01004968/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4969 to reduce the stack consumption. */
4970Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Victor Stinner09532fe2019-05-10 23:39:09 +02004971call_function(PyThreadState *tstate, PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004972{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004973 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004974 PyObject *func = *pfunc;
4975 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004976 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4977 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004978 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004979
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004980 if (tstate->use_tracing) {
4981 x = trace_call_function(tstate, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09004982 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004983 else {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004984 x = _PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004985 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004986
Victor Stinner438a12d2019-05-24 17:01:38 +02004987 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004988
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004989 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004990 while ((*pp_stack) > pfunc) {
4991 w = EXT_POP(*pp_stack);
4992 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004993 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004995 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004996}
4997
Jeremy Hylton52820442001-01-03 23:52:36 +00004998static PyObject *
Victor Stinner09532fe2019-05-10 23:39:09 +02004999do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005000{
jdemeyere89de732018-09-19 12:06:20 +02005001 PyObject *result;
5002
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005003 if (PyCFunction_Check(func)) {
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +02005004 C_TRACE(result, PyObject_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005005 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005006 }
jdemeyere89de732018-09-19 12:06:20 +02005007 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
jdemeyere89de732018-09-19 12:06:20 +02005008 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
5009 if (nargs > 0 && tstate->use_tracing) {
5010 /* We need to create a temporary bound method as argument
5011 for profiling.
5012
5013 If nargs == 0, then this cannot work because we have no
5014 "self". In any case, the call itself would raise
5015 TypeError (foo needs an argument), so we just skip
5016 profiling. */
5017 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
5018 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5019 if (func == NULL) {
5020 return NULL;
5021 }
5022
Jeroen Demeyer7e1a9aa2019-06-20 17:38:46 +02005023 C_TRACE(result, _PyObject_FastCallDict(func,
5024 &_PyTuple_ITEMS(callargs)[1],
5025 nargs - 1,
5026 kwdict));
jdemeyere89de732018-09-19 12:06:20 +02005027 Py_DECREF(func);
5028 return result;
5029 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005030 }
jdemeyere89de732018-09-19 12:06:20 +02005031 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00005032}
5033
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005034/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005035 nb_index slot defined, and store in *pi.
5036 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08005037 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00005038 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005039*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005040int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005041_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005042{
Victor Stinner438a12d2019-05-24 17:01:38 +02005043 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005044 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005045 Py_ssize_t x;
5046 if (PyIndex_Check(v)) {
5047 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005048 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005049 return 0;
5050 }
5051 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005052 _PyErr_SetString(tstate, PyExc_TypeError,
5053 "slice indices must be integers or "
5054 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005055 return 0;
5056 }
5057 *pi = x;
5058 }
5059 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005060}
5061
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005062int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005063_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005064{
Victor Stinner438a12d2019-05-24 17:01:38 +02005065 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005066 Py_ssize_t x;
5067 if (PyIndex_Check(v)) {
5068 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005069 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005070 return 0;
5071 }
5072 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005073 _PyErr_SetString(tstate, PyExc_TypeError,
5074 "slice indices must be integers or "
5075 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005076 return 0;
5077 }
5078 *pi = x;
5079 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005080}
5081
5082
Guido van Rossum486364b2007-06-30 05:01:58 +00005083#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005084 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00005085
Guido van Rossumb209a111997-04-29 18:18:01 +00005086static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005087cmp_outcome(PyThreadState *tstate, int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005088{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005089 int res = 0;
5090 switch (op) {
5091 case PyCmp_IS:
5092 res = (v == w);
5093 break;
5094 case PyCmp_IS_NOT:
5095 res = (v != w);
5096 break;
5097 case PyCmp_IN:
5098 res = PySequence_Contains(w, v);
5099 if (res < 0)
5100 return NULL;
5101 break;
5102 case PyCmp_NOT_IN:
5103 res = PySequence_Contains(w, v);
5104 if (res < 0)
5105 return NULL;
5106 res = !res;
5107 break;
5108 case PyCmp_EXC_MATCH:
5109 if (PyTuple_Check(w)) {
5110 Py_ssize_t i, length;
5111 length = PyTuple_Size(w);
5112 for (i = 0; i < length; i += 1) {
5113 PyObject *exc = PyTuple_GET_ITEM(w, i);
5114 if (!PyExceptionClass_Check(exc)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005115 _PyErr_SetString(tstate, PyExc_TypeError,
5116 CANNOT_CATCH_MSG);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005117 return NULL;
5118 }
5119 }
5120 }
5121 else {
5122 if (!PyExceptionClass_Check(w)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005123 _PyErr_SetString(tstate, PyExc_TypeError,
5124 CANNOT_CATCH_MSG);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005125 return NULL;
5126 }
5127 }
5128 res = PyErr_GivenExceptionMatches(v, w);
5129 break;
5130 default:
5131 return PyObject_RichCompare(v, w, op);
5132 }
5133 v = res ? Py_True : Py_False;
5134 Py_INCREF(v);
5135 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005136}
5137
Thomas Wouters52152252000-08-17 22:55:00 +00005138static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005139import_name(PyThreadState *tstate, PyFrameObject *f,
5140 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005141{
5142 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005143 PyObject *import_func, *res;
5144 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005145
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005146 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005147 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005148 if (!_PyErr_Occurred(tstate)) {
5149 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005150 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005151 return NULL;
5152 }
5153
5154 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005155 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005156 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02005157 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005158 return NULL;
5159 }
5160 res = PyImport_ImportModuleLevelObject(
5161 name,
5162 f->f_globals,
5163 f->f_locals == NULL ? Py_None : f->f_locals,
5164 fromlist,
5165 ilevel);
5166 return res;
5167 }
5168
5169 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005170
5171 stack[0] = name;
5172 stack[1] = f->f_globals;
5173 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5174 stack[3] = fromlist;
5175 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005176 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005177 Py_DECREF(import_func);
5178 return res;
5179}
5180
5181static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005182import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005183{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005184 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02005185 _Py_IDENTIFIER(__name__);
Xiang Zhang4830f582017-03-21 11:13:42 +08005186 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005187
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005188 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005189 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005190 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005191 /* Issue #17636: in case this failed because of a circular relative
5192 import, try to fallback on reading the module directly from
5193 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02005194 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005195 if (pkgname == NULL) {
5196 goto error;
5197 }
Oren Milman6db70332017-09-19 14:23:01 +03005198 if (!PyUnicode_Check(pkgname)) {
5199 Py_CLEAR(pkgname);
5200 goto error;
5201 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005202 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005203 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005204 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005205 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005206 }
Eric Snow3f9eee62017-09-15 16:35:20 -06005207 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005208 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02005209 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07005210 goto error;
5211 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005212 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005213 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005214 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005215 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005216 if (pkgname == NULL) {
5217 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5218 if (pkgname_or_unknown == NULL) {
5219 Py_XDECREF(pkgpath);
5220 return NULL;
5221 }
5222 } else {
5223 pkgname_or_unknown = pkgname;
5224 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005225
5226 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005227 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08005228 errmsg = PyUnicode_FromFormat(
5229 "cannot import name %R from %R (unknown location)",
5230 name, pkgname_or_unknown
5231 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005232 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005233 PyErr_SetImportError(errmsg, pkgname, NULL);
5234 }
5235 else {
Anthony Sottile65366bc2019-09-09 08:17:50 -07005236 _Py_IDENTIFIER(__spec__);
5237 PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
Anthony Sottile65366bc2019-09-09 08:17:50 -07005238 const char *fmt =
5239 _PyModuleSpec_IsInitializing(spec) ?
5240 "cannot import name %R from partially initialized module %R "
5241 "(most likely due to a circular import) (%S)" :
5242 "cannot import name %R from %R (%S)";
5243 Py_XDECREF(spec);
5244
5245 errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
Stefan Krah027b09c2019-03-25 21:50:58 +01005246 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005247 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005248 }
5249
Xiang Zhang4830f582017-03-21 11:13:42 +08005250 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005251 Py_XDECREF(pkgname_or_unknown);
5252 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005253 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005254}
Guido van Rossumac7be682001-01-17 15:42:30 +00005255
Thomas Wouters52152252000-08-17 22:55:00 +00005256static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005257import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00005258{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005259 _Py_IDENTIFIER(__all__);
5260 _Py_IDENTIFIER(__dict__);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005261 _Py_IDENTIFIER(__name__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005262 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005263 int skip_leading_underscores = 0;
5264 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005265
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005266 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5267 return -1; /* Unexpected error */
5268 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005269 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005270 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5271 return -1;
5272 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005273 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005274 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005275 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005276 return -1;
5277 }
5278 all = PyMapping_Keys(dict);
5279 Py_DECREF(dict);
5280 if (all == NULL)
5281 return -1;
5282 skip_leading_underscores = 1;
5283 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005285 for (pos = 0, err = 0; ; pos++) {
5286 name = PySequence_GetItem(all, pos);
5287 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005288 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005289 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02005290 }
5291 else {
5292 _PyErr_Clear(tstate);
5293 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005294 break;
5295 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005296 if (!PyUnicode_Check(name)) {
5297 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5298 if (modname == NULL) {
5299 Py_DECREF(name);
5300 err = -1;
5301 break;
5302 }
5303 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005304 _PyErr_Format(tstate, PyExc_TypeError,
5305 "module __name__ must be a string, not %.100s",
5306 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005307 }
5308 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005309 _PyErr_Format(tstate, PyExc_TypeError,
5310 "%s in %U.%s must be str, not %.100s",
5311 skip_leading_underscores ? "Key" : "Item",
5312 modname,
5313 skip_leading_underscores ? "__dict__" : "__all__",
5314 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005315 }
5316 Py_DECREF(modname);
5317 Py_DECREF(name);
5318 err = -1;
5319 break;
5320 }
5321 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005322 if (PyUnicode_READY(name) == -1) {
5323 Py_DECREF(name);
5324 err = -1;
5325 break;
5326 }
5327 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5328 Py_DECREF(name);
5329 continue;
5330 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005331 }
5332 value = PyObject_GetAttr(v, name);
5333 if (value == NULL)
5334 err = -1;
5335 else if (PyDict_CheckExact(locals))
5336 err = PyDict_SetItem(locals, name, value);
5337 else
5338 err = PyObject_SetItem(locals, name, value);
5339 Py_DECREF(name);
5340 Py_XDECREF(value);
5341 if (err != 0)
5342 break;
5343 }
5344 Py_DECREF(all);
5345 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005346}
5347
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005348static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005349check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005350{
5351 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005352 _PyErr_Format(tstate, PyExc_TypeError,
5353 "%.200s%.200s argument after * "
5354 "must be an iterable, not %.200s",
5355 PyEval_GetFuncName(func),
5356 PyEval_GetFuncDesc(func),
5357 args->ob_type->tp_name);
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005358 return -1;
5359 }
5360 return 0;
5361}
5362
5363static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005364format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005365{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005366 /* _PyDict_MergeEx raises attribute
5367 * error (percolated from an attempt
5368 * to get 'keys' attribute) instead of
5369 * a type error if its second argument
5370 * is not a mapping.
5371 */
Victor Stinner438a12d2019-05-24 17:01:38 +02005372 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
5373 _PyErr_Format(tstate, PyExc_TypeError,
5374 "%.200s%.200s argument after ** "
5375 "must be a mapping, not %.200s",
5376 PyEval_GetFuncName(func),
5377 PyEval_GetFuncDesc(func),
5378 kwargs->ob_type->tp_name);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005379 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005380 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005381 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02005382 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005383 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
5384 PyObject *key = PyTuple_GET_ITEM(val, 0);
Jeroen Demeyer05677862019-08-16 12:41:27 +02005385 _PyErr_Format(tstate, PyExc_TypeError,
5386 "%.200s%.200s got multiple "
5387 "values for keyword argument '%S'",
5388 PyEval_GetFuncName(func),
5389 PyEval_GetFuncDesc(func),
5390 key);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005391 Py_XDECREF(exc);
5392 Py_XDECREF(val);
5393 Py_XDECREF(tb);
5394 }
5395 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005396 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005397 }
5398 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005399}
5400
Guido van Rossumac7be682001-01-17 15:42:30 +00005401static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005402format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
5403 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005404{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005405 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005407 if (!obj)
5408 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005409
Serhiy Storchaka06515832016-11-20 09:13:07 +02005410 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005411 if (!obj_str)
5412 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005413
Victor Stinner438a12d2019-05-24 17:01:38 +02005414 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005415}
Guido van Rossum950361c1997-01-24 13:49:28 +00005416
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005417static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005418format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005419{
5420 PyObject *name;
5421 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02005422 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005423 return;
5424 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5425 name = PyTuple_GET_ITEM(co->co_cellvars,
5426 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005427 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005428 PyExc_UnboundLocalError,
5429 UNBOUNDLOCAL_ERROR_MSG,
5430 name);
5431 } else {
5432 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5433 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02005434 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005435 UNBOUNDFREE_ERROR_MSG, name);
5436 }
5437}
5438
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005439static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005440format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005441{
5442 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5443 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005444 _PyErr_Format(tstate, PyExc_TypeError,
5445 "'async with' received an object from __aenter__ "
5446 "that does not implement __await__: %.100s",
5447 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005448 }
5449 else if (prevopcode == WITH_CLEANUP_START) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005450 _PyErr_Format(tstate, PyExc_TypeError,
5451 "'async with' received an object from __aexit__ "
5452 "that does not implement __await__: %.100s",
5453 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005454 }
5455 }
5456}
5457
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005458static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005459unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005460 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005461{
5462 PyObject *res;
5463 if (Py_REFCNT(v) == 2) {
5464 /* In the common case, there are 2 references to the value
5465 * stored in 'variable' when the += is performed: one on the
5466 * value stack (in 'v') and one still stored in the
5467 * 'variable'. We try to delete the variable now to reduce
5468 * the refcnt to 1.
5469 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005470 int opcode, oparg;
5471 NEXTOPARG();
5472 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005473 case STORE_FAST:
5474 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005475 PyObject **fastlocals = f->f_localsplus;
5476 if (GETLOCAL(oparg) == v)
5477 SETLOCAL(oparg, NULL);
5478 break;
5479 }
5480 case STORE_DEREF:
5481 {
5482 PyObject **freevars = (f->f_localsplus +
5483 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005484 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005485 if (PyCell_GET(c) == v) {
5486 PyCell_SET(c, NULL);
5487 Py_DECREF(v);
5488 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005489 break;
5490 }
5491 case STORE_NAME:
5492 {
5493 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005494 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005495 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005496 if (locals && PyDict_CheckExact(locals)) {
5497 PyObject *w = PyDict_GetItemWithError(locals, name);
5498 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02005499 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005500 {
5501 Py_DECREF(v);
5502 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005503 }
5504 }
5505 break;
5506 }
5507 }
5508 }
5509 res = v;
5510 PyUnicode_Append(&res, w);
5511 return res;
5512}
5513
Guido van Rossum950361c1997-01-24 13:49:28 +00005514#ifdef DYNAMIC_EXECUTION_PROFILE
5515
Skip Montanarof118cb12001-10-15 20:51:38 +00005516static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005517getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005518{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005519 int i;
5520 PyObject *l = PyList_New(256);
5521 if (l == NULL) return NULL;
5522 for (i = 0; i < 256; i++) {
5523 PyObject *x = PyLong_FromLong(a[i]);
5524 if (x == NULL) {
5525 Py_DECREF(l);
5526 return NULL;
5527 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005528 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005529 }
5530 for (i = 0; i < 256; i++)
5531 a[i] = 0;
5532 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005533}
5534
5535PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005536_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005537{
5538#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005539 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005540#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005541 int i;
5542 PyObject *l = PyList_New(257);
5543 if (l == NULL) return NULL;
5544 for (i = 0; i < 257; i++) {
5545 PyObject *x = getarray(dxpairs[i]);
5546 if (x == NULL) {
5547 Py_DECREF(l);
5548 return NULL;
5549 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005550 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005551 }
5552 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005553#endif
5554}
5555
5556#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005557
5558Py_ssize_t
5559_PyEval_RequestCodeExtraIndex(freefunc free)
5560{
Victor Stinnercaba55b2018-08-03 15:33:52 +02005561 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -07005562 Py_ssize_t new_index;
5563
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005564 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005565 return -1;
5566 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005567 new_index = interp->co_extra_user_count++;
5568 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005569 return new_index;
5570}
Łukasz Langaa785c872016-09-09 17:37:37 -07005571
5572static void
5573dtrace_function_entry(PyFrameObject *f)
5574{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005575 const char *filename;
5576 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005577 int lineno;
5578
5579 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5580 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5581 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5582
5583 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5584}
5585
5586static void
5587dtrace_function_return(PyFrameObject *f)
5588{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005589 const char *filename;
5590 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005591 int lineno;
5592
5593 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5594 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5595 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5596
5597 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5598}
5599
5600/* DTrace equivalent of maybe_call_line_trace. */
5601static void
5602maybe_dtrace_line(PyFrameObject *frame,
5603 int *instr_lb, int *instr_ub, int *instr_prev)
5604{
5605 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005606 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005607
5608 /* If the last instruction executed isn't in the current
5609 instruction window, reset the window.
5610 */
5611 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5612 PyAddrPair bounds;
5613 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5614 &bounds);
5615 *instr_lb = bounds.ap_lower;
5616 *instr_ub = bounds.ap_upper;
5617 }
5618 /* If the last instruction falls at the start of a line or if
5619 it represents a jump backwards, update the frame's line
5620 number and call the trace function. */
5621 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5622 frame->f_lineno = line;
5623 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5624 if (!co_filename)
5625 co_filename = "?";
5626 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5627 if (!co_name)
5628 co_name = "?";
5629 PyDTrace_LINE(co_filename, co_name, line);
5630 }
5631 *instr_prev = frame->f_lasti;
5632}