blob: 0a4af915d6ffe0b3df4ecdc605900e61fc84a350 [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
Yury Selivanovf2392132016-12-13 19:03:51 -050041/* Private API for the LOAD_METHOD opcode. */
42extern int _PyObject_GetMethod(PyObject *, PyObject *, PyObject **);
43
Jeremy Hylton52820442001-01-03 23:52:36 +000044typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000045
Guido van Rossum374a9221991-04-04 10:40:29 +000046/* Forward declarations */
Victor Stinner09532fe2019-05-10 23:39:09 +020047Py_LOCAL_INLINE(PyObject *) call_function(
48 PyThreadState *tstate, PyObject ***pp_stack,
49 Py_ssize_t oparg, PyObject *kwnames);
50static PyObject * do_call_core(
51 PyThreadState *tstate, PyObject *func,
52 PyObject *callargs, PyObject *kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +000053
Guido van Rossum0a066c01992-03-27 17:29:15 +000054#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000055static int lltrace;
Victor Stinner438a12d2019-05-24 17:01:38 +020056static int prtrace(PyThreadState *, PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000057#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010058static int call_trace(Py_tracefunc, PyObject *,
59 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000061static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010062 PyThreadState *, PyFrameObject *,
63 int, PyObject *);
64static void call_exc_trace(Py_tracefunc, PyObject *,
65 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000066static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Eric Snow2ebc5ce2017-09-07 23:51:28 -060067 PyThreadState *, PyFrameObject *,
68 int *, int *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070069static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *);
70static void dtrace_function_entry(PyFrameObject *);
71static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000072
Victor Stinner438a12d2019-05-24 17:01:38 +020073static PyObject * cmp_outcome(PyThreadState *, int, PyObject *, PyObject *);
74static PyObject * import_name(PyThreadState *, PyFrameObject *,
75 PyObject *, PyObject *, PyObject *);
76static PyObject * import_from(PyThreadState *, PyObject *, PyObject *);
77static int import_all_from(PyThreadState *, PyObject *, PyObject *);
78static void format_exc_check_arg(PyThreadState *, PyObject *, const char *, PyObject *);
79static void format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg);
80static PyObject * unicode_concatenate(PyThreadState *, PyObject *, PyObject *,
Serhiy Storchakaab874002016-09-11 13:48:15 +030081 PyFrameObject *, const _Py_CODEUNIT *);
Victor Stinner438a12d2019-05-24 17:01:38 +020082static PyObject * special_lookup(PyThreadState *, PyObject *, _Py_Identifier *);
83static int check_args_iterable(PyThreadState *, PyObject *func, PyObject *vararg);
84static void format_kwargs_error(PyThreadState *, PyObject *func, PyObject *kwargs);
85static void format_awaitable_error(PyThreadState *, PyTypeObject *, int);
Guido van Rossum374a9221991-04-04 10:40:29 +000086
Paul Prescode68140d2000-08-30 20:25:01 +000087#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000089#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000091#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 "free variable '%.200s' referenced before assignment" \
93 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000094
Guido van Rossum950361c1997-01-24 13:49:28 +000095/* Dynamic execution profile */
96#ifdef DYNAMIC_EXECUTION_PROFILE
97#ifdef DXPAIRS
98static long dxpairs[257][256];
99#define dxp dxpairs[256]
100#else
101static long dxp[256];
102#endif
103#endif
104
Inada Naoki91234a12019-06-03 21:30:58 +0900105/* per opcode cache */
106#define OPCACHE_MIN_RUNS 1024 /* create opcache when code executed this time */
107#define OPCACHE_STATS 0 /* Enable stats */
108
109#if OPCACHE_STATS
110static size_t opcache_code_objects = 0;
111static size_t opcache_code_objects_extra_mem = 0;
112
113static size_t opcache_global_opts = 0;
114static size_t opcache_global_hits = 0;
115static size_t opcache_global_misses = 0;
116#endif
117
Victor Stinnere225beb2019-06-03 18:14:24 +0200118#define GIL_REQUEST _Py_atomic_load_relaxed(&ceval->gil_drop_request)
Inada Naoki91234a12019-06-03 21:30:58 +0900119
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000120/* This can set eval_breaker to 0 even though gil_drop_request became
121 1. We believe this is all right because the eval loop will release
122 the GIL eventually anyway. */
Victor Stinnere225beb2019-06-03 18:14:24 +0200123#define COMPUTE_EVAL_BREAKER(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 _Py_atomic_store_relaxed( \
Victor Stinnere225beb2019-06-03 18:14:24 +0200125 &(ceval)->eval_breaker, \
126 GIL_REQUEST | \
127 _Py_atomic_load_relaxed(&(ceval)->signals_pending) | \
128 _Py_atomic_load_relaxed(&(ceval)->pending.calls_to_do) | \
129 (ceval)->pending.async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000130
Victor Stinnere225beb2019-06-03 18:14:24 +0200131#define SET_GIL_DROP_REQUEST(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200133 _Py_atomic_store_relaxed(&(ceval)->gil_drop_request, 1); \
134 _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000136
Victor Stinnere225beb2019-06-03 18:14:24 +0200137#define RESET_GIL_DROP_REQUEST(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000138 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200139 _Py_atomic_store_relaxed(&(ceval)->gil_drop_request, 0); \
140 COMPUTE_EVAL_BREAKER(ceval); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000141 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000142
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000143/* Pending calls are only modified under pending_lock */
Victor Stinnere225beb2019-06-03 18:14:24 +0200144#define SIGNAL_PENDING_CALLS(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200146 _Py_atomic_store_relaxed(&(ceval)->pending.calls_to_do, 1); \
147 _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000149
Victor Stinnere225beb2019-06-03 18:14:24 +0200150#define UNSIGNAL_PENDING_CALLS(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200152 _Py_atomic_store_relaxed(&(ceval)->pending.calls_to_do, 0); \
153 COMPUTE_EVAL_BREAKER(ceval); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000154 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000155
Victor Stinnere225beb2019-06-03 18:14:24 +0200156#define SIGNAL_PENDING_SIGNALS(ceval) \
Eric Snowfdf282d2019-01-11 14:26:55 -0700157 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200158 _Py_atomic_store_relaxed(&(ceval)->signals_pending, 1); \
159 _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \
Eric Snowfdf282d2019-01-11 14:26:55 -0700160 } while (0)
161
Victor Stinnere225beb2019-06-03 18:14:24 +0200162#define UNSIGNAL_PENDING_SIGNALS(ceval) \
Eric Snowfdf282d2019-01-11 14:26:55 -0700163 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200164 _Py_atomic_store_relaxed(&(ceval)->signals_pending, 0); \
165 COMPUTE_EVAL_BREAKER(ceval); \
Eric Snowfdf282d2019-01-11 14:26:55 -0700166 } while (0)
167
Victor Stinnere225beb2019-06-03 18:14:24 +0200168#define SIGNAL_ASYNC_EXC(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200170 (ceval)->pending.async_exc = 1; \
171 _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000173
Victor Stinnere225beb2019-06-03 18:14:24 +0200174#define UNSIGNAL_ASYNC_EXC(ceval) \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600175 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200176 (ceval)->pending.async_exc = 0; \
177 COMPUTE_EVAL_BREAKER(ceval); \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600178 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000179
180
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000181#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000182#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000183#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000184#include "pythread.h"
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000185#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000186
Tim Peters7f468f22004-10-11 02:40:51 +0000187int
188PyEval_ThreadsInitialized(void)
189{
Victor Stinner09532fe2019-05-10 23:39:09 +0200190 return gil_created(&_PyRuntime.ceval.gil);
Tim Peters7f468f22004-10-11 02:40:51 +0000191}
192
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000193void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000194PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000195{
Victor Stinner09532fe2019-05-10 23:39:09 +0200196 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200197 struct _ceval_runtime_state *ceval = &runtime->ceval;
198 struct _gil_runtime_state *gil = &ceval->gil;
Victor Stinner09532fe2019-05-10 23:39:09 +0200199 if (gil_created(gil)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 return;
Victor Stinnera7126792019-03-19 14:19:38 +0100201 }
202
Inada Naoki001fee12019-02-20 10:00:09 +0900203 PyThread_init_thread();
Victor Stinner09532fe2019-05-10 23:39:09 +0200204 create_gil(gil);
205 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinnere225beb2019-06-03 18:14:24 +0200206 take_gil(ceval, tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700207
Victor Stinnere225beb2019-06-03 18:14:24 +0200208 struct _pending_calls *pending = &ceval->pending;
209 pending->lock = PyThread_allocate_lock();
210 if (pending->lock == NULL) {
211 Py_FatalError("Can't initialize threads for pending calls");
212 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000213}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000214
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000215void
Victor Stinnere225beb2019-06-03 18:14:24 +0200216_PyEval_FiniThreads(struct _ceval_runtime_state *ceval)
Antoine Pitrou1df15362010-09-13 14:16:46 +0000217{
Victor Stinnere225beb2019-06-03 18:14:24 +0200218 struct _gil_runtime_state *gil = &ceval->gil;
Victor Stinner09532fe2019-05-10 23:39:09 +0200219 if (!gil_created(gil)) {
Antoine Pitrou1df15362010-09-13 14:16:46 +0000220 return;
Victor Stinnera7126792019-03-19 14:19:38 +0100221 }
222
Victor Stinner09532fe2019-05-10 23:39:09 +0200223 destroy_gil(gil);
224 assert(!gil_created(gil));
Victor Stinner99fcc612019-04-29 13:04:07 +0200225
Victor Stinnere225beb2019-06-03 18:14:24 +0200226 struct _pending_calls *pending = &ceval->pending;
227 if (pending->lock != NULL) {
228 PyThread_free_lock(pending->lock);
229 pending->lock = NULL;
230 }
Antoine Pitrou1df15362010-09-13 14:16:46 +0000231}
232
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400233static inline void
Eric Snow396e0a82019-05-31 21:16:47 -0600234exit_thread_if_finalizing(PyThreadState *tstate)
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400235{
Victor Stinnere225beb2019-06-03 18:14:24 +0200236 _PyRuntimeState *runtime = tstate->interp->runtime;
237 /* _Py_Finalizing is protected by the GIL */
Victor Stinner09532fe2019-05-10 23:39:09 +0200238 if (runtime->finalizing != NULL && !_Py_CURRENTLY_FINALIZING(runtime, tstate)) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200239 drop_gil(&runtime->ceval, tstate);
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400240 PyThread_exit_thread();
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400241 }
242}
243
Antoine Pitrou1df15362010-09-13 14:16:46 +0000244void
Inada Naoki91234a12019-06-03 21:30:58 +0900245_PyEval_Fini(void)
246{
247#if OPCACHE_STATS
248 fprintf(stderr, "-- Opcode cache number of objects = %zd\n",
249 opcache_code_objects);
250
251 fprintf(stderr, "-- Opcode cache total extra mem = %zd\n",
252 opcache_code_objects_extra_mem);
253
254 fprintf(stderr, "\n");
255
256 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL hits = %zd (%d%%)\n",
257 opcache_global_hits,
258 (int) (100.0 * opcache_global_hits /
259 (opcache_global_hits + opcache_global_misses)));
260
261 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL misses = %zd (%d%%)\n",
262 opcache_global_misses,
263 (int) (100.0 * opcache_global_misses /
264 (opcache_global_hits + opcache_global_misses)));
265
266 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL opts = %zd\n",
267 opcache_global_opts);
268
269 fprintf(stderr, "\n");
270#endif
271}
272
273void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000274PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000275{
Victor Stinner09532fe2019-05-10 23:39:09 +0200276 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200277 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinner09532fe2019-05-10 23:39:09 +0200278 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
279 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
Victor Stinner09532fe2019-05-10 23:39:09 +0200281 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200282 take_gil(ceval, tstate);
Eric Snow396e0a82019-05-31 21:16:47 -0600283 exit_thread_if_finalizing(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000284}
285
286void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000287PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000288{
Victor Stinner09532fe2019-05-10 23:39:09 +0200289 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200290 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100292 We therefore avoid PyThreadState_Get() which dumps a fatal error
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 in debug mode.
294 */
Victor Stinnere225beb2019-06-03 18:14:24 +0200295 drop_gil(&runtime->ceval, tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000296}
297
298void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000299PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000300{
Victor Stinner09532fe2019-05-10 23:39:09 +0200301 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200303 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200304 assert(tstate->interp != NULL);
305
306 _PyRuntimeState *runtime = tstate->interp->runtime;
307 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinner09532fe2019-05-10 23:39:09 +0200308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 /* Check someone has called PyEval_InitThreads() to create the lock */
Victor Stinnere225beb2019-06-03 18:14:24 +0200310 assert(gil_created(&ceval->gil));
311 take_gil(ceval, tstate);
Eric Snow396e0a82019-05-31 21:16:47 -0600312 exit_thread_if_finalizing(tstate);
Victor Stinner09532fe2019-05-10 23:39:09 +0200313 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
314 Py_FatalError("PyEval_AcquireThread: non-NULL old thread state");
315 }
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000316}
317
318void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000319PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000320{
Victor Stinner09532fe2019-05-10 23:39:09 +0200321 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200323 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200324 assert(tstate->interp != NULL);
Victor Stinner09532fe2019-05-10 23:39:09 +0200325
Victor Stinnere225beb2019-06-03 18:14:24 +0200326 _PyRuntimeState *runtime = tstate->interp->runtime;
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 */
Eric Snow396e0a82019-05-31 21:16:47 -0600357 _PyThreadState_DeleteExcept(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 Stinner09532fe2019-05-10 23:39:09 +0200386 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Victor Stinner09532fe2019-05-10 23:39:09 +0200388 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200389 assert(tstate->interp != NULL);
Eric Snow396e0a82019-05-31 21:16:47 -0600390
Victor Stinnere225beb2019-06-03 18:14:24 +0200391 _PyRuntimeState *runtime = tstate->interp->runtime;
392 struct _ceval_runtime_state *ceval = &runtime->ceval;
393 assert(gil_created(&ceval->gil));
Victor Stinner2914bb32018-01-29 11:57:45 +0100394
395 int err = errno;
Victor Stinnere225beb2019-06-03 18:14:24 +0200396 take_gil(ceval, tstate);
Eric Snow396e0a82019-05-31 21:16:47 -0600397 exit_thread_if_finalizing(tstate);
Victor Stinner2914bb32018-01-29 11:57:45 +0100398 errno = err;
399
Victor Stinner09532fe2019-05-10 23:39:09 +0200400 _PyThreadState_Swap(&runtime->gilstate, tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000401}
402
403
Guido van Rossuma9672091994-09-14 13:31:22 +0000404/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
405 signal handlers or Mac I/O completion routines) can schedule calls
406 to a function to be called synchronously.
407 The synchronous function is called with one void* argument.
408 It should return 0 for success or -1 for failure -- failure should
409 be accompanied by an exception.
410
411 If registry succeeds, the registry function returns 0; if it fails
412 (e.g. due to too many pending calls) it returns -1 (without setting
413 an exception condition).
414
415 Note that because registry may occur from within signal handlers,
416 or other asynchronous events, calling malloc() is unsafe!
417
Guido van Rossuma9672091994-09-14 13:31:22 +0000418 Any thread can schedule pending calls, but only the main thread
419 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000420 There is no facility to schedule calls to a particular thread, but
421 that should be easy to change, should that ever be required. In
422 that case, the static variables here should go into the python
423 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000424*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000425
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200426void
Victor Stinnere225beb2019-06-03 18:14:24 +0200427_PyEval_SignalReceived(struct _ceval_runtime_state *ceval)
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200428{
429 /* bpo-30703: Function called when the C signal handler of Python gets a
430 signal. We cannot queue a callback using Py_AddPendingCall() since
431 that function is not async-signal-safe. */
Victor Stinnere225beb2019-06-03 18:14:24 +0200432 SIGNAL_PENDING_SIGNALS(ceval);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200433}
434
Eric Snow5be45a62019-03-08 22:47:07 -0700435/* Push one item onto the queue while holding the lock. */
436static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200437_push_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600438 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700439{
Eric Snow842a2f02019-03-15 15:47:51 -0600440 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700441 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600442 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700443 return -1; /* Queue full */
444 }
Eric Snow842a2f02019-03-15 15:47:51 -0600445 pending->calls[i].func = func;
446 pending->calls[i].arg = arg;
447 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700448 return 0;
449}
450
451/* Pop one item off the queue while holding the lock. */
452static void
Victor Stinnere225beb2019-06-03 18:14:24 +0200453_pop_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600454 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700455{
Eric Snow842a2f02019-03-15 15:47:51 -0600456 int i = pending->first;
457 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700458 return; /* Queue empty */
459 }
460
Eric Snow842a2f02019-03-15 15:47:51 -0600461 *func = pending->calls[i].func;
462 *arg = pending->calls[i].arg;
463 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700464}
465
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200466/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000467 scheduling to be made from any thread, and even from an executing
468 callback.
469 */
470
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000471int
Victor Stinner438a12d2019-05-24 17:01:38 +0200472_PyEval_AddPendingCall(PyThreadState *tstate,
Victor Stinnere225beb2019-06-03 18:14:24 +0200473 struct _ceval_runtime_state *ceval,
Victor Stinner09532fe2019-05-10 23:39:09 +0200474 int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000475{
Victor Stinnere225beb2019-06-03 18:14:24 +0200476 struct _pending_calls *pending = &ceval->pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600477
478 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
479 if (pending->finishing) {
480 PyThread_release_lock(pending->lock);
481
482 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +0200483 _PyErr_Fetch(tstate, &exc, &val, &tb);
484 _PyErr_SetString(tstate, PyExc_SystemError,
Eric Snow842a2f02019-03-15 15:47:51 -0600485 "Py_AddPendingCall: cannot add pending calls "
486 "(Python shutting down)");
Victor Stinner438a12d2019-05-24 17:01:38 +0200487 _PyErr_Print(tstate);
488 _PyErr_Restore(tstate, exc, val, tb);
Eric Snow842a2f02019-03-15 15:47:51 -0600489 return -1;
490 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200491 int result = _push_pending_call(pending, func, arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600492 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700493
Victor Stinnere225beb2019-06-03 18:14:24 +0200494 /* signal main loop */
495 SIGNAL_PENDING_CALLS(ceval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000497}
498
Victor Stinner09532fe2019-05-10 23:39:09 +0200499int
500Py_AddPendingCall(int (*func)(void *), void *arg)
501{
Victor Stinner438a12d2019-05-24 17:01:38 +0200502 _PyRuntimeState *runtime = &_PyRuntime;
503 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinnere225beb2019-06-03 18:14:24 +0200504 return _PyEval_AddPendingCall(tstate, &runtime->ceval, func, arg);
Victor Stinner09532fe2019-05-10 23:39:09 +0200505}
506
Eric Snowfdf282d2019-01-11 14:26:55 -0700507static int
Victor Stinner09532fe2019-05-10 23:39:09 +0200508handle_signals(_PyRuntimeState *runtime)
Eric Snowfdf282d2019-01-11 14:26:55 -0700509{
Eric Snow5be45a62019-03-08 22:47:07 -0700510 /* Only handle signals on main thread. PyEval_InitThreads must
511 * have been called already.
512 */
Victor Stinner09532fe2019-05-10 23:39:09 +0200513 if (PyThread_get_thread_ident() != runtime->main_thread) {
Eric Snowfdf282d2019-01-11 14:26:55 -0700514 return 0;
515 }
Eric Snow64d6cc82019-02-23 15:40:43 -0700516 /*
517 * Ensure that the thread isn't currently running some other
518 * interpreter.
519 */
Victor Stinner09532fe2019-05-10 23:39:09 +0200520 PyInterpreterState *interp = _PyRuntimeState_GetThreadState(runtime)->interp;
521 if (interp != runtime->interpreters.main) {
Eric Snow64d6cc82019-02-23 15:40:43 -0700522 return 0;
523 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700524
Victor Stinnere225beb2019-06-03 18:14:24 +0200525 struct _ceval_runtime_state *ceval = &runtime->ceval;
526 UNSIGNAL_PENDING_SIGNALS(ceval);
Eric Snow64d6cc82019-02-23 15:40:43 -0700527 if (_PyErr_CheckSignals() < 0) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200528 SIGNAL_PENDING_SIGNALS(ceval); /* We're not done yet */
Eric Snowfdf282d2019-01-11 14:26:55 -0700529 return -1;
530 }
531 return 0;
532}
533
534static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200535make_pending_calls(_PyRuntimeState *runtime)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000536{
Eric Snow6a150bc2019-06-01 15:39:46 -0600537 static int busy = 0;
Eric Snowb75b1a352019-04-12 10:20:10 -0600538
Victor Stinnere225beb2019-06-03 18:14:24 +0200539 /* only service pending calls on main thread */
540 if (PyThread_get_thread_ident() != runtime->main_thread) {
541 return 0;
542 }
543
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 /* don't perform recursive pending calls */
Eric Snowfdf282d2019-01-11 14:26:55 -0700545 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700547 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200548 busy = 1;
Victor Stinnere225beb2019-06-03 18:14:24 +0200549 struct _ceval_runtime_state *ceval = &runtime->ceval;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200550 /* unsignal before starting to call callbacks, so that any callback
551 added in-between re-signals */
Victor Stinnere225beb2019-06-03 18:14:24 +0200552 UNSIGNAL_PENDING_CALLS(ceval);
Eric Snowfdf282d2019-01-11 14:26:55 -0700553 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 /* perform a bounded number of calls, in case of recursion */
Victor Stinnere225beb2019-06-03 18:14:24 +0200556 struct _pending_calls *pending = &ceval->pending;
Eric Snowfdf282d2019-01-11 14:26:55 -0700557 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700558 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 void *arg = NULL;
560
561 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600562 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200563 _pop_pending_call(pending, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600564 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700565
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100566 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700567 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100568 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700569 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700570 res = func(arg);
571 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200572 goto error;
573 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200575
Charles-François Natalif23339a2011-07-23 18:15:43 +0200576 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700577 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200578
579error:
580 busy = 0;
Victor Stinnere225beb2019-06-03 18:14:24 +0200581 SIGNAL_PENDING_CALLS(ceval);
Eric Snowfdf282d2019-01-11 14:26:55 -0700582 return res;
583}
584
Eric Snow842a2f02019-03-15 15:47:51 -0600585void
Victor Stinnere225beb2019-06-03 18:14:24 +0200586_Py_FinishPendingCalls(_PyRuntimeState *runtime)
Eric Snow842a2f02019-03-15 15:47:51 -0600587{
Eric Snow842a2f02019-03-15 15:47:51 -0600588 assert(PyGILState_Check());
589
Victor Stinnere225beb2019-06-03 18:14:24 +0200590 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
591 struct _pending_calls *pending = &runtime->ceval.pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200592
Eric Snow842a2f02019-03-15 15:47:51 -0600593 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
594 pending->finishing = 1;
595 PyThread_release_lock(pending->lock);
596
597 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
598 return;
599 }
600
Victor Stinnere225beb2019-06-03 18:14:24 +0200601 if (make_pending_calls(runtime) < 0) {
602 PyObject *exc, *val, *tb;
603 _PyErr_Fetch(tstate, &exc, &val, &tb);
604 PyErr_BadInternalCall();
605 _PyErr_ChainExceptions(exc, val, tb);
606 _PyErr_Print(tstate);
Eric Snow842a2f02019-03-15 15:47:51 -0600607 }
608}
609
Eric Snowfdf282d2019-01-11 14:26:55 -0700610/* Py_MakePendingCalls() is a simple wrapper for the sake
611 of backward-compatibility. */
612int
613Py_MakePendingCalls(void)
614{
615 assert(PyGILState_Check());
616
617 /* Python signal handler doesn't really queue a callback: it only signals
618 that a signal was received, see _PyEval_SignalReceived(). */
Victor Stinner09532fe2019-05-10 23:39:09 +0200619 _PyRuntimeState *runtime = &_PyRuntime;
620 int res = handle_signals(runtime);
Eric Snowfdf282d2019-01-11 14:26:55 -0700621 if (res != 0) {
622 return res;
623 }
624
Victor Stinnere225beb2019-06-03 18:14:24 +0200625 res = make_pending_calls(runtime);
Eric Snowb75b1a352019-04-12 10:20:10 -0600626 if (res != 0) {
627 return res;
628 }
629
630 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000631}
632
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000633/* The interpreter's recursion limit */
634
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000635#ifndef Py_DEFAULT_RECURSION_LIMIT
636#define Py_DEFAULT_RECURSION_LIMIT 1000
637#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600638
Eric Snow05351c12017-09-05 21:43:08 -0700639int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000640
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600641void
Victor Stinnere225beb2019-06-03 18:14:24 +0200642_PyEval_Initialize(struct _ceval_runtime_state *state)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600643{
Victor Stinnere225beb2019-06-03 18:14:24 +0200644 state->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600645 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Victor Stinnere225beb2019-06-03 18:14:24 +0200646 _gil_initialize(&state->gil);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600647}
648
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000649int
650Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000651{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600652 return _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000653}
654
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000655void
656Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000657{
Victor Stinnere225beb2019-06-03 18:14:24 +0200658 struct _ceval_runtime_state *ceval = &_PyRuntime.ceval;
659 ceval->recursion_limit = new_limit;
660 _Py_CheckRecursionLimit = ceval->recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000661}
662
Armin Rigo2b3eb402003-10-28 12:05:48 +0000663/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
664 if the recursion_depth reaches _Py_CheckRecursionLimit.
665 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
666 to guarantee that _Py_CheckRecursiveCall() is regularly called.
667 Without USE_STACKCHECK, there is no need for this. */
668int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300669_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000670{
Victor Stinner09532fe2019-05-10 23:39:09 +0200671 _PyRuntimeState *runtime = &_PyRuntime;
672 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
673 int recursion_limit = runtime->ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000674
675#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700676 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 if (PyOS_CheckStack()) {
678 --tstate->recursion_depth;
Victor Stinner438a12d2019-05-24 17:01:38 +0200679 _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 return -1;
681 }
pdox18967932017-10-25 23:03:01 -0700682 /* Needed for ABI backwards-compatibility (see bpo-31857) */
Eric Snow05351c12017-09-05 21:43:08 -0700683 _Py_CheckRecursionLimit = recursion_limit;
pdox18967932017-10-25 23:03:01 -0700684#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000685 if (tstate->recursion_critical)
686 /* Somebody asked that we don't check for recursion. */
687 return 0;
688 if (tstate->overflowed) {
689 if (tstate->recursion_depth > recursion_limit + 50) {
690 /* Overflowing while handling an overflow. Give up. */
691 Py_FatalError("Cannot recover from stack overflow.");
692 }
693 return 0;
694 }
695 if (tstate->recursion_depth > recursion_limit) {
696 --tstate->recursion_depth;
697 tstate->overflowed = 1;
Victor Stinner438a12d2019-05-24 17:01:38 +0200698 _PyErr_Format(tstate, PyExc_RecursionError,
699 "maximum recursion depth exceeded%s",
700 where);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 return -1;
702 }
703 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000704}
705
Victor Stinner09532fe2019-05-10 23:39:09 +0200706static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
Victor Stinner438a12d2019-05-24 17:01:38 +0200707static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000708
Victor Stinnere225beb2019-06-03 18:14:24 +0200709#define _Py_TracingPossible(ceval) ((ceval)->tracing_possible)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000710
Guido van Rossum374a9221991-04-04 10:40:29 +0000711
Guido van Rossumb209a111997-04-29 18:18:01 +0000712PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000713PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000714{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 return PyEval_EvalCodeEx(co,
716 globals, locals,
717 (PyObject **)NULL, 0,
718 (PyObject **)NULL, 0,
719 (PyObject **)NULL, 0,
720 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000721}
722
723
724/* Interpreter main loop */
725
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000726PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000727PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 /* This is for backward compatibility with extension modules that
729 used this API; core interpreter code should call
730 PyEval_EvalFrameEx() */
731 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000732}
733
734PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000735PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000736{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200737 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
738 return interp->eval_frame(f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700739}
740
Victor Stinnerc6944e72016-11-11 02:13:35 +0100741PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700742_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
743{
Guido van Rossum950361c1997-01-24 13:49:28 +0000744#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000746#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200747 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300748 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200749 int opcode; /* Current opcode */
750 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200751 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 PyObject *retval = NULL; /* Return value */
Victor Stinner09532fe2019-05-10 23:39:09 +0200753 _PyRuntimeState * const runtime = &_PyRuntime;
754 PyThreadState * const tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinnere225beb2019-06-03 18:14:24 +0200755 struct _ceval_runtime_state * const ceval = &runtime->ceval;
756 _Py_atomic_int * const eval_breaker = &ceval->eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000758
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000760
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 is true when the line being executed has changed. The
764 initial values are such as to make this false the first
765 time it is tested. */
766 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000767
Serhiy Storchakaab874002016-09-11 13:48:15 +0300768 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 PyObject *names;
770 PyObject *consts;
Inada Naoki91234a12019-06-03 21:30:58 +0900771 _PyOpcache *co_opcache;
Guido van Rossum374a9221991-04-04 10:40:29 +0000772
Brett Cannon368b4b72012-04-02 12:17:59 -0400773#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200774 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400775#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200776
Antoine Pitroub52ec782009-01-25 16:34:23 +0000777/* Computed GOTOs, or
778 the-optimization-commonly-but-improperly-known-as-"threaded code"
779 using gcc's labels-as-values extension
780 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
781
782 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000784 combined with a lookup table of jump addresses. However, since the
785 indirect jump instruction is shared by all opcodes, the CPU will have a
786 hard time making the right prediction for where to jump next (actually,
787 it will be always wrong except in the uncommon case of a sequence of
788 several identical opcodes).
789
790 "Threaded code" in contrast, uses an explicit jump table and an explicit
791 indirect jump instruction at the end of each opcode. Since the jump
792 instruction is at a different address for each opcode, the CPU will make a
793 separate prediction for each of these instructions, which is equivalent to
794 predicting the second opcode of each opcode pair. These predictions have
795 a much better chance to turn out valid, especially in small bytecode loops.
796
797 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000799 and potentially many more instructions (depending on the pipeline width).
800 A correctly predicted branch, however, is nearly free.
801
802 At the time of this writing, the "threaded code" version is up to 15-20%
803 faster than the normal "switch" version, depending on the compiler and the
804 CPU architecture.
805
806 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
807 because it would render the measurements invalid.
808
809
810 NOTE: care must be taken that the compiler doesn't try to "optimize" the
811 indirect jumps by sharing them between all opcodes. Such optimizations
812 can be disabled on gcc by using the -fno-gcse flag (or possibly
813 -fno-crossjumping).
814*/
815
Antoine Pitrou042b1282010-08-13 21:15:58 +0000816#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000817#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000818#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000819#endif
820
Antoine Pitrou042b1282010-08-13 21:15:58 +0000821#ifdef HAVE_COMPUTED_GOTOS
822 #ifndef USE_COMPUTED_GOTOS
823 #define USE_COMPUTED_GOTOS 1
824 #endif
825#else
826 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
827 #error "Computed gotos are not supported on this compiler."
828 #endif
829 #undef USE_COMPUTED_GOTOS
830 #define USE_COMPUTED_GOTOS 0
831#endif
832
833#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000834/* Import the static jump table */
835#include "opcode_targets.h"
836
Antoine Pitroub52ec782009-01-25 16:34:23 +0000837#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -0700838 op: \
839 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000840
Antoine Pitroub52ec782009-01-25 16:34:23 +0000841#ifdef LLTRACE
842#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200844 if (!lltrace && !_Py_TracingPossible(ceval) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300846 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300847 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 } \
849 goto fast_next_opcode; \
850 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000851#else
852#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200854 if (!_Py_TracingPossible(ceval) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300856 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300857 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 } \
859 goto fast_next_opcode; \
860 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000861#endif
862
Victor Stinner09532fe2019-05-10 23:39:09 +0200863#define DISPATCH() \
864 { \
865 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
866 FAST_DISPATCH(); \
867 } \
868 continue; \
869 }
870
Antoine Pitroub52ec782009-01-25 16:34:23 +0000871#else
Benjamin Petersonddd19492018-09-16 22:38:02 -0700872#define TARGET(op) op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000873#define FAST_DISPATCH() goto fast_next_opcode
Victor Stinner09532fe2019-05-10 23:39:09 +0200874#define DISPATCH() continue
Antoine Pitroub52ec782009-01-25 16:34:23 +0000875#endif
876
877
Neal Norwitza81d2202002-07-14 00:27:26 +0000878/* Tuple access macros */
879
880#ifndef Py_DEBUG
881#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
882#else
883#define GETITEM(v, i) PyTuple_GetItem((v), (i))
884#endif
885
Guido van Rossum374a9221991-04-04 10:40:29 +0000886/* Code access macros */
887
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300888/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600889#define INSTR_OFFSET() \
890 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300891#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300892 _Py_CODEUNIT word = *next_instr; \
893 opcode = _Py_OPCODE(word); \
894 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300895 next_instr++; \
896 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300897#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
898#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000899
Raymond Hettingerf606f872003-03-16 03:11:04 +0000900/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 Some opcodes tend to come in pairs thus making it possible to
902 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300903 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000904
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 Verifying the prediction costs a single high-speed test of a register
906 variable against a constant. If the pairing was good, then the
907 processor's own internal branch predication has a high likelihood of
908 success, resulting in a nearly zero-overhead transition to the
909 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300910 including its unpredictable switch-case branch. Combined with the
911 processor's internal branch prediction, a successful PREDICT has the
912 effect of making the two opcodes run as if they were a single new opcode
913 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000914
Georg Brandl86b2fb92008-07-16 03:43:04 +0000915 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 predictions turned-on and interpret the results as if some opcodes
917 had been combined or turn-off predictions so that the opcode frequency
918 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000919
920 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 the CPU to record separate branch prediction information for each
922 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000923
Raymond Hettingerf606f872003-03-16 03:11:04 +0000924*/
925
Antoine Pitrou042b1282010-08-13 21:15:58 +0000926#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000928#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300929#define PREDICT(op) \
930 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300931 _Py_CODEUNIT word = *next_instr; \
932 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300933 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300934 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300935 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300936 goto PRED_##op; \
937 } \
938 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000939#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300940#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000941
Raymond Hettingerf606f872003-03-16 03:11:04 +0000942
Guido van Rossum374a9221991-04-04 10:40:29 +0000943/* Stack manipulation macros */
944
Martin v. Löwis18e16552006-02-15 17:27:45 +0000945/* The stack can grow at most MAXINT deep, as co_nlocals and
946 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000947#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
948#define EMPTY() (STACK_LEVEL() == 0)
949#define TOP() (stack_pointer[-1])
950#define SECOND() (stack_pointer[-2])
951#define THIRD() (stack_pointer[-3])
952#define FOURTH() (stack_pointer[-4])
953#define PEEK(n) (stack_pointer[-(n)])
954#define SET_TOP(v) (stack_pointer[-1] = (v))
955#define SET_SECOND(v) (stack_pointer[-2] = (v))
956#define SET_THIRD(v) (stack_pointer[-3] = (v))
957#define SET_FOURTH(v) (stack_pointer[-4] = (v))
958#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
959#define BASIC_STACKADJ(n) (stack_pointer += n)
960#define BASIC_PUSH(v) (*stack_pointer++ = (v))
961#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000962
Guido van Rossum96a42c81992-01-12 02:29:51 +0000963#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +0200965 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000966 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +0200967#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000968 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +0000969#define STACK_GROW(n) do { \
970 assert(n >= 0); \
971 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +0200972 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +0000973 assert(STACK_LEVEL() <= co->co_stacksize); \
974 } while (0)
975#define STACK_SHRINK(n) do { \
976 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +0200977 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +0000978 (void)(BASIC_STACKADJ(-n)); \
979 assert(STACK_LEVEL() <= co->co_stacksize); \
980 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +0000981#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +0200982 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000983 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000984#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000985#define PUSH(v) BASIC_PUSH(v)
986#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +0000987#define STACK_GROW(n) BASIC_STACKADJ(n)
988#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000989#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000990#endif
991
Guido van Rossum681d79a1995-07-18 14:51:37 +0000992/* Local variable macros */
993
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000995
996/* The SETLOCAL() macro must not DECREF the local variable in-place and
997 then store the new value; it must copy the old value to a temporary
998 value, then store the new value, and then DECREF the temporary value.
999 This is because it is possible that during the DECREF the frame is
1000 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1001 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001003 GETLOCAL(i) = value; \
1004 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001005
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001006
1007#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 while (STACK_LEVEL() > (b)->b_level) { \
1009 PyObject *v = POP(); \
1010 Py_XDECREF(v); \
1011 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001012
1013#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001014 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +01001016 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1018 while (STACK_LEVEL() > (b)->b_level + 3) { \
1019 value = POP(); \
1020 Py_XDECREF(value); \
1021 } \
Mark Shannonae3087c2017-10-22 22:41:51 +01001022 exc_info = tstate->exc_info; \
1023 type = exc_info->exc_type; \
1024 value = exc_info->exc_value; \
1025 traceback = exc_info->exc_traceback; \
1026 exc_info->exc_type = POP(); \
1027 exc_info->exc_value = POP(); \
1028 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 Py_XDECREF(type); \
1030 Py_XDECREF(value); \
1031 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001032 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001033
Inada Naoki91234a12019-06-03 21:30:58 +09001034 /* macros for opcode cache */
1035#define OPCACHE_CHECK() \
1036 do { \
1037 co_opcache = NULL; \
1038 if (co->co_opcache != NULL) { \
1039 unsigned char co_opt_offset = \
1040 co->co_opcache_map[next_instr - first_instr]; \
1041 if (co_opt_offset > 0) { \
1042 assert(co_opt_offset <= co->co_opcache_size); \
1043 co_opcache = &co->co_opcache[co_opt_offset - 1]; \
1044 assert(co_opcache != NULL); \
Inada Naoki91234a12019-06-03 21:30:58 +09001045 } \
1046 } \
1047 } while (0)
1048
1049#if OPCACHE_STATS
1050
1051#define OPCACHE_STAT_GLOBAL_HIT() \
1052 do { \
1053 if (co->co_opcache != NULL) opcache_global_hits++; \
1054 } while (0)
1055
1056#define OPCACHE_STAT_GLOBAL_MISS() \
1057 do { \
1058 if (co->co_opcache != NULL) opcache_global_misses++; \
1059 } while (0)
1060
1061#define OPCACHE_STAT_GLOBAL_OPT() \
1062 do { \
1063 if (co->co_opcache != NULL) opcache_global_opts++; \
1064 } while (0)
1065
1066#else /* OPCACHE_STATS */
1067
1068#define OPCACHE_STAT_GLOBAL_HIT()
1069#define OPCACHE_STAT_GLOBAL_MISS()
1070#define OPCACHE_STAT_GLOBAL_OPT()
1071
1072#endif
1073
Guido van Rossuma027efa1997-05-05 20:56:21 +00001074/* Start of code */
1075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076 /* push frame */
1077 if (Py_EnterRecursiveCall(""))
1078 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001079
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001081
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 if (tstate->use_tracing) {
1083 if (tstate->c_tracefunc != NULL) {
1084 /* tstate->c_tracefunc, if defined, is a
1085 function that will be called on *every* entry
1086 to a code block. Its return value, if not
1087 None, is a function that will be called at
1088 the start of each executed line of code.
1089 (Actually, the function must return itself
1090 in order to continue tracing.) The trace
1091 functions are called with three arguments:
1092 a pointer to the current frame, a string
1093 indicating why the function is called, and
1094 an argument which depends on the situation.
1095 The global trace function is also called
1096 whenever an exception is detected. */
1097 if (call_trace_protected(tstate->c_tracefunc,
1098 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001099 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 /* Trace function raised an error */
1101 goto exit_eval_frame;
1102 }
1103 }
1104 if (tstate->c_profilefunc != NULL) {
1105 /* Similar for c_profilefunc, except it needn't
1106 return itself and isn't called for "line" events */
1107 if (call_trace_protected(tstate->c_profilefunc,
1108 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001109 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001110 /* Profile function raised an error */
1111 goto exit_eval_frame;
1112 }
1113 }
1114 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001115
Łukasz Langaa785c872016-09-09 17:37:37 -07001116 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1117 dtrace_function_entry(f);
1118
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 co = f->f_code;
1120 names = co->co_names;
1121 consts = co->co_consts;
1122 fastlocals = f->f_localsplus;
1123 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001124 assert(PyBytes_Check(co->co_code));
1125 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001126 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1127 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1128 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001129 /*
1130 f->f_lasti refers to the index of the last instruction,
1131 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001132
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001133 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001134 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001135
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001136 When the PREDICT() macros are enabled, some opcode pairs follow in
1137 direct succession without updating f->f_lasti. A successful
1138 prediction effectively links the two codes together as if they
1139 were a single new opcode; accordingly,f->f_lasti will point to
1140 the first code in the pair (for instance, GET_ITER followed by
1141 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001142 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001144 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001145 next_instr = first_instr;
1146 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001147 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1148 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001149 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 stack_pointer = f->f_stacktop;
1151 assert(stack_pointer != NULL);
1152 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001153 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001154
Inada Naoki91234a12019-06-03 21:30:58 +09001155 if (co->co_opcache_flag < OPCACHE_MIN_RUNS) {
1156 co->co_opcache_flag++;
1157 if (co->co_opcache_flag == OPCACHE_MIN_RUNS) {
1158 if (_PyCode_InitOpcache(co) < 0) {
1159 return NULL;
1160 }
1161#if OPCACHE_STATS
1162 opcache_code_objects_extra_mem +=
1163 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1164 sizeof(_PyOpcache) * co->co_opcache_size;
1165 opcache_code_objects++;
1166#endif
1167 }
1168 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001169
Tim Peters5ca576e2001-06-18 22:08:13 +00001170#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001171 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001172#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001173
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001174 if (throwflag) /* support for generator.throw() */
1175 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001176
Victor Stinnerace47d72013-07-18 01:41:08 +02001177#ifdef Py_DEBUG
1178 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001179 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001180 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001181 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001182#endif
1183
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001184main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1187 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001188 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001189
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 /* Do periodic things. Doing this every time through
1191 the loop would add too much overhead, so we do it
1192 only every Nth instruction. We also do it if
1193 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1194 event needs attention (e.g. a signal handler or
1195 async I/O handler); see Py_AddPendingCall() and
1196 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001197
Eric Snow7bda9de2019-03-08 17:25:54 -07001198 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001199 opcode = _Py_OPCODE(*next_instr);
1200 if (opcode == SETUP_FINALLY ||
1201 opcode == SETUP_WITH ||
1202 opcode == BEFORE_ASYNC_WITH ||
1203 opcode == YIELD_FROM) {
1204 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001205 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001206 - If we're about to enter the 'with:'. It will prevent
1207 emitting a resource warning in the common idiom
1208 'with open(path) as file:'.
1209 - If we're about to enter the 'async with:'.
1210 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001211 *very* useful, but might help in some cases and it's
1212 traditional)
1213 - If we're resuming a chain of nested 'yield from' or
1214 'await' calls, then each frame is parked with YIELD_FROM
1215 as its next opcode. If the user hit control-C we want to
1216 wait until we've reached the innermost frame before
1217 running the signal handler and raising KeyboardInterrupt
1218 (see bpo-30039).
1219 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 goto fast_next_opcode;
1221 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001222
Victor Stinnere225beb2019-06-03 18:14:24 +02001223 if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
Victor Stinner09532fe2019-05-10 23:39:09 +02001224 if (handle_signals(runtime) != 0) {
Eric Snowfdf282d2019-01-11 14:26:55 -07001225 goto error;
1226 }
1227 }
Victor Stinnere225beb2019-06-03 18:14:24 +02001228 if (_Py_atomic_load_relaxed(&ceval->pending.calls_to_do)) {
1229 if (make_pending_calls(runtime) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001230 goto error;
Eric Snowfdf282d2019-01-11 14:26:55 -07001231 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001233
Victor Stinnere225beb2019-06-03 18:14:24 +02001234 if (_Py_atomic_load_relaxed(&ceval->gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 /* Give another thread a chance */
Victor Stinner09532fe2019-05-10 23:39:09 +02001236 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 Py_FatalError("ceval: tstate mix-up");
Victor Stinner09532fe2019-05-10 23:39:09 +02001238 }
Victor Stinnere225beb2019-06-03 18:14:24 +02001239 drop_gil(ceval, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240
1241 /* Other threads may run now */
1242
Victor Stinnere225beb2019-06-03 18:14:24 +02001243 take_gil(ceval, tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001244
1245 /* Check if we should make a quick exit. */
Eric Snow396e0a82019-05-31 21:16:47 -06001246 exit_thread_if_finalizing(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001247
Victor Stinner09532fe2019-05-10 23:39:09 +02001248 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 Py_FatalError("ceval: orphan tstate");
Victor Stinner09532fe2019-05-10 23:39:09 +02001250 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 }
1252 /* Check for asynchronous exceptions. */
1253 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001254 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 tstate->async_exc = NULL;
Victor Stinnere225beb2019-06-03 18:14:24 +02001256 UNSIGNAL_ASYNC_EXC(ceval);
Victor Stinner438a12d2019-05-24 17:01:38 +02001257 _PyErr_SetNone(tstate, exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001258 Py_DECREF(exc);
1259 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 }
1261 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 fast_next_opcode:
1264 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001265
Łukasz Langaa785c872016-09-09 17:37:37 -07001266 if (PyDTrace_LINE_ENABLED())
1267 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001270
Victor Stinnere225beb2019-06-03 18:14:24 +02001271 if (_Py_TracingPossible(ceval) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001272 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001273 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 /* see maybe_call_line_trace
1275 for expository comments */
1276 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 err = maybe_call_line_trace(tstate->c_tracefunc,
1279 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001280 tstate, f,
1281 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 /* Reload possibly changed frame fields */
1283 JUMPTO(f->f_lasti);
1284 if (f->f_stacktop != NULL) {
1285 stack_pointer = f->f_stacktop;
1286 f->f_stacktop = NULL;
1287 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001288 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001290 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001294
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001295 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001296 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001297#ifdef DYNAMIC_EXECUTION_PROFILE
1298#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 dxpairs[lastopcode][opcode]++;
1300 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001301#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001302 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001303#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001304
Guido van Rossum96a42c81992-01-12 02:29:51 +00001305#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 if (lltrace) {
1309 if (HAS_ARG(opcode)) {
1310 printf("%d: %d, %d\n",
1311 f->f_lasti, opcode, oparg);
1312 }
1313 else {
1314 printf("%d: %d\n",
1315 f->f_lasti, opcode);
1316 }
1317 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001318#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001323 It is essential that any operation that fails must goto error
1324 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001325
Benjamin Petersonddd19492018-09-16 22:38:02 -07001326 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001328 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001329
Benjamin Petersonddd19492018-09-16 22:38:02 -07001330 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001331 PyObject *value = GETLOCAL(oparg);
1332 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001333 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001334 UNBOUNDLOCAL_ERROR_MSG,
1335 PyTuple_GetItem(co->co_varnames, oparg));
1336 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001338 Py_INCREF(value);
1339 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001341 }
1342
Benjamin Petersonddd19492018-09-16 22:38:02 -07001343 case TARGET(LOAD_CONST): {
1344 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001345 PyObject *value = GETITEM(consts, oparg);
1346 Py_INCREF(value);
1347 PUSH(value);
1348 FAST_DISPATCH();
1349 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001350
Benjamin Petersonddd19492018-09-16 22:38:02 -07001351 case TARGET(STORE_FAST): {
1352 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001353 PyObject *value = POP();
1354 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001356 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001357
Benjamin Petersonddd19492018-09-16 22:38:02 -07001358 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001359 PyObject *value = POP();
1360 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001361 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001362 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001363
Benjamin Petersonddd19492018-09-16 22:38:02 -07001364 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001365 PyObject *top = TOP();
1366 PyObject *second = SECOND();
1367 SET_TOP(second);
1368 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001370 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001371
Benjamin Petersonddd19492018-09-16 22:38:02 -07001372 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001373 PyObject *top = TOP();
1374 PyObject *second = SECOND();
1375 PyObject *third = THIRD();
1376 SET_TOP(second);
1377 SET_SECOND(third);
1378 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001379 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001380 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001381
Benjamin Petersonddd19492018-09-16 22:38:02 -07001382 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001383 PyObject *top = TOP();
1384 PyObject *second = SECOND();
1385 PyObject *third = THIRD();
1386 PyObject *fourth = FOURTH();
1387 SET_TOP(second);
1388 SET_SECOND(third);
1389 SET_THIRD(fourth);
1390 SET_FOURTH(top);
1391 FAST_DISPATCH();
1392 }
1393
Benjamin Petersonddd19492018-09-16 22:38:02 -07001394 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001395 PyObject *top = TOP();
1396 Py_INCREF(top);
1397 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001399 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001400
Benjamin Petersonddd19492018-09-16 22:38:02 -07001401 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001402 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001403 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001404 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001405 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001406 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001407 SET_TOP(top);
1408 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001409 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001410 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001411
Benjamin Petersonddd19492018-09-16 22:38:02 -07001412 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001413 PyObject *value = TOP();
1414 PyObject *res = PyNumber_Positive(value);
1415 Py_DECREF(value);
1416 SET_TOP(res);
1417 if (res == NULL)
1418 goto error;
1419 DISPATCH();
1420 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001421
Benjamin Petersonddd19492018-09-16 22:38:02 -07001422 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001423 PyObject *value = TOP();
1424 PyObject *res = PyNumber_Negative(value);
1425 Py_DECREF(value);
1426 SET_TOP(res);
1427 if (res == NULL)
1428 goto error;
1429 DISPATCH();
1430 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001431
Benjamin Petersonddd19492018-09-16 22:38:02 -07001432 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001433 PyObject *value = TOP();
1434 int err = PyObject_IsTrue(value);
1435 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 if (err == 0) {
1437 Py_INCREF(Py_True);
1438 SET_TOP(Py_True);
1439 DISPATCH();
1440 }
1441 else if (err > 0) {
1442 Py_INCREF(Py_False);
1443 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001444 DISPATCH();
1445 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001446 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001447 goto error;
1448 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001449
Benjamin Petersonddd19492018-09-16 22:38:02 -07001450 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001451 PyObject *value = TOP();
1452 PyObject *res = PyNumber_Invert(value);
1453 Py_DECREF(value);
1454 SET_TOP(res);
1455 if (res == NULL)
1456 goto error;
1457 DISPATCH();
1458 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001459
Benjamin Petersonddd19492018-09-16 22:38:02 -07001460 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001461 PyObject *exp = POP();
1462 PyObject *base = TOP();
1463 PyObject *res = PyNumber_Power(base, exp, Py_None);
1464 Py_DECREF(base);
1465 Py_DECREF(exp);
1466 SET_TOP(res);
1467 if (res == NULL)
1468 goto error;
1469 DISPATCH();
1470 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001471
Benjamin Petersonddd19492018-09-16 22:38:02 -07001472 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001473 PyObject *right = POP();
1474 PyObject *left = TOP();
1475 PyObject *res = PyNumber_Multiply(left, right);
1476 Py_DECREF(left);
1477 Py_DECREF(right);
1478 SET_TOP(res);
1479 if (res == NULL)
1480 goto error;
1481 DISPATCH();
1482 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001483
Benjamin Petersonddd19492018-09-16 22:38:02 -07001484 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001485 PyObject *right = POP();
1486 PyObject *left = TOP();
1487 PyObject *res = PyNumber_MatrixMultiply(left, right);
1488 Py_DECREF(left);
1489 Py_DECREF(right);
1490 SET_TOP(res);
1491 if (res == NULL)
1492 goto error;
1493 DISPATCH();
1494 }
1495
Benjamin Petersonddd19492018-09-16 22:38:02 -07001496 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001497 PyObject *divisor = POP();
1498 PyObject *dividend = TOP();
1499 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1500 Py_DECREF(dividend);
1501 Py_DECREF(divisor);
1502 SET_TOP(quotient);
1503 if (quotient == NULL)
1504 goto error;
1505 DISPATCH();
1506 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001507
Benjamin Petersonddd19492018-09-16 22:38:02 -07001508 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001509 PyObject *divisor = POP();
1510 PyObject *dividend = TOP();
1511 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1512 Py_DECREF(dividend);
1513 Py_DECREF(divisor);
1514 SET_TOP(quotient);
1515 if (quotient == NULL)
1516 goto error;
1517 DISPATCH();
1518 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001519
Benjamin Petersonddd19492018-09-16 22:38:02 -07001520 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001521 PyObject *divisor = POP();
1522 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001523 PyObject *res;
1524 if (PyUnicode_CheckExact(dividend) && (
1525 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1526 // fast path; string formatting, but not if the RHS is a str subclass
1527 // (see issue28598)
1528 res = PyUnicode_Format(dividend, divisor);
1529 } else {
1530 res = PyNumber_Remainder(dividend, divisor);
1531 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001532 Py_DECREF(divisor);
1533 Py_DECREF(dividend);
1534 SET_TOP(res);
1535 if (res == NULL)
1536 goto error;
1537 DISPATCH();
1538 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001539
Benjamin Petersonddd19492018-09-16 22:38:02 -07001540 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001541 PyObject *right = POP();
1542 PyObject *left = TOP();
1543 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001544 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1545 CPython using bytecode, it is simply worthless.
1546 See http://bugs.python.org/issue21955 and
1547 http://bugs.python.org/issue10044 for the discussion. In short,
1548 no patch shown any impact on a realistic benchmark, only a minor
1549 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001550 if (PyUnicode_CheckExact(left) &&
1551 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001552 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001553 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001554 }
1555 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001556 sum = PyNumber_Add(left, right);
1557 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001558 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001559 Py_DECREF(right);
1560 SET_TOP(sum);
1561 if (sum == NULL)
1562 goto error;
1563 DISPATCH();
1564 }
1565
Benjamin Petersonddd19492018-09-16 22:38:02 -07001566 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001567 PyObject *right = POP();
1568 PyObject *left = TOP();
1569 PyObject *diff = PyNumber_Subtract(left, right);
1570 Py_DECREF(right);
1571 Py_DECREF(left);
1572 SET_TOP(diff);
1573 if (diff == NULL)
1574 goto error;
1575 DISPATCH();
1576 }
1577
Benjamin Petersonddd19492018-09-16 22:38:02 -07001578 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001579 PyObject *sub = POP();
1580 PyObject *container = TOP();
1581 PyObject *res = PyObject_GetItem(container, sub);
1582 Py_DECREF(container);
1583 Py_DECREF(sub);
1584 SET_TOP(res);
1585 if (res == NULL)
1586 goto error;
1587 DISPATCH();
1588 }
1589
Benjamin Petersonddd19492018-09-16 22:38:02 -07001590 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001591 PyObject *right = POP();
1592 PyObject *left = TOP();
1593 PyObject *res = PyNumber_Lshift(left, right);
1594 Py_DECREF(left);
1595 Py_DECREF(right);
1596 SET_TOP(res);
1597 if (res == NULL)
1598 goto error;
1599 DISPATCH();
1600 }
1601
Benjamin Petersonddd19492018-09-16 22:38:02 -07001602 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001603 PyObject *right = POP();
1604 PyObject *left = TOP();
1605 PyObject *res = PyNumber_Rshift(left, right);
1606 Py_DECREF(left);
1607 Py_DECREF(right);
1608 SET_TOP(res);
1609 if (res == NULL)
1610 goto error;
1611 DISPATCH();
1612 }
1613
Benjamin Petersonddd19492018-09-16 22:38:02 -07001614 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001615 PyObject *right = POP();
1616 PyObject *left = TOP();
1617 PyObject *res = PyNumber_And(left, right);
1618 Py_DECREF(left);
1619 Py_DECREF(right);
1620 SET_TOP(res);
1621 if (res == NULL)
1622 goto error;
1623 DISPATCH();
1624 }
1625
Benjamin Petersonddd19492018-09-16 22:38:02 -07001626 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001627 PyObject *right = POP();
1628 PyObject *left = TOP();
1629 PyObject *res = PyNumber_Xor(left, right);
1630 Py_DECREF(left);
1631 Py_DECREF(right);
1632 SET_TOP(res);
1633 if (res == NULL)
1634 goto error;
1635 DISPATCH();
1636 }
1637
Benjamin Petersonddd19492018-09-16 22:38:02 -07001638 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001639 PyObject *right = POP();
1640 PyObject *left = TOP();
1641 PyObject *res = PyNumber_Or(left, right);
1642 Py_DECREF(left);
1643 Py_DECREF(right);
1644 SET_TOP(res);
1645 if (res == NULL)
1646 goto error;
1647 DISPATCH();
1648 }
1649
Benjamin Petersonddd19492018-09-16 22:38:02 -07001650 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001651 PyObject *v = POP();
1652 PyObject *list = PEEK(oparg);
1653 int err;
1654 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001655 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001656 if (err != 0)
1657 goto error;
1658 PREDICT(JUMP_ABSOLUTE);
1659 DISPATCH();
1660 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001661
Benjamin Petersonddd19492018-09-16 22:38:02 -07001662 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001663 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001664 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001665 int err;
1666 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001667 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001668 if (err != 0)
1669 goto error;
1670 PREDICT(JUMP_ABSOLUTE);
1671 DISPATCH();
1672 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001673
Benjamin Petersonddd19492018-09-16 22:38:02 -07001674 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001675 PyObject *exp = POP();
1676 PyObject *base = TOP();
1677 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1678 Py_DECREF(base);
1679 Py_DECREF(exp);
1680 SET_TOP(res);
1681 if (res == NULL)
1682 goto error;
1683 DISPATCH();
1684 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001685
Benjamin Petersonddd19492018-09-16 22:38:02 -07001686 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001687 PyObject *right = POP();
1688 PyObject *left = TOP();
1689 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1690 Py_DECREF(left);
1691 Py_DECREF(right);
1692 SET_TOP(res);
1693 if (res == NULL)
1694 goto error;
1695 DISPATCH();
1696 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001697
Benjamin Petersonddd19492018-09-16 22:38:02 -07001698 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001699 PyObject *right = POP();
1700 PyObject *left = TOP();
1701 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1702 Py_DECREF(left);
1703 Py_DECREF(right);
1704 SET_TOP(res);
1705 if (res == NULL)
1706 goto error;
1707 DISPATCH();
1708 }
1709
Benjamin Petersonddd19492018-09-16 22:38:02 -07001710 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001711 PyObject *divisor = POP();
1712 PyObject *dividend = TOP();
1713 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1714 Py_DECREF(dividend);
1715 Py_DECREF(divisor);
1716 SET_TOP(quotient);
1717 if (quotient == NULL)
1718 goto error;
1719 DISPATCH();
1720 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001721
Benjamin Petersonddd19492018-09-16 22:38:02 -07001722 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001723 PyObject *divisor = POP();
1724 PyObject *dividend = TOP();
1725 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1726 Py_DECREF(dividend);
1727 Py_DECREF(divisor);
1728 SET_TOP(quotient);
1729 if (quotient == NULL)
1730 goto error;
1731 DISPATCH();
1732 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001733
Benjamin Petersonddd19492018-09-16 22:38:02 -07001734 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001735 PyObject *right = POP();
1736 PyObject *left = TOP();
1737 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1738 Py_DECREF(left);
1739 Py_DECREF(right);
1740 SET_TOP(mod);
1741 if (mod == NULL)
1742 goto error;
1743 DISPATCH();
1744 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001745
Benjamin Petersonddd19492018-09-16 22:38:02 -07001746 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001747 PyObject *right = POP();
1748 PyObject *left = TOP();
1749 PyObject *sum;
1750 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001751 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001752 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001753 }
1754 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001755 sum = PyNumber_InPlaceAdd(left, right);
1756 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001757 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001758 Py_DECREF(right);
1759 SET_TOP(sum);
1760 if (sum == NULL)
1761 goto error;
1762 DISPATCH();
1763 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001764
Benjamin Petersonddd19492018-09-16 22:38:02 -07001765 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001766 PyObject *right = POP();
1767 PyObject *left = TOP();
1768 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1769 Py_DECREF(left);
1770 Py_DECREF(right);
1771 SET_TOP(diff);
1772 if (diff == NULL)
1773 goto error;
1774 DISPATCH();
1775 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001776
Benjamin Petersonddd19492018-09-16 22:38:02 -07001777 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001778 PyObject *right = POP();
1779 PyObject *left = TOP();
1780 PyObject *res = PyNumber_InPlaceLshift(left, right);
1781 Py_DECREF(left);
1782 Py_DECREF(right);
1783 SET_TOP(res);
1784 if (res == NULL)
1785 goto error;
1786 DISPATCH();
1787 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001788
Benjamin Petersonddd19492018-09-16 22:38:02 -07001789 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001790 PyObject *right = POP();
1791 PyObject *left = TOP();
1792 PyObject *res = PyNumber_InPlaceRshift(left, right);
1793 Py_DECREF(left);
1794 Py_DECREF(right);
1795 SET_TOP(res);
1796 if (res == NULL)
1797 goto error;
1798 DISPATCH();
1799 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001800
Benjamin Petersonddd19492018-09-16 22:38:02 -07001801 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001802 PyObject *right = POP();
1803 PyObject *left = TOP();
1804 PyObject *res = PyNumber_InPlaceAnd(left, right);
1805 Py_DECREF(left);
1806 Py_DECREF(right);
1807 SET_TOP(res);
1808 if (res == NULL)
1809 goto error;
1810 DISPATCH();
1811 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001812
Benjamin Petersonddd19492018-09-16 22:38:02 -07001813 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001814 PyObject *right = POP();
1815 PyObject *left = TOP();
1816 PyObject *res = PyNumber_InPlaceXor(left, right);
1817 Py_DECREF(left);
1818 Py_DECREF(right);
1819 SET_TOP(res);
1820 if (res == NULL)
1821 goto error;
1822 DISPATCH();
1823 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001824
Benjamin Petersonddd19492018-09-16 22:38:02 -07001825 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001826 PyObject *right = POP();
1827 PyObject *left = TOP();
1828 PyObject *res = PyNumber_InPlaceOr(left, right);
1829 Py_DECREF(left);
1830 Py_DECREF(right);
1831 SET_TOP(res);
1832 if (res == NULL)
1833 goto error;
1834 DISPATCH();
1835 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001836
Benjamin Petersonddd19492018-09-16 22:38:02 -07001837 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001838 PyObject *sub = TOP();
1839 PyObject *container = SECOND();
1840 PyObject *v = THIRD();
1841 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001842 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001843 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001844 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001845 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001846 Py_DECREF(container);
1847 Py_DECREF(sub);
1848 if (err != 0)
1849 goto error;
1850 DISPATCH();
1851 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001852
Benjamin Petersonddd19492018-09-16 22:38:02 -07001853 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001854 PyObject *sub = TOP();
1855 PyObject *container = SECOND();
1856 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001857 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00001858 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001859 err = PyObject_DelItem(container, sub);
1860 Py_DECREF(container);
1861 Py_DECREF(sub);
1862 if (err != 0)
1863 goto error;
1864 DISPATCH();
1865 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001866
Benjamin Petersonddd19492018-09-16 22:38:02 -07001867 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01001868 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001869 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001870 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001871 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001872 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001873 _PyErr_SetString(tstate, PyExc_RuntimeError,
1874 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001875 Py_DECREF(value);
1876 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001877 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001878 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001879 Py_DECREF(value);
1880 if (res == NULL)
1881 goto error;
1882 Py_DECREF(res);
1883 DISPATCH();
1884 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001885
Benjamin Petersonddd19492018-09-16 22:38:02 -07001886 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001887 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 switch (oparg) {
1889 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001890 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001891 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001893 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001894 /* fall through */
1895 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02001896 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001897 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001898 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001899 break;
1900 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02001901 _PyErr_SetString(tstate, PyExc_SystemError,
1902 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903 break;
1904 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001905 goto error;
1906 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001907
Benjamin Petersonddd19492018-09-16 22:38:02 -07001908 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001909 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001910 assert(f->f_iblock == 0);
Pablo Galindof00828a2019-05-09 16:52:02 +01001911 goto exit_returning;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001912 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001913
Benjamin Petersonddd19492018-09-16 22:38:02 -07001914 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001915 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001916 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001917 PyObject *obj = TOP();
1918 PyTypeObject *type = Py_TYPE(obj);
1919
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001920 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001921 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001922 }
Yury Selivanov75445082015-05-11 22:57:16 -04001923
1924 if (getter != NULL) {
1925 iter = (*getter)(obj);
1926 Py_DECREF(obj);
1927 if (iter == NULL) {
1928 SET_TOP(NULL);
1929 goto error;
1930 }
1931 }
1932 else {
1933 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02001934 _PyErr_Format(tstate, PyExc_TypeError,
1935 "'async for' requires an object with "
1936 "__aiter__ method, got %.100s",
1937 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001938 Py_DECREF(obj);
1939 goto error;
1940 }
1941
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001942 if (Py_TYPE(iter)->tp_as_async == NULL ||
1943 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001944
Yury Selivanov398ff912017-03-02 22:20:00 -05001945 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02001946 _PyErr_Format(tstate, PyExc_TypeError,
1947 "'async for' received an object from __aiter__ "
1948 "that does not implement __anext__: %.100s",
1949 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001950 Py_DECREF(iter);
1951 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001952 }
1953
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001954 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001955 DISPATCH();
1956 }
1957
Benjamin Petersonddd19492018-09-16 22:38:02 -07001958 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001959 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001960 PyObject *next_iter = NULL;
1961 PyObject *awaitable = NULL;
1962 PyObject *aiter = TOP();
1963 PyTypeObject *type = Py_TYPE(aiter);
1964
Yury Selivanoveb636452016-09-08 22:01:51 -07001965 if (PyAsyncGen_CheckExact(aiter)) {
1966 awaitable = type->tp_as_async->am_anext(aiter);
1967 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001968 goto error;
1969 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001970 } else {
1971 if (type->tp_as_async != NULL){
1972 getter = type->tp_as_async->am_anext;
1973 }
Yury Selivanov75445082015-05-11 22:57:16 -04001974
Yury Selivanoveb636452016-09-08 22:01:51 -07001975 if (getter != NULL) {
1976 next_iter = (*getter)(aiter);
1977 if (next_iter == NULL) {
1978 goto error;
1979 }
1980 }
1981 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02001982 _PyErr_Format(tstate, PyExc_TypeError,
1983 "'async for' requires an iterator with "
1984 "__anext__ method, got %.100s",
1985 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07001986 goto error;
1987 }
Yury Selivanov75445082015-05-11 22:57:16 -04001988
Yury Selivanoveb636452016-09-08 22:01:51 -07001989 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1990 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001991 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001992 PyExc_TypeError,
1993 "'async for' received an invalid object "
1994 "from __anext__: %.100s",
1995 Py_TYPE(next_iter)->tp_name);
1996
1997 Py_DECREF(next_iter);
1998 goto error;
1999 } else {
2000 Py_DECREF(next_iter);
2001 }
2002 }
Yury Selivanov75445082015-05-11 22:57:16 -04002003
2004 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002005 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002006 DISPATCH();
2007 }
2008
Benjamin Petersonddd19492018-09-16 22:38:02 -07002009 case TARGET(GET_AWAITABLE): {
2010 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002011 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002012 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002013
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002014 if (iter == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002015 format_awaitable_error(tstate, Py_TYPE(iterable),
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002016 _Py_OPCODE(next_instr[-2]));
2017 }
2018
Yury Selivanov75445082015-05-11 22:57:16 -04002019 Py_DECREF(iterable);
2020
Yury Selivanovc724bae2016-03-02 11:30:46 -05002021 if (iter != NULL && PyCoro_CheckExact(iter)) {
2022 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2023 if (yf != NULL) {
2024 /* `iter` is a coroutine object that is being
2025 awaited, `yf` is a pointer to the current awaitable
2026 being awaited on. */
2027 Py_DECREF(yf);
2028 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002029 _PyErr_SetString(tstate, PyExc_RuntimeError,
2030 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002031 /* The code below jumps to `error` if `iter` is NULL. */
2032 }
2033 }
2034
Yury Selivanov75445082015-05-11 22:57:16 -04002035 SET_TOP(iter); /* Even if it's NULL */
2036
2037 if (iter == NULL) {
2038 goto error;
2039 }
2040
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002041 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002042 DISPATCH();
2043 }
2044
Benjamin Petersonddd19492018-09-16 22:38:02 -07002045 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002046 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002047 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002048 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002049 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
2050 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002051 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002052 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002053 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002054 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002055 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002056 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002057 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002058 Py_DECREF(v);
2059 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002060 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002061 if (tstate->c_tracefunc != NULL
Victor Stinner438a12d2019-05-24 17:01:38 +02002062 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002063 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002064 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002065 if (err < 0)
2066 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002067 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002068 SET_TOP(val);
2069 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002070 }
Martin Panter95f53c12016-07-18 08:23:26 +00002071 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002072 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002073 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002074 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002075 f->f_lasti -= sizeof(_Py_CODEUNIT);
Pablo Galindof00828a2019-05-09 16:52:02 +01002076 goto exit_yielding;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002077 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002078
Benjamin Petersonddd19492018-09-16 22:38:02 -07002079 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002080 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002081
2082 if (co->co_flags & CO_ASYNC_GENERATOR) {
2083 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2084 Py_DECREF(retval);
2085 if (w == NULL) {
2086 retval = NULL;
2087 goto error;
2088 }
2089 retval = w;
2090 }
2091
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002092 f->f_stacktop = stack_pointer;
Pablo Galindof00828a2019-05-09 16:52:02 +01002093 goto exit_yielding;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002094 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002095
Benjamin Petersonddd19492018-09-16 22:38:02 -07002096 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002097 PyObject *type, *value, *traceback;
2098 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002099 PyTryBlock *b = PyFrame_BlockPop(f);
2100 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002101 _PyErr_SetString(tstate, PyExc_SystemError,
2102 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002103 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002104 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002105 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2106 STACK_LEVEL() <= (b)->b_level + 4);
2107 exc_info = tstate->exc_info;
2108 type = exc_info->exc_type;
2109 value = exc_info->exc_value;
2110 traceback = exc_info->exc_traceback;
2111 exc_info->exc_type = POP();
2112 exc_info->exc_value = POP();
2113 exc_info->exc_traceback = POP();
2114 Py_XDECREF(type);
2115 Py_XDECREF(value);
2116 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002117 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002118 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002119
Benjamin Petersonddd19492018-09-16 22:38:02 -07002120 case TARGET(POP_BLOCK): {
2121 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002122 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002123 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002124 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002125
Benjamin Petersonddd19492018-09-16 22:38:02 -07002126 case TARGET(POP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002127 /* If oparg is 0 at the top of the stack are 1 or 6 values:
2128 Either:
2129 - TOP = NULL or an integer
2130 or:
2131 - (TOP, SECOND, THIRD) = exc_info()
2132 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2133
2134 If oparg is 1 the value for 'return' was additionally pushed
2135 at the top of the stack.
2136 */
2137 PyObject *res = NULL;
2138 if (oparg) {
2139 res = POP();
2140 }
2141 PyObject *exc = POP();
2142 if (exc == NULL || PyLong_CheckExact(exc)) {
2143 Py_XDECREF(exc);
2144 }
2145 else {
2146 Py_DECREF(exc);
2147 Py_DECREF(POP());
2148 Py_DECREF(POP());
2149
2150 PyObject *type, *value, *traceback;
2151 _PyErr_StackItem *exc_info;
2152 PyTryBlock *b = PyFrame_BlockPop(f);
2153 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002154 _PyErr_SetString(tstate, PyExc_SystemError,
2155 "popped block is not an except handler");
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002156 Py_XDECREF(res);
2157 goto error;
2158 }
2159 assert(STACK_LEVEL() == (b)->b_level + 3);
2160 exc_info = tstate->exc_info;
2161 type = exc_info->exc_type;
2162 value = exc_info->exc_value;
2163 traceback = exc_info->exc_traceback;
2164 exc_info->exc_type = POP();
2165 exc_info->exc_value = POP();
2166 exc_info->exc_traceback = POP();
2167 Py_XDECREF(type);
2168 Py_XDECREF(value);
2169 Py_XDECREF(traceback);
2170 }
2171 if (oparg) {
2172 PUSH(res);
2173 }
2174 DISPATCH();
2175 }
2176
Benjamin Petersonddd19492018-09-16 22:38:02 -07002177 case TARGET(CALL_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002178 PyObject *ret = PyLong_FromLong(INSTR_OFFSET());
2179 if (ret == NULL) {
2180 goto error;
2181 }
2182 PUSH(ret);
2183 JUMPBY(oparg);
2184 FAST_DISPATCH();
2185 }
2186
Benjamin Petersonddd19492018-09-16 22:38:02 -07002187 case TARGET(BEGIN_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002188 /* Push NULL onto the stack for using it in END_FINALLY,
2189 POP_FINALLY, WITH_CLEANUP_START and WITH_CLEANUP_FINISH.
2190 */
2191 PUSH(NULL);
2192 FAST_DISPATCH();
2193 }
2194
Benjamin Petersonddd19492018-09-16 22:38:02 -07002195 case TARGET(END_FINALLY): {
2196 PREDICTED(END_FINALLY);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002197 /* At the top of the stack are 1 or 6 values:
2198 Either:
2199 - TOP = NULL or an integer
2200 or:
2201 - (TOP, SECOND, THIRD) = exc_info()
2202 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2203 */
2204 PyObject *exc = POP();
2205 if (exc == NULL) {
2206 FAST_DISPATCH();
2207 }
2208 else if (PyLong_CheckExact(exc)) {
2209 int ret = _PyLong_AsInt(exc);
2210 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02002211 if (ret == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002212 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002213 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002214 JUMPTO(ret);
2215 FAST_DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002216 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002217 else {
2218 assert(PyExceptionClass_Check(exc));
2219 PyObject *val = POP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002220 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002221 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002222 goto exception_unwind;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002223 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002224 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002225
Benjamin Petersonddd19492018-09-16 22:38:02 -07002226 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002227 PyObject *exc = POP();
2228 assert(PyExceptionClass_Check(exc));
2229 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2230 PyTryBlock *b = PyFrame_BlockPop(f);
2231 assert(b->b_type == EXCEPT_HANDLER);
2232 Py_DECREF(exc);
2233 UNWIND_EXCEPT_HANDLER(b);
2234 Py_DECREF(POP());
2235 JUMPBY(oparg);
2236 FAST_DISPATCH();
2237 }
2238 else {
2239 PyObject *val = POP();
2240 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002241 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002242 goto exception_unwind;
2243 }
2244 }
2245
Benjamin Petersonddd19492018-09-16 22:38:02 -07002246 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002247 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002248
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002249 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002250 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002251 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002252 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002253 if (!_PyErr_Occurred(tstate)) {
2254 _PyErr_SetString(tstate, PyExc_NameError,
2255 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002256 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002257 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002258 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002259 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002260 }
2261 else {
2262 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2263 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002264 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002265 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2266 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002267 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2268 _PyErr_SetString(tstate, PyExc_NameError,
2269 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002270 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002271 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002272 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002273 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002274 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002275 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002276
Benjamin Petersonddd19492018-09-16 22:38:02 -07002277 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002278 PyObject *name = GETITEM(names, oparg);
2279 PyObject *v = POP();
2280 PyObject *ns = f->f_locals;
2281 int err;
2282 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002283 _PyErr_Format(tstate, PyExc_SystemError,
2284 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002285 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002286 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002287 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002288 if (PyDict_CheckExact(ns))
2289 err = PyDict_SetItem(ns, name, v);
2290 else
2291 err = PyObject_SetItem(ns, name, v);
2292 Py_DECREF(v);
2293 if (err != 0)
2294 goto error;
2295 DISPATCH();
2296 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002297
Benjamin Petersonddd19492018-09-16 22:38:02 -07002298 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002299 PyObject *name = GETITEM(names, oparg);
2300 PyObject *ns = f->f_locals;
2301 int err;
2302 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002303 _PyErr_Format(tstate, PyExc_SystemError,
2304 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002305 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002306 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002307 err = PyObject_DelItem(ns, name);
2308 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002309 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002310 NAME_ERROR_MSG,
2311 name);
2312 goto error;
2313 }
2314 DISPATCH();
2315 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002316
Benjamin Petersonddd19492018-09-16 22:38:02 -07002317 case TARGET(UNPACK_SEQUENCE): {
2318 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002319 PyObject *seq = POP(), *item, **items;
2320 if (PyTuple_CheckExact(seq) &&
2321 PyTuple_GET_SIZE(seq) == oparg) {
2322 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002323 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002324 item = items[oparg];
2325 Py_INCREF(item);
2326 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002327 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002328 } else if (PyList_CheckExact(seq) &&
2329 PyList_GET_SIZE(seq) == oparg) {
2330 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002331 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002332 item = items[oparg];
2333 Py_INCREF(item);
2334 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002335 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002336 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002337 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002338 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002339 } else {
2340 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002341 Py_DECREF(seq);
2342 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002343 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002344 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002345 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002346 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002347
Benjamin Petersonddd19492018-09-16 22:38:02 -07002348 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002349 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2350 PyObject *seq = POP();
2351
Victor Stinner438a12d2019-05-24 17:01:38 +02002352 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002353 stack_pointer + totalargs)) {
2354 stack_pointer += totalargs;
2355 } else {
2356 Py_DECREF(seq);
2357 goto error;
2358 }
2359 Py_DECREF(seq);
2360 DISPATCH();
2361 }
2362
Benjamin Petersonddd19492018-09-16 22:38:02 -07002363 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002364 PyObject *name = GETITEM(names, oparg);
2365 PyObject *owner = TOP();
2366 PyObject *v = SECOND();
2367 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002368 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002369 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002370 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002371 Py_DECREF(owner);
2372 if (err != 0)
2373 goto error;
2374 DISPATCH();
2375 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002376
Benjamin Petersonddd19492018-09-16 22:38:02 -07002377 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002378 PyObject *name = GETITEM(names, oparg);
2379 PyObject *owner = POP();
2380 int err;
2381 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2382 Py_DECREF(owner);
2383 if (err != 0)
2384 goto error;
2385 DISPATCH();
2386 }
2387
Benjamin Petersonddd19492018-09-16 22:38:02 -07002388 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002389 PyObject *name = GETITEM(names, oparg);
2390 PyObject *v = POP();
2391 int err;
2392 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002393 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002394 if (err != 0)
2395 goto error;
2396 DISPATCH();
2397 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002398
Benjamin Petersonddd19492018-09-16 22:38:02 -07002399 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002400 PyObject *name = GETITEM(names, oparg);
2401 int err;
2402 err = PyDict_DelItem(f->f_globals, name);
2403 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002404 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2405 format_exc_check_arg(tstate, PyExc_NameError,
2406 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002407 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002408 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002409 }
2410 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002411 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002412
Benjamin Petersonddd19492018-09-16 22:38:02 -07002413 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002414 PyObject *name = GETITEM(names, oparg);
2415 PyObject *locals = f->f_locals;
2416 PyObject *v;
2417 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002418 _PyErr_Format(tstate, PyExc_SystemError,
2419 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002420 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002421 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002422 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002423 v = PyDict_GetItemWithError(locals, name);
2424 if (v != NULL) {
2425 Py_INCREF(v);
2426 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002427 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002428 goto error;
2429 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002430 }
2431 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002432 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002433 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002434 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002435 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002436 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002437 }
2438 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002439 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002440 v = PyDict_GetItemWithError(f->f_globals, name);
2441 if (v != NULL) {
2442 Py_INCREF(v);
2443 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002444 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002445 goto error;
2446 }
2447 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002448 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002449 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002450 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002451 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002452 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002453 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002454 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002455 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002456 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002457 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002458 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002459 }
2460 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002461 v = PyObject_GetItem(f->f_builtins, name);
2462 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002463 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002464 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002465 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002466 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002467 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002468 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002469 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002470 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002471 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002472 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002473 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002474 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002475 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002476
Benjamin Petersonddd19492018-09-16 22:38:02 -07002477 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002478 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002479 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002480 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002481 && PyDict_CheckExact(f->f_builtins))
2482 {
Inada Naoki91234a12019-06-03 21:30:58 +09002483 OPCACHE_CHECK();
2484 if (co_opcache != NULL && co_opcache->optimized > 0) {
2485 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2486
2487 if (lg->globals_ver ==
2488 ((PyDictObject *)f->f_globals)->ma_version_tag
2489 && lg->builtins_ver ==
2490 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2491 {
2492 PyObject *ptr = lg->ptr;
2493 OPCACHE_STAT_GLOBAL_HIT();
2494 assert(ptr != NULL);
2495 Py_INCREF(ptr);
2496 PUSH(ptr);
2497 DISPATCH();
2498 }
2499 }
2500
2501 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002502 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002503 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002504 name);
2505 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002506 if (!_PyErr_OCCURRED()) {
2507 /* _PyDict_LoadGlobal() returns NULL without raising
2508 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002509 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002510 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002511 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002512 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002513 }
Inada Naoki91234a12019-06-03 21:30:58 +09002514
2515 if (co_opcache != NULL) {
2516 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2517
2518 if (co_opcache->optimized == 0) {
2519 /* Wasn't optimized before. */
2520 OPCACHE_STAT_GLOBAL_OPT();
2521 } else {
2522 OPCACHE_STAT_GLOBAL_MISS();
2523 }
2524
2525 co_opcache->optimized = 1;
2526 lg->globals_ver =
2527 ((PyDictObject *)f->f_globals)->ma_version_tag;
2528 lg->builtins_ver =
2529 ((PyDictObject *)f->f_builtins)->ma_version_tag;
2530 lg->ptr = v; /* borrowed */
2531 }
2532
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002533 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002534 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002535 else {
2536 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002537
2538 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09002539 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002540 v = PyObject_GetItem(f->f_globals, name);
2541 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002542 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002543 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002544 }
2545 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002546
Victor Stinnerb4efc962015-11-20 09:24:02 +01002547 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002548 v = PyObject_GetItem(f->f_builtins, name);
2549 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002550 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002551 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002552 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002553 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002554 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002555 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002556 }
2557 }
2558 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002559 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002560 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002561 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002562
Benjamin Petersonddd19492018-09-16 22:38:02 -07002563 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002564 PyObject *v = GETLOCAL(oparg);
2565 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002566 SETLOCAL(oparg, NULL);
2567 DISPATCH();
2568 }
2569 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002570 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002571 UNBOUNDLOCAL_ERROR_MSG,
2572 PyTuple_GetItem(co->co_varnames, oparg)
2573 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002574 goto error;
2575 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002576
Benjamin Petersonddd19492018-09-16 22:38:02 -07002577 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002578 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002579 PyObject *oldobj = PyCell_GET(cell);
2580 if (oldobj != NULL) {
2581 PyCell_SET(cell, NULL);
2582 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002583 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002584 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002585 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002586 goto error;
2587 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002588
Benjamin Petersonddd19492018-09-16 22:38:02 -07002589 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002590 PyObject *cell = freevars[oparg];
2591 Py_INCREF(cell);
2592 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002593 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002594 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002595
Benjamin Petersonddd19492018-09-16 22:38:02 -07002596 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002597 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002598 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002599 assert(locals);
2600 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2601 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2602 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2603 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2604 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002605 value = PyDict_GetItemWithError(locals, name);
2606 if (value != NULL) {
2607 Py_INCREF(value);
2608 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002609 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002610 goto error;
2611 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002612 }
2613 else {
2614 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002615 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002616 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002617 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002618 }
2619 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002620 }
2621 }
2622 if (!value) {
2623 PyObject *cell = freevars[oparg];
2624 value = PyCell_GET(cell);
2625 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002626 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002627 goto error;
2628 }
2629 Py_INCREF(value);
2630 }
2631 PUSH(value);
2632 DISPATCH();
2633 }
2634
Benjamin Petersonddd19492018-09-16 22:38:02 -07002635 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002636 PyObject *cell = freevars[oparg];
2637 PyObject *value = PyCell_GET(cell);
2638 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002639 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002640 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002641 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002642 Py_INCREF(value);
2643 PUSH(value);
2644 DISPATCH();
2645 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002646
Benjamin Petersonddd19492018-09-16 22:38:02 -07002647 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002648 PyObject *v = POP();
2649 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002650 PyObject *oldobj = PyCell_GET(cell);
2651 PyCell_SET(cell, v);
2652 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002653 DISPATCH();
2654 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002655
Benjamin Petersonddd19492018-09-16 22:38:02 -07002656 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002657 PyObject *str;
2658 PyObject *empty = PyUnicode_New(0, 0);
2659 if (empty == NULL) {
2660 goto error;
2661 }
2662 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2663 Py_DECREF(empty);
2664 if (str == NULL)
2665 goto error;
2666 while (--oparg >= 0) {
2667 PyObject *item = POP();
2668 Py_DECREF(item);
2669 }
2670 PUSH(str);
2671 DISPATCH();
2672 }
2673
Benjamin Petersonddd19492018-09-16 22:38:02 -07002674 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002675 PyObject *tup = PyTuple_New(oparg);
2676 if (tup == NULL)
2677 goto error;
2678 while (--oparg >= 0) {
2679 PyObject *item = POP();
2680 PyTuple_SET_ITEM(tup, oparg, item);
2681 }
2682 PUSH(tup);
2683 DISPATCH();
2684 }
2685
Benjamin Petersonddd19492018-09-16 22:38:02 -07002686 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002687 PyObject *list = PyList_New(oparg);
2688 if (list == NULL)
2689 goto error;
2690 while (--oparg >= 0) {
2691 PyObject *item = POP();
2692 PyList_SET_ITEM(list, oparg, item);
2693 }
2694 PUSH(list);
2695 DISPATCH();
2696 }
2697
Benjamin Petersonddd19492018-09-16 22:38:02 -07002698 case TARGET(BUILD_TUPLE_UNPACK_WITH_CALL):
2699 case TARGET(BUILD_TUPLE_UNPACK):
2700 case TARGET(BUILD_LIST_UNPACK): {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002701 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002702 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002703 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002704 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002705
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002706 if (sum == NULL)
2707 goto error;
2708
2709 for (i = oparg; i > 0; i--) {
2710 PyObject *none_val;
2711
2712 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2713 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002714 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
Victor Stinner438a12d2019-05-24 17:01:38 +02002715 _PyErr_ExceptionMatches(tstate, PyExc_TypeError))
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002716 {
Victor Stinner438a12d2019-05-24 17:01:38 +02002717 check_args_iterable(tstate, PEEK(1 + oparg), PEEK(i));
Serhiy Storchaka73442852016-10-02 10:33:46 +03002718 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002719 Py_DECREF(sum);
2720 goto error;
2721 }
2722 Py_DECREF(none_val);
2723 }
2724
2725 if (convert_to_tuple) {
2726 return_value = PyList_AsTuple(sum);
2727 Py_DECREF(sum);
2728 if (return_value == NULL)
2729 goto error;
2730 }
2731 else {
2732 return_value = sum;
2733 }
2734
2735 while (oparg--)
2736 Py_DECREF(POP());
2737 PUSH(return_value);
2738 DISPATCH();
2739 }
2740
Benjamin Petersonddd19492018-09-16 22:38:02 -07002741 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002742 PyObject *set = PySet_New(NULL);
2743 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002744 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002745 if (set == NULL)
2746 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002747 for (i = oparg; i > 0; i--) {
2748 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002749 if (err == 0)
2750 err = PySet_Add(set, item);
2751 Py_DECREF(item);
2752 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002753 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002754 if (err != 0) {
2755 Py_DECREF(set);
2756 goto error;
2757 }
2758 PUSH(set);
2759 DISPATCH();
2760 }
2761
Benjamin Petersonddd19492018-09-16 22:38:02 -07002762 case TARGET(BUILD_SET_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002763 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002764 PyObject *sum = PySet_New(NULL);
2765 if (sum == NULL)
2766 goto error;
2767
2768 for (i = oparg; i > 0; i--) {
2769 if (_PySet_Update(sum, PEEK(i)) < 0) {
2770 Py_DECREF(sum);
2771 goto error;
2772 }
2773 }
2774
2775 while (oparg--)
2776 Py_DECREF(POP());
2777 PUSH(sum);
2778 DISPATCH();
2779 }
2780
Benjamin Petersonddd19492018-09-16 22:38:02 -07002781 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002782 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002783 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2784 if (map == NULL)
2785 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002786 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002787 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002788 PyObject *key = PEEK(2*i);
2789 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002790 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002791 if (err != 0) {
2792 Py_DECREF(map);
2793 goto error;
2794 }
2795 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002796
2797 while (oparg--) {
2798 Py_DECREF(POP());
2799 Py_DECREF(POP());
2800 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002801 PUSH(map);
2802 DISPATCH();
2803 }
2804
Benjamin Petersonddd19492018-09-16 22:38:02 -07002805 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002806 _Py_IDENTIFIER(__annotations__);
2807 int err;
2808 PyObject *ann_dict;
2809 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002810 _PyErr_Format(tstate, PyExc_SystemError,
2811 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002812 goto error;
2813 }
2814 /* check if __annotations__ in locals()... */
2815 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002816 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002817 &PyId___annotations__);
2818 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002819 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002820 goto error;
2821 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002822 /* ...if not, create a new one */
2823 ann_dict = PyDict_New();
2824 if (ann_dict == NULL) {
2825 goto error;
2826 }
2827 err = _PyDict_SetItemId(f->f_locals,
2828 &PyId___annotations__, ann_dict);
2829 Py_DECREF(ann_dict);
2830 if (err != 0) {
2831 goto error;
2832 }
2833 }
2834 }
2835 else {
2836 /* do the same if locals() is not a dict */
2837 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2838 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002839 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002840 }
2841 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2842 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002843 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002844 goto error;
2845 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002846 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002847 ann_dict = PyDict_New();
2848 if (ann_dict == NULL) {
2849 goto error;
2850 }
2851 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2852 Py_DECREF(ann_dict);
2853 if (err != 0) {
2854 goto error;
2855 }
2856 }
2857 else {
2858 Py_DECREF(ann_dict);
2859 }
2860 }
2861 DISPATCH();
2862 }
2863
Benjamin Petersonddd19492018-09-16 22:38:02 -07002864 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002865 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002866 PyObject *map;
2867 PyObject *keys = TOP();
2868 if (!PyTuple_CheckExact(keys) ||
2869 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002870 _PyErr_SetString(tstate, PyExc_SystemError,
2871 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002872 goto error;
2873 }
2874 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2875 if (map == NULL) {
2876 goto error;
2877 }
2878 for (i = oparg; i > 0; i--) {
2879 int err;
2880 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2881 PyObject *value = PEEK(i + 1);
2882 err = PyDict_SetItem(map, key, value);
2883 if (err != 0) {
2884 Py_DECREF(map);
2885 goto error;
2886 }
2887 }
2888
2889 Py_DECREF(POP());
2890 while (oparg--) {
2891 Py_DECREF(POP());
2892 }
2893 PUSH(map);
2894 DISPATCH();
2895 }
2896
Benjamin Petersonddd19492018-09-16 22:38:02 -07002897 case TARGET(BUILD_MAP_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002898 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002899 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002900 if (sum == NULL)
2901 goto error;
2902
2903 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002904 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002905 if (PyDict_Update(sum, arg) < 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002906 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
2907 _PyErr_Format(tstate, PyExc_TypeError,
2908 "'%.200s' object is not a mapping",
2909 arg->ob_type->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002910 }
2911 Py_DECREF(sum);
2912 goto error;
2913 }
2914 }
2915
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002916 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002917 Py_DECREF(POP());
2918 PUSH(sum);
2919 DISPATCH();
2920 }
2921
Benjamin Petersonddd19492018-09-16 22:38:02 -07002922 case TARGET(BUILD_MAP_UNPACK_WITH_CALL): {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002923 Py_ssize_t i;
2924 PyObject *sum = PyDict_New();
2925 if (sum == NULL)
2926 goto error;
2927
2928 for (i = oparg; i > 0; i--) {
2929 PyObject *arg = PEEK(i);
2930 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002931 Py_DECREF(sum);
Victor Stinner438a12d2019-05-24 17:01:38 +02002932 format_kwargs_error(tstate, PEEK(2 + oparg), arg);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002933 goto error;
2934 }
2935 }
2936
2937 while (oparg--)
2938 Py_DECREF(POP());
2939 PUSH(sum);
2940 DISPATCH();
2941 }
2942
Benjamin Petersonddd19492018-09-16 22:38:02 -07002943 case TARGET(MAP_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002944 PyObject *key = TOP();
2945 PyObject *value = SECOND();
2946 PyObject *map;
2947 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002948 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002949 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002950 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002951 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002952 Py_DECREF(value);
2953 Py_DECREF(key);
2954 if (err != 0)
2955 goto error;
2956 PREDICT(JUMP_ABSOLUTE);
2957 DISPATCH();
2958 }
2959
Benjamin Petersonddd19492018-09-16 22:38:02 -07002960 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002961 PyObject *name = GETITEM(names, oparg);
2962 PyObject *owner = TOP();
2963 PyObject *res = PyObject_GetAttr(owner, name);
2964 Py_DECREF(owner);
2965 SET_TOP(res);
2966 if (res == NULL)
2967 goto error;
2968 DISPATCH();
2969 }
2970
Benjamin Petersonddd19492018-09-16 22:38:02 -07002971 case TARGET(COMPARE_OP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002972 PyObject *right = POP();
2973 PyObject *left = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002974 PyObject *res = cmp_outcome(tstate, oparg, left, right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002975 Py_DECREF(left);
2976 Py_DECREF(right);
2977 SET_TOP(res);
2978 if (res == NULL)
2979 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002980 PREDICT(POP_JUMP_IF_FALSE);
2981 PREDICT(POP_JUMP_IF_TRUE);
2982 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002983 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002984
Benjamin Petersonddd19492018-09-16 22:38:02 -07002985 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002986 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002987 PyObject *fromlist = POP();
2988 PyObject *level = TOP();
2989 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02002990 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002991 Py_DECREF(level);
2992 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002993 SET_TOP(res);
2994 if (res == NULL)
2995 goto error;
2996 DISPATCH();
2997 }
2998
Benjamin Petersonddd19492018-09-16 22:38:02 -07002999 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003000 PyObject *from = POP(), *locals;
3001 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003002 if (PyFrame_FastToLocalsWithError(f) < 0) {
3003 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01003004 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003005 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01003006
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003007 locals = f->f_locals;
3008 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003009 _PyErr_SetString(tstate, PyExc_SystemError,
3010 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003011 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003012 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003013 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003014 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003015 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003016 Py_DECREF(from);
3017 if (err != 0)
3018 goto error;
3019 DISPATCH();
3020 }
Guido van Rossum25831651993-05-19 14:50:45 +00003021
Benjamin Petersonddd19492018-09-16 22:38:02 -07003022 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003023 PyObject *name = GETITEM(names, oparg);
3024 PyObject *from = TOP();
3025 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003026 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003027 PUSH(res);
3028 if (res == NULL)
3029 goto error;
3030 DISPATCH();
3031 }
Thomas Wouters52152252000-08-17 22:55:00 +00003032
Benjamin Petersonddd19492018-09-16 22:38:02 -07003033 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003034 JUMPBY(oparg);
3035 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003036 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003037
Benjamin Petersonddd19492018-09-16 22:38:02 -07003038 case TARGET(POP_JUMP_IF_FALSE): {
3039 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003040 PyObject *cond = POP();
3041 int err;
3042 if (cond == Py_True) {
3043 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003044 FAST_DISPATCH();
3045 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003046 if (cond == Py_False) {
3047 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003048 JUMPTO(oparg);
3049 FAST_DISPATCH();
3050 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003051 err = PyObject_IsTrue(cond);
3052 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003053 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003054 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003055 else if (err == 0)
3056 JUMPTO(oparg);
3057 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003058 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003059 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003060 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003061
Benjamin Petersonddd19492018-09-16 22:38:02 -07003062 case TARGET(POP_JUMP_IF_TRUE): {
3063 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003064 PyObject *cond = POP();
3065 int err;
3066 if (cond == Py_False) {
3067 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003068 FAST_DISPATCH();
3069 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003070 if (cond == Py_True) {
3071 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003072 JUMPTO(oparg);
3073 FAST_DISPATCH();
3074 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003075 err = PyObject_IsTrue(cond);
3076 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003077 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003078 JUMPTO(oparg);
3079 }
3080 else if (err == 0)
3081 ;
3082 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003083 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003084 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003085 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003086
Benjamin Petersonddd19492018-09-16 22:38:02 -07003087 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003088 PyObject *cond = TOP();
3089 int err;
3090 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003091 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003092 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003093 FAST_DISPATCH();
3094 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003095 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003096 JUMPTO(oparg);
3097 FAST_DISPATCH();
3098 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003099 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003100 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003101 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003102 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003103 }
3104 else if (err == 0)
3105 JUMPTO(oparg);
3106 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003107 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003108 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003109 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003110
Benjamin Petersonddd19492018-09-16 22:38:02 -07003111 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003112 PyObject *cond = TOP();
3113 int err;
3114 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003115 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003116 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003117 FAST_DISPATCH();
3118 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003119 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003120 JUMPTO(oparg);
3121 FAST_DISPATCH();
3122 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003123 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003124 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003125 JUMPTO(oparg);
3126 }
3127 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003128 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003129 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003130 }
3131 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003132 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003133 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003134 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003135
Benjamin Petersonddd19492018-09-16 22:38:02 -07003136 case TARGET(JUMP_ABSOLUTE): {
3137 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003138 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003139#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003140 /* Enabling this path speeds-up all while and for-loops by bypassing
3141 the per-loop checks for signals. By default, this should be turned-off
3142 because it prevents detection of a control-break in tight loops like
3143 "while 1: pass". Compile with this option turned-on when you need
3144 the speed-up and do not need break checking inside tight loops (ones
3145 that contain only instructions ending with FAST_DISPATCH).
3146 */
3147 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003148#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003149 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003150#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003151 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003152
Benjamin Petersonddd19492018-09-16 22:38:02 -07003153 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003154 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003155 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003156 PyObject *iter = PyObject_GetIter(iterable);
3157 Py_DECREF(iterable);
3158 SET_TOP(iter);
3159 if (iter == NULL)
3160 goto error;
3161 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003162 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003163 DISPATCH();
3164 }
3165
Benjamin Petersonddd19492018-09-16 22:38:02 -07003166 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04003167 /* before: [obj]; after [getiter(obj)] */
3168 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003169 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003170 if (PyCoro_CheckExact(iterable)) {
3171 /* `iterable` is a coroutine */
3172 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3173 /* and it is used in a 'yield from' expression of a
3174 regular generator. */
3175 Py_DECREF(iterable);
3176 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003177 _PyErr_SetString(tstate, PyExc_TypeError,
3178 "cannot 'yield from' a coroutine object "
3179 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04003180 goto error;
3181 }
3182 }
3183 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003184 /* `iterable` is not a generator. */
3185 iter = PyObject_GetIter(iterable);
3186 Py_DECREF(iterable);
3187 SET_TOP(iter);
3188 if (iter == NULL)
3189 goto error;
3190 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003191 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003192 DISPATCH();
3193 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003194
Benjamin Petersonddd19492018-09-16 22:38:02 -07003195 case TARGET(FOR_ITER): {
3196 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003197 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003198 PyObject *iter = TOP();
3199 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
3200 if (next != NULL) {
3201 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003202 PREDICT(STORE_FAST);
3203 PREDICT(UNPACK_SEQUENCE);
3204 DISPATCH();
3205 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003206 if (_PyErr_Occurred(tstate)) {
3207 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003208 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003209 }
3210 else if (tstate->c_tracefunc != NULL) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003211 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Victor Stinner438a12d2019-05-24 17:01:38 +02003212 }
3213 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003214 }
3215 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003216 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003217 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003218 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003219 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003220 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003221 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003222
Benjamin Petersonddd19492018-09-16 22:38:02 -07003223 case TARGET(SETUP_FINALLY): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003224 /* NOTE: If you add any new block-setup opcodes that
3225 are not try/except/finally handlers, you may need
3226 to update the PyGen_NeedsFinalizing() function.
3227 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003228
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003229 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003230 STACK_LEVEL());
3231 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003232 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003233
Benjamin Petersonddd19492018-09-16 22:38:02 -07003234 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003235 _Py_IDENTIFIER(__aexit__);
3236 _Py_IDENTIFIER(__aenter__);
3237
3238 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003239 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__),
Yury Selivanov75445082015-05-11 22:57:16 -04003240 *enter;
3241 PyObject *res;
3242 if (exit == NULL)
3243 goto error;
3244 SET_TOP(exit);
Victor Stinner438a12d2019-05-24 17:01:38 +02003245 enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04003246 Py_DECREF(mgr);
3247 if (enter == NULL)
3248 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003249 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003250 Py_DECREF(enter);
3251 if (res == NULL)
3252 goto error;
3253 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003254 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003255 DISPATCH();
3256 }
3257
Benjamin Petersonddd19492018-09-16 22:38:02 -07003258 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003259 PyObject *res = POP();
3260 /* Setup the finally block before pushing the result
3261 of __aenter__ on the stack. */
3262 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3263 STACK_LEVEL());
3264 PUSH(res);
3265 DISPATCH();
3266 }
3267
Benjamin Petersonddd19492018-09-16 22:38:02 -07003268 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003269 _Py_IDENTIFIER(__exit__);
3270 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003271 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003272 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003273 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003274 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08003275 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003276 }
3277 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003278 if (exit == NULL) {
3279 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003280 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003281 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003282 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003283 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003284 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003285 Py_DECREF(enter);
3286 if (res == NULL)
3287 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003288 /* Setup the finally block before pushing the result
3289 of __enter__ on the stack. */
3290 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3291 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003292
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003293 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003294 DISPATCH();
3295 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003296
Benjamin Petersonddd19492018-09-16 22:38:02 -07003297 case TARGET(WITH_CLEANUP_START): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003298 /* At the top of the stack are 1 or 6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003299 how/why we entered the finally clause:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003300 - TOP = NULL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003301 - (TOP, SECOND, THIRD) = exc_info()
3302 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003303 Below them is EXIT, the context.__exit__ or context.__aexit__
3304 bound method.
3305 In the first case, we must call
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003306 EXIT(None, None, None)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003307 otherwise we must call
3308 EXIT(TOP, SECOND, THIRD)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003309
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003310 In the first case, we remove EXIT from the
3311 stack, leaving TOP, and push TOP on the stack.
3312 Otherwise we shift the bottom 3 values of the
3313 stack down, replace the empty spot with NULL, and push
3314 None on the stack.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003315
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003316 Finally we push the result of the call.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003317 */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003318 PyObject *stack[3];
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
3355 stack[0] = exc;
3356 stack[1] = val;
3357 stack[2] = tb;
3358 res = _PyObject_FastCall(exit_func, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003359 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003360 if (res == NULL)
3361 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003362
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003363 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003364 PUSH(exc);
3365 PUSH(res);
3366 PREDICT(WITH_CLEANUP_FINISH);
3367 DISPATCH();
3368 }
3369
Benjamin Petersonddd19492018-09-16 22:38:02 -07003370 case TARGET(WITH_CLEANUP_FINISH): {
3371 PREDICTED(WITH_CLEANUP_FINISH);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003372 /* TOP = the result of calling the context.__exit__ bound method
3373 SECOND = either None or exception type
3374
3375 If SECOND is None below is NULL or the return address,
3376 otherwise below are 7 values representing an exception.
3377 */
Yury Selivanov75445082015-05-11 22:57:16 -04003378 PyObject *res = POP();
3379 PyObject *exc = POP();
3380 int err;
3381
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003382 if (exc != Py_None)
3383 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003384 else
3385 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003386
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003387 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003388 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003390 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003391 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003392 else if (err > 0) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003393 /* There was an exception and a True return.
3394 * We must manually unwind the EXCEPT_HANDLER block
3395 * which was created when the exception was caught,
Quan Tian3bd0d622018-10-20 05:30:03 +08003396 * otherwise the stack will be in an inconsistent state.
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003397 */
3398 PyTryBlock *b = PyFrame_BlockPop(f);
3399 assert(b->b_type == EXCEPT_HANDLER);
3400 UNWIND_EXCEPT_HANDLER(b);
3401 PUSH(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003402 }
3403 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003404 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003405 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003406
Benjamin Petersonddd19492018-09-16 22:38:02 -07003407 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10003408 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003409 PyObject *name = GETITEM(names, oparg);
3410 PyObject *obj = TOP();
3411 PyObject *meth = NULL;
3412
3413 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3414
Yury Selivanovf2392132016-12-13 19:03:51 -05003415 if (meth == NULL) {
3416 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003417 goto error;
3418 }
3419
3420 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003421 /* We can bypass temporary bound method object.
3422 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003423
INADA Naoki015bce62017-01-16 17:23:30 +09003424 meth | self | arg1 | ... | argN
3425 */
3426 SET_TOP(meth);
3427 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003428 }
3429 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003430 /* meth is not an unbound method (but a regular attr, or
3431 something was returned by a descriptor protocol). Set
3432 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003433 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003434
3435 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003436 */
INADA Naoki015bce62017-01-16 17:23:30 +09003437 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003438 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003439 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003440 }
3441 DISPATCH();
3442 }
3443
Benjamin Petersonddd19492018-09-16 22:38:02 -07003444 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003445 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003446 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003447
3448 sp = stack_pointer;
3449
INADA Naoki015bce62017-01-16 17:23:30 +09003450 meth = PEEK(oparg + 2);
3451 if (meth == NULL) {
3452 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3453 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003454
3455 Stack layout:
3456
INADA Naoki015bce62017-01-16 17:23:30 +09003457 ... | NULL | callable | arg1 | ... | argN
3458 ^- TOP()
3459 ^- (-oparg)
3460 ^- (-oparg-1)
3461 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003462
Ville Skyttä49b27342017-08-03 09:00:59 +03003463 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003464 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003465 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003466 res = call_function(tstate, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003467 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003468 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003469 }
3470 else {
3471 /* This is a method call. Stack layout:
3472
INADA Naoki015bce62017-01-16 17:23:30 +09003473 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003474 ^- TOP()
3475 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003476 ^- (-oparg-1)
3477 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003478
INADA Naoki015bce62017-01-16 17:23:30 +09003479 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003480 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003481 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003482 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003483 res = call_function(tstate, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003484 stack_pointer = sp;
3485 }
3486
3487 PUSH(res);
3488 if (res == NULL)
3489 goto error;
3490 DISPATCH();
3491 }
3492
Benjamin Petersonddd19492018-09-16 22:38:02 -07003493 case TARGET(CALL_FUNCTION): {
3494 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003495 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003496 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003497 res = call_function(tstate, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003498 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003499 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003500 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003501 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003502 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003503 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003504 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003505
Benjamin Petersonddd19492018-09-16 22:38:02 -07003506 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003507 PyObject **sp, *res, *names;
3508
3509 names = POP();
3510 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003511 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003512 res = call_function(tstate, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003513 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003514 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003515 Py_DECREF(names);
3516
3517 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003518 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003519 }
3520 DISPATCH();
3521 }
3522
Benjamin Petersonddd19492018-09-16 22:38:02 -07003523 case TARGET(CALL_FUNCTION_EX): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003524 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003525 if (oparg & 0x01) {
3526 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003527 if (!PyDict_CheckExact(kwargs)) {
3528 PyObject *d = PyDict_New();
3529 if (d == NULL)
3530 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003531 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003532 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02003533 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003534 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003535 goto error;
3536 }
3537 Py_DECREF(kwargs);
3538 kwargs = d;
3539 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003540 assert(PyDict_CheckExact(kwargs));
3541 }
3542 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003543 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003544 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003545 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003546 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003547 goto error;
3548 }
3549 Py_SETREF(callargs, PySequence_Tuple(callargs));
3550 if (callargs == NULL) {
3551 goto error;
3552 }
3553 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003554 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003555
Victor Stinner09532fe2019-05-10 23:39:09 +02003556 result = do_call_core(tstate, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003557 Py_DECREF(func);
3558 Py_DECREF(callargs);
3559 Py_XDECREF(kwargs);
3560
3561 SET_TOP(result);
3562 if (result == NULL) {
3563 goto error;
3564 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003565 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003566 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003567
Benjamin Petersonddd19492018-09-16 22:38:02 -07003568 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003569 PyObject *qualname = POP();
3570 PyObject *codeobj = POP();
3571 PyFunctionObject *func = (PyFunctionObject *)
3572 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003573
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003574 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003575 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003576 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003577 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003578 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003579
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003580 if (oparg & 0x08) {
3581 assert(PyTuple_CheckExact(TOP()));
3582 func ->func_closure = POP();
3583 }
3584 if (oparg & 0x04) {
3585 assert(PyDict_CheckExact(TOP()));
3586 func->func_annotations = POP();
3587 }
3588 if (oparg & 0x02) {
3589 assert(PyDict_CheckExact(TOP()));
3590 func->func_kwdefaults = POP();
3591 }
3592 if (oparg & 0x01) {
3593 assert(PyTuple_CheckExact(TOP()));
3594 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003595 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003596
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003597 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003598 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003599 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003600
Benjamin Petersonddd19492018-09-16 22:38:02 -07003601 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003602 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003603 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003604 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003605 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003606 step = NULL;
3607 stop = POP();
3608 start = TOP();
3609 slice = PySlice_New(start, stop, step);
3610 Py_DECREF(start);
3611 Py_DECREF(stop);
3612 Py_XDECREF(step);
3613 SET_TOP(slice);
3614 if (slice == NULL)
3615 goto error;
3616 DISPATCH();
3617 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003618
Benjamin Petersonddd19492018-09-16 22:38:02 -07003619 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003620 /* Handles f-string value formatting. */
3621 PyObject *result;
3622 PyObject *fmt_spec;
3623 PyObject *value;
3624 PyObject *(*conv_fn)(PyObject *);
3625 int which_conversion = oparg & FVC_MASK;
3626 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3627
3628 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003629 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003630
3631 /* See if any conversion is specified. */
3632 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003633 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003634 case FVC_STR: conv_fn = PyObject_Str; break;
3635 case FVC_REPR: conv_fn = PyObject_Repr; break;
3636 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003637 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02003638 _PyErr_Format(tstate, PyExc_SystemError,
3639 "unexpected conversion flag %d",
3640 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003641 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003642 }
3643
3644 /* If there's a conversion function, call it and replace
3645 value with that result. Otherwise, just use value,
3646 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003647 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003648 result = conv_fn(value);
3649 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003650 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003651 Py_XDECREF(fmt_spec);
3652 goto error;
3653 }
3654 value = result;
3655 }
3656
3657 /* If value is a unicode object, and there's no fmt_spec,
3658 then we know the result of format(value) is value
3659 itself. In that case, skip calling format(). I plan to
3660 move this optimization in to PyObject_Format()
3661 itself. */
3662 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3663 /* Do nothing, just transfer ownership to result. */
3664 result = value;
3665 } else {
3666 /* Actually call format(). */
3667 result = PyObject_Format(value, fmt_spec);
3668 Py_DECREF(value);
3669 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003670 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003671 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003672 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003673 }
3674
Eric V. Smith135d5f42016-02-05 18:23:08 -05003675 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003676 DISPATCH();
3677 }
3678
Benjamin Petersonddd19492018-09-16 22:38:02 -07003679 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003680 int oldoparg = oparg;
3681 NEXTOPARG();
3682 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003683 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003684 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003685
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003686
Antoine Pitrou042b1282010-08-13 21:15:58 +00003687#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003688 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003689#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003690 default:
3691 fprintf(stderr,
3692 "XXX lineno: %d, opcode: %d\n",
3693 PyFrame_GetLineNumber(f),
3694 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02003695 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003696 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003698 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003699
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003700 /* This should never be reached. Every opcode should end with DISPATCH()
3701 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003702 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003703
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003704error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003705 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003706#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02003707 if (!_PyErr_Occurred(tstate)) {
3708 _PyErr_SetString(tstate, PyExc_SystemError,
3709 "error return without exception set");
3710 }
Victor Stinner365b6932013-07-12 00:11:58 +02003711#else
Victor Stinner438a12d2019-05-24 17:01:38 +02003712 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02003713#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003714
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003715 /* Log traceback info. */
3716 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003717
Benjamin Peterson51f46162013-01-23 08:38:47 -05003718 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003719 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3720 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003721
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003722exception_unwind:
3723 /* Unwind stacks if an exception occurred */
3724 while (f->f_iblock > 0) {
3725 /* Pop the current block. */
3726 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003727
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003728 if (b->b_type == EXCEPT_HANDLER) {
3729 UNWIND_EXCEPT_HANDLER(b);
3730 continue;
3731 }
3732 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003733 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003734 PyObject *exc, *val, *tb;
3735 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003736 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003737 /* Beware, this invalidates all b->b_* fields */
3738 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003739 PUSH(exc_info->exc_traceback);
3740 PUSH(exc_info->exc_value);
3741 if (exc_info->exc_type != NULL) {
3742 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003743 }
3744 else {
3745 Py_INCREF(Py_None);
3746 PUSH(Py_None);
3747 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003748 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003749 /* Make the raw exception data
3750 available to the handler,
3751 so a program can emulate the
3752 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02003753 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003754 if (tb != NULL)
3755 PyException_SetTraceback(val, tb);
3756 else
3757 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003758 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003759 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003760 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003761 exc_info->exc_value = val;
3762 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003763 if (tb == NULL)
3764 tb = Py_None;
3765 Py_INCREF(tb);
3766 PUSH(tb);
3767 PUSH(val);
3768 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003769 JUMPTO(handler);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003770 /* Resume normal execution */
3771 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003772 }
3773 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003774
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003775 /* End the loop as we still have an error */
3776 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003777 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003778
Pablo Galindof00828a2019-05-09 16:52:02 +01003779 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003780 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01003781
3782exit_returning:
3783
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003784 /* Pop remaining stack entries. */
3785 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003786 PyObject *o = POP();
3787 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003788 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003789
Pablo Galindof00828a2019-05-09 16:52:02 +01003790exit_yielding:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003791 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003792 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003793 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3794 tstate, f, PyTrace_RETURN, retval)) {
3795 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003796 }
3797 }
3798 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003799 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3800 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003801 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003802 }
3803 }
3804 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003805
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003806 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003807exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003808 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3809 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003810 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003811 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003812 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003813
Victor Stinnerefde1462015-03-21 15:04:43 +01003814 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003815}
3816
Benjamin Petersonb204a422011-06-05 22:04:07 -05003817static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003818format_missing(PyThreadState *tstate, const char *kind,
3819 PyCodeObject *co, PyObject *names)
Benjamin Petersone109c702011-06-24 09:37:26 -05003820{
3821 int err;
3822 Py_ssize_t len = PyList_GET_SIZE(names);
3823 PyObject *name_str, *comma, *tail, *tmp;
3824
3825 assert(PyList_CheckExact(names));
3826 assert(len >= 1);
3827 /* Deal with the joys of natural language. */
3828 switch (len) {
3829 case 1:
3830 name_str = PyList_GET_ITEM(names, 0);
3831 Py_INCREF(name_str);
3832 break;
3833 case 2:
3834 name_str = PyUnicode_FromFormat("%U and %U",
3835 PyList_GET_ITEM(names, len - 2),
3836 PyList_GET_ITEM(names, len - 1));
3837 break;
3838 default:
3839 tail = PyUnicode_FromFormat(", %U, and %U",
3840 PyList_GET_ITEM(names, len - 2),
3841 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003842 if (tail == NULL)
3843 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003844 /* Chop off the last two objects in the list. This shouldn't actually
3845 fail, but we can't be too careful. */
3846 err = PyList_SetSlice(names, len - 2, len, NULL);
3847 if (err == -1) {
3848 Py_DECREF(tail);
3849 return;
3850 }
3851 /* Stitch everything up into a nice comma-separated list. */
3852 comma = PyUnicode_FromString(", ");
3853 if (comma == NULL) {
3854 Py_DECREF(tail);
3855 return;
3856 }
3857 tmp = PyUnicode_Join(comma, names);
3858 Py_DECREF(comma);
3859 if (tmp == NULL) {
3860 Py_DECREF(tail);
3861 return;
3862 }
3863 name_str = PyUnicode_Concat(tmp, tail);
3864 Py_DECREF(tmp);
3865 Py_DECREF(tail);
3866 break;
3867 }
3868 if (name_str == NULL)
3869 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02003870 _PyErr_Format(tstate, PyExc_TypeError,
3871 "%U() missing %i required %s argument%s: %U",
3872 co->co_name,
3873 len,
3874 kind,
3875 len == 1 ? "" : "s",
3876 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05003877 Py_DECREF(name_str);
3878}
3879
3880static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003881missing_arguments(PyThreadState *tstate, PyCodeObject *co,
3882 Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003883 PyObject **fastlocals)
3884{
Victor Stinner74319ae2016-08-25 00:04:09 +02003885 Py_ssize_t i, j = 0;
3886 Py_ssize_t start, end;
3887 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003888 const char *kind = positional ? "positional" : "keyword-only";
3889 PyObject *missing_names;
3890
3891 /* Compute the names of the arguments that are missing. */
3892 missing_names = PyList_New(missing);
3893 if (missing_names == NULL)
3894 return;
3895 if (positional) {
3896 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01003897 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003898 }
3899 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003900 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003901 end = start + co->co_kwonlyargcount;
3902 }
3903 for (i = start; i < end; i++) {
3904 if (GETLOCAL(i) == NULL) {
3905 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3906 PyObject *name = PyObject_Repr(raw);
3907 if (name == NULL) {
3908 Py_DECREF(missing_names);
3909 return;
3910 }
3911 PyList_SET_ITEM(missing_names, j++, name);
3912 }
3913 }
3914 assert(j == missing);
Victor Stinner438a12d2019-05-24 17:01:38 +02003915 format_missing(tstate, kind, co, missing_names);
Benjamin Petersone109c702011-06-24 09:37:26 -05003916 Py_DECREF(missing_names);
3917}
3918
3919static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003920too_many_positional(PyThreadState *tstate, PyCodeObject *co,
3921 Py_ssize_t given, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003922 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003923{
3924 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003925 Py_ssize_t kwonly_given = 0;
3926 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003927 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003928 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003929
Benjamin Petersone109c702011-06-24 09:37:26 -05003930 assert((co->co_flags & CO_VARARGS) == 0);
3931 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01003932 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003933 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003934 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003935 }
3936 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003937 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01003938 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003939 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01003940 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003941 }
3942 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003943 plural = (co_argcount != 1);
3944 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003945 }
3946 if (sig == NULL)
3947 return;
3948 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003949 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3950 kwonly_sig = PyUnicode_FromFormat(format,
3951 given != 1 ? "s" : "",
3952 kwonly_given,
3953 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003954 if (kwonly_sig == NULL) {
3955 Py_DECREF(sig);
3956 return;
3957 }
3958 }
3959 else {
3960 /* This will not fail. */
3961 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003962 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003963 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003964 _PyErr_Format(tstate, PyExc_TypeError,
3965 "%U() takes %U positional argument%s but %zd%U %s given",
3966 co->co_name,
3967 sig,
3968 plural ? "s" : "",
3969 given,
3970 kwonly_sig,
3971 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003972 Py_DECREF(sig);
3973 Py_DECREF(kwonly_sig);
3974}
3975
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003976static int
Victor Stinner438a12d2019-05-24 17:01:38 +02003977positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
3978 Py_ssize_t kwcount, PyObject* const* kwnames)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003979{
3980 int posonly_conflicts = 0;
3981 PyObject* posonly_names = PyList_New(0);
3982
3983 for(int k=0; k < co->co_posonlyargcount; k++){
3984 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
3985
3986 for (int k2=0; k2<kwcount; k2++){
3987 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
3988 PyObject* kwname = kwnames[k2];
3989 if (kwname == posonly_name){
3990 if(PyList_Append(posonly_names, kwname) != 0) {
3991 goto fail;
3992 }
3993 posonly_conflicts++;
3994 continue;
3995 }
3996
3997 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
3998
3999 if ( cmp > 0) {
4000 if(PyList_Append(posonly_names, kwname) != 0) {
4001 goto fail;
4002 }
4003 posonly_conflicts++;
4004 } else if (cmp < 0) {
4005 goto fail;
4006 }
4007
4008 }
4009 }
4010 if (posonly_conflicts) {
4011 PyObject* comma = PyUnicode_FromString(", ");
4012 if (comma == NULL) {
4013 goto fail;
4014 }
4015 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
4016 Py_DECREF(comma);
4017 if (error_names == NULL) {
4018 goto fail;
4019 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004020 _PyErr_Format(tstate, PyExc_TypeError,
4021 "%U() got some positional-only arguments passed"
4022 " as keyword arguments: '%U'",
4023 co->co_name, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004024 Py_DECREF(error_names);
4025 goto fail;
4026 }
4027
4028 Py_DECREF(posonly_names);
4029 return 0;
4030
4031fail:
4032 Py_XDECREF(posonly_names);
4033 return 1;
4034
4035}
4036
Guido van Rossumc2e20742006-02-27 22:32:47 +00004037/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02004038 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00004039 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00004040
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004041PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +02004042_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004043 PyObject *const *args, Py_ssize_t argcount,
4044 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03004045 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004046 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02004047 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004048 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00004049{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00004050 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004051 PyFrameObject *f;
4052 PyObject *retval = NULL;
4053 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004054 PyObject *x, *u;
Pablo Galindocd74e662019-06-01 18:08:04 +01004055 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004056 Py_ssize_t i, j, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02004057 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00004058
Victor Stinner438a12d2019-05-24 17:01:38 +02004059 PyThreadState *tstate = _PyThreadState_GET();
4060 assert(tstate != NULL);
4061
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004062 if (globals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004063 _PyErr_SetString(tstate, PyExc_SystemError,
4064 "PyEval_EvalCodeEx: NULL globals");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004065 return NULL;
4066 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004067
Victor Stinnerc7020012016-08-16 23:40:29 +02004068 /* Create the frame */
INADA Naoki5a625d02016-12-24 20:19:08 +09004069 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02004070 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004071 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02004072 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004073 fastlocals = f->f_localsplus;
4074 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00004075
Victor Stinnerc7020012016-08-16 23:40:29 +02004076 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004077 if (co->co_flags & CO_VARKEYWORDS) {
4078 kwdict = PyDict_New();
4079 if (kwdict == NULL)
4080 goto fail;
4081 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02004082 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004083 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02004084 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004085 SETLOCAL(i, kwdict);
4086 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004087 else {
4088 kwdict = NULL;
4089 }
4090
Pablo Galindocd74e662019-06-01 18:08:04 +01004091 /* Copy all positional arguments into local variables */
4092 if (argcount > co->co_argcount) {
4093 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02004094 }
4095 else {
4096 n = argcount;
4097 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004098 for (j = 0; j < n; j++) {
4099 x = args[j];
4100 Py_INCREF(x);
4101 SETLOCAL(j, x);
4102 }
4103
Victor Stinnerc7020012016-08-16 23:40:29 +02004104 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004105 if (co->co_flags & CO_VARARGS) {
Sergey Fedoseev234531b2019-02-25 21:59:12 +05004106 u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02004107 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004108 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004109 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004110 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004111 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004112
Serhiy Storchakab7281052016-09-12 00:52:40 +03004113 /* Handle keyword arguments passed as two strided arrays */
4114 kwcount *= kwstep;
4115 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004116 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03004117 PyObject *keyword = kwnames[i];
4118 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02004119 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004120
Benjamin Petersonb204a422011-06-05 22:04:07 -05004121 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004122 _PyErr_Format(tstate, PyExc_TypeError,
4123 "%U() keywords must be strings",
4124 co->co_name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004125 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004126 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004127
Benjamin Petersonb204a422011-06-05 22:04:07 -05004128 /* Speed hack: do raw pointer compares. As names are
4129 normally interned this should almost always hit. */
4130 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004131 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004132 PyObject *name = co_varnames[j];
4133 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004134 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004135 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004136 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004137
Benjamin Petersonb204a422011-06-05 22:04:07 -05004138 /* Slow fallback, just in case */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004139 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004140 PyObject *name = co_varnames[j];
4141 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
4142 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004143 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004144 }
4145 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004146 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004147 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004148 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004149
Victor Stinner231d1f32017-01-11 02:12:06 +01004150 assert(j >= total_args);
4151 if (kwdict == NULL) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004152
Victor Stinner438a12d2019-05-24 17:01:38 +02004153 if (co->co_posonlyargcount
4154 && positional_only_passed_as_keyword(tstate, co,
4155 kwcount, kwnames))
4156 {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004157 goto fail;
4158 }
4159
Victor Stinner438a12d2019-05-24 17:01:38 +02004160 _PyErr_Format(tstate, PyExc_TypeError,
4161 "%U() got an unexpected keyword argument '%S'",
4162 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004163 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004164 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004165
Christian Heimes0bd447f2013-07-20 14:48:10 +02004166 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4167 goto fail;
4168 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004169 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004170
Benjamin Petersonb204a422011-06-05 22:04:07 -05004171 kw_found:
4172 if (GETLOCAL(j) != NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004173 _PyErr_Format(tstate, PyExc_TypeError,
4174 "%U() got multiple values for argument '%S'",
4175 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004176 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004177 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004178 Py_INCREF(value);
4179 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004180 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004181
4182 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004183 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004184 too_many_positional(tstate, co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004185 goto fail;
4186 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004187
4188 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004189 if (argcount < co->co_argcount) {
4190 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004191 Py_ssize_t missing = 0;
4192 for (i = argcount; i < m; i++) {
4193 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004194 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004195 }
4196 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004197 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004198 missing_arguments(tstate, co, missing, defcount, fastlocals);
Benjamin Petersone109c702011-06-24 09:37:26 -05004199 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004200 }
4201 if (n > m)
4202 i = n - m;
4203 else
4204 i = 0;
4205 for (; i < defcount; i++) {
4206 if (GETLOCAL(m+i) == NULL) {
4207 PyObject *def = defs[i];
4208 Py_INCREF(def);
4209 SETLOCAL(m+i, def);
4210 }
4211 }
4212 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004213
4214 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004215 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004216 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004217 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004218 PyObject *name;
4219 if (GETLOCAL(i) != NULL)
4220 continue;
4221 name = PyTuple_GET_ITEM(co->co_varnames, i);
4222 if (kwdefs != NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004223 PyObject *def = PyDict_GetItemWithError(kwdefs, name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004224 if (def) {
4225 Py_INCREF(def);
4226 SETLOCAL(i, def);
4227 continue;
4228 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004229 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004230 goto fail;
4231 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004232 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004233 missing++;
4234 }
4235 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004236 missing_arguments(tstate, co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004237 goto fail;
4238 }
4239 }
4240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004241 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004242 vars into frame. */
4243 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004244 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004245 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004246 /* Possibly account for the cell variable being an argument. */
4247 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004248 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004249 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004250 /* Clear the local copy. */
4251 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004252 }
4253 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004254 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004255 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004256 if (c == NULL)
4257 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004258 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004259 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004260
4261 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004262 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4263 PyObject *o = PyTuple_GET_ITEM(closure, i);
4264 Py_INCREF(o);
4265 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004266 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004267
Yury Selivanoveb636452016-09-08 22:01:51 -07004268 /* Handle generator/coroutine/asynchronous generator */
4269 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004270 PyObject *gen;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004271 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004273 /* Don't need to keep the reference to f_back, it will be set
4274 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004275 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004277 /* Create a new generator that owns the ready to run frame
4278 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004279 if (is_coro) {
4280 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004281 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4282 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004283 } else {
4284 gen = PyGen_NewWithQualName(f, name, qualname);
4285 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004286 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004287 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004288 }
INADA Naoki9c157762016-12-26 18:52:46 +09004289
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004290 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004291
Yury Selivanov75445082015-05-11 22:57:16 -04004292 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004293 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004294
Victor Stinner59a73272016-12-09 18:51:13 +01004295 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004296
Thomas Woutersce272b62007-09-19 21:19:28 +00004297fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004299 /* decref'ing the frame can cause __del__ methods to get invoked,
4300 which can call back into Python. While we're done with the
4301 current Python frame (f), the associated C stack is still in use,
4302 so recursion_depth must be boosted for the duration.
4303 */
4304 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09004305 if (Py_REFCNT(f) > 1) {
4306 Py_DECREF(f);
4307 _PyObject_GC_TRACK(f);
4308 }
4309 else {
4310 ++tstate->recursion_depth;
4311 Py_DECREF(f);
4312 --tstate->recursion_depth;
4313 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004314 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004315}
4316
Victor Stinner40ee3012014-06-16 15:59:28 +02004317PyObject *
4318PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004319 PyObject *const *args, int argcount,
4320 PyObject *const *kws, int kwcount,
4321 PyObject *const *defs, int defcount,
4322 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004323{
4324 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004325 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004326 kws, kws != NULL ? kws + 1 : NULL,
4327 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004328 defs, defcount,
4329 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004330 NULL, NULL);
4331}
Tim Peters5ca576e2001-06-18 22:08:13 +00004332
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004333static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004334special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004335{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004336 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004337 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02004338 if (res == NULL && !_PyErr_Occurred(tstate)) {
4339 _PyErr_SetObject(tstate, PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004340 return NULL;
4341 }
4342 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004343}
4344
4345
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004346/* Logic for the raise statement (too complicated for inlining).
4347 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004348static int
Victor Stinner09532fe2019-05-10 23:39:09 +02004349do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004350{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004351 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004353 if (exc == NULL) {
4354 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01004355 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004356 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004357 type = exc_info->exc_type;
4358 value = exc_info->exc_value;
4359 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004360 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004361 _PyErr_SetString(tstate, PyExc_RuntimeError,
4362 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004363 return 0;
4364 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004365 Py_XINCREF(type);
4366 Py_XINCREF(value);
4367 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02004368 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004369 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004370 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004372 /* We support the following forms of raise:
4373 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004374 raise <instance>
4375 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004377 if (PyExceptionClass_Check(exc)) {
4378 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004379 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004380 if (value == NULL)
4381 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004382 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004383 _PyErr_Format(tstate, PyExc_TypeError,
4384 "calling %R should have returned an instance of "
4385 "BaseException, not %R",
4386 type, Py_TYPE(value));
4387 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004388 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004389 }
4390 else if (PyExceptionInstance_Check(exc)) {
4391 value = exc;
4392 type = PyExceptionInstance_Class(exc);
4393 Py_INCREF(type);
4394 }
4395 else {
4396 /* Not something you can raise. You get an exception
4397 anyway, just not what you specified :-) */
4398 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02004399 _PyErr_SetString(tstate, PyExc_TypeError,
4400 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004401 goto raise_error;
4402 }
Collin Winter828f04a2007-08-31 00:04:24 +00004403
Serhiy Storchakac0191582016-09-27 11:37:10 +03004404 assert(type != NULL);
4405 assert(value != NULL);
4406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004407 if (cause) {
4408 PyObject *fixed_cause;
4409 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004410 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004411 if (fixed_cause == NULL)
4412 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004413 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004414 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004415 else if (PyExceptionInstance_Check(cause)) {
4416 fixed_cause = cause;
4417 }
4418 else if (cause == Py_None) {
4419 Py_DECREF(cause);
4420 fixed_cause = NULL;
4421 }
4422 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004423 _PyErr_SetString(tstate, PyExc_TypeError,
4424 "exception causes must derive from "
4425 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004426 goto raise_error;
4427 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004428 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004429 }
Collin Winter828f04a2007-08-31 00:04:24 +00004430
Victor Stinner438a12d2019-05-24 17:01:38 +02004431 _PyErr_SetObject(tstate, type, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004432 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004433 Py_DECREF(value);
4434 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004435 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004436
4437raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004438 Py_XDECREF(value);
4439 Py_XDECREF(type);
4440 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004441 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004442}
4443
Tim Petersd6d010b2001-06-21 02:49:55 +00004444/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004445 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004446
Guido van Rossum0368b722007-05-11 16:50:42 +00004447 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4448 with a variable target.
4449*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004450
Barry Warsawe42b18f1997-08-25 22:13:04 +00004451static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004452unpack_iterable(PyThreadState *tstate, PyObject *v,
4453 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004454{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004455 int i = 0, j = 0;
4456 Py_ssize_t ll = 0;
4457 PyObject *it; /* iter(v) */
4458 PyObject *w;
4459 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004461 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004463 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004464 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004465 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004466 v->ob_type->tp_iter == NULL && !PySequence_Check(v))
4467 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004468 _PyErr_Format(tstate, PyExc_TypeError,
4469 "cannot unpack non-iterable %.200s object",
4470 v->ob_type->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004471 }
4472 return 0;
4473 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004475 for (; i < argcnt; i++) {
4476 w = PyIter_Next(it);
4477 if (w == NULL) {
4478 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004479 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04004480 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004481 _PyErr_Format(tstate, PyExc_ValueError,
4482 "not enough values to unpack "
4483 "(expected %d, got %d)",
4484 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004485 }
4486 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004487 _PyErr_Format(tstate, PyExc_ValueError,
4488 "not enough values to unpack "
4489 "(expected at least %d, got %d)",
4490 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004491 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004492 }
4493 goto Error;
4494 }
4495 *--sp = w;
4496 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004498 if (argcntafter == -1) {
4499 /* We better have exhausted the iterator now. */
4500 w = PyIter_Next(it);
4501 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004502 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004503 goto Error;
4504 Py_DECREF(it);
4505 return 1;
4506 }
4507 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02004508 _PyErr_Format(tstate, PyExc_ValueError,
4509 "too many values to unpack (expected %d)",
4510 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004511 goto Error;
4512 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004514 l = PySequence_List(it);
4515 if (l == NULL)
4516 goto Error;
4517 *--sp = l;
4518 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004520 ll = PyList_GET_SIZE(l);
4521 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004522 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04004523 "not enough values to unpack (expected at least %d, got %zd)",
4524 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004525 goto Error;
4526 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004527
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004528 /* Pop the "after-variable" args off the list. */
4529 for (j = argcntafter; j > 0; j--, i++) {
4530 *--sp = PyList_GET_ITEM(l, ll - j);
4531 }
4532 /* Resize the list. */
4533 Py_SIZE(l) = ll - argcntafter;
4534 Py_DECREF(it);
4535 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004536
Tim Petersd6d010b2001-06-21 02:49:55 +00004537Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004538 for (; i > 0; i--, sp++)
4539 Py_DECREF(*sp);
4540 Py_XDECREF(it);
4541 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004542}
4543
4544
Guido van Rossum96a42c81992-01-12 02:29:51 +00004545#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004546static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004547prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004548{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004549 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02004550 if (PyObject_Print(v, stdout, 0) != 0) {
4551 /* Don't know what else to do */
4552 _PyErr_Clear(tstate);
4553 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004554 printf("\n");
4555 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004556}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004557#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004558
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004559static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004560call_exc_trace(Py_tracefunc func, PyObject *self,
4561 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004562{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004563 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004564 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004565 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004566 if (value == NULL) {
4567 value = Py_None;
4568 Py_INCREF(value);
4569 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004570 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01004571 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004572 arg = PyTuple_Pack(3, type, value, traceback);
4573 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004574 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004575 return;
4576 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004577 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004578 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02004579 if (err == 0) {
4580 _PyErr_Restore(tstate, type, value, orig_traceback);
4581 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004582 else {
4583 Py_XDECREF(type);
4584 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004585 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004586 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004587}
4588
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004589static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004590call_trace_protected(Py_tracefunc func, PyObject *obj,
4591 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004592 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004593{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004594 PyObject *type, *value, *traceback;
4595 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004596 _PyErr_Fetch(tstate, &type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004597 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004598 if (err == 0)
4599 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004600 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004601 return 0;
4602 }
4603 else {
4604 Py_XDECREF(type);
4605 Py_XDECREF(value);
4606 Py_XDECREF(traceback);
4607 return -1;
4608 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004609}
4610
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004611static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004612call_trace(Py_tracefunc func, PyObject *obj,
4613 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004614 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004615{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004616 int result;
4617 if (tstate->tracing)
4618 return 0;
4619 tstate->tracing++;
4620 tstate->use_tracing = 0;
4621 result = func(obj, frame, what, arg);
4622 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4623 || (tstate->c_profilefunc != NULL));
4624 tstate->tracing--;
4625 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004626}
4627
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004628PyObject *
4629_PyEval_CallTracing(PyObject *func, PyObject *args)
4630{
Victor Stinner50b48572018-11-01 01:51:40 +01004631 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004632 int save_tracing = tstate->tracing;
4633 int save_use_tracing = tstate->use_tracing;
4634 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004636 tstate->tracing = 0;
4637 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4638 || (tstate->c_profilefunc != NULL));
4639 result = PyObject_Call(func, args, NULL);
4640 tstate->tracing = save_tracing;
4641 tstate->use_tracing = save_use_tracing;
4642 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004643}
4644
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004645/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004646static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004647maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004648 PyThreadState *tstate, PyFrameObject *frame,
4649 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004650{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004651 int result = 0;
4652 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004653
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004654 /* If the last instruction executed isn't in the current
4655 instruction window, reset the window.
4656 */
4657 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4658 PyAddrPair bounds;
4659 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4660 &bounds);
4661 *instr_lb = bounds.ap_lower;
4662 *instr_ub = bounds.ap_upper;
4663 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004664 /* If the last instruction falls at the start of a line or if it
4665 represents a jump backwards, update the frame's line number and
4666 then call the trace function if we're tracing source lines.
4667 */
4668 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004669 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004670 if (frame->f_trace_lines) {
4671 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4672 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004673 }
George King20faa682017-10-18 17:44:22 -07004674 /* Always emit an opcode event if we're tracing all opcodes. */
4675 if (frame->f_trace_opcodes) {
4676 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4677 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004678 *instr_prev = frame->f_lasti;
4679 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004680}
4681
Fred Drake5755ce62001-06-27 19:19:46 +00004682void
4683PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004684{
Steve Dowerb82e17e2019-05-23 08:45:22 -07004685 if (PySys_Audit("sys.setprofile", NULL) < 0) {
4686 return;
4687 }
4688
Victor Stinner50b48572018-11-01 01:51:40 +01004689 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004690 PyObject *temp = tstate->c_profileobj;
4691 Py_XINCREF(arg);
4692 tstate->c_profilefunc = NULL;
4693 tstate->c_profileobj = NULL;
4694 /* Must make sure that tracing is not ignored if 'temp' is freed */
4695 tstate->use_tracing = tstate->c_tracefunc != NULL;
4696 Py_XDECREF(temp);
4697 tstate->c_profilefunc = func;
4698 tstate->c_profileobj = arg;
4699 /* Flag that tracing or profiling is turned on */
4700 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004701}
4702
4703void
4704PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4705{
Steve Dowerb82e17e2019-05-23 08:45:22 -07004706 if (PySys_Audit("sys.settrace", NULL) < 0) {
4707 return;
4708 }
4709
Victor Stinner09532fe2019-05-10 23:39:09 +02004710 _PyRuntimeState *runtime = &_PyRuntime;
4711 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004712 PyObject *temp = tstate->c_traceobj;
Victor Stinner09532fe2019-05-10 23:39:09 +02004713 runtime->ceval.tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004714 Py_XINCREF(arg);
4715 tstate->c_tracefunc = NULL;
4716 tstate->c_traceobj = NULL;
4717 /* Must make sure that profiling is not ignored if 'temp' is freed */
4718 tstate->use_tracing = tstate->c_profilefunc != NULL;
4719 Py_XDECREF(temp);
4720 tstate->c_tracefunc = func;
4721 tstate->c_traceobj = arg;
4722 /* Flag that tracing or profiling is turned on */
4723 tstate->use_tracing = ((func != NULL)
4724 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004725}
4726
Yury Selivanov75445082015-05-11 22:57:16 -04004727void
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004728_PyEval_SetCoroutineOriginTrackingDepth(int new_depth)
4729{
4730 assert(new_depth >= 0);
Victor Stinner50b48572018-11-01 01:51:40 +01004731 PyThreadState *tstate = _PyThreadState_GET();
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 Demeyer37788bc2019-05-30 15:11:22 +02004943 C_TRACE(x, _PyCFunction_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 Demeyer37788bc2019-05-30 15:11:22 +02004959 C_TRACE(x, _PyCFunction_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)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005004 C_TRACE(result, PyCFunction_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
5023 C_TRACE(result, _PyCFunction_FastCallDict(func,
Victor Stinnerd17a6932018-11-09 16:56:48 +01005024 &_PyTuple_ITEMS(callargs)[1],
jdemeyere89de732018-09-19 12:06:20 +02005025 nargs - 1,
5026 kwdict));
5027 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 {
5236 errmsg = PyUnicode_FromFormat(
5237 "cannot import name %R from %R (%S)",
5238 name, pkgname_or_unknown, pkgpath
5239 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005240 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005241 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005242 }
5243
Xiang Zhang4830f582017-03-21 11:13:42 +08005244 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005245 Py_XDECREF(pkgname_or_unknown);
5246 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005247 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005248}
Guido van Rossumac7be682001-01-17 15:42:30 +00005249
Thomas Wouters52152252000-08-17 22:55:00 +00005250static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005251import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00005252{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005253 _Py_IDENTIFIER(__all__);
5254 _Py_IDENTIFIER(__dict__);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005255 _Py_IDENTIFIER(__name__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005256 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005257 int skip_leading_underscores = 0;
5258 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005259
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005260 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5261 return -1; /* Unexpected error */
5262 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005263 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005264 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5265 return -1;
5266 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005267 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005268 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005269 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005270 return -1;
5271 }
5272 all = PyMapping_Keys(dict);
5273 Py_DECREF(dict);
5274 if (all == NULL)
5275 return -1;
5276 skip_leading_underscores = 1;
5277 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005279 for (pos = 0, err = 0; ; pos++) {
5280 name = PySequence_GetItem(all, pos);
5281 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005282 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005283 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02005284 }
5285 else {
5286 _PyErr_Clear(tstate);
5287 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005288 break;
5289 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005290 if (!PyUnicode_Check(name)) {
5291 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5292 if (modname == NULL) {
5293 Py_DECREF(name);
5294 err = -1;
5295 break;
5296 }
5297 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005298 _PyErr_Format(tstate, PyExc_TypeError,
5299 "module __name__ must be a string, not %.100s",
5300 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005301 }
5302 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005303 _PyErr_Format(tstate, PyExc_TypeError,
5304 "%s in %U.%s must be str, not %.100s",
5305 skip_leading_underscores ? "Key" : "Item",
5306 modname,
5307 skip_leading_underscores ? "__dict__" : "__all__",
5308 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005309 }
5310 Py_DECREF(modname);
5311 Py_DECREF(name);
5312 err = -1;
5313 break;
5314 }
5315 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005316 if (PyUnicode_READY(name) == -1) {
5317 Py_DECREF(name);
5318 err = -1;
5319 break;
5320 }
5321 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5322 Py_DECREF(name);
5323 continue;
5324 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005325 }
5326 value = PyObject_GetAttr(v, name);
5327 if (value == NULL)
5328 err = -1;
5329 else if (PyDict_CheckExact(locals))
5330 err = PyDict_SetItem(locals, name, value);
5331 else
5332 err = PyObject_SetItem(locals, name, value);
5333 Py_DECREF(name);
5334 Py_XDECREF(value);
5335 if (err != 0)
5336 break;
5337 }
5338 Py_DECREF(all);
5339 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005340}
5341
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005342static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005343check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005344{
5345 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005346 _PyErr_Format(tstate, PyExc_TypeError,
5347 "%.200s%.200s argument after * "
5348 "must be an iterable, not %.200s",
5349 PyEval_GetFuncName(func),
5350 PyEval_GetFuncDesc(func),
5351 args->ob_type->tp_name);
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005352 return -1;
5353 }
5354 return 0;
5355}
5356
5357static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005358format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005359{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005360 /* _PyDict_MergeEx raises attribute
5361 * error (percolated from an attempt
5362 * to get 'keys' attribute) instead of
5363 * a type error if its second argument
5364 * is not a mapping.
5365 */
Victor Stinner438a12d2019-05-24 17:01:38 +02005366 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
5367 _PyErr_Format(tstate, PyExc_TypeError,
5368 "%.200s%.200s argument after ** "
5369 "must be a mapping, not %.200s",
5370 PyEval_GetFuncName(func),
5371 PyEval_GetFuncDesc(func),
5372 kwargs->ob_type->tp_name);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005373 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005374 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005375 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02005376 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005377 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
5378 PyObject *key = PyTuple_GET_ITEM(val, 0);
5379 if (!PyUnicode_Check(key)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005380 _PyErr_Format(tstate, PyExc_TypeError,
5381 "%.200s%.200s keywords must be strings",
5382 PyEval_GetFuncName(func),
5383 PyEval_GetFuncDesc(func));
5384 }
5385 else {
5386 _PyErr_Format(tstate, PyExc_TypeError,
5387 "%.200s%.200s got multiple "
5388 "values for keyword argument '%U'",
5389 PyEval_GetFuncName(func),
5390 PyEval_GetFuncDesc(func),
5391 key);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005392 }
5393 Py_XDECREF(exc);
5394 Py_XDECREF(val);
5395 Py_XDECREF(tb);
5396 }
5397 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005398 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005399 }
5400 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005401}
5402
Guido van Rossumac7be682001-01-17 15:42:30 +00005403static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005404format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
5405 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005406{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005407 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005409 if (!obj)
5410 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005411
Serhiy Storchaka06515832016-11-20 09:13:07 +02005412 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005413 if (!obj_str)
5414 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005415
Victor Stinner438a12d2019-05-24 17:01:38 +02005416 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005417}
Guido van Rossum950361c1997-01-24 13:49:28 +00005418
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005419static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005420format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005421{
5422 PyObject *name;
5423 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02005424 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005425 return;
5426 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5427 name = PyTuple_GET_ITEM(co->co_cellvars,
5428 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005429 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005430 PyExc_UnboundLocalError,
5431 UNBOUNDLOCAL_ERROR_MSG,
5432 name);
5433 } else {
5434 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5435 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02005436 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005437 UNBOUNDFREE_ERROR_MSG, name);
5438 }
5439}
5440
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005441static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005442format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005443{
5444 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5445 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005446 _PyErr_Format(tstate, PyExc_TypeError,
5447 "'async with' received an object from __aenter__ "
5448 "that does not implement __await__: %.100s",
5449 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005450 }
5451 else if (prevopcode == WITH_CLEANUP_START) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005452 _PyErr_Format(tstate, PyExc_TypeError,
5453 "'async with' received an object from __aexit__ "
5454 "that does not implement __await__: %.100s",
5455 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005456 }
5457 }
5458}
5459
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005460static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005461unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005462 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005463{
5464 PyObject *res;
5465 if (Py_REFCNT(v) == 2) {
5466 /* In the common case, there are 2 references to the value
5467 * stored in 'variable' when the += is performed: one on the
5468 * value stack (in 'v') and one still stored in the
5469 * 'variable'. We try to delete the variable now to reduce
5470 * the refcnt to 1.
5471 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005472 int opcode, oparg;
5473 NEXTOPARG();
5474 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005475 case STORE_FAST:
5476 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005477 PyObject **fastlocals = f->f_localsplus;
5478 if (GETLOCAL(oparg) == v)
5479 SETLOCAL(oparg, NULL);
5480 break;
5481 }
5482 case STORE_DEREF:
5483 {
5484 PyObject **freevars = (f->f_localsplus +
5485 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005486 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005487 if (PyCell_GET(c) == v) {
5488 PyCell_SET(c, NULL);
5489 Py_DECREF(v);
5490 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005491 break;
5492 }
5493 case STORE_NAME:
5494 {
5495 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005496 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005497 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005498 if (locals && PyDict_CheckExact(locals)) {
5499 PyObject *w = PyDict_GetItemWithError(locals, name);
5500 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02005501 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005502 {
5503 Py_DECREF(v);
5504 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005505 }
5506 }
5507 break;
5508 }
5509 }
5510 }
5511 res = v;
5512 PyUnicode_Append(&res, w);
5513 return res;
5514}
5515
Guido van Rossum950361c1997-01-24 13:49:28 +00005516#ifdef DYNAMIC_EXECUTION_PROFILE
5517
Skip Montanarof118cb12001-10-15 20:51:38 +00005518static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005519getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005520{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005521 int i;
5522 PyObject *l = PyList_New(256);
5523 if (l == NULL) return NULL;
5524 for (i = 0; i < 256; i++) {
5525 PyObject *x = PyLong_FromLong(a[i]);
5526 if (x == NULL) {
5527 Py_DECREF(l);
5528 return NULL;
5529 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005530 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005531 }
5532 for (i = 0; i < 256; i++)
5533 a[i] = 0;
5534 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005535}
5536
5537PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005538_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005539{
5540#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005541 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005542#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005543 int i;
5544 PyObject *l = PyList_New(257);
5545 if (l == NULL) return NULL;
5546 for (i = 0; i < 257; i++) {
5547 PyObject *x = getarray(dxpairs[i]);
5548 if (x == NULL) {
5549 Py_DECREF(l);
5550 return NULL;
5551 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005552 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005553 }
5554 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005555#endif
5556}
5557
5558#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005559
5560Py_ssize_t
5561_PyEval_RequestCodeExtraIndex(freefunc free)
5562{
Victor Stinnercaba55b2018-08-03 15:33:52 +02005563 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -07005564 Py_ssize_t new_index;
5565
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005566 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005567 return -1;
5568 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005569 new_index = interp->co_extra_user_count++;
5570 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005571 return new_index;
5572}
Łukasz Langaa785c872016-09-09 17:37:37 -07005573
5574static void
5575dtrace_function_entry(PyFrameObject *f)
5576{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005577 const char *filename;
5578 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005579 int lineno;
5580
5581 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5582 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5583 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5584
5585 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5586}
5587
5588static void
5589dtrace_function_return(PyFrameObject *f)
5590{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005591 const char *filename;
5592 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005593 int lineno;
5594
5595 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5596 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5597 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5598
5599 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5600}
5601
5602/* DTrace equivalent of maybe_call_line_trace. */
5603static void
5604maybe_dtrace_line(PyFrameObject *frame,
5605 int *instr_lb, int *instr_ub, int *instr_prev)
5606{
5607 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005608 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005609
5610 /* If the last instruction executed isn't in the current
5611 instruction window, reset the window.
5612 */
5613 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5614 PyAddrPair bounds;
5615 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5616 &bounds);
5617 *instr_lb = bounds.ap_lower;
5618 *instr_ub = bounds.ap_upper;
5619 }
5620 /* If the last instruction falls at the start of a line or if
5621 it represents a jump backwards, update the frame's line
5622 number and call the trace function. */
5623 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5624 frame->f_lineno = line;
5625 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5626 if (!co_filename)
5627 co_filename = "?";
5628 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5629 if (!co_name)
5630 co_name = "?";
5631 PyDTrace_LINE(co_filename, co_name, line);
5632 }
5633 *instr_prev = frame->f_lasti;
5634}