blob: 2c0a23dfdd2917dba69eeb8e571f95f1788174c6 [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 Stinner09532fe2019-05-10 23:39:09 +0200305 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200307 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200308
Victor Stinner01b1cc12019-11-20 02:27:56 +0100309 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200310 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinner09532fe2019-05-10 23:39:09 +0200311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 /* Check someone has called PyEval_InitThreads() to create the lock */
Victor Stinnere225beb2019-06-03 18:14:24 +0200313 assert(gil_created(&ceval->gil));
314 take_gil(ceval, tstate);
Victor Stinner01b1cc12019-11-20 02:27:56 +0100315 exit_thread_if_finalizing(tstate);
Victor Stinner09532fe2019-05-10 23:39:09 +0200316 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
317 Py_FatalError("PyEval_AcquireThread: non-NULL old thread state");
318 }
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000319}
320
321void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000322PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000323{
Victor Stinner09532fe2019-05-10 23:39:09 +0200324 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200326 }
327
Victor Stinner01b1cc12019-11-20 02:27:56 +0100328 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200329 PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
330 if (new_tstate != tstate) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200332 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200333 drop_gil(&runtime->ceval, tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000334}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000335
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200336/* This function is called from PyOS_AfterFork_Child to destroy all threads
337 * which are not running in the child process, and clear internal locks
338 * which might be held by those threads.
339 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000340
341void
Victor Stinnerd5d9e812019-05-13 12:35:37 +0200342_PyEval_ReInitThreads(_PyRuntimeState *runtime)
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000343{
Victor Stinnere225beb2019-06-03 18:14:24 +0200344 struct _ceval_runtime_state *ceval = &runtime->ceval;
345 if (!gil_created(&ceval->gil)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 return;
Victor Stinner09532fe2019-05-10 23:39:09 +0200347 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200348 recreate_gil(&ceval->gil);
Victor Stinner09532fe2019-05-10 23:39:09 +0200349 PyThreadState *current_tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinnere225beb2019-06-03 18:14:24 +0200350 take_gil(ceval, current_tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700351
Victor Stinnere225beb2019-06-03 18:14:24 +0200352 struct _pending_calls *pending = &ceval->pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200353 pending->lock = PyThread_allocate_lock();
354 if (pending->lock == NULL) {
Eric Snow8479a342019-03-08 23:44:33 -0700355 Py_FatalError("Can't initialize threads for pending calls");
356 }
Jesse Nollera8513972008-07-17 16:49:17 +0000357
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200358 /* Destroy all threads except the current one */
Victor Stinner0fd2c302019-06-04 03:15:09 +0200359 _PyThreadState_DeleteExcept(runtime, current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000360}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000361
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000362/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600363 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000364
365void
Victor Stinnere225beb2019-06-03 18:14:24 +0200366_PyEval_SignalAsyncExc(struct _ceval_runtime_state *ceval)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000367{
Victor Stinnere225beb2019-06-03 18:14:24 +0200368 SIGNAL_ASYNC_EXC(ceval);
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000369}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000370
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000371PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000372PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000373{
Victor Stinner09532fe2019-05-10 23:39:09 +0200374 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200375 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinner09532fe2019-05-10 23:39:09 +0200376 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
377 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 Py_FatalError("PyEval_SaveThread: NULL tstate");
Victor Stinner09532fe2019-05-10 23:39:09 +0200379 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200380 assert(gil_created(&ceval->gil));
381 drop_gil(ceval, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000383}
384
385void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000386PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000387{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100388 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner0fd2c302019-06-04 03:15:09 +0200389 struct _ceval_runtime_state *ceval = &runtime->ceval;
390
Victor Stinner09532fe2019-05-10 23:39:09 +0200391 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Victor Stinner09532fe2019-05-10 23:39:09 +0200393 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200394 assert(gil_created(&ceval->gil));
Victor Stinner2914bb32018-01-29 11:57:45 +0100395
396 int err = errno;
Victor Stinnere225beb2019-06-03 18:14:24 +0200397 take_gil(ceval, tstate);
Victor Stinner01b1cc12019-11-20 02:27:56 +0100398 exit_thread_if_finalizing(tstate);
Victor Stinner2914bb32018-01-29 11:57:45 +0100399 errno = err;
400
Victor Stinner09532fe2019-05-10 23:39:09 +0200401 _PyThreadState_Swap(&runtime->gilstate, tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000402}
403
404
Guido van Rossuma9672091994-09-14 13:31:22 +0000405/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
406 signal handlers or Mac I/O completion routines) can schedule calls
407 to a function to be called synchronously.
408 The synchronous function is called with one void* argument.
409 It should return 0 for success or -1 for failure -- failure should
410 be accompanied by an exception.
411
412 If registry succeeds, the registry function returns 0; if it fails
413 (e.g. due to too many pending calls) it returns -1 (without setting
414 an exception condition).
415
416 Note that because registry may occur from within signal handlers,
417 or other asynchronous events, calling malloc() is unsafe!
418
Guido van Rossuma9672091994-09-14 13:31:22 +0000419 Any thread can schedule pending calls, but only the main thread
420 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000421 There is no facility to schedule calls to a particular thread, but
422 that should be easy to change, should that ever be required. In
423 that case, the static variables here should go into the python
424 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000425*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000426
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200427void
Victor Stinnere225beb2019-06-03 18:14:24 +0200428_PyEval_SignalReceived(struct _ceval_runtime_state *ceval)
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200429{
430 /* bpo-30703: Function called when the C signal handler of Python gets a
431 signal. We cannot queue a callback using Py_AddPendingCall() since
432 that function is not async-signal-safe. */
Victor Stinnere225beb2019-06-03 18:14:24 +0200433 SIGNAL_PENDING_SIGNALS(ceval);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200434}
435
Eric Snow5be45a62019-03-08 22:47:07 -0700436/* Push one item onto the queue while holding the lock. */
437static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200438_push_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600439 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700440{
Eric Snow842a2f02019-03-15 15:47:51 -0600441 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700442 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600443 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700444 return -1; /* Queue full */
445 }
Eric Snow842a2f02019-03-15 15:47:51 -0600446 pending->calls[i].func = func;
447 pending->calls[i].arg = arg;
448 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700449 return 0;
450}
451
452/* Pop one item off the queue while holding the lock. */
453static void
Victor Stinnere225beb2019-06-03 18:14:24 +0200454_pop_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600455 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700456{
Eric Snow842a2f02019-03-15 15:47:51 -0600457 int i = pending->first;
458 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700459 return; /* Queue empty */
460 }
461
Eric Snow842a2f02019-03-15 15:47:51 -0600462 *func = pending->calls[i].func;
463 *arg = pending->calls[i].arg;
464 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700465}
466
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200467/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000468 scheduling to be made from any thread, and even from an executing
469 callback.
470 */
471
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000472int
Victor Stinner438a12d2019-05-24 17:01:38 +0200473_PyEval_AddPendingCall(PyThreadState *tstate,
Victor Stinnere225beb2019-06-03 18:14:24 +0200474 struct _ceval_runtime_state *ceval,
Victor Stinner09532fe2019-05-10 23:39:09 +0200475 int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000476{
Victor Stinnere225beb2019-06-03 18:14:24 +0200477 struct _pending_calls *pending = &ceval->pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600478
479 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
480 if (pending->finishing) {
481 PyThread_release_lock(pending->lock);
482
483 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +0200484 _PyErr_Fetch(tstate, &exc, &val, &tb);
485 _PyErr_SetString(tstate, PyExc_SystemError,
Eric Snow842a2f02019-03-15 15:47:51 -0600486 "Py_AddPendingCall: cannot add pending calls "
487 "(Python shutting down)");
Victor Stinner438a12d2019-05-24 17:01:38 +0200488 _PyErr_Print(tstate);
489 _PyErr_Restore(tstate, exc, val, tb);
Eric Snow842a2f02019-03-15 15:47:51 -0600490 return -1;
491 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200492 int result = _push_pending_call(pending, func, arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600493 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700494
Victor Stinnere225beb2019-06-03 18:14:24 +0200495 /* signal main loop */
496 SIGNAL_PENDING_CALLS(ceval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000498}
499
Victor Stinner09532fe2019-05-10 23:39:09 +0200500int
501Py_AddPendingCall(int (*func)(void *), void *arg)
502{
Victor Stinner438a12d2019-05-24 17:01:38 +0200503 _PyRuntimeState *runtime = &_PyRuntime;
504 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinnere225beb2019-06-03 18:14:24 +0200505 return _PyEval_AddPendingCall(tstate, &runtime->ceval, func, arg);
Victor Stinner09532fe2019-05-10 23:39:09 +0200506}
507
Eric Snowfdf282d2019-01-11 14:26:55 -0700508static int
Victor Stinner09532fe2019-05-10 23:39:09 +0200509handle_signals(_PyRuntimeState *runtime)
Eric Snowfdf282d2019-01-11 14:26:55 -0700510{
Eric Snow5be45a62019-03-08 22:47:07 -0700511 /* Only handle signals on main thread. PyEval_InitThreads must
512 * have been called already.
513 */
Victor Stinner09532fe2019-05-10 23:39:09 +0200514 if (PyThread_get_thread_ident() != runtime->main_thread) {
Eric Snowfdf282d2019-01-11 14:26:55 -0700515 return 0;
516 }
Eric Snow64d6cc82019-02-23 15:40:43 -0700517 /*
518 * Ensure that the thread isn't currently running some other
519 * interpreter.
520 */
Victor Stinner09532fe2019-05-10 23:39:09 +0200521 PyInterpreterState *interp = _PyRuntimeState_GetThreadState(runtime)->interp;
522 if (interp != runtime->interpreters.main) {
Eric Snow64d6cc82019-02-23 15:40:43 -0700523 return 0;
524 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700525
Victor Stinnere225beb2019-06-03 18:14:24 +0200526 struct _ceval_runtime_state *ceval = &runtime->ceval;
527 UNSIGNAL_PENDING_SIGNALS(ceval);
Eric Snow64d6cc82019-02-23 15:40:43 -0700528 if (_PyErr_CheckSignals() < 0) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200529 SIGNAL_PENDING_SIGNALS(ceval); /* We're not done yet */
Eric Snowfdf282d2019-01-11 14:26:55 -0700530 return -1;
531 }
532 return 0;
533}
534
535static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200536make_pending_calls(_PyRuntimeState *runtime)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000537{
Eric Snow6a150bc2019-06-01 15:39:46 -0600538 static int busy = 0;
Eric Snowb75b1a352019-04-12 10:20:10 -0600539
Victor Stinnere225beb2019-06-03 18:14:24 +0200540 /* only service pending calls on main thread */
541 if (PyThread_get_thread_ident() != runtime->main_thread) {
542 return 0;
543 }
544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 /* don't perform recursive pending calls */
Eric Snowfdf282d2019-01-11 14:26:55 -0700546 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700548 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200549 busy = 1;
Victor Stinnere225beb2019-06-03 18:14:24 +0200550 struct _ceval_runtime_state *ceval = &runtime->ceval;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200551 /* unsignal before starting to call callbacks, so that any callback
552 added in-between re-signals */
Victor Stinnere225beb2019-06-03 18:14:24 +0200553 UNSIGNAL_PENDING_CALLS(ceval);
Eric Snowfdf282d2019-01-11 14:26:55 -0700554 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 /* perform a bounded number of calls, in case of recursion */
Victor Stinnere225beb2019-06-03 18:14:24 +0200557 struct _pending_calls *pending = &ceval->pending;
Eric Snowfdf282d2019-01-11 14:26:55 -0700558 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700559 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 void *arg = NULL;
561
562 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600563 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200564 _pop_pending_call(pending, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600565 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700566
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100567 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700568 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100569 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700570 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700571 res = func(arg);
572 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200573 goto error;
574 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200576
Charles-François Natalif23339a2011-07-23 18:15:43 +0200577 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700578 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200579
580error:
581 busy = 0;
Victor Stinnere225beb2019-06-03 18:14:24 +0200582 SIGNAL_PENDING_CALLS(ceval);
Eric Snowfdf282d2019-01-11 14:26:55 -0700583 return res;
584}
585
Eric Snow842a2f02019-03-15 15:47:51 -0600586void
Victor Stinner2b1df452020-01-13 18:46:59 +0100587_Py_FinishPendingCalls(PyThreadState *tstate)
Eric Snow842a2f02019-03-15 15:47:51 -0600588{
Eric Snow842a2f02019-03-15 15:47:51 -0600589 assert(PyGILState_Check());
590
Victor Stinner2b1df452020-01-13 18:46:59 +0100591 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200592 struct _pending_calls *pending = &runtime->ceval.pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200593
Eric Snow842a2f02019-03-15 15:47:51 -0600594 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
595 pending->finishing = 1;
596 PyThread_release_lock(pending->lock);
597
598 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
599 return;
600 }
601
Victor Stinnere225beb2019-06-03 18:14:24 +0200602 if (make_pending_calls(runtime) < 0) {
603 PyObject *exc, *val, *tb;
604 _PyErr_Fetch(tstate, &exc, &val, &tb);
605 PyErr_BadInternalCall();
606 _PyErr_ChainExceptions(exc, val, tb);
607 _PyErr_Print(tstate);
Eric Snow842a2f02019-03-15 15:47:51 -0600608 }
609}
610
Eric Snowfdf282d2019-01-11 14:26:55 -0700611/* Py_MakePendingCalls() is a simple wrapper for the sake
612 of backward-compatibility. */
613int
614Py_MakePendingCalls(void)
615{
616 assert(PyGILState_Check());
617
618 /* Python signal handler doesn't really queue a callback: it only signals
619 that a signal was received, see _PyEval_SignalReceived(). */
Victor Stinner09532fe2019-05-10 23:39:09 +0200620 _PyRuntimeState *runtime = &_PyRuntime;
621 int res = handle_signals(runtime);
Eric Snowfdf282d2019-01-11 14:26:55 -0700622 if (res != 0) {
623 return res;
624 }
625
Victor Stinnere225beb2019-06-03 18:14:24 +0200626 res = make_pending_calls(runtime);
Eric Snowb75b1a352019-04-12 10:20:10 -0600627 if (res != 0) {
628 return res;
629 }
630
631 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000632}
633
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000634/* The interpreter's recursion limit */
635
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000636#ifndef Py_DEFAULT_RECURSION_LIMIT
637#define Py_DEFAULT_RECURSION_LIMIT 1000
638#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600639
Eric Snow05351c12017-09-05 21:43:08 -0700640int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000641
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600642void
Victor Stinnere225beb2019-06-03 18:14:24 +0200643_PyEval_Initialize(struct _ceval_runtime_state *state)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600644{
Victor Stinnere225beb2019-06-03 18:14:24 +0200645 state->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600646 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Victor Stinnere225beb2019-06-03 18:14:24 +0200647 _gil_initialize(&state->gil);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600648}
649
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000650int
651Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000652{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100653 struct _ceval_runtime_state *ceval = &_PyRuntime.ceval;
654 return ceval->recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000655}
656
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000657void
658Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000659{
Victor Stinnere225beb2019-06-03 18:14:24 +0200660 struct _ceval_runtime_state *ceval = &_PyRuntime.ceval;
661 ceval->recursion_limit = new_limit;
662 _Py_CheckRecursionLimit = ceval->recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000663}
664
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100665/* The function _Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
Armin Rigo2b3eb402003-10-28 12:05:48 +0000666 if the recursion_depth reaches _Py_CheckRecursionLimit.
667 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
668 to guarantee that _Py_CheckRecursiveCall() is regularly called.
669 Without USE_STACKCHECK, there is no need for this. */
670int
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100671_Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000672{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100673 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200674 int recursion_limit = runtime->ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000675
676#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700677 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 if (PyOS_CheckStack()) {
679 --tstate->recursion_depth;
Victor Stinner438a12d2019-05-24 17:01:38 +0200680 _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 return -1;
682 }
pdox18967932017-10-25 23:03:01 -0700683 /* Needed for ABI backwards-compatibility (see bpo-31857) */
Eric Snow05351c12017-09-05 21:43:08 -0700684 _Py_CheckRecursionLimit = recursion_limit;
pdox18967932017-10-25 23:03:01 -0700685#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 if (tstate->recursion_critical)
687 /* Somebody asked that we don't check for recursion. */
688 return 0;
689 if (tstate->overflowed) {
690 if (tstate->recursion_depth > recursion_limit + 50) {
691 /* Overflowing while handling an overflow. Give up. */
692 Py_FatalError("Cannot recover from stack overflow.");
693 }
694 return 0;
695 }
696 if (tstate->recursion_depth > recursion_limit) {
697 --tstate->recursion_depth;
698 tstate->overflowed = 1;
Victor Stinner438a12d2019-05-24 17:01:38 +0200699 _PyErr_Format(tstate, PyExc_RecursionError,
700 "maximum recursion depth exceeded%s",
701 where);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 return -1;
703 }
704 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000705}
706
Victor Stinner09532fe2019-05-10 23:39:09 +0200707static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
Victor Stinner438a12d2019-05-24 17:01:38 +0200708static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000709
Victor Stinnere225beb2019-06-03 18:14:24 +0200710#define _Py_TracingPossible(ceval) ((ceval)->tracing_possible)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000711
Guido van Rossum374a9221991-04-04 10:40:29 +0000712
Guido van Rossumb209a111997-04-29 18:18:01 +0000713PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000714PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000715{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 return PyEval_EvalCodeEx(co,
717 globals, locals,
718 (PyObject **)NULL, 0,
719 (PyObject **)NULL, 0,
720 (PyObject **)NULL, 0,
721 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000722}
723
724
725/* Interpreter main loop */
726
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000727PyObject *
Victor Stinnerb9e68122019-11-14 12:20:46 +0100728PyEval_EvalFrame(PyFrameObject *f)
729{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 /* This is for backward compatibility with extension modules that
731 used this API; core interpreter code should call
732 PyEval_EvalFrameEx() */
Victor Stinnerb9e68122019-11-14 12:20:46 +0100733 PyThreadState *tstate = _PyThreadState_GET();
734 return _PyEval_EvalFrame(tstate, f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000735}
736
737PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000738PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000739{
Victor Stinnerb9e68122019-11-14 12:20:46 +0100740 PyThreadState *tstate = _PyThreadState_GET();
741 return _PyEval_EvalFrame(tstate, f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700742}
743
Victor Stinnerc6944e72016-11-11 02:13:35 +0100744PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700745_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
746{
Guido van Rossum950361c1997-01-24 13:49:28 +0000747#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000748 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000749#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200750 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300751 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200752 int opcode; /* Current opcode */
753 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200754 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 PyObject *retval = NULL; /* Return value */
Victor Stinner09532fe2019-05-10 23:39:09 +0200756 _PyRuntimeState * const runtime = &_PyRuntime;
757 PyThreadState * const tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinnere225beb2019-06-03 18:14:24 +0200758 struct _ceval_runtime_state * const ceval = &runtime->ceval;
759 _Py_atomic_int * const eval_breaker = &ceval->eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 is true when the line being executed has changed. The
767 initial values are such as to make this false the first
768 time it is tested. */
769 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000770
Serhiy Storchakaab874002016-09-11 13:48:15 +0300771 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 PyObject *names;
773 PyObject *consts;
Inada Naoki91234a12019-06-03 21:30:58 +0900774 _PyOpcache *co_opcache;
Guido van Rossum374a9221991-04-04 10:40:29 +0000775
Brett Cannon368b4b72012-04-02 12:17:59 -0400776#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200777 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400778#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200779
Antoine Pitroub52ec782009-01-25 16:34:23 +0000780/* Computed GOTOs, or
781 the-optimization-commonly-but-improperly-known-as-"threaded code"
782 using gcc's labels-as-values extension
783 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
784
785 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000787 combined with a lookup table of jump addresses. However, since the
788 indirect jump instruction is shared by all opcodes, the CPU will have a
789 hard time making the right prediction for where to jump next (actually,
790 it will be always wrong except in the uncommon case of a sequence of
791 several identical opcodes).
792
793 "Threaded code" in contrast, uses an explicit jump table and an explicit
794 indirect jump instruction at the end of each opcode. Since the jump
795 instruction is at a different address for each opcode, the CPU will make a
796 separate prediction for each of these instructions, which is equivalent to
797 predicting the second opcode of each opcode pair. These predictions have
798 a much better chance to turn out valid, especially in small bytecode loops.
799
800 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000802 and potentially many more instructions (depending on the pipeline width).
803 A correctly predicted branch, however, is nearly free.
804
805 At the time of this writing, the "threaded code" version is up to 15-20%
806 faster than the normal "switch" version, depending on the compiler and the
807 CPU architecture.
808
809 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
810 because it would render the measurements invalid.
811
812
813 NOTE: care must be taken that the compiler doesn't try to "optimize" the
814 indirect jumps by sharing them between all opcodes. Such optimizations
815 can be disabled on gcc by using the -fno-gcse flag (or possibly
816 -fno-crossjumping).
817*/
818
Antoine Pitrou042b1282010-08-13 21:15:58 +0000819#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000820#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000821#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000822#endif
823
Antoine Pitrou042b1282010-08-13 21:15:58 +0000824#ifdef HAVE_COMPUTED_GOTOS
825 #ifndef USE_COMPUTED_GOTOS
826 #define USE_COMPUTED_GOTOS 1
827 #endif
828#else
829 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
830 #error "Computed gotos are not supported on this compiler."
831 #endif
832 #undef USE_COMPUTED_GOTOS
833 #define USE_COMPUTED_GOTOS 0
834#endif
835
836#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000837/* Import the static jump table */
838#include "opcode_targets.h"
839
Antoine Pitroub52ec782009-01-25 16:34:23 +0000840#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -0700841 op: \
842 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000843
Antoine Pitroub52ec782009-01-25 16:34:23 +0000844#ifdef LLTRACE
845#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200847 if (!lltrace && !_Py_TracingPossible(ceval) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300849 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300850 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 } \
852 goto fast_next_opcode; \
853 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000854#else
855#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200857 if (!_Py_TracingPossible(ceval) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300859 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300860 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 } \
862 goto fast_next_opcode; \
863 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000864#endif
865
Victor Stinner09532fe2019-05-10 23:39:09 +0200866#define DISPATCH() \
867 { \
868 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
869 FAST_DISPATCH(); \
870 } \
871 continue; \
872 }
873
Antoine Pitroub52ec782009-01-25 16:34:23 +0000874#else
Benjamin Petersonddd19492018-09-16 22:38:02 -0700875#define TARGET(op) op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000876#define FAST_DISPATCH() goto fast_next_opcode
Victor Stinner09532fe2019-05-10 23:39:09 +0200877#define DISPATCH() continue
Antoine Pitroub52ec782009-01-25 16:34:23 +0000878#endif
879
880
Neal Norwitza81d2202002-07-14 00:27:26 +0000881/* Tuple access macros */
882
883#ifndef Py_DEBUG
884#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
885#else
886#define GETITEM(v, i) PyTuple_GetItem((v), (i))
887#endif
888
Guido van Rossum374a9221991-04-04 10:40:29 +0000889/* Code access macros */
890
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300891/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600892#define INSTR_OFFSET() \
893 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300894#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300895 _Py_CODEUNIT word = *next_instr; \
896 opcode = _Py_OPCODE(word); \
897 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300898 next_instr++; \
899 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300900#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
901#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000902
Raymond Hettingerf606f872003-03-16 03:11:04 +0000903/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 Some opcodes tend to come in pairs thus making it possible to
905 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300906 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 Verifying the prediction costs a single high-speed test of a register
909 variable against a constant. If the pairing was good, then the
910 processor's own internal branch predication has a high likelihood of
911 success, resulting in a nearly zero-overhead transition to the
912 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300913 including its unpredictable switch-case branch. Combined with the
914 processor's internal branch prediction, a successful PREDICT has the
915 effect of making the two opcodes run as if they were a single new opcode
916 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000917
Georg Brandl86b2fb92008-07-16 03:43:04 +0000918 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 predictions turned-on and interpret the results as if some opcodes
920 had been combined or turn-off predictions so that the opcode frequency
921 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000922
923 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 the CPU to record separate branch prediction information for each
925 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000926
Raymond Hettingerf606f872003-03-16 03:11:04 +0000927*/
928
Antoine Pitrou042b1282010-08-13 21:15:58 +0000929#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000931#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300932#define PREDICT(op) \
933 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300934 _Py_CODEUNIT word = *next_instr; \
935 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300936 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300937 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300938 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300939 goto PRED_##op; \
940 } \
941 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000942#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300943#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000944
Raymond Hettingerf606f872003-03-16 03:11:04 +0000945
Guido van Rossum374a9221991-04-04 10:40:29 +0000946/* Stack manipulation macros */
947
Martin v. Löwis18e16552006-02-15 17:27:45 +0000948/* The stack can grow at most MAXINT deep, as co_nlocals and
949 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000950#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
951#define EMPTY() (STACK_LEVEL() == 0)
952#define TOP() (stack_pointer[-1])
953#define SECOND() (stack_pointer[-2])
954#define THIRD() (stack_pointer[-3])
955#define FOURTH() (stack_pointer[-4])
956#define PEEK(n) (stack_pointer[-(n)])
957#define SET_TOP(v) (stack_pointer[-1] = (v))
958#define SET_SECOND(v) (stack_pointer[-2] = (v))
959#define SET_THIRD(v) (stack_pointer[-3] = (v))
960#define SET_FOURTH(v) (stack_pointer[-4] = (v))
961#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
962#define BASIC_STACKADJ(n) (stack_pointer += n)
963#define BASIC_PUSH(v) (*stack_pointer++ = (v))
964#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000965
Guido van Rossum96a42c81992-01-12 02:29:51 +0000966#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +0200968 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000969 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +0200970#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000971 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +0000972#define STACK_GROW(n) do { \
973 assert(n >= 0); \
974 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +0200975 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +0000976 assert(STACK_LEVEL() <= co->co_stacksize); \
977 } while (0)
978#define STACK_SHRINK(n) do { \
979 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +0200980 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +0000981 (void)(BASIC_STACKADJ(-n)); \
982 assert(STACK_LEVEL() <= co->co_stacksize); \
983 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +0000984#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +0200985 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000986 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000987#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000988#define PUSH(v) BASIC_PUSH(v)
989#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +0000990#define STACK_GROW(n) BASIC_STACKADJ(n)
991#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000992#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000993#endif
994
Guido van Rossum681d79a1995-07-18 14:51:37 +0000995/* Local variable macros */
996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000998
999/* The SETLOCAL() macro must not DECREF the local variable in-place and
1000 then store the new value; it must copy the old value to a temporary
1001 value, then store the new value, and then DECREF the temporary value.
1002 This is because it is possible that during the DECREF the frame is
1003 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1004 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001006 GETLOCAL(i) = value; \
1007 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001008
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001009
1010#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 while (STACK_LEVEL() > (b)->b_level) { \
1012 PyObject *v = POP(); \
1013 Py_XDECREF(v); \
1014 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001015
1016#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001017 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +01001019 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1021 while (STACK_LEVEL() > (b)->b_level + 3) { \
1022 value = POP(); \
1023 Py_XDECREF(value); \
1024 } \
Mark Shannonae3087c2017-10-22 22:41:51 +01001025 exc_info = tstate->exc_info; \
1026 type = exc_info->exc_type; \
1027 value = exc_info->exc_value; \
1028 traceback = exc_info->exc_traceback; \
1029 exc_info->exc_type = POP(); \
1030 exc_info->exc_value = POP(); \
1031 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 Py_XDECREF(type); \
1033 Py_XDECREF(value); \
1034 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001035 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001036
Inada Naoki91234a12019-06-03 21:30:58 +09001037 /* macros for opcode cache */
1038#define OPCACHE_CHECK() \
1039 do { \
1040 co_opcache = NULL; \
1041 if (co->co_opcache != NULL) { \
1042 unsigned char co_opt_offset = \
1043 co->co_opcache_map[next_instr - first_instr]; \
1044 if (co_opt_offset > 0) { \
1045 assert(co_opt_offset <= co->co_opcache_size); \
1046 co_opcache = &co->co_opcache[co_opt_offset - 1]; \
1047 assert(co_opcache != NULL); \
Inada Naoki91234a12019-06-03 21:30:58 +09001048 } \
1049 } \
1050 } while (0)
1051
1052#if OPCACHE_STATS
1053
1054#define OPCACHE_STAT_GLOBAL_HIT() \
1055 do { \
1056 if (co->co_opcache != NULL) opcache_global_hits++; \
1057 } while (0)
1058
1059#define OPCACHE_STAT_GLOBAL_MISS() \
1060 do { \
1061 if (co->co_opcache != NULL) opcache_global_misses++; \
1062 } while (0)
1063
1064#define OPCACHE_STAT_GLOBAL_OPT() \
1065 do { \
1066 if (co->co_opcache != NULL) opcache_global_opts++; \
1067 } while (0)
1068
1069#else /* OPCACHE_STATS */
1070
1071#define OPCACHE_STAT_GLOBAL_HIT()
1072#define OPCACHE_STAT_GLOBAL_MISS()
1073#define OPCACHE_STAT_GLOBAL_OPT()
1074
1075#endif
1076
Guido van Rossuma027efa1997-05-05 20:56:21 +00001077/* Start of code */
1078
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 /* push frame */
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001080 if (_Py_EnterRecursiveCall(tstate, "")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001082 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001083
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001085
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001086 if (tstate->use_tracing) {
1087 if (tstate->c_tracefunc != NULL) {
1088 /* tstate->c_tracefunc, if defined, is a
1089 function that will be called on *every* entry
1090 to a code block. Its return value, if not
1091 None, is a function that will be called at
1092 the start of each executed line of code.
1093 (Actually, the function must return itself
1094 in order to continue tracing.) The trace
1095 functions are called with three arguments:
1096 a pointer to the current frame, a string
1097 indicating why the function is called, and
1098 an argument which depends on the situation.
1099 The global trace function is also called
1100 whenever an exception is detected. */
1101 if (call_trace_protected(tstate->c_tracefunc,
1102 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001103 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 /* Trace function raised an error */
1105 goto exit_eval_frame;
1106 }
1107 }
1108 if (tstate->c_profilefunc != NULL) {
1109 /* Similar for c_profilefunc, except it needn't
1110 return itself and isn't called for "line" events */
1111 if (call_trace_protected(tstate->c_profilefunc,
1112 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001113 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 /* Profile function raised an error */
1115 goto exit_eval_frame;
1116 }
1117 }
1118 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001119
Łukasz Langaa785c872016-09-09 17:37:37 -07001120 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1121 dtrace_function_entry(f);
1122
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 co = f->f_code;
1124 names = co->co_names;
1125 consts = co->co_consts;
1126 fastlocals = f->f_localsplus;
1127 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001128 assert(PyBytes_Check(co->co_code));
1129 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001130 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1131 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1132 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001133 /*
1134 f->f_lasti refers to the index of the last instruction,
1135 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001136
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001137 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001138 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 When the PREDICT() macros are enabled, some opcode pairs follow in
1141 direct succession without updating f->f_lasti. A successful
1142 prediction effectively links the two codes together as if they
1143 were a single new opcode; accordingly,f->f_lasti will point to
1144 the first code in the pair (for instance, GET_ITER followed by
1145 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001146 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001148 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001149 next_instr = first_instr;
1150 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001151 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1152 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001153 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001154 stack_pointer = f->f_stacktop;
1155 assert(stack_pointer != NULL);
1156 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001157 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001158
Inada Naoki91234a12019-06-03 21:30:58 +09001159 if (co->co_opcache_flag < OPCACHE_MIN_RUNS) {
1160 co->co_opcache_flag++;
1161 if (co->co_opcache_flag == OPCACHE_MIN_RUNS) {
1162 if (_PyCode_InitOpcache(co) < 0) {
1163 return NULL;
1164 }
1165#if OPCACHE_STATS
1166 opcache_code_objects_extra_mem +=
1167 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1168 sizeof(_PyOpcache) * co->co_opcache_size;
1169 opcache_code_objects++;
1170#endif
1171 }
1172 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001173
Tim Peters5ca576e2001-06-18 22:08:13 +00001174#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001175 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001176#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001177
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001178 if (throwflag) /* support for generator.throw() */
1179 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001180
Victor Stinnerace47d72013-07-18 01:41:08 +02001181#ifdef Py_DEBUG
1182 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001183 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001184 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001185 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001186#endif
1187
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001188main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1191 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001192 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001193
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 /* Do periodic things. Doing this every time through
1195 the loop would add too much overhead, so we do it
1196 only every Nth instruction. We also do it if
1197 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1198 event needs attention (e.g. a signal handler or
1199 async I/O handler); see Py_AddPendingCall() and
1200 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001201
Eric Snow7bda9de2019-03-08 17:25:54 -07001202 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001203 opcode = _Py_OPCODE(*next_instr);
1204 if (opcode == SETUP_FINALLY ||
1205 opcode == SETUP_WITH ||
1206 opcode == BEFORE_ASYNC_WITH ||
1207 opcode == YIELD_FROM) {
1208 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001209 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001210 - If we're about to enter the 'with:'. It will prevent
1211 emitting a resource warning in the common idiom
1212 'with open(path) as file:'.
1213 - If we're about to enter the 'async with:'.
1214 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001215 *very* useful, but might help in some cases and it's
1216 traditional)
1217 - If we're resuming a chain of nested 'yield from' or
1218 'await' calls, then each frame is parked with YIELD_FROM
1219 as its next opcode. If the user hit control-C we want to
1220 wait until we've reached the innermost frame before
1221 running the signal handler and raising KeyboardInterrupt
1222 (see bpo-30039).
1223 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 goto fast_next_opcode;
1225 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001226
Victor Stinnere225beb2019-06-03 18:14:24 +02001227 if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
Victor Stinner09532fe2019-05-10 23:39:09 +02001228 if (handle_signals(runtime) != 0) {
Eric Snowfdf282d2019-01-11 14:26:55 -07001229 goto error;
1230 }
1231 }
Victor Stinnere225beb2019-06-03 18:14:24 +02001232 if (_Py_atomic_load_relaxed(&ceval->pending.calls_to_do)) {
1233 if (make_pending_calls(runtime) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001234 goto error;
Eric Snowfdf282d2019-01-11 14:26:55 -07001235 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001237
Victor Stinnere225beb2019-06-03 18:14:24 +02001238 if (_Py_atomic_load_relaxed(&ceval->gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 /* Give another thread a chance */
Victor Stinner09532fe2019-05-10 23:39:09 +02001240 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 Py_FatalError("ceval: tstate mix-up");
Victor Stinner09532fe2019-05-10 23:39:09 +02001242 }
Victor Stinnere225beb2019-06-03 18:14:24 +02001243 drop_gil(ceval, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244
1245 /* Other threads may run now */
1246
Victor Stinnere225beb2019-06-03 18:14:24 +02001247 take_gil(ceval, tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001248
1249 /* Check if we should make a quick exit. */
Victor Stinner01b1cc12019-11-20 02:27:56 +01001250 exit_thread_if_finalizing(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001251
Victor Stinner09532fe2019-05-10 23:39:09 +02001252 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 Py_FatalError("ceval: orphan tstate");
Victor Stinner09532fe2019-05-10 23:39:09 +02001254 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 }
1256 /* Check for asynchronous exceptions. */
1257 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001258 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 tstate->async_exc = NULL;
Victor Stinnere225beb2019-06-03 18:14:24 +02001260 UNSIGNAL_ASYNC_EXC(ceval);
Victor Stinner438a12d2019-05-24 17:01:38 +02001261 _PyErr_SetNone(tstate, exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001262 Py_DECREF(exc);
1263 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 }
1265 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 fast_next_opcode:
1268 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001269
Łukasz Langaa785c872016-09-09 17:37:37 -07001270 if (PyDTrace_LINE_ENABLED())
1271 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001274
Victor Stinnere225beb2019-06-03 18:14:24 +02001275 if (_Py_TracingPossible(ceval) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001276 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001277 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 /* see maybe_call_line_trace
1279 for expository comments */
1280 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 err = maybe_call_line_trace(tstate->c_tracefunc,
1283 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001284 tstate, f,
1285 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 /* Reload possibly changed frame fields */
1287 JUMPTO(f->f_lasti);
1288 if (f->f_stacktop != NULL) {
1289 stack_pointer = f->f_stacktop;
1290 f->f_stacktop = NULL;
1291 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001292 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001294 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001295 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001298
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001299 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001300 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001301#ifdef DYNAMIC_EXECUTION_PROFILE
1302#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 dxpairs[lastopcode][opcode]++;
1304 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001305#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001307#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001308
Guido van Rossum96a42c81992-01-12 02:29:51 +00001309#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 if (lltrace) {
1313 if (HAS_ARG(opcode)) {
1314 printf("%d: %d, %d\n",
1315 f->f_lasti, opcode, oparg);
1316 }
1317 else {
1318 printf("%d: %d\n",
1319 f->f_lasti, opcode);
1320 }
1321 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001322#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001327 It is essential that any operation that fails must goto error
1328 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001329
Benjamin Petersonddd19492018-09-16 22:38:02 -07001330 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001331 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001332 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001333
Benjamin Petersonddd19492018-09-16 22:38:02 -07001334 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001335 PyObject *value = GETLOCAL(oparg);
1336 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001337 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001338 UNBOUNDLOCAL_ERROR_MSG,
1339 PyTuple_GetItem(co->co_varnames, oparg));
1340 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001342 Py_INCREF(value);
1343 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001345 }
1346
Benjamin Petersonddd19492018-09-16 22:38:02 -07001347 case TARGET(LOAD_CONST): {
1348 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001349 PyObject *value = GETITEM(consts, oparg);
1350 Py_INCREF(value);
1351 PUSH(value);
1352 FAST_DISPATCH();
1353 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001354
Benjamin Petersonddd19492018-09-16 22:38:02 -07001355 case TARGET(STORE_FAST): {
1356 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001357 PyObject *value = POP();
1358 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001359 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001360 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001361
Benjamin Petersonddd19492018-09-16 22:38:02 -07001362 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001363 PyObject *value = POP();
1364 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001365 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001366 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001367
Benjamin Petersonddd19492018-09-16 22:38:02 -07001368 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001369 PyObject *top = TOP();
1370 PyObject *second = SECOND();
1371 SET_TOP(second);
1372 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001374 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001375
Benjamin Petersonddd19492018-09-16 22:38:02 -07001376 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001377 PyObject *top = TOP();
1378 PyObject *second = SECOND();
1379 PyObject *third = THIRD();
1380 SET_TOP(second);
1381 SET_SECOND(third);
1382 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001384 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001385
Benjamin Petersonddd19492018-09-16 22:38:02 -07001386 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001387 PyObject *top = TOP();
1388 PyObject *second = SECOND();
1389 PyObject *third = THIRD();
1390 PyObject *fourth = FOURTH();
1391 SET_TOP(second);
1392 SET_SECOND(third);
1393 SET_THIRD(fourth);
1394 SET_FOURTH(top);
1395 FAST_DISPATCH();
1396 }
1397
Benjamin Petersonddd19492018-09-16 22:38:02 -07001398 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001399 PyObject *top = TOP();
1400 Py_INCREF(top);
1401 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001403 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001404
Benjamin Petersonddd19492018-09-16 22:38:02 -07001405 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001406 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001407 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001408 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001409 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001410 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001411 SET_TOP(top);
1412 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001413 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001414 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001415
Benjamin Petersonddd19492018-09-16 22:38:02 -07001416 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001417 PyObject *value = TOP();
1418 PyObject *res = PyNumber_Positive(value);
1419 Py_DECREF(value);
1420 SET_TOP(res);
1421 if (res == NULL)
1422 goto error;
1423 DISPATCH();
1424 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001425
Benjamin Petersonddd19492018-09-16 22:38:02 -07001426 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001427 PyObject *value = TOP();
1428 PyObject *res = PyNumber_Negative(value);
1429 Py_DECREF(value);
1430 SET_TOP(res);
1431 if (res == NULL)
1432 goto error;
1433 DISPATCH();
1434 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001435
Benjamin Petersonddd19492018-09-16 22:38:02 -07001436 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001437 PyObject *value = TOP();
1438 int err = PyObject_IsTrue(value);
1439 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 if (err == 0) {
1441 Py_INCREF(Py_True);
1442 SET_TOP(Py_True);
1443 DISPATCH();
1444 }
1445 else if (err > 0) {
1446 Py_INCREF(Py_False);
1447 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001448 DISPATCH();
1449 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001450 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001451 goto error;
1452 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001453
Benjamin Petersonddd19492018-09-16 22:38:02 -07001454 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001455 PyObject *value = TOP();
1456 PyObject *res = PyNumber_Invert(value);
1457 Py_DECREF(value);
1458 SET_TOP(res);
1459 if (res == NULL)
1460 goto error;
1461 DISPATCH();
1462 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001463
Benjamin Petersonddd19492018-09-16 22:38:02 -07001464 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001465 PyObject *exp = POP();
1466 PyObject *base = TOP();
1467 PyObject *res = PyNumber_Power(base, exp, Py_None);
1468 Py_DECREF(base);
1469 Py_DECREF(exp);
1470 SET_TOP(res);
1471 if (res == NULL)
1472 goto error;
1473 DISPATCH();
1474 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001475
Benjamin Petersonddd19492018-09-16 22:38:02 -07001476 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001477 PyObject *right = POP();
1478 PyObject *left = TOP();
1479 PyObject *res = PyNumber_Multiply(left, right);
1480 Py_DECREF(left);
1481 Py_DECREF(right);
1482 SET_TOP(res);
1483 if (res == NULL)
1484 goto error;
1485 DISPATCH();
1486 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001487
Benjamin Petersonddd19492018-09-16 22:38:02 -07001488 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001489 PyObject *right = POP();
1490 PyObject *left = TOP();
1491 PyObject *res = PyNumber_MatrixMultiply(left, right);
1492 Py_DECREF(left);
1493 Py_DECREF(right);
1494 SET_TOP(res);
1495 if (res == NULL)
1496 goto error;
1497 DISPATCH();
1498 }
1499
Benjamin Petersonddd19492018-09-16 22:38:02 -07001500 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001501 PyObject *divisor = POP();
1502 PyObject *dividend = TOP();
1503 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1504 Py_DECREF(dividend);
1505 Py_DECREF(divisor);
1506 SET_TOP(quotient);
1507 if (quotient == NULL)
1508 goto error;
1509 DISPATCH();
1510 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001511
Benjamin Petersonddd19492018-09-16 22:38:02 -07001512 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001513 PyObject *divisor = POP();
1514 PyObject *dividend = TOP();
1515 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1516 Py_DECREF(dividend);
1517 Py_DECREF(divisor);
1518 SET_TOP(quotient);
1519 if (quotient == NULL)
1520 goto error;
1521 DISPATCH();
1522 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001523
Benjamin Petersonddd19492018-09-16 22:38:02 -07001524 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001525 PyObject *divisor = POP();
1526 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001527 PyObject *res;
1528 if (PyUnicode_CheckExact(dividend) && (
1529 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1530 // fast path; string formatting, but not if the RHS is a str subclass
1531 // (see issue28598)
1532 res = PyUnicode_Format(dividend, divisor);
1533 } else {
1534 res = PyNumber_Remainder(dividend, divisor);
1535 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001536 Py_DECREF(divisor);
1537 Py_DECREF(dividend);
1538 SET_TOP(res);
1539 if (res == NULL)
1540 goto error;
1541 DISPATCH();
1542 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001543
Benjamin Petersonddd19492018-09-16 22:38:02 -07001544 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001545 PyObject *right = POP();
1546 PyObject *left = TOP();
1547 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001548 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1549 CPython using bytecode, it is simply worthless.
1550 See http://bugs.python.org/issue21955 and
1551 http://bugs.python.org/issue10044 for the discussion. In short,
1552 no patch shown any impact on a realistic benchmark, only a minor
1553 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001554 if (PyUnicode_CheckExact(left) &&
1555 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001556 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001557 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001558 }
1559 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001560 sum = PyNumber_Add(left, right);
1561 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001562 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001563 Py_DECREF(right);
1564 SET_TOP(sum);
1565 if (sum == NULL)
1566 goto error;
1567 DISPATCH();
1568 }
1569
Benjamin Petersonddd19492018-09-16 22:38:02 -07001570 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001571 PyObject *right = POP();
1572 PyObject *left = TOP();
1573 PyObject *diff = PyNumber_Subtract(left, right);
1574 Py_DECREF(right);
1575 Py_DECREF(left);
1576 SET_TOP(diff);
1577 if (diff == NULL)
1578 goto error;
1579 DISPATCH();
1580 }
1581
Benjamin Petersonddd19492018-09-16 22:38:02 -07001582 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001583 PyObject *sub = POP();
1584 PyObject *container = TOP();
1585 PyObject *res = PyObject_GetItem(container, sub);
1586 Py_DECREF(container);
1587 Py_DECREF(sub);
1588 SET_TOP(res);
1589 if (res == NULL)
1590 goto error;
1591 DISPATCH();
1592 }
1593
Benjamin Petersonddd19492018-09-16 22:38:02 -07001594 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001595 PyObject *right = POP();
1596 PyObject *left = TOP();
1597 PyObject *res = PyNumber_Lshift(left, right);
1598 Py_DECREF(left);
1599 Py_DECREF(right);
1600 SET_TOP(res);
1601 if (res == NULL)
1602 goto error;
1603 DISPATCH();
1604 }
1605
Benjamin Petersonddd19492018-09-16 22:38:02 -07001606 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001607 PyObject *right = POP();
1608 PyObject *left = TOP();
1609 PyObject *res = PyNumber_Rshift(left, right);
1610 Py_DECREF(left);
1611 Py_DECREF(right);
1612 SET_TOP(res);
1613 if (res == NULL)
1614 goto error;
1615 DISPATCH();
1616 }
1617
Benjamin Petersonddd19492018-09-16 22:38:02 -07001618 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001619 PyObject *right = POP();
1620 PyObject *left = TOP();
1621 PyObject *res = PyNumber_And(left, right);
1622 Py_DECREF(left);
1623 Py_DECREF(right);
1624 SET_TOP(res);
1625 if (res == NULL)
1626 goto error;
1627 DISPATCH();
1628 }
1629
Benjamin Petersonddd19492018-09-16 22:38:02 -07001630 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001631 PyObject *right = POP();
1632 PyObject *left = TOP();
1633 PyObject *res = PyNumber_Xor(left, right);
1634 Py_DECREF(left);
1635 Py_DECREF(right);
1636 SET_TOP(res);
1637 if (res == NULL)
1638 goto error;
1639 DISPATCH();
1640 }
1641
Benjamin Petersonddd19492018-09-16 22:38:02 -07001642 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001643 PyObject *right = POP();
1644 PyObject *left = TOP();
1645 PyObject *res = PyNumber_Or(left, right);
1646 Py_DECREF(left);
1647 Py_DECREF(right);
1648 SET_TOP(res);
1649 if (res == NULL)
1650 goto error;
1651 DISPATCH();
1652 }
1653
Benjamin Petersonddd19492018-09-16 22:38:02 -07001654 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001655 PyObject *v = POP();
1656 PyObject *list = PEEK(oparg);
1657 int err;
1658 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001659 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001660 if (err != 0)
1661 goto error;
1662 PREDICT(JUMP_ABSOLUTE);
1663 DISPATCH();
1664 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001665
Benjamin Petersonddd19492018-09-16 22:38:02 -07001666 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001667 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001668 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001669 int err;
1670 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001672 if (err != 0)
1673 goto error;
1674 PREDICT(JUMP_ABSOLUTE);
1675 DISPATCH();
1676 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001677
Benjamin Petersonddd19492018-09-16 22:38:02 -07001678 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001679 PyObject *exp = POP();
1680 PyObject *base = TOP();
1681 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1682 Py_DECREF(base);
1683 Py_DECREF(exp);
1684 SET_TOP(res);
1685 if (res == NULL)
1686 goto error;
1687 DISPATCH();
1688 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001689
Benjamin Petersonddd19492018-09-16 22:38:02 -07001690 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001691 PyObject *right = POP();
1692 PyObject *left = TOP();
1693 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1694 Py_DECREF(left);
1695 Py_DECREF(right);
1696 SET_TOP(res);
1697 if (res == NULL)
1698 goto error;
1699 DISPATCH();
1700 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001701
Benjamin Petersonddd19492018-09-16 22:38:02 -07001702 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001703 PyObject *right = POP();
1704 PyObject *left = TOP();
1705 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1706 Py_DECREF(left);
1707 Py_DECREF(right);
1708 SET_TOP(res);
1709 if (res == NULL)
1710 goto error;
1711 DISPATCH();
1712 }
1713
Benjamin Petersonddd19492018-09-16 22:38:02 -07001714 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001715 PyObject *divisor = POP();
1716 PyObject *dividend = TOP();
1717 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1718 Py_DECREF(dividend);
1719 Py_DECREF(divisor);
1720 SET_TOP(quotient);
1721 if (quotient == NULL)
1722 goto error;
1723 DISPATCH();
1724 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001725
Benjamin Petersonddd19492018-09-16 22:38:02 -07001726 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001727 PyObject *divisor = POP();
1728 PyObject *dividend = TOP();
1729 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1730 Py_DECREF(dividend);
1731 Py_DECREF(divisor);
1732 SET_TOP(quotient);
1733 if (quotient == NULL)
1734 goto error;
1735 DISPATCH();
1736 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001737
Benjamin Petersonddd19492018-09-16 22:38:02 -07001738 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001739 PyObject *right = POP();
1740 PyObject *left = TOP();
1741 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1742 Py_DECREF(left);
1743 Py_DECREF(right);
1744 SET_TOP(mod);
1745 if (mod == NULL)
1746 goto error;
1747 DISPATCH();
1748 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001749
Benjamin Petersonddd19492018-09-16 22:38:02 -07001750 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001751 PyObject *right = POP();
1752 PyObject *left = TOP();
1753 PyObject *sum;
1754 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001755 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001756 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001757 }
1758 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001759 sum = PyNumber_InPlaceAdd(left, right);
1760 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001761 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001762 Py_DECREF(right);
1763 SET_TOP(sum);
1764 if (sum == NULL)
1765 goto error;
1766 DISPATCH();
1767 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001768
Benjamin Petersonddd19492018-09-16 22:38:02 -07001769 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001770 PyObject *right = POP();
1771 PyObject *left = TOP();
1772 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1773 Py_DECREF(left);
1774 Py_DECREF(right);
1775 SET_TOP(diff);
1776 if (diff == NULL)
1777 goto error;
1778 DISPATCH();
1779 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001780
Benjamin Petersonddd19492018-09-16 22:38:02 -07001781 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001782 PyObject *right = POP();
1783 PyObject *left = TOP();
1784 PyObject *res = PyNumber_InPlaceLshift(left, right);
1785 Py_DECREF(left);
1786 Py_DECREF(right);
1787 SET_TOP(res);
1788 if (res == NULL)
1789 goto error;
1790 DISPATCH();
1791 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001792
Benjamin Petersonddd19492018-09-16 22:38:02 -07001793 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001794 PyObject *right = POP();
1795 PyObject *left = TOP();
1796 PyObject *res = PyNumber_InPlaceRshift(left, right);
1797 Py_DECREF(left);
1798 Py_DECREF(right);
1799 SET_TOP(res);
1800 if (res == NULL)
1801 goto error;
1802 DISPATCH();
1803 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001804
Benjamin Petersonddd19492018-09-16 22:38:02 -07001805 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001806 PyObject *right = POP();
1807 PyObject *left = TOP();
1808 PyObject *res = PyNumber_InPlaceAnd(left, right);
1809 Py_DECREF(left);
1810 Py_DECREF(right);
1811 SET_TOP(res);
1812 if (res == NULL)
1813 goto error;
1814 DISPATCH();
1815 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001816
Benjamin Petersonddd19492018-09-16 22:38:02 -07001817 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001818 PyObject *right = POP();
1819 PyObject *left = TOP();
1820 PyObject *res = PyNumber_InPlaceXor(left, right);
1821 Py_DECREF(left);
1822 Py_DECREF(right);
1823 SET_TOP(res);
1824 if (res == NULL)
1825 goto error;
1826 DISPATCH();
1827 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001828
Benjamin Petersonddd19492018-09-16 22:38:02 -07001829 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001830 PyObject *right = POP();
1831 PyObject *left = TOP();
1832 PyObject *res = PyNumber_InPlaceOr(left, right);
1833 Py_DECREF(left);
1834 Py_DECREF(right);
1835 SET_TOP(res);
1836 if (res == NULL)
1837 goto error;
1838 DISPATCH();
1839 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001840
Benjamin Petersonddd19492018-09-16 22:38:02 -07001841 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001842 PyObject *sub = TOP();
1843 PyObject *container = SECOND();
1844 PyObject *v = THIRD();
1845 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001846 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001847 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001848 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001849 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001850 Py_DECREF(container);
1851 Py_DECREF(sub);
1852 if (err != 0)
1853 goto error;
1854 DISPATCH();
1855 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001856
Benjamin Petersonddd19492018-09-16 22:38:02 -07001857 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001858 PyObject *sub = TOP();
1859 PyObject *container = SECOND();
1860 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001861 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00001862 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001863 err = PyObject_DelItem(container, sub);
1864 Py_DECREF(container);
1865 Py_DECREF(sub);
1866 if (err != 0)
1867 goto error;
1868 DISPATCH();
1869 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001870
Benjamin Petersonddd19492018-09-16 22:38:02 -07001871 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01001872 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001873 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001874 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001875 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001876 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001877 _PyErr_SetString(tstate, PyExc_RuntimeError,
1878 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001879 Py_DECREF(value);
1880 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001881 }
Jeroen Demeyer196a5302019-07-04 12:31:34 +02001882 res = _PyObject_CallOneArg(hook, value);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001883 Py_DECREF(value);
1884 if (res == NULL)
1885 goto error;
1886 Py_DECREF(res);
1887 DISPATCH();
1888 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001889
Benjamin Petersonddd19492018-09-16 22:38:02 -07001890 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001891 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 switch (oparg) {
1893 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001894 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001895 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001896 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001897 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001898 /* fall through */
1899 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02001900 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001901 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001902 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903 break;
1904 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02001905 _PyErr_SetString(tstate, PyExc_SystemError,
1906 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001907 break;
1908 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001909 goto error;
1910 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001911
Benjamin Petersonddd19492018-09-16 22:38:02 -07001912 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001913 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001914 assert(f->f_iblock == 0);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00001915 assert(EMPTY());
1916 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001917 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001918
Benjamin Petersonddd19492018-09-16 22:38:02 -07001919 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001920 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001921 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001922 PyObject *obj = TOP();
1923 PyTypeObject *type = Py_TYPE(obj);
1924
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001925 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001926 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001927 }
Yury Selivanov75445082015-05-11 22:57:16 -04001928
1929 if (getter != NULL) {
1930 iter = (*getter)(obj);
1931 Py_DECREF(obj);
1932 if (iter == NULL) {
1933 SET_TOP(NULL);
1934 goto error;
1935 }
1936 }
1937 else {
1938 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02001939 _PyErr_Format(tstate, PyExc_TypeError,
1940 "'async for' requires an object with "
1941 "__aiter__ method, got %.100s",
1942 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001943 Py_DECREF(obj);
1944 goto error;
1945 }
1946
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001947 if (Py_TYPE(iter)->tp_as_async == NULL ||
1948 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001949
Yury Selivanov398ff912017-03-02 22:20:00 -05001950 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02001951 _PyErr_Format(tstate, PyExc_TypeError,
1952 "'async for' received an object from __aiter__ "
1953 "that does not implement __anext__: %.100s",
1954 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001955 Py_DECREF(iter);
1956 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001957 }
1958
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001959 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001960 DISPATCH();
1961 }
1962
Benjamin Petersonddd19492018-09-16 22:38:02 -07001963 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001964 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001965 PyObject *next_iter = NULL;
1966 PyObject *awaitable = NULL;
1967 PyObject *aiter = TOP();
1968 PyTypeObject *type = Py_TYPE(aiter);
1969
Yury Selivanoveb636452016-09-08 22:01:51 -07001970 if (PyAsyncGen_CheckExact(aiter)) {
1971 awaitable = type->tp_as_async->am_anext(aiter);
1972 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001973 goto error;
1974 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001975 } else {
1976 if (type->tp_as_async != NULL){
1977 getter = type->tp_as_async->am_anext;
1978 }
Yury Selivanov75445082015-05-11 22:57:16 -04001979
Yury Selivanoveb636452016-09-08 22:01:51 -07001980 if (getter != NULL) {
1981 next_iter = (*getter)(aiter);
1982 if (next_iter == NULL) {
1983 goto error;
1984 }
1985 }
1986 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02001987 _PyErr_Format(tstate, PyExc_TypeError,
1988 "'async for' requires an iterator with "
1989 "__anext__ method, got %.100s",
1990 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07001991 goto error;
1992 }
Yury Selivanov75445082015-05-11 22:57:16 -04001993
Yury Selivanoveb636452016-09-08 22:01:51 -07001994 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1995 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001996 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001997 PyExc_TypeError,
1998 "'async for' received an invalid object "
1999 "from __anext__: %.100s",
2000 Py_TYPE(next_iter)->tp_name);
2001
2002 Py_DECREF(next_iter);
2003 goto error;
2004 } else {
2005 Py_DECREF(next_iter);
2006 }
2007 }
Yury Selivanov75445082015-05-11 22:57:16 -04002008
2009 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002010 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002011 DISPATCH();
2012 }
2013
Benjamin Petersonddd19492018-09-16 22:38:02 -07002014 case TARGET(GET_AWAITABLE): {
2015 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002016 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002017 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002018
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002019 if (iter == NULL) {
Mark Shannonfee55262019-11-21 09:11:43 +00002020 int opcode_at_minus_3 = 0;
2021 if ((next_instr - first_instr) > 2) {
2022 opcode_at_minus_3 = _Py_OPCODE(next_instr[-3]);
2023 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002024 format_awaitable_error(tstate, Py_TYPE(iterable),
Mark Shannonfee55262019-11-21 09:11:43 +00002025 opcode_at_minus_3,
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002026 _Py_OPCODE(next_instr[-2]));
2027 }
2028
Yury Selivanov75445082015-05-11 22:57:16 -04002029 Py_DECREF(iterable);
2030
Yury Selivanovc724bae2016-03-02 11:30:46 -05002031 if (iter != NULL && PyCoro_CheckExact(iter)) {
2032 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2033 if (yf != NULL) {
2034 /* `iter` is a coroutine object that is being
2035 awaited, `yf` is a pointer to the current awaitable
2036 being awaited on. */
2037 Py_DECREF(yf);
2038 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002039 _PyErr_SetString(tstate, PyExc_RuntimeError,
2040 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002041 /* The code below jumps to `error` if `iter` is NULL. */
2042 }
2043 }
2044
Yury Selivanov75445082015-05-11 22:57:16 -04002045 SET_TOP(iter); /* Even if it's NULL */
2046
2047 if (iter == NULL) {
2048 goto error;
2049 }
2050
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002051 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002052 DISPATCH();
2053 }
2054
Benjamin Petersonddd19492018-09-16 22:38:02 -07002055 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002056 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002057 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002058 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002059 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
2060 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002061 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002062 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002063 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002064 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002065 else
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02002066 retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002067 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002068 Py_DECREF(v);
2069 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002070 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002071 if (tstate->c_tracefunc != NULL
Victor Stinner438a12d2019-05-24 17:01:38 +02002072 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002073 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002074 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002075 if (err < 0)
2076 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002077 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002078 SET_TOP(val);
2079 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002080 }
Martin Panter95f53c12016-07-18 08:23:26 +00002081 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002082 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002083 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002084 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002085 f->f_lasti -= sizeof(_Py_CODEUNIT);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002086 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002087 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002088
Benjamin Petersonddd19492018-09-16 22:38:02 -07002089 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002090 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002091
2092 if (co->co_flags & CO_ASYNC_GENERATOR) {
2093 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2094 Py_DECREF(retval);
2095 if (w == NULL) {
2096 retval = NULL;
2097 goto error;
2098 }
2099 retval = w;
2100 }
2101
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002102 f->f_stacktop = stack_pointer;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002103 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002104 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002105
Benjamin Petersonddd19492018-09-16 22:38:02 -07002106 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002107 PyObject *type, *value, *traceback;
2108 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002109 PyTryBlock *b = PyFrame_BlockPop(f);
2110 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002111 _PyErr_SetString(tstate, PyExc_SystemError,
2112 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002113 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002114 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002115 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2116 STACK_LEVEL() <= (b)->b_level + 4);
2117 exc_info = tstate->exc_info;
2118 type = exc_info->exc_type;
2119 value = exc_info->exc_value;
2120 traceback = exc_info->exc_traceback;
2121 exc_info->exc_type = POP();
2122 exc_info->exc_value = POP();
2123 exc_info->exc_traceback = POP();
2124 Py_XDECREF(type);
2125 Py_XDECREF(value);
2126 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002127 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002128 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002129
Benjamin Petersonddd19492018-09-16 22:38:02 -07002130 case TARGET(POP_BLOCK): {
2131 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002132 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002133 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002134 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002135
Mark Shannonfee55262019-11-21 09:11:43 +00002136 case TARGET(RERAISE): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002137 PyObject *exc = POP();
Mark Shannonfee55262019-11-21 09:11:43 +00002138 PyObject *val = POP();
2139 PyObject *tb = POP();
2140 assert(PyExceptionClass_Check(exc));
Victor Stinner61f4db82020-01-28 03:37:45 +01002141 _PyErr_Restore(tstate, exc, val, tb);
Mark Shannonfee55262019-11-21 09:11:43 +00002142 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002143 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002144
Benjamin Petersonddd19492018-09-16 22:38:02 -07002145 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002146 PyObject *exc = POP();
2147 assert(PyExceptionClass_Check(exc));
2148 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2149 PyTryBlock *b = PyFrame_BlockPop(f);
2150 assert(b->b_type == EXCEPT_HANDLER);
2151 Py_DECREF(exc);
2152 UNWIND_EXCEPT_HANDLER(b);
2153 Py_DECREF(POP());
2154 JUMPBY(oparg);
2155 FAST_DISPATCH();
2156 }
2157 else {
2158 PyObject *val = POP();
2159 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002160 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002161 goto exception_unwind;
2162 }
2163 }
2164
Zackery Spytzce6a0702019-08-25 03:44:09 -06002165 case TARGET(LOAD_ASSERTION_ERROR): {
2166 PyObject *value = PyExc_AssertionError;
2167 Py_INCREF(value);
2168 PUSH(value);
2169 FAST_DISPATCH();
2170 }
2171
Benjamin Petersonddd19492018-09-16 22:38:02 -07002172 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002173 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002174
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002175 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002176 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002177 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002178 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002179 if (!_PyErr_Occurred(tstate)) {
2180 _PyErr_SetString(tstate, PyExc_NameError,
2181 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002182 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002183 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002184 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002185 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002186 }
2187 else {
2188 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2189 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002190 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002191 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2192 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002193 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2194 _PyErr_SetString(tstate, PyExc_NameError,
2195 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002196 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002197 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002198 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002199 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002200 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002201 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002202
Benjamin Petersonddd19492018-09-16 22:38:02 -07002203 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002204 PyObject *name = GETITEM(names, oparg);
2205 PyObject *v = POP();
2206 PyObject *ns = f->f_locals;
2207 int err;
2208 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002209 _PyErr_Format(tstate, PyExc_SystemError,
2210 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002211 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002212 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002213 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002214 if (PyDict_CheckExact(ns))
2215 err = PyDict_SetItem(ns, name, v);
2216 else
2217 err = PyObject_SetItem(ns, name, v);
2218 Py_DECREF(v);
2219 if (err != 0)
2220 goto error;
2221 DISPATCH();
2222 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002223
Benjamin Petersonddd19492018-09-16 22:38:02 -07002224 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002225 PyObject *name = GETITEM(names, oparg);
2226 PyObject *ns = f->f_locals;
2227 int err;
2228 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002229 _PyErr_Format(tstate, PyExc_SystemError,
2230 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002231 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002232 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002233 err = PyObject_DelItem(ns, name);
2234 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002235 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002236 NAME_ERROR_MSG,
2237 name);
2238 goto error;
2239 }
2240 DISPATCH();
2241 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002242
Benjamin Petersonddd19492018-09-16 22:38:02 -07002243 case TARGET(UNPACK_SEQUENCE): {
2244 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002245 PyObject *seq = POP(), *item, **items;
2246 if (PyTuple_CheckExact(seq) &&
2247 PyTuple_GET_SIZE(seq) == oparg) {
2248 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002249 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002250 item = items[oparg];
2251 Py_INCREF(item);
2252 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002253 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002254 } else if (PyList_CheckExact(seq) &&
2255 PyList_GET_SIZE(seq) == oparg) {
2256 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002257 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002258 item = items[oparg];
2259 Py_INCREF(item);
2260 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002261 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002262 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002263 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002264 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002265 } else {
2266 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002267 Py_DECREF(seq);
2268 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002269 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002270 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002271 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002272 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002273
Benjamin Petersonddd19492018-09-16 22:38:02 -07002274 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002275 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2276 PyObject *seq = POP();
2277
Victor Stinner438a12d2019-05-24 17:01:38 +02002278 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002279 stack_pointer + totalargs)) {
2280 stack_pointer += totalargs;
2281 } else {
2282 Py_DECREF(seq);
2283 goto error;
2284 }
2285 Py_DECREF(seq);
2286 DISPATCH();
2287 }
2288
Benjamin Petersonddd19492018-09-16 22:38:02 -07002289 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002290 PyObject *name = GETITEM(names, oparg);
2291 PyObject *owner = TOP();
2292 PyObject *v = SECOND();
2293 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002294 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002295 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002296 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002297 Py_DECREF(owner);
2298 if (err != 0)
2299 goto error;
2300 DISPATCH();
2301 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002302
Benjamin Petersonddd19492018-09-16 22:38:02 -07002303 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002304 PyObject *name = GETITEM(names, oparg);
2305 PyObject *owner = POP();
2306 int err;
2307 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2308 Py_DECREF(owner);
2309 if (err != 0)
2310 goto error;
2311 DISPATCH();
2312 }
2313
Benjamin Petersonddd19492018-09-16 22:38:02 -07002314 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002315 PyObject *name = GETITEM(names, oparg);
2316 PyObject *v = POP();
2317 int err;
2318 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002319 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002320 if (err != 0)
2321 goto error;
2322 DISPATCH();
2323 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002324
Benjamin Petersonddd19492018-09-16 22:38:02 -07002325 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002326 PyObject *name = GETITEM(names, oparg);
2327 int err;
2328 err = PyDict_DelItem(f->f_globals, name);
2329 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002330 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2331 format_exc_check_arg(tstate, PyExc_NameError,
2332 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002333 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002334 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002335 }
2336 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002337 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002338
Benjamin Petersonddd19492018-09-16 22:38:02 -07002339 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002340 PyObject *name = GETITEM(names, oparg);
2341 PyObject *locals = f->f_locals;
2342 PyObject *v;
2343 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002344 _PyErr_Format(tstate, PyExc_SystemError,
2345 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002346 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002347 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002348 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002349 v = PyDict_GetItemWithError(locals, name);
2350 if (v != NULL) {
2351 Py_INCREF(v);
2352 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002353 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002354 goto error;
2355 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002356 }
2357 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002358 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002359 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002360 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002361 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002362 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002363 }
2364 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002365 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002366 v = PyDict_GetItemWithError(f->f_globals, name);
2367 if (v != NULL) {
2368 Py_INCREF(v);
2369 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002370 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002371 goto error;
2372 }
2373 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002374 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002375 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002376 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002377 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002378 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002379 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002380 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002381 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002382 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002383 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002384 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002385 }
2386 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002387 v = PyObject_GetItem(f->f_builtins, name);
2388 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002389 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002390 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002391 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002392 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002393 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002394 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002395 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002396 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002397 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002398 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002399 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002400 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002401 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002402
Benjamin Petersonddd19492018-09-16 22:38:02 -07002403 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002404 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002405 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002406 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002407 && PyDict_CheckExact(f->f_builtins))
2408 {
Inada Naoki91234a12019-06-03 21:30:58 +09002409 OPCACHE_CHECK();
2410 if (co_opcache != NULL && co_opcache->optimized > 0) {
2411 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2412
2413 if (lg->globals_ver ==
2414 ((PyDictObject *)f->f_globals)->ma_version_tag
2415 && lg->builtins_ver ==
2416 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2417 {
2418 PyObject *ptr = lg->ptr;
2419 OPCACHE_STAT_GLOBAL_HIT();
2420 assert(ptr != NULL);
2421 Py_INCREF(ptr);
2422 PUSH(ptr);
2423 DISPATCH();
2424 }
2425 }
2426
2427 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002428 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002429 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002430 name);
2431 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002432 if (!_PyErr_OCCURRED()) {
2433 /* _PyDict_LoadGlobal() returns NULL without raising
2434 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002435 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002436 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002437 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002438 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002439 }
Inada Naoki91234a12019-06-03 21:30:58 +09002440
2441 if (co_opcache != NULL) {
2442 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2443
2444 if (co_opcache->optimized == 0) {
2445 /* Wasn't optimized before. */
2446 OPCACHE_STAT_GLOBAL_OPT();
2447 } else {
2448 OPCACHE_STAT_GLOBAL_MISS();
2449 }
2450
2451 co_opcache->optimized = 1;
2452 lg->globals_ver =
2453 ((PyDictObject *)f->f_globals)->ma_version_tag;
2454 lg->builtins_ver =
2455 ((PyDictObject *)f->f_builtins)->ma_version_tag;
2456 lg->ptr = v; /* borrowed */
2457 }
2458
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002459 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002460 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002461 else {
2462 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002463
2464 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09002465 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002466 v = PyObject_GetItem(f->f_globals, name);
2467 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002468 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002469 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002470 }
2471 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002472
Victor Stinnerb4efc962015-11-20 09:24:02 +01002473 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002474 v = PyObject_GetItem(f->f_builtins, name);
2475 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002476 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002477 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002478 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002479 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002480 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002481 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002482 }
2483 }
2484 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002485 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002486 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002487 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002488
Benjamin Petersonddd19492018-09-16 22:38:02 -07002489 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002490 PyObject *v = GETLOCAL(oparg);
2491 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002492 SETLOCAL(oparg, NULL);
2493 DISPATCH();
2494 }
2495 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002496 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002497 UNBOUNDLOCAL_ERROR_MSG,
2498 PyTuple_GetItem(co->co_varnames, oparg)
2499 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002500 goto error;
2501 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002502
Benjamin Petersonddd19492018-09-16 22:38:02 -07002503 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002504 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002505 PyObject *oldobj = PyCell_GET(cell);
2506 if (oldobj != NULL) {
2507 PyCell_SET(cell, NULL);
2508 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002509 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002510 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002511 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002512 goto error;
2513 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002514
Benjamin Petersonddd19492018-09-16 22:38:02 -07002515 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002516 PyObject *cell = freevars[oparg];
2517 Py_INCREF(cell);
2518 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002519 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002520 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002521
Benjamin Petersonddd19492018-09-16 22:38:02 -07002522 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002523 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002524 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002525 assert(locals);
2526 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2527 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2528 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2529 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2530 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002531 value = PyDict_GetItemWithError(locals, name);
2532 if (value != NULL) {
2533 Py_INCREF(value);
2534 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002535 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002536 goto error;
2537 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002538 }
2539 else {
2540 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002541 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002542 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002543 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002544 }
2545 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002546 }
2547 }
2548 if (!value) {
2549 PyObject *cell = freevars[oparg];
2550 value = PyCell_GET(cell);
2551 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002552 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002553 goto error;
2554 }
2555 Py_INCREF(value);
2556 }
2557 PUSH(value);
2558 DISPATCH();
2559 }
2560
Benjamin Petersonddd19492018-09-16 22:38:02 -07002561 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002562 PyObject *cell = freevars[oparg];
2563 PyObject *value = PyCell_GET(cell);
2564 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002565 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002566 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002567 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002568 Py_INCREF(value);
2569 PUSH(value);
2570 DISPATCH();
2571 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002572
Benjamin Petersonddd19492018-09-16 22:38:02 -07002573 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002574 PyObject *v = POP();
2575 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002576 PyObject *oldobj = PyCell_GET(cell);
2577 PyCell_SET(cell, v);
2578 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002579 DISPATCH();
2580 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002581
Benjamin Petersonddd19492018-09-16 22:38:02 -07002582 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002583 PyObject *str;
2584 PyObject *empty = PyUnicode_New(0, 0);
2585 if (empty == NULL) {
2586 goto error;
2587 }
2588 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2589 Py_DECREF(empty);
2590 if (str == NULL)
2591 goto error;
2592 while (--oparg >= 0) {
2593 PyObject *item = POP();
2594 Py_DECREF(item);
2595 }
2596 PUSH(str);
2597 DISPATCH();
2598 }
2599
Benjamin Petersonddd19492018-09-16 22:38:02 -07002600 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002601 PyObject *tup = PyTuple_New(oparg);
2602 if (tup == NULL)
2603 goto error;
2604 while (--oparg >= 0) {
2605 PyObject *item = POP();
2606 PyTuple_SET_ITEM(tup, oparg, item);
2607 }
2608 PUSH(tup);
2609 DISPATCH();
2610 }
2611
Benjamin Petersonddd19492018-09-16 22:38:02 -07002612 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002613 PyObject *list = PyList_New(oparg);
2614 if (list == NULL)
2615 goto error;
2616 while (--oparg >= 0) {
2617 PyObject *item = POP();
2618 PyList_SET_ITEM(list, oparg, item);
2619 }
2620 PUSH(list);
2621 DISPATCH();
2622 }
2623
Mark Shannon13bc1392020-01-23 09:25:17 +00002624 case TARGET(LIST_TO_TUPLE): {
2625 PyObject *list = POP();
2626 PyObject *tuple = PyList_AsTuple(list);
2627 Py_DECREF(list);
2628 if (tuple == NULL) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002629 goto error;
Mark Shannon13bc1392020-01-23 09:25:17 +00002630 }
2631 PUSH(tuple);
2632 DISPATCH();
2633 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002634
Mark Shannon13bc1392020-01-23 09:25:17 +00002635 case TARGET(LIST_EXTEND): {
2636 PyObject *iterable = POP();
2637 PyObject *list = PEEK(oparg);
2638 PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
2639 if (none_val == NULL) {
2640 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
2641 (iterable->ob_type->tp_iter == NULL && !PySequence_Check(iterable)))
2642 {
Victor Stinner61f4db82020-01-28 03:37:45 +01002643 _PyErr_Clear(tstate);
Mark Shannon13bc1392020-01-23 09:25:17 +00002644 _PyErr_Format(tstate, PyExc_TypeError,
2645 "Value after * must be an iterable, not %.200s",
2646 Py_TYPE(iterable)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002647 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002648 Py_DECREF(iterable);
2649 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002650 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002651 Py_DECREF(none_val);
2652 Py_DECREF(iterable);
2653 DISPATCH();
2654 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002655
Mark Shannon13bc1392020-01-23 09:25:17 +00002656 case TARGET(SET_UPDATE): {
2657 PyObject *iterable = POP();
2658 PyObject *set = PEEK(oparg);
2659 int err = _PySet_Update(set, iterable);
2660 Py_DECREF(iterable);
2661 if (err < 0) {
2662 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002663 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002664 DISPATCH();
2665 }
2666
Benjamin Petersonddd19492018-09-16 22:38:02 -07002667 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002668 PyObject *set = PySet_New(NULL);
2669 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002670 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002671 if (set == NULL)
2672 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002673 for (i = oparg; i > 0; i--) {
2674 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002675 if (err == 0)
2676 err = PySet_Add(set, item);
2677 Py_DECREF(item);
2678 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002679 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002680 if (err != 0) {
2681 Py_DECREF(set);
2682 goto error;
2683 }
2684 PUSH(set);
2685 DISPATCH();
2686 }
2687
Benjamin Petersonddd19492018-09-16 22:38:02 -07002688 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002689 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002690 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2691 if (map == NULL)
2692 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002693 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002694 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002695 PyObject *key = PEEK(2*i);
2696 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002697 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002698 if (err != 0) {
2699 Py_DECREF(map);
2700 goto error;
2701 }
2702 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002703
2704 while (oparg--) {
2705 Py_DECREF(POP());
2706 Py_DECREF(POP());
2707 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002708 PUSH(map);
2709 DISPATCH();
2710 }
2711
Benjamin Petersonddd19492018-09-16 22:38:02 -07002712 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002713 _Py_IDENTIFIER(__annotations__);
2714 int err;
2715 PyObject *ann_dict;
2716 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002717 _PyErr_Format(tstate, PyExc_SystemError,
2718 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002719 goto error;
2720 }
2721 /* check if __annotations__ in locals()... */
2722 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002723 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002724 &PyId___annotations__);
2725 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002726 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002727 goto error;
2728 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002729 /* ...if not, create a new one */
2730 ann_dict = PyDict_New();
2731 if (ann_dict == NULL) {
2732 goto error;
2733 }
2734 err = _PyDict_SetItemId(f->f_locals,
2735 &PyId___annotations__, ann_dict);
2736 Py_DECREF(ann_dict);
2737 if (err != 0) {
2738 goto error;
2739 }
2740 }
2741 }
2742 else {
2743 /* do the same if locals() is not a dict */
2744 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2745 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002746 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002747 }
2748 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2749 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002750 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002751 goto error;
2752 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002753 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002754 ann_dict = PyDict_New();
2755 if (ann_dict == NULL) {
2756 goto error;
2757 }
2758 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2759 Py_DECREF(ann_dict);
2760 if (err != 0) {
2761 goto error;
2762 }
2763 }
2764 else {
2765 Py_DECREF(ann_dict);
2766 }
2767 }
2768 DISPATCH();
2769 }
2770
Benjamin Petersonddd19492018-09-16 22:38:02 -07002771 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002772 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002773 PyObject *map;
2774 PyObject *keys = TOP();
2775 if (!PyTuple_CheckExact(keys) ||
2776 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002777 _PyErr_SetString(tstate, PyExc_SystemError,
2778 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002779 goto error;
2780 }
2781 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2782 if (map == NULL) {
2783 goto error;
2784 }
2785 for (i = oparg; i > 0; i--) {
2786 int err;
2787 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2788 PyObject *value = PEEK(i + 1);
2789 err = PyDict_SetItem(map, key, value);
2790 if (err != 0) {
2791 Py_DECREF(map);
2792 goto error;
2793 }
2794 }
2795
2796 Py_DECREF(POP());
2797 while (oparg--) {
2798 Py_DECREF(POP());
2799 }
2800 PUSH(map);
2801 DISPATCH();
2802 }
2803
Mark Shannon8a4cd702020-01-27 09:57:45 +00002804 case TARGET(DICT_UPDATE): {
2805 PyObject *update = POP();
2806 PyObject *dict = PEEK(oparg);
2807 if (PyDict_Update(dict, update) < 0) {
2808 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
2809 _PyErr_Format(tstate, PyExc_TypeError,
2810 "'%.200s' object is not a mapping",
2811 update->ob_type->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002812 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002813 Py_DECREF(update);
2814 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002815 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002816 Py_DECREF(update);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002817 DISPATCH();
2818 }
2819
Mark Shannon8a4cd702020-01-27 09:57:45 +00002820 case TARGET(DICT_MERGE): {
2821 PyObject *update = POP();
2822 PyObject *dict = PEEK(oparg);
2823
2824 if (_PyDict_MergeEx(dict, update, 2) < 0) {
2825 format_kwargs_error(tstate, PEEK(2 + oparg), update);
2826 Py_DECREF(update);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002827 goto error;
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002828 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002829 Py_DECREF(update);
Brandt Bucherf185a732019-09-28 17:12:49 -07002830 PREDICT(CALL_FUNCTION_EX);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002831 DISPATCH();
2832 }
2833
Benjamin Petersonddd19492018-09-16 22:38:02 -07002834 case TARGET(MAP_ADD): {
Jörn Heisslerc8a35412019-06-22 16:40:55 +02002835 PyObject *value = TOP();
2836 PyObject *key = SECOND();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002837 PyObject *map;
2838 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002839 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002840 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002841 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002842 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002843 Py_DECREF(value);
2844 Py_DECREF(key);
2845 if (err != 0)
2846 goto error;
2847 PREDICT(JUMP_ABSOLUTE);
2848 DISPATCH();
2849 }
2850
Benjamin Petersonddd19492018-09-16 22:38:02 -07002851 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002852 PyObject *name = GETITEM(names, oparg);
2853 PyObject *owner = TOP();
2854 PyObject *res = PyObject_GetAttr(owner, name);
2855 Py_DECREF(owner);
2856 SET_TOP(res);
2857 if (res == NULL)
2858 goto error;
2859 DISPATCH();
2860 }
2861
Benjamin Petersonddd19492018-09-16 22:38:02 -07002862 case TARGET(COMPARE_OP): {
Mark Shannon9af0e472020-01-14 10:12:45 +00002863 assert(oparg <= Py_GE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002864 PyObject *right = POP();
2865 PyObject *left = TOP();
Mark Shannon9af0e472020-01-14 10:12:45 +00002866 PyObject *res = PyObject_RichCompare(left, right, oparg);
2867 SET_TOP(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002868 Py_DECREF(left);
2869 Py_DECREF(right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002870 if (res == NULL)
2871 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002872 PREDICT(POP_JUMP_IF_FALSE);
2873 PREDICT(POP_JUMP_IF_TRUE);
2874 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002875 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002876
Mark Shannon9af0e472020-01-14 10:12:45 +00002877 case TARGET(IS_OP): {
2878 PyObject *right = POP();
2879 PyObject *left = TOP();
2880 int res = (left == right)^oparg;
2881 PyObject *b = res ? Py_True : Py_False;
2882 Py_INCREF(b);
2883 SET_TOP(b);
2884 Py_DECREF(left);
2885 Py_DECREF(right);
2886 PREDICT(POP_JUMP_IF_FALSE);
2887 PREDICT(POP_JUMP_IF_TRUE);
2888 FAST_DISPATCH();
2889 }
2890
2891 case TARGET(CONTAINS_OP): {
2892 PyObject *right = POP();
2893 PyObject *left = POP();
2894 int res = PySequence_Contains(right, left);
2895 Py_DECREF(left);
2896 Py_DECREF(right);
2897 if (res < 0) {
2898 goto error;
2899 }
2900 PyObject *b = (res^oparg) ? Py_True : Py_False;
2901 Py_INCREF(b);
2902 PUSH(b);
2903 PREDICT(POP_JUMP_IF_FALSE);
2904 PREDICT(POP_JUMP_IF_TRUE);
2905 FAST_DISPATCH();
2906 }
2907
2908#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
2909 "BaseException is not allowed"
2910
2911 case TARGET(JUMP_IF_NOT_EXC_MATCH): {
2912 PyObject *right = POP();
2913 PyObject *left = POP();
2914 if (PyTuple_Check(right)) {
2915 Py_ssize_t i, length;
2916 length = PyTuple_GET_SIZE(right);
2917 for (i = 0; i < length; i++) {
2918 PyObject *exc = PyTuple_GET_ITEM(right, i);
2919 if (!PyExceptionClass_Check(exc)) {
2920 _PyErr_SetString(tstate, PyExc_TypeError,
2921 CANNOT_CATCH_MSG);
2922 Py_DECREF(left);
2923 Py_DECREF(right);
2924 goto error;
2925 }
2926 }
2927 }
2928 else {
2929 if (!PyExceptionClass_Check(right)) {
2930 _PyErr_SetString(tstate, PyExc_TypeError,
2931 CANNOT_CATCH_MSG);
2932 Py_DECREF(left);
2933 Py_DECREF(right);
2934 goto error;
2935 }
2936 }
2937 int res = PyErr_GivenExceptionMatches(left, right);
2938 Py_DECREF(left);
2939 Py_DECREF(right);
2940 if (res > 0) {
2941 /* Exception matches -- Do nothing */;
2942 }
2943 else if (res == 0) {
2944 JUMPTO(oparg);
2945 }
2946 else {
2947 goto error;
2948 }
2949 DISPATCH();
2950 }
2951
Benjamin Petersonddd19492018-09-16 22:38:02 -07002952 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002953 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002954 PyObject *fromlist = POP();
2955 PyObject *level = TOP();
2956 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02002957 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002958 Py_DECREF(level);
2959 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002960 SET_TOP(res);
2961 if (res == NULL)
2962 goto error;
2963 DISPATCH();
2964 }
2965
Benjamin Petersonddd19492018-09-16 22:38:02 -07002966 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002967 PyObject *from = POP(), *locals;
2968 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002969 if (PyFrame_FastToLocalsWithError(f) < 0) {
2970 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002971 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002972 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002973
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002974 locals = f->f_locals;
2975 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002976 _PyErr_SetString(tstate, PyExc_SystemError,
2977 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002978 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002979 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002980 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002981 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002982 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002983 Py_DECREF(from);
2984 if (err != 0)
2985 goto error;
2986 DISPATCH();
2987 }
Guido van Rossum25831651993-05-19 14:50:45 +00002988
Benjamin Petersonddd19492018-09-16 22:38:02 -07002989 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002990 PyObject *name = GETITEM(names, oparg);
2991 PyObject *from = TOP();
2992 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02002993 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002994 PUSH(res);
2995 if (res == NULL)
2996 goto error;
2997 DISPATCH();
2998 }
Thomas Wouters52152252000-08-17 22:55:00 +00002999
Benjamin Petersonddd19492018-09-16 22:38:02 -07003000 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003001 JUMPBY(oparg);
3002 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003003 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003004
Benjamin Petersonddd19492018-09-16 22:38:02 -07003005 case TARGET(POP_JUMP_IF_FALSE): {
3006 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003007 PyObject *cond = POP();
3008 int err;
3009 if (cond == Py_True) {
3010 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003011 FAST_DISPATCH();
3012 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003013 if (cond == Py_False) {
3014 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003015 JUMPTO(oparg);
3016 FAST_DISPATCH();
3017 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003018 err = PyObject_IsTrue(cond);
3019 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003020 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003021 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003022 else if (err == 0)
3023 JUMPTO(oparg);
3024 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003025 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003026 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003027 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003028
Benjamin Petersonddd19492018-09-16 22:38:02 -07003029 case TARGET(POP_JUMP_IF_TRUE): {
3030 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003031 PyObject *cond = POP();
3032 int err;
3033 if (cond == Py_False) {
3034 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003035 FAST_DISPATCH();
3036 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003037 if (cond == Py_True) {
3038 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003039 JUMPTO(oparg);
3040 FAST_DISPATCH();
3041 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003042 err = PyObject_IsTrue(cond);
3043 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003044 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003045 JUMPTO(oparg);
3046 }
3047 else if (err == 0)
3048 ;
3049 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003050 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003051 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003052 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003053
Benjamin Petersonddd19492018-09-16 22:38:02 -07003054 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003055 PyObject *cond = TOP();
3056 int err;
3057 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003058 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003059 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003060 FAST_DISPATCH();
3061 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003062 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003063 JUMPTO(oparg);
3064 FAST_DISPATCH();
3065 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003066 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003067 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003068 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003069 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003070 }
3071 else if (err == 0)
3072 JUMPTO(oparg);
3073 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003074 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003075 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003076 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003077
Benjamin Petersonddd19492018-09-16 22:38:02 -07003078 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003079 PyObject *cond = TOP();
3080 int err;
3081 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003082 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003083 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003084 FAST_DISPATCH();
3085 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003086 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003087 JUMPTO(oparg);
3088 FAST_DISPATCH();
3089 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003090 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003091 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003092 JUMPTO(oparg);
3093 }
3094 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003095 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003096 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003097 }
3098 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003099 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003100 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003101 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003102
Benjamin Petersonddd19492018-09-16 22:38:02 -07003103 case TARGET(JUMP_ABSOLUTE): {
3104 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003105 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003106#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003107 /* Enabling this path speeds-up all while and for-loops by bypassing
3108 the per-loop checks for signals. By default, this should be turned-off
3109 because it prevents detection of a control-break in tight loops like
3110 "while 1: pass". Compile with this option turned-on when you need
3111 the speed-up and do not need break checking inside tight loops (ones
3112 that contain only instructions ending with FAST_DISPATCH).
3113 */
3114 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003115#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003116 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003117#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003118 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003119
Benjamin Petersonddd19492018-09-16 22:38:02 -07003120 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003121 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003122 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003123 PyObject *iter = PyObject_GetIter(iterable);
3124 Py_DECREF(iterable);
3125 SET_TOP(iter);
3126 if (iter == NULL)
3127 goto error;
3128 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003129 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003130 DISPATCH();
3131 }
3132
Benjamin Petersonddd19492018-09-16 22:38:02 -07003133 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04003134 /* before: [obj]; after [getiter(obj)] */
3135 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003136 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003137 if (PyCoro_CheckExact(iterable)) {
3138 /* `iterable` is a coroutine */
3139 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3140 /* and it is used in a 'yield from' expression of a
3141 regular generator. */
3142 Py_DECREF(iterable);
3143 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003144 _PyErr_SetString(tstate, PyExc_TypeError,
3145 "cannot 'yield from' a coroutine object "
3146 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04003147 goto error;
3148 }
3149 }
3150 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003151 /* `iterable` is not a generator. */
3152 iter = PyObject_GetIter(iterable);
3153 Py_DECREF(iterable);
3154 SET_TOP(iter);
3155 if (iter == NULL)
3156 goto error;
3157 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003158 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003159 DISPATCH();
3160 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003161
Benjamin Petersonddd19492018-09-16 22:38:02 -07003162 case TARGET(FOR_ITER): {
3163 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003164 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003165 PyObject *iter = TOP();
3166 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
3167 if (next != NULL) {
3168 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003169 PREDICT(STORE_FAST);
3170 PREDICT(UNPACK_SEQUENCE);
3171 DISPATCH();
3172 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003173 if (_PyErr_Occurred(tstate)) {
3174 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003175 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003176 }
3177 else if (tstate->c_tracefunc != NULL) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003178 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Victor Stinner438a12d2019-05-24 17:01:38 +02003179 }
3180 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003181 }
3182 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003183 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003184 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003185 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003186 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003187 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003188 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003189
Benjamin Petersonddd19492018-09-16 22:38:02 -07003190 case TARGET(SETUP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003191 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003192 STACK_LEVEL());
3193 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003194 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003195
Benjamin Petersonddd19492018-09-16 22:38:02 -07003196 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003197 _Py_IDENTIFIER(__aenter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003198 _Py_IDENTIFIER(__aexit__);
Yury Selivanov75445082015-05-11 22:57:16 -04003199 PyObject *mgr = TOP();
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003200 PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04003201 PyObject *res;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003202 if (enter == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003203 goto error;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003204 }
3205 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__);
3206 if (exit == NULL) {
3207 Py_DECREF(enter);
3208 goto error;
3209 }
Yury Selivanov75445082015-05-11 22:57:16 -04003210 SET_TOP(exit);
Yury Selivanov75445082015-05-11 22:57:16 -04003211 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003212 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003213 Py_DECREF(enter);
3214 if (res == NULL)
3215 goto error;
3216 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003217 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003218 DISPATCH();
3219 }
3220
Benjamin Petersonddd19492018-09-16 22:38:02 -07003221 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003222 PyObject *res = POP();
3223 /* Setup the finally block before pushing the result
3224 of __aenter__ on the stack. */
3225 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3226 STACK_LEVEL());
3227 PUSH(res);
3228 DISPATCH();
3229 }
3230
Benjamin Petersonddd19492018-09-16 22:38:02 -07003231 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003232 _Py_IDENTIFIER(__enter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003233 _Py_IDENTIFIER(__exit__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003234 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003235 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003236 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003237 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08003238 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003239 }
3240 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003241 if (exit == NULL) {
3242 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003243 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003244 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003245 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003246 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003247 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003248 Py_DECREF(enter);
3249 if (res == NULL)
3250 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003251 /* Setup the finally block before pushing the result
3252 of __enter__ on the stack. */
3253 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3254 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003255
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003256 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003257 DISPATCH();
3258 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003259
Mark Shannonfee55262019-11-21 09:11:43 +00003260 case TARGET(WITH_EXCEPT_START): {
3261 /* At the top of the stack are 7 values:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003262 - (TOP, SECOND, THIRD) = exc_info()
Mark Shannonfee55262019-11-21 09:11:43 +00003263 - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER
3264 - SEVENTH: the context.__exit__ bound method
3265 We call SEVENTH(TOP, SECOND, THIRD).
3266 Then we push again the TOP exception and the __exit__
3267 return value.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003268 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003269 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003270 PyObject *exc, *val, *tb, *res;
3271
Victor Stinner842cfff2016-12-01 14:45:31 +01003272 exc = TOP();
Mark Shannonfee55262019-11-21 09:11:43 +00003273 val = SECOND();
3274 tb = THIRD();
3275 assert(exc != Py_None);
3276 assert(!PyLong_Check(exc));
3277 exit_func = PEEK(7);
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003278 PyObject *stack[4] = {NULL, exc, val, tb};
3279 res = _PyObject_Vectorcall(exit_func, stack + 1,
3280 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003281 if (res == NULL)
3282 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003283
Yury Selivanov75445082015-05-11 22:57:16 -04003284 PUSH(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003285 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003286 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003287
Benjamin Petersonddd19492018-09-16 22:38:02 -07003288 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10003289 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003290 PyObject *name = GETITEM(names, oparg);
3291 PyObject *obj = TOP();
3292 PyObject *meth = NULL;
3293
3294 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3295
Yury Selivanovf2392132016-12-13 19:03:51 -05003296 if (meth == NULL) {
3297 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003298 goto error;
3299 }
3300
3301 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003302 /* We can bypass temporary bound method object.
3303 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003304
INADA Naoki015bce62017-01-16 17:23:30 +09003305 meth | self | arg1 | ... | argN
3306 */
3307 SET_TOP(meth);
3308 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003309 }
3310 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003311 /* meth is not an unbound method (but a regular attr, or
3312 something was returned by a descriptor protocol). Set
3313 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003314 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003315
3316 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003317 */
INADA Naoki015bce62017-01-16 17:23:30 +09003318 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003319 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003320 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003321 }
3322 DISPATCH();
3323 }
3324
Benjamin Petersonddd19492018-09-16 22:38:02 -07003325 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003326 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003327 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003328
3329 sp = stack_pointer;
3330
INADA Naoki015bce62017-01-16 17:23:30 +09003331 meth = PEEK(oparg + 2);
3332 if (meth == NULL) {
3333 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3334 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003335
3336 Stack layout:
3337
INADA Naoki015bce62017-01-16 17:23:30 +09003338 ... | NULL | callable | arg1 | ... | argN
3339 ^- TOP()
3340 ^- (-oparg)
3341 ^- (-oparg-1)
3342 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003343
Ville Skyttä49b27342017-08-03 09:00:59 +03003344 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003345 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003346 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003347 res = call_function(tstate, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003348 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003349 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003350 }
3351 else {
3352 /* This is a method call. Stack layout:
3353
INADA Naoki015bce62017-01-16 17:23:30 +09003354 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003355 ^- TOP()
3356 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003357 ^- (-oparg-1)
3358 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003359
INADA Naoki015bce62017-01-16 17:23:30 +09003360 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003361 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003362 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003363 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003364 res = call_function(tstate, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003365 stack_pointer = sp;
3366 }
3367
3368 PUSH(res);
3369 if (res == NULL)
3370 goto error;
3371 DISPATCH();
3372 }
3373
Benjamin Petersonddd19492018-09-16 22:38:02 -07003374 case TARGET(CALL_FUNCTION): {
3375 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003376 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003377 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003378 res = call_function(tstate, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003379 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003380 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003381 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003382 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003383 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003384 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003385 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003386
Benjamin Petersonddd19492018-09-16 22:38:02 -07003387 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003388 PyObject **sp, *res, *names;
3389
3390 names = POP();
Jeroen Demeyer05677862019-08-16 12:41:27 +02003391 assert(PyTuple_Check(names));
3392 assert(PyTuple_GET_SIZE(names) <= oparg);
3393 /* We assume without checking that names contains only strings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003394 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003395 res = call_function(tstate, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003396 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003397 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003398 Py_DECREF(names);
3399
3400 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003401 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003402 }
3403 DISPATCH();
3404 }
3405
Benjamin Petersonddd19492018-09-16 22:38:02 -07003406 case TARGET(CALL_FUNCTION_EX): {
Brandt Bucherf185a732019-09-28 17:12:49 -07003407 PREDICTED(CALL_FUNCTION_EX);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003408 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003409 if (oparg & 0x01) {
3410 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003411 if (!PyDict_CheckExact(kwargs)) {
3412 PyObject *d = PyDict_New();
3413 if (d == NULL)
3414 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003415 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003416 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02003417 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003418 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003419 goto error;
3420 }
3421 Py_DECREF(kwargs);
3422 kwargs = d;
3423 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003424 assert(PyDict_CheckExact(kwargs));
3425 }
3426 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003427 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003428 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003429 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003430 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003431 goto error;
3432 }
3433 Py_SETREF(callargs, PySequence_Tuple(callargs));
3434 if (callargs == NULL) {
3435 goto error;
3436 }
3437 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003438 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003439
Victor Stinner09532fe2019-05-10 23:39:09 +02003440 result = do_call_core(tstate, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003441 Py_DECREF(func);
3442 Py_DECREF(callargs);
3443 Py_XDECREF(kwargs);
3444
3445 SET_TOP(result);
3446 if (result == NULL) {
3447 goto error;
3448 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003449 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003450 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003451
Benjamin Petersonddd19492018-09-16 22:38:02 -07003452 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003453 PyObject *qualname = POP();
3454 PyObject *codeobj = POP();
3455 PyFunctionObject *func = (PyFunctionObject *)
3456 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003457
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003458 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003459 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003460 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003461 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003462 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003463
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003464 if (oparg & 0x08) {
3465 assert(PyTuple_CheckExact(TOP()));
3466 func ->func_closure = POP();
3467 }
3468 if (oparg & 0x04) {
3469 assert(PyDict_CheckExact(TOP()));
3470 func->func_annotations = POP();
3471 }
3472 if (oparg & 0x02) {
3473 assert(PyDict_CheckExact(TOP()));
3474 func->func_kwdefaults = POP();
3475 }
3476 if (oparg & 0x01) {
3477 assert(PyTuple_CheckExact(TOP()));
3478 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003479 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003480
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003481 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003482 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003483 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003484
Benjamin Petersonddd19492018-09-16 22:38:02 -07003485 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003486 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003487 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003488 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003489 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003490 step = NULL;
3491 stop = POP();
3492 start = TOP();
3493 slice = PySlice_New(start, stop, step);
3494 Py_DECREF(start);
3495 Py_DECREF(stop);
3496 Py_XDECREF(step);
3497 SET_TOP(slice);
3498 if (slice == NULL)
3499 goto error;
3500 DISPATCH();
3501 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003502
Benjamin Petersonddd19492018-09-16 22:38:02 -07003503 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003504 /* Handles f-string value formatting. */
3505 PyObject *result;
3506 PyObject *fmt_spec;
3507 PyObject *value;
3508 PyObject *(*conv_fn)(PyObject *);
3509 int which_conversion = oparg & FVC_MASK;
3510 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3511
3512 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003513 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003514
3515 /* See if any conversion is specified. */
3516 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003517 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003518 case FVC_STR: conv_fn = PyObject_Str; break;
3519 case FVC_REPR: conv_fn = PyObject_Repr; break;
3520 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003521 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02003522 _PyErr_Format(tstate, PyExc_SystemError,
3523 "unexpected conversion flag %d",
3524 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003525 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003526 }
3527
3528 /* If there's a conversion function, call it and replace
3529 value with that result. Otherwise, just use value,
3530 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003531 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003532 result = conv_fn(value);
3533 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003534 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003535 Py_XDECREF(fmt_spec);
3536 goto error;
3537 }
3538 value = result;
3539 }
3540
3541 /* If value is a unicode object, and there's no fmt_spec,
3542 then we know the result of format(value) is value
3543 itself. In that case, skip calling format(). I plan to
3544 move this optimization in to PyObject_Format()
3545 itself. */
3546 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3547 /* Do nothing, just transfer ownership to result. */
3548 result = value;
3549 } else {
3550 /* Actually call format(). */
3551 result = PyObject_Format(value, fmt_spec);
3552 Py_DECREF(value);
3553 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003554 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003555 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003556 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003557 }
3558
Eric V. Smith135d5f42016-02-05 18:23:08 -05003559 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003560 DISPATCH();
3561 }
3562
Benjamin Petersonddd19492018-09-16 22:38:02 -07003563 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003564 int oldoparg = oparg;
3565 NEXTOPARG();
3566 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003567 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003568 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003569
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003570
Antoine Pitrou042b1282010-08-13 21:15:58 +00003571#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003572 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003573#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003574 default:
3575 fprintf(stderr,
3576 "XXX lineno: %d, opcode: %d\n",
3577 PyFrame_GetLineNumber(f),
3578 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02003579 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003580 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003582 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003583
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003584 /* This should never be reached. Every opcode should end with DISPATCH()
3585 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003586 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003587
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003588error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003589 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003590#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02003591 if (!_PyErr_Occurred(tstate)) {
3592 _PyErr_SetString(tstate, PyExc_SystemError,
3593 "error return without exception set");
3594 }
Victor Stinner365b6932013-07-12 00:11:58 +02003595#else
Victor Stinner438a12d2019-05-24 17:01:38 +02003596 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02003597#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003598
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003599 /* Log traceback info. */
3600 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003601
Benjamin Peterson51f46162013-01-23 08:38:47 -05003602 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003603 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3604 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003605
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003606exception_unwind:
3607 /* Unwind stacks if an exception occurred */
3608 while (f->f_iblock > 0) {
3609 /* Pop the current block. */
3610 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003612 if (b->b_type == EXCEPT_HANDLER) {
3613 UNWIND_EXCEPT_HANDLER(b);
3614 continue;
3615 }
3616 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003617 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003618 PyObject *exc, *val, *tb;
3619 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003620 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003621 /* Beware, this invalidates all b->b_* fields */
3622 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003623 PUSH(exc_info->exc_traceback);
3624 PUSH(exc_info->exc_value);
3625 if (exc_info->exc_type != NULL) {
3626 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003627 }
3628 else {
3629 Py_INCREF(Py_None);
3630 PUSH(Py_None);
3631 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003632 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003633 /* Make the raw exception data
3634 available to the handler,
3635 so a program can emulate the
3636 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02003637 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003638 if (tb != NULL)
3639 PyException_SetTraceback(val, tb);
3640 else
3641 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003642 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003643 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003644 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003645 exc_info->exc_value = val;
3646 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003647 if (tb == NULL)
3648 tb = Py_None;
3649 Py_INCREF(tb);
3650 PUSH(tb);
3651 PUSH(val);
3652 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003653 JUMPTO(handler);
Pablo Galindo4c53e632020-01-10 09:24:22 +00003654 if (_Py_TracingPossible(ceval)) {
3655 int needs_new_execution_window = (f->f_lasti < instr_lb || f->f_lasti >= instr_ub);
3656 int needs_line_update = (f->f_lasti == instr_lb || f->f_lasti < instr_prev);
3657 /* Make sure that we trace line after exception if we are in a new execution
3658 * window or we don't need a line update and we are not in the first instruction
3659 * of the line. */
3660 if (needs_new_execution_window || (!needs_line_update && instr_lb > 0)) {
3661 instr_prev = INT_MAX;
3662 }
Mark Shannonfee55262019-11-21 09:11:43 +00003663 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003664 /* Resume normal execution */
3665 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003666 }
3667 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003668
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003669 /* End the loop as we still have an error */
3670 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003671 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003672
Pablo Galindof00828a2019-05-09 16:52:02 +01003673 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003674 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01003675
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003676 /* Pop remaining stack entries. */
3677 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003678 PyObject *o = POP();
3679 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003680 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003681
Mark Shannone7c9f4a2020-01-13 12:51:26 +00003682exiting:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003683 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003684 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003685 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3686 tstate, f, PyTrace_RETURN, retval)) {
3687 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003688 }
3689 }
3690 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003691 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3692 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003693 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003694 }
3695 }
3696 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003698 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003699exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003700 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3701 dtrace_function_return(f);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01003702 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrou58720d62013-08-05 23:26:40 +02003703 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003704 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003705
Victor Stinner17269092019-11-05 01:22:12 +01003706 return _Py_CheckFunctionResult(tstate, NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003707}
3708
Benjamin Petersonb204a422011-06-05 22:04:07 -05003709static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003710format_missing(PyThreadState *tstate, const char *kind,
3711 PyCodeObject *co, PyObject *names)
Benjamin Petersone109c702011-06-24 09:37:26 -05003712{
3713 int err;
3714 Py_ssize_t len = PyList_GET_SIZE(names);
3715 PyObject *name_str, *comma, *tail, *tmp;
3716
3717 assert(PyList_CheckExact(names));
3718 assert(len >= 1);
3719 /* Deal with the joys of natural language. */
3720 switch (len) {
3721 case 1:
3722 name_str = PyList_GET_ITEM(names, 0);
3723 Py_INCREF(name_str);
3724 break;
3725 case 2:
3726 name_str = PyUnicode_FromFormat("%U and %U",
3727 PyList_GET_ITEM(names, len - 2),
3728 PyList_GET_ITEM(names, len - 1));
3729 break;
3730 default:
3731 tail = PyUnicode_FromFormat(", %U, and %U",
3732 PyList_GET_ITEM(names, len - 2),
3733 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003734 if (tail == NULL)
3735 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003736 /* Chop off the last two objects in the list. This shouldn't actually
3737 fail, but we can't be too careful. */
3738 err = PyList_SetSlice(names, len - 2, len, NULL);
3739 if (err == -1) {
3740 Py_DECREF(tail);
3741 return;
3742 }
3743 /* Stitch everything up into a nice comma-separated list. */
3744 comma = PyUnicode_FromString(", ");
3745 if (comma == NULL) {
3746 Py_DECREF(tail);
3747 return;
3748 }
3749 tmp = PyUnicode_Join(comma, names);
3750 Py_DECREF(comma);
3751 if (tmp == NULL) {
3752 Py_DECREF(tail);
3753 return;
3754 }
3755 name_str = PyUnicode_Concat(tmp, tail);
3756 Py_DECREF(tmp);
3757 Py_DECREF(tail);
3758 break;
3759 }
3760 if (name_str == NULL)
3761 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02003762 _PyErr_Format(tstate, PyExc_TypeError,
3763 "%U() missing %i required %s argument%s: %U",
3764 co->co_name,
3765 len,
3766 kind,
3767 len == 1 ? "" : "s",
3768 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05003769 Py_DECREF(name_str);
3770}
3771
3772static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003773missing_arguments(PyThreadState *tstate, PyCodeObject *co,
3774 Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003775 PyObject **fastlocals)
3776{
Victor Stinner74319ae2016-08-25 00:04:09 +02003777 Py_ssize_t i, j = 0;
3778 Py_ssize_t start, end;
3779 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003780 const char *kind = positional ? "positional" : "keyword-only";
3781 PyObject *missing_names;
3782
3783 /* Compute the names of the arguments that are missing. */
3784 missing_names = PyList_New(missing);
3785 if (missing_names == NULL)
3786 return;
3787 if (positional) {
3788 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01003789 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003790 }
3791 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003792 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003793 end = start + co->co_kwonlyargcount;
3794 }
3795 for (i = start; i < end; i++) {
3796 if (GETLOCAL(i) == NULL) {
3797 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3798 PyObject *name = PyObject_Repr(raw);
3799 if (name == NULL) {
3800 Py_DECREF(missing_names);
3801 return;
3802 }
3803 PyList_SET_ITEM(missing_names, j++, name);
3804 }
3805 }
3806 assert(j == missing);
Victor Stinner438a12d2019-05-24 17:01:38 +02003807 format_missing(tstate, kind, co, missing_names);
Benjamin Petersone109c702011-06-24 09:37:26 -05003808 Py_DECREF(missing_names);
3809}
3810
3811static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003812too_many_positional(PyThreadState *tstate, PyCodeObject *co,
3813 Py_ssize_t given, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003814 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003815{
3816 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003817 Py_ssize_t kwonly_given = 0;
3818 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003819 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003820 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003821
Benjamin Petersone109c702011-06-24 09:37:26 -05003822 assert((co->co_flags & CO_VARARGS) == 0);
3823 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01003824 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003825 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003826 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003827 }
3828 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003829 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01003830 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003831 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01003832 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003833 }
3834 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003835 plural = (co_argcount != 1);
3836 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003837 }
3838 if (sig == NULL)
3839 return;
3840 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003841 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3842 kwonly_sig = PyUnicode_FromFormat(format,
3843 given != 1 ? "s" : "",
3844 kwonly_given,
3845 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003846 if (kwonly_sig == NULL) {
3847 Py_DECREF(sig);
3848 return;
3849 }
3850 }
3851 else {
3852 /* This will not fail. */
3853 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003854 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003855 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003856 _PyErr_Format(tstate, PyExc_TypeError,
3857 "%U() takes %U positional argument%s but %zd%U %s given",
3858 co->co_name,
3859 sig,
3860 plural ? "s" : "",
3861 given,
3862 kwonly_sig,
3863 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003864 Py_DECREF(sig);
3865 Py_DECREF(kwonly_sig);
3866}
3867
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003868static int
Victor Stinner438a12d2019-05-24 17:01:38 +02003869positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
3870 Py_ssize_t kwcount, PyObject* const* kwnames)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003871{
3872 int posonly_conflicts = 0;
3873 PyObject* posonly_names = PyList_New(0);
3874
3875 for(int k=0; k < co->co_posonlyargcount; k++){
3876 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
3877
3878 for (int k2=0; k2<kwcount; k2++){
3879 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
3880 PyObject* kwname = kwnames[k2];
3881 if (kwname == posonly_name){
3882 if(PyList_Append(posonly_names, kwname) != 0) {
3883 goto fail;
3884 }
3885 posonly_conflicts++;
3886 continue;
3887 }
3888
3889 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
3890
3891 if ( cmp > 0) {
3892 if(PyList_Append(posonly_names, kwname) != 0) {
3893 goto fail;
3894 }
3895 posonly_conflicts++;
3896 } else if (cmp < 0) {
3897 goto fail;
3898 }
3899
3900 }
3901 }
3902 if (posonly_conflicts) {
3903 PyObject* comma = PyUnicode_FromString(", ");
3904 if (comma == NULL) {
3905 goto fail;
3906 }
3907 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
3908 Py_DECREF(comma);
3909 if (error_names == NULL) {
3910 goto fail;
3911 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003912 _PyErr_Format(tstate, PyExc_TypeError,
3913 "%U() got some positional-only arguments passed"
3914 " as keyword arguments: '%U'",
3915 co->co_name, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003916 Py_DECREF(error_names);
3917 goto fail;
3918 }
3919
3920 Py_DECREF(posonly_names);
3921 return 0;
3922
3923fail:
3924 Py_XDECREF(posonly_names);
3925 return 1;
3926
3927}
3928
Guido van Rossumc2e20742006-02-27 22:32:47 +00003929/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02003930 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003931 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003932
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003933PyObject *
Victor Stinnerb5e170f2019-11-16 01:03:22 +01003934_PyEval_EvalCode(PyThreadState *tstate,
3935 PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003936 PyObject *const *args, Py_ssize_t argcount,
3937 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003938 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003939 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003940 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003941 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003942{
Victor Stinnerb5e170f2019-11-16 01:03:22 +01003943 assert(tstate != NULL);
3944
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003945 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003946 PyFrameObject *f;
3947 PyObject *retval = NULL;
3948 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003949 PyObject *x, *u;
Pablo Galindocd74e662019-06-01 18:08:04 +01003950 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003951 Py_ssize_t i, j, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003952 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003953
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003954 if (globals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003955 _PyErr_SetString(tstate, PyExc_SystemError,
3956 "PyEval_EvalCodeEx: NULL globals");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003957 return NULL;
3958 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003959
Victor Stinnerc7020012016-08-16 23:40:29 +02003960 /* Create the frame */
INADA Naoki5a625d02016-12-24 20:19:08 +09003961 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003962 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003963 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003964 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003965 fastlocals = f->f_localsplus;
3966 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003967
Victor Stinnerc7020012016-08-16 23:40:29 +02003968 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003969 if (co->co_flags & CO_VARKEYWORDS) {
3970 kwdict = PyDict_New();
3971 if (kwdict == NULL)
3972 goto fail;
3973 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003974 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003975 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003976 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003977 SETLOCAL(i, kwdict);
3978 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003979 else {
3980 kwdict = NULL;
3981 }
3982
Pablo Galindocd74e662019-06-01 18:08:04 +01003983 /* Copy all positional arguments into local variables */
3984 if (argcount > co->co_argcount) {
3985 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003986 }
3987 else {
3988 n = argcount;
3989 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003990 for (j = 0; j < n; j++) {
3991 x = args[j];
3992 Py_INCREF(x);
3993 SETLOCAL(j, x);
3994 }
3995
Victor Stinnerc7020012016-08-16 23:40:29 +02003996 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003997 if (co->co_flags & CO_VARARGS) {
Sergey Fedoseev234531b2019-02-25 21:59:12 +05003998 u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003999 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004000 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004001 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004002 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004003 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004004
Serhiy Storchakab7281052016-09-12 00:52:40 +03004005 /* Handle keyword arguments passed as two strided arrays */
4006 kwcount *= kwstep;
4007 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004008 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03004009 PyObject *keyword = kwnames[i];
4010 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02004011 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004012
Benjamin Petersonb204a422011-06-05 22:04:07 -05004013 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004014 _PyErr_Format(tstate, PyExc_TypeError,
4015 "%U() keywords must be strings",
4016 co->co_name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004017 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004018 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004019
Benjamin Petersonb204a422011-06-05 22:04:07 -05004020 /* Speed hack: do raw pointer compares. As names are
4021 normally interned this should almost always hit. */
4022 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004023 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004024 PyObject *name = co_varnames[j];
4025 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004026 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004027 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004028 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004029
Benjamin Petersonb204a422011-06-05 22:04:07 -05004030 /* Slow fallback, just in case */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004031 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004032 PyObject *name = co_varnames[j];
4033 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
4034 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004035 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004036 }
4037 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004038 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004039 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004040 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004041
Victor Stinner231d1f32017-01-11 02:12:06 +01004042 assert(j >= total_args);
4043 if (kwdict == NULL) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004044
Victor Stinner438a12d2019-05-24 17:01:38 +02004045 if (co->co_posonlyargcount
4046 && positional_only_passed_as_keyword(tstate, co,
4047 kwcount, kwnames))
4048 {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004049 goto fail;
4050 }
4051
Victor Stinner438a12d2019-05-24 17:01:38 +02004052 _PyErr_Format(tstate, PyExc_TypeError,
4053 "%U() got an unexpected keyword argument '%S'",
4054 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004055 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004056 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004057
Christian Heimes0bd447f2013-07-20 14:48:10 +02004058 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4059 goto fail;
4060 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004061 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004062
Benjamin Petersonb204a422011-06-05 22:04:07 -05004063 kw_found:
4064 if (GETLOCAL(j) != NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004065 _PyErr_Format(tstate, PyExc_TypeError,
4066 "%U() got multiple values for argument '%S'",
4067 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004068 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004069 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004070 Py_INCREF(value);
4071 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004072 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004073
4074 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004075 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004076 too_many_positional(tstate, co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004077 goto fail;
4078 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004079
4080 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004081 if (argcount < co->co_argcount) {
4082 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004083 Py_ssize_t missing = 0;
4084 for (i = argcount; i < m; i++) {
4085 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004086 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004087 }
4088 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004089 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004090 missing_arguments(tstate, co, missing, defcount, fastlocals);
Benjamin Petersone109c702011-06-24 09:37:26 -05004091 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004092 }
4093 if (n > m)
4094 i = n - m;
4095 else
4096 i = 0;
4097 for (; i < defcount; i++) {
4098 if (GETLOCAL(m+i) == NULL) {
4099 PyObject *def = defs[i];
4100 Py_INCREF(def);
4101 SETLOCAL(m+i, def);
4102 }
4103 }
4104 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004105
4106 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004107 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004108 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004109 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004110 PyObject *name;
4111 if (GETLOCAL(i) != NULL)
4112 continue;
4113 name = PyTuple_GET_ITEM(co->co_varnames, i);
4114 if (kwdefs != NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004115 PyObject *def = PyDict_GetItemWithError(kwdefs, name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004116 if (def) {
4117 Py_INCREF(def);
4118 SETLOCAL(i, def);
4119 continue;
4120 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004121 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004122 goto fail;
4123 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004124 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004125 missing++;
4126 }
4127 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004128 missing_arguments(tstate, co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004129 goto fail;
4130 }
4131 }
4132
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004133 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004134 vars into frame. */
4135 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004136 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004137 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004138 /* Possibly account for the cell variable being an argument. */
4139 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004140 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004141 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004142 /* Clear the local copy. */
4143 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004144 }
4145 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004146 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004147 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004148 if (c == NULL)
4149 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004150 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004151 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004152
4153 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004154 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4155 PyObject *o = PyTuple_GET_ITEM(closure, i);
4156 Py_INCREF(o);
4157 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004158 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004159
Yury Selivanoveb636452016-09-08 22:01:51 -07004160 /* Handle generator/coroutine/asynchronous generator */
4161 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004162 PyObject *gen;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004163 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004165 /* Don't need to keep the reference to f_back, it will be set
4166 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004167 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004168
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004169 /* Create a new generator that owns the ready to run frame
4170 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004171 if (is_coro) {
4172 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004173 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4174 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004175 } else {
4176 gen = PyGen_NewWithQualName(f, name, qualname);
4177 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004178 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004179 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004180 }
INADA Naoki9c157762016-12-26 18:52:46 +09004181
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004182 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004183
Yury Selivanov75445082015-05-11 22:57:16 -04004184 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004185 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004186
Victor Stinnerb9e68122019-11-14 12:20:46 +01004187 retval = _PyEval_EvalFrame(tstate, f, 0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004188
Thomas Woutersce272b62007-09-19 21:19:28 +00004189fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004191 /* decref'ing the frame can cause __del__ methods to get invoked,
4192 which can call back into Python. While we're done with the
4193 current Python frame (f), the associated C stack is still in use,
4194 so recursion_depth must be boosted for the duration.
4195 */
INADA Naoki5a625d02016-12-24 20:19:08 +09004196 if (Py_REFCNT(f) > 1) {
4197 Py_DECREF(f);
4198 _PyObject_GC_TRACK(f);
4199 }
4200 else {
4201 ++tstate->recursion_depth;
4202 Py_DECREF(f);
4203 --tstate->recursion_depth;
4204 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004205 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004206}
4207
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004208
4209PyObject *
4210_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
4211 PyObject *const *args, Py_ssize_t argcount,
4212 PyObject *const *kwnames, PyObject *const *kwargs,
4213 Py_ssize_t kwcount, int kwstep,
4214 PyObject *const *defs, Py_ssize_t defcount,
4215 PyObject *kwdefs, PyObject *closure,
4216 PyObject *name, PyObject *qualname)
4217{
4218 PyThreadState *tstate = _PyThreadState_GET();
4219 return _PyEval_EvalCode(tstate, _co, globals, locals,
4220 args, argcount,
4221 kwnames, kwargs,
4222 kwcount, kwstep,
4223 defs, defcount,
4224 kwdefs, closure,
4225 name, qualname);
4226}
4227
Victor Stinner40ee3012014-06-16 15:59:28 +02004228PyObject *
4229PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004230 PyObject *const *args, int argcount,
4231 PyObject *const *kws, int kwcount,
4232 PyObject *const *defs, int defcount,
4233 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004234{
4235 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004236 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004237 kws, kws != NULL ? kws + 1 : NULL,
4238 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004239 defs, defcount,
4240 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004241 NULL, NULL);
4242}
Tim Peters5ca576e2001-06-18 22:08:13 +00004243
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004244static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004245special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004246{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004247 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004248 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02004249 if (res == NULL && !_PyErr_Occurred(tstate)) {
4250 _PyErr_SetObject(tstate, PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004251 return NULL;
4252 }
4253 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004254}
4255
4256
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004257/* Logic for the raise statement (too complicated for inlining).
4258 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004259static int
Victor Stinner09532fe2019-05-10 23:39:09 +02004260do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004261{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004262 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004264 if (exc == NULL) {
4265 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01004266 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004267 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004268 type = exc_info->exc_type;
4269 value = exc_info->exc_value;
4270 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004271 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004272 _PyErr_SetString(tstate, PyExc_RuntimeError,
4273 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004274 return 0;
4275 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004276 Py_XINCREF(type);
4277 Py_XINCREF(value);
4278 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02004279 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004280 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004281 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004283 /* We support the following forms of raise:
4284 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004285 raise <instance>
4286 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004288 if (PyExceptionClass_Check(exc)) {
4289 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004290 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004291 if (value == NULL)
4292 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004293 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004294 _PyErr_Format(tstate, PyExc_TypeError,
4295 "calling %R should have returned an instance of "
4296 "BaseException, not %R",
4297 type, Py_TYPE(value));
4298 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004299 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004300 }
4301 else if (PyExceptionInstance_Check(exc)) {
4302 value = exc;
4303 type = PyExceptionInstance_Class(exc);
4304 Py_INCREF(type);
4305 }
4306 else {
4307 /* Not something you can raise. You get an exception
4308 anyway, just not what you specified :-) */
4309 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02004310 _PyErr_SetString(tstate, PyExc_TypeError,
4311 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004312 goto raise_error;
4313 }
Collin Winter828f04a2007-08-31 00:04:24 +00004314
Serhiy Storchakac0191582016-09-27 11:37:10 +03004315 assert(type != NULL);
4316 assert(value != NULL);
4317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004318 if (cause) {
4319 PyObject *fixed_cause;
4320 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004321 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004322 if (fixed_cause == NULL)
4323 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004324 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004325 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004326 else if (PyExceptionInstance_Check(cause)) {
4327 fixed_cause = cause;
4328 }
4329 else if (cause == Py_None) {
4330 Py_DECREF(cause);
4331 fixed_cause = NULL;
4332 }
4333 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004334 _PyErr_SetString(tstate, PyExc_TypeError,
4335 "exception causes must derive from "
4336 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004337 goto raise_error;
4338 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004339 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004340 }
Collin Winter828f04a2007-08-31 00:04:24 +00004341
Victor Stinner438a12d2019-05-24 17:01:38 +02004342 _PyErr_SetObject(tstate, type, value);
Victor Stinner61f4db82020-01-28 03:37:45 +01004343 /* _PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004344 Py_DECREF(value);
4345 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004346 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004347
4348raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004349 Py_XDECREF(value);
4350 Py_XDECREF(type);
4351 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004352 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004353}
4354
Tim Petersd6d010b2001-06-21 02:49:55 +00004355/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004356 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004357
Guido van Rossum0368b722007-05-11 16:50:42 +00004358 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4359 with a variable target.
4360*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004361
Barry Warsawe42b18f1997-08-25 22:13:04 +00004362static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004363unpack_iterable(PyThreadState *tstate, PyObject *v,
4364 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004365{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004366 int i = 0, j = 0;
4367 Py_ssize_t ll = 0;
4368 PyObject *it; /* iter(v) */
4369 PyObject *w;
4370 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004372 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004374 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004375 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004376 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004377 v->ob_type->tp_iter == NULL && !PySequence_Check(v))
4378 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004379 _PyErr_Format(tstate, PyExc_TypeError,
4380 "cannot unpack non-iterable %.200s object",
4381 v->ob_type->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004382 }
4383 return 0;
4384 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004386 for (; i < argcnt; i++) {
4387 w = PyIter_Next(it);
4388 if (w == NULL) {
4389 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004390 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04004391 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004392 _PyErr_Format(tstate, PyExc_ValueError,
4393 "not enough values to unpack "
4394 "(expected %d, got %d)",
4395 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004396 }
4397 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004398 _PyErr_Format(tstate, PyExc_ValueError,
4399 "not enough values to unpack "
4400 "(expected at least %d, got %d)",
4401 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004402 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004403 }
4404 goto Error;
4405 }
4406 *--sp = w;
4407 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004409 if (argcntafter == -1) {
4410 /* We better have exhausted the iterator now. */
4411 w = PyIter_Next(it);
4412 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004413 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004414 goto Error;
4415 Py_DECREF(it);
4416 return 1;
4417 }
4418 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02004419 _PyErr_Format(tstate, PyExc_ValueError,
4420 "too many values to unpack (expected %d)",
4421 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004422 goto Error;
4423 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004425 l = PySequence_List(it);
4426 if (l == NULL)
4427 goto Error;
4428 *--sp = l;
4429 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004431 ll = PyList_GET_SIZE(l);
4432 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004433 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04004434 "not enough values to unpack (expected at least %d, got %zd)",
4435 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004436 goto Error;
4437 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004439 /* Pop the "after-variable" args off the list. */
4440 for (j = argcntafter; j > 0; j--, i++) {
4441 *--sp = PyList_GET_ITEM(l, ll - j);
4442 }
4443 /* Resize the list. */
4444 Py_SIZE(l) = ll - argcntafter;
4445 Py_DECREF(it);
4446 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004447
Tim Petersd6d010b2001-06-21 02:49:55 +00004448Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004449 for (; i > 0; i--, sp++)
4450 Py_DECREF(*sp);
4451 Py_XDECREF(it);
4452 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004453}
4454
4455
Guido van Rossum96a42c81992-01-12 02:29:51 +00004456#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004457static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004458prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004459{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004460 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02004461 if (PyObject_Print(v, stdout, 0) != 0) {
4462 /* Don't know what else to do */
4463 _PyErr_Clear(tstate);
4464 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004465 printf("\n");
4466 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004467}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004468#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004469
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004470static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004471call_exc_trace(Py_tracefunc func, PyObject *self,
4472 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004473{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004474 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004475 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004476 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004477 if (value == NULL) {
4478 value = Py_None;
4479 Py_INCREF(value);
4480 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004481 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01004482 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004483 arg = PyTuple_Pack(3, type, value, traceback);
4484 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004485 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004486 return;
4487 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004488 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004489 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02004490 if (err == 0) {
4491 _PyErr_Restore(tstate, type, value, orig_traceback);
4492 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004493 else {
4494 Py_XDECREF(type);
4495 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004496 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004497 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004498}
4499
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004500static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004501call_trace_protected(Py_tracefunc func, PyObject *obj,
4502 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004503 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004504{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004505 PyObject *type, *value, *traceback;
4506 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004507 _PyErr_Fetch(tstate, &type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004508 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004509 if (err == 0)
4510 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004511 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004512 return 0;
4513 }
4514 else {
4515 Py_XDECREF(type);
4516 Py_XDECREF(value);
4517 Py_XDECREF(traceback);
4518 return -1;
4519 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004520}
4521
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004522static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004523call_trace(Py_tracefunc func, PyObject *obj,
4524 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004525 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004526{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004527 int result;
4528 if (tstate->tracing)
4529 return 0;
4530 tstate->tracing++;
4531 tstate->use_tracing = 0;
4532 result = func(obj, frame, what, arg);
4533 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4534 || (tstate->c_profilefunc != NULL));
4535 tstate->tracing--;
4536 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004537}
4538
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004539PyObject *
4540_PyEval_CallTracing(PyObject *func, PyObject *args)
4541{
Victor Stinner50b48572018-11-01 01:51:40 +01004542 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004543 int save_tracing = tstate->tracing;
4544 int save_use_tracing = tstate->use_tracing;
4545 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004547 tstate->tracing = 0;
4548 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4549 || (tstate->c_profilefunc != NULL));
4550 result = PyObject_Call(func, args, NULL);
4551 tstate->tracing = save_tracing;
4552 tstate->use_tracing = save_use_tracing;
4553 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004554}
4555
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004556/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004557static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004558maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004559 PyThreadState *tstate, PyFrameObject *frame,
4560 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004561{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004562 int result = 0;
4563 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004565 /* If the last instruction executed isn't in the current
4566 instruction window, reset the window.
4567 */
4568 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4569 PyAddrPair bounds;
4570 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4571 &bounds);
4572 *instr_lb = bounds.ap_lower;
4573 *instr_ub = bounds.ap_upper;
4574 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004575 /* If the last instruction falls at the start of a line or if it
4576 represents a jump backwards, update the frame's line number and
4577 then call the trace function if we're tracing source lines.
4578 */
4579 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004580 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004581 if (frame->f_trace_lines) {
4582 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4583 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004584 }
George King20faa682017-10-18 17:44:22 -07004585 /* Always emit an opcode event if we're tracing all opcodes. */
4586 if (frame->f_trace_opcodes) {
4587 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4588 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004589 *instr_prev = frame->f_lasti;
4590 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004591}
4592
Fred Drake5755ce62001-06-27 19:19:46 +00004593void
4594PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004595{
Steve Dowerb82e17e2019-05-23 08:45:22 -07004596 if (PySys_Audit("sys.setprofile", NULL) < 0) {
4597 return;
4598 }
4599
Victor Stinner50b48572018-11-01 01:51:40 +01004600 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004601 PyObject *temp = tstate->c_profileobj;
4602 Py_XINCREF(arg);
4603 tstate->c_profilefunc = NULL;
4604 tstate->c_profileobj = NULL;
4605 /* Must make sure that tracing is not ignored if 'temp' is freed */
4606 tstate->use_tracing = tstate->c_tracefunc != NULL;
4607 Py_XDECREF(temp);
4608 tstate->c_profilefunc = func;
4609 tstate->c_profileobj = arg;
4610 /* Flag that tracing or profiling is turned on */
4611 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004612}
4613
4614void
4615PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4616{
Steve Dowerb82e17e2019-05-23 08:45:22 -07004617 if (PySys_Audit("sys.settrace", NULL) < 0) {
4618 return;
4619 }
4620
Victor Stinner09532fe2019-05-10 23:39:09 +02004621 _PyRuntimeState *runtime = &_PyRuntime;
4622 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004623 PyObject *temp = tstate->c_traceobj;
Victor Stinner09532fe2019-05-10 23:39:09 +02004624 runtime->ceval.tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004625 Py_XINCREF(arg);
4626 tstate->c_tracefunc = NULL;
4627 tstate->c_traceobj = NULL;
4628 /* Must make sure that profiling is not ignored if 'temp' is freed */
4629 tstate->use_tracing = tstate->c_profilefunc != NULL;
4630 Py_XDECREF(temp);
4631 tstate->c_tracefunc = func;
4632 tstate->c_traceobj = arg;
4633 /* Flag that tracing or profiling is turned on */
4634 tstate->use_tracing = ((func != NULL)
4635 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004636}
4637
Yury Selivanov75445082015-05-11 22:57:16 -04004638void
Victor Stinner838f2642019-06-13 22:41:23 +02004639_PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004640{
4641 assert(new_depth >= 0);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004642 tstate->coroutine_origin_tracking_depth = new_depth;
4643}
4644
4645int
4646_PyEval_GetCoroutineOriginTrackingDepth(void)
4647{
Victor Stinner50b48572018-11-01 01:51:40 +01004648 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004649 return tstate->coroutine_origin_tracking_depth;
4650}
4651
4652void
Yury Selivanoveb636452016-09-08 22:01:51 -07004653_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4654{
Victor Stinner50b48572018-11-01 01:51:40 +01004655 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004656
4657 if (PySys_Audit("sys.set_asyncgen_hook_firstiter", NULL) < 0) {
4658 return;
4659 }
4660
Yury Selivanoveb636452016-09-08 22:01:51 -07004661 Py_XINCREF(firstiter);
4662 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4663}
4664
4665PyObject *
4666_PyEval_GetAsyncGenFirstiter(void)
4667{
Victor Stinner50b48572018-11-01 01:51:40 +01004668 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004669 return tstate->async_gen_firstiter;
4670}
4671
4672void
4673_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4674{
Victor Stinner50b48572018-11-01 01:51:40 +01004675 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004676
4677 if (PySys_Audit("sys.set_asyncgen_hook_finalizer", NULL) < 0) {
4678 return;
4679 }
4680
Yury Selivanoveb636452016-09-08 22:01:51 -07004681 Py_XINCREF(finalizer);
4682 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4683}
4684
4685PyObject *
4686_PyEval_GetAsyncGenFinalizer(void)
4687{
Victor Stinner50b48572018-11-01 01:51:40 +01004688 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004689 return tstate->async_gen_finalizer;
4690}
4691
Victor Stinner438a12d2019-05-24 17:01:38 +02004692static PyFrameObject *
4693_PyEval_GetFrame(PyThreadState *tstate)
4694{
Victor Stinner01b1cc12019-11-20 02:27:56 +01004695 _PyRuntimeState *runtime = tstate->interp->runtime;
4696 return runtime->gilstate.getframe(tstate);
Victor Stinner438a12d2019-05-24 17:01:38 +02004697}
4698
4699PyFrameObject *
4700PyEval_GetFrame(void)
4701{
4702 PyThreadState *tstate = _PyThreadState_GET();
4703 return _PyEval_GetFrame(tstate);
4704}
4705
Guido van Rossumb209a111997-04-29 18:18:01 +00004706PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004707PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004708{
Victor Stinner438a12d2019-05-24 17:01:38 +02004709 PyThreadState *tstate = _PyThreadState_GET();
4710 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004711 if (current_frame == NULL)
Victor Stinner438a12d2019-05-24 17:01:38 +02004712 return tstate->interp->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004713 else
4714 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004715}
4716
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004717/* Convenience function to get a builtin from its name */
4718PyObject *
4719_PyEval_GetBuiltinId(_Py_Identifier *name)
4720{
Victor Stinner438a12d2019-05-24 17:01:38 +02004721 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004722 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4723 if (attr) {
4724 Py_INCREF(attr);
4725 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004726 else if (!_PyErr_Occurred(tstate)) {
4727 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004728 }
4729 return attr;
4730}
4731
Guido van Rossumb209a111997-04-29 18:18:01 +00004732PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004733PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004734{
Victor Stinner438a12d2019-05-24 17:01:38 +02004735 PyThreadState *tstate = _PyThreadState_GET();
4736 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Victor Stinner41bb43a2013-10-29 01:19:37 +01004737 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004738 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004739 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004740 }
4741
Victor Stinner438a12d2019-05-24 17:01:38 +02004742 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01004743 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004744 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004745
4746 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004747 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004748}
4749
Guido van Rossumb209a111997-04-29 18:18:01 +00004750PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004751PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004752{
Victor Stinner438a12d2019-05-24 17:01:38 +02004753 PyThreadState *tstate = _PyThreadState_GET();
4754 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
4755 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004756 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004757 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004758
4759 assert(current_frame->f_globals != NULL);
4760 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004761}
4762
Guido van Rossum6135a871995-01-09 17:53:26 +00004763int
Tim Peters5ba58662001-07-16 02:29:45 +00004764PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004765{
Victor Stinner438a12d2019-05-24 17:01:38 +02004766 PyThreadState *tstate = _PyThreadState_GET();
4767 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004768 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004769
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004770 if (current_frame != NULL) {
4771 const int codeflags = current_frame->f_code->co_flags;
4772 const int compilerflags = codeflags & PyCF_MASK;
4773 if (compilerflags) {
4774 result = 1;
4775 cf->cf_flags |= compilerflags;
4776 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004777#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004778 if (codeflags & CO_GENERATOR_ALLOWED) {
4779 result = 1;
4780 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4781 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004782#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004783 }
4784 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004785}
4786
Guido van Rossum3f5da241990-12-20 15:06:42 +00004787
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004788const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004789PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004790{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004791 if (PyMethod_Check(func))
4792 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4793 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004794 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004795 else if (PyCFunction_Check(func))
4796 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4797 else
4798 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004799}
4800
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004801const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004802PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004803{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004804 if (PyMethod_Check(func))
4805 return "()";
4806 else if (PyFunction_Check(func))
4807 return "()";
4808 else if (PyCFunction_Check(func))
4809 return "()";
4810 else
4811 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004812}
4813
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004814#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004815if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004816 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4817 tstate, tstate->frame, \
4818 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004819 x = NULL; \
4820 } \
4821 else { \
4822 x = call; \
4823 if (tstate->c_profilefunc != NULL) { \
4824 if (x == NULL) { \
4825 call_trace_protected(tstate->c_profilefunc, \
4826 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004827 tstate, tstate->frame, \
4828 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004829 /* XXX should pass (type, value, tb) */ \
4830 } else { \
4831 if (call_trace(tstate->c_profilefunc, \
4832 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004833 tstate, tstate->frame, \
4834 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004835 Py_DECREF(x); \
4836 x = NULL; \
4837 } \
4838 } \
4839 } \
4840 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004841} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004842 x = call; \
4843 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004844
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004845
4846static PyObject *
4847trace_call_function(PyThreadState *tstate,
4848 PyObject *func,
4849 PyObject **args, Py_ssize_t nargs,
4850 PyObject *kwnames)
4851{
4852 PyObject *x;
4853 if (PyCFunction_Check(func)) {
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02004854 C_TRACE(x, _PyObject_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004855 return x;
4856 }
4857 else if (Py_TYPE(func) == &PyMethodDescr_Type && nargs > 0) {
4858 /* We need to create a temporary bound method as argument
4859 for profiling.
4860
4861 If nargs == 0, then this cannot work because we have no
4862 "self". In any case, the call itself would raise
4863 TypeError (foo needs an argument), so we just skip
4864 profiling. */
4865 PyObject *self = args[0];
4866 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4867 if (func == NULL) {
4868 return NULL;
4869 }
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02004870 C_TRACE(x, _PyObject_Vectorcall(func,
4871 args+1, nargs-1,
4872 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004873 Py_DECREF(func);
4874 return x;
4875 }
4876 return _PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
4877}
4878
Victor Stinner415c5102017-01-11 00:54:57 +01004879/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4880 to reduce the stack consumption. */
4881Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Victor Stinner09532fe2019-05-10 23:39:09 +02004882call_function(PyThreadState *tstate, PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004883{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004884 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004885 PyObject *func = *pfunc;
4886 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004887 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4888 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004889 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004890
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004891 if (tstate->use_tracing) {
4892 x = trace_call_function(tstate, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09004893 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004894 else {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004895 x = _PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004896 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004897
Victor Stinner438a12d2019-05-24 17:01:38 +02004898 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004899
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004900 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004901 while ((*pp_stack) > pfunc) {
4902 w = EXT_POP(*pp_stack);
4903 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004904 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004905
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004906 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004907}
4908
Jeremy Hylton52820442001-01-03 23:52:36 +00004909static PyObject *
Victor Stinner09532fe2019-05-10 23:39:09 +02004910do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004911{
jdemeyere89de732018-09-19 12:06:20 +02004912 PyObject *result;
4913
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004914 if (PyCFunction_Check(func)) {
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +02004915 C_TRACE(result, PyObject_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004916 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004917 }
jdemeyere89de732018-09-19 12:06:20 +02004918 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
jdemeyere89de732018-09-19 12:06:20 +02004919 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
4920 if (nargs > 0 && tstate->use_tracing) {
4921 /* We need to create a temporary bound method as argument
4922 for profiling.
4923
4924 If nargs == 0, then this cannot work because we have no
4925 "self". In any case, the call itself would raise
4926 TypeError (foo needs an argument), so we just skip
4927 profiling. */
4928 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
4929 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4930 if (func == NULL) {
4931 return NULL;
4932 }
4933
Victor Stinner4d231bc2019-11-14 13:36:21 +01004934 C_TRACE(result, _PyObject_FastCallDictTstate(
4935 tstate, func,
4936 &_PyTuple_ITEMS(callargs)[1],
4937 nargs - 1,
4938 kwdict));
jdemeyere89de732018-09-19 12:06:20 +02004939 Py_DECREF(func);
4940 return result;
4941 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004942 }
jdemeyere89de732018-09-19 12:06:20 +02004943 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00004944}
4945
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004946/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004947 nb_index slot defined, and store in *pi.
4948 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004949 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004950 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004951*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004952int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004953_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004954{
Victor Stinner438a12d2019-05-24 17:01:38 +02004955 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004956 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004957 Py_ssize_t x;
4958 if (PyIndex_Check(v)) {
4959 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02004960 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004961 return 0;
4962 }
4963 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004964 _PyErr_SetString(tstate, PyExc_TypeError,
4965 "slice indices must be integers or "
4966 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004967 return 0;
4968 }
4969 *pi = x;
4970 }
4971 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004972}
4973
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004974int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004975_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004976{
Victor Stinner438a12d2019-05-24 17:01:38 +02004977 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004978 Py_ssize_t x;
4979 if (PyIndex_Check(v)) {
4980 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02004981 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004982 return 0;
4983 }
4984 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004985 _PyErr_SetString(tstate, PyExc_TypeError,
4986 "slice indices must be integers or "
4987 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004988 return 0;
4989 }
4990 *pi = x;
4991 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004992}
4993
Thomas Wouters52152252000-08-17 22:55:00 +00004994static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004995import_name(PyThreadState *tstate, PyFrameObject *f,
4996 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004997{
4998 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004999 PyObject *import_func, *res;
5000 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005001
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005002 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005003 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005004 if (!_PyErr_Occurred(tstate)) {
5005 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005006 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005007 return NULL;
5008 }
5009
5010 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005011 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005012 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02005013 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005014 return NULL;
5015 }
5016 res = PyImport_ImportModuleLevelObject(
5017 name,
5018 f->f_globals,
5019 f->f_locals == NULL ? Py_None : f->f_locals,
5020 fromlist,
5021 ilevel);
5022 return res;
5023 }
5024
5025 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005026
5027 stack[0] = name;
5028 stack[1] = f->f_globals;
5029 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5030 stack[3] = fromlist;
5031 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005032 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005033 Py_DECREF(import_func);
5034 return res;
5035}
5036
5037static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005038import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005039{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005040 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02005041 _Py_IDENTIFIER(__name__);
Xiang Zhang4830f582017-03-21 11:13:42 +08005042 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005043
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005044 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005045 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005046 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005047 /* Issue #17636: in case this failed because of a circular relative
5048 import, try to fallback on reading the module directly from
5049 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02005050 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005051 if (pkgname == NULL) {
5052 goto error;
5053 }
Oren Milman6db70332017-09-19 14:23:01 +03005054 if (!PyUnicode_Check(pkgname)) {
5055 Py_CLEAR(pkgname);
5056 goto error;
5057 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005058 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005059 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005060 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005061 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005062 }
Eric Snow3f9eee62017-09-15 16:35:20 -06005063 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005064 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02005065 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07005066 goto error;
5067 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005068 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005069 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005070 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005071 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005072 if (pkgname == NULL) {
5073 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5074 if (pkgname_or_unknown == NULL) {
5075 Py_XDECREF(pkgpath);
5076 return NULL;
5077 }
5078 } else {
5079 pkgname_or_unknown = pkgname;
5080 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005081
5082 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005083 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08005084 errmsg = PyUnicode_FromFormat(
5085 "cannot import name %R from %R (unknown location)",
5086 name, pkgname_or_unknown
5087 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005088 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005089 PyErr_SetImportError(errmsg, pkgname, NULL);
5090 }
5091 else {
Anthony Sottile65366bc2019-09-09 08:17:50 -07005092 _Py_IDENTIFIER(__spec__);
5093 PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
Anthony Sottile65366bc2019-09-09 08:17:50 -07005094 const char *fmt =
5095 _PyModuleSpec_IsInitializing(spec) ?
5096 "cannot import name %R from partially initialized module %R "
5097 "(most likely due to a circular import) (%S)" :
5098 "cannot import name %R from %R (%S)";
5099 Py_XDECREF(spec);
5100
5101 errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
Stefan Krah027b09c2019-03-25 21:50:58 +01005102 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005103 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005104 }
5105
Xiang Zhang4830f582017-03-21 11:13:42 +08005106 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005107 Py_XDECREF(pkgname_or_unknown);
5108 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005109 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005110}
Guido van Rossumac7be682001-01-17 15:42:30 +00005111
Thomas Wouters52152252000-08-17 22:55:00 +00005112static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005113import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00005114{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005115 _Py_IDENTIFIER(__all__);
5116 _Py_IDENTIFIER(__dict__);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005117 _Py_IDENTIFIER(__name__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005118 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005119 int skip_leading_underscores = 0;
5120 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005121
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005122 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5123 return -1; /* Unexpected error */
5124 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005125 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005126 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5127 return -1;
5128 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005129 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005130 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005131 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005132 return -1;
5133 }
5134 all = PyMapping_Keys(dict);
5135 Py_DECREF(dict);
5136 if (all == NULL)
5137 return -1;
5138 skip_leading_underscores = 1;
5139 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005140
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005141 for (pos = 0, err = 0; ; pos++) {
5142 name = PySequence_GetItem(all, pos);
5143 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005144 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005145 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02005146 }
5147 else {
5148 _PyErr_Clear(tstate);
5149 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005150 break;
5151 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005152 if (!PyUnicode_Check(name)) {
5153 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5154 if (modname == NULL) {
5155 Py_DECREF(name);
5156 err = -1;
5157 break;
5158 }
5159 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005160 _PyErr_Format(tstate, PyExc_TypeError,
5161 "module __name__ must be a string, not %.100s",
5162 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005163 }
5164 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005165 _PyErr_Format(tstate, PyExc_TypeError,
5166 "%s in %U.%s must be str, not %.100s",
5167 skip_leading_underscores ? "Key" : "Item",
5168 modname,
5169 skip_leading_underscores ? "__dict__" : "__all__",
5170 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005171 }
5172 Py_DECREF(modname);
5173 Py_DECREF(name);
5174 err = -1;
5175 break;
5176 }
5177 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005178 if (PyUnicode_READY(name) == -1) {
5179 Py_DECREF(name);
5180 err = -1;
5181 break;
5182 }
5183 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5184 Py_DECREF(name);
5185 continue;
5186 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005187 }
5188 value = PyObject_GetAttr(v, name);
5189 if (value == NULL)
5190 err = -1;
5191 else if (PyDict_CheckExact(locals))
5192 err = PyDict_SetItem(locals, name, value);
5193 else
5194 err = PyObject_SetItem(locals, name, value);
5195 Py_DECREF(name);
5196 Py_XDECREF(value);
5197 if (err != 0)
5198 break;
5199 }
5200 Py_DECREF(all);
5201 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005202}
5203
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005204static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005205check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005206{
5207 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005208 /* check_args_iterable() may be called with a live exception:
5209 * clear it to prevent calling _PyObject_FunctionStr() with an
5210 * exception set. */
Victor Stinner61f4db82020-01-28 03:37:45 +01005211 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005212 PyObject *funcstr = _PyObject_FunctionStr(func);
5213 if (funcstr != NULL) {
5214 _PyErr_Format(tstate, PyExc_TypeError,
5215 "%U argument after * must be an iterable, not %.200s",
5216 funcstr, Py_TYPE(args)->tp_name);
5217 Py_DECREF(funcstr);
5218 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005219 return -1;
5220 }
5221 return 0;
5222}
5223
5224static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005225format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005226{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005227 /* _PyDict_MergeEx raises attribute
5228 * error (percolated from an attempt
5229 * to get 'keys' attribute) instead of
5230 * a type error if its second argument
5231 * is not a mapping.
5232 */
Victor Stinner438a12d2019-05-24 17:01:38 +02005233 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005234 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005235 PyObject *funcstr = _PyObject_FunctionStr(func);
5236 if (funcstr != NULL) {
5237 _PyErr_Format(
5238 tstate, PyExc_TypeError,
5239 "%U argument after ** must be a mapping, not %.200s",
5240 funcstr, Py_TYPE(kwargs)->tp_name);
5241 Py_DECREF(funcstr);
5242 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005243 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005244 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005245 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02005246 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005247 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005248 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005249 PyObject *funcstr = _PyObject_FunctionStr(func);
5250 if (funcstr != NULL) {
5251 PyObject *key = PyTuple_GET_ITEM(val, 0);
5252 _PyErr_Format(
5253 tstate, PyExc_TypeError,
5254 "%U got multiple values for keyword argument '%S'",
5255 funcstr, key);
5256 Py_DECREF(funcstr);
5257 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005258 Py_XDECREF(exc);
5259 Py_XDECREF(val);
5260 Py_XDECREF(tb);
5261 }
5262 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005263 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005264 }
5265 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005266}
5267
Guido van Rossumac7be682001-01-17 15:42:30 +00005268static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005269format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
5270 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005271{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005272 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005274 if (!obj)
5275 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005276
Serhiy Storchaka06515832016-11-20 09:13:07 +02005277 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005278 if (!obj_str)
5279 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005280
Victor Stinner438a12d2019-05-24 17:01:38 +02005281 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005282}
Guido van Rossum950361c1997-01-24 13:49:28 +00005283
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005284static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005285format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005286{
5287 PyObject *name;
5288 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02005289 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005290 return;
5291 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5292 name = PyTuple_GET_ITEM(co->co_cellvars,
5293 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005294 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005295 PyExc_UnboundLocalError,
5296 UNBOUNDLOCAL_ERROR_MSG,
5297 name);
5298 } else {
5299 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5300 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02005301 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005302 UNBOUNDFREE_ERROR_MSG, name);
5303 }
5304}
5305
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005306static void
Mark Shannonfee55262019-11-21 09:11:43 +00005307format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevprevopcode, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005308{
5309 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5310 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005311 _PyErr_Format(tstate, PyExc_TypeError,
5312 "'async with' received an object from __aenter__ "
5313 "that does not implement __await__: %.100s",
5314 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005315 }
Mark Shannonfee55262019-11-21 09:11:43 +00005316 else if (prevopcode == WITH_EXCEPT_START || (prevopcode == CALL_FUNCTION && prevprevopcode == DUP_TOP)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005317 _PyErr_Format(tstate, PyExc_TypeError,
5318 "'async with' received an object from __aexit__ "
5319 "that does not implement __await__: %.100s",
5320 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005321 }
5322 }
5323}
5324
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005325static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005326unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005327 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005328{
5329 PyObject *res;
5330 if (Py_REFCNT(v) == 2) {
5331 /* In the common case, there are 2 references to the value
5332 * stored in 'variable' when the += is performed: one on the
5333 * value stack (in 'v') and one still stored in the
5334 * 'variable'. We try to delete the variable now to reduce
5335 * the refcnt to 1.
5336 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005337 int opcode, oparg;
5338 NEXTOPARG();
5339 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005340 case STORE_FAST:
5341 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005342 PyObject **fastlocals = f->f_localsplus;
5343 if (GETLOCAL(oparg) == v)
5344 SETLOCAL(oparg, NULL);
5345 break;
5346 }
5347 case STORE_DEREF:
5348 {
5349 PyObject **freevars = (f->f_localsplus +
5350 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005351 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005352 if (PyCell_GET(c) == v) {
5353 PyCell_SET(c, NULL);
5354 Py_DECREF(v);
5355 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005356 break;
5357 }
5358 case STORE_NAME:
5359 {
5360 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005361 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005362 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005363 if (locals && PyDict_CheckExact(locals)) {
5364 PyObject *w = PyDict_GetItemWithError(locals, name);
5365 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02005366 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005367 {
5368 Py_DECREF(v);
5369 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005370 }
5371 }
5372 break;
5373 }
5374 }
5375 }
5376 res = v;
5377 PyUnicode_Append(&res, w);
5378 return res;
5379}
5380
Guido van Rossum950361c1997-01-24 13:49:28 +00005381#ifdef DYNAMIC_EXECUTION_PROFILE
5382
Skip Montanarof118cb12001-10-15 20:51:38 +00005383static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005384getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005385{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005386 int i;
5387 PyObject *l = PyList_New(256);
5388 if (l == NULL) return NULL;
5389 for (i = 0; i < 256; i++) {
5390 PyObject *x = PyLong_FromLong(a[i]);
5391 if (x == NULL) {
5392 Py_DECREF(l);
5393 return NULL;
5394 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005395 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005396 }
5397 for (i = 0; i < 256; i++)
5398 a[i] = 0;
5399 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005400}
5401
5402PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005403_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005404{
5405#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005406 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005407#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005408 int i;
5409 PyObject *l = PyList_New(257);
5410 if (l == NULL) return NULL;
5411 for (i = 0; i < 257; i++) {
5412 PyObject *x = getarray(dxpairs[i]);
5413 if (x == NULL) {
5414 Py_DECREF(l);
5415 return NULL;
5416 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005417 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005418 }
5419 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005420#endif
5421}
5422
5423#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005424
5425Py_ssize_t
5426_PyEval_RequestCodeExtraIndex(freefunc free)
5427{
Victor Stinnercaba55b2018-08-03 15:33:52 +02005428 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -07005429 Py_ssize_t new_index;
5430
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005431 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005432 return -1;
5433 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005434 new_index = interp->co_extra_user_count++;
5435 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005436 return new_index;
5437}
Łukasz Langaa785c872016-09-09 17:37:37 -07005438
5439static void
5440dtrace_function_entry(PyFrameObject *f)
5441{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005442 const char *filename;
5443 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005444 int lineno;
5445
5446 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5447 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5448 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5449
5450 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5451}
5452
5453static void
5454dtrace_function_return(PyFrameObject *f)
5455{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005456 const char *filename;
5457 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005458 int lineno;
5459
5460 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5461 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5462 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5463
5464 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5465}
5466
5467/* DTrace equivalent of maybe_call_line_trace. */
5468static void
5469maybe_dtrace_line(PyFrameObject *frame,
5470 int *instr_lb, int *instr_ub, int *instr_prev)
5471{
5472 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005473 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005474
5475 /* If the last instruction executed isn't in the current
5476 instruction window, reset the window.
5477 */
5478 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5479 PyAddrPair bounds;
5480 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5481 &bounds);
5482 *instr_lb = bounds.ap_lower;
5483 *instr_ub = bounds.ap_upper;
5484 }
5485 /* If the last instruction falls at the start of a line or if
5486 it represents a jump backwards, update the frame's line
5487 number and call the trace function. */
5488 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5489 frame->f_lineno = line;
5490 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5491 if (!co_filename)
5492 co_filename = "?";
5493 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5494 if (!co_name)
5495 co_name = "?";
5496 PyDTrace_LINE(co_filename, co_name, line);
5497 }
5498 *instr_prev = frame->f_lasti;
5499}
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005500
5501
5502/* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions
5503 for the limited API. */
5504
5505#undef Py_EnterRecursiveCall
5506
5507int Py_EnterRecursiveCall(const char *where)
5508{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005509 return _Py_EnterRecursiveCall_inline(where);
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005510}
5511
5512#undef Py_LeaveRecursiveCall
5513
5514void Py_LeaveRecursiveCall(void)
5515{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005516 _Py_LeaveRecursiveCall_inline();
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005517}