blob: 2bf64eda4224af8fb0e47b9edc7228d0daecbe78 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Thomas Wouters477c8d52006-05-27 19:21:47 +00009/* enable more aggressive intra-module optimizations, where available */
10#define PY_LOCAL_AGGRESSIVE
11
Guido van Rossumb209a111997-04-29 18:18:01 +000012#include "Python.h"
Victor Stinner4d231bc2019-11-14 13:36:21 +010013#include "pycore_call.h"
Victor Stinner09532fe2019-05-10 23:39:09 +020014#include "pycore_ceval.h"
Inada Naoki91234a12019-06-03 21:30:58 +090015#include "pycore_code.h"
Victor Stinnerbcda8f12018-11-21 22:27:47 +010016#include "pycore_object.h"
Victor Stinner438a12d2019-05-24 17:01:38 +020017#include "pycore_pyerrors.h"
18#include "pycore_pylifecycle.h"
Victor Stinner621cebe2018-11-12 16:53:38 +010019#include "pycore_pystate.h"
Victor Stinnerec13b932018-11-25 23:56:17 +010020#include "pycore_tupleobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000021
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000022#include "code.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040023#include "dictobject.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000024#include "frameobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000025#include "opcode.h"
Łukasz Langaa785c872016-09-09 17:37:37 -070026#include "pydtrace.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040027#include "setobject.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000028#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000029
Guido van Rossumc6004111993-11-05 10:22:19 +000030#include <ctype.h>
31
Guido van Rossum408027e1996-12-30 16:17:54 +000032#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000033/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000034#define LLTRACE 1 /* Low-level trace feature */
35#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000036#endif
37
Victor Stinner5c75f372019-04-17 23:02:26 +020038#if !defined(Py_BUILD_CORE)
39# error "ceval.c must be build with Py_BUILD_CORE define for best performance"
40#endif
41
Guido van Rossum5b722181993-03-30 17:46:03 +000042
Guido van Rossum374a9221991-04-04 10:40:29 +000043/* Forward declarations */
Victor Stinner09532fe2019-05-10 23:39:09 +020044Py_LOCAL_INLINE(PyObject *) call_function(
45 PyThreadState *tstate, PyObject ***pp_stack,
46 Py_ssize_t oparg, PyObject *kwnames);
47static PyObject * do_call_core(
48 PyThreadState *tstate, PyObject *func,
49 PyObject *callargs, PyObject *kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +000050
Guido van Rossum0a066c01992-03-27 17:29:15 +000051#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000052static int lltrace;
Victor Stinner438a12d2019-05-24 17:01:38 +020053static int prtrace(PyThreadState *, PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000054#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010055static int call_trace(Py_tracefunc, PyObject *,
56 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000057 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000058static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010059 PyThreadState *, PyFrameObject *,
60 int, PyObject *);
61static void call_exc_trace(Py_tracefunc, PyObject *,
62 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000063static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Eric Snow2ebc5ce2017-09-07 23:51:28 -060064 PyThreadState *, PyFrameObject *,
65 int *, int *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070066static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *);
67static void dtrace_function_entry(PyFrameObject *);
68static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000069
Victor Stinner438a12d2019-05-24 17:01:38 +020070static PyObject * import_name(PyThreadState *, PyFrameObject *,
71 PyObject *, PyObject *, PyObject *);
72static PyObject * import_from(PyThreadState *, PyObject *, PyObject *);
73static int import_all_from(PyThreadState *, PyObject *, PyObject *);
74static void format_exc_check_arg(PyThreadState *, PyObject *, const char *, PyObject *);
75static void format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg);
76static PyObject * unicode_concatenate(PyThreadState *, PyObject *, PyObject *,
Serhiy Storchakaab874002016-09-11 13:48:15 +030077 PyFrameObject *, const _Py_CODEUNIT *);
Victor Stinner438a12d2019-05-24 17:01:38 +020078static PyObject * special_lookup(PyThreadState *, PyObject *, _Py_Identifier *);
79static int check_args_iterable(PyThreadState *, PyObject *func, PyObject *vararg);
80static void format_kwargs_error(PyThreadState *, PyObject *func, PyObject *kwargs);
Mark Shannonfee55262019-11-21 09:11:43 +000081static void format_awaitable_error(PyThreadState *, PyTypeObject *, int, int);
Guido van Rossum374a9221991-04-04 10:40:29 +000082
Paul Prescode68140d2000-08-30 20:25:01 +000083#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000085#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000087#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 "free variable '%.200s' referenced before assignment" \
89 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000090
Guido van Rossum950361c1997-01-24 13:49:28 +000091/* Dynamic execution profile */
92#ifdef DYNAMIC_EXECUTION_PROFILE
93#ifdef DXPAIRS
94static long dxpairs[257][256];
95#define dxp dxpairs[256]
96#else
97static long dxp[256];
98#endif
99#endif
100
Inada Naoki91234a12019-06-03 21:30:58 +0900101/* per opcode cache */
Inada Naokieddef862019-06-04 07:38:10 +0900102#ifdef Py_DEBUG
103// --with-pydebug is used to find memory leak. opcache makes it harder.
104// So we disable opcache when Py_DEBUG is defined.
105// See bpo-37146
106#define OPCACHE_MIN_RUNS 0 /* disable opcache */
107#else
Inada Naoki91234a12019-06-03 21:30:58 +0900108#define OPCACHE_MIN_RUNS 1024 /* create opcache when code executed this time */
Inada Naokieddef862019-06-04 07:38:10 +0900109#endif
Inada Naoki91234a12019-06-03 21:30:58 +0900110#define OPCACHE_STATS 0 /* Enable stats */
111
112#if OPCACHE_STATS
113static size_t opcache_code_objects = 0;
114static size_t opcache_code_objects_extra_mem = 0;
115
116static size_t opcache_global_opts = 0;
117static size_t opcache_global_hits = 0;
118static size_t opcache_global_misses = 0;
119#endif
120
Victor Stinnere225beb2019-06-03 18:14:24 +0200121#define GIL_REQUEST _Py_atomic_load_relaxed(&ceval->gil_drop_request)
Inada Naoki91234a12019-06-03 21:30:58 +0900122
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000123/* This can set eval_breaker to 0 even though gil_drop_request became
124 1. We believe this is all right because the eval loop will release
125 the GIL eventually anyway. */
Victor Stinnere225beb2019-06-03 18:14:24 +0200126#define COMPUTE_EVAL_BREAKER(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 _Py_atomic_store_relaxed( \
Victor Stinnere225beb2019-06-03 18:14:24 +0200128 &(ceval)->eval_breaker, \
129 GIL_REQUEST | \
130 _Py_atomic_load_relaxed(&(ceval)->signals_pending) | \
131 _Py_atomic_load_relaxed(&(ceval)->pending.calls_to_do) | \
132 (ceval)->pending.async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000133
Victor Stinnere225beb2019-06-03 18:14:24 +0200134#define SET_GIL_DROP_REQUEST(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200136 _Py_atomic_store_relaxed(&(ceval)->gil_drop_request, 1); \
137 _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000138 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000139
Victor Stinnere225beb2019-06-03 18:14:24 +0200140#define RESET_GIL_DROP_REQUEST(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000141 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200142 _Py_atomic_store_relaxed(&(ceval)->gil_drop_request, 0); \
143 COMPUTE_EVAL_BREAKER(ceval); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000145
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000146/* Pending calls are only modified under pending_lock */
Victor Stinnere225beb2019-06-03 18:14:24 +0200147#define SIGNAL_PENDING_CALLS(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200149 _Py_atomic_store_relaxed(&(ceval)->pending.calls_to_do, 1); \
150 _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000152
Victor Stinnere225beb2019-06-03 18:14:24 +0200153#define UNSIGNAL_PENDING_CALLS(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000154 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200155 _Py_atomic_store_relaxed(&(ceval)->pending.calls_to_do, 0); \
156 COMPUTE_EVAL_BREAKER(ceval); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000157 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000158
Victor Stinnere225beb2019-06-03 18:14:24 +0200159#define SIGNAL_PENDING_SIGNALS(ceval) \
Eric Snowfdf282d2019-01-11 14:26:55 -0700160 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200161 _Py_atomic_store_relaxed(&(ceval)->signals_pending, 1); \
162 _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \
Eric Snowfdf282d2019-01-11 14:26:55 -0700163 } while (0)
164
Victor Stinnere225beb2019-06-03 18:14:24 +0200165#define UNSIGNAL_PENDING_SIGNALS(ceval) \
Eric Snowfdf282d2019-01-11 14:26:55 -0700166 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200167 _Py_atomic_store_relaxed(&(ceval)->signals_pending, 0); \
168 COMPUTE_EVAL_BREAKER(ceval); \
Eric Snowfdf282d2019-01-11 14:26:55 -0700169 } while (0)
170
Victor Stinnere225beb2019-06-03 18:14:24 +0200171#define SIGNAL_ASYNC_EXC(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200173 (ceval)->pending.async_exc = 1; \
174 _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000176
Victor Stinnere225beb2019-06-03 18:14:24 +0200177#define UNSIGNAL_ASYNC_EXC(ceval) \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600178 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200179 (ceval)->pending.async_exc = 0; \
180 COMPUTE_EVAL_BREAKER(ceval); \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600181 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000182
183
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000184#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000185#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000186#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000187#include "pythread.h"
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000188#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000189
Tim Peters7f468f22004-10-11 02:40:51 +0000190int
191PyEval_ThreadsInitialized(void)
192{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100193 _PyRuntimeState *runtime = &_PyRuntime;
194 return gil_created(&runtime->ceval.gil);
Tim Peters7f468f22004-10-11 02:40:51 +0000195}
196
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000197void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000198PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000199{
Victor Stinner09532fe2019-05-10 23:39:09 +0200200 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200201 struct _ceval_runtime_state *ceval = &runtime->ceval;
202 struct _gil_runtime_state *gil = &ceval->gil;
Victor Stinner09532fe2019-05-10 23:39:09 +0200203 if (gil_created(gil)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 return;
Victor Stinnera7126792019-03-19 14:19:38 +0100205 }
206
Inada Naoki001fee12019-02-20 10:00:09 +0900207 PyThread_init_thread();
Victor Stinner09532fe2019-05-10 23:39:09 +0200208 create_gil(gil);
209 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinnere225beb2019-06-03 18:14:24 +0200210 take_gil(ceval, tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700211
Victor Stinnere225beb2019-06-03 18:14:24 +0200212 struct _pending_calls *pending = &ceval->pending;
213 pending->lock = PyThread_allocate_lock();
214 if (pending->lock == NULL) {
215 Py_FatalError("Can't initialize threads for pending calls");
216 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000217}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000218
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000219void
Victor Stinnere225beb2019-06-03 18:14:24 +0200220_PyEval_FiniThreads(struct _ceval_runtime_state *ceval)
Antoine Pitrou1df15362010-09-13 14:16:46 +0000221{
Victor Stinnere225beb2019-06-03 18:14:24 +0200222 struct _gil_runtime_state *gil = &ceval->gil;
Victor Stinner09532fe2019-05-10 23:39:09 +0200223 if (!gil_created(gil)) {
Antoine Pitrou1df15362010-09-13 14:16:46 +0000224 return;
Victor Stinnera7126792019-03-19 14:19:38 +0100225 }
226
Victor Stinner09532fe2019-05-10 23:39:09 +0200227 destroy_gil(gil);
228 assert(!gil_created(gil));
Victor Stinner99fcc612019-04-29 13:04:07 +0200229
Victor Stinnere225beb2019-06-03 18:14:24 +0200230 struct _pending_calls *pending = &ceval->pending;
231 if (pending->lock != NULL) {
232 PyThread_free_lock(pending->lock);
233 pending->lock = NULL;
234 }
Antoine Pitrou1df15362010-09-13 14:16:46 +0000235}
236
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400237static inline void
Victor Stinner01b1cc12019-11-20 02:27:56 +0100238exit_thread_if_finalizing(PyThreadState *tstate)
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400239{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100240 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200241 /* _Py_Finalizing is protected by the GIL */
Victor Stinner09532fe2019-05-10 23:39:09 +0200242 if (runtime->finalizing != NULL && !_Py_CURRENTLY_FINALIZING(runtime, tstate)) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200243 drop_gil(&runtime->ceval, tstate);
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400244 PyThread_exit_thread();
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400245 }
246}
247
Antoine Pitrou1df15362010-09-13 14:16:46 +0000248void
Inada Naoki91234a12019-06-03 21:30:58 +0900249_PyEval_Fini(void)
250{
251#if OPCACHE_STATS
252 fprintf(stderr, "-- Opcode cache number of objects = %zd\n",
253 opcache_code_objects);
254
255 fprintf(stderr, "-- Opcode cache total extra mem = %zd\n",
256 opcache_code_objects_extra_mem);
257
258 fprintf(stderr, "\n");
259
260 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL hits = %zd (%d%%)\n",
261 opcache_global_hits,
262 (int) (100.0 * opcache_global_hits /
263 (opcache_global_hits + opcache_global_misses)));
264
265 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL misses = %zd (%d%%)\n",
266 opcache_global_misses,
267 (int) (100.0 * opcache_global_misses /
268 (opcache_global_hits + opcache_global_misses)));
269
270 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL opts = %zd\n",
271 opcache_global_opts);
272
273 fprintf(stderr, "\n");
274#endif
275}
276
277void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000278PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000279{
Victor Stinner09532fe2019-05-10 23:39:09 +0200280 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200281 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinner09532fe2019-05-10 23:39:09 +0200282 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
283 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
Victor Stinner09532fe2019-05-10 23:39:09 +0200285 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200286 take_gil(ceval, tstate);
Victor Stinner01b1cc12019-11-20 02:27:56 +0100287 exit_thread_if_finalizing(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000288}
289
290void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000291PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000292{
Victor Stinner09532fe2019-05-10 23:39:09 +0200293 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200294 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100296 We therefore avoid PyThreadState_Get() which dumps a fatal error
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 in debug mode.
298 */
Victor Stinnere225beb2019-06-03 18:14:24 +0200299 drop_gil(&runtime->ceval, tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000300}
301
302void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000303PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000304{
Victor Stinner17c68b82020-01-30 12:20:48 +0100305 assert(tstate != NULL);
Victor Stinnere225beb2019-06-03 18:14:24 +0200306
Victor Stinner01b1cc12019-11-20 02:27:56 +0100307 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200308 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinner09532fe2019-05-10 23:39:09 +0200309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 /* Check someone has called PyEval_InitThreads() to create the lock */
Victor Stinnere225beb2019-06-03 18:14:24 +0200311 assert(gil_created(&ceval->gil));
312 take_gil(ceval, tstate);
Victor Stinner01b1cc12019-11-20 02:27:56 +0100313 exit_thread_if_finalizing(tstate);
Victor Stinner09532fe2019-05-10 23:39:09 +0200314 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
315 Py_FatalError("PyEval_AcquireThread: non-NULL old thread state");
316 }
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000317}
318
319void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000320PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000321{
Victor Stinner17c68b82020-01-30 12:20:48 +0100322 assert(tstate != NULL);
Victor Stinner09532fe2019-05-10 23:39:09 +0200323
Victor Stinner01b1cc12019-11-20 02:27:56 +0100324 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200325 PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
326 if (new_tstate != tstate) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200328 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200329 drop_gil(&runtime->ceval, tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000330}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000331
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200332/* This function is called from PyOS_AfterFork_Child to destroy all threads
333 * which are not running in the child process, and clear internal locks
334 * which might be held by those threads.
335 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000336
337void
Victor Stinnerd5d9e812019-05-13 12:35:37 +0200338_PyEval_ReInitThreads(_PyRuntimeState *runtime)
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000339{
Victor Stinnere225beb2019-06-03 18:14:24 +0200340 struct _ceval_runtime_state *ceval = &runtime->ceval;
341 if (!gil_created(&ceval->gil)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 return;
Victor Stinner09532fe2019-05-10 23:39:09 +0200343 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200344 recreate_gil(&ceval->gil);
Victor Stinner09532fe2019-05-10 23:39:09 +0200345 PyThreadState *current_tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinnere225beb2019-06-03 18:14:24 +0200346 take_gil(ceval, current_tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700347
Victor Stinnere225beb2019-06-03 18:14:24 +0200348 struct _pending_calls *pending = &ceval->pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200349 pending->lock = PyThread_allocate_lock();
350 if (pending->lock == NULL) {
Eric Snow8479a342019-03-08 23:44:33 -0700351 Py_FatalError("Can't initialize threads for pending calls");
352 }
Jesse Nollera8513972008-07-17 16:49:17 +0000353
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200354 /* Destroy all threads except the current one */
Victor Stinner0fd2c302019-06-04 03:15:09 +0200355 _PyThreadState_DeleteExcept(runtime, current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000356}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000357
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000358/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600359 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000360
361void
Victor Stinnere225beb2019-06-03 18:14:24 +0200362_PyEval_SignalAsyncExc(struct _ceval_runtime_state *ceval)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000363{
Victor Stinnere225beb2019-06-03 18:14:24 +0200364 SIGNAL_ASYNC_EXC(ceval);
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000365}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000366
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000367PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000368PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000369{
Victor Stinner09532fe2019-05-10 23:39:09 +0200370 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200371 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinner09532fe2019-05-10 23:39:09 +0200372 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
373 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 Py_FatalError("PyEval_SaveThread: NULL tstate");
Victor Stinner09532fe2019-05-10 23:39:09 +0200375 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200376 assert(gil_created(&ceval->gil));
377 drop_gil(ceval, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000379}
380
381void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000382PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000383{
Victor Stinner17c68b82020-01-30 12:20:48 +0100384 assert(tstate != NULL);
385
Victor Stinner01b1cc12019-11-20 02:27:56 +0100386 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner0fd2c302019-06-04 03:15:09 +0200387 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinnere225beb2019-06-03 18:14:24 +0200388 assert(gil_created(&ceval->gil));
Victor Stinner2914bb32018-01-29 11:57:45 +0100389
390 int err = errno;
Victor Stinnere225beb2019-06-03 18:14:24 +0200391 take_gil(ceval, tstate);
Victor Stinner01b1cc12019-11-20 02:27:56 +0100392 exit_thread_if_finalizing(tstate);
Victor Stinner2914bb32018-01-29 11:57:45 +0100393 errno = err;
394
Victor Stinner09532fe2019-05-10 23:39:09 +0200395 _PyThreadState_Swap(&runtime->gilstate, tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000396}
397
398
Guido van Rossuma9672091994-09-14 13:31:22 +0000399/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
400 signal handlers or Mac I/O completion routines) can schedule calls
401 to a function to be called synchronously.
402 The synchronous function is called with one void* argument.
403 It should return 0 for success or -1 for failure -- failure should
404 be accompanied by an exception.
405
406 If registry succeeds, the registry function returns 0; if it fails
407 (e.g. due to too many pending calls) it returns -1 (without setting
408 an exception condition).
409
410 Note that because registry may occur from within signal handlers,
411 or other asynchronous events, calling malloc() is unsafe!
412
Guido van Rossuma9672091994-09-14 13:31:22 +0000413 Any thread can schedule pending calls, but only the main thread
414 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000415 There is no facility to schedule calls to a particular thread, but
416 that should be easy to change, should that ever be required. In
417 that case, the static variables here should go into the python
418 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000419*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000420
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200421void
Victor Stinnere225beb2019-06-03 18:14:24 +0200422_PyEval_SignalReceived(struct _ceval_runtime_state *ceval)
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200423{
424 /* bpo-30703: Function called when the C signal handler of Python gets a
425 signal. We cannot queue a callback using Py_AddPendingCall() since
426 that function is not async-signal-safe. */
Victor Stinnere225beb2019-06-03 18:14:24 +0200427 SIGNAL_PENDING_SIGNALS(ceval);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200428}
429
Eric Snow5be45a62019-03-08 22:47:07 -0700430/* Push one item onto the queue while holding the lock. */
431static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200432_push_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600433 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700434{
Eric Snow842a2f02019-03-15 15:47:51 -0600435 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700436 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600437 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700438 return -1; /* Queue full */
439 }
Eric Snow842a2f02019-03-15 15:47:51 -0600440 pending->calls[i].func = func;
441 pending->calls[i].arg = arg;
442 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700443 return 0;
444}
445
446/* Pop one item off the queue while holding the lock. */
447static void
Victor Stinnere225beb2019-06-03 18:14:24 +0200448_pop_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600449 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700450{
Eric Snow842a2f02019-03-15 15:47:51 -0600451 int i = pending->first;
452 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700453 return; /* Queue empty */
454 }
455
Eric Snow842a2f02019-03-15 15:47:51 -0600456 *func = pending->calls[i].func;
457 *arg = pending->calls[i].arg;
458 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700459}
460
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200461/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000462 scheduling to be made from any thread, and even from an executing
463 callback.
464 */
465
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000466int
Victor Stinner438a12d2019-05-24 17:01:38 +0200467_PyEval_AddPendingCall(PyThreadState *tstate,
Victor Stinnere225beb2019-06-03 18:14:24 +0200468 struct _ceval_runtime_state *ceval,
Victor Stinner09532fe2019-05-10 23:39:09 +0200469 int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000470{
Victor Stinnere225beb2019-06-03 18:14:24 +0200471 struct _pending_calls *pending = &ceval->pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600472
473 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
474 if (pending->finishing) {
475 PyThread_release_lock(pending->lock);
476
477 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +0200478 _PyErr_Fetch(tstate, &exc, &val, &tb);
479 _PyErr_SetString(tstate, PyExc_SystemError,
Eric Snow842a2f02019-03-15 15:47:51 -0600480 "Py_AddPendingCall: cannot add pending calls "
481 "(Python shutting down)");
Victor Stinner438a12d2019-05-24 17:01:38 +0200482 _PyErr_Print(tstate);
483 _PyErr_Restore(tstate, exc, val, tb);
Eric Snow842a2f02019-03-15 15:47:51 -0600484 return -1;
485 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200486 int result = _push_pending_call(pending, func, arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600487 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700488
Victor Stinnere225beb2019-06-03 18:14:24 +0200489 /* signal main loop */
490 SIGNAL_PENDING_CALLS(ceval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000492}
493
Victor Stinner09532fe2019-05-10 23:39:09 +0200494int
495Py_AddPendingCall(int (*func)(void *), void *arg)
496{
Victor Stinner438a12d2019-05-24 17:01:38 +0200497 _PyRuntimeState *runtime = &_PyRuntime;
498 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinnere225beb2019-06-03 18:14:24 +0200499 return _PyEval_AddPendingCall(tstate, &runtime->ceval, func, arg);
Victor Stinner09532fe2019-05-10 23:39:09 +0200500}
501
Eric Snowfdf282d2019-01-11 14:26:55 -0700502static int
Victor Stinner09532fe2019-05-10 23:39:09 +0200503handle_signals(_PyRuntimeState *runtime)
Eric Snowfdf282d2019-01-11 14:26:55 -0700504{
Eric Snow5be45a62019-03-08 22:47:07 -0700505 /* Only handle signals on main thread. PyEval_InitThreads must
506 * have been called already.
507 */
Victor Stinner09532fe2019-05-10 23:39:09 +0200508 if (PyThread_get_thread_ident() != runtime->main_thread) {
Eric Snowfdf282d2019-01-11 14:26:55 -0700509 return 0;
510 }
Eric Snow64d6cc82019-02-23 15:40:43 -0700511 /*
512 * Ensure that the thread isn't currently running some other
513 * interpreter.
514 */
Victor Stinner09532fe2019-05-10 23:39:09 +0200515 PyInterpreterState *interp = _PyRuntimeState_GetThreadState(runtime)->interp;
516 if (interp != runtime->interpreters.main) {
Eric Snow64d6cc82019-02-23 15:40:43 -0700517 return 0;
518 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700519
Victor Stinnere225beb2019-06-03 18:14:24 +0200520 struct _ceval_runtime_state *ceval = &runtime->ceval;
521 UNSIGNAL_PENDING_SIGNALS(ceval);
Eric Snow64d6cc82019-02-23 15:40:43 -0700522 if (_PyErr_CheckSignals() < 0) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200523 SIGNAL_PENDING_SIGNALS(ceval); /* We're not done yet */
Eric Snowfdf282d2019-01-11 14:26:55 -0700524 return -1;
525 }
526 return 0;
527}
528
529static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200530make_pending_calls(_PyRuntimeState *runtime)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000531{
Eric Snow6a150bc2019-06-01 15:39:46 -0600532 static int busy = 0;
Eric Snowb75b1a352019-04-12 10:20:10 -0600533
Victor Stinnere225beb2019-06-03 18:14:24 +0200534 /* only service pending calls on main thread */
535 if (PyThread_get_thread_ident() != runtime->main_thread) {
536 return 0;
537 }
538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 /* don't perform recursive pending calls */
Eric Snowfdf282d2019-01-11 14:26:55 -0700540 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700542 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200543 busy = 1;
Victor Stinnere225beb2019-06-03 18:14:24 +0200544 struct _ceval_runtime_state *ceval = &runtime->ceval;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200545 /* unsignal before starting to call callbacks, so that any callback
546 added in-between re-signals */
Victor Stinnere225beb2019-06-03 18:14:24 +0200547 UNSIGNAL_PENDING_CALLS(ceval);
Eric Snowfdf282d2019-01-11 14:26:55 -0700548 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 /* perform a bounded number of calls, in case of recursion */
Victor Stinnere225beb2019-06-03 18:14:24 +0200551 struct _pending_calls *pending = &ceval->pending;
Eric Snowfdf282d2019-01-11 14:26:55 -0700552 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700553 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 void *arg = NULL;
555
556 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600557 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200558 _pop_pending_call(pending, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600559 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700560
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100561 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700562 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100563 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700564 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700565 res = func(arg);
566 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200567 goto error;
568 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200570
Charles-François Natalif23339a2011-07-23 18:15:43 +0200571 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700572 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200573
574error:
575 busy = 0;
Victor Stinnere225beb2019-06-03 18:14:24 +0200576 SIGNAL_PENDING_CALLS(ceval);
Eric Snowfdf282d2019-01-11 14:26:55 -0700577 return res;
578}
579
Eric Snow842a2f02019-03-15 15:47:51 -0600580void
Victor Stinner2b1df452020-01-13 18:46:59 +0100581_Py_FinishPendingCalls(PyThreadState *tstate)
Eric Snow842a2f02019-03-15 15:47:51 -0600582{
Eric Snow842a2f02019-03-15 15:47:51 -0600583 assert(PyGILState_Check());
584
Victor Stinner2b1df452020-01-13 18:46:59 +0100585 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200586 struct _pending_calls *pending = &runtime->ceval.pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200587
Eric Snow842a2f02019-03-15 15:47:51 -0600588 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
589 pending->finishing = 1;
590 PyThread_release_lock(pending->lock);
591
592 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
593 return;
594 }
595
Victor Stinnere225beb2019-06-03 18:14:24 +0200596 if (make_pending_calls(runtime) < 0) {
597 PyObject *exc, *val, *tb;
598 _PyErr_Fetch(tstate, &exc, &val, &tb);
599 PyErr_BadInternalCall();
600 _PyErr_ChainExceptions(exc, val, tb);
601 _PyErr_Print(tstate);
Eric Snow842a2f02019-03-15 15:47:51 -0600602 }
603}
604
Eric Snowfdf282d2019-01-11 14:26:55 -0700605/* Py_MakePendingCalls() is a simple wrapper for the sake
606 of backward-compatibility. */
607int
608Py_MakePendingCalls(void)
609{
610 assert(PyGILState_Check());
611
612 /* Python signal handler doesn't really queue a callback: it only signals
613 that a signal was received, see _PyEval_SignalReceived(). */
Victor Stinner09532fe2019-05-10 23:39:09 +0200614 _PyRuntimeState *runtime = &_PyRuntime;
615 int res = handle_signals(runtime);
Eric Snowfdf282d2019-01-11 14:26:55 -0700616 if (res != 0) {
617 return res;
618 }
619
Victor Stinnere225beb2019-06-03 18:14:24 +0200620 res = make_pending_calls(runtime);
Eric Snowb75b1a352019-04-12 10:20:10 -0600621 if (res != 0) {
622 return res;
623 }
624
625 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000626}
627
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000628/* The interpreter's recursion limit */
629
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000630#ifndef Py_DEFAULT_RECURSION_LIMIT
631#define Py_DEFAULT_RECURSION_LIMIT 1000
632#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600633
Eric Snow05351c12017-09-05 21:43:08 -0700634int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000635
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600636void
Victor Stinnere225beb2019-06-03 18:14:24 +0200637_PyEval_Initialize(struct _ceval_runtime_state *state)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600638{
Victor Stinnere225beb2019-06-03 18:14:24 +0200639 state->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600640 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Victor Stinnere225beb2019-06-03 18:14:24 +0200641 _gil_initialize(&state->gil);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600642}
643
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000644int
645Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000646{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100647 struct _ceval_runtime_state *ceval = &_PyRuntime.ceval;
648 return ceval->recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000649}
650
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000651void
652Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000653{
Victor Stinnere225beb2019-06-03 18:14:24 +0200654 struct _ceval_runtime_state *ceval = &_PyRuntime.ceval;
655 ceval->recursion_limit = new_limit;
656 _Py_CheckRecursionLimit = ceval->recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000657}
658
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100659/* The function _Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
Armin Rigo2b3eb402003-10-28 12:05:48 +0000660 if the recursion_depth reaches _Py_CheckRecursionLimit.
661 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
662 to guarantee that _Py_CheckRecursiveCall() is regularly called.
663 Without USE_STACKCHECK, there is no need for this. */
664int
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100665_Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000666{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100667 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200668 int recursion_limit = runtime->ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000669
670#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700671 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 if (PyOS_CheckStack()) {
673 --tstate->recursion_depth;
Victor Stinner438a12d2019-05-24 17:01:38 +0200674 _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 return -1;
676 }
pdox18967932017-10-25 23:03:01 -0700677 /* Needed for ABI backwards-compatibility (see bpo-31857) */
Eric Snow05351c12017-09-05 21:43:08 -0700678 _Py_CheckRecursionLimit = recursion_limit;
pdox18967932017-10-25 23:03:01 -0700679#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 if (tstate->recursion_critical)
681 /* Somebody asked that we don't check for recursion. */
682 return 0;
683 if (tstate->overflowed) {
684 if (tstate->recursion_depth > recursion_limit + 50) {
685 /* Overflowing while handling an overflow. Give up. */
686 Py_FatalError("Cannot recover from stack overflow.");
687 }
688 return 0;
689 }
690 if (tstate->recursion_depth > recursion_limit) {
691 --tstate->recursion_depth;
692 tstate->overflowed = 1;
Victor Stinner438a12d2019-05-24 17:01:38 +0200693 _PyErr_Format(tstate, PyExc_RecursionError,
694 "maximum recursion depth exceeded%s",
695 where);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 return -1;
697 }
698 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000699}
700
Victor Stinner09532fe2019-05-10 23:39:09 +0200701static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
Victor Stinner438a12d2019-05-24 17:01:38 +0200702static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000703
Victor Stinnere225beb2019-06-03 18:14:24 +0200704#define _Py_TracingPossible(ceval) ((ceval)->tracing_possible)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000705
Guido van Rossum374a9221991-04-04 10:40:29 +0000706
Guido van Rossumb209a111997-04-29 18:18:01 +0000707PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000708PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000709{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 return PyEval_EvalCodeEx(co,
711 globals, locals,
712 (PyObject **)NULL, 0,
713 (PyObject **)NULL, 0,
714 (PyObject **)NULL, 0,
715 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000716}
717
718
719/* Interpreter main loop */
720
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000721PyObject *
Victor Stinnerb9e68122019-11-14 12:20:46 +0100722PyEval_EvalFrame(PyFrameObject *f)
723{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 /* This is for backward compatibility with extension modules that
725 used this API; core interpreter code should call
726 PyEval_EvalFrameEx() */
Victor Stinnerb9e68122019-11-14 12:20:46 +0100727 PyThreadState *tstate = _PyThreadState_GET();
728 return _PyEval_EvalFrame(tstate, f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000729}
730
731PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000732PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000733{
Victor Stinnerb9e68122019-11-14 12:20:46 +0100734 PyThreadState *tstate = _PyThreadState_GET();
735 return _PyEval_EvalFrame(tstate, f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700736}
737
Victor Stinnerc6944e72016-11-11 02:13:35 +0100738PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700739_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
740{
Guido van Rossum950361c1997-01-24 13:49:28 +0000741#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000743#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200744 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300745 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200746 int opcode; /* Current opcode */
747 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200748 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 PyObject *retval = NULL; /* Return value */
Victor Stinner09532fe2019-05-10 23:39:09 +0200750 _PyRuntimeState * const runtime = &_PyRuntime;
751 PyThreadState * const tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinnere225beb2019-06-03 18:14:24 +0200752 struct _ceval_runtime_state * const ceval = &runtime->ceval;
753 _Py_atomic_int * const eval_breaker = &ceval->eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000757
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000759
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 is true when the line being executed has changed. The
761 initial values are such as to make this false the first
762 time it is tested. */
763 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000764
Serhiy Storchakaab874002016-09-11 13:48:15 +0300765 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 PyObject *names;
767 PyObject *consts;
Inada Naoki91234a12019-06-03 21:30:58 +0900768 _PyOpcache *co_opcache;
Guido van Rossum374a9221991-04-04 10:40:29 +0000769
Brett Cannon368b4b72012-04-02 12:17:59 -0400770#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200771 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400772#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200773
Antoine Pitroub52ec782009-01-25 16:34:23 +0000774/* Computed GOTOs, or
775 the-optimization-commonly-but-improperly-known-as-"threaded code"
776 using gcc's labels-as-values extension
777 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
778
779 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000781 combined with a lookup table of jump addresses. However, since the
782 indirect jump instruction is shared by all opcodes, the CPU will have a
783 hard time making the right prediction for where to jump next (actually,
784 it will be always wrong except in the uncommon case of a sequence of
785 several identical opcodes).
786
787 "Threaded code" in contrast, uses an explicit jump table and an explicit
788 indirect jump instruction at the end of each opcode. Since the jump
789 instruction is at a different address for each opcode, the CPU will make a
790 separate prediction for each of these instructions, which is equivalent to
791 predicting the second opcode of each opcode pair. These predictions have
792 a much better chance to turn out valid, especially in small bytecode loops.
793
794 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000796 and potentially many more instructions (depending on the pipeline width).
797 A correctly predicted branch, however, is nearly free.
798
799 At the time of this writing, the "threaded code" version is up to 15-20%
800 faster than the normal "switch" version, depending on the compiler and the
801 CPU architecture.
802
803 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
804 because it would render the measurements invalid.
805
806
807 NOTE: care must be taken that the compiler doesn't try to "optimize" the
808 indirect jumps by sharing them between all opcodes. Such optimizations
809 can be disabled on gcc by using the -fno-gcse flag (or possibly
810 -fno-crossjumping).
811*/
812
Antoine Pitrou042b1282010-08-13 21:15:58 +0000813#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000814#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000815#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000816#endif
817
Antoine Pitrou042b1282010-08-13 21:15:58 +0000818#ifdef HAVE_COMPUTED_GOTOS
819 #ifndef USE_COMPUTED_GOTOS
820 #define USE_COMPUTED_GOTOS 1
821 #endif
822#else
823 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
824 #error "Computed gotos are not supported on this compiler."
825 #endif
826 #undef USE_COMPUTED_GOTOS
827 #define USE_COMPUTED_GOTOS 0
828#endif
829
830#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000831/* Import the static jump table */
832#include "opcode_targets.h"
833
Antoine Pitroub52ec782009-01-25 16:34:23 +0000834#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -0700835 op: \
836 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000837
Antoine Pitroub52ec782009-01-25 16:34:23 +0000838#ifdef LLTRACE
839#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200841 if (!lltrace && !_Py_TracingPossible(ceval) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300843 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300844 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 } \
846 goto fast_next_opcode; \
847 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000848#else
849#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200851 if (!_Py_TracingPossible(ceval) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300853 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300854 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 } \
856 goto fast_next_opcode; \
857 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000858#endif
859
Victor Stinner09532fe2019-05-10 23:39:09 +0200860#define DISPATCH() \
861 { \
862 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
863 FAST_DISPATCH(); \
864 } \
865 continue; \
866 }
867
Antoine Pitroub52ec782009-01-25 16:34:23 +0000868#else
Benjamin Petersonddd19492018-09-16 22:38:02 -0700869#define TARGET(op) op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000870#define FAST_DISPATCH() goto fast_next_opcode
Victor Stinner09532fe2019-05-10 23:39:09 +0200871#define DISPATCH() continue
Antoine Pitroub52ec782009-01-25 16:34:23 +0000872#endif
873
874
Neal Norwitza81d2202002-07-14 00:27:26 +0000875/* Tuple access macros */
876
877#ifndef Py_DEBUG
878#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
879#else
880#define GETITEM(v, i) PyTuple_GetItem((v), (i))
881#endif
882
Guido van Rossum374a9221991-04-04 10:40:29 +0000883/* Code access macros */
884
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300885/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600886#define INSTR_OFFSET() \
887 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300888#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300889 _Py_CODEUNIT word = *next_instr; \
890 opcode = _Py_OPCODE(word); \
891 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300892 next_instr++; \
893 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300894#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
895#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000896
Raymond Hettingerf606f872003-03-16 03:11:04 +0000897/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 Some opcodes tend to come in pairs thus making it possible to
899 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300900 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000901
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 Verifying the prediction costs a single high-speed test of a register
903 variable against a constant. If the pairing was good, then the
904 processor's own internal branch predication has a high likelihood of
905 success, resulting in a nearly zero-overhead transition to the
906 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300907 including its unpredictable switch-case branch. Combined with the
908 processor's internal branch prediction, a successful PREDICT has the
909 effect of making the two opcodes run as if they were a single new opcode
910 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000911
Georg Brandl86b2fb92008-07-16 03:43:04 +0000912 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 predictions turned-on and interpret the results as if some opcodes
914 had been combined or turn-off predictions so that the opcode frequency
915 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000916
917 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 the CPU to record separate branch prediction information for each
919 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000920
Raymond Hettingerf606f872003-03-16 03:11:04 +0000921*/
922
Antoine Pitrou042b1282010-08-13 21:15:58 +0000923#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000925#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300926#define PREDICT(op) \
927 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300928 _Py_CODEUNIT word = *next_instr; \
929 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300930 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300931 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300932 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300933 goto PRED_##op; \
934 } \
935 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000936#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300937#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000938
Raymond Hettingerf606f872003-03-16 03:11:04 +0000939
Guido van Rossum374a9221991-04-04 10:40:29 +0000940/* Stack manipulation macros */
941
Martin v. Löwis18e16552006-02-15 17:27:45 +0000942/* The stack can grow at most MAXINT deep, as co_nlocals and
943 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000944#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
945#define EMPTY() (STACK_LEVEL() == 0)
946#define TOP() (stack_pointer[-1])
947#define SECOND() (stack_pointer[-2])
948#define THIRD() (stack_pointer[-3])
949#define FOURTH() (stack_pointer[-4])
950#define PEEK(n) (stack_pointer[-(n)])
951#define SET_TOP(v) (stack_pointer[-1] = (v))
952#define SET_SECOND(v) (stack_pointer[-2] = (v))
953#define SET_THIRD(v) (stack_pointer[-3] = (v))
954#define SET_FOURTH(v) (stack_pointer[-4] = (v))
955#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
956#define BASIC_STACKADJ(n) (stack_pointer += n)
957#define BASIC_PUSH(v) (*stack_pointer++ = (v))
958#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000959
Guido van Rossum96a42c81992-01-12 02:29:51 +0000960#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +0200962 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000963 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +0200964#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000965 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +0000966#define STACK_GROW(n) do { \
967 assert(n >= 0); \
968 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +0200969 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +0000970 assert(STACK_LEVEL() <= co->co_stacksize); \
971 } while (0)
972#define STACK_SHRINK(n) do { \
973 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +0200974 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +0000975 (void)(BASIC_STACKADJ(-n)); \
976 assert(STACK_LEVEL() <= co->co_stacksize); \
977 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +0000978#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +0200979 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000980 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000981#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000982#define PUSH(v) BASIC_PUSH(v)
983#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +0000984#define STACK_GROW(n) BASIC_STACKADJ(n)
985#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000986#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000987#endif
988
Guido van Rossum681d79a1995-07-18 14:51:37 +0000989/* Local variable macros */
990
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000992
993/* The SETLOCAL() macro must not DECREF the local variable in-place and
994 then store the new value; it must copy the old value to a temporary
995 value, then store the new value, and then DECREF the temporary value.
996 This is because it is possible that during the DECREF the frame is
997 accessed by other code (e.g. a __del__ method or gc.collect()) and the
998 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001000 GETLOCAL(i) = value; \
1001 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001002
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001003
1004#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 while (STACK_LEVEL() > (b)->b_level) { \
1006 PyObject *v = POP(); \
1007 Py_XDECREF(v); \
1008 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001009
1010#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001011 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +01001013 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1015 while (STACK_LEVEL() > (b)->b_level + 3) { \
1016 value = POP(); \
1017 Py_XDECREF(value); \
1018 } \
Mark Shannonae3087c2017-10-22 22:41:51 +01001019 exc_info = tstate->exc_info; \
1020 type = exc_info->exc_type; \
1021 value = exc_info->exc_value; \
1022 traceback = exc_info->exc_traceback; \
1023 exc_info->exc_type = POP(); \
1024 exc_info->exc_value = POP(); \
1025 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 Py_XDECREF(type); \
1027 Py_XDECREF(value); \
1028 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001029 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001030
Inada Naoki91234a12019-06-03 21:30:58 +09001031 /* macros for opcode cache */
1032#define OPCACHE_CHECK() \
1033 do { \
1034 co_opcache = NULL; \
1035 if (co->co_opcache != NULL) { \
1036 unsigned char co_opt_offset = \
1037 co->co_opcache_map[next_instr - first_instr]; \
1038 if (co_opt_offset > 0) { \
1039 assert(co_opt_offset <= co->co_opcache_size); \
1040 co_opcache = &co->co_opcache[co_opt_offset - 1]; \
1041 assert(co_opcache != NULL); \
Inada Naoki91234a12019-06-03 21:30:58 +09001042 } \
1043 } \
1044 } while (0)
1045
1046#if OPCACHE_STATS
1047
1048#define OPCACHE_STAT_GLOBAL_HIT() \
1049 do { \
1050 if (co->co_opcache != NULL) opcache_global_hits++; \
1051 } while (0)
1052
1053#define OPCACHE_STAT_GLOBAL_MISS() \
1054 do { \
1055 if (co->co_opcache != NULL) opcache_global_misses++; \
1056 } while (0)
1057
1058#define OPCACHE_STAT_GLOBAL_OPT() \
1059 do { \
1060 if (co->co_opcache != NULL) opcache_global_opts++; \
1061 } while (0)
1062
1063#else /* OPCACHE_STATS */
1064
1065#define OPCACHE_STAT_GLOBAL_HIT()
1066#define OPCACHE_STAT_GLOBAL_MISS()
1067#define OPCACHE_STAT_GLOBAL_OPT()
1068
1069#endif
1070
Guido van Rossuma027efa1997-05-05 20:56:21 +00001071/* Start of code */
1072
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 /* push frame */
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001074 if (_Py_EnterRecursiveCall(tstate, "")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001076 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001077
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001079
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 if (tstate->use_tracing) {
1081 if (tstate->c_tracefunc != NULL) {
1082 /* tstate->c_tracefunc, if defined, is a
1083 function that will be called on *every* entry
1084 to a code block. Its return value, if not
1085 None, is a function that will be called at
1086 the start of each executed line of code.
1087 (Actually, the function must return itself
1088 in order to continue tracing.) The trace
1089 functions are called with three arguments:
1090 a pointer to the current frame, a string
1091 indicating why the function is called, and
1092 an argument which depends on the situation.
1093 The global trace function is also called
1094 whenever an exception is detected. */
1095 if (call_trace_protected(tstate->c_tracefunc,
1096 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001097 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 /* Trace function raised an error */
1099 goto exit_eval_frame;
1100 }
1101 }
1102 if (tstate->c_profilefunc != NULL) {
1103 /* Similar for c_profilefunc, except it needn't
1104 return itself and isn't called for "line" events */
1105 if (call_trace_protected(tstate->c_profilefunc,
1106 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001107 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 /* Profile function raised an error */
1109 goto exit_eval_frame;
1110 }
1111 }
1112 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001113
Łukasz Langaa785c872016-09-09 17:37:37 -07001114 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1115 dtrace_function_entry(f);
1116
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 co = f->f_code;
1118 names = co->co_names;
1119 consts = co->co_consts;
1120 fastlocals = f->f_localsplus;
1121 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001122 assert(PyBytes_Check(co->co_code));
1123 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001124 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1125 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1126 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001127 /*
1128 f->f_lasti refers to the index of the last instruction,
1129 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001130
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001131 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001132 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001133
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001134 When the PREDICT() macros are enabled, some opcode pairs follow in
1135 direct succession without updating f->f_lasti. A successful
1136 prediction effectively links the two codes together as if they
1137 were a single new opcode; accordingly,f->f_lasti will point to
1138 the first code in the pair (for instance, GET_ITER followed by
1139 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001140 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001141 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001142 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001143 next_instr = first_instr;
1144 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001145 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1146 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001147 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 stack_pointer = f->f_stacktop;
1149 assert(stack_pointer != NULL);
1150 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001151 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001152
Inada Naoki91234a12019-06-03 21:30:58 +09001153 if (co->co_opcache_flag < OPCACHE_MIN_RUNS) {
1154 co->co_opcache_flag++;
1155 if (co->co_opcache_flag == OPCACHE_MIN_RUNS) {
1156 if (_PyCode_InitOpcache(co) < 0) {
1157 return NULL;
1158 }
1159#if OPCACHE_STATS
1160 opcache_code_objects_extra_mem +=
1161 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1162 sizeof(_PyOpcache) * co->co_opcache_size;
1163 opcache_code_objects++;
1164#endif
1165 }
1166 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001167
Tim Peters5ca576e2001-06-18 22:08:13 +00001168#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001169 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001170#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001171
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001172 if (throwflag) /* support for generator.throw() */
1173 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001174
Victor Stinnerace47d72013-07-18 01:41:08 +02001175#ifdef Py_DEBUG
1176 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001177 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001178 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001179 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001180#endif
1181
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001182main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1185 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001186 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001187
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 /* Do periodic things. Doing this every time through
1189 the loop would add too much overhead, so we do it
1190 only every Nth instruction. We also do it if
1191 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1192 event needs attention (e.g. a signal handler or
1193 async I/O handler); see Py_AddPendingCall() and
1194 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001195
Eric Snow7bda9de2019-03-08 17:25:54 -07001196 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001197 opcode = _Py_OPCODE(*next_instr);
1198 if (opcode == SETUP_FINALLY ||
1199 opcode == SETUP_WITH ||
1200 opcode == BEFORE_ASYNC_WITH ||
1201 opcode == YIELD_FROM) {
1202 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001203 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001204 - If we're about to enter the 'with:'. It will prevent
1205 emitting a resource warning in the common idiom
1206 'with open(path) as file:'.
1207 - If we're about to enter the 'async with:'.
1208 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001209 *very* useful, but might help in some cases and it's
1210 traditional)
1211 - If we're resuming a chain of nested 'yield from' or
1212 'await' calls, then each frame is parked with YIELD_FROM
1213 as its next opcode. If the user hit control-C we want to
1214 wait until we've reached the innermost frame before
1215 running the signal handler and raising KeyboardInterrupt
1216 (see bpo-30039).
1217 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 goto fast_next_opcode;
1219 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001220
Victor Stinnere225beb2019-06-03 18:14:24 +02001221 if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
Victor Stinner09532fe2019-05-10 23:39:09 +02001222 if (handle_signals(runtime) != 0) {
Eric Snowfdf282d2019-01-11 14:26:55 -07001223 goto error;
1224 }
1225 }
Victor Stinnere225beb2019-06-03 18:14:24 +02001226 if (_Py_atomic_load_relaxed(&ceval->pending.calls_to_do)) {
1227 if (make_pending_calls(runtime) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001228 goto error;
Eric Snowfdf282d2019-01-11 14:26:55 -07001229 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001231
Victor Stinnere225beb2019-06-03 18:14:24 +02001232 if (_Py_atomic_load_relaxed(&ceval->gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 /* Give another thread a chance */
Victor Stinner09532fe2019-05-10 23:39:09 +02001234 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 Py_FatalError("ceval: tstate mix-up");
Victor Stinner09532fe2019-05-10 23:39:09 +02001236 }
Victor Stinnere225beb2019-06-03 18:14:24 +02001237 drop_gil(ceval, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001238
1239 /* Other threads may run now */
1240
Victor Stinnere225beb2019-06-03 18:14:24 +02001241 take_gil(ceval, tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001242
1243 /* Check if we should make a quick exit. */
Victor Stinner01b1cc12019-11-20 02:27:56 +01001244 exit_thread_if_finalizing(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001245
Victor Stinner09532fe2019-05-10 23:39:09 +02001246 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 Py_FatalError("ceval: orphan tstate");
Victor Stinner09532fe2019-05-10 23:39:09 +02001248 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 }
1250 /* Check for asynchronous exceptions. */
1251 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001252 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 tstate->async_exc = NULL;
Victor Stinnere225beb2019-06-03 18:14:24 +02001254 UNSIGNAL_ASYNC_EXC(ceval);
Victor Stinner438a12d2019-05-24 17:01:38 +02001255 _PyErr_SetNone(tstate, exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001256 Py_DECREF(exc);
1257 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 }
1259 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 fast_next_opcode:
1262 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001263
Łukasz Langaa785c872016-09-09 17:37:37 -07001264 if (PyDTrace_LINE_ENABLED())
1265 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001268
Victor Stinnere225beb2019-06-03 18:14:24 +02001269 if (_Py_TracingPossible(ceval) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001270 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001271 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001272 /* see maybe_call_line_trace
1273 for expository comments */
1274 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 err = maybe_call_line_trace(tstate->c_tracefunc,
1277 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001278 tstate, f,
1279 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 /* Reload possibly changed frame fields */
1281 JUMPTO(f->f_lasti);
1282 if (f->f_stacktop != NULL) {
1283 stack_pointer = f->f_stacktop;
1284 f->f_stacktop = NULL;
1285 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001286 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001288 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001292
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001293 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001294 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001295#ifdef DYNAMIC_EXECUTION_PROFILE
1296#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 dxpairs[lastopcode][opcode]++;
1298 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001299#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001301#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001302
Guido van Rossum96a42c81992-01-12 02:29:51 +00001303#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 if (lltrace) {
1307 if (HAS_ARG(opcode)) {
1308 printf("%d: %d, %d\n",
1309 f->f_lasti, opcode, oparg);
1310 }
1311 else {
1312 printf("%d: %d\n",
1313 f->f_lasti, opcode);
1314 }
1315 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001316#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001321 It is essential that any operation that fails must goto error
1322 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001323
Benjamin Petersonddd19492018-09-16 22:38:02 -07001324 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001326 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001327
Benjamin Petersonddd19492018-09-16 22:38:02 -07001328 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001329 PyObject *value = GETLOCAL(oparg);
1330 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001331 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001332 UNBOUNDLOCAL_ERROR_MSG,
1333 PyTuple_GetItem(co->co_varnames, oparg));
1334 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001336 Py_INCREF(value);
1337 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001339 }
1340
Benjamin Petersonddd19492018-09-16 22:38:02 -07001341 case TARGET(LOAD_CONST): {
1342 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001343 PyObject *value = GETITEM(consts, oparg);
1344 Py_INCREF(value);
1345 PUSH(value);
1346 FAST_DISPATCH();
1347 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001348
Benjamin Petersonddd19492018-09-16 22:38:02 -07001349 case TARGET(STORE_FAST): {
1350 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001351 PyObject *value = POP();
1352 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001354 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001355
Benjamin Petersonddd19492018-09-16 22:38:02 -07001356 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001357 PyObject *value = POP();
1358 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001359 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001360 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001361
Benjamin Petersonddd19492018-09-16 22:38:02 -07001362 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001363 PyObject *top = TOP();
1364 PyObject *second = SECOND();
1365 SET_TOP(second);
1366 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001368 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001369
Benjamin Petersonddd19492018-09-16 22:38:02 -07001370 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001371 PyObject *top = TOP();
1372 PyObject *second = SECOND();
1373 PyObject *third = THIRD();
1374 SET_TOP(second);
1375 SET_SECOND(third);
1376 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001378 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001379
Benjamin Petersonddd19492018-09-16 22:38:02 -07001380 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001381 PyObject *top = TOP();
1382 PyObject *second = SECOND();
1383 PyObject *third = THIRD();
1384 PyObject *fourth = FOURTH();
1385 SET_TOP(second);
1386 SET_SECOND(third);
1387 SET_THIRD(fourth);
1388 SET_FOURTH(top);
1389 FAST_DISPATCH();
1390 }
1391
Benjamin Petersonddd19492018-09-16 22:38:02 -07001392 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001393 PyObject *top = TOP();
1394 Py_INCREF(top);
1395 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001397 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001398
Benjamin Petersonddd19492018-09-16 22:38:02 -07001399 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001400 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001401 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001402 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001403 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001404 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001405 SET_TOP(top);
1406 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001407 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001408 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001409
Benjamin Petersonddd19492018-09-16 22:38:02 -07001410 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001411 PyObject *value = TOP();
1412 PyObject *res = PyNumber_Positive(value);
1413 Py_DECREF(value);
1414 SET_TOP(res);
1415 if (res == NULL)
1416 goto error;
1417 DISPATCH();
1418 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001419
Benjamin Petersonddd19492018-09-16 22:38:02 -07001420 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001421 PyObject *value = TOP();
1422 PyObject *res = PyNumber_Negative(value);
1423 Py_DECREF(value);
1424 SET_TOP(res);
1425 if (res == NULL)
1426 goto error;
1427 DISPATCH();
1428 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001429
Benjamin Petersonddd19492018-09-16 22:38:02 -07001430 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001431 PyObject *value = TOP();
1432 int err = PyObject_IsTrue(value);
1433 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434 if (err == 0) {
1435 Py_INCREF(Py_True);
1436 SET_TOP(Py_True);
1437 DISPATCH();
1438 }
1439 else if (err > 0) {
1440 Py_INCREF(Py_False);
1441 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 DISPATCH();
1443 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001444 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001445 goto error;
1446 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001447
Benjamin Petersonddd19492018-09-16 22:38:02 -07001448 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001449 PyObject *value = TOP();
1450 PyObject *res = PyNumber_Invert(value);
1451 Py_DECREF(value);
1452 SET_TOP(res);
1453 if (res == NULL)
1454 goto error;
1455 DISPATCH();
1456 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001457
Benjamin Petersonddd19492018-09-16 22:38:02 -07001458 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001459 PyObject *exp = POP();
1460 PyObject *base = TOP();
1461 PyObject *res = PyNumber_Power(base, exp, Py_None);
1462 Py_DECREF(base);
1463 Py_DECREF(exp);
1464 SET_TOP(res);
1465 if (res == NULL)
1466 goto error;
1467 DISPATCH();
1468 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001469
Benjamin Petersonddd19492018-09-16 22:38:02 -07001470 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001471 PyObject *right = POP();
1472 PyObject *left = TOP();
1473 PyObject *res = PyNumber_Multiply(left, right);
1474 Py_DECREF(left);
1475 Py_DECREF(right);
1476 SET_TOP(res);
1477 if (res == NULL)
1478 goto error;
1479 DISPATCH();
1480 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001481
Benjamin Petersonddd19492018-09-16 22:38:02 -07001482 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001483 PyObject *right = POP();
1484 PyObject *left = TOP();
1485 PyObject *res = PyNumber_MatrixMultiply(left, right);
1486 Py_DECREF(left);
1487 Py_DECREF(right);
1488 SET_TOP(res);
1489 if (res == NULL)
1490 goto error;
1491 DISPATCH();
1492 }
1493
Benjamin Petersonddd19492018-09-16 22:38:02 -07001494 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001495 PyObject *divisor = POP();
1496 PyObject *dividend = TOP();
1497 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1498 Py_DECREF(dividend);
1499 Py_DECREF(divisor);
1500 SET_TOP(quotient);
1501 if (quotient == NULL)
1502 goto error;
1503 DISPATCH();
1504 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001505
Benjamin Petersonddd19492018-09-16 22:38:02 -07001506 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001507 PyObject *divisor = POP();
1508 PyObject *dividend = TOP();
1509 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1510 Py_DECREF(dividend);
1511 Py_DECREF(divisor);
1512 SET_TOP(quotient);
1513 if (quotient == NULL)
1514 goto error;
1515 DISPATCH();
1516 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001517
Benjamin Petersonddd19492018-09-16 22:38:02 -07001518 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001519 PyObject *divisor = POP();
1520 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001521 PyObject *res;
1522 if (PyUnicode_CheckExact(dividend) && (
1523 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1524 // fast path; string formatting, but not if the RHS is a str subclass
1525 // (see issue28598)
1526 res = PyUnicode_Format(dividend, divisor);
1527 } else {
1528 res = PyNumber_Remainder(dividend, divisor);
1529 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001530 Py_DECREF(divisor);
1531 Py_DECREF(dividend);
1532 SET_TOP(res);
1533 if (res == NULL)
1534 goto error;
1535 DISPATCH();
1536 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001537
Benjamin Petersonddd19492018-09-16 22:38:02 -07001538 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001539 PyObject *right = POP();
1540 PyObject *left = TOP();
1541 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001542 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1543 CPython using bytecode, it is simply worthless.
1544 See http://bugs.python.org/issue21955 and
1545 http://bugs.python.org/issue10044 for the discussion. In short,
1546 no patch shown any impact on a realistic benchmark, only a minor
1547 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001548 if (PyUnicode_CheckExact(left) &&
1549 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001550 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001551 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001552 }
1553 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001554 sum = PyNumber_Add(left, right);
1555 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001556 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001557 Py_DECREF(right);
1558 SET_TOP(sum);
1559 if (sum == NULL)
1560 goto error;
1561 DISPATCH();
1562 }
1563
Benjamin Petersonddd19492018-09-16 22:38:02 -07001564 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001565 PyObject *right = POP();
1566 PyObject *left = TOP();
1567 PyObject *diff = PyNumber_Subtract(left, right);
1568 Py_DECREF(right);
1569 Py_DECREF(left);
1570 SET_TOP(diff);
1571 if (diff == NULL)
1572 goto error;
1573 DISPATCH();
1574 }
1575
Benjamin Petersonddd19492018-09-16 22:38:02 -07001576 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001577 PyObject *sub = POP();
1578 PyObject *container = TOP();
1579 PyObject *res = PyObject_GetItem(container, sub);
1580 Py_DECREF(container);
1581 Py_DECREF(sub);
1582 SET_TOP(res);
1583 if (res == NULL)
1584 goto error;
1585 DISPATCH();
1586 }
1587
Benjamin Petersonddd19492018-09-16 22:38:02 -07001588 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001589 PyObject *right = POP();
1590 PyObject *left = TOP();
1591 PyObject *res = PyNumber_Lshift(left, right);
1592 Py_DECREF(left);
1593 Py_DECREF(right);
1594 SET_TOP(res);
1595 if (res == NULL)
1596 goto error;
1597 DISPATCH();
1598 }
1599
Benjamin Petersonddd19492018-09-16 22:38:02 -07001600 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001601 PyObject *right = POP();
1602 PyObject *left = TOP();
1603 PyObject *res = PyNumber_Rshift(left, right);
1604 Py_DECREF(left);
1605 Py_DECREF(right);
1606 SET_TOP(res);
1607 if (res == NULL)
1608 goto error;
1609 DISPATCH();
1610 }
1611
Benjamin Petersonddd19492018-09-16 22:38:02 -07001612 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001613 PyObject *right = POP();
1614 PyObject *left = TOP();
1615 PyObject *res = PyNumber_And(left, right);
1616 Py_DECREF(left);
1617 Py_DECREF(right);
1618 SET_TOP(res);
1619 if (res == NULL)
1620 goto error;
1621 DISPATCH();
1622 }
1623
Benjamin Petersonddd19492018-09-16 22:38:02 -07001624 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001625 PyObject *right = POP();
1626 PyObject *left = TOP();
1627 PyObject *res = PyNumber_Xor(left, right);
1628 Py_DECREF(left);
1629 Py_DECREF(right);
1630 SET_TOP(res);
1631 if (res == NULL)
1632 goto error;
1633 DISPATCH();
1634 }
1635
Benjamin Petersonddd19492018-09-16 22:38:02 -07001636 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001637 PyObject *right = POP();
1638 PyObject *left = TOP();
1639 PyObject *res = PyNumber_Or(left, right);
1640 Py_DECREF(left);
1641 Py_DECREF(right);
1642 SET_TOP(res);
1643 if (res == NULL)
1644 goto error;
1645 DISPATCH();
1646 }
1647
Benjamin Petersonddd19492018-09-16 22:38:02 -07001648 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001649 PyObject *v = POP();
1650 PyObject *list = PEEK(oparg);
1651 int err;
1652 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001653 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001654 if (err != 0)
1655 goto error;
1656 PREDICT(JUMP_ABSOLUTE);
1657 DISPATCH();
1658 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001659
Benjamin Petersonddd19492018-09-16 22:38:02 -07001660 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001661 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001662 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001663 int err;
1664 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001665 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001666 if (err != 0)
1667 goto error;
1668 PREDICT(JUMP_ABSOLUTE);
1669 DISPATCH();
1670 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001671
Benjamin Petersonddd19492018-09-16 22:38:02 -07001672 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001673 PyObject *exp = POP();
1674 PyObject *base = TOP();
1675 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1676 Py_DECREF(base);
1677 Py_DECREF(exp);
1678 SET_TOP(res);
1679 if (res == NULL)
1680 goto error;
1681 DISPATCH();
1682 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001683
Benjamin Petersonddd19492018-09-16 22:38:02 -07001684 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001685 PyObject *right = POP();
1686 PyObject *left = TOP();
1687 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1688 Py_DECREF(left);
1689 Py_DECREF(right);
1690 SET_TOP(res);
1691 if (res == NULL)
1692 goto error;
1693 DISPATCH();
1694 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001695
Benjamin Petersonddd19492018-09-16 22:38:02 -07001696 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001697 PyObject *right = POP();
1698 PyObject *left = TOP();
1699 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1700 Py_DECREF(left);
1701 Py_DECREF(right);
1702 SET_TOP(res);
1703 if (res == NULL)
1704 goto error;
1705 DISPATCH();
1706 }
1707
Benjamin Petersonddd19492018-09-16 22:38:02 -07001708 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001709 PyObject *divisor = POP();
1710 PyObject *dividend = TOP();
1711 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1712 Py_DECREF(dividend);
1713 Py_DECREF(divisor);
1714 SET_TOP(quotient);
1715 if (quotient == NULL)
1716 goto error;
1717 DISPATCH();
1718 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001719
Benjamin Petersonddd19492018-09-16 22:38:02 -07001720 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001721 PyObject *divisor = POP();
1722 PyObject *dividend = TOP();
1723 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1724 Py_DECREF(dividend);
1725 Py_DECREF(divisor);
1726 SET_TOP(quotient);
1727 if (quotient == NULL)
1728 goto error;
1729 DISPATCH();
1730 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001731
Benjamin Petersonddd19492018-09-16 22:38:02 -07001732 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001733 PyObject *right = POP();
1734 PyObject *left = TOP();
1735 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1736 Py_DECREF(left);
1737 Py_DECREF(right);
1738 SET_TOP(mod);
1739 if (mod == NULL)
1740 goto error;
1741 DISPATCH();
1742 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001743
Benjamin Petersonddd19492018-09-16 22:38:02 -07001744 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001745 PyObject *right = POP();
1746 PyObject *left = TOP();
1747 PyObject *sum;
1748 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001749 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001750 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001751 }
1752 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001753 sum = PyNumber_InPlaceAdd(left, right);
1754 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001755 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001756 Py_DECREF(right);
1757 SET_TOP(sum);
1758 if (sum == NULL)
1759 goto error;
1760 DISPATCH();
1761 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001762
Benjamin Petersonddd19492018-09-16 22:38:02 -07001763 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001764 PyObject *right = POP();
1765 PyObject *left = TOP();
1766 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1767 Py_DECREF(left);
1768 Py_DECREF(right);
1769 SET_TOP(diff);
1770 if (diff == NULL)
1771 goto error;
1772 DISPATCH();
1773 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001774
Benjamin Petersonddd19492018-09-16 22:38:02 -07001775 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001776 PyObject *right = POP();
1777 PyObject *left = TOP();
1778 PyObject *res = PyNumber_InPlaceLshift(left, right);
1779 Py_DECREF(left);
1780 Py_DECREF(right);
1781 SET_TOP(res);
1782 if (res == NULL)
1783 goto error;
1784 DISPATCH();
1785 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001786
Benjamin Petersonddd19492018-09-16 22:38:02 -07001787 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001788 PyObject *right = POP();
1789 PyObject *left = TOP();
1790 PyObject *res = PyNumber_InPlaceRshift(left, right);
1791 Py_DECREF(left);
1792 Py_DECREF(right);
1793 SET_TOP(res);
1794 if (res == NULL)
1795 goto error;
1796 DISPATCH();
1797 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001798
Benjamin Petersonddd19492018-09-16 22:38:02 -07001799 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001800 PyObject *right = POP();
1801 PyObject *left = TOP();
1802 PyObject *res = PyNumber_InPlaceAnd(left, right);
1803 Py_DECREF(left);
1804 Py_DECREF(right);
1805 SET_TOP(res);
1806 if (res == NULL)
1807 goto error;
1808 DISPATCH();
1809 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001810
Benjamin Petersonddd19492018-09-16 22:38:02 -07001811 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001812 PyObject *right = POP();
1813 PyObject *left = TOP();
1814 PyObject *res = PyNumber_InPlaceXor(left, right);
1815 Py_DECREF(left);
1816 Py_DECREF(right);
1817 SET_TOP(res);
1818 if (res == NULL)
1819 goto error;
1820 DISPATCH();
1821 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001822
Benjamin Petersonddd19492018-09-16 22:38:02 -07001823 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001824 PyObject *right = POP();
1825 PyObject *left = TOP();
1826 PyObject *res = PyNumber_InPlaceOr(left, right);
1827 Py_DECREF(left);
1828 Py_DECREF(right);
1829 SET_TOP(res);
1830 if (res == NULL)
1831 goto error;
1832 DISPATCH();
1833 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001834
Benjamin Petersonddd19492018-09-16 22:38:02 -07001835 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001836 PyObject *sub = TOP();
1837 PyObject *container = SECOND();
1838 PyObject *v = THIRD();
1839 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001840 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001841 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001842 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001843 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001844 Py_DECREF(container);
1845 Py_DECREF(sub);
1846 if (err != 0)
1847 goto error;
1848 DISPATCH();
1849 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001850
Benjamin Petersonddd19492018-09-16 22:38:02 -07001851 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001852 PyObject *sub = TOP();
1853 PyObject *container = SECOND();
1854 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001855 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00001856 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001857 err = PyObject_DelItem(container, sub);
1858 Py_DECREF(container);
1859 Py_DECREF(sub);
1860 if (err != 0)
1861 goto error;
1862 DISPATCH();
1863 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001864
Benjamin Petersonddd19492018-09-16 22:38:02 -07001865 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01001866 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001867 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001868 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001869 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001870 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001871 _PyErr_SetString(tstate, PyExc_RuntimeError,
1872 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001873 Py_DECREF(value);
1874 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001875 }
Jeroen Demeyer196a5302019-07-04 12:31:34 +02001876 res = _PyObject_CallOneArg(hook, value);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001877 Py_DECREF(value);
1878 if (res == NULL)
1879 goto error;
1880 Py_DECREF(res);
1881 DISPATCH();
1882 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001883
Benjamin Petersonddd19492018-09-16 22:38:02 -07001884 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001885 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001886 switch (oparg) {
1887 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001888 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001889 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001890 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001891 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001892 /* fall through */
1893 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02001894 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001895 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001896 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001897 break;
1898 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02001899 _PyErr_SetString(tstate, PyExc_SystemError,
1900 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001901 break;
1902 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001903 goto error;
1904 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001905
Benjamin Petersonddd19492018-09-16 22:38:02 -07001906 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001907 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001908 assert(f->f_iblock == 0);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00001909 assert(EMPTY());
1910 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001911 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001912
Benjamin Petersonddd19492018-09-16 22:38:02 -07001913 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001914 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001915 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001916 PyObject *obj = TOP();
1917 PyTypeObject *type = Py_TYPE(obj);
1918
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001919 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001920 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001921 }
Yury Selivanov75445082015-05-11 22:57:16 -04001922
1923 if (getter != NULL) {
1924 iter = (*getter)(obj);
1925 Py_DECREF(obj);
1926 if (iter == NULL) {
1927 SET_TOP(NULL);
1928 goto error;
1929 }
1930 }
1931 else {
1932 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02001933 _PyErr_Format(tstate, PyExc_TypeError,
1934 "'async for' requires an object with "
1935 "__aiter__ method, got %.100s",
1936 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001937 Py_DECREF(obj);
1938 goto error;
1939 }
1940
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001941 if (Py_TYPE(iter)->tp_as_async == NULL ||
1942 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001943
Yury Selivanov398ff912017-03-02 22:20:00 -05001944 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02001945 _PyErr_Format(tstate, PyExc_TypeError,
1946 "'async for' received an object from __aiter__ "
1947 "that does not implement __anext__: %.100s",
1948 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001949 Py_DECREF(iter);
1950 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001951 }
1952
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001953 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001954 DISPATCH();
1955 }
1956
Benjamin Petersonddd19492018-09-16 22:38:02 -07001957 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001958 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001959 PyObject *next_iter = NULL;
1960 PyObject *awaitable = NULL;
1961 PyObject *aiter = TOP();
1962 PyTypeObject *type = Py_TYPE(aiter);
1963
Yury Selivanoveb636452016-09-08 22:01:51 -07001964 if (PyAsyncGen_CheckExact(aiter)) {
1965 awaitable = type->tp_as_async->am_anext(aiter);
1966 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001967 goto error;
1968 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001969 } else {
1970 if (type->tp_as_async != NULL){
1971 getter = type->tp_as_async->am_anext;
1972 }
Yury Selivanov75445082015-05-11 22:57:16 -04001973
Yury Selivanoveb636452016-09-08 22:01:51 -07001974 if (getter != NULL) {
1975 next_iter = (*getter)(aiter);
1976 if (next_iter == NULL) {
1977 goto error;
1978 }
1979 }
1980 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02001981 _PyErr_Format(tstate, PyExc_TypeError,
1982 "'async for' requires an iterator with "
1983 "__anext__ method, got %.100s",
1984 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07001985 goto error;
1986 }
Yury Selivanov75445082015-05-11 22:57:16 -04001987
Yury Selivanoveb636452016-09-08 22:01:51 -07001988 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1989 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001990 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001991 PyExc_TypeError,
1992 "'async for' received an invalid object "
1993 "from __anext__: %.100s",
1994 Py_TYPE(next_iter)->tp_name);
1995
1996 Py_DECREF(next_iter);
1997 goto error;
1998 } else {
1999 Py_DECREF(next_iter);
2000 }
2001 }
Yury Selivanov75445082015-05-11 22:57:16 -04002002
2003 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002004 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002005 DISPATCH();
2006 }
2007
Benjamin Petersonddd19492018-09-16 22:38:02 -07002008 case TARGET(GET_AWAITABLE): {
2009 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002010 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002011 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002012
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002013 if (iter == NULL) {
Mark Shannonfee55262019-11-21 09:11:43 +00002014 int opcode_at_minus_3 = 0;
2015 if ((next_instr - first_instr) > 2) {
2016 opcode_at_minus_3 = _Py_OPCODE(next_instr[-3]);
2017 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002018 format_awaitable_error(tstate, Py_TYPE(iterable),
Mark Shannonfee55262019-11-21 09:11:43 +00002019 opcode_at_minus_3,
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002020 _Py_OPCODE(next_instr[-2]));
2021 }
2022
Yury Selivanov75445082015-05-11 22:57:16 -04002023 Py_DECREF(iterable);
2024
Yury Selivanovc724bae2016-03-02 11:30:46 -05002025 if (iter != NULL && PyCoro_CheckExact(iter)) {
2026 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2027 if (yf != NULL) {
2028 /* `iter` is a coroutine object that is being
2029 awaited, `yf` is a pointer to the current awaitable
2030 being awaited on. */
2031 Py_DECREF(yf);
2032 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002033 _PyErr_SetString(tstate, PyExc_RuntimeError,
2034 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002035 /* The code below jumps to `error` if `iter` is NULL. */
2036 }
2037 }
2038
Yury Selivanov75445082015-05-11 22:57:16 -04002039 SET_TOP(iter); /* Even if it's NULL */
2040
2041 if (iter == NULL) {
2042 goto error;
2043 }
2044
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002045 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002046 DISPATCH();
2047 }
2048
Benjamin Petersonddd19492018-09-16 22:38:02 -07002049 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002050 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002051 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002052 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002053 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
2054 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002055 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002056 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002057 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002058 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002059 else
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02002060 retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002061 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002062 Py_DECREF(v);
2063 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002064 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002065 if (tstate->c_tracefunc != NULL
Victor Stinner438a12d2019-05-24 17:01:38 +02002066 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002067 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002068 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002069 if (err < 0)
2070 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002071 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002072 SET_TOP(val);
2073 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002074 }
Martin Panter95f53c12016-07-18 08:23:26 +00002075 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002076 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002077 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002078 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002079 f->f_lasti -= sizeof(_Py_CODEUNIT);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002080 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002081 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002082
Benjamin Petersonddd19492018-09-16 22:38:02 -07002083 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002084 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002085
2086 if (co->co_flags & CO_ASYNC_GENERATOR) {
2087 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2088 Py_DECREF(retval);
2089 if (w == NULL) {
2090 retval = NULL;
2091 goto error;
2092 }
2093 retval = w;
2094 }
2095
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002096 f->f_stacktop = stack_pointer;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002097 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002098 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002099
Benjamin Petersonddd19492018-09-16 22:38:02 -07002100 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002101 PyObject *type, *value, *traceback;
2102 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002103 PyTryBlock *b = PyFrame_BlockPop(f);
2104 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002105 _PyErr_SetString(tstate, PyExc_SystemError,
2106 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002107 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002108 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002109 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2110 STACK_LEVEL() <= (b)->b_level + 4);
2111 exc_info = tstate->exc_info;
2112 type = exc_info->exc_type;
2113 value = exc_info->exc_value;
2114 traceback = exc_info->exc_traceback;
2115 exc_info->exc_type = POP();
2116 exc_info->exc_value = POP();
2117 exc_info->exc_traceback = POP();
2118 Py_XDECREF(type);
2119 Py_XDECREF(value);
2120 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002121 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002122 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002123
Benjamin Petersonddd19492018-09-16 22:38:02 -07002124 case TARGET(POP_BLOCK): {
2125 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002126 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002127 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002128 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002129
Mark Shannonfee55262019-11-21 09:11:43 +00002130 case TARGET(RERAISE): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002131 PyObject *exc = POP();
Mark Shannonfee55262019-11-21 09:11:43 +00002132 PyObject *val = POP();
2133 PyObject *tb = POP();
2134 assert(PyExceptionClass_Check(exc));
Victor Stinner61f4db82020-01-28 03:37:45 +01002135 _PyErr_Restore(tstate, exc, val, tb);
Mark Shannonfee55262019-11-21 09:11:43 +00002136 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002137 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002138
Benjamin Petersonddd19492018-09-16 22:38:02 -07002139 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002140 PyObject *exc = POP();
2141 assert(PyExceptionClass_Check(exc));
2142 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2143 PyTryBlock *b = PyFrame_BlockPop(f);
2144 assert(b->b_type == EXCEPT_HANDLER);
2145 Py_DECREF(exc);
2146 UNWIND_EXCEPT_HANDLER(b);
2147 Py_DECREF(POP());
2148 JUMPBY(oparg);
2149 FAST_DISPATCH();
2150 }
2151 else {
2152 PyObject *val = POP();
2153 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002154 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002155 goto exception_unwind;
2156 }
2157 }
2158
Zackery Spytzce6a0702019-08-25 03:44:09 -06002159 case TARGET(LOAD_ASSERTION_ERROR): {
2160 PyObject *value = PyExc_AssertionError;
2161 Py_INCREF(value);
2162 PUSH(value);
2163 FAST_DISPATCH();
2164 }
2165
Benjamin Petersonddd19492018-09-16 22:38:02 -07002166 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002167 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002168
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002169 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002170 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002171 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002172 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002173 if (!_PyErr_Occurred(tstate)) {
2174 _PyErr_SetString(tstate, PyExc_NameError,
2175 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002176 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002177 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002178 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002179 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002180 }
2181 else {
2182 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2183 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002184 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002185 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2186 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002187 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2188 _PyErr_SetString(tstate, PyExc_NameError,
2189 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002190 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002191 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002192 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002193 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002194 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002195 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002196
Benjamin Petersonddd19492018-09-16 22:38:02 -07002197 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002198 PyObject *name = GETITEM(names, oparg);
2199 PyObject *v = POP();
2200 PyObject *ns = f->f_locals;
2201 int err;
2202 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002203 _PyErr_Format(tstate, PyExc_SystemError,
2204 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002205 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002206 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002207 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002208 if (PyDict_CheckExact(ns))
2209 err = PyDict_SetItem(ns, name, v);
2210 else
2211 err = PyObject_SetItem(ns, name, v);
2212 Py_DECREF(v);
2213 if (err != 0)
2214 goto error;
2215 DISPATCH();
2216 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002217
Benjamin Petersonddd19492018-09-16 22:38:02 -07002218 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002219 PyObject *name = GETITEM(names, oparg);
2220 PyObject *ns = f->f_locals;
2221 int err;
2222 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002223 _PyErr_Format(tstate, PyExc_SystemError,
2224 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002225 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002226 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002227 err = PyObject_DelItem(ns, name);
2228 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002229 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002230 NAME_ERROR_MSG,
2231 name);
2232 goto error;
2233 }
2234 DISPATCH();
2235 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002236
Benjamin Petersonddd19492018-09-16 22:38:02 -07002237 case TARGET(UNPACK_SEQUENCE): {
2238 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002239 PyObject *seq = POP(), *item, **items;
2240 if (PyTuple_CheckExact(seq) &&
2241 PyTuple_GET_SIZE(seq) == oparg) {
2242 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002243 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002244 item = items[oparg];
2245 Py_INCREF(item);
2246 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002247 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002248 } else if (PyList_CheckExact(seq) &&
2249 PyList_GET_SIZE(seq) == oparg) {
2250 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002251 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002252 item = items[oparg];
2253 Py_INCREF(item);
2254 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002255 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002256 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002257 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002258 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002259 } else {
2260 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002261 Py_DECREF(seq);
2262 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002263 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002264 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002265 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002266 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002267
Benjamin Petersonddd19492018-09-16 22:38:02 -07002268 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002269 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2270 PyObject *seq = POP();
2271
Victor Stinner438a12d2019-05-24 17:01:38 +02002272 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002273 stack_pointer + totalargs)) {
2274 stack_pointer += totalargs;
2275 } else {
2276 Py_DECREF(seq);
2277 goto error;
2278 }
2279 Py_DECREF(seq);
2280 DISPATCH();
2281 }
2282
Benjamin Petersonddd19492018-09-16 22:38:02 -07002283 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002284 PyObject *name = GETITEM(names, oparg);
2285 PyObject *owner = TOP();
2286 PyObject *v = SECOND();
2287 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002288 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002289 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002290 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002291 Py_DECREF(owner);
2292 if (err != 0)
2293 goto error;
2294 DISPATCH();
2295 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002296
Benjamin Petersonddd19492018-09-16 22:38:02 -07002297 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002298 PyObject *name = GETITEM(names, oparg);
2299 PyObject *owner = POP();
2300 int err;
2301 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2302 Py_DECREF(owner);
2303 if (err != 0)
2304 goto error;
2305 DISPATCH();
2306 }
2307
Benjamin Petersonddd19492018-09-16 22:38:02 -07002308 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002309 PyObject *name = GETITEM(names, oparg);
2310 PyObject *v = POP();
2311 int err;
2312 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002313 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002314 if (err != 0)
2315 goto error;
2316 DISPATCH();
2317 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002318
Benjamin Petersonddd19492018-09-16 22:38:02 -07002319 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002320 PyObject *name = GETITEM(names, oparg);
2321 int err;
2322 err = PyDict_DelItem(f->f_globals, name);
2323 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002324 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2325 format_exc_check_arg(tstate, PyExc_NameError,
2326 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002327 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002328 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002329 }
2330 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002331 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002332
Benjamin Petersonddd19492018-09-16 22:38:02 -07002333 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002334 PyObject *name = GETITEM(names, oparg);
2335 PyObject *locals = f->f_locals;
2336 PyObject *v;
2337 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002338 _PyErr_Format(tstate, PyExc_SystemError,
2339 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002340 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002341 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002342 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002343 v = PyDict_GetItemWithError(locals, name);
2344 if (v != NULL) {
2345 Py_INCREF(v);
2346 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002347 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002348 goto error;
2349 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002350 }
2351 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002352 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002353 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002354 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002355 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002356 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002357 }
2358 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002359 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002360 v = PyDict_GetItemWithError(f->f_globals, name);
2361 if (v != NULL) {
2362 Py_INCREF(v);
2363 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002364 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002365 goto error;
2366 }
2367 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002368 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002369 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002370 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002371 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002372 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002373 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002374 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002375 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002376 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002377 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002378 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002379 }
2380 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002381 v = PyObject_GetItem(f->f_builtins, name);
2382 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002383 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002384 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002385 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002386 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002387 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002388 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002389 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002390 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002391 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002392 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002393 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002394 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002395 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002396
Benjamin Petersonddd19492018-09-16 22:38:02 -07002397 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002398 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002399 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002400 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002401 && PyDict_CheckExact(f->f_builtins))
2402 {
Inada Naoki91234a12019-06-03 21:30:58 +09002403 OPCACHE_CHECK();
2404 if (co_opcache != NULL && co_opcache->optimized > 0) {
2405 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2406
2407 if (lg->globals_ver ==
2408 ((PyDictObject *)f->f_globals)->ma_version_tag
2409 && lg->builtins_ver ==
2410 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2411 {
2412 PyObject *ptr = lg->ptr;
2413 OPCACHE_STAT_GLOBAL_HIT();
2414 assert(ptr != NULL);
2415 Py_INCREF(ptr);
2416 PUSH(ptr);
2417 DISPATCH();
2418 }
2419 }
2420
2421 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002422 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002423 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002424 name);
2425 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002426 if (!_PyErr_OCCURRED()) {
2427 /* _PyDict_LoadGlobal() returns NULL without raising
2428 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002429 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002430 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002431 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002432 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002433 }
Inada Naoki91234a12019-06-03 21:30:58 +09002434
2435 if (co_opcache != NULL) {
2436 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2437
2438 if (co_opcache->optimized == 0) {
2439 /* Wasn't optimized before. */
2440 OPCACHE_STAT_GLOBAL_OPT();
2441 } else {
2442 OPCACHE_STAT_GLOBAL_MISS();
2443 }
2444
2445 co_opcache->optimized = 1;
2446 lg->globals_ver =
2447 ((PyDictObject *)f->f_globals)->ma_version_tag;
2448 lg->builtins_ver =
2449 ((PyDictObject *)f->f_builtins)->ma_version_tag;
2450 lg->ptr = v; /* borrowed */
2451 }
2452
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002453 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002454 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002455 else {
2456 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002457
2458 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09002459 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002460 v = PyObject_GetItem(f->f_globals, name);
2461 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002462 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002463 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002464 }
2465 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002466
Victor Stinnerb4efc962015-11-20 09:24:02 +01002467 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002468 v = PyObject_GetItem(f->f_builtins, name);
2469 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002470 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002471 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002472 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002473 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002474 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002475 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002476 }
2477 }
2478 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002479 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002480 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002481 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002482
Benjamin Petersonddd19492018-09-16 22:38:02 -07002483 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002484 PyObject *v = GETLOCAL(oparg);
2485 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002486 SETLOCAL(oparg, NULL);
2487 DISPATCH();
2488 }
2489 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002490 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002491 UNBOUNDLOCAL_ERROR_MSG,
2492 PyTuple_GetItem(co->co_varnames, oparg)
2493 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002494 goto error;
2495 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002496
Benjamin Petersonddd19492018-09-16 22:38:02 -07002497 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002498 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002499 PyObject *oldobj = PyCell_GET(cell);
2500 if (oldobj != NULL) {
2501 PyCell_SET(cell, NULL);
2502 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002503 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002504 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002505 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002506 goto error;
2507 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002508
Benjamin Petersonddd19492018-09-16 22:38:02 -07002509 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002510 PyObject *cell = freevars[oparg];
2511 Py_INCREF(cell);
2512 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002513 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002514 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002515
Benjamin Petersonddd19492018-09-16 22:38:02 -07002516 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002517 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002518 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002519 assert(locals);
2520 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2521 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2522 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2523 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2524 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002525 value = PyDict_GetItemWithError(locals, name);
2526 if (value != NULL) {
2527 Py_INCREF(value);
2528 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002529 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002530 goto error;
2531 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002532 }
2533 else {
2534 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002535 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002536 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002537 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002538 }
2539 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002540 }
2541 }
2542 if (!value) {
2543 PyObject *cell = freevars[oparg];
2544 value = PyCell_GET(cell);
2545 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002546 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002547 goto error;
2548 }
2549 Py_INCREF(value);
2550 }
2551 PUSH(value);
2552 DISPATCH();
2553 }
2554
Benjamin Petersonddd19492018-09-16 22:38:02 -07002555 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002556 PyObject *cell = freevars[oparg];
2557 PyObject *value = PyCell_GET(cell);
2558 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002559 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002560 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002561 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002562 Py_INCREF(value);
2563 PUSH(value);
2564 DISPATCH();
2565 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002566
Benjamin Petersonddd19492018-09-16 22:38:02 -07002567 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002568 PyObject *v = POP();
2569 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002570 PyObject *oldobj = PyCell_GET(cell);
2571 PyCell_SET(cell, v);
2572 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002573 DISPATCH();
2574 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002575
Benjamin Petersonddd19492018-09-16 22:38:02 -07002576 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002577 PyObject *str;
2578 PyObject *empty = PyUnicode_New(0, 0);
2579 if (empty == NULL) {
2580 goto error;
2581 }
2582 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2583 Py_DECREF(empty);
2584 if (str == NULL)
2585 goto error;
2586 while (--oparg >= 0) {
2587 PyObject *item = POP();
2588 Py_DECREF(item);
2589 }
2590 PUSH(str);
2591 DISPATCH();
2592 }
2593
Benjamin Petersonddd19492018-09-16 22:38:02 -07002594 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002595 PyObject *tup = PyTuple_New(oparg);
2596 if (tup == NULL)
2597 goto error;
2598 while (--oparg >= 0) {
2599 PyObject *item = POP();
2600 PyTuple_SET_ITEM(tup, oparg, item);
2601 }
2602 PUSH(tup);
2603 DISPATCH();
2604 }
2605
Benjamin Petersonddd19492018-09-16 22:38:02 -07002606 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002607 PyObject *list = PyList_New(oparg);
2608 if (list == NULL)
2609 goto error;
2610 while (--oparg >= 0) {
2611 PyObject *item = POP();
2612 PyList_SET_ITEM(list, oparg, item);
2613 }
2614 PUSH(list);
2615 DISPATCH();
2616 }
2617
Mark Shannon13bc1392020-01-23 09:25:17 +00002618 case TARGET(LIST_TO_TUPLE): {
2619 PyObject *list = POP();
2620 PyObject *tuple = PyList_AsTuple(list);
2621 Py_DECREF(list);
2622 if (tuple == NULL) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002623 goto error;
Mark Shannon13bc1392020-01-23 09:25:17 +00002624 }
2625 PUSH(tuple);
2626 DISPATCH();
2627 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002628
Mark Shannon13bc1392020-01-23 09:25:17 +00002629 case TARGET(LIST_EXTEND): {
2630 PyObject *iterable = POP();
2631 PyObject *list = PEEK(oparg);
2632 PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
2633 if (none_val == NULL) {
2634 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
2635 (iterable->ob_type->tp_iter == NULL && !PySequence_Check(iterable)))
2636 {
Victor Stinner61f4db82020-01-28 03:37:45 +01002637 _PyErr_Clear(tstate);
Mark Shannon13bc1392020-01-23 09:25:17 +00002638 _PyErr_Format(tstate, PyExc_TypeError,
2639 "Value after * must be an iterable, not %.200s",
2640 Py_TYPE(iterable)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002641 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002642 Py_DECREF(iterable);
2643 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002644 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002645 Py_DECREF(none_val);
2646 Py_DECREF(iterable);
2647 DISPATCH();
2648 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002649
Mark Shannon13bc1392020-01-23 09:25:17 +00002650 case TARGET(SET_UPDATE): {
2651 PyObject *iterable = POP();
2652 PyObject *set = PEEK(oparg);
2653 int err = _PySet_Update(set, iterable);
2654 Py_DECREF(iterable);
2655 if (err < 0) {
2656 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002657 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002658 DISPATCH();
2659 }
2660
Benjamin Petersonddd19492018-09-16 22:38:02 -07002661 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002662 PyObject *set = PySet_New(NULL);
2663 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002664 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002665 if (set == NULL)
2666 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002667 for (i = oparg; i > 0; i--) {
2668 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002669 if (err == 0)
2670 err = PySet_Add(set, item);
2671 Py_DECREF(item);
2672 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002673 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002674 if (err != 0) {
2675 Py_DECREF(set);
2676 goto error;
2677 }
2678 PUSH(set);
2679 DISPATCH();
2680 }
2681
Benjamin Petersonddd19492018-09-16 22:38:02 -07002682 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002683 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002684 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2685 if (map == NULL)
2686 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002687 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002688 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002689 PyObject *key = PEEK(2*i);
2690 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002691 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002692 if (err != 0) {
2693 Py_DECREF(map);
2694 goto error;
2695 }
2696 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002697
2698 while (oparg--) {
2699 Py_DECREF(POP());
2700 Py_DECREF(POP());
2701 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002702 PUSH(map);
2703 DISPATCH();
2704 }
2705
Benjamin Petersonddd19492018-09-16 22:38:02 -07002706 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002707 _Py_IDENTIFIER(__annotations__);
2708 int err;
2709 PyObject *ann_dict;
2710 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002711 _PyErr_Format(tstate, PyExc_SystemError,
2712 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002713 goto error;
2714 }
2715 /* check if __annotations__ in locals()... */
2716 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002717 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002718 &PyId___annotations__);
2719 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002720 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002721 goto error;
2722 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002723 /* ...if not, create a new one */
2724 ann_dict = PyDict_New();
2725 if (ann_dict == NULL) {
2726 goto error;
2727 }
2728 err = _PyDict_SetItemId(f->f_locals,
2729 &PyId___annotations__, ann_dict);
2730 Py_DECREF(ann_dict);
2731 if (err != 0) {
2732 goto error;
2733 }
2734 }
2735 }
2736 else {
2737 /* do the same if locals() is not a dict */
2738 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2739 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002740 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002741 }
2742 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2743 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002744 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002745 goto error;
2746 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002747 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002748 ann_dict = PyDict_New();
2749 if (ann_dict == NULL) {
2750 goto error;
2751 }
2752 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2753 Py_DECREF(ann_dict);
2754 if (err != 0) {
2755 goto error;
2756 }
2757 }
2758 else {
2759 Py_DECREF(ann_dict);
2760 }
2761 }
2762 DISPATCH();
2763 }
2764
Benjamin Petersonddd19492018-09-16 22:38:02 -07002765 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002766 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002767 PyObject *map;
2768 PyObject *keys = TOP();
2769 if (!PyTuple_CheckExact(keys) ||
2770 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002771 _PyErr_SetString(tstate, PyExc_SystemError,
2772 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002773 goto error;
2774 }
2775 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2776 if (map == NULL) {
2777 goto error;
2778 }
2779 for (i = oparg; i > 0; i--) {
2780 int err;
2781 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2782 PyObject *value = PEEK(i + 1);
2783 err = PyDict_SetItem(map, key, value);
2784 if (err != 0) {
2785 Py_DECREF(map);
2786 goto error;
2787 }
2788 }
2789
2790 Py_DECREF(POP());
2791 while (oparg--) {
2792 Py_DECREF(POP());
2793 }
2794 PUSH(map);
2795 DISPATCH();
2796 }
2797
Mark Shannon8a4cd702020-01-27 09:57:45 +00002798 case TARGET(DICT_UPDATE): {
2799 PyObject *update = POP();
2800 PyObject *dict = PEEK(oparg);
2801 if (PyDict_Update(dict, update) < 0) {
2802 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
2803 _PyErr_Format(tstate, PyExc_TypeError,
2804 "'%.200s' object is not a mapping",
2805 update->ob_type->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002806 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002807 Py_DECREF(update);
2808 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002809 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002810 Py_DECREF(update);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002811 DISPATCH();
2812 }
2813
Mark Shannon8a4cd702020-01-27 09:57:45 +00002814 case TARGET(DICT_MERGE): {
2815 PyObject *update = POP();
2816 PyObject *dict = PEEK(oparg);
2817
2818 if (_PyDict_MergeEx(dict, update, 2) < 0) {
2819 format_kwargs_error(tstate, PEEK(2 + oparg), update);
2820 Py_DECREF(update);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002821 goto error;
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002822 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002823 Py_DECREF(update);
Brandt Bucherf185a732019-09-28 17:12:49 -07002824 PREDICT(CALL_FUNCTION_EX);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002825 DISPATCH();
2826 }
2827
Benjamin Petersonddd19492018-09-16 22:38:02 -07002828 case TARGET(MAP_ADD): {
Jörn Heisslerc8a35412019-06-22 16:40:55 +02002829 PyObject *value = TOP();
2830 PyObject *key = SECOND();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002831 PyObject *map;
2832 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002833 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002834 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002835 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002836 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002837 Py_DECREF(value);
2838 Py_DECREF(key);
2839 if (err != 0)
2840 goto error;
2841 PREDICT(JUMP_ABSOLUTE);
2842 DISPATCH();
2843 }
2844
Benjamin Petersonddd19492018-09-16 22:38:02 -07002845 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002846 PyObject *name = GETITEM(names, oparg);
2847 PyObject *owner = TOP();
2848 PyObject *res = PyObject_GetAttr(owner, name);
2849 Py_DECREF(owner);
2850 SET_TOP(res);
2851 if (res == NULL)
2852 goto error;
2853 DISPATCH();
2854 }
2855
Benjamin Petersonddd19492018-09-16 22:38:02 -07002856 case TARGET(COMPARE_OP): {
Mark Shannon9af0e472020-01-14 10:12:45 +00002857 assert(oparg <= Py_GE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002858 PyObject *right = POP();
2859 PyObject *left = TOP();
Mark Shannon9af0e472020-01-14 10:12:45 +00002860 PyObject *res = PyObject_RichCompare(left, right, oparg);
2861 SET_TOP(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002862 Py_DECREF(left);
2863 Py_DECREF(right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002864 if (res == NULL)
2865 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002866 PREDICT(POP_JUMP_IF_FALSE);
2867 PREDICT(POP_JUMP_IF_TRUE);
2868 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002869 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002870
Mark Shannon9af0e472020-01-14 10:12:45 +00002871 case TARGET(IS_OP): {
2872 PyObject *right = POP();
2873 PyObject *left = TOP();
2874 int res = (left == right)^oparg;
2875 PyObject *b = res ? Py_True : Py_False;
2876 Py_INCREF(b);
2877 SET_TOP(b);
2878 Py_DECREF(left);
2879 Py_DECREF(right);
2880 PREDICT(POP_JUMP_IF_FALSE);
2881 PREDICT(POP_JUMP_IF_TRUE);
2882 FAST_DISPATCH();
2883 }
2884
2885 case TARGET(CONTAINS_OP): {
2886 PyObject *right = POP();
2887 PyObject *left = POP();
2888 int res = PySequence_Contains(right, left);
2889 Py_DECREF(left);
2890 Py_DECREF(right);
2891 if (res < 0) {
2892 goto error;
2893 }
2894 PyObject *b = (res^oparg) ? Py_True : Py_False;
2895 Py_INCREF(b);
2896 PUSH(b);
2897 PREDICT(POP_JUMP_IF_FALSE);
2898 PREDICT(POP_JUMP_IF_TRUE);
2899 FAST_DISPATCH();
2900 }
2901
2902#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
2903 "BaseException is not allowed"
2904
2905 case TARGET(JUMP_IF_NOT_EXC_MATCH): {
2906 PyObject *right = POP();
2907 PyObject *left = POP();
2908 if (PyTuple_Check(right)) {
2909 Py_ssize_t i, length;
2910 length = PyTuple_GET_SIZE(right);
2911 for (i = 0; i < length; i++) {
2912 PyObject *exc = PyTuple_GET_ITEM(right, i);
2913 if (!PyExceptionClass_Check(exc)) {
2914 _PyErr_SetString(tstate, PyExc_TypeError,
2915 CANNOT_CATCH_MSG);
2916 Py_DECREF(left);
2917 Py_DECREF(right);
2918 goto error;
2919 }
2920 }
2921 }
2922 else {
2923 if (!PyExceptionClass_Check(right)) {
2924 _PyErr_SetString(tstate, PyExc_TypeError,
2925 CANNOT_CATCH_MSG);
2926 Py_DECREF(left);
2927 Py_DECREF(right);
2928 goto error;
2929 }
2930 }
2931 int res = PyErr_GivenExceptionMatches(left, right);
2932 Py_DECREF(left);
2933 Py_DECREF(right);
2934 if (res > 0) {
2935 /* Exception matches -- Do nothing */;
2936 }
2937 else if (res == 0) {
2938 JUMPTO(oparg);
2939 }
2940 else {
2941 goto error;
2942 }
2943 DISPATCH();
2944 }
2945
Benjamin Petersonddd19492018-09-16 22:38:02 -07002946 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002947 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002948 PyObject *fromlist = POP();
2949 PyObject *level = TOP();
2950 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02002951 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002952 Py_DECREF(level);
2953 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002954 SET_TOP(res);
2955 if (res == NULL)
2956 goto error;
2957 DISPATCH();
2958 }
2959
Benjamin Petersonddd19492018-09-16 22:38:02 -07002960 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002961 PyObject *from = POP(), *locals;
2962 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002963 if (PyFrame_FastToLocalsWithError(f) < 0) {
2964 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002965 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002966 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002967
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002968 locals = f->f_locals;
2969 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002970 _PyErr_SetString(tstate, PyExc_SystemError,
2971 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002972 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002973 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002974 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002975 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002976 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002977 Py_DECREF(from);
2978 if (err != 0)
2979 goto error;
2980 DISPATCH();
2981 }
Guido van Rossum25831651993-05-19 14:50:45 +00002982
Benjamin Petersonddd19492018-09-16 22:38:02 -07002983 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002984 PyObject *name = GETITEM(names, oparg);
2985 PyObject *from = TOP();
2986 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02002987 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002988 PUSH(res);
2989 if (res == NULL)
2990 goto error;
2991 DISPATCH();
2992 }
Thomas Wouters52152252000-08-17 22:55:00 +00002993
Benjamin Petersonddd19492018-09-16 22:38:02 -07002994 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002995 JUMPBY(oparg);
2996 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002997 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002998
Benjamin Petersonddd19492018-09-16 22:38:02 -07002999 case TARGET(POP_JUMP_IF_FALSE): {
3000 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003001 PyObject *cond = POP();
3002 int err;
3003 if (cond == Py_True) {
3004 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003005 FAST_DISPATCH();
3006 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003007 if (cond == Py_False) {
3008 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003009 JUMPTO(oparg);
3010 FAST_DISPATCH();
3011 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003012 err = PyObject_IsTrue(cond);
3013 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003014 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003015 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003016 else if (err == 0)
3017 JUMPTO(oparg);
3018 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003019 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003020 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003021 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003022
Benjamin Petersonddd19492018-09-16 22:38:02 -07003023 case TARGET(POP_JUMP_IF_TRUE): {
3024 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003025 PyObject *cond = POP();
3026 int err;
3027 if (cond == Py_False) {
3028 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003029 FAST_DISPATCH();
3030 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003031 if (cond == Py_True) {
3032 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003033 JUMPTO(oparg);
3034 FAST_DISPATCH();
3035 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003036 err = PyObject_IsTrue(cond);
3037 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003038 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003039 JUMPTO(oparg);
3040 }
3041 else if (err == 0)
3042 ;
3043 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003044 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003045 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003046 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003047
Benjamin Petersonddd19492018-09-16 22:38:02 -07003048 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003049 PyObject *cond = TOP();
3050 int err;
3051 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003052 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003053 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003054 FAST_DISPATCH();
3055 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003056 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003057 JUMPTO(oparg);
3058 FAST_DISPATCH();
3059 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003060 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003061 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003062 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003063 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003064 }
3065 else if (err == 0)
3066 JUMPTO(oparg);
3067 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003068 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003069 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003070 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003071
Benjamin Petersonddd19492018-09-16 22:38:02 -07003072 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003073 PyObject *cond = TOP();
3074 int err;
3075 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003076 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003077 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003078 FAST_DISPATCH();
3079 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003080 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003081 JUMPTO(oparg);
3082 FAST_DISPATCH();
3083 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003084 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003085 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003086 JUMPTO(oparg);
3087 }
3088 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003089 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003090 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003091 }
3092 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003093 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003094 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003095 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003096
Benjamin Petersonddd19492018-09-16 22:38:02 -07003097 case TARGET(JUMP_ABSOLUTE): {
3098 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003099 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003100#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003101 /* Enabling this path speeds-up all while and for-loops by bypassing
3102 the per-loop checks for signals. By default, this should be turned-off
3103 because it prevents detection of a control-break in tight loops like
3104 "while 1: pass". Compile with this option turned-on when you need
3105 the speed-up and do not need break checking inside tight loops (ones
3106 that contain only instructions ending with FAST_DISPATCH).
3107 */
3108 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003109#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003110 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003111#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003112 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003113
Benjamin Petersonddd19492018-09-16 22:38:02 -07003114 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003115 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003116 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003117 PyObject *iter = PyObject_GetIter(iterable);
3118 Py_DECREF(iterable);
3119 SET_TOP(iter);
3120 if (iter == NULL)
3121 goto error;
3122 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003123 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003124 DISPATCH();
3125 }
3126
Benjamin Petersonddd19492018-09-16 22:38:02 -07003127 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04003128 /* before: [obj]; after [getiter(obj)] */
3129 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003130 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003131 if (PyCoro_CheckExact(iterable)) {
3132 /* `iterable` is a coroutine */
3133 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3134 /* and it is used in a 'yield from' expression of a
3135 regular generator. */
3136 Py_DECREF(iterable);
3137 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003138 _PyErr_SetString(tstate, PyExc_TypeError,
3139 "cannot 'yield from' a coroutine object "
3140 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04003141 goto error;
3142 }
3143 }
3144 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003145 /* `iterable` is not a generator. */
3146 iter = PyObject_GetIter(iterable);
3147 Py_DECREF(iterable);
3148 SET_TOP(iter);
3149 if (iter == NULL)
3150 goto error;
3151 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003152 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003153 DISPATCH();
3154 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003155
Benjamin Petersonddd19492018-09-16 22:38:02 -07003156 case TARGET(FOR_ITER): {
3157 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003158 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003159 PyObject *iter = TOP();
3160 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
3161 if (next != NULL) {
3162 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003163 PREDICT(STORE_FAST);
3164 PREDICT(UNPACK_SEQUENCE);
3165 DISPATCH();
3166 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003167 if (_PyErr_Occurred(tstate)) {
3168 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003169 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003170 }
3171 else if (tstate->c_tracefunc != NULL) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003172 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Victor Stinner438a12d2019-05-24 17:01:38 +02003173 }
3174 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003175 }
3176 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003177 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003178 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003179 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003180 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003181 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003182 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003183
Benjamin Petersonddd19492018-09-16 22:38:02 -07003184 case TARGET(SETUP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003185 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003186 STACK_LEVEL());
3187 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003188 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003189
Benjamin Petersonddd19492018-09-16 22:38:02 -07003190 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003191 _Py_IDENTIFIER(__aenter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003192 _Py_IDENTIFIER(__aexit__);
Yury Selivanov75445082015-05-11 22:57:16 -04003193 PyObject *mgr = TOP();
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003194 PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04003195 PyObject *res;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003196 if (enter == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003197 goto error;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003198 }
3199 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__);
3200 if (exit == NULL) {
3201 Py_DECREF(enter);
3202 goto error;
3203 }
Yury Selivanov75445082015-05-11 22:57:16 -04003204 SET_TOP(exit);
Yury Selivanov75445082015-05-11 22:57:16 -04003205 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003206 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003207 Py_DECREF(enter);
3208 if (res == NULL)
3209 goto error;
3210 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003211 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003212 DISPATCH();
3213 }
3214
Benjamin Petersonddd19492018-09-16 22:38:02 -07003215 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003216 PyObject *res = POP();
3217 /* Setup the finally block before pushing the result
3218 of __aenter__ on the stack. */
3219 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3220 STACK_LEVEL());
3221 PUSH(res);
3222 DISPATCH();
3223 }
3224
Benjamin Petersonddd19492018-09-16 22:38:02 -07003225 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003226 _Py_IDENTIFIER(__enter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003227 _Py_IDENTIFIER(__exit__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003228 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003229 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003230 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003231 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08003232 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003233 }
3234 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003235 if (exit == NULL) {
3236 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003237 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003238 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003239 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003240 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003241 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003242 Py_DECREF(enter);
3243 if (res == NULL)
3244 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003245 /* Setup the finally block before pushing the result
3246 of __enter__ on the stack. */
3247 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3248 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003249
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003250 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003251 DISPATCH();
3252 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003253
Mark Shannonfee55262019-11-21 09:11:43 +00003254 case TARGET(WITH_EXCEPT_START): {
3255 /* At the top of the stack are 7 values:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003256 - (TOP, SECOND, THIRD) = exc_info()
Mark Shannonfee55262019-11-21 09:11:43 +00003257 - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER
3258 - SEVENTH: the context.__exit__ bound method
3259 We call SEVENTH(TOP, SECOND, THIRD).
3260 Then we push again the TOP exception and the __exit__
3261 return value.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003262 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003263 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003264 PyObject *exc, *val, *tb, *res;
3265
Victor Stinner842cfff2016-12-01 14:45:31 +01003266 exc = TOP();
Mark Shannonfee55262019-11-21 09:11:43 +00003267 val = SECOND();
3268 tb = THIRD();
3269 assert(exc != Py_None);
3270 assert(!PyLong_Check(exc));
3271 exit_func = PEEK(7);
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003272 PyObject *stack[4] = {NULL, exc, val, tb};
3273 res = _PyObject_Vectorcall(exit_func, stack + 1,
3274 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003275 if (res == NULL)
3276 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003277
Yury Selivanov75445082015-05-11 22:57:16 -04003278 PUSH(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003279 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003280 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003281
Benjamin Petersonddd19492018-09-16 22:38:02 -07003282 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10003283 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003284 PyObject *name = GETITEM(names, oparg);
3285 PyObject *obj = TOP();
3286 PyObject *meth = NULL;
3287
3288 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3289
Yury Selivanovf2392132016-12-13 19:03:51 -05003290 if (meth == NULL) {
3291 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003292 goto error;
3293 }
3294
3295 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003296 /* We can bypass temporary bound method object.
3297 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003298
INADA Naoki015bce62017-01-16 17:23:30 +09003299 meth | self | arg1 | ... | argN
3300 */
3301 SET_TOP(meth);
3302 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003303 }
3304 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003305 /* meth is not an unbound method (but a regular attr, or
3306 something was returned by a descriptor protocol). Set
3307 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003308 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003309
3310 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003311 */
INADA Naoki015bce62017-01-16 17:23:30 +09003312 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003313 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003314 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003315 }
3316 DISPATCH();
3317 }
3318
Benjamin Petersonddd19492018-09-16 22:38:02 -07003319 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003320 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003321 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003322
3323 sp = stack_pointer;
3324
INADA Naoki015bce62017-01-16 17:23:30 +09003325 meth = PEEK(oparg + 2);
3326 if (meth == NULL) {
3327 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3328 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003329
3330 Stack layout:
3331
INADA Naoki015bce62017-01-16 17:23:30 +09003332 ... | NULL | callable | arg1 | ... | argN
3333 ^- TOP()
3334 ^- (-oparg)
3335 ^- (-oparg-1)
3336 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003337
Ville Skyttä49b27342017-08-03 09:00:59 +03003338 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003339 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003340 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003341 res = call_function(tstate, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003342 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003343 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003344 }
3345 else {
3346 /* This is a method call. Stack layout:
3347
INADA Naoki015bce62017-01-16 17:23:30 +09003348 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003349 ^- TOP()
3350 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003351 ^- (-oparg-1)
3352 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003353
INADA Naoki015bce62017-01-16 17:23:30 +09003354 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003355 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003356 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003357 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003358 res = call_function(tstate, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003359 stack_pointer = sp;
3360 }
3361
3362 PUSH(res);
3363 if (res == NULL)
3364 goto error;
3365 DISPATCH();
3366 }
3367
Benjamin Petersonddd19492018-09-16 22:38:02 -07003368 case TARGET(CALL_FUNCTION): {
3369 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003370 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003371 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003372 res = call_function(tstate, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003373 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003374 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003375 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003376 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003377 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003378 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003379 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003380
Benjamin Petersonddd19492018-09-16 22:38:02 -07003381 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003382 PyObject **sp, *res, *names;
3383
3384 names = POP();
Jeroen Demeyer05677862019-08-16 12:41:27 +02003385 assert(PyTuple_Check(names));
3386 assert(PyTuple_GET_SIZE(names) <= oparg);
3387 /* We assume without checking that names contains only strings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003388 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003389 res = call_function(tstate, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003390 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003391 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003392 Py_DECREF(names);
3393
3394 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003395 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003396 }
3397 DISPATCH();
3398 }
3399
Benjamin Petersonddd19492018-09-16 22:38:02 -07003400 case TARGET(CALL_FUNCTION_EX): {
Brandt Bucherf185a732019-09-28 17:12:49 -07003401 PREDICTED(CALL_FUNCTION_EX);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003402 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003403 if (oparg & 0x01) {
3404 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003405 if (!PyDict_CheckExact(kwargs)) {
3406 PyObject *d = PyDict_New();
3407 if (d == NULL)
3408 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003409 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003410 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02003411 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003412 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003413 goto error;
3414 }
3415 Py_DECREF(kwargs);
3416 kwargs = d;
3417 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003418 assert(PyDict_CheckExact(kwargs));
3419 }
3420 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003421 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003422 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003423 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003424 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003425 goto error;
3426 }
3427 Py_SETREF(callargs, PySequence_Tuple(callargs));
3428 if (callargs == NULL) {
3429 goto error;
3430 }
3431 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003432 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003433
Victor Stinner09532fe2019-05-10 23:39:09 +02003434 result = do_call_core(tstate, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003435 Py_DECREF(func);
3436 Py_DECREF(callargs);
3437 Py_XDECREF(kwargs);
3438
3439 SET_TOP(result);
3440 if (result == NULL) {
3441 goto error;
3442 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003443 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003444 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003445
Benjamin Petersonddd19492018-09-16 22:38:02 -07003446 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003447 PyObject *qualname = POP();
3448 PyObject *codeobj = POP();
3449 PyFunctionObject *func = (PyFunctionObject *)
3450 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003451
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003452 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003453 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003454 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003455 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003456 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003457
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003458 if (oparg & 0x08) {
3459 assert(PyTuple_CheckExact(TOP()));
3460 func ->func_closure = POP();
3461 }
3462 if (oparg & 0x04) {
3463 assert(PyDict_CheckExact(TOP()));
3464 func->func_annotations = POP();
3465 }
3466 if (oparg & 0x02) {
3467 assert(PyDict_CheckExact(TOP()));
3468 func->func_kwdefaults = POP();
3469 }
3470 if (oparg & 0x01) {
3471 assert(PyTuple_CheckExact(TOP()));
3472 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003473 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003474
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003475 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003476 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003477 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003478
Benjamin Petersonddd19492018-09-16 22:38:02 -07003479 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003480 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003481 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003482 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003483 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003484 step = NULL;
3485 stop = POP();
3486 start = TOP();
3487 slice = PySlice_New(start, stop, step);
3488 Py_DECREF(start);
3489 Py_DECREF(stop);
3490 Py_XDECREF(step);
3491 SET_TOP(slice);
3492 if (slice == NULL)
3493 goto error;
3494 DISPATCH();
3495 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003496
Benjamin Petersonddd19492018-09-16 22:38:02 -07003497 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003498 /* Handles f-string value formatting. */
3499 PyObject *result;
3500 PyObject *fmt_spec;
3501 PyObject *value;
3502 PyObject *(*conv_fn)(PyObject *);
3503 int which_conversion = oparg & FVC_MASK;
3504 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3505
3506 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003507 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003508
3509 /* See if any conversion is specified. */
3510 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003511 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003512 case FVC_STR: conv_fn = PyObject_Str; break;
3513 case FVC_REPR: conv_fn = PyObject_Repr; break;
3514 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003515 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02003516 _PyErr_Format(tstate, PyExc_SystemError,
3517 "unexpected conversion flag %d",
3518 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003519 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003520 }
3521
3522 /* If there's a conversion function, call it and replace
3523 value with that result. Otherwise, just use value,
3524 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003525 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003526 result = conv_fn(value);
3527 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003528 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003529 Py_XDECREF(fmt_spec);
3530 goto error;
3531 }
3532 value = result;
3533 }
3534
3535 /* If value is a unicode object, and there's no fmt_spec,
3536 then we know the result of format(value) is value
3537 itself. In that case, skip calling format(). I plan to
3538 move this optimization in to PyObject_Format()
3539 itself. */
3540 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3541 /* Do nothing, just transfer ownership to result. */
3542 result = value;
3543 } else {
3544 /* Actually call format(). */
3545 result = PyObject_Format(value, fmt_spec);
3546 Py_DECREF(value);
3547 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003548 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003549 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003550 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003551 }
3552
Eric V. Smith135d5f42016-02-05 18:23:08 -05003553 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003554 DISPATCH();
3555 }
3556
Benjamin Petersonddd19492018-09-16 22:38:02 -07003557 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003558 int oldoparg = oparg;
3559 NEXTOPARG();
3560 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003561 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003562 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003563
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003564
Antoine Pitrou042b1282010-08-13 21:15:58 +00003565#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003566 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003567#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003568 default:
3569 fprintf(stderr,
3570 "XXX lineno: %d, opcode: %d\n",
3571 PyFrame_GetLineNumber(f),
3572 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02003573 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003574 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003576 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003577
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003578 /* This should never be reached. Every opcode should end with DISPATCH()
3579 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003580 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003581
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003582error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003583 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003584#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02003585 if (!_PyErr_Occurred(tstate)) {
3586 _PyErr_SetString(tstate, PyExc_SystemError,
3587 "error return without exception set");
3588 }
Victor Stinner365b6932013-07-12 00:11:58 +02003589#else
Victor Stinner438a12d2019-05-24 17:01:38 +02003590 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02003591#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003592
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003593 /* Log traceback info. */
3594 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003595
Benjamin Peterson51f46162013-01-23 08:38:47 -05003596 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003597 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3598 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003599
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003600exception_unwind:
3601 /* Unwind stacks if an exception occurred */
3602 while (f->f_iblock > 0) {
3603 /* Pop the current block. */
3604 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003606 if (b->b_type == EXCEPT_HANDLER) {
3607 UNWIND_EXCEPT_HANDLER(b);
3608 continue;
3609 }
3610 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003611 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003612 PyObject *exc, *val, *tb;
3613 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003614 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003615 /* Beware, this invalidates all b->b_* fields */
3616 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003617 PUSH(exc_info->exc_traceback);
3618 PUSH(exc_info->exc_value);
3619 if (exc_info->exc_type != NULL) {
3620 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003621 }
3622 else {
3623 Py_INCREF(Py_None);
3624 PUSH(Py_None);
3625 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003626 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003627 /* Make the raw exception data
3628 available to the handler,
3629 so a program can emulate the
3630 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02003631 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003632 if (tb != NULL)
3633 PyException_SetTraceback(val, tb);
3634 else
3635 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003636 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003637 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003638 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003639 exc_info->exc_value = val;
3640 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003641 if (tb == NULL)
3642 tb = Py_None;
3643 Py_INCREF(tb);
3644 PUSH(tb);
3645 PUSH(val);
3646 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003647 JUMPTO(handler);
Pablo Galindo4c53e632020-01-10 09:24:22 +00003648 if (_Py_TracingPossible(ceval)) {
3649 int needs_new_execution_window = (f->f_lasti < instr_lb || f->f_lasti >= instr_ub);
3650 int needs_line_update = (f->f_lasti == instr_lb || f->f_lasti < instr_prev);
3651 /* Make sure that we trace line after exception if we are in a new execution
3652 * window or we don't need a line update and we are not in the first instruction
3653 * of the line. */
3654 if (needs_new_execution_window || (!needs_line_update && instr_lb > 0)) {
3655 instr_prev = INT_MAX;
3656 }
Mark Shannonfee55262019-11-21 09:11:43 +00003657 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003658 /* Resume normal execution */
3659 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003660 }
3661 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003662
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003663 /* End the loop as we still have an error */
3664 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003665 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003666
Pablo Galindof00828a2019-05-09 16:52:02 +01003667 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003668 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01003669
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003670 /* Pop remaining stack entries. */
3671 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003672 PyObject *o = POP();
3673 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003674 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003675
Mark Shannone7c9f4a2020-01-13 12:51:26 +00003676exiting:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003677 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003678 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003679 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3680 tstate, f, PyTrace_RETURN, retval)) {
3681 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003682 }
3683 }
3684 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003685 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3686 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003687 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003688 }
3689 }
3690 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003691
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003692 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003693exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003694 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3695 dtrace_function_return(f);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01003696 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrou58720d62013-08-05 23:26:40 +02003697 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003698 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003699
Victor Stinner17269092019-11-05 01:22:12 +01003700 return _Py_CheckFunctionResult(tstate, NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003701}
3702
Benjamin Petersonb204a422011-06-05 22:04:07 -05003703static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003704format_missing(PyThreadState *tstate, const char *kind,
3705 PyCodeObject *co, PyObject *names)
Benjamin Petersone109c702011-06-24 09:37:26 -05003706{
3707 int err;
3708 Py_ssize_t len = PyList_GET_SIZE(names);
3709 PyObject *name_str, *comma, *tail, *tmp;
3710
3711 assert(PyList_CheckExact(names));
3712 assert(len >= 1);
3713 /* Deal with the joys of natural language. */
3714 switch (len) {
3715 case 1:
3716 name_str = PyList_GET_ITEM(names, 0);
3717 Py_INCREF(name_str);
3718 break;
3719 case 2:
3720 name_str = PyUnicode_FromFormat("%U and %U",
3721 PyList_GET_ITEM(names, len - 2),
3722 PyList_GET_ITEM(names, len - 1));
3723 break;
3724 default:
3725 tail = PyUnicode_FromFormat(", %U, and %U",
3726 PyList_GET_ITEM(names, len - 2),
3727 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003728 if (tail == NULL)
3729 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003730 /* Chop off the last two objects in the list. This shouldn't actually
3731 fail, but we can't be too careful. */
3732 err = PyList_SetSlice(names, len - 2, len, NULL);
3733 if (err == -1) {
3734 Py_DECREF(tail);
3735 return;
3736 }
3737 /* Stitch everything up into a nice comma-separated list. */
3738 comma = PyUnicode_FromString(", ");
3739 if (comma == NULL) {
3740 Py_DECREF(tail);
3741 return;
3742 }
3743 tmp = PyUnicode_Join(comma, names);
3744 Py_DECREF(comma);
3745 if (tmp == NULL) {
3746 Py_DECREF(tail);
3747 return;
3748 }
3749 name_str = PyUnicode_Concat(tmp, tail);
3750 Py_DECREF(tmp);
3751 Py_DECREF(tail);
3752 break;
3753 }
3754 if (name_str == NULL)
3755 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02003756 _PyErr_Format(tstate, PyExc_TypeError,
3757 "%U() missing %i required %s argument%s: %U",
3758 co->co_name,
3759 len,
3760 kind,
3761 len == 1 ? "" : "s",
3762 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05003763 Py_DECREF(name_str);
3764}
3765
3766static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003767missing_arguments(PyThreadState *tstate, PyCodeObject *co,
3768 Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003769 PyObject **fastlocals)
3770{
Victor Stinner74319ae2016-08-25 00:04:09 +02003771 Py_ssize_t i, j = 0;
3772 Py_ssize_t start, end;
3773 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003774 const char *kind = positional ? "positional" : "keyword-only";
3775 PyObject *missing_names;
3776
3777 /* Compute the names of the arguments that are missing. */
3778 missing_names = PyList_New(missing);
3779 if (missing_names == NULL)
3780 return;
3781 if (positional) {
3782 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01003783 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003784 }
3785 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003786 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003787 end = start + co->co_kwonlyargcount;
3788 }
3789 for (i = start; i < end; i++) {
3790 if (GETLOCAL(i) == NULL) {
3791 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3792 PyObject *name = PyObject_Repr(raw);
3793 if (name == NULL) {
3794 Py_DECREF(missing_names);
3795 return;
3796 }
3797 PyList_SET_ITEM(missing_names, j++, name);
3798 }
3799 }
3800 assert(j == missing);
Victor Stinner438a12d2019-05-24 17:01:38 +02003801 format_missing(tstate, kind, co, missing_names);
Benjamin Petersone109c702011-06-24 09:37:26 -05003802 Py_DECREF(missing_names);
3803}
3804
3805static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003806too_many_positional(PyThreadState *tstate, PyCodeObject *co,
3807 Py_ssize_t given, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003808 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003809{
3810 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003811 Py_ssize_t kwonly_given = 0;
3812 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003813 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003814 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003815
Benjamin Petersone109c702011-06-24 09:37:26 -05003816 assert((co->co_flags & CO_VARARGS) == 0);
3817 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01003818 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003819 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003820 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003821 }
3822 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003823 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01003824 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003825 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01003826 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003827 }
3828 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003829 plural = (co_argcount != 1);
3830 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003831 }
3832 if (sig == NULL)
3833 return;
3834 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003835 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3836 kwonly_sig = PyUnicode_FromFormat(format,
3837 given != 1 ? "s" : "",
3838 kwonly_given,
3839 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003840 if (kwonly_sig == NULL) {
3841 Py_DECREF(sig);
3842 return;
3843 }
3844 }
3845 else {
3846 /* This will not fail. */
3847 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003848 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003849 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003850 _PyErr_Format(tstate, PyExc_TypeError,
3851 "%U() takes %U positional argument%s but %zd%U %s given",
3852 co->co_name,
3853 sig,
3854 plural ? "s" : "",
3855 given,
3856 kwonly_sig,
3857 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003858 Py_DECREF(sig);
3859 Py_DECREF(kwonly_sig);
3860}
3861
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003862static int
Victor Stinner438a12d2019-05-24 17:01:38 +02003863positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
3864 Py_ssize_t kwcount, PyObject* const* kwnames)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003865{
3866 int posonly_conflicts = 0;
3867 PyObject* posonly_names = PyList_New(0);
3868
3869 for(int k=0; k < co->co_posonlyargcount; k++){
3870 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
3871
3872 for (int k2=0; k2<kwcount; k2++){
3873 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
3874 PyObject* kwname = kwnames[k2];
3875 if (kwname == posonly_name){
3876 if(PyList_Append(posonly_names, kwname) != 0) {
3877 goto fail;
3878 }
3879 posonly_conflicts++;
3880 continue;
3881 }
3882
3883 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
3884
3885 if ( cmp > 0) {
3886 if(PyList_Append(posonly_names, kwname) != 0) {
3887 goto fail;
3888 }
3889 posonly_conflicts++;
3890 } else if (cmp < 0) {
3891 goto fail;
3892 }
3893
3894 }
3895 }
3896 if (posonly_conflicts) {
3897 PyObject* comma = PyUnicode_FromString(", ");
3898 if (comma == NULL) {
3899 goto fail;
3900 }
3901 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
3902 Py_DECREF(comma);
3903 if (error_names == NULL) {
3904 goto fail;
3905 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003906 _PyErr_Format(tstate, PyExc_TypeError,
3907 "%U() got some positional-only arguments passed"
3908 " as keyword arguments: '%U'",
3909 co->co_name, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003910 Py_DECREF(error_names);
3911 goto fail;
3912 }
3913
3914 Py_DECREF(posonly_names);
3915 return 0;
3916
3917fail:
3918 Py_XDECREF(posonly_names);
3919 return 1;
3920
3921}
3922
Guido van Rossumc2e20742006-02-27 22:32:47 +00003923/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02003924 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003925 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003926
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003927PyObject *
Victor Stinnerb5e170f2019-11-16 01:03:22 +01003928_PyEval_EvalCode(PyThreadState *tstate,
3929 PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003930 PyObject *const *args, Py_ssize_t argcount,
3931 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003932 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003933 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003934 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003935 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003936{
Victor Stinnerb5e170f2019-11-16 01:03:22 +01003937 assert(tstate != NULL);
3938
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003939 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003940 PyFrameObject *f;
3941 PyObject *retval = NULL;
3942 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003943 PyObject *x, *u;
Pablo Galindocd74e662019-06-01 18:08:04 +01003944 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003945 Py_ssize_t i, j, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003946 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003947
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003948 if (globals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003949 _PyErr_SetString(tstate, PyExc_SystemError,
3950 "PyEval_EvalCodeEx: NULL globals");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003951 return NULL;
3952 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003953
Victor Stinnerc7020012016-08-16 23:40:29 +02003954 /* Create the frame */
INADA Naoki5a625d02016-12-24 20:19:08 +09003955 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003956 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003957 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003958 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003959 fastlocals = f->f_localsplus;
3960 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003961
Victor Stinnerc7020012016-08-16 23:40:29 +02003962 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003963 if (co->co_flags & CO_VARKEYWORDS) {
3964 kwdict = PyDict_New();
3965 if (kwdict == NULL)
3966 goto fail;
3967 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003968 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003969 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003970 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003971 SETLOCAL(i, kwdict);
3972 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003973 else {
3974 kwdict = NULL;
3975 }
3976
Pablo Galindocd74e662019-06-01 18:08:04 +01003977 /* Copy all positional arguments into local variables */
3978 if (argcount > co->co_argcount) {
3979 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003980 }
3981 else {
3982 n = argcount;
3983 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003984 for (j = 0; j < n; j++) {
3985 x = args[j];
3986 Py_INCREF(x);
3987 SETLOCAL(j, x);
3988 }
3989
Victor Stinnerc7020012016-08-16 23:40:29 +02003990 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003991 if (co->co_flags & CO_VARARGS) {
Sergey Fedoseev234531b2019-02-25 21:59:12 +05003992 u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003993 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003994 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003995 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003996 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003997 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003998
Serhiy Storchakab7281052016-09-12 00:52:40 +03003999 /* Handle keyword arguments passed as two strided arrays */
4000 kwcount *= kwstep;
4001 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004002 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03004003 PyObject *keyword = kwnames[i];
4004 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02004005 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004006
Benjamin Petersonb204a422011-06-05 22:04:07 -05004007 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004008 _PyErr_Format(tstate, PyExc_TypeError,
4009 "%U() keywords must be strings",
4010 co->co_name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004011 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004012 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004013
Benjamin Petersonb204a422011-06-05 22:04:07 -05004014 /* Speed hack: do raw pointer compares. As names are
4015 normally interned this should almost always hit. */
4016 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004017 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004018 PyObject *name = co_varnames[j];
4019 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004020 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004021 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004022 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004023
Benjamin Petersonb204a422011-06-05 22:04:07 -05004024 /* Slow fallback, just in case */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004025 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004026 PyObject *name = co_varnames[j];
4027 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
4028 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004029 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004030 }
4031 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004032 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004033 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004034 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004035
Victor Stinner231d1f32017-01-11 02:12:06 +01004036 assert(j >= total_args);
4037 if (kwdict == NULL) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004038
Victor Stinner438a12d2019-05-24 17:01:38 +02004039 if (co->co_posonlyargcount
4040 && positional_only_passed_as_keyword(tstate, co,
4041 kwcount, kwnames))
4042 {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004043 goto fail;
4044 }
4045
Victor Stinner438a12d2019-05-24 17:01:38 +02004046 _PyErr_Format(tstate, PyExc_TypeError,
4047 "%U() got an unexpected keyword argument '%S'",
4048 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004049 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004050 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004051
Christian Heimes0bd447f2013-07-20 14:48:10 +02004052 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4053 goto fail;
4054 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004055 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004056
Benjamin Petersonb204a422011-06-05 22:04:07 -05004057 kw_found:
4058 if (GETLOCAL(j) != NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004059 _PyErr_Format(tstate, PyExc_TypeError,
4060 "%U() got multiple values for argument '%S'",
4061 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004062 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004063 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004064 Py_INCREF(value);
4065 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004066 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004067
4068 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004069 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004070 too_many_positional(tstate, co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004071 goto fail;
4072 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004073
4074 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004075 if (argcount < co->co_argcount) {
4076 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004077 Py_ssize_t missing = 0;
4078 for (i = argcount; i < m; i++) {
4079 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004080 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004081 }
4082 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004083 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004084 missing_arguments(tstate, co, missing, defcount, fastlocals);
Benjamin Petersone109c702011-06-24 09:37:26 -05004085 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004086 }
4087 if (n > m)
4088 i = n - m;
4089 else
4090 i = 0;
4091 for (; i < defcount; i++) {
4092 if (GETLOCAL(m+i) == NULL) {
4093 PyObject *def = defs[i];
4094 Py_INCREF(def);
4095 SETLOCAL(m+i, def);
4096 }
4097 }
4098 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004099
4100 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004101 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004102 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004103 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004104 PyObject *name;
4105 if (GETLOCAL(i) != NULL)
4106 continue;
4107 name = PyTuple_GET_ITEM(co->co_varnames, i);
4108 if (kwdefs != NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004109 PyObject *def = PyDict_GetItemWithError(kwdefs, name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004110 if (def) {
4111 Py_INCREF(def);
4112 SETLOCAL(i, def);
4113 continue;
4114 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004115 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004116 goto fail;
4117 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004118 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004119 missing++;
4120 }
4121 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004122 missing_arguments(tstate, co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004123 goto fail;
4124 }
4125 }
4126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004127 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004128 vars into frame. */
4129 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004130 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004131 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004132 /* Possibly account for the cell variable being an argument. */
4133 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004134 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004135 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004136 /* Clear the local copy. */
4137 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004138 }
4139 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004140 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004141 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004142 if (c == NULL)
4143 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004144 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004145 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004146
4147 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004148 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4149 PyObject *o = PyTuple_GET_ITEM(closure, i);
4150 Py_INCREF(o);
4151 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004152 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004153
Yury Selivanoveb636452016-09-08 22:01:51 -07004154 /* Handle generator/coroutine/asynchronous generator */
4155 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004156 PyObject *gen;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004157 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004158
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004159 /* Don't need to keep the reference to f_back, it will be set
4160 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004161 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004162
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004163 /* Create a new generator that owns the ready to run frame
4164 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004165 if (is_coro) {
4166 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004167 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4168 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004169 } else {
4170 gen = PyGen_NewWithQualName(f, name, qualname);
4171 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004172 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004173 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004174 }
INADA Naoki9c157762016-12-26 18:52:46 +09004175
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004176 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004177
Yury Selivanov75445082015-05-11 22:57:16 -04004178 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004179 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004180
Victor Stinnerb9e68122019-11-14 12:20:46 +01004181 retval = _PyEval_EvalFrame(tstate, f, 0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004182
Thomas Woutersce272b62007-09-19 21:19:28 +00004183fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004184
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004185 /* decref'ing the frame can cause __del__ methods to get invoked,
4186 which can call back into Python. While we're done with the
4187 current Python frame (f), the associated C stack is still in use,
4188 so recursion_depth must be boosted for the duration.
4189 */
INADA Naoki5a625d02016-12-24 20:19:08 +09004190 if (Py_REFCNT(f) > 1) {
4191 Py_DECREF(f);
4192 _PyObject_GC_TRACK(f);
4193 }
4194 else {
4195 ++tstate->recursion_depth;
4196 Py_DECREF(f);
4197 --tstate->recursion_depth;
4198 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004199 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004200}
4201
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004202
4203PyObject *
4204_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
4205 PyObject *const *args, Py_ssize_t argcount,
4206 PyObject *const *kwnames, PyObject *const *kwargs,
4207 Py_ssize_t kwcount, int kwstep,
4208 PyObject *const *defs, Py_ssize_t defcount,
4209 PyObject *kwdefs, PyObject *closure,
4210 PyObject *name, PyObject *qualname)
4211{
4212 PyThreadState *tstate = _PyThreadState_GET();
4213 return _PyEval_EvalCode(tstate, _co, globals, locals,
4214 args, argcount,
4215 kwnames, kwargs,
4216 kwcount, kwstep,
4217 defs, defcount,
4218 kwdefs, closure,
4219 name, qualname);
4220}
4221
Victor Stinner40ee3012014-06-16 15:59:28 +02004222PyObject *
4223PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004224 PyObject *const *args, int argcount,
4225 PyObject *const *kws, int kwcount,
4226 PyObject *const *defs, int defcount,
4227 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004228{
4229 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004230 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004231 kws, kws != NULL ? kws + 1 : NULL,
4232 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004233 defs, defcount,
4234 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004235 NULL, NULL);
4236}
Tim Peters5ca576e2001-06-18 22:08:13 +00004237
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004238static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004239special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004240{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004241 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004242 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02004243 if (res == NULL && !_PyErr_Occurred(tstate)) {
4244 _PyErr_SetObject(tstate, PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004245 return NULL;
4246 }
4247 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004248}
4249
4250
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004251/* Logic for the raise statement (too complicated for inlining).
4252 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004253static int
Victor Stinner09532fe2019-05-10 23:39:09 +02004254do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004255{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004256 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004257
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004258 if (exc == NULL) {
4259 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01004260 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004261 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004262 type = exc_info->exc_type;
4263 value = exc_info->exc_value;
4264 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004265 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004266 _PyErr_SetString(tstate, PyExc_RuntimeError,
4267 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004268 return 0;
4269 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004270 Py_XINCREF(type);
4271 Py_XINCREF(value);
4272 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02004273 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004274 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004275 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004277 /* We support the following forms of raise:
4278 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004279 raise <instance>
4280 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004282 if (PyExceptionClass_Check(exc)) {
4283 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004284 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004285 if (value == NULL)
4286 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004287 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004288 _PyErr_Format(tstate, PyExc_TypeError,
4289 "calling %R should have returned an instance of "
4290 "BaseException, not %R",
4291 type, Py_TYPE(value));
4292 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004293 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004294 }
4295 else if (PyExceptionInstance_Check(exc)) {
4296 value = exc;
4297 type = PyExceptionInstance_Class(exc);
4298 Py_INCREF(type);
4299 }
4300 else {
4301 /* Not something you can raise. You get an exception
4302 anyway, just not what you specified :-) */
4303 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02004304 _PyErr_SetString(tstate, PyExc_TypeError,
4305 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004306 goto raise_error;
4307 }
Collin Winter828f04a2007-08-31 00:04:24 +00004308
Serhiy Storchakac0191582016-09-27 11:37:10 +03004309 assert(type != NULL);
4310 assert(value != NULL);
4311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004312 if (cause) {
4313 PyObject *fixed_cause;
4314 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004315 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004316 if (fixed_cause == NULL)
4317 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004318 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004319 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004320 else if (PyExceptionInstance_Check(cause)) {
4321 fixed_cause = cause;
4322 }
4323 else if (cause == Py_None) {
4324 Py_DECREF(cause);
4325 fixed_cause = NULL;
4326 }
4327 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004328 _PyErr_SetString(tstate, PyExc_TypeError,
4329 "exception causes must derive from "
4330 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004331 goto raise_error;
4332 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004333 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004334 }
Collin Winter828f04a2007-08-31 00:04:24 +00004335
Victor Stinner438a12d2019-05-24 17:01:38 +02004336 _PyErr_SetObject(tstate, type, value);
Victor Stinner61f4db82020-01-28 03:37:45 +01004337 /* _PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004338 Py_DECREF(value);
4339 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004340 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004341
4342raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004343 Py_XDECREF(value);
4344 Py_XDECREF(type);
4345 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004346 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004347}
4348
Tim Petersd6d010b2001-06-21 02:49:55 +00004349/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004350 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004351
Guido van Rossum0368b722007-05-11 16:50:42 +00004352 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4353 with a variable target.
4354*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004355
Barry Warsawe42b18f1997-08-25 22:13:04 +00004356static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004357unpack_iterable(PyThreadState *tstate, PyObject *v,
4358 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004359{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004360 int i = 0, j = 0;
4361 Py_ssize_t ll = 0;
4362 PyObject *it; /* iter(v) */
4363 PyObject *w;
4364 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004366 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004368 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004369 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004370 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004371 v->ob_type->tp_iter == NULL && !PySequence_Check(v))
4372 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004373 _PyErr_Format(tstate, PyExc_TypeError,
4374 "cannot unpack non-iterable %.200s object",
4375 v->ob_type->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004376 }
4377 return 0;
4378 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004380 for (; i < argcnt; i++) {
4381 w = PyIter_Next(it);
4382 if (w == NULL) {
4383 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004384 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04004385 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004386 _PyErr_Format(tstate, PyExc_ValueError,
4387 "not enough values to unpack "
4388 "(expected %d, got %d)",
4389 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004390 }
4391 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004392 _PyErr_Format(tstate, PyExc_ValueError,
4393 "not enough values to unpack "
4394 "(expected at least %d, got %d)",
4395 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004396 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004397 }
4398 goto Error;
4399 }
4400 *--sp = w;
4401 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004403 if (argcntafter == -1) {
4404 /* We better have exhausted the iterator now. */
4405 w = PyIter_Next(it);
4406 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004407 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004408 goto Error;
4409 Py_DECREF(it);
4410 return 1;
4411 }
4412 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02004413 _PyErr_Format(tstate, PyExc_ValueError,
4414 "too many values to unpack (expected %d)",
4415 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004416 goto Error;
4417 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004419 l = PySequence_List(it);
4420 if (l == NULL)
4421 goto Error;
4422 *--sp = l;
4423 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004425 ll = PyList_GET_SIZE(l);
4426 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004427 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04004428 "not enough values to unpack (expected at least %d, got %zd)",
4429 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004430 goto Error;
4431 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004433 /* Pop the "after-variable" args off the list. */
4434 for (j = argcntafter; j > 0; j--, i++) {
4435 *--sp = PyList_GET_ITEM(l, ll - j);
4436 }
4437 /* Resize the list. */
4438 Py_SIZE(l) = ll - argcntafter;
4439 Py_DECREF(it);
4440 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004441
Tim Petersd6d010b2001-06-21 02:49:55 +00004442Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004443 for (; i > 0; i--, sp++)
4444 Py_DECREF(*sp);
4445 Py_XDECREF(it);
4446 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004447}
4448
4449
Guido van Rossum96a42c81992-01-12 02:29:51 +00004450#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004451static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004452prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004453{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004454 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02004455 if (PyObject_Print(v, stdout, 0) != 0) {
4456 /* Don't know what else to do */
4457 _PyErr_Clear(tstate);
4458 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004459 printf("\n");
4460 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004461}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004462#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004463
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004464static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004465call_exc_trace(Py_tracefunc func, PyObject *self,
4466 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004467{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004468 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004469 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004470 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004471 if (value == NULL) {
4472 value = Py_None;
4473 Py_INCREF(value);
4474 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004475 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01004476 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004477 arg = PyTuple_Pack(3, type, value, traceback);
4478 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004479 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004480 return;
4481 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004482 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004483 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02004484 if (err == 0) {
4485 _PyErr_Restore(tstate, type, value, orig_traceback);
4486 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004487 else {
4488 Py_XDECREF(type);
4489 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004490 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004491 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004492}
4493
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004494static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004495call_trace_protected(Py_tracefunc func, PyObject *obj,
4496 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004497 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004498{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004499 PyObject *type, *value, *traceback;
4500 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004501 _PyErr_Fetch(tstate, &type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004502 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004503 if (err == 0)
4504 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004505 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004506 return 0;
4507 }
4508 else {
4509 Py_XDECREF(type);
4510 Py_XDECREF(value);
4511 Py_XDECREF(traceback);
4512 return -1;
4513 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004514}
4515
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004516static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004517call_trace(Py_tracefunc func, PyObject *obj,
4518 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004519 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004520{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004521 int result;
4522 if (tstate->tracing)
4523 return 0;
4524 tstate->tracing++;
4525 tstate->use_tracing = 0;
4526 result = func(obj, frame, what, arg);
4527 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4528 || (tstate->c_profilefunc != NULL));
4529 tstate->tracing--;
4530 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004531}
4532
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004533PyObject *
4534_PyEval_CallTracing(PyObject *func, PyObject *args)
4535{
Victor Stinner50b48572018-11-01 01:51:40 +01004536 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004537 int save_tracing = tstate->tracing;
4538 int save_use_tracing = tstate->use_tracing;
4539 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004541 tstate->tracing = 0;
4542 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4543 || (tstate->c_profilefunc != NULL));
4544 result = PyObject_Call(func, args, NULL);
4545 tstate->tracing = save_tracing;
4546 tstate->use_tracing = save_use_tracing;
4547 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004548}
4549
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004550/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004551static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004552maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004553 PyThreadState *tstate, PyFrameObject *frame,
4554 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004555{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004556 int result = 0;
4557 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004558
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004559 /* If the last instruction executed isn't in the current
4560 instruction window, reset the window.
4561 */
4562 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4563 PyAddrPair bounds;
4564 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4565 &bounds);
4566 *instr_lb = bounds.ap_lower;
4567 *instr_ub = bounds.ap_upper;
4568 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004569 /* If the last instruction falls at the start of a line or if it
4570 represents a jump backwards, update the frame's line number and
4571 then call the trace function if we're tracing source lines.
4572 */
4573 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004574 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004575 if (frame->f_trace_lines) {
4576 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4577 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004578 }
George King20faa682017-10-18 17:44:22 -07004579 /* Always emit an opcode event if we're tracing all opcodes. */
4580 if (frame->f_trace_opcodes) {
4581 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4582 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004583 *instr_prev = frame->f_lasti;
4584 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004585}
4586
Fred Drake5755ce62001-06-27 19:19:46 +00004587void
4588PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004589{
Steve Dowerb82e17e2019-05-23 08:45:22 -07004590 if (PySys_Audit("sys.setprofile", NULL) < 0) {
4591 return;
4592 }
4593
Victor Stinner50b48572018-11-01 01:51:40 +01004594 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004595 PyObject *temp = tstate->c_profileobj;
4596 Py_XINCREF(arg);
4597 tstate->c_profilefunc = NULL;
4598 tstate->c_profileobj = NULL;
4599 /* Must make sure that tracing is not ignored if 'temp' is freed */
4600 tstate->use_tracing = tstate->c_tracefunc != NULL;
4601 Py_XDECREF(temp);
4602 tstate->c_profilefunc = func;
4603 tstate->c_profileobj = arg;
4604 /* Flag that tracing or profiling is turned on */
4605 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004606}
4607
4608void
4609PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4610{
Steve Dowerb82e17e2019-05-23 08:45:22 -07004611 if (PySys_Audit("sys.settrace", NULL) < 0) {
4612 return;
4613 }
4614
Victor Stinner09532fe2019-05-10 23:39:09 +02004615 _PyRuntimeState *runtime = &_PyRuntime;
4616 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004617 PyObject *temp = tstate->c_traceobj;
Victor Stinner09532fe2019-05-10 23:39:09 +02004618 runtime->ceval.tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004619 Py_XINCREF(arg);
4620 tstate->c_tracefunc = NULL;
4621 tstate->c_traceobj = NULL;
4622 /* Must make sure that profiling is not ignored if 'temp' is freed */
4623 tstate->use_tracing = tstate->c_profilefunc != NULL;
4624 Py_XDECREF(temp);
4625 tstate->c_tracefunc = func;
4626 tstate->c_traceobj = arg;
4627 /* Flag that tracing or profiling is turned on */
4628 tstate->use_tracing = ((func != NULL)
4629 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004630}
4631
Yury Selivanov75445082015-05-11 22:57:16 -04004632void
Victor Stinner838f2642019-06-13 22:41:23 +02004633_PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004634{
4635 assert(new_depth >= 0);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004636 tstate->coroutine_origin_tracking_depth = new_depth;
4637}
4638
4639int
4640_PyEval_GetCoroutineOriginTrackingDepth(void)
4641{
Victor Stinner50b48572018-11-01 01:51:40 +01004642 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004643 return tstate->coroutine_origin_tracking_depth;
4644}
4645
4646void
Yury Selivanoveb636452016-09-08 22:01:51 -07004647_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4648{
Victor Stinner50b48572018-11-01 01:51:40 +01004649 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004650
4651 if (PySys_Audit("sys.set_asyncgen_hook_firstiter", NULL) < 0) {
4652 return;
4653 }
4654
Yury Selivanoveb636452016-09-08 22:01:51 -07004655 Py_XINCREF(firstiter);
4656 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4657}
4658
4659PyObject *
4660_PyEval_GetAsyncGenFirstiter(void)
4661{
Victor Stinner50b48572018-11-01 01:51:40 +01004662 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004663 return tstate->async_gen_firstiter;
4664}
4665
4666void
4667_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4668{
Victor Stinner50b48572018-11-01 01:51:40 +01004669 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004670
4671 if (PySys_Audit("sys.set_asyncgen_hook_finalizer", NULL) < 0) {
4672 return;
4673 }
4674
Yury Selivanoveb636452016-09-08 22:01:51 -07004675 Py_XINCREF(finalizer);
4676 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4677}
4678
4679PyObject *
4680_PyEval_GetAsyncGenFinalizer(void)
4681{
Victor Stinner50b48572018-11-01 01:51:40 +01004682 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004683 return tstate->async_gen_finalizer;
4684}
4685
Victor Stinner438a12d2019-05-24 17:01:38 +02004686static PyFrameObject *
4687_PyEval_GetFrame(PyThreadState *tstate)
4688{
Victor Stinner01b1cc12019-11-20 02:27:56 +01004689 _PyRuntimeState *runtime = tstate->interp->runtime;
4690 return runtime->gilstate.getframe(tstate);
Victor Stinner438a12d2019-05-24 17:01:38 +02004691}
4692
4693PyFrameObject *
4694PyEval_GetFrame(void)
4695{
4696 PyThreadState *tstate = _PyThreadState_GET();
4697 return _PyEval_GetFrame(tstate);
4698}
4699
Guido van Rossumb209a111997-04-29 18:18:01 +00004700PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004701PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004702{
Victor Stinner438a12d2019-05-24 17:01:38 +02004703 PyThreadState *tstate = _PyThreadState_GET();
4704 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004705 if (current_frame == NULL)
Victor Stinner438a12d2019-05-24 17:01:38 +02004706 return tstate->interp->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004707 else
4708 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004709}
4710
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004711/* Convenience function to get a builtin from its name */
4712PyObject *
4713_PyEval_GetBuiltinId(_Py_Identifier *name)
4714{
Victor Stinner438a12d2019-05-24 17:01:38 +02004715 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004716 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4717 if (attr) {
4718 Py_INCREF(attr);
4719 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004720 else if (!_PyErr_Occurred(tstate)) {
4721 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004722 }
4723 return attr;
4724}
4725
Guido van Rossumb209a111997-04-29 18:18:01 +00004726PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004727PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004728{
Victor Stinner438a12d2019-05-24 17:01:38 +02004729 PyThreadState *tstate = _PyThreadState_GET();
4730 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Victor Stinner41bb43a2013-10-29 01:19:37 +01004731 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004732 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004733 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004734 }
4735
Victor Stinner438a12d2019-05-24 17:01:38 +02004736 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01004737 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004738 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004739
4740 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004741 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004742}
4743
Guido van Rossumb209a111997-04-29 18:18:01 +00004744PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004745PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004746{
Victor Stinner438a12d2019-05-24 17:01:38 +02004747 PyThreadState *tstate = _PyThreadState_GET();
4748 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
4749 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004750 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004751 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004752
4753 assert(current_frame->f_globals != NULL);
4754 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004755}
4756
Guido van Rossum6135a871995-01-09 17:53:26 +00004757int
Tim Peters5ba58662001-07-16 02:29:45 +00004758PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004759{
Victor Stinner438a12d2019-05-24 17:01:38 +02004760 PyThreadState *tstate = _PyThreadState_GET();
4761 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004762 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004764 if (current_frame != NULL) {
4765 const int codeflags = current_frame->f_code->co_flags;
4766 const int compilerflags = codeflags & PyCF_MASK;
4767 if (compilerflags) {
4768 result = 1;
4769 cf->cf_flags |= compilerflags;
4770 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004771#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004772 if (codeflags & CO_GENERATOR_ALLOWED) {
4773 result = 1;
4774 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4775 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004776#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004777 }
4778 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004779}
4780
Guido van Rossum3f5da241990-12-20 15:06:42 +00004781
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004782const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004783PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004784{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004785 if (PyMethod_Check(func))
4786 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4787 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004788 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004789 else if (PyCFunction_Check(func))
4790 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4791 else
4792 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004793}
4794
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004795const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004796PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004797{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004798 if (PyMethod_Check(func))
4799 return "()";
4800 else if (PyFunction_Check(func))
4801 return "()";
4802 else if (PyCFunction_Check(func))
4803 return "()";
4804 else
4805 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004806}
4807
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004808#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004809if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004810 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4811 tstate, tstate->frame, \
4812 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004813 x = NULL; \
4814 } \
4815 else { \
4816 x = call; \
4817 if (tstate->c_profilefunc != NULL) { \
4818 if (x == NULL) { \
4819 call_trace_protected(tstate->c_profilefunc, \
4820 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004821 tstate, tstate->frame, \
4822 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004823 /* XXX should pass (type, value, tb) */ \
4824 } else { \
4825 if (call_trace(tstate->c_profilefunc, \
4826 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004827 tstate, tstate->frame, \
4828 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004829 Py_DECREF(x); \
4830 x = NULL; \
4831 } \
4832 } \
4833 } \
4834 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004835} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004836 x = call; \
4837 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004838
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004839
4840static PyObject *
4841trace_call_function(PyThreadState *tstate,
4842 PyObject *func,
4843 PyObject **args, Py_ssize_t nargs,
4844 PyObject *kwnames)
4845{
4846 PyObject *x;
4847 if (PyCFunction_Check(func)) {
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02004848 C_TRACE(x, _PyObject_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004849 return x;
4850 }
4851 else if (Py_TYPE(func) == &PyMethodDescr_Type && nargs > 0) {
4852 /* We need to create a temporary bound method as argument
4853 for profiling.
4854
4855 If nargs == 0, then this cannot work because we have no
4856 "self". In any case, the call itself would raise
4857 TypeError (foo needs an argument), so we just skip
4858 profiling. */
4859 PyObject *self = args[0];
4860 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4861 if (func == NULL) {
4862 return NULL;
4863 }
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02004864 C_TRACE(x, _PyObject_Vectorcall(func,
4865 args+1, nargs-1,
4866 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004867 Py_DECREF(func);
4868 return x;
4869 }
4870 return _PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
4871}
4872
Victor Stinner415c5102017-01-11 00:54:57 +01004873/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4874 to reduce the stack consumption. */
4875Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Victor Stinner09532fe2019-05-10 23:39:09 +02004876call_function(PyThreadState *tstate, PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004877{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004878 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004879 PyObject *func = *pfunc;
4880 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004881 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4882 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004883 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004884
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004885 if (tstate->use_tracing) {
4886 x = trace_call_function(tstate, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09004887 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004888 else {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004889 x = _PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004890 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004891
Victor Stinner438a12d2019-05-24 17:01:38 +02004892 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004893
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004894 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004895 while ((*pp_stack) > pfunc) {
4896 w = EXT_POP(*pp_stack);
4897 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004898 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004899
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004900 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004901}
4902
Jeremy Hylton52820442001-01-03 23:52:36 +00004903static PyObject *
Victor Stinner09532fe2019-05-10 23:39:09 +02004904do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004905{
jdemeyere89de732018-09-19 12:06:20 +02004906 PyObject *result;
4907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004908 if (PyCFunction_Check(func)) {
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +02004909 C_TRACE(result, PyObject_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004910 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004911 }
jdemeyere89de732018-09-19 12:06:20 +02004912 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
jdemeyere89de732018-09-19 12:06:20 +02004913 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
4914 if (nargs > 0 && tstate->use_tracing) {
4915 /* We need to create a temporary bound method as argument
4916 for profiling.
4917
4918 If nargs == 0, then this cannot work because we have no
4919 "self". In any case, the call itself would raise
4920 TypeError (foo needs an argument), so we just skip
4921 profiling. */
4922 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
4923 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4924 if (func == NULL) {
4925 return NULL;
4926 }
4927
Victor Stinner4d231bc2019-11-14 13:36:21 +01004928 C_TRACE(result, _PyObject_FastCallDictTstate(
4929 tstate, func,
4930 &_PyTuple_ITEMS(callargs)[1],
4931 nargs - 1,
4932 kwdict));
jdemeyere89de732018-09-19 12:06:20 +02004933 Py_DECREF(func);
4934 return result;
4935 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004936 }
jdemeyere89de732018-09-19 12:06:20 +02004937 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00004938}
4939
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004940/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004941 nb_index slot defined, and store in *pi.
4942 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004943 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004944 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004945*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004946int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004947_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004948{
Victor Stinner438a12d2019-05-24 17:01:38 +02004949 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004950 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004951 Py_ssize_t x;
4952 if (PyIndex_Check(v)) {
4953 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02004954 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004955 return 0;
4956 }
4957 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004958 _PyErr_SetString(tstate, PyExc_TypeError,
4959 "slice indices must be integers or "
4960 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004961 return 0;
4962 }
4963 *pi = x;
4964 }
4965 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004966}
4967
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004968int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004969_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004970{
Victor Stinner438a12d2019-05-24 17:01:38 +02004971 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004972 Py_ssize_t x;
4973 if (PyIndex_Check(v)) {
4974 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02004975 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004976 return 0;
4977 }
4978 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004979 _PyErr_SetString(tstate, PyExc_TypeError,
4980 "slice indices must be integers or "
4981 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004982 return 0;
4983 }
4984 *pi = x;
4985 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004986}
4987
Thomas Wouters52152252000-08-17 22:55:00 +00004988static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004989import_name(PyThreadState *tstate, PyFrameObject *f,
4990 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004991{
4992 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004993 PyObject *import_func, *res;
4994 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004995
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004996 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004997 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004998 if (!_PyErr_Occurred(tstate)) {
4999 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005000 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005001 return NULL;
5002 }
5003
5004 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005005 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005006 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02005007 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005008 return NULL;
5009 }
5010 res = PyImport_ImportModuleLevelObject(
5011 name,
5012 f->f_globals,
5013 f->f_locals == NULL ? Py_None : f->f_locals,
5014 fromlist,
5015 ilevel);
5016 return res;
5017 }
5018
5019 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005020
5021 stack[0] = name;
5022 stack[1] = f->f_globals;
5023 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5024 stack[3] = fromlist;
5025 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005026 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005027 Py_DECREF(import_func);
5028 return res;
5029}
5030
5031static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005032import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005033{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005034 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02005035 _Py_IDENTIFIER(__name__);
Xiang Zhang4830f582017-03-21 11:13:42 +08005036 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005037
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005038 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005039 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005040 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005041 /* Issue #17636: in case this failed because of a circular relative
5042 import, try to fallback on reading the module directly from
5043 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02005044 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005045 if (pkgname == NULL) {
5046 goto error;
5047 }
Oren Milman6db70332017-09-19 14:23:01 +03005048 if (!PyUnicode_Check(pkgname)) {
5049 Py_CLEAR(pkgname);
5050 goto error;
5051 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005052 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005053 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005054 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005055 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005056 }
Eric Snow3f9eee62017-09-15 16:35:20 -06005057 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005058 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02005059 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07005060 goto error;
5061 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005062 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005063 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005064 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005065 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005066 if (pkgname == NULL) {
5067 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5068 if (pkgname_or_unknown == NULL) {
5069 Py_XDECREF(pkgpath);
5070 return NULL;
5071 }
5072 } else {
5073 pkgname_or_unknown = pkgname;
5074 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005075
5076 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005077 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08005078 errmsg = PyUnicode_FromFormat(
5079 "cannot import name %R from %R (unknown location)",
5080 name, pkgname_or_unknown
5081 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005082 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005083 PyErr_SetImportError(errmsg, pkgname, NULL);
5084 }
5085 else {
Anthony Sottile65366bc2019-09-09 08:17:50 -07005086 _Py_IDENTIFIER(__spec__);
5087 PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
Anthony Sottile65366bc2019-09-09 08:17:50 -07005088 const char *fmt =
5089 _PyModuleSpec_IsInitializing(spec) ?
5090 "cannot import name %R from partially initialized module %R "
5091 "(most likely due to a circular import) (%S)" :
5092 "cannot import name %R from %R (%S)";
5093 Py_XDECREF(spec);
5094
5095 errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
Stefan Krah027b09c2019-03-25 21:50:58 +01005096 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005097 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005098 }
5099
Xiang Zhang4830f582017-03-21 11:13:42 +08005100 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005101 Py_XDECREF(pkgname_or_unknown);
5102 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005103 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005104}
Guido van Rossumac7be682001-01-17 15:42:30 +00005105
Thomas Wouters52152252000-08-17 22:55:00 +00005106static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005107import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00005108{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005109 _Py_IDENTIFIER(__all__);
5110 _Py_IDENTIFIER(__dict__);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005111 _Py_IDENTIFIER(__name__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005112 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005113 int skip_leading_underscores = 0;
5114 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005115
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005116 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5117 return -1; /* Unexpected error */
5118 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005119 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005120 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5121 return -1;
5122 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005123 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005124 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005125 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005126 return -1;
5127 }
5128 all = PyMapping_Keys(dict);
5129 Py_DECREF(dict);
5130 if (all == NULL)
5131 return -1;
5132 skip_leading_underscores = 1;
5133 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005134
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005135 for (pos = 0, err = 0; ; pos++) {
5136 name = PySequence_GetItem(all, pos);
5137 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005138 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005139 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02005140 }
5141 else {
5142 _PyErr_Clear(tstate);
5143 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005144 break;
5145 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005146 if (!PyUnicode_Check(name)) {
5147 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5148 if (modname == NULL) {
5149 Py_DECREF(name);
5150 err = -1;
5151 break;
5152 }
5153 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005154 _PyErr_Format(tstate, PyExc_TypeError,
5155 "module __name__ must be a string, not %.100s",
5156 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005157 }
5158 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005159 _PyErr_Format(tstate, PyExc_TypeError,
5160 "%s in %U.%s must be str, not %.100s",
5161 skip_leading_underscores ? "Key" : "Item",
5162 modname,
5163 skip_leading_underscores ? "__dict__" : "__all__",
5164 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005165 }
5166 Py_DECREF(modname);
5167 Py_DECREF(name);
5168 err = -1;
5169 break;
5170 }
5171 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005172 if (PyUnicode_READY(name) == -1) {
5173 Py_DECREF(name);
5174 err = -1;
5175 break;
5176 }
5177 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5178 Py_DECREF(name);
5179 continue;
5180 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005181 }
5182 value = PyObject_GetAttr(v, name);
5183 if (value == NULL)
5184 err = -1;
5185 else if (PyDict_CheckExact(locals))
5186 err = PyDict_SetItem(locals, name, value);
5187 else
5188 err = PyObject_SetItem(locals, name, value);
5189 Py_DECREF(name);
5190 Py_XDECREF(value);
5191 if (err != 0)
5192 break;
5193 }
5194 Py_DECREF(all);
5195 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005196}
5197
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005198static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005199check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005200{
5201 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005202 /* check_args_iterable() may be called with a live exception:
5203 * clear it to prevent calling _PyObject_FunctionStr() with an
5204 * exception set. */
Victor Stinner61f4db82020-01-28 03:37:45 +01005205 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005206 PyObject *funcstr = _PyObject_FunctionStr(func);
5207 if (funcstr != NULL) {
5208 _PyErr_Format(tstate, PyExc_TypeError,
5209 "%U argument after * must be an iterable, not %.200s",
5210 funcstr, Py_TYPE(args)->tp_name);
5211 Py_DECREF(funcstr);
5212 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005213 return -1;
5214 }
5215 return 0;
5216}
5217
5218static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005219format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005220{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005221 /* _PyDict_MergeEx raises attribute
5222 * error (percolated from an attempt
5223 * to get 'keys' attribute) instead of
5224 * a type error if its second argument
5225 * is not a mapping.
5226 */
Victor Stinner438a12d2019-05-24 17:01:38 +02005227 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005228 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005229 PyObject *funcstr = _PyObject_FunctionStr(func);
5230 if (funcstr != NULL) {
5231 _PyErr_Format(
5232 tstate, PyExc_TypeError,
5233 "%U argument after ** must be a mapping, not %.200s",
5234 funcstr, Py_TYPE(kwargs)->tp_name);
5235 Py_DECREF(funcstr);
5236 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005237 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005238 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005239 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02005240 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005241 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005242 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005243 PyObject *funcstr = _PyObject_FunctionStr(func);
5244 if (funcstr != NULL) {
5245 PyObject *key = PyTuple_GET_ITEM(val, 0);
5246 _PyErr_Format(
5247 tstate, PyExc_TypeError,
5248 "%U got multiple values for keyword argument '%S'",
5249 funcstr, key);
5250 Py_DECREF(funcstr);
5251 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005252 Py_XDECREF(exc);
5253 Py_XDECREF(val);
5254 Py_XDECREF(tb);
5255 }
5256 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005257 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005258 }
5259 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005260}
5261
Guido van Rossumac7be682001-01-17 15:42:30 +00005262static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005263format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
5264 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005265{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005266 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005268 if (!obj)
5269 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005270
Serhiy Storchaka06515832016-11-20 09:13:07 +02005271 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005272 if (!obj_str)
5273 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005274
Victor Stinner438a12d2019-05-24 17:01:38 +02005275 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005276}
Guido van Rossum950361c1997-01-24 13:49:28 +00005277
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005278static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005279format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005280{
5281 PyObject *name;
5282 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02005283 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005284 return;
5285 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5286 name = PyTuple_GET_ITEM(co->co_cellvars,
5287 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005288 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005289 PyExc_UnboundLocalError,
5290 UNBOUNDLOCAL_ERROR_MSG,
5291 name);
5292 } else {
5293 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5294 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02005295 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005296 UNBOUNDFREE_ERROR_MSG, name);
5297 }
5298}
5299
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005300static void
Mark Shannonfee55262019-11-21 09:11:43 +00005301format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevprevopcode, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005302{
5303 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5304 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005305 _PyErr_Format(tstate, PyExc_TypeError,
5306 "'async with' received an object from __aenter__ "
5307 "that does not implement __await__: %.100s",
5308 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005309 }
Mark Shannonfee55262019-11-21 09:11:43 +00005310 else if (prevopcode == WITH_EXCEPT_START || (prevopcode == CALL_FUNCTION && prevprevopcode == DUP_TOP)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005311 _PyErr_Format(tstate, PyExc_TypeError,
5312 "'async with' received an object from __aexit__ "
5313 "that does not implement __await__: %.100s",
5314 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005315 }
5316 }
5317}
5318
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005319static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005320unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005321 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005322{
5323 PyObject *res;
5324 if (Py_REFCNT(v) == 2) {
5325 /* In the common case, there are 2 references to the value
5326 * stored in 'variable' when the += is performed: one on the
5327 * value stack (in 'v') and one still stored in the
5328 * 'variable'. We try to delete the variable now to reduce
5329 * the refcnt to 1.
5330 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005331 int opcode, oparg;
5332 NEXTOPARG();
5333 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005334 case STORE_FAST:
5335 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005336 PyObject **fastlocals = f->f_localsplus;
5337 if (GETLOCAL(oparg) == v)
5338 SETLOCAL(oparg, NULL);
5339 break;
5340 }
5341 case STORE_DEREF:
5342 {
5343 PyObject **freevars = (f->f_localsplus +
5344 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005345 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005346 if (PyCell_GET(c) == v) {
5347 PyCell_SET(c, NULL);
5348 Py_DECREF(v);
5349 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005350 break;
5351 }
5352 case STORE_NAME:
5353 {
5354 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005355 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005356 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005357 if (locals && PyDict_CheckExact(locals)) {
5358 PyObject *w = PyDict_GetItemWithError(locals, name);
5359 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02005360 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005361 {
5362 Py_DECREF(v);
5363 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005364 }
5365 }
5366 break;
5367 }
5368 }
5369 }
5370 res = v;
5371 PyUnicode_Append(&res, w);
5372 return res;
5373}
5374
Guido van Rossum950361c1997-01-24 13:49:28 +00005375#ifdef DYNAMIC_EXECUTION_PROFILE
5376
Skip Montanarof118cb12001-10-15 20:51:38 +00005377static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005378getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005379{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005380 int i;
5381 PyObject *l = PyList_New(256);
5382 if (l == NULL) return NULL;
5383 for (i = 0; i < 256; i++) {
5384 PyObject *x = PyLong_FromLong(a[i]);
5385 if (x == NULL) {
5386 Py_DECREF(l);
5387 return NULL;
5388 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005389 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005390 }
5391 for (i = 0; i < 256; i++)
5392 a[i] = 0;
5393 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005394}
5395
5396PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005397_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005398{
5399#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005400 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005401#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005402 int i;
5403 PyObject *l = PyList_New(257);
5404 if (l == NULL) return NULL;
5405 for (i = 0; i < 257; i++) {
5406 PyObject *x = getarray(dxpairs[i]);
5407 if (x == NULL) {
5408 Py_DECREF(l);
5409 return NULL;
5410 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005411 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005412 }
5413 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005414#endif
5415}
5416
5417#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005418
5419Py_ssize_t
5420_PyEval_RequestCodeExtraIndex(freefunc free)
5421{
Victor Stinnercaba55b2018-08-03 15:33:52 +02005422 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -07005423 Py_ssize_t new_index;
5424
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005425 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005426 return -1;
5427 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005428 new_index = interp->co_extra_user_count++;
5429 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005430 return new_index;
5431}
Łukasz Langaa785c872016-09-09 17:37:37 -07005432
5433static void
5434dtrace_function_entry(PyFrameObject *f)
5435{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005436 const char *filename;
5437 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005438 int lineno;
5439
5440 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5441 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5442 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5443
David Carlieraabdeb72020-01-28 12:53:32 +00005444 PyDTrace_FUNCTION_ENTRY((char *)filename, (char *)funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005445}
5446
5447static void
5448dtrace_function_return(PyFrameObject *f)
5449{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005450 const char *filename;
5451 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005452 int lineno;
5453
5454 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5455 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5456 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5457
David Carlieraabdeb72020-01-28 12:53:32 +00005458 PyDTrace_FUNCTION_RETURN((char *)filename, (char *)funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005459}
5460
5461/* DTrace equivalent of maybe_call_line_trace. */
5462static void
5463maybe_dtrace_line(PyFrameObject *frame,
5464 int *instr_lb, int *instr_ub, int *instr_prev)
5465{
5466 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005467 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005468
5469 /* If the last instruction executed isn't in the current
5470 instruction window, reset the window.
5471 */
5472 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5473 PyAddrPair bounds;
5474 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5475 &bounds);
5476 *instr_lb = bounds.ap_lower;
5477 *instr_ub = bounds.ap_upper;
5478 }
5479 /* If the last instruction falls at the start of a line or if
5480 it represents a jump backwards, update the frame's line
5481 number and call the trace function. */
5482 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5483 frame->f_lineno = line;
5484 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5485 if (!co_filename)
5486 co_filename = "?";
5487 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5488 if (!co_name)
5489 co_name = "?";
David Carlieraabdeb72020-01-28 12:53:32 +00005490 PyDTrace_LINE((char *)co_filename, (char *)co_name, line);
Łukasz Langaa785c872016-09-09 17:37:37 -07005491 }
5492 *instr_prev = frame->f_lasti;
5493}
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005494
5495
5496/* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions
5497 for the limited API. */
5498
5499#undef Py_EnterRecursiveCall
5500
5501int Py_EnterRecursiveCall(const char *where)
5502{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005503 return _Py_EnterRecursiveCall_inline(where);
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005504}
5505
5506#undef Py_LeaveRecursiveCall
5507
5508void Py_LeaveRecursiveCall(void)
5509{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005510 _Py_LeaveRecursiveCall_inline();
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005511}