blob: 26cefeadcde681b72dd63ec741f2e22d4678d4a0 [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
Hai Shi46874c22020-01-30 17:20:25 -060042_Py_IDENTIFIER(__name__);
Guido van Rossum5b722181993-03-30 17:46:03 +000043
Guido van Rossum374a9221991-04-04 10:40:29 +000044/* Forward declarations */
Victor Stinner09532fe2019-05-10 23:39:09 +020045Py_LOCAL_INLINE(PyObject *) call_function(
46 PyThreadState *tstate, PyObject ***pp_stack,
47 Py_ssize_t oparg, PyObject *kwnames);
48static PyObject * do_call_core(
49 PyThreadState *tstate, PyObject *func,
50 PyObject *callargs, PyObject *kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +000051
Guido van Rossum0a066c01992-03-27 17:29:15 +000052#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000053static int lltrace;
Victor Stinner438a12d2019-05-24 17:01:38 +020054static int prtrace(PyThreadState *, PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000055#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010056static int call_trace(Py_tracefunc, PyObject *,
57 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000058 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000059static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010060 PyThreadState *, PyFrameObject *,
61 int, PyObject *);
62static void call_exc_trace(Py_tracefunc, PyObject *,
63 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000064static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Eric Snow2ebc5ce2017-09-07 23:51:28 -060065 PyThreadState *, PyFrameObject *,
66 int *, int *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070067static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *);
68static void dtrace_function_entry(PyFrameObject *);
69static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000070
Victor Stinner438a12d2019-05-24 17:01:38 +020071static PyObject * import_name(PyThreadState *, PyFrameObject *,
72 PyObject *, PyObject *, PyObject *);
73static PyObject * import_from(PyThreadState *, PyObject *, PyObject *);
74static int import_all_from(PyThreadState *, PyObject *, PyObject *);
75static void format_exc_check_arg(PyThreadState *, PyObject *, const char *, PyObject *);
76static void format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg);
77static PyObject * unicode_concatenate(PyThreadState *, PyObject *, PyObject *,
Serhiy Storchakaab874002016-09-11 13:48:15 +030078 PyFrameObject *, const _Py_CODEUNIT *);
Victor Stinner438a12d2019-05-24 17:01:38 +020079static PyObject * special_lookup(PyThreadState *, PyObject *, _Py_Identifier *);
80static int check_args_iterable(PyThreadState *, PyObject *func, PyObject *vararg);
81static void format_kwargs_error(PyThreadState *, PyObject *func, PyObject *kwargs);
Mark Shannonfee55262019-11-21 09:11:43 +000082static void format_awaitable_error(PyThreadState *, PyTypeObject *, int, int);
Guido van Rossum374a9221991-04-04 10:40:29 +000083
Paul Prescode68140d2000-08-30 20:25:01 +000084#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000086#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000088#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 "free variable '%.200s' referenced before assignment" \
90 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000091
Guido van Rossum950361c1997-01-24 13:49:28 +000092/* Dynamic execution profile */
93#ifdef DYNAMIC_EXECUTION_PROFILE
94#ifdef DXPAIRS
95static long dxpairs[257][256];
96#define dxp dxpairs[256]
97#else
98static long dxp[256];
99#endif
100#endif
101
Inada Naoki91234a12019-06-03 21:30:58 +0900102/* per opcode cache */
Inada Naokieddef862019-06-04 07:38:10 +0900103#ifdef Py_DEBUG
104// --with-pydebug is used to find memory leak. opcache makes it harder.
105// So we disable opcache when Py_DEBUG is defined.
106// See bpo-37146
107#define OPCACHE_MIN_RUNS 0 /* disable opcache */
108#else
Inada Naoki91234a12019-06-03 21:30:58 +0900109#define OPCACHE_MIN_RUNS 1024 /* create opcache when code executed this time */
Inada Naokieddef862019-06-04 07:38:10 +0900110#endif
Inada Naoki91234a12019-06-03 21:30:58 +0900111#define OPCACHE_STATS 0 /* Enable stats */
112
113#if OPCACHE_STATS
114static size_t opcache_code_objects = 0;
115static size_t opcache_code_objects_extra_mem = 0;
116
117static size_t opcache_global_opts = 0;
118static size_t opcache_global_hits = 0;
119static size_t opcache_global_misses = 0;
120#endif
121
Victor Stinnere225beb2019-06-03 18:14:24 +0200122#define GIL_REQUEST _Py_atomic_load_relaxed(&ceval->gil_drop_request)
Inada Naoki91234a12019-06-03 21:30:58 +0900123
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000124/* This can set eval_breaker to 0 even though gil_drop_request became
125 1. We believe this is all right because the eval loop will release
126 the GIL eventually anyway. */
Victor Stinnere225beb2019-06-03 18:14:24 +0200127#define COMPUTE_EVAL_BREAKER(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 _Py_atomic_store_relaxed( \
Victor Stinnere225beb2019-06-03 18:14:24 +0200129 &(ceval)->eval_breaker, \
130 GIL_REQUEST | \
131 _Py_atomic_load_relaxed(&(ceval)->signals_pending) | \
132 _Py_atomic_load_relaxed(&(ceval)->pending.calls_to_do) | \
133 (ceval)->pending.async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000134
Victor Stinnere225beb2019-06-03 18:14:24 +0200135#define SET_GIL_DROP_REQUEST(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000136 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200137 _Py_atomic_store_relaxed(&(ceval)->gil_drop_request, 1); \
138 _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000140
Victor Stinnere225beb2019-06-03 18:14:24 +0200141#define RESET_GIL_DROP_REQUEST(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200143 _Py_atomic_store_relaxed(&(ceval)->gil_drop_request, 0); \
144 COMPUTE_EVAL_BREAKER(ceval); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000146
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000147/* Pending calls are only modified under pending_lock */
Victor Stinnere225beb2019-06-03 18:14:24 +0200148#define SIGNAL_PENDING_CALLS(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200150 _Py_atomic_store_relaxed(&(ceval)->pending.calls_to_do, 1); \
151 _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000153
Victor Stinnere225beb2019-06-03 18:14:24 +0200154#define UNSIGNAL_PENDING_CALLS(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200156 _Py_atomic_store_relaxed(&(ceval)->pending.calls_to_do, 0); \
157 COMPUTE_EVAL_BREAKER(ceval); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000159
Victor Stinnere225beb2019-06-03 18:14:24 +0200160#define SIGNAL_PENDING_SIGNALS(ceval) \
Eric Snowfdf282d2019-01-11 14:26:55 -0700161 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200162 _Py_atomic_store_relaxed(&(ceval)->signals_pending, 1); \
163 _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \
Eric Snowfdf282d2019-01-11 14:26:55 -0700164 } while (0)
165
Victor Stinnere225beb2019-06-03 18:14:24 +0200166#define UNSIGNAL_PENDING_SIGNALS(ceval) \
Eric Snowfdf282d2019-01-11 14:26:55 -0700167 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200168 _Py_atomic_store_relaxed(&(ceval)->signals_pending, 0); \
169 COMPUTE_EVAL_BREAKER(ceval); \
Eric Snowfdf282d2019-01-11 14:26:55 -0700170 } while (0)
171
Victor Stinnere225beb2019-06-03 18:14:24 +0200172#define SIGNAL_ASYNC_EXC(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200174 (ceval)->pending.async_exc = 1; \
175 _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000177
Victor Stinnere225beb2019-06-03 18:14:24 +0200178#define UNSIGNAL_ASYNC_EXC(ceval) \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600179 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200180 (ceval)->pending.async_exc = 0; \
181 COMPUTE_EVAL_BREAKER(ceval); \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600182 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000183
184
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000185#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000186#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000187#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000188#include "pythread.h"
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000189#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000190
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100191static void
192ensure_tstate_not_null(const char *func, PyThreadState *tstate)
193{
194 if (tstate == NULL) {
195 _Py_FatalErrorFunc(func, "current thread state is NULL");
196 }
197}
198
199
200#ifndef NDEBUG
201static int is_tstate_valid(PyThreadState *tstate)
202{
203 assert(!_PyMem_IsPtrFreed(tstate));
204 assert(!_PyMem_IsPtrFreed(tstate->interp));
205 return 1;
206}
207#endif
208
209
Tim Peters7f468f22004-10-11 02:40:51 +0000210int
211PyEval_ThreadsInitialized(void)
212{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100213 _PyRuntimeState *runtime = &_PyRuntime;
214 return gil_created(&runtime->ceval.gil);
Tim Peters7f468f22004-10-11 02:40:51 +0000215}
216
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000217void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000218PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000219{
Victor Stinner09532fe2019-05-10 23:39:09 +0200220 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200221 struct _ceval_runtime_state *ceval = &runtime->ceval;
222 struct _gil_runtime_state *gil = &ceval->gil;
Victor Stinner09532fe2019-05-10 23:39:09 +0200223 if (gil_created(gil)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 return;
Victor Stinnera7126792019-03-19 14:19:38 +0100225 }
226
Inada Naoki001fee12019-02-20 10:00:09 +0900227 PyThread_init_thread();
Victor Stinner09532fe2019-05-10 23:39:09 +0200228 create_gil(gil);
229 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100230 ensure_tstate_not_null(__func__, tstate);
Victor Stinnere225beb2019-06-03 18:14:24 +0200231 take_gil(ceval, tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700232
Victor Stinnere225beb2019-06-03 18:14:24 +0200233 struct _pending_calls *pending = &ceval->pending;
234 pending->lock = PyThread_allocate_lock();
235 if (pending->lock == NULL) {
236 Py_FatalError("Can't initialize threads for pending calls");
237 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000238}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000239
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000240void
Victor Stinnere225beb2019-06-03 18:14:24 +0200241_PyEval_FiniThreads(struct _ceval_runtime_state *ceval)
Antoine Pitrou1df15362010-09-13 14:16:46 +0000242{
Victor Stinnere225beb2019-06-03 18:14:24 +0200243 struct _gil_runtime_state *gil = &ceval->gil;
Victor Stinner09532fe2019-05-10 23:39:09 +0200244 if (!gil_created(gil)) {
Antoine Pitrou1df15362010-09-13 14:16:46 +0000245 return;
Victor Stinnera7126792019-03-19 14:19:38 +0100246 }
247
Victor Stinner09532fe2019-05-10 23:39:09 +0200248 destroy_gil(gil);
249 assert(!gil_created(gil));
Victor Stinner99fcc612019-04-29 13:04:07 +0200250
Victor Stinnere225beb2019-06-03 18:14:24 +0200251 struct _pending_calls *pending = &ceval->pending;
252 if (pending->lock != NULL) {
253 PyThread_free_lock(pending->lock);
254 pending->lock = NULL;
255 }
Antoine Pitrou1df15362010-09-13 14:16:46 +0000256}
257
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100258/* This function is designed to exit daemon threads immediately rather than
259 taking the GIL if Py_Finalize() has been called.
260
261 The caller must *not* hold the GIL, since this function does not release
262 the GIL before exiting the thread.
263
264 When this function is called by a daemon thread after Py_Finalize() has been
265 called, the GIL does no longer exist.
266
267 tstate must be non-NULL. */
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400268static inline void
Victor Stinner01b1cc12019-11-20 02:27:56 +0100269exit_thread_if_finalizing(PyThreadState *tstate)
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400270{
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100271 /* bpo-39877: Access _PyRuntime directly rather than using
272 tstate->interp->runtime to support calls from Python daemon threads.
273 After Py_Finalize() has been called, tstate can be a dangling pointer:
274 point to PyThreadState freed memory. */
275 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner7b3c2522020-03-07 00:24:23 +0100276 PyThreadState *finalizing = _PyRuntimeState_GetFinalizing(runtime);
277 if (finalizing != NULL && finalizing != tstate) {
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400278 PyThread_exit_thread();
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400279 }
280}
281
Antoine Pitrou1df15362010-09-13 14:16:46 +0000282void
Inada Naoki91234a12019-06-03 21:30:58 +0900283_PyEval_Fini(void)
284{
285#if OPCACHE_STATS
286 fprintf(stderr, "-- Opcode cache number of objects = %zd\n",
287 opcache_code_objects);
288
289 fprintf(stderr, "-- Opcode cache total extra mem = %zd\n",
290 opcache_code_objects_extra_mem);
291
292 fprintf(stderr, "\n");
293
294 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL hits = %zd (%d%%)\n",
295 opcache_global_hits,
296 (int) (100.0 * opcache_global_hits /
297 (opcache_global_hits + opcache_global_misses)));
298
299 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL misses = %zd (%d%%)\n",
300 opcache_global_misses,
301 (int) (100.0 * opcache_global_misses /
302 (opcache_global_hits + opcache_global_misses)));
303
304 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL opts = %zd\n",
305 opcache_global_opts);
306
307 fprintf(stderr, "\n");
308#endif
309}
310
311void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000312PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000313{
Victor Stinner09532fe2019-05-10 23:39:09 +0200314 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200315 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100316 ensure_tstate_not_null(__func__, tstate);
317
Victor Stinner01b1cc12019-11-20 02:27:56 +0100318 exit_thread_if_finalizing(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100319 assert(is_tstate_valid(tstate));
320
321 struct _ceval_runtime_state *ceval = &runtime->ceval;
322 take_gil(ceval, tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000323}
324
325void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000326PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000327{
Victor Stinner09532fe2019-05-10 23:39:09 +0200328 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200329 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100331 We therefore avoid PyThreadState_Get() which dumps a fatal error
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 in debug mode.
333 */
Victor Stinnere225beb2019-06-03 18:14:24 +0200334 drop_gil(&runtime->ceval, tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000335}
336
337void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000338PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000339{
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100340 ensure_tstate_not_null(__func__, tstate);
341
342 exit_thread_if_finalizing(tstate);
343 assert(is_tstate_valid(tstate));
Victor Stinnere225beb2019-06-03 18:14:24 +0200344
Victor Stinner01b1cc12019-11-20 02:27:56 +0100345 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200346 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinner09532fe2019-05-10 23:39:09 +0200347
Victor Stinner3225b9f2020-03-09 20:56:57 +0100348 /* Check that _PyEval_InitThreads() was called to create the lock */
Victor Stinnere225beb2019-06-03 18:14:24 +0200349 assert(gil_created(&ceval->gil));
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100350
Victor Stinnere225beb2019-06-03 18:14:24 +0200351 take_gil(ceval, tstate);
Victor Stinner09532fe2019-05-10 23:39:09 +0200352 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100353 Py_FatalError("non-NULL old thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200354 }
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000355}
356
357void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000358PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000359{
Victor Stinner17c68b82020-01-30 12:20:48 +0100360 assert(tstate != NULL);
Victor Stinner09532fe2019-05-10 23:39:09 +0200361
Victor Stinner01b1cc12019-11-20 02:27:56 +0100362 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200363 PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
364 if (new_tstate != tstate) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100365 Py_FatalError("wrong thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200366 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200367 drop_gil(&runtime->ceval, tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000368}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000369
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200370/* This function is called from PyOS_AfterFork_Child to destroy all threads
371 * which are not running in the child process, and clear internal locks
372 * which might be held by those threads.
373 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000374
375void
Victor Stinnerd5d9e812019-05-13 12:35:37 +0200376_PyEval_ReInitThreads(_PyRuntimeState *runtime)
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000377{
Victor Stinnere225beb2019-06-03 18:14:24 +0200378 struct _ceval_runtime_state *ceval = &runtime->ceval;
379 if (!gil_created(&ceval->gil)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 return;
Victor Stinner09532fe2019-05-10 23:39:09 +0200381 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200382 recreate_gil(&ceval->gil);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100383 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
384 ensure_tstate_not_null(__func__, tstate);
385 take_gil(ceval, tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700386
Victor Stinnere225beb2019-06-03 18:14:24 +0200387 struct _pending_calls *pending = &ceval->pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200388 pending->lock = PyThread_allocate_lock();
389 if (pending->lock == NULL) {
Eric Snow8479a342019-03-08 23:44:33 -0700390 Py_FatalError("Can't initialize threads for pending calls");
391 }
Jesse Nollera8513972008-07-17 16:49:17 +0000392
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200393 /* Destroy all threads except the current one */
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100394 _PyThreadState_DeleteExcept(runtime, tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000395}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000396
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000397/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600398 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000399
400void
Victor Stinnere225beb2019-06-03 18:14:24 +0200401_PyEval_SignalAsyncExc(struct _ceval_runtime_state *ceval)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000402{
Victor Stinnere225beb2019-06-03 18:14:24 +0200403 SIGNAL_ASYNC_EXC(ceval);
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000404}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000405
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000406PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000407PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000408{
Victor Stinner09532fe2019-05-10 23:39:09 +0200409 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200410 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinner09532fe2019-05-10 23:39:09 +0200411 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
412 if (tstate == NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100413 Py_FatalError("NULL tstate");
Victor Stinner09532fe2019-05-10 23:39:09 +0200414 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200415 assert(gil_created(&ceval->gil));
416 drop_gil(ceval, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000418}
419
420void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000421PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000422{
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100423 ensure_tstate_not_null(__func__, tstate);
424
425 exit_thread_if_finalizing(tstate);
426 assert(is_tstate_valid(tstate));
Victor Stinner17c68b82020-01-30 12:20:48 +0100427
Victor Stinner01b1cc12019-11-20 02:27:56 +0100428 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner0fd2c302019-06-04 03:15:09 +0200429 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinnere225beb2019-06-03 18:14:24 +0200430 assert(gil_created(&ceval->gil));
Victor Stinner2914bb32018-01-29 11:57:45 +0100431
Victor Stinnere225beb2019-06-03 18:14:24 +0200432 take_gil(ceval, tstate);
Victor Stinner2914bb32018-01-29 11:57:45 +0100433
Victor Stinner09532fe2019-05-10 23:39:09 +0200434 _PyThreadState_Swap(&runtime->gilstate, tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000435}
436
437
Guido van Rossuma9672091994-09-14 13:31:22 +0000438/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
439 signal handlers or Mac I/O completion routines) can schedule calls
440 to a function to be called synchronously.
441 The synchronous function is called with one void* argument.
442 It should return 0 for success or -1 for failure -- failure should
443 be accompanied by an exception.
444
445 If registry succeeds, the registry function returns 0; if it fails
446 (e.g. due to too many pending calls) it returns -1 (without setting
447 an exception condition).
448
449 Note that because registry may occur from within signal handlers,
450 or other asynchronous events, calling malloc() is unsafe!
451
Guido van Rossuma9672091994-09-14 13:31:22 +0000452 Any thread can schedule pending calls, but only the main thread
453 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000454 There is no facility to schedule calls to a particular thread, but
455 that should be easy to change, should that ever be required. In
456 that case, the static variables here should go into the python
457 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000458*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000459
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200460void
Victor Stinnere225beb2019-06-03 18:14:24 +0200461_PyEval_SignalReceived(struct _ceval_runtime_state *ceval)
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200462{
463 /* bpo-30703: Function called when the C signal handler of Python gets a
464 signal. We cannot queue a callback using Py_AddPendingCall() since
465 that function is not async-signal-safe. */
Victor Stinnere225beb2019-06-03 18:14:24 +0200466 SIGNAL_PENDING_SIGNALS(ceval);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200467}
468
Eric Snow5be45a62019-03-08 22:47:07 -0700469/* Push one item onto the queue while holding the lock. */
470static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200471_push_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600472 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700473{
Eric Snow842a2f02019-03-15 15:47:51 -0600474 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700475 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600476 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700477 return -1; /* Queue full */
478 }
Eric Snow842a2f02019-03-15 15:47:51 -0600479 pending->calls[i].func = func;
480 pending->calls[i].arg = arg;
481 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700482 return 0;
483}
484
485/* Pop one item off the queue while holding the lock. */
486static void
Victor Stinnere225beb2019-06-03 18:14:24 +0200487_pop_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600488 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700489{
Eric Snow842a2f02019-03-15 15:47:51 -0600490 int i = pending->first;
491 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700492 return; /* Queue empty */
493 }
494
Eric Snow842a2f02019-03-15 15:47:51 -0600495 *func = pending->calls[i].func;
496 *arg = pending->calls[i].arg;
497 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700498}
499
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200500/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000501 scheduling to be made from any thread, and even from an executing
502 callback.
503 */
504
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000505int
Victor Stinner438a12d2019-05-24 17:01:38 +0200506_PyEval_AddPendingCall(PyThreadState *tstate,
Victor Stinnere225beb2019-06-03 18:14:24 +0200507 struct _ceval_runtime_state *ceval,
Victor Stinner09532fe2019-05-10 23:39:09 +0200508 int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000509{
Victor Stinnere225beb2019-06-03 18:14:24 +0200510 struct _pending_calls *pending = &ceval->pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600511
512 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
513 if (pending->finishing) {
514 PyThread_release_lock(pending->lock);
515
516 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +0200517 _PyErr_Fetch(tstate, &exc, &val, &tb);
518 _PyErr_SetString(tstate, PyExc_SystemError,
Eric Snow842a2f02019-03-15 15:47:51 -0600519 "Py_AddPendingCall: cannot add pending calls "
520 "(Python shutting down)");
Victor Stinner438a12d2019-05-24 17:01:38 +0200521 _PyErr_Print(tstate);
522 _PyErr_Restore(tstate, exc, val, tb);
Eric Snow842a2f02019-03-15 15:47:51 -0600523 return -1;
524 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200525 int result = _push_pending_call(pending, func, arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600526 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700527
Victor Stinnere225beb2019-06-03 18:14:24 +0200528 /* signal main loop */
529 SIGNAL_PENDING_CALLS(ceval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000531}
532
Victor Stinner09532fe2019-05-10 23:39:09 +0200533int
534Py_AddPendingCall(int (*func)(void *), void *arg)
535{
Victor Stinner438a12d2019-05-24 17:01:38 +0200536 _PyRuntimeState *runtime = &_PyRuntime;
537 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinnere225beb2019-06-03 18:14:24 +0200538 return _PyEval_AddPendingCall(tstate, &runtime->ceval, func, arg);
Victor Stinner09532fe2019-05-10 23:39:09 +0200539}
540
Eric Snowfdf282d2019-01-11 14:26:55 -0700541static int
Victor Stinner09532fe2019-05-10 23:39:09 +0200542handle_signals(_PyRuntimeState *runtime)
Eric Snowfdf282d2019-01-11 14:26:55 -0700543{
Victor Stinner3225b9f2020-03-09 20:56:57 +0100544 /* Only handle signals on main thread */
Victor Stinner09532fe2019-05-10 23:39:09 +0200545 if (PyThread_get_thread_ident() != runtime->main_thread) {
Eric Snowfdf282d2019-01-11 14:26:55 -0700546 return 0;
547 }
Eric Snow64d6cc82019-02-23 15:40:43 -0700548 /*
549 * Ensure that the thread isn't currently running some other
550 * interpreter.
551 */
Victor Stinner09532fe2019-05-10 23:39:09 +0200552 PyInterpreterState *interp = _PyRuntimeState_GetThreadState(runtime)->interp;
553 if (interp != runtime->interpreters.main) {
Eric Snow64d6cc82019-02-23 15:40:43 -0700554 return 0;
555 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700556
Victor Stinnere225beb2019-06-03 18:14:24 +0200557 struct _ceval_runtime_state *ceval = &runtime->ceval;
558 UNSIGNAL_PENDING_SIGNALS(ceval);
Eric Snow64d6cc82019-02-23 15:40:43 -0700559 if (_PyErr_CheckSignals() < 0) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200560 SIGNAL_PENDING_SIGNALS(ceval); /* We're not done yet */
Eric Snowfdf282d2019-01-11 14:26:55 -0700561 return -1;
562 }
563 return 0;
564}
565
566static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200567make_pending_calls(_PyRuntimeState *runtime)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000568{
Eric Snow6a150bc2019-06-01 15:39:46 -0600569 static int busy = 0;
Eric Snowb75b1a352019-04-12 10:20:10 -0600570
Victor Stinnere225beb2019-06-03 18:14:24 +0200571 /* only service pending calls on main thread */
572 if (PyThread_get_thread_ident() != runtime->main_thread) {
573 return 0;
574 }
575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 /* don't perform recursive pending calls */
Eric Snowfdf282d2019-01-11 14:26:55 -0700577 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700579 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200580 busy = 1;
Victor Stinnere225beb2019-06-03 18:14:24 +0200581 struct _ceval_runtime_state *ceval = &runtime->ceval;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200582 /* unsignal before starting to call callbacks, so that any callback
583 added in-between re-signals */
Victor Stinnere225beb2019-06-03 18:14:24 +0200584 UNSIGNAL_PENDING_CALLS(ceval);
Eric Snowfdf282d2019-01-11 14:26:55 -0700585 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 /* perform a bounded number of calls, in case of recursion */
Victor Stinnere225beb2019-06-03 18:14:24 +0200588 struct _pending_calls *pending = &ceval->pending;
Eric Snowfdf282d2019-01-11 14:26:55 -0700589 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700590 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 void *arg = NULL;
592
593 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600594 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200595 _pop_pending_call(pending, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600596 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700597
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100598 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700599 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100600 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700601 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700602 res = func(arg);
603 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200604 goto error;
605 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200607
Charles-François Natalif23339a2011-07-23 18:15:43 +0200608 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700609 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200610
611error:
612 busy = 0;
Victor Stinnere225beb2019-06-03 18:14:24 +0200613 SIGNAL_PENDING_CALLS(ceval);
Eric Snowfdf282d2019-01-11 14:26:55 -0700614 return res;
615}
616
Eric Snow842a2f02019-03-15 15:47:51 -0600617void
Victor Stinner2b1df452020-01-13 18:46:59 +0100618_Py_FinishPendingCalls(PyThreadState *tstate)
Eric Snow842a2f02019-03-15 15:47:51 -0600619{
Eric Snow842a2f02019-03-15 15:47:51 -0600620 assert(PyGILState_Check());
621
Victor Stinner2b1df452020-01-13 18:46:59 +0100622 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200623 struct _pending_calls *pending = &runtime->ceval.pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200624
Eric Snow842a2f02019-03-15 15:47:51 -0600625 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
626 pending->finishing = 1;
627 PyThread_release_lock(pending->lock);
628
629 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
630 return;
631 }
632
Victor Stinnere225beb2019-06-03 18:14:24 +0200633 if (make_pending_calls(runtime) < 0) {
634 PyObject *exc, *val, *tb;
635 _PyErr_Fetch(tstate, &exc, &val, &tb);
636 PyErr_BadInternalCall();
637 _PyErr_ChainExceptions(exc, val, tb);
638 _PyErr_Print(tstate);
Eric Snow842a2f02019-03-15 15:47:51 -0600639 }
640}
641
Eric Snowfdf282d2019-01-11 14:26:55 -0700642/* Py_MakePendingCalls() is a simple wrapper for the sake
643 of backward-compatibility. */
644int
645Py_MakePendingCalls(void)
646{
647 assert(PyGILState_Check());
648
649 /* Python signal handler doesn't really queue a callback: it only signals
650 that a signal was received, see _PyEval_SignalReceived(). */
Victor Stinner09532fe2019-05-10 23:39:09 +0200651 _PyRuntimeState *runtime = &_PyRuntime;
652 int res = handle_signals(runtime);
Eric Snowfdf282d2019-01-11 14:26:55 -0700653 if (res != 0) {
654 return res;
655 }
656
Victor Stinnere225beb2019-06-03 18:14:24 +0200657 res = make_pending_calls(runtime);
Eric Snowb75b1a352019-04-12 10:20:10 -0600658 if (res != 0) {
659 return res;
660 }
661
662 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000663}
664
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000665/* The interpreter's recursion limit */
666
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000667#ifndef Py_DEFAULT_RECURSION_LIMIT
668#define Py_DEFAULT_RECURSION_LIMIT 1000
669#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600670
Eric Snow05351c12017-09-05 21:43:08 -0700671int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000672
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600673void
Victor Stinnere225beb2019-06-03 18:14:24 +0200674_PyEval_Initialize(struct _ceval_runtime_state *state)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600675{
Victor Stinnere225beb2019-06-03 18:14:24 +0200676 state->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600677 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Victor Stinnere225beb2019-06-03 18:14:24 +0200678 _gil_initialize(&state->gil);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600679}
680
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000681int
682Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000683{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100684 struct _ceval_runtime_state *ceval = &_PyRuntime.ceval;
685 return ceval->recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000686}
687
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000688void
689Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000690{
Victor Stinnere225beb2019-06-03 18:14:24 +0200691 struct _ceval_runtime_state *ceval = &_PyRuntime.ceval;
692 ceval->recursion_limit = new_limit;
693 _Py_CheckRecursionLimit = ceval->recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000694}
695
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100696/* The function _Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
Armin Rigo2b3eb402003-10-28 12:05:48 +0000697 if the recursion_depth reaches _Py_CheckRecursionLimit.
698 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
699 to guarantee that _Py_CheckRecursiveCall() is regularly called.
700 Without USE_STACKCHECK, there is no need for this. */
701int
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100702_Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000703{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100704 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200705 int recursion_limit = runtime->ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000706
707#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700708 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 if (PyOS_CheckStack()) {
710 --tstate->recursion_depth;
Victor Stinner438a12d2019-05-24 17:01:38 +0200711 _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 return -1;
713 }
pdox18967932017-10-25 23:03:01 -0700714 /* Needed for ABI backwards-compatibility (see bpo-31857) */
Eric Snow05351c12017-09-05 21:43:08 -0700715 _Py_CheckRecursionLimit = recursion_limit;
pdox18967932017-10-25 23:03:01 -0700716#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 if (tstate->recursion_critical)
718 /* Somebody asked that we don't check for recursion. */
719 return 0;
720 if (tstate->overflowed) {
721 if (tstate->recursion_depth > recursion_limit + 50) {
722 /* Overflowing while handling an overflow. Give up. */
723 Py_FatalError("Cannot recover from stack overflow.");
724 }
725 return 0;
726 }
727 if (tstate->recursion_depth > recursion_limit) {
728 --tstate->recursion_depth;
729 tstate->overflowed = 1;
Victor Stinner438a12d2019-05-24 17:01:38 +0200730 _PyErr_Format(tstate, PyExc_RecursionError,
731 "maximum recursion depth exceeded%s",
732 where);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 return -1;
734 }
735 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000736}
737
Victor Stinner09532fe2019-05-10 23:39:09 +0200738static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
Victor Stinner438a12d2019-05-24 17:01:38 +0200739static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000740
Victor Stinnere225beb2019-06-03 18:14:24 +0200741#define _Py_TracingPossible(ceval) ((ceval)->tracing_possible)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000742
Guido van Rossum374a9221991-04-04 10:40:29 +0000743
Guido van Rossumb209a111997-04-29 18:18:01 +0000744PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000745PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000746{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 return PyEval_EvalCodeEx(co,
748 globals, locals,
749 (PyObject **)NULL, 0,
750 (PyObject **)NULL, 0,
751 (PyObject **)NULL, 0,
752 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000753}
754
755
756/* Interpreter main loop */
757
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000758PyObject *
Victor Stinnerb9e68122019-11-14 12:20:46 +0100759PyEval_EvalFrame(PyFrameObject *f)
760{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 /* This is for backward compatibility with extension modules that
762 used this API; core interpreter code should call
763 PyEval_EvalFrameEx() */
Victor Stinnerb9e68122019-11-14 12:20:46 +0100764 PyThreadState *tstate = _PyThreadState_GET();
765 return _PyEval_EvalFrame(tstate, f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000766}
767
768PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000769PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000770{
Victor Stinnerb9e68122019-11-14 12:20:46 +0100771 PyThreadState *tstate = _PyThreadState_GET();
772 return _PyEval_EvalFrame(tstate, f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700773}
774
Victor Stinnerc6944e72016-11-11 02:13:35 +0100775PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700776_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
777{
Guido van Rossum950361c1997-01-24 13:49:28 +0000778#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000780#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200781 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300782 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200783 int opcode; /* Current opcode */
784 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200785 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 PyObject *retval = NULL; /* Return value */
Victor Stinner09532fe2019-05-10 23:39:09 +0200787 _PyRuntimeState * const runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200788 struct _ceval_runtime_state * const ceval = &runtime->ceval;
789 _Py_atomic_int * const eval_breaker = &ceval->eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000791
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100792 PyThreadState * const tstate = _PyRuntimeState_GetThreadState(runtime);
793 ensure_tstate_not_null(__func__, tstate);
794 assert(is_tstate_valid(tstate));
795
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000797
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000799
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 is true when the line being executed has changed. The
801 initial values are such as to make this false the first
802 time it is tested. */
803 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000804
Serhiy Storchakaab874002016-09-11 13:48:15 +0300805 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 PyObject *names;
807 PyObject *consts;
Inada Naoki91234a12019-06-03 21:30:58 +0900808 _PyOpcache *co_opcache;
Guido van Rossum374a9221991-04-04 10:40:29 +0000809
Brett Cannon368b4b72012-04-02 12:17:59 -0400810#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200811 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400812#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200813
Antoine Pitroub52ec782009-01-25 16:34:23 +0000814/* Computed GOTOs, or
815 the-optimization-commonly-but-improperly-known-as-"threaded code"
816 using gcc's labels-as-values extension
817 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
818
819 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000821 combined with a lookup table of jump addresses. However, since the
822 indirect jump instruction is shared by all opcodes, the CPU will have a
823 hard time making the right prediction for where to jump next (actually,
824 it will be always wrong except in the uncommon case of a sequence of
825 several identical opcodes).
826
827 "Threaded code" in contrast, uses an explicit jump table and an explicit
828 indirect jump instruction at the end of each opcode. Since the jump
829 instruction is at a different address for each opcode, the CPU will make a
830 separate prediction for each of these instructions, which is equivalent to
831 predicting the second opcode of each opcode pair. These predictions have
832 a much better chance to turn out valid, especially in small bytecode loops.
833
834 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000836 and potentially many more instructions (depending on the pipeline width).
837 A correctly predicted branch, however, is nearly free.
838
839 At the time of this writing, the "threaded code" version is up to 15-20%
840 faster than the normal "switch" version, depending on the compiler and the
841 CPU architecture.
842
843 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
844 because it would render the measurements invalid.
845
846
847 NOTE: care must be taken that the compiler doesn't try to "optimize" the
848 indirect jumps by sharing them between all opcodes. Such optimizations
849 can be disabled on gcc by using the -fno-gcse flag (or possibly
850 -fno-crossjumping).
851*/
852
Antoine Pitrou042b1282010-08-13 21:15:58 +0000853#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000854#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000855#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000856#endif
857
Antoine Pitrou042b1282010-08-13 21:15:58 +0000858#ifdef HAVE_COMPUTED_GOTOS
859 #ifndef USE_COMPUTED_GOTOS
860 #define USE_COMPUTED_GOTOS 1
861 #endif
862#else
863 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
864 #error "Computed gotos are not supported on this compiler."
865 #endif
866 #undef USE_COMPUTED_GOTOS
867 #define USE_COMPUTED_GOTOS 0
868#endif
869
870#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000871/* Import the static jump table */
872#include "opcode_targets.h"
873
Antoine Pitroub52ec782009-01-25 16:34:23 +0000874#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -0700875 op: \
876 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000877
Antoine Pitroub52ec782009-01-25 16:34:23 +0000878#ifdef LLTRACE
879#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200881 if (!lltrace && !_Py_TracingPossible(ceval) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300883 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300884 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 } \
886 goto fast_next_opcode; \
887 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000888#else
889#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200891 if (!_Py_TracingPossible(ceval) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300893 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300894 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 } \
896 goto fast_next_opcode; \
897 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000898#endif
899
Victor Stinner09532fe2019-05-10 23:39:09 +0200900#define DISPATCH() \
901 { \
902 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
903 FAST_DISPATCH(); \
904 } \
905 continue; \
906 }
907
Antoine Pitroub52ec782009-01-25 16:34:23 +0000908#else
Benjamin Petersonddd19492018-09-16 22:38:02 -0700909#define TARGET(op) op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000910#define FAST_DISPATCH() goto fast_next_opcode
Victor Stinner09532fe2019-05-10 23:39:09 +0200911#define DISPATCH() continue
Antoine Pitroub52ec782009-01-25 16:34:23 +0000912#endif
913
914
Neal Norwitza81d2202002-07-14 00:27:26 +0000915/* Tuple access macros */
916
917#ifndef Py_DEBUG
918#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
919#else
920#define GETITEM(v, i) PyTuple_GetItem((v), (i))
921#endif
922
Guido van Rossum374a9221991-04-04 10:40:29 +0000923/* Code access macros */
924
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300925/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600926#define INSTR_OFFSET() \
927 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300928#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300929 _Py_CODEUNIT word = *next_instr; \
930 opcode = _Py_OPCODE(word); \
931 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300932 next_instr++; \
933 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300934#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
935#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000936
Raymond Hettingerf606f872003-03-16 03:11:04 +0000937/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 Some opcodes tend to come in pairs thus making it possible to
939 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300940 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000941
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 Verifying the prediction costs a single high-speed test of a register
943 variable against a constant. If the pairing was good, then the
944 processor's own internal branch predication has a high likelihood of
945 success, resulting in a nearly zero-overhead transition to the
946 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300947 including its unpredictable switch-case branch. Combined with the
948 processor's internal branch prediction, a successful PREDICT has the
949 effect of making the two opcodes run as if they were a single new opcode
950 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000951
Georg Brandl86b2fb92008-07-16 03:43:04 +0000952 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 predictions turned-on and interpret the results as if some opcodes
954 had been combined or turn-off predictions so that the opcode frequency
955 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000956
957 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 the CPU to record separate branch prediction information for each
959 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000960
Raymond Hettingerf606f872003-03-16 03:11:04 +0000961*/
962
Denis Chernikovbaf29b22020-02-21 12:17:50 +0300963#define PREDICT_ID(op) PRED_##op
964
Antoine Pitrou042b1282010-08-13 21:15:58 +0000965#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Denis Chernikovbaf29b22020-02-21 12:17:50 +0300966#define PREDICT(op) if (0) goto PREDICT_ID(op)
Raymond Hettingera7216982004-02-08 19:59:27 +0000967#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300968#define PREDICT(op) \
Denis Chernikovbaf29b22020-02-21 12:17:50 +0300969 do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300970 _Py_CODEUNIT word = *next_instr; \
971 opcode = _Py_OPCODE(word); \
Denis Chernikovbaf29b22020-02-21 12:17:50 +0300972 if (opcode == op) { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300973 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300974 next_instr++; \
Denis Chernikovbaf29b22020-02-21 12:17:50 +0300975 goto PREDICT_ID(op); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300976 } \
977 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000978#endif
Denis Chernikovbaf29b22020-02-21 12:17:50 +0300979#define PREDICTED(op) PREDICT_ID(op):
Antoine Pitroub52ec782009-01-25 16:34:23 +0000980
Raymond Hettingerf606f872003-03-16 03:11:04 +0000981
Guido van Rossum374a9221991-04-04 10:40:29 +0000982/* Stack manipulation macros */
983
Martin v. Löwis18e16552006-02-15 17:27:45 +0000984/* The stack can grow at most MAXINT deep, as co_nlocals and
985 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000986#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
987#define EMPTY() (STACK_LEVEL() == 0)
988#define TOP() (stack_pointer[-1])
989#define SECOND() (stack_pointer[-2])
990#define THIRD() (stack_pointer[-3])
991#define FOURTH() (stack_pointer[-4])
992#define PEEK(n) (stack_pointer[-(n)])
993#define SET_TOP(v) (stack_pointer[-1] = (v))
994#define SET_SECOND(v) (stack_pointer[-2] = (v))
995#define SET_THIRD(v) (stack_pointer[-3] = (v))
996#define SET_FOURTH(v) (stack_pointer[-4] = (v))
997#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
998#define BASIC_STACKADJ(n) (stack_pointer += n)
999#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1000#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001001
Guido van Rossum96a42c81992-01-12 02:29:51 +00001002#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001004 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001005 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +02001006#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001007 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +00001008#define STACK_GROW(n) do { \
1009 assert(n >= 0); \
1010 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001011 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001012 assert(STACK_LEVEL() <= co->co_stacksize); \
1013 } while (0)
1014#define STACK_SHRINK(n) do { \
1015 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +02001016 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001017 (void)(BASIC_STACKADJ(-n)); \
1018 assert(STACK_LEVEL() <= co->co_stacksize); \
1019 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +00001020#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +02001021 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001022 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001023#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001024#define PUSH(v) BASIC_PUSH(v)
1025#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +00001026#define STACK_GROW(n) BASIC_STACKADJ(n)
1027#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001028#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001029#endif
1030
Guido van Rossum681d79a1995-07-18 14:51:37 +00001031/* Local variable macros */
1032
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001034
1035/* The SETLOCAL() macro must not DECREF the local variable in-place and
1036 then store the new value; it must copy the old value to a temporary
1037 value, then store the new value, and then DECREF the temporary value.
1038 This is because it is possible that during the DECREF the frame is
1039 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1040 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001042 GETLOCAL(i) = value; \
1043 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001044
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001045
1046#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 while (STACK_LEVEL() > (b)->b_level) { \
1048 PyObject *v = POP(); \
1049 Py_XDECREF(v); \
1050 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001051
1052#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001053 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +01001055 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1057 while (STACK_LEVEL() > (b)->b_level + 3) { \
1058 value = POP(); \
1059 Py_XDECREF(value); \
1060 } \
Mark Shannonae3087c2017-10-22 22:41:51 +01001061 exc_info = tstate->exc_info; \
1062 type = exc_info->exc_type; \
1063 value = exc_info->exc_value; \
1064 traceback = exc_info->exc_traceback; \
1065 exc_info->exc_type = POP(); \
1066 exc_info->exc_value = POP(); \
1067 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 Py_XDECREF(type); \
1069 Py_XDECREF(value); \
1070 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001071 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001072
Inada Naoki91234a12019-06-03 21:30:58 +09001073 /* macros for opcode cache */
1074#define OPCACHE_CHECK() \
1075 do { \
1076 co_opcache = NULL; \
1077 if (co->co_opcache != NULL) { \
1078 unsigned char co_opt_offset = \
1079 co->co_opcache_map[next_instr - first_instr]; \
1080 if (co_opt_offset > 0) { \
1081 assert(co_opt_offset <= co->co_opcache_size); \
1082 co_opcache = &co->co_opcache[co_opt_offset - 1]; \
1083 assert(co_opcache != NULL); \
Inada Naoki91234a12019-06-03 21:30:58 +09001084 } \
1085 } \
1086 } while (0)
1087
1088#if OPCACHE_STATS
1089
1090#define OPCACHE_STAT_GLOBAL_HIT() \
1091 do { \
1092 if (co->co_opcache != NULL) opcache_global_hits++; \
1093 } while (0)
1094
1095#define OPCACHE_STAT_GLOBAL_MISS() \
1096 do { \
1097 if (co->co_opcache != NULL) opcache_global_misses++; \
1098 } while (0)
1099
1100#define OPCACHE_STAT_GLOBAL_OPT() \
1101 do { \
1102 if (co->co_opcache != NULL) opcache_global_opts++; \
1103 } while (0)
1104
1105#else /* OPCACHE_STATS */
1106
1107#define OPCACHE_STAT_GLOBAL_HIT()
1108#define OPCACHE_STAT_GLOBAL_MISS()
1109#define OPCACHE_STAT_GLOBAL_OPT()
1110
1111#endif
1112
Guido van Rossuma027efa1997-05-05 20:56:21 +00001113/* Start of code */
1114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 /* push frame */
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001116 if (_Py_EnterRecursiveCall(tstate, "")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001118 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 if (tstate->use_tracing) {
1123 if (tstate->c_tracefunc != NULL) {
1124 /* tstate->c_tracefunc, if defined, is a
1125 function that will be called on *every* entry
1126 to a code block. Its return value, if not
1127 None, is a function that will be called at
1128 the start of each executed line of code.
1129 (Actually, the function must return itself
1130 in order to continue tracing.) The trace
1131 functions are called with three arguments:
1132 a pointer to the current frame, a string
1133 indicating why the function is called, and
1134 an argument which depends on the situation.
1135 The global trace function is also called
1136 whenever an exception is detected. */
1137 if (call_trace_protected(tstate->c_tracefunc,
1138 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001139 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 /* Trace function raised an error */
1141 goto exit_eval_frame;
1142 }
1143 }
1144 if (tstate->c_profilefunc != NULL) {
1145 /* Similar for c_profilefunc, except it needn't
1146 return itself and isn't called for "line" events */
1147 if (call_trace_protected(tstate->c_profilefunc,
1148 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001149 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 /* Profile function raised an error */
1151 goto exit_eval_frame;
1152 }
1153 }
1154 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001155
Łukasz Langaa785c872016-09-09 17:37:37 -07001156 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1157 dtrace_function_entry(f);
1158
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 co = f->f_code;
1160 names = co->co_names;
1161 consts = co->co_consts;
1162 fastlocals = f->f_localsplus;
1163 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001164 assert(PyBytes_Check(co->co_code));
1165 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001166 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1167 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1168 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001169 /*
1170 f->f_lasti refers to the index of the last instruction,
1171 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001172
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001173 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001174 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001175
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001176 When the PREDICT() macros are enabled, some opcode pairs follow in
1177 direct succession without updating f->f_lasti. A successful
1178 prediction effectively links the two codes together as if they
1179 were a single new opcode; accordingly,f->f_lasti will point to
1180 the first code in the pair (for instance, GET_ITER followed by
1181 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001182 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001184 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001185 next_instr = first_instr;
1186 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001187 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1188 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001189 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 stack_pointer = f->f_stacktop;
1191 assert(stack_pointer != NULL);
1192 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001193 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001194
Inada Naoki91234a12019-06-03 21:30:58 +09001195 if (co->co_opcache_flag < OPCACHE_MIN_RUNS) {
1196 co->co_opcache_flag++;
1197 if (co->co_opcache_flag == OPCACHE_MIN_RUNS) {
1198 if (_PyCode_InitOpcache(co) < 0) {
1199 return NULL;
1200 }
1201#if OPCACHE_STATS
1202 opcache_code_objects_extra_mem +=
1203 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1204 sizeof(_PyOpcache) * co->co_opcache_size;
1205 opcache_code_objects++;
1206#endif
1207 }
1208 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001209
Tim Peters5ca576e2001-06-18 22:08:13 +00001210#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001211 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001212#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001213
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001214 if (throwflag) /* support for generator.throw() */
1215 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001216
Victor Stinnerace47d72013-07-18 01:41:08 +02001217#ifdef Py_DEBUG
1218 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001219 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001220 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001221 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001222#endif
1223
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001224main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1227 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001228 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 /* Do periodic things. Doing this every time through
1231 the loop would add too much overhead, so we do it
1232 only every Nth instruction. We also do it if
1233 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1234 event needs attention (e.g. a signal handler or
1235 async I/O handler); see Py_AddPendingCall() and
1236 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001237
Eric Snow7bda9de2019-03-08 17:25:54 -07001238 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001239 opcode = _Py_OPCODE(*next_instr);
1240 if (opcode == SETUP_FINALLY ||
1241 opcode == SETUP_WITH ||
1242 opcode == BEFORE_ASYNC_WITH ||
1243 opcode == YIELD_FROM) {
1244 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001245 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001246 - If we're about to enter the 'with:'. It will prevent
1247 emitting a resource warning in the common idiom
1248 'with open(path) as file:'.
1249 - If we're about to enter the 'async with:'.
1250 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001251 *very* useful, but might help in some cases and it's
1252 traditional)
1253 - If we're resuming a chain of nested 'yield from' or
1254 'await' calls, then each frame is parked with YIELD_FROM
1255 as its next opcode. If the user hit control-C we want to
1256 wait until we've reached the innermost frame before
1257 running the signal handler and raising KeyboardInterrupt
1258 (see bpo-30039).
1259 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 goto fast_next_opcode;
1261 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001262
Victor Stinnere225beb2019-06-03 18:14:24 +02001263 if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
Victor Stinner09532fe2019-05-10 23:39:09 +02001264 if (handle_signals(runtime) != 0) {
Eric Snowfdf282d2019-01-11 14:26:55 -07001265 goto error;
1266 }
1267 }
Victor Stinnere225beb2019-06-03 18:14:24 +02001268 if (_Py_atomic_load_relaxed(&ceval->pending.calls_to_do)) {
1269 if (make_pending_calls(runtime) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001270 goto error;
Eric Snowfdf282d2019-01-11 14:26:55 -07001271 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001272 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001273
Victor Stinnere225beb2019-06-03 18:14:24 +02001274 if (_Py_atomic_load_relaxed(&ceval->gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 /* Give another thread a chance */
Victor Stinner09532fe2019-05-10 23:39:09 +02001276 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +01001277 Py_FatalError("tstate mix-up");
Victor Stinner09532fe2019-05-10 23:39:09 +02001278 }
Victor Stinnere225beb2019-06-03 18:14:24 +02001279 drop_gil(ceval, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280
1281 /* Other threads may run now */
1282
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001283 /* Check if we should make a quick exit. */
Victor Stinner01b1cc12019-11-20 02:27:56 +01001284 exit_thread_if_finalizing(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001285
Victor Stinnereb4e2ae2020-03-08 11:57:45 +01001286 take_gil(ceval, tstate);
1287
Victor Stinner09532fe2019-05-10 23:39:09 +02001288 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +01001289 Py_FatalError("orphan tstate");
Victor Stinner09532fe2019-05-10 23:39:09 +02001290 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 }
1292 /* Check for asynchronous exceptions. */
1293 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001294 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001295 tstate->async_exc = NULL;
Victor Stinnere225beb2019-06-03 18:14:24 +02001296 UNSIGNAL_ASYNC_EXC(ceval);
Victor Stinner438a12d2019-05-24 17:01:38 +02001297 _PyErr_SetNone(tstate, exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001298 Py_DECREF(exc);
1299 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 }
1301 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 fast_next_opcode:
1304 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001305
Łukasz Langaa785c872016-09-09 17:37:37 -07001306 if (PyDTrace_LINE_ENABLED())
1307 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001310
Victor Stinnere225beb2019-06-03 18:14:24 +02001311 if (_Py_TracingPossible(ceval) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001312 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001313 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 /* see maybe_call_line_trace
1315 for expository comments */
1316 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 err = maybe_call_line_trace(tstate->c_tracefunc,
1319 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001320 tstate, f,
1321 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 /* Reload possibly changed frame fields */
1323 JUMPTO(f->f_lasti);
1324 if (f->f_stacktop != NULL) {
1325 stack_pointer = f->f_stacktop;
1326 f->f_stacktop = NULL;
1327 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001328 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001330 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001331 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001334
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001335 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001336 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001337#ifdef DYNAMIC_EXECUTION_PROFILE
1338#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 dxpairs[lastopcode][opcode]++;
1340 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001341#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001343#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001344
Guido van Rossum96a42c81992-01-12 02:29:51 +00001345#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001348 if (lltrace) {
1349 if (HAS_ARG(opcode)) {
1350 printf("%d: %d, %d\n",
1351 f->f_lasti, opcode, oparg);
1352 }
1353 else {
1354 printf("%d: %d\n",
1355 f->f_lasti, opcode);
1356 }
1357 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001358#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001363 It is essential that any operation that fails must goto error
1364 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001365
Benjamin Petersonddd19492018-09-16 22:38:02 -07001366 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001368 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001369
Benjamin Petersonddd19492018-09-16 22:38:02 -07001370 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001371 PyObject *value = GETLOCAL(oparg);
1372 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001373 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001374 UNBOUNDLOCAL_ERROR_MSG,
1375 PyTuple_GetItem(co->co_varnames, oparg));
1376 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001378 Py_INCREF(value);
1379 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001381 }
1382
Benjamin Petersonddd19492018-09-16 22:38:02 -07001383 case TARGET(LOAD_CONST): {
1384 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001385 PyObject *value = GETITEM(consts, oparg);
1386 Py_INCREF(value);
1387 PUSH(value);
1388 FAST_DISPATCH();
1389 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001390
Benjamin Petersonddd19492018-09-16 22:38:02 -07001391 case TARGET(STORE_FAST): {
1392 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001393 PyObject *value = POP();
1394 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001396 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001397
Benjamin Petersonddd19492018-09-16 22:38:02 -07001398 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001399 PyObject *value = POP();
1400 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001402 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001403
Benjamin Petersonddd19492018-09-16 22:38:02 -07001404 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001405 PyObject *top = TOP();
1406 PyObject *second = SECOND();
1407 SET_TOP(second);
1408 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001409 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001410 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001411
Benjamin Petersonddd19492018-09-16 22:38:02 -07001412 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001413 PyObject *top = TOP();
1414 PyObject *second = SECOND();
1415 PyObject *third = THIRD();
1416 SET_TOP(second);
1417 SET_SECOND(third);
1418 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001420 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001421
Benjamin Petersonddd19492018-09-16 22:38:02 -07001422 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001423 PyObject *top = TOP();
1424 PyObject *second = SECOND();
1425 PyObject *third = THIRD();
1426 PyObject *fourth = FOURTH();
1427 SET_TOP(second);
1428 SET_SECOND(third);
1429 SET_THIRD(fourth);
1430 SET_FOURTH(top);
1431 FAST_DISPATCH();
1432 }
1433
Benjamin Petersonddd19492018-09-16 22:38:02 -07001434 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001435 PyObject *top = TOP();
1436 Py_INCREF(top);
1437 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001439 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001440
Benjamin Petersonddd19492018-09-16 22:38:02 -07001441 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001442 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001443 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001444 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001445 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001446 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001447 SET_TOP(top);
1448 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001449 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001450 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001451
Benjamin Petersonddd19492018-09-16 22:38:02 -07001452 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001453 PyObject *value = TOP();
1454 PyObject *res = PyNumber_Positive(value);
1455 Py_DECREF(value);
1456 SET_TOP(res);
1457 if (res == NULL)
1458 goto error;
1459 DISPATCH();
1460 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001461
Benjamin Petersonddd19492018-09-16 22:38:02 -07001462 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001463 PyObject *value = TOP();
1464 PyObject *res = PyNumber_Negative(value);
1465 Py_DECREF(value);
1466 SET_TOP(res);
1467 if (res == NULL)
1468 goto error;
1469 DISPATCH();
1470 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001471
Benjamin Petersonddd19492018-09-16 22:38:02 -07001472 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001473 PyObject *value = TOP();
1474 int err = PyObject_IsTrue(value);
1475 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001476 if (err == 0) {
1477 Py_INCREF(Py_True);
1478 SET_TOP(Py_True);
1479 DISPATCH();
1480 }
1481 else if (err > 0) {
1482 Py_INCREF(Py_False);
1483 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 DISPATCH();
1485 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001486 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001487 goto error;
1488 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001489
Benjamin Petersonddd19492018-09-16 22:38:02 -07001490 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001491 PyObject *value = TOP();
1492 PyObject *res = PyNumber_Invert(value);
1493 Py_DECREF(value);
1494 SET_TOP(res);
1495 if (res == NULL)
1496 goto error;
1497 DISPATCH();
1498 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001499
Benjamin Petersonddd19492018-09-16 22:38:02 -07001500 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001501 PyObject *exp = POP();
1502 PyObject *base = TOP();
1503 PyObject *res = PyNumber_Power(base, exp, Py_None);
1504 Py_DECREF(base);
1505 Py_DECREF(exp);
1506 SET_TOP(res);
1507 if (res == 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_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001513 PyObject *right = POP();
1514 PyObject *left = TOP();
1515 PyObject *res = PyNumber_Multiply(left, right);
1516 Py_DECREF(left);
1517 Py_DECREF(right);
1518 SET_TOP(res);
1519 if (res == NULL)
1520 goto error;
1521 DISPATCH();
1522 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001523
Benjamin Petersonddd19492018-09-16 22:38:02 -07001524 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001525 PyObject *right = POP();
1526 PyObject *left = TOP();
1527 PyObject *res = PyNumber_MatrixMultiply(left, right);
1528 Py_DECREF(left);
1529 Py_DECREF(right);
1530 SET_TOP(res);
1531 if (res == NULL)
1532 goto error;
1533 DISPATCH();
1534 }
1535
Benjamin Petersonddd19492018-09-16 22:38:02 -07001536 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001537 PyObject *divisor = POP();
1538 PyObject *dividend = TOP();
1539 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1540 Py_DECREF(dividend);
1541 Py_DECREF(divisor);
1542 SET_TOP(quotient);
1543 if (quotient == NULL)
1544 goto error;
1545 DISPATCH();
1546 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001547
Benjamin Petersonddd19492018-09-16 22:38:02 -07001548 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001549 PyObject *divisor = POP();
1550 PyObject *dividend = TOP();
1551 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1552 Py_DECREF(dividend);
1553 Py_DECREF(divisor);
1554 SET_TOP(quotient);
1555 if (quotient == NULL)
1556 goto error;
1557 DISPATCH();
1558 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001559
Benjamin Petersonddd19492018-09-16 22:38:02 -07001560 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001561 PyObject *divisor = POP();
1562 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001563 PyObject *res;
1564 if (PyUnicode_CheckExact(dividend) && (
1565 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1566 // fast path; string formatting, but not if the RHS is a str subclass
1567 // (see issue28598)
1568 res = PyUnicode_Format(dividend, divisor);
1569 } else {
1570 res = PyNumber_Remainder(dividend, divisor);
1571 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001572 Py_DECREF(divisor);
1573 Py_DECREF(dividend);
1574 SET_TOP(res);
1575 if (res == NULL)
1576 goto error;
1577 DISPATCH();
1578 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001579
Benjamin Petersonddd19492018-09-16 22:38:02 -07001580 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001581 PyObject *right = POP();
1582 PyObject *left = TOP();
1583 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001584 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1585 CPython using bytecode, it is simply worthless.
1586 See http://bugs.python.org/issue21955 and
1587 http://bugs.python.org/issue10044 for the discussion. In short,
1588 no patch shown any impact on a realistic benchmark, only a minor
1589 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001590 if (PyUnicode_CheckExact(left) &&
1591 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001592 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001593 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001594 }
1595 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001596 sum = PyNumber_Add(left, right);
1597 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001598 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001599 Py_DECREF(right);
1600 SET_TOP(sum);
1601 if (sum == NULL)
1602 goto error;
1603 DISPATCH();
1604 }
1605
Benjamin Petersonddd19492018-09-16 22:38:02 -07001606 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001607 PyObject *right = POP();
1608 PyObject *left = TOP();
1609 PyObject *diff = PyNumber_Subtract(left, right);
1610 Py_DECREF(right);
1611 Py_DECREF(left);
1612 SET_TOP(diff);
1613 if (diff == NULL)
1614 goto error;
1615 DISPATCH();
1616 }
1617
Benjamin Petersonddd19492018-09-16 22:38:02 -07001618 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001619 PyObject *sub = POP();
1620 PyObject *container = TOP();
1621 PyObject *res = PyObject_GetItem(container, sub);
1622 Py_DECREF(container);
1623 Py_DECREF(sub);
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_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001631 PyObject *right = POP();
1632 PyObject *left = TOP();
1633 PyObject *res = PyNumber_Lshift(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_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001643 PyObject *right = POP();
1644 PyObject *left = TOP();
1645 PyObject *res = PyNumber_Rshift(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(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001655 PyObject *right = POP();
1656 PyObject *left = TOP();
1657 PyObject *res = PyNumber_And(left, right);
1658 Py_DECREF(left);
1659 Py_DECREF(right);
1660 SET_TOP(res);
1661 if (res == NULL)
1662 goto error;
1663 DISPATCH();
1664 }
1665
Benjamin Petersonddd19492018-09-16 22:38:02 -07001666 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001667 PyObject *right = POP();
1668 PyObject *left = TOP();
1669 PyObject *res = PyNumber_Xor(left, right);
1670 Py_DECREF(left);
1671 Py_DECREF(right);
1672 SET_TOP(res);
1673 if (res == NULL)
1674 goto error;
1675 DISPATCH();
1676 }
1677
Benjamin Petersonddd19492018-09-16 22:38:02 -07001678 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001679 PyObject *right = POP();
1680 PyObject *left = TOP();
1681 PyObject *res = PyNumber_Or(left, right);
1682 Py_DECREF(left);
1683 Py_DECREF(right);
1684 SET_TOP(res);
1685 if (res == NULL)
1686 goto error;
1687 DISPATCH();
1688 }
1689
Benjamin Petersonddd19492018-09-16 22:38:02 -07001690 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001691 PyObject *v = POP();
1692 PyObject *list = PEEK(oparg);
1693 int err;
1694 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001695 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001696 if (err != 0)
1697 goto error;
1698 PREDICT(JUMP_ABSOLUTE);
1699 DISPATCH();
1700 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001701
Benjamin Petersonddd19492018-09-16 22:38:02 -07001702 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001703 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001704 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001705 int err;
1706 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001707 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001708 if (err != 0)
1709 goto error;
1710 PREDICT(JUMP_ABSOLUTE);
1711 DISPATCH();
1712 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001713
Benjamin Petersonddd19492018-09-16 22:38:02 -07001714 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001715 PyObject *exp = POP();
1716 PyObject *base = TOP();
1717 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1718 Py_DECREF(base);
1719 Py_DECREF(exp);
1720 SET_TOP(res);
1721 if (res == 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_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001727 PyObject *right = POP();
1728 PyObject *left = TOP();
1729 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1730 Py_DECREF(left);
1731 Py_DECREF(right);
1732 SET_TOP(res);
1733 if (res == 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_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001739 PyObject *right = POP();
1740 PyObject *left = TOP();
1741 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1742 Py_DECREF(left);
1743 Py_DECREF(right);
1744 SET_TOP(res);
1745 if (res == NULL)
1746 goto error;
1747 DISPATCH();
1748 }
1749
Benjamin Petersonddd19492018-09-16 22:38:02 -07001750 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001751 PyObject *divisor = POP();
1752 PyObject *dividend = TOP();
1753 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1754 Py_DECREF(dividend);
1755 Py_DECREF(divisor);
1756 SET_TOP(quotient);
1757 if (quotient == NULL)
1758 goto error;
1759 DISPATCH();
1760 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001761
Benjamin Petersonddd19492018-09-16 22:38:02 -07001762 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001763 PyObject *divisor = POP();
1764 PyObject *dividend = TOP();
1765 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1766 Py_DECREF(dividend);
1767 Py_DECREF(divisor);
1768 SET_TOP(quotient);
1769 if (quotient == NULL)
1770 goto error;
1771 DISPATCH();
1772 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001773
Benjamin Petersonddd19492018-09-16 22:38:02 -07001774 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001775 PyObject *right = POP();
1776 PyObject *left = TOP();
1777 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1778 Py_DECREF(left);
1779 Py_DECREF(right);
1780 SET_TOP(mod);
1781 if (mod == NULL)
1782 goto error;
1783 DISPATCH();
1784 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001785
Benjamin Petersonddd19492018-09-16 22:38:02 -07001786 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001787 PyObject *right = POP();
1788 PyObject *left = TOP();
1789 PyObject *sum;
1790 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001791 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001792 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001793 }
1794 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001795 sum = PyNumber_InPlaceAdd(left, right);
1796 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001797 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001798 Py_DECREF(right);
1799 SET_TOP(sum);
1800 if (sum == 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_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001806 PyObject *right = POP();
1807 PyObject *left = TOP();
1808 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1809 Py_DECREF(left);
1810 Py_DECREF(right);
1811 SET_TOP(diff);
1812 if (diff == 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_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001818 PyObject *right = POP();
1819 PyObject *left = TOP();
1820 PyObject *res = PyNumber_InPlaceLshift(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_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001830 PyObject *right = POP();
1831 PyObject *left = TOP();
1832 PyObject *res = PyNumber_InPlaceRshift(left, right);
1833 Py_DECREF(left);
1834 Py_DECREF(right);
1835 SET_TOP(res);
1836 if (res == NULL)
1837 goto error;
1838 DISPATCH();
1839 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001840
Benjamin Petersonddd19492018-09-16 22:38:02 -07001841 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001842 PyObject *right = POP();
1843 PyObject *left = TOP();
1844 PyObject *res = PyNumber_InPlaceAnd(left, right);
1845 Py_DECREF(left);
1846 Py_DECREF(right);
1847 SET_TOP(res);
1848 if (res == NULL)
1849 goto error;
1850 DISPATCH();
1851 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001852
Benjamin Petersonddd19492018-09-16 22:38:02 -07001853 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001854 PyObject *right = POP();
1855 PyObject *left = TOP();
1856 PyObject *res = PyNumber_InPlaceXor(left, right);
1857 Py_DECREF(left);
1858 Py_DECREF(right);
1859 SET_TOP(res);
1860 if (res == NULL)
1861 goto error;
1862 DISPATCH();
1863 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001864
Benjamin Petersonddd19492018-09-16 22:38:02 -07001865 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001866 PyObject *right = POP();
1867 PyObject *left = TOP();
1868 PyObject *res = PyNumber_InPlaceOr(left, right);
1869 Py_DECREF(left);
1870 Py_DECREF(right);
1871 SET_TOP(res);
1872 if (res == NULL)
1873 goto error;
1874 DISPATCH();
1875 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001876
Benjamin Petersonddd19492018-09-16 22:38:02 -07001877 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001878 PyObject *sub = TOP();
1879 PyObject *container = SECOND();
1880 PyObject *v = THIRD();
1881 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001882 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001883 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001884 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001885 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001886 Py_DECREF(container);
1887 Py_DECREF(sub);
1888 if (err != 0)
1889 goto error;
1890 DISPATCH();
1891 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001892
Benjamin Petersonddd19492018-09-16 22:38:02 -07001893 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001894 PyObject *sub = TOP();
1895 PyObject *container = SECOND();
1896 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001897 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00001898 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001899 err = PyObject_DelItem(container, sub);
1900 Py_DECREF(container);
1901 Py_DECREF(sub);
1902 if (err != 0)
1903 goto error;
1904 DISPATCH();
1905 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001906
Benjamin Petersonddd19492018-09-16 22:38:02 -07001907 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01001908 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001909 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001910 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001911 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001912 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001913 _PyErr_SetString(tstate, PyExc_RuntimeError,
1914 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001915 Py_DECREF(value);
1916 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001917 }
Petr Viktorinffd97532020-02-11 17:46:57 +01001918 res = PyObject_CallOneArg(hook, value);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001919 Py_DECREF(value);
1920 if (res == NULL)
1921 goto error;
1922 Py_DECREF(res);
1923 DISPATCH();
1924 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001925
Benjamin Petersonddd19492018-09-16 22:38:02 -07001926 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001927 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001928 switch (oparg) {
1929 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001930 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001931 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001932 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001933 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001934 /* fall through */
1935 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02001936 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001937 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001938 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001939 break;
1940 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02001941 _PyErr_SetString(tstate, PyExc_SystemError,
1942 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001943 break;
1944 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001945 goto error;
1946 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001947
Benjamin Petersonddd19492018-09-16 22:38:02 -07001948 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001949 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001950 assert(f->f_iblock == 0);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00001951 assert(EMPTY());
1952 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001953 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001954
Benjamin Petersonddd19492018-09-16 22:38:02 -07001955 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001956 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001957 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001958 PyObject *obj = TOP();
1959 PyTypeObject *type = Py_TYPE(obj);
1960
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001961 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001962 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001963 }
Yury Selivanov75445082015-05-11 22:57:16 -04001964
1965 if (getter != NULL) {
1966 iter = (*getter)(obj);
1967 Py_DECREF(obj);
1968 if (iter == NULL) {
1969 SET_TOP(NULL);
1970 goto error;
1971 }
1972 }
1973 else {
1974 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02001975 _PyErr_Format(tstate, PyExc_TypeError,
1976 "'async for' requires an object with "
1977 "__aiter__ method, got %.100s",
1978 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001979 Py_DECREF(obj);
1980 goto error;
1981 }
1982
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001983 if (Py_TYPE(iter)->tp_as_async == NULL ||
1984 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001985
Yury Selivanov398ff912017-03-02 22:20:00 -05001986 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02001987 _PyErr_Format(tstate, PyExc_TypeError,
1988 "'async for' received an object from __aiter__ "
1989 "that does not implement __anext__: %.100s",
1990 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001991 Py_DECREF(iter);
1992 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001993 }
1994
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001995 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001996 DISPATCH();
1997 }
1998
Benjamin Petersonddd19492018-09-16 22:38:02 -07001999 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002000 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002001 PyObject *next_iter = NULL;
2002 PyObject *awaitable = NULL;
2003 PyObject *aiter = TOP();
2004 PyTypeObject *type = Py_TYPE(aiter);
2005
Yury Selivanoveb636452016-09-08 22:01:51 -07002006 if (PyAsyncGen_CheckExact(aiter)) {
2007 awaitable = type->tp_as_async->am_anext(aiter);
2008 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002009 goto error;
2010 }
Yury Selivanoveb636452016-09-08 22:01:51 -07002011 } else {
2012 if (type->tp_as_async != NULL){
2013 getter = type->tp_as_async->am_anext;
2014 }
Yury Selivanov75445082015-05-11 22:57:16 -04002015
Yury Selivanoveb636452016-09-08 22:01:51 -07002016 if (getter != NULL) {
2017 next_iter = (*getter)(aiter);
2018 if (next_iter == NULL) {
2019 goto error;
2020 }
2021 }
2022 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02002023 _PyErr_Format(tstate, PyExc_TypeError,
2024 "'async for' requires an iterator with "
2025 "__anext__ method, got %.100s",
2026 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07002027 goto error;
2028 }
Yury Selivanov75445082015-05-11 22:57:16 -04002029
Yury Selivanoveb636452016-09-08 22:01:51 -07002030 awaitable = _PyCoro_GetAwaitableIter(next_iter);
2031 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05002032 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07002033 PyExc_TypeError,
2034 "'async for' received an invalid object "
2035 "from __anext__: %.100s",
2036 Py_TYPE(next_iter)->tp_name);
2037
2038 Py_DECREF(next_iter);
2039 goto error;
2040 } else {
2041 Py_DECREF(next_iter);
2042 }
2043 }
Yury Selivanov75445082015-05-11 22:57:16 -04002044
2045 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002046 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002047 DISPATCH();
2048 }
2049
Benjamin Petersonddd19492018-09-16 22:38:02 -07002050 case TARGET(GET_AWAITABLE): {
2051 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002052 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002053 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002054
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002055 if (iter == NULL) {
Mark Shannonfee55262019-11-21 09:11:43 +00002056 int opcode_at_minus_3 = 0;
2057 if ((next_instr - first_instr) > 2) {
2058 opcode_at_minus_3 = _Py_OPCODE(next_instr[-3]);
2059 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002060 format_awaitable_error(tstate, Py_TYPE(iterable),
Mark Shannonfee55262019-11-21 09:11:43 +00002061 opcode_at_minus_3,
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002062 _Py_OPCODE(next_instr[-2]));
2063 }
2064
Yury Selivanov75445082015-05-11 22:57:16 -04002065 Py_DECREF(iterable);
2066
Yury Selivanovc724bae2016-03-02 11:30:46 -05002067 if (iter != NULL && PyCoro_CheckExact(iter)) {
2068 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2069 if (yf != NULL) {
2070 /* `iter` is a coroutine object that is being
2071 awaited, `yf` is a pointer to the current awaitable
2072 being awaited on. */
2073 Py_DECREF(yf);
2074 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002075 _PyErr_SetString(tstate, PyExc_RuntimeError,
2076 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002077 /* The code below jumps to `error` if `iter` is NULL. */
2078 }
2079 }
2080
Yury Selivanov75445082015-05-11 22:57:16 -04002081 SET_TOP(iter); /* Even if it's NULL */
2082
2083 if (iter == NULL) {
2084 goto error;
2085 }
2086
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002087 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002088 DISPATCH();
2089 }
2090
Benjamin Petersonddd19492018-09-16 22:38:02 -07002091 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002092 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002093 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002094 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002095 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
2096 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002097 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002098 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002099 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002100 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002101 else
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02002102 retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002103 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002104 Py_DECREF(v);
2105 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002106 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002107 if (tstate->c_tracefunc != NULL
Victor Stinner438a12d2019-05-24 17:01:38 +02002108 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002109 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002110 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002111 if (err < 0)
2112 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002113 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002114 SET_TOP(val);
2115 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002116 }
Martin Panter95f53c12016-07-18 08:23:26 +00002117 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002118 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002119 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002120 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002121 f->f_lasti -= sizeof(_Py_CODEUNIT);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002122 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002123 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002124
Benjamin Petersonddd19492018-09-16 22:38:02 -07002125 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002126 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002127
2128 if (co->co_flags & CO_ASYNC_GENERATOR) {
2129 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2130 Py_DECREF(retval);
2131 if (w == NULL) {
2132 retval = NULL;
2133 goto error;
2134 }
2135 retval = w;
2136 }
2137
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002138 f->f_stacktop = stack_pointer;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002139 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002140 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002141
Benjamin Petersonddd19492018-09-16 22:38:02 -07002142 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002143 PyObject *type, *value, *traceback;
2144 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002145 PyTryBlock *b = PyFrame_BlockPop(f);
2146 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002147 _PyErr_SetString(tstate, PyExc_SystemError,
2148 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002149 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002150 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002151 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2152 STACK_LEVEL() <= (b)->b_level + 4);
2153 exc_info = tstate->exc_info;
2154 type = exc_info->exc_type;
2155 value = exc_info->exc_value;
2156 traceback = exc_info->exc_traceback;
2157 exc_info->exc_type = POP();
2158 exc_info->exc_value = POP();
2159 exc_info->exc_traceback = POP();
2160 Py_XDECREF(type);
2161 Py_XDECREF(value);
2162 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002163 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002164 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002165
Benjamin Petersonddd19492018-09-16 22:38:02 -07002166 case TARGET(POP_BLOCK): {
2167 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002168 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002169 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002170 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002171
Mark Shannonfee55262019-11-21 09:11:43 +00002172 case TARGET(RERAISE): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002173 PyObject *exc = POP();
Mark Shannonfee55262019-11-21 09:11:43 +00002174 PyObject *val = POP();
2175 PyObject *tb = POP();
2176 assert(PyExceptionClass_Check(exc));
Victor Stinner61f4db82020-01-28 03:37:45 +01002177 _PyErr_Restore(tstate, exc, val, tb);
Mark Shannonfee55262019-11-21 09:11:43 +00002178 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002179 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002180
Benjamin Petersonddd19492018-09-16 22:38:02 -07002181 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002182 PyObject *exc = POP();
2183 assert(PyExceptionClass_Check(exc));
2184 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2185 PyTryBlock *b = PyFrame_BlockPop(f);
2186 assert(b->b_type == EXCEPT_HANDLER);
2187 Py_DECREF(exc);
2188 UNWIND_EXCEPT_HANDLER(b);
2189 Py_DECREF(POP());
2190 JUMPBY(oparg);
2191 FAST_DISPATCH();
2192 }
2193 else {
2194 PyObject *val = POP();
2195 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002196 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002197 goto exception_unwind;
2198 }
2199 }
2200
Zackery Spytzce6a0702019-08-25 03:44:09 -06002201 case TARGET(LOAD_ASSERTION_ERROR): {
2202 PyObject *value = PyExc_AssertionError;
2203 Py_INCREF(value);
2204 PUSH(value);
2205 FAST_DISPATCH();
2206 }
2207
Benjamin Petersonddd19492018-09-16 22:38:02 -07002208 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002209 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002210
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002211 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002212 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002213 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002214 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002215 if (!_PyErr_Occurred(tstate)) {
2216 _PyErr_SetString(tstate, PyExc_NameError,
2217 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002218 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002219 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002220 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002221 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002222 }
2223 else {
2224 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2225 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002226 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002227 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2228 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002229 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2230 _PyErr_SetString(tstate, PyExc_NameError,
2231 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002232 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002233 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002234 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002235 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002236 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002237 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002238
Benjamin Petersonddd19492018-09-16 22:38:02 -07002239 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002240 PyObject *name = GETITEM(names, oparg);
2241 PyObject *v = POP();
2242 PyObject *ns = f->f_locals;
2243 int err;
2244 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002245 _PyErr_Format(tstate, PyExc_SystemError,
2246 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002247 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002248 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002249 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002250 if (PyDict_CheckExact(ns))
2251 err = PyDict_SetItem(ns, name, v);
2252 else
2253 err = PyObject_SetItem(ns, name, v);
2254 Py_DECREF(v);
2255 if (err != 0)
2256 goto error;
2257 DISPATCH();
2258 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002259
Benjamin Petersonddd19492018-09-16 22:38:02 -07002260 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002261 PyObject *name = GETITEM(names, oparg);
2262 PyObject *ns = f->f_locals;
2263 int err;
2264 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002265 _PyErr_Format(tstate, PyExc_SystemError,
2266 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002267 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002268 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002269 err = PyObject_DelItem(ns, name);
2270 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002271 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002272 NAME_ERROR_MSG,
2273 name);
2274 goto error;
2275 }
2276 DISPATCH();
2277 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002278
Benjamin Petersonddd19492018-09-16 22:38:02 -07002279 case TARGET(UNPACK_SEQUENCE): {
2280 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002281 PyObject *seq = POP(), *item, **items;
2282 if (PyTuple_CheckExact(seq) &&
2283 PyTuple_GET_SIZE(seq) == oparg) {
2284 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002285 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002286 item = items[oparg];
2287 Py_INCREF(item);
2288 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002289 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002290 } else if (PyList_CheckExact(seq) &&
2291 PyList_GET_SIZE(seq) == oparg) {
2292 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002293 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002294 item = items[oparg];
2295 Py_INCREF(item);
2296 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002297 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002298 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002299 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002300 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002301 } else {
2302 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002303 Py_DECREF(seq);
2304 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002305 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002306 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002307 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002308 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002309
Benjamin Petersonddd19492018-09-16 22:38:02 -07002310 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002311 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2312 PyObject *seq = POP();
2313
Victor Stinner438a12d2019-05-24 17:01:38 +02002314 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002315 stack_pointer + totalargs)) {
2316 stack_pointer += totalargs;
2317 } else {
2318 Py_DECREF(seq);
2319 goto error;
2320 }
2321 Py_DECREF(seq);
2322 DISPATCH();
2323 }
2324
Benjamin Petersonddd19492018-09-16 22:38:02 -07002325 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002326 PyObject *name = GETITEM(names, oparg);
2327 PyObject *owner = TOP();
2328 PyObject *v = SECOND();
2329 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002330 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002331 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002332 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002333 Py_DECREF(owner);
2334 if (err != 0)
2335 goto error;
2336 DISPATCH();
2337 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002338
Benjamin Petersonddd19492018-09-16 22:38:02 -07002339 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002340 PyObject *name = GETITEM(names, oparg);
2341 PyObject *owner = POP();
2342 int err;
2343 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2344 Py_DECREF(owner);
2345 if (err != 0)
2346 goto error;
2347 DISPATCH();
2348 }
2349
Benjamin Petersonddd19492018-09-16 22:38:02 -07002350 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002351 PyObject *name = GETITEM(names, oparg);
2352 PyObject *v = POP();
2353 int err;
2354 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002355 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002356 if (err != 0)
2357 goto error;
2358 DISPATCH();
2359 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002360
Benjamin Petersonddd19492018-09-16 22:38:02 -07002361 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002362 PyObject *name = GETITEM(names, oparg);
2363 int err;
2364 err = PyDict_DelItem(f->f_globals, name);
2365 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002366 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2367 format_exc_check_arg(tstate, PyExc_NameError,
2368 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002369 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002370 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002371 }
2372 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002373 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002374
Benjamin Petersonddd19492018-09-16 22:38:02 -07002375 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002376 PyObject *name = GETITEM(names, oparg);
2377 PyObject *locals = f->f_locals;
2378 PyObject *v;
2379 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002380 _PyErr_Format(tstate, PyExc_SystemError,
2381 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002382 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002383 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002384 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002385 v = PyDict_GetItemWithError(locals, name);
2386 if (v != NULL) {
2387 Py_INCREF(v);
2388 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002389 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002390 goto error;
2391 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002392 }
2393 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002394 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002395 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002396 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002397 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002398 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002399 }
2400 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002401 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002402 v = PyDict_GetItemWithError(f->f_globals, name);
2403 if (v != NULL) {
2404 Py_INCREF(v);
2405 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002406 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002407 goto error;
2408 }
2409 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002410 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002411 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002412 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002413 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002414 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002415 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002416 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002417 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002418 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002419 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002420 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002421 }
2422 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002423 v = PyObject_GetItem(f->f_builtins, name);
2424 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002425 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002426 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002427 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002428 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002429 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002430 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002431 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002432 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002433 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002434 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002435 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002436 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002437 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002438
Benjamin Petersonddd19492018-09-16 22:38:02 -07002439 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002440 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002441 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002442 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002443 && PyDict_CheckExact(f->f_builtins))
2444 {
Inada Naoki91234a12019-06-03 21:30:58 +09002445 OPCACHE_CHECK();
2446 if (co_opcache != NULL && co_opcache->optimized > 0) {
2447 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2448
2449 if (lg->globals_ver ==
2450 ((PyDictObject *)f->f_globals)->ma_version_tag
2451 && lg->builtins_ver ==
2452 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2453 {
2454 PyObject *ptr = lg->ptr;
2455 OPCACHE_STAT_GLOBAL_HIT();
2456 assert(ptr != NULL);
2457 Py_INCREF(ptr);
2458 PUSH(ptr);
2459 DISPATCH();
2460 }
2461 }
2462
2463 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002464 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002465 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002466 name);
2467 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002468 if (!_PyErr_OCCURRED()) {
2469 /* _PyDict_LoadGlobal() returns NULL without raising
2470 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002471 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002472 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002473 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002474 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002475 }
Inada Naoki91234a12019-06-03 21:30:58 +09002476
2477 if (co_opcache != NULL) {
2478 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2479
2480 if (co_opcache->optimized == 0) {
2481 /* Wasn't optimized before. */
2482 OPCACHE_STAT_GLOBAL_OPT();
2483 } else {
2484 OPCACHE_STAT_GLOBAL_MISS();
2485 }
2486
2487 co_opcache->optimized = 1;
2488 lg->globals_ver =
2489 ((PyDictObject *)f->f_globals)->ma_version_tag;
2490 lg->builtins_ver =
2491 ((PyDictObject *)f->f_builtins)->ma_version_tag;
2492 lg->ptr = v; /* borrowed */
2493 }
2494
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002495 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002496 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002497 else {
2498 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002499
2500 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09002501 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002502 v = PyObject_GetItem(f->f_globals, name);
2503 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002504 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002505 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002506 }
2507 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002508
Victor Stinnerb4efc962015-11-20 09:24:02 +01002509 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002510 v = PyObject_GetItem(f->f_builtins, name);
2511 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002512 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002513 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002514 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002515 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002516 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002517 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002518 }
2519 }
2520 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002521 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002522 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002523 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002524
Benjamin Petersonddd19492018-09-16 22:38:02 -07002525 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002526 PyObject *v = GETLOCAL(oparg);
2527 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002528 SETLOCAL(oparg, NULL);
2529 DISPATCH();
2530 }
2531 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002532 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002533 UNBOUNDLOCAL_ERROR_MSG,
2534 PyTuple_GetItem(co->co_varnames, oparg)
2535 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002536 goto error;
2537 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002538
Benjamin Petersonddd19492018-09-16 22:38:02 -07002539 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002540 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002541 PyObject *oldobj = PyCell_GET(cell);
2542 if (oldobj != NULL) {
2543 PyCell_SET(cell, NULL);
2544 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002545 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002546 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002547 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002548 goto error;
2549 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002550
Benjamin Petersonddd19492018-09-16 22:38:02 -07002551 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002552 PyObject *cell = freevars[oparg];
2553 Py_INCREF(cell);
2554 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002555 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002556 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002557
Benjamin Petersonddd19492018-09-16 22:38:02 -07002558 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002559 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002560 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002561 assert(locals);
2562 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2563 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2564 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2565 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2566 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002567 value = PyDict_GetItemWithError(locals, name);
2568 if (value != NULL) {
2569 Py_INCREF(value);
2570 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002571 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002572 goto error;
2573 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002574 }
2575 else {
2576 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002577 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002578 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002579 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002580 }
2581 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002582 }
2583 }
2584 if (!value) {
2585 PyObject *cell = freevars[oparg];
2586 value = PyCell_GET(cell);
2587 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002588 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002589 goto error;
2590 }
2591 Py_INCREF(value);
2592 }
2593 PUSH(value);
2594 DISPATCH();
2595 }
2596
Benjamin Petersonddd19492018-09-16 22:38:02 -07002597 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002598 PyObject *cell = freevars[oparg];
2599 PyObject *value = PyCell_GET(cell);
2600 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002601 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002602 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002603 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002604 Py_INCREF(value);
2605 PUSH(value);
2606 DISPATCH();
2607 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002608
Benjamin Petersonddd19492018-09-16 22:38:02 -07002609 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002610 PyObject *v = POP();
2611 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002612 PyObject *oldobj = PyCell_GET(cell);
2613 PyCell_SET(cell, v);
2614 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002615 DISPATCH();
2616 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002617
Benjamin Petersonddd19492018-09-16 22:38:02 -07002618 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002619 PyObject *str;
2620 PyObject *empty = PyUnicode_New(0, 0);
2621 if (empty == NULL) {
2622 goto error;
2623 }
2624 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2625 Py_DECREF(empty);
2626 if (str == NULL)
2627 goto error;
2628 while (--oparg >= 0) {
2629 PyObject *item = POP();
2630 Py_DECREF(item);
2631 }
2632 PUSH(str);
2633 DISPATCH();
2634 }
2635
Benjamin Petersonddd19492018-09-16 22:38:02 -07002636 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002637 PyObject *tup = PyTuple_New(oparg);
2638 if (tup == NULL)
2639 goto error;
2640 while (--oparg >= 0) {
2641 PyObject *item = POP();
2642 PyTuple_SET_ITEM(tup, oparg, item);
2643 }
2644 PUSH(tup);
2645 DISPATCH();
2646 }
2647
Benjamin Petersonddd19492018-09-16 22:38:02 -07002648 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002649 PyObject *list = PyList_New(oparg);
2650 if (list == NULL)
2651 goto error;
2652 while (--oparg >= 0) {
2653 PyObject *item = POP();
2654 PyList_SET_ITEM(list, oparg, item);
2655 }
2656 PUSH(list);
2657 DISPATCH();
2658 }
2659
Mark Shannon13bc1392020-01-23 09:25:17 +00002660 case TARGET(LIST_TO_TUPLE): {
2661 PyObject *list = POP();
2662 PyObject *tuple = PyList_AsTuple(list);
2663 Py_DECREF(list);
2664 if (tuple == NULL) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002665 goto error;
Mark Shannon13bc1392020-01-23 09:25:17 +00002666 }
2667 PUSH(tuple);
2668 DISPATCH();
2669 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002670
Mark Shannon13bc1392020-01-23 09:25:17 +00002671 case TARGET(LIST_EXTEND): {
2672 PyObject *iterable = POP();
2673 PyObject *list = PEEK(oparg);
2674 PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
2675 if (none_val == NULL) {
2676 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01002677 (Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable)))
Mark Shannon13bc1392020-01-23 09:25:17 +00002678 {
Victor Stinner61f4db82020-01-28 03:37:45 +01002679 _PyErr_Clear(tstate);
Mark Shannon13bc1392020-01-23 09:25:17 +00002680 _PyErr_Format(tstate, PyExc_TypeError,
2681 "Value after * must be an iterable, not %.200s",
2682 Py_TYPE(iterable)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002683 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002684 Py_DECREF(iterable);
2685 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002686 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002687 Py_DECREF(none_val);
2688 Py_DECREF(iterable);
2689 DISPATCH();
2690 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002691
Mark Shannon13bc1392020-01-23 09:25:17 +00002692 case TARGET(SET_UPDATE): {
2693 PyObject *iterable = POP();
2694 PyObject *set = PEEK(oparg);
2695 int err = _PySet_Update(set, iterable);
2696 Py_DECREF(iterable);
2697 if (err < 0) {
2698 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002699 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002700 DISPATCH();
2701 }
2702
Benjamin Petersonddd19492018-09-16 22:38:02 -07002703 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002704 PyObject *set = PySet_New(NULL);
2705 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002706 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002707 if (set == NULL)
2708 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002709 for (i = oparg; i > 0; i--) {
2710 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002711 if (err == 0)
2712 err = PySet_Add(set, item);
2713 Py_DECREF(item);
2714 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002715 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002716 if (err != 0) {
2717 Py_DECREF(set);
2718 goto error;
2719 }
2720 PUSH(set);
2721 DISPATCH();
2722 }
2723
Benjamin Petersonddd19492018-09-16 22:38:02 -07002724 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002725 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002726 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2727 if (map == NULL)
2728 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002729 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002730 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002731 PyObject *key = PEEK(2*i);
2732 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002733 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002734 if (err != 0) {
2735 Py_DECREF(map);
2736 goto error;
2737 }
2738 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002739
2740 while (oparg--) {
2741 Py_DECREF(POP());
2742 Py_DECREF(POP());
2743 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002744 PUSH(map);
2745 DISPATCH();
2746 }
2747
Benjamin Petersonddd19492018-09-16 22:38:02 -07002748 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002749 _Py_IDENTIFIER(__annotations__);
2750 int err;
2751 PyObject *ann_dict;
2752 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002753 _PyErr_Format(tstate, PyExc_SystemError,
2754 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002755 goto error;
2756 }
2757 /* check if __annotations__ in locals()... */
2758 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002759 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002760 &PyId___annotations__);
2761 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002762 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002763 goto error;
2764 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002765 /* ...if not, create a new one */
2766 ann_dict = PyDict_New();
2767 if (ann_dict == NULL) {
2768 goto error;
2769 }
2770 err = _PyDict_SetItemId(f->f_locals,
2771 &PyId___annotations__, ann_dict);
2772 Py_DECREF(ann_dict);
2773 if (err != 0) {
2774 goto error;
2775 }
2776 }
2777 }
2778 else {
2779 /* do the same if locals() is not a dict */
2780 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2781 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002782 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002783 }
2784 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2785 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002786 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002787 goto error;
2788 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002789 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002790 ann_dict = PyDict_New();
2791 if (ann_dict == NULL) {
2792 goto error;
2793 }
2794 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2795 Py_DECREF(ann_dict);
2796 if (err != 0) {
2797 goto error;
2798 }
2799 }
2800 else {
2801 Py_DECREF(ann_dict);
2802 }
2803 }
2804 DISPATCH();
2805 }
2806
Benjamin Petersonddd19492018-09-16 22:38:02 -07002807 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002808 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002809 PyObject *map;
2810 PyObject *keys = TOP();
2811 if (!PyTuple_CheckExact(keys) ||
2812 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002813 _PyErr_SetString(tstate, PyExc_SystemError,
2814 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002815 goto error;
2816 }
2817 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2818 if (map == NULL) {
2819 goto error;
2820 }
2821 for (i = oparg; i > 0; i--) {
2822 int err;
2823 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2824 PyObject *value = PEEK(i + 1);
2825 err = PyDict_SetItem(map, key, value);
2826 if (err != 0) {
2827 Py_DECREF(map);
2828 goto error;
2829 }
2830 }
2831
2832 Py_DECREF(POP());
2833 while (oparg--) {
2834 Py_DECREF(POP());
2835 }
2836 PUSH(map);
2837 DISPATCH();
2838 }
2839
Mark Shannon8a4cd702020-01-27 09:57:45 +00002840 case TARGET(DICT_UPDATE): {
2841 PyObject *update = POP();
2842 PyObject *dict = PEEK(oparg);
2843 if (PyDict_Update(dict, update) < 0) {
2844 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
2845 _PyErr_Format(tstate, PyExc_TypeError,
2846 "'%.200s' object is not a mapping",
Victor Stinnera102ed72020-02-07 02:24:48 +01002847 Py_TYPE(update)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002848 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002849 Py_DECREF(update);
2850 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002851 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002852 Py_DECREF(update);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002853 DISPATCH();
2854 }
2855
Mark Shannon8a4cd702020-01-27 09:57:45 +00002856 case TARGET(DICT_MERGE): {
2857 PyObject *update = POP();
2858 PyObject *dict = PEEK(oparg);
2859
2860 if (_PyDict_MergeEx(dict, update, 2) < 0) {
2861 format_kwargs_error(tstate, PEEK(2 + oparg), update);
2862 Py_DECREF(update);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002863 goto error;
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002864 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002865 Py_DECREF(update);
Brandt Bucherf185a732019-09-28 17:12:49 -07002866 PREDICT(CALL_FUNCTION_EX);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002867 DISPATCH();
2868 }
2869
Benjamin Petersonddd19492018-09-16 22:38:02 -07002870 case TARGET(MAP_ADD): {
Jörn Heisslerc8a35412019-06-22 16:40:55 +02002871 PyObject *value = TOP();
2872 PyObject *key = SECOND();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002873 PyObject *map;
2874 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002875 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002876 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002877 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002878 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002879 Py_DECREF(value);
2880 Py_DECREF(key);
2881 if (err != 0)
2882 goto error;
2883 PREDICT(JUMP_ABSOLUTE);
2884 DISPATCH();
2885 }
2886
Benjamin Petersonddd19492018-09-16 22:38:02 -07002887 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002888 PyObject *name = GETITEM(names, oparg);
2889 PyObject *owner = TOP();
2890 PyObject *res = PyObject_GetAttr(owner, name);
2891 Py_DECREF(owner);
2892 SET_TOP(res);
2893 if (res == NULL)
2894 goto error;
2895 DISPATCH();
2896 }
2897
Benjamin Petersonddd19492018-09-16 22:38:02 -07002898 case TARGET(COMPARE_OP): {
Mark Shannon9af0e472020-01-14 10:12:45 +00002899 assert(oparg <= Py_GE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002900 PyObject *right = POP();
2901 PyObject *left = TOP();
Mark Shannon9af0e472020-01-14 10:12:45 +00002902 PyObject *res = PyObject_RichCompare(left, right, oparg);
2903 SET_TOP(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002904 Py_DECREF(left);
2905 Py_DECREF(right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002906 if (res == NULL)
2907 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002908 PREDICT(POP_JUMP_IF_FALSE);
2909 PREDICT(POP_JUMP_IF_TRUE);
2910 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002911 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002912
Mark Shannon9af0e472020-01-14 10:12:45 +00002913 case TARGET(IS_OP): {
2914 PyObject *right = POP();
2915 PyObject *left = TOP();
2916 int res = (left == right)^oparg;
2917 PyObject *b = res ? Py_True : Py_False;
2918 Py_INCREF(b);
2919 SET_TOP(b);
2920 Py_DECREF(left);
2921 Py_DECREF(right);
2922 PREDICT(POP_JUMP_IF_FALSE);
2923 PREDICT(POP_JUMP_IF_TRUE);
2924 FAST_DISPATCH();
2925 }
2926
2927 case TARGET(CONTAINS_OP): {
2928 PyObject *right = POP();
2929 PyObject *left = POP();
2930 int res = PySequence_Contains(right, left);
2931 Py_DECREF(left);
2932 Py_DECREF(right);
2933 if (res < 0) {
2934 goto error;
2935 }
2936 PyObject *b = (res^oparg) ? Py_True : Py_False;
2937 Py_INCREF(b);
2938 PUSH(b);
2939 PREDICT(POP_JUMP_IF_FALSE);
2940 PREDICT(POP_JUMP_IF_TRUE);
2941 FAST_DISPATCH();
2942 }
2943
2944#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
2945 "BaseException is not allowed"
2946
2947 case TARGET(JUMP_IF_NOT_EXC_MATCH): {
2948 PyObject *right = POP();
2949 PyObject *left = POP();
2950 if (PyTuple_Check(right)) {
2951 Py_ssize_t i, length;
2952 length = PyTuple_GET_SIZE(right);
2953 for (i = 0; i < length; i++) {
2954 PyObject *exc = PyTuple_GET_ITEM(right, i);
2955 if (!PyExceptionClass_Check(exc)) {
2956 _PyErr_SetString(tstate, PyExc_TypeError,
2957 CANNOT_CATCH_MSG);
2958 Py_DECREF(left);
2959 Py_DECREF(right);
2960 goto error;
2961 }
2962 }
2963 }
2964 else {
2965 if (!PyExceptionClass_Check(right)) {
2966 _PyErr_SetString(tstate, PyExc_TypeError,
2967 CANNOT_CATCH_MSG);
2968 Py_DECREF(left);
2969 Py_DECREF(right);
2970 goto error;
2971 }
2972 }
2973 int res = PyErr_GivenExceptionMatches(left, right);
2974 Py_DECREF(left);
2975 Py_DECREF(right);
2976 if (res > 0) {
2977 /* Exception matches -- Do nothing */;
2978 }
2979 else if (res == 0) {
2980 JUMPTO(oparg);
2981 }
2982 else {
2983 goto error;
2984 }
2985 DISPATCH();
2986 }
2987
Benjamin Petersonddd19492018-09-16 22:38:02 -07002988 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002989 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002990 PyObject *fromlist = POP();
2991 PyObject *level = TOP();
2992 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02002993 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002994 Py_DECREF(level);
2995 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002996 SET_TOP(res);
2997 if (res == NULL)
2998 goto error;
2999 DISPATCH();
3000 }
3001
Benjamin Petersonddd19492018-09-16 22:38:02 -07003002 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003003 PyObject *from = POP(), *locals;
3004 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003005 if (PyFrame_FastToLocalsWithError(f) < 0) {
3006 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01003007 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003008 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01003009
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003010 locals = f->f_locals;
3011 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003012 _PyErr_SetString(tstate, PyExc_SystemError,
3013 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003014 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003015 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003016 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003017 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003018 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003019 Py_DECREF(from);
3020 if (err != 0)
3021 goto error;
3022 DISPATCH();
3023 }
Guido van Rossum25831651993-05-19 14:50:45 +00003024
Benjamin Petersonddd19492018-09-16 22:38:02 -07003025 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003026 PyObject *name = GETITEM(names, oparg);
3027 PyObject *from = TOP();
3028 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003029 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003030 PUSH(res);
3031 if (res == NULL)
3032 goto error;
3033 DISPATCH();
3034 }
Thomas Wouters52152252000-08-17 22:55:00 +00003035
Benjamin Petersonddd19492018-09-16 22:38:02 -07003036 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003037 JUMPBY(oparg);
3038 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003039 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003040
Benjamin Petersonddd19492018-09-16 22:38:02 -07003041 case TARGET(POP_JUMP_IF_FALSE): {
3042 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003043 PyObject *cond = POP();
3044 int err;
3045 if (cond == Py_True) {
3046 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003047 FAST_DISPATCH();
3048 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003049 if (cond == Py_False) {
3050 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003051 JUMPTO(oparg);
3052 FAST_DISPATCH();
3053 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003054 err = PyObject_IsTrue(cond);
3055 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003056 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003057 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003058 else if (err == 0)
3059 JUMPTO(oparg);
3060 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003061 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003062 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003063 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003064
Benjamin Petersonddd19492018-09-16 22:38:02 -07003065 case TARGET(POP_JUMP_IF_TRUE): {
3066 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003067 PyObject *cond = POP();
3068 int err;
3069 if (cond == Py_False) {
3070 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003071 FAST_DISPATCH();
3072 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003073 if (cond == Py_True) {
3074 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003075 JUMPTO(oparg);
3076 FAST_DISPATCH();
3077 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003078 err = PyObject_IsTrue(cond);
3079 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003080 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003081 JUMPTO(oparg);
3082 }
3083 else if (err == 0)
3084 ;
3085 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003086 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003087 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003088 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003089
Benjamin Petersonddd19492018-09-16 22:38:02 -07003090 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003091 PyObject *cond = TOP();
3092 int err;
3093 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003094 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003095 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003096 FAST_DISPATCH();
3097 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003098 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003099 JUMPTO(oparg);
3100 FAST_DISPATCH();
3101 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003102 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003103 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003104 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003105 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003106 }
3107 else if (err == 0)
3108 JUMPTO(oparg);
3109 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003110 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003111 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003112 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003113
Benjamin Petersonddd19492018-09-16 22:38:02 -07003114 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003115 PyObject *cond = TOP();
3116 int err;
3117 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003118 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003119 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003120 FAST_DISPATCH();
3121 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003122 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003123 JUMPTO(oparg);
3124 FAST_DISPATCH();
3125 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003126 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003127 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003128 JUMPTO(oparg);
3129 }
3130 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003131 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003132 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003133 }
3134 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003135 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003136 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003137 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003138
Benjamin Petersonddd19492018-09-16 22:38:02 -07003139 case TARGET(JUMP_ABSOLUTE): {
3140 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003141 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003142#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003143 /* Enabling this path speeds-up all while and for-loops by bypassing
3144 the per-loop checks for signals. By default, this should be turned-off
3145 because it prevents detection of a control-break in tight loops like
3146 "while 1: pass". Compile with this option turned-on when you need
3147 the speed-up and do not need break checking inside tight loops (ones
3148 that contain only instructions ending with FAST_DISPATCH).
3149 */
3150 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003151#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003152 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003153#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003154 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003155
Benjamin Petersonddd19492018-09-16 22:38:02 -07003156 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003157 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003158 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003159 PyObject *iter = PyObject_GetIter(iterable);
3160 Py_DECREF(iterable);
3161 SET_TOP(iter);
3162 if (iter == NULL)
3163 goto error;
3164 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003165 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003166 DISPATCH();
3167 }
3168
Benjamin Petersonddd19492018-09-16 22:38:02 -07003169 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04003170 /* before: [obj]; after [getiter(obj)] */
3171 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003172 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003173 if (PyCoro_CheckExact(iterable)) {
3174 /* `iterable` is a coroutine */
3175 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3176 /* and it is used in a 'yield from' expression of a
3177 regular generator. */
3178 Py_DECREF(iterable);
3179 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003180 _PyErr_SetString(tstate, PyExc_TypeError,
3181 "cannot 'yield from' a coroutine object "
3182 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04003183 goto error;
3184 }
3185 }
3186 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003187 /* `iterable` is not a generator. */
3188 iter = PyObject_GetIter(iterable);
3189 Py_DECREF(iterable);
3190 SET_TOP(iter);
3191 if (iter == NULL)
3192 goto error;
3193 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003194 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003195 DISPATCH();
3196 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003197
Benjamin Petersonddd19492018-09-16 22:38:02 -07003198 case TARGET(FOR_ITER): {
3199 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003200 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003201 PyObject *iter = TOP();
Victor Stinnera102ed72020-02-07 02:24:48 +01003202 PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003203 if (next != NULL) {
3204 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003205 PREDICT(STORE_FAST);
3206 PREDICT(UNPACK_SEQUENCE);
3207 DISPATCH();
3208 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003209 if (_PyErr_Occurred(tstate)) {
3210 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003211 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003212 }
3213 else if (tstate->c_tracefunc != NULL) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003214 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Victor Stinner438a12d2019-05-24 17:01:38 +02003215 }
3216 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003217 }
3218 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003219 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003220 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003221 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003222 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003223 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003224 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003225
Benjamin Petersonddd19492018-09-16 22:38:02 -07003226 case TARGET(SETUP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003227 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003228 STACK_LEVEL());
3229 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003230 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003231
Benjamin Petersonddd19492018-09-16 22:38:02 -07003232 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003233 _Py_IDENTIFIER(__aenter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003234 _Py_IDENTIFIER(__aexit__);
Yury Selivanov75445082015-05-11 22:57:16 -04003235 PyObject *mgr = TOP();
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003236 PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04003237 PyObject *res;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003238 if (enter == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003239 goto error;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003240 }
3241 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__);
3242 if (exit == NULL) {
3243 Py_DECREF(enter);
3244 goto error;
3245 }
Yury Selivanov75445082015-05-11 22:57:16 -04003246 SET_TOP(exit);
Yury Selivanov75445082015-05-11 22:57:16 -04003247 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003248 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003249 Py_DECREF(enter);
3250 if (res == NULL)
3251 goto error;
3252 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003253 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003254 DISPATCH();
3255 }
3256
Benjamin Petersonddd19492018-09-16 22:38:02 -07003257 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003258 PyObject *res = POP();
3259 /* Setup the finally block before pushing the result
3260 of __aenter__ on the stack. */
3261 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3262 STACK_LEVEL());
3263 PUSH(res);
3264 DISPATCH();
3265 }
3266
Benjamin Petersonddd19492018-09-16 22:38:02 -07003267 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003268 _Py_IDENTIFIER(__enter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003269 _Py_IDENTIFIER(__exit__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003270 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003271 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003272 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003273 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08003274 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003275 }
3276 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003277 if (exit == NULL) {
3278 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003279 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003280 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003281 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003282 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003283 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003284 Py_DECREF(enter);
3285 if (res == NULL)
3286 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003287 /* Setup the finally block before pushing the result
3288 of __enter__ on the stack. */
3289 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3290 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003291
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003292 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003293 DISPATCH();
3294 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003295
Mark Shannonfee55262019-11-21 09:11:43 +00003296 case TARGET(WITH_EXCEPT_START): {
3297 /* At the top of the stack are 7 values:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003298 - (TOP, SECOND, THIRD) = exc_info()
Mark Shannonfee55262019-11-21 09:11:43 +00003299 - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER
3300 - SEVENTH: the context.__exit__ bound method
3301 We call SEVENTH(TOP, SECOND, THIRD).
3302 Then we push again the TOP exception and the __exit__
3303 return value.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003304 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003305 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003306 PyObject *exc, *val, *tb, *res;
3307
Victor Stinner842cfff2016-12-01 14:45:31 +01003308 exc = TOP();
Mark Shannonfee55262019-11-21 09:11:43 +00003309 val = SECOND();
3310 tb = THIRD();
3311 assert(exc != Py_None);
3312 assert(!PyLong_Check(exc));
3313 exit_func = PEEK(7);
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003314 PyObject *stack[4] = {NULL, exc, val, tb};
Petr Viktorinffd97532020-02-11 17:46:57 +01003315 res = PyObject_Vectorcall(exit_func, stack + 1,
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003316 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003317 if (res == NULL)
3318 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003319
Yury Selivanov75445082015-05-11 22:57:16 -04003320 PUSH(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003321 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003322 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003323
Benjamin Petersonddd19492018-09-16 22:38:02 -07003324 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10003325 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003326 PyObject *name = GETITEM(names, oparg);
3327 PyObject *obj = TOP();
3328 PyObject *meth = NULL;
3329
3330 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3331
Yury Selivanovf2392132016-12-13 19:03:51 -05003332 if (meth == NULL) {
3333 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003334 goto error;
3335 }
3336
3337 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003338 /* We can bypass temporary bound method object.
3339 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003340
INADA Naoki015bce62017-01-16 17:23:30 +09003341 meth | self | arg1 | ... | argN
3342 */
3343 SET_TOP(meth);
3344 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003345 }
3346 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003347 /* meth is not an unbound method (but a regular attr, or
3348 something was returned by a descriptor protocol). Set
3349 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003350 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003351
3352 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003353 */
INADA Naoki015bce62017-01-16 17:23:30 +09003354 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003355 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003356 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003357 }
3358 DISPATCH();
3359 }
3360
Benjamin Petersonddd19492018-09-16 22:38:02 -07003361 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003362 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003363 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003364
3365 sp = stack_pointer;
3366
INADA Naoki015bce62017-01-16 17:23:30 +09003367 meth = PEEK(oparg + 2);
3368 if (meth == NULL) {
3369 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3370 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003371
3372 Stack layout:
3373
INADA Naoki015bce62017-01-16 17:23:30 +09003374 ... | NULL | callable | arg1 | ... | argN
3375 ^- TOP()
3376 ^- (-oparg)
3377 ^- (-oparg-1)
3378 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003379
Ville Skyttä49b27342017-08-03 09:00:59 +03003380 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003381 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003382 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003383 res = call_function(tstate, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003384 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003385 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003386 }
3387 else {
3388 /* This is a method call. Stack layout:
3389
INADA Naoki015bce62017-01-16 17:23:30 +09003390 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003391 ^- TOP()
3392 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003393 ^- (-oparg-1)
3394 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003395
INADA Naoki015bce62017-01-16 17:23:30 +09003396 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003397 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003398 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003399 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003400 res = call_function(tstate, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003401 stack_pointer = sp;
3402 }
3403
3404 PUSH(res);
3405 if (res == NULL)
3406 goto error;
3407 DISPATCH();
3408 }
3409
Benjamin Petersonddd19492018-09-16 22:38:02 -07003410 case TARGET(CALL_FUNCTION): {
3411 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003412 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003413 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003414 res = call_function(tstate, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003415 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003416 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003417 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003418 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003419 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003420 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003421 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003422
Benjamin Petersonddd19492018-09-16 22:38:02 -07003423 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003424 PyObject **sp, *res, *names;
3425
3426 names = POP();
Jeroen Demeyer05677862019-08-16 12:41:27 +02003427 assert(PyTuple_Check(names));
3428 assert(PyTuple_GET_SIZE(names) <= oparg);
3429 /* We assume without checking that names contains only strings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003430 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003431 res = call_function(tstate, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003432 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003433 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003434 Py_DECREF(names);
3435
3436 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003437 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003438 }
3439 DISPATCH();
3440 }
3441
Benjamin Petersonddd19492018-09-16 22:38:02 -07003442 case TARGET(CALL_FUNCTION_EX): {
Brandt Bucherf185a732019-09-28 17:12:49 -07003443 PREDICTED(CALL_FUNCTION_EX);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003444 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003445 if (oparg & 0x01) {
3446 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003447 if (!PyDict_CheckExact(kwargs)) {
3448 PyObject *d = PyDict_New();
3449 if (d == NULL)
3450 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003451 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003452 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02003453 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003454 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003455 goto error;
3456 }
3457 Py_DECREF(kwargs);
3458 kwargs = d;
3459 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003460 assert(PyDict_CheckExact(kwargs));
3461 }
3462 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003463 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003464 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003465 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003466 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003467 goto error;
3468 }
3469 Py_SETREF(callargs, PySequence_Tuple(callargs));
3470 if (callargs == NULL) {
3471 goto error;
3472 }
3473 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003474 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003475
Victor Stinner09532fe2019-05-10 23:39:09 +02003476 result = do_call_core(tstate, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003477 Py_DECREF(func);
3478 Py_DECREF(callargs);
3479 Py_XDECREF(kwargs);
3480
3481 SET_TOP(result);
3482 if (result == NULL) {
3483 goto error;
3484 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003485 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003486 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003487
Benjamin Petersonddd19492018-09-16 22:38:02 -07003488 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003489 PyObject *qualname = POP();
3490 PyObject *codeobj = POP();
3491 PyFunctionObject *func = (PyFunctionObject *)
3492 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003493
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003494 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003495 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003496 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003497 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003498 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003499
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003500 if (oparg & 0x08) {
3501 assert(PyTuple_CheckExact(TOP()));
3502 func ->func_closure = POP();
3503 }
3504 if (oparg & 0x04) {
3505 assert(PyDict_CheckExact(TOP()));
3506 func->func_annotations = POP();
3507 }
3508 if (oparg & 0x02) {
3509 assert(PyDict_CheckExact(TOP()));
3510 func->func_kwdefaults = POP();
3511 }
3512 if (oparg & 0x01) {
3513 assert(PyTuple_CheckExact(TOP()));
3514 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003515 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003516
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003517 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003518 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003519 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003520
Benjamin Petersonddd19492018-09-16 22:38:02 -07003521 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003522 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003523 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003524 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003525 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003526 step = NULL;
3527 stop = POP();
3528 start = TOP();
3529 slice = PySlice_New(start, stop, step);
3530 Py_DECREF(start);
3531 Py_DECREF(stop);
3532 Py_XDECREF(step);
3533 SET_TOP(slice);
3534 if (slice == NULL)
3535 goto error;
3536 DISPATCH();
3537 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003538
Benjamin Petersonddd19492018-09-16 22:38:02 -07003539 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003540 /* Handles f-string value formatting. */
3541 PyObject *result;
3542 PyObject *fmt_spec;
3543 PyObject *value;
3544 PyObject *(*conv_fn)(PyObject *);
3545 int which_conversion = oparg & FVC_MASK;
3546 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3547
3548 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003549 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003550
3551 /* See if any conversion is specified. */
3552 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003553 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003554 case FVC_STR: conv_fn = PyObject_Str; break;
3555 case FVC_REPR: conv_fn = PyObject_Repr; break;
3556 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003557 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02003558 _PyErr_Format(tstate, PyExc_SystemError,
3559 "unexpected conversion flag %d",
3560 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003561 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003562 }
3563
3564 /* If there's a conversion function, call it and replace
3565 value with that result. Otherwise, just use value,
3566 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003567 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003568 result = conv_fn(value);
3569 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003570 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003571 Py_XDECREF(fmt_spec);
3572 goto error;
3573 }
3574 value = result;
3575 }
3576
3577 /* If value is a unicode object, and there's no fmt_spec,
3578 then we know the result of format(value) is value
3579 itself. In that case, skip calling format(). I plan to
3580 move this optimization in to PyObject_Format()
3581 itself. */
3582 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3583 /* Do nothing, just transfer ownership to result. */
3584 result = value;
3585 } else {
3586 /* Actually call format(). */
3587 result = PyObject_Format(value, fmt_spec);
3588 Py_DECREF(value);
3589 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003590 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003591 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003592 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003593 }
3594
Eric V. Smith135d5f42016-02-05 18:23:08 -05003595 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003596 DISPATCH();
3597 }
3598
Benjamin Petersonddd19492018-09-16 22:38:02 -07003599 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003600 int oldoparg = oparg;
3601 NEXTOPARG();
3602 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003603 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003604 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003605
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003606
Antoine Pitrou042b1282010-08-13 21:15:58 +00003607#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003608 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003609#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003610 default:
3611 fprintf(stderr,
3612 "XXX lineno: %d, opcode: %d\n",
3613 PyFrame_GetLineNumber(f),
3614 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02003615 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003616 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003617
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003618 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003619
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003620 /* This should never be reached. Every opcode should end with DISPATCH()
3621 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003622 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003623
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003624error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003625 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003626#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02003627 if (!_PyErr_Occurred(tstate)) {
3628 _PyErr_SetString(tstate, PyExc_SystemError,
3629 "error return without exception set");
3630 }
Victor Stinner365b6932013-07-12 00:11:58 +02003631#else
Victor Stinner438a12d2019-05-24 17:01:38 +02003632 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02003633#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003634
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003635 /* Log traceback info. */
3636 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003637
Benjamin Peterson51f46162013-01-23 08:38:47 -05003638 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003639 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3640 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003641
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003642exception_unwind:
3643 /* Unwind stacks if an exception occurred */
3644 while (f->f_iblock > 0) {
3645 /* Pop the current block. */
3646 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003648 if (b->b_type == EXCEPT_HANDLER) {
3649 UNWIND_EXCEPT_HANDLER(b);
3650 continue;
3651 }
3652 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003653 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003654 PyObject *exc, *val, *tb;
3655 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003656 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003657 /* Beware, this invalidates all b->b_* fields */
3658 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003659 PUSH(exc_info->exc_traceback);
3660 PUSH(exc_info->exc_value);
3661 if (exc_info->exc_type != NULL) {
3662 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003663 }
3664 else {
3665 Py_INCREF(Py_None);
3666 PUSH(Py_None);
3667 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003668 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003669 /* Make the raw exception data
3670 available to the handler,
3671 so a program can emulate the
3672 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02003673 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003674 if (tb != NULL)
3675 PyException_SetTraceback(val, tb);
3676 else
3677 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003678 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003679 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003680 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003681 exc_info->exc_value = val;
3682 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003683 if (tb == NULL)
3684 tb = Py_None;
3685 Py_INCREF(tb);
3686 PUSH(tb);
3687 PUSH(val);
3688 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003689 JUMPTO(handler);
Pablo Galindo4c53e632020-01-10 09:24:22 +00003690 if (_Py_TracingPossible(ceval)) {
3691 int needs_new_execution_window = (f->f_lasti < instr_lb || f->f_lasti >= instr_ub);
3692 int needs_line_update = (f->f_lasti == instr_lb || f->f_lasti < instr_prev);
3693 /* Make sure that we trace line after exception if we are in a new execution
3694 * window or we don't need a line update and we are not in the first instruction
3695 * of the line. */
3696 if (needs_new_execution_window || (!needs_line_update && instr_lb > 0)) {
3697 instr_prev = INT_MAX;
3698 }
Mark Shannonfee55262019-11-21 09:11:43 +00003699 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003700 /* Resume normal execution */
3701 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003702 }
3703 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003704
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003705 /* End the loop as we still have an error */
3706 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003707 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003708
Pablo Galindof00828a2019-05-09 16:52:02 +01003709 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003710 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01003711
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003712 /* Pop remaining stack entries. */
3713 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003714 PyObject *o = POP();
3715 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003716 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003717
Mark Shannone7c9f4a2020-01-13 12:51:26 +00003718exiting:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003719 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003720 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003721 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3722 tstate, f, PyTrace_RETURN, retval)) {
3723 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003724 }
3725 }
3726 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003727 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3728 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003729 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003730 }
3731 }
3732 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003733
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003734 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003735exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003736 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3737 dtrace_function_return(f);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01003738 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrou58720d62013-08-05 23:26:40 +02003739 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003740 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003741
Victor Stinner17269092019-11-05 01:22:12 +01003742 return _Py_CheckFunctionResult(tstate, NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003743}
3744
Benjamin Petersonb204a422011-06-05 22:04:07 -05003745static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003746format_missing(PyThreadState *tstate, const char *kind,
3747 PyCodeObject *co, PyObject *names)
Benjamin Petersone109c702011-06-24 09:37:26 -05003748{
3749 int err;
3750 Py_ssize_t len = PyList_GET_SIZE(names);
3751 PyObject *name_str, *comma, *tail, *tmp;
3752
3753 assert(PyList_CheckExact(names));
3754 assert(len >= 1);
3755 /* Deal with the joys of natural language. */
3756 switch (len) {
3757 case 1:
3758 name_str = PyList_GET_ITEM(names, 0);
3759 Py_INCREF(name_str);
3760 break;
3761 case 2:
3762 name_str = PyUnicode_FromFormat("%U and %U",
3763 PyList_GET_ITEM(names, len - 2),
3764 PyList_GET_ITEM(names, len - 1));
3765 break;
3766 default:
3767 tail = PyUnicode_FromFormat(", %U, and %U",
3768 PyList_GET_ITEM(names, len - 2),
3769 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003770 if (tail == NULL)
3771 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003772 /* Chop off the last two objects in the list. This shouldn't actually
3773 fail, but we can't be too careful. */
3774 err = PyList_SetSlice(names, len - 2, len, NULL);
3775 if (err == -1) {
3776 Py_DECREF(tail);
3777 return;
3778 }
3779 /* Stitch everything up into a nice comma-separated list. */
3780 comma = PyUnicode_FromString(", ");
3781 if (comma == NULL) {
3782 Py_DECREF(tail);
3783 return;
3784 }
3785 tmp = PyUnicode_Join(comma, names);
3786 Py_DECREF(comma);
3787 if (tmp == NULL) {
3788 Py_DECREF(tail);
3789 return;
3790 }
3791 name_str = PyUnicode_Concat(tmp, tail);
3792 Py_DECREF(tmp);
3793 Py_DECREF(tail);
3794 break;
3795 }
3796 if (name_str == NULL)
3797 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02003798 _PyErr_Format(tstate, PyExc_TypeError,
3799 "%U() missing %i required %s argument%s: %U",
3800 co->co_name,
3801 len,
3802 kind,
3803 len == 1 ? "" : "s",
3804 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05003805 Py_DECREF(name_str);
3806}
3807
3808static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003809missing_arguments(PyThreadState *tstate, PyCodeObject *co,
3810 Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003811 PyObject **fastlocals)
3812{
Victor Stinner74319ae2016-08-25 00:04:09 +02003813 Py_ssize_t i, j = 0;
3814 Py_ssize_t start, end;
3815 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003816 const char *kind = positional ? "positional" : "keyword-only";
3817 PyObject *missing_names;
3818
3819 /* Compute the names of the arguments that are missing. */
3820 missing_names = PyList_New(missing);
3821 if (missing_names == NULL)
3822 return;
3823 if (positional) {
3824 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01003825 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003826 }
3827 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003828 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003829 end = start + co->co_kwonlyargcount;
3830 }
3831 for (i = start; i < end; i++) {
3832 if (GETLOCAL(i) == NULL) {
3833 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3834 PyObject *name = PyObject_Repr(raw);
3835 if (name == NULL) {
3836 Py_DECREF(missing_names);
3837 return;
3838 }
3839 PyList_SET_ITEM(missing_names, j++, name);
3840 }
3841 }
3842 assert(j == missing);
Victor Stinner438a12d2019-05-24 17:01:38 +02003843 format_missing(tstate, kind, co, missing_names);
Benjamin Petersone109c702011-06-24 09:37:26 -05003844 Py_DECREF(missing_names);
3845}
3846
3847static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003848too_many_positional(PyThreadState *tstate, PyCodeObject *co,
3849 Py_ssize_t given, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003850 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003851{
3852 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003853 Py_ssize_t kwonly_given = 0;
3854 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003855 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003856 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003857
Benjamin Petersone109c702011-06-24 09:37:26 -05003858 assert((co->co_flags & CO_VARARGS) == 0);
3859 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01003860 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003861 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003862 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003863 }
3864 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003865 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01003866 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003867 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01003868 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003869 }
3870 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003871 plural = (co_argcount != 1);
3872 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003873 }
3874 if (sig == NULL)
3875 return;
3876 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003877 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3878 kwonly_sig = PyUnicode_FromFormat(format,
3879 given != 1 ? "s" : "",
3880 kwonly_given,
3881 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003882 if (kwonly_sig == NULL) {
3883 Py_DECREF(sig);
3884 return;
3885 }
3886 }
3887 else {
3888 /* This will not fail. */
3889 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003890 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003891 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003892 _PyErr_Format(tstate, PyExc_TypeError,
3893 "%U() takes %U positional argument%s but %zd%U %s given",
3894 co->co_name,
3895 sig,
3896 plural ? "s" : "",
3897 given,
3898 kwonly_sig,
3899 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003900 Py_DECREF(sig);
3901 Py_DECREF(kwonly_sig);
3902}
3903
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003904static int
Victor Stinner438a12d2019-05-24 17:01:38 +02003905positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
3906 Py_ssize_t kwcount, PyObject* const* kwnames)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003907{
3908 int posonly_conflicts = 0;
3909 PyObject* posonly_names = PyList_New(0);
3910
3911 for(int k=0; k < co->co_posonlyargcount; k++){
3912 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
3913
3914 for (int k2=0; k2<kwcount; k2++){
3915 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
3916 PyObject* kwname = kwnames[k2];
3917 if (kwname == posonly_name){
3918 if(PyList_Append(posonly_names, kwname) != 0) {
3919 goto fail;
3920 }
3921 posonly_conflicts++;
3922 continue;
3923 }
3924
3925 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
3926
3927 if ( cmp > 0) {
3928 if(PyList_Append(posonly_names, kwname) != 0) {
3929 goto fail;
3930 }
3931 posonly_conflicts++;
3932 } else if (cmp < 0) {
3933 goto fail;
3934 }
3935
3936 }
3937 }
3938 if (posonly_conflicts) {
3939 PyObject* comma = PyUnicode_FromString(", ");
3940 if (comma == NULL) {
3941 goto fail;
3942 }
3943 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
3944 Py_DECREF(comma);
3945 if (error_names == NULL) {
3946 goto fail;
3947 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003948 _PyErr_Format(tstate, PyExc_TypeError,
3949 "%U() got some positional-only arguments passed"
3950 " as keyword arguments: '%U'",
3951 co->co_name, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003952 Py_DECREF(error_names);
3953 goto fail;
3954 }
3955
3956 Py_DECREF(posonly_names);
3957 return 0;
3958
3959fail:
3960 Py_XDECREF(posonly_names);
3961 return 1;
3962
3963}
3964
Guido van Rossumc2e20742006-02-27 22:32:47 +00003965/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02003966 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003967 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003968
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003969PyObject *
Victor Stinnerb5e170f2019-11-16 01:03:22 +01003970_PyEval_EvalCode(PyThreadState *tstate,
3971 PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003972 PyObject *const *args, Py_ssize_t argcount,
3973 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003974 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003975 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003976 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003977 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003978{
Victor Stinnerb5e170f2019-11-16 01:03:22 +01003979 assert(tstate != NULL);
3980
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003981 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003982 PyFrameObject *f;
3983 PyObject *retval = NULL;
3984 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003985 PyObject *x, *u;
Pablo Galindocd74e662019-06-01 18:08:04 +01003986 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003987 Py_ssize_t i, j, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003988 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003990 if (globals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003991 _PyErr_SetString(tstate, PyExc_SystemError,
3992 "PyEval_EvalCodeEx: NULL globals");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003993 return NULL;
3994 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003995
Victor Stinnerc7020012016-08-16 23:40:29 +02003996 /* Create the frame */
INADA Naoki5a625d02016-12-24 20:19:08 +09003997 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003998 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003999 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02004000 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004001 fastlocals = f->f_localsplus;
4002 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00004003
Victor Stinnerc7020012016-08-16 23:40:29 +02004004 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004005 if (co->co_flags & CO_VARKEYWORDS) {
4006 kwdict = PyDict_New();
4007 if (kwdict == NULL)
4008 goto fail;
4009 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02004010 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004011 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02004012 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004013 SETLOCAL(i, kwdict);
4014 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004015 else {
4016 kwdict = NULL;
4017 }
4018
Pablo Galindocd74e662019-06-01 18:08:04 +01004019 /* Copy all positional arguments into local variables */
4020 if (argcount > co->co_argcount) {
4021 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02004022 }
4023 else {
4024 n = argcount;
4025 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004026 for (j = 0; j < n; j++) {
4027 x = args[j];
4028 Py_INCREF(x);
4029 SETLOCAL(j, x);
4030 }
4031
Victor Stinnerc7020012016-08-16 23:40:29 +02004032 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004033 if (co->co_flags & CO_VARARGS) {
Sergey Fedoseev234531b2019-02-25 21:59:12 +05004034 u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02004035 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004036 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004037 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004038 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004039 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004040
Serhiy Storchakab7281052016-09-12 00:52:40 +03004041 /* Handle keyword arguments passed as two strided arrays */
4042 kwcount *= kwstep;
4043 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004044 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03004045 PyObject *keyword = kwnames[i];
4046 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02004047 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004048
Benjamin Petersonb204a422011-06-05 22:04:07 -05004049 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004050 _PyErr_Format(tstate, PyExc_TypeError,
4051 "%U() keywords must be strings",
4052 co->co_name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004053 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004054 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004055
Benjamin Petersonb204a422011-06-05 22:04:07 -05004056 /* Speed hack: do raw pointer compares. As names are
4057 normally interned this should almost always hit. */
4058 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004059 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004060 PyObject *name = co_varnames[j];
4061 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004062 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004063 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004064 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004065
Benjamin Petersonb204a422011-06-05 22:04:07 -05004066 /* Slow fallback, just in case */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004067 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004068 PyObject *name = co_varnames[j];
4069 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
4070 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004071 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004072 }
4073 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004074 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004075 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004076 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004077
Victor Stinner231d1f32017-01-11 02:12:06 +01004078 assert(j >= total_args);
4079 if (kwdict == NULL) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004080
Victor Stinner438a12d2019-05-24 17:01:38 +02004081 if (co->co_posonlyargcount
4082 && positional_only_passed_as_keyword(tstate, co,
4083 kwcount, kwnames))
4084 {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004085 goto fail;
4086 }
4087
Victor Stinner438a12d2019-05-24 17:01:38 +02004088 _PyErr_Format(tstate, PyExc_TypeError,
4089 "%U() got an unexpected keyword argument '%S'",
4090 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004091 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004092 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004093
Christian Heimes0bd447f2013-07-20 14:48:10 +02004094 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4095 goto fail;
4096 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004097 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004098
Benjamin Petersonb204a422011-06-05 22:04:07 -05004099 kw_found:
4100 if (GETLOCAL(j) != NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004101 _PyErr_Format(tstate, PyExc_TypeError,
4102 "%U() got multiple values for argument '%S'",
4103 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004104 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004105 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004106 Py_INCREF(value);
4107 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004108 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004109
4110 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004111 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004112 too_many_positional(tstate, co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004113 goto fail;
4114 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004115
4116 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004117 if (argcount < co->co_argcount) {
4118 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004119 Py_ssize_t missing = 0;
4120 for (i = argcount; i < m; i++) {
4121 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004122 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004123 }
4124 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004125 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004126 missing_arguments(tstate, co, missing, defcount, fastlocals);
Benjamin Petersone109c702011-06-24 09:37:26 -05004127 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004128 }
4129 if (n > m)
4130 i = n - m;
4131 else
4132 i = 0;
4133 for (; i < defcount; i++) {
4134 if (GETLOCAL(m+i) == NULL) {
4135 PyObject *def = defs[i];
4136 Py_INCREF(def);
4137 SETLOCAL(m+i, def);
4138 }
4139 }
4140 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004141
4142 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004143 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004144 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004145 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004146 PyObject *name;
4147 if (GETLOCAL(i) != NULL)
4148 continue;
4149 name = PyTuple_GET_ITEM(co->co_varnames, i);
4150 if (kwdefs != NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004151 PyObject *def = PyDict_GetItemWithError(kwdefs, name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004152 if (def) {
4153 Py_INCREF(def);
4154 SETLOCAL(i, def);
4155 continue;
4156 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004157 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004158 goto fail;
4159 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004160 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004161 missing++;
4162 }
4163 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004164 missing_arguments(tstate, co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004165 goto fail;
4166 }
4167 }
4168
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004169 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004170 vars into frame. */
4171 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004172 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004173 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004174 /* Possibly account for the cell variable being an argument. */
4175 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004176 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004177 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004178 /* Clear the local copy. */
4179 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004180 }
4181 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004182 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004183 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004184 if (c == NULL)
4185 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004186 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004187 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004188
4189 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004190 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4191 PyObject *o = PyTuple_GET_ITEM(closure, i);
4192 Py_INCREF(o);
4193 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004194 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004195
Yury Selivanoveb636452016-09-08 22:01:51 -07004196 /* Handle generator/coroutine/asynchronous generator */
4197 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004198 PyObject *gen;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004199 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004201 /* Don't need to keep the reference to f_back, it will be set
4202 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004203 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004204
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004205 /* Create a new generator that owns the ready to run frame
4206 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004207 if (is_coro) {
4208 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004209 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4210 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004211 } else {
4212 gen = PyGen_NewWithQualName(f, name, qualname);
4213 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004214 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004215 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004216 }
INADA Naoki9c157762016-12-26 18:52:46 +09004217
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004218 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004219
Yury Selivanov75445082015-05-11 22:57:16 -04004220 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004221 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004222
Victor Stinnerb9e68122019-11-14 12:20:46 +01004223 retval = _PyEval_EvalFrame(tstate, f, 0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004224
Thomas Woutersce272b62007-09-19 21:19:28 +00004225fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004227 /* decref'ing the frame can cause __del__ methods to get invoked,
4228 which can call back into Python. While we're done with the
4229 current Python frame (f), the associated C stack is still in use,
4230 so recursion_depth must be boosted for the duration.
4231 */
INADA Naoki5a625d02016-12-24 20:19:08 +09004232 if (Py_REFCNT(f) > 1) {
4233 Py_DECREF(f);
4234 _PyObject_GC_TRACK(f);
4235 }
4236 else {
4237 ++tstate->recursion_depth;
4238 Py_DECREF(f);
4239 --tstate->recursion_depth;
4240 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004241 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004242}
4243
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004244
4245PyObject *
4246_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
4247 PyObject *const *args, Py_ssize_t argcount,
4248 PyObject *const *kwnames, PyObject *const *kwargs,
4249 Py_ssize_t kwcount, int kwstep,
4250 PyObject *const *defs, Py_ssize_t defcount,
4251 PyObject *kwdefs, PyObject *closure,
4252 PyObject *name, PyObject *qualname)
4253{
4254 PyThreadState *tstate = _PyThreadState_GET();
4255 return _PyEval_EvalCode(tstate, _co, globals, locals,
4256 args, argcount,
4257 kwnames, kwargs,
4258 kwcount, kwstep,
4259 defs, defcount,
4260 kwdefs, closure,
4261 name, qualname);
4262}
4263
Victor Stinner40ee3012014-06-16 15:59:28 +02004264PyObject *
4265PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004266 PyObject *const *args, int argcount,
4267 PyObject *const *kws, int kwcount,
4268 PyObject *const *defs, int defcount,
4269 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004270{
4271 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004272 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004273 kws, kws != NULL ? kws + 1 : NULL,
4274 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004275 defs, defcount,
4276 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004277 NULL, NULL);
4278}
Tim Peters5ca576e2001-06-18 22:08:13 +00004279
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004280static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004281special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004282{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004283 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004284 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02004285 if (res == NULL && !_PyErr_Occurred(tstate)) {
4286 _PyErr_SetObject(tstate, PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004287 return NULL;
4288 }
4289 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004290}
4291
4292
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004293/* Logic for the raise statement (too complicated for inlining).
4294 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004295static int
Victor Stinner09532fe2019-05-10 23:39:09 +02004296do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004297{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004298 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004300 if (exc == NULL) {
4301 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01004302 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004303 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004304 type = exc_info->exc_type;
4305 value = exc_info->exc_value;
4306 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004307 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004308 _PyErr_SetString(tstate, PyExc_RuntimeError,
4309 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004310 return 0;
4311 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004312 Py_XINCREF(type);
4313 Py_XINCREF(value);
4314 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02004315 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004316 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004317 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004319 /* We support the following forms of raise:
4320 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004321 raise <instance>
4322 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004324 if (PyExceptionClass_Check(exc)) {
4325 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004326 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004327 if (value == NULL)
4328 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004329 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004330 _PyErr_Format(tstate, PyExc_TypeError,
4331 "calling %R should have returned an instance of "
4332 "BaseException, not %R",
4333 type, Py_TYPE(value));
4334 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004335 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004336 }
4337 else if (PyExceptionInstance_Check(exc)) {
4338 value = exc;
4339 type = PyExceptionInstance_Class(exc);
4340 Py_INCREF(type);
4341 }
4342 else {
4343 /* Not something you can raise. You get an exception
4344 anyway, just not what you specified :-) */
4345 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02004346 _PyErr_SetString(tstate, PyExc_TypeError,
4347 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004348 goto raise_error;
4349 }
Collin Winter828f04a2007-08-31 00:04:24 +00004350
Serhiy Storchakac0191582016-09-27 11:37:10 +03004351 assert(type != NULL);
4352 assert(value != NULL);
4353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004354 if (cause) {
4355 PyObject *fixed_cause;
4356 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004357 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004358 if (fixed_cause == NULL)
4359 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004360 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004361 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004362 else if (PyExceptionInstance_Check(cause)) {
4363 fixed_cause = cause;
4364 }
4365 else if (cause == Py_None) {
4366 Py_DECREF(cause);
4367 fixed_cause = NULL;
4368 }
4369 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004370 _PyErr_SetString(tstate, PyExc_TypeError,
4371 "exception causes must derive from "
4372 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004373 goto raise_error;
4374 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004375 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004376 }
Collin Winter828f04a2007-08-31 00:04:24 +00004377
Victor Stinner438a12d2019-05-24 17:01:38 +02004378 _PyErr_SetObject(tstate, type, value);
Victor Stinner61f4db82020-01-28 03:37:45 +01004379 /* _PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004380 Py_DECREF(value);
4381 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004382 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004383
4384raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004385 Py_XDECREF(value);
4386 Py_XDECREF(type);
4387 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004388 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004389}
4390
Tim Petersd6d010b2001-06-21 02:49:55 +00004391/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004392 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004393
Guido van Rossum0368b722007-05-11 16:50:42 +00004394 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4395 with a variable target.
4396*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004397
Barry Warsawe42b18f1997-08-25 22:13:04 +00004398static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004399unpack_iterable(PyThreadState *tstate, PyObject *v,
4400 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004401{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004402 int i = 0, j = 0;
4403 Py_ssize_t ll = 0;
4404 PyObject *it; /* iter(v) */
4405 PyObject *w;
4406 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004408 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004410 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004411 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004412 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01004413 Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v))
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004414 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004415 _PyErr_Format(tstate, PyExc_TypeError,
4416 "cannot unpack non-iterable %.200s object",
Victor Stinnera102ed72020-02-07 02:24:48 +01004417 Py_TYPE(v)->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004418 }
4419 return 0;
4420 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004422 for (; i < argcnt; i++) {
4423 w = PyIter_Next(it);
4424 if (w == NULL) {
4425 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004426 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04004427 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004428 _PyErr_Format(tstate, PyExc_ValueError,
4429 "not enough values to unpack "
4430 "(expected %d, got %d)",
4431 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004432 }
4433 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004434 _PyErr_Format(tstate, PyExc_ValueError,
4435 "not enough values to unpack "
4436 "(expected at least %d, got %d)",
4437 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004438 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004439 }
4440 goto Error;
4441 }
4442 *--sp = w;
4443 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004445 if (argcntafter == -1) {
4446 /* We better have exhausted the iterator now. */
4447 w = PyIter_Next(it);
4448 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004449 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004450 goto Error;
4451 Py_DECREF(it);
4452 return 1;
4453 }
4454 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02004455 _PyErr_Format(tstate, PyExc_ValueError,
4456 "too many values to unpack (expected %d)",
4457 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004458 goto Error;
4459 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004461 l = PySequence_List(it);
4462 if (l == NULL)
4463 goto Error;
4464 *--sp = l;
4465 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004466
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004467 ll = PyList_GET_SIZE(l);
4468 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004469 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04004470 "not enough values to unpack (expected at least %d, got %zd)",
4471 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004472 goto Error;
4473 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004475 /* Pop the "after-variable" args off the list. */
4476 for (j = argcntafter; j > 0; j--, i++) {
4477 *--sp = PyList_GET_ITEM(l, ll - j);
4478 }
4479 /* Resize the list. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +01004480 Py_SET_SIZE(l, ll - argcntafter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004481 Py_DECREF(it);
4482 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004483
Tim Petersd6d010b2001-06-21 02:49:55 +00004484Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004485 for (; i > 0; i--, sp++)
4486 Py_DECREF(*sp);
4487 Py_XDECREF(it);
4488 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004489}
4490
4491
Guido van Rossum96a42c81992-01-12 02:29:51 +00004492#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004493static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004494prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004495{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004496 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02004497 if (PyObject_Print(v, stdout, 0) != 0) {
4498 /* Don't know what else to do */
4499 _PyErr_Clear(tstate);
4500 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004501 printf("\n");
4502 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004503}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004504#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004505
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004506static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004507call_exc_trace(Py_tracefunc func, PyObject *self,
4508 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004509{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004510 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004511 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004512 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004513 if (value == NULL) {
4514 value = Py_None;
4515 Py_INCREF(value);
4516 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004517 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01004518 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004519 arg = PyTuple_Pack(3, type, value, traceback);
4520 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004521 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004522 return;
4523 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004524 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004525 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02004526 if (err == 0) {
4527 _PyErr_Restore(tstate, type, value, orig_traceback);
4528 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004529 else {
4530 Py_XDECREF(type);
4531 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004532 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004533 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004534}
4535
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004536static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004537call_trace_protected(Py_tracefunc func, PyObject *obj,
4538 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004539 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004540{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004541 PyObject *type, *value, *traceback;
4542 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004543 _PyErr_Fetch(tstate, &type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004544 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004545 if (err == 0)
4546 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004547 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004548 return 0;
4549 }
4550 else {
4551 Py_XDECREF(type);
4552 Py_XDECREF(value);
4553 Py_XDECREF(traceback);
4554 return -1;
4555 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004556}
4557
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004558static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004559call_trace(Py_tracefunc func, PyObject *obj,
4560 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004561 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004562{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004563 int result;
4564 if (tstate->tracing)
4565 return 0;
4566 tstate->tracing++;
4567 tstate->use_tracing = 0;
4568 result = func(obj, frame, what, arg);
4569 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4570 || (tstate->c_profilefunc != NULL));
4571 tstate->tracing--;
4572 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004573}
4574
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004575PyObject *
4576_PyEval_CallTracing(PyObject *func, PyObject *args)
4577{
Victor Stinner50b48572018-11-01 01:51:40 +01004578 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004579 int save_tracing = tstate->tracing;
4580 int save_use_tracing = tstate->use_tracing;
4581 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004582
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004583 tstate->tracing = 0;
4584 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4585 || (tstate->c_profilefunc != NULL));
4586 result = PyObject_Call(func, args, NULL);
4587 tstate->tracing = save_tracing;
4588 tstate->use_tracing = save_use_tracing;
4589 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004590}
4591
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004592/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004593static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004594maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004595 PyThreadState *tstate, PyFrameObject *frame,
4596 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004597{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004598 int result = 0;
4599 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004601 /* If the last instruction executed isn't in the current
4602 instruction window, reset the window.
4603 */
4604 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4605 PyAddrPair bounds;
4606 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4607 &bounds);
4608 *instr_lb = bounds.ap_lower;
4609 *instr_ub = bounds.ap_upper;
4610 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004611 /* If the last instruction falls at the start of a line or if it
4612 represents a jump backwards, update the frame's line number and
4613 then call the trace function if we're tracing source lines.
4614 */
4615 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004616 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004617 if (frame->f_trace_lines) {
4618 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4619 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004620 }
George King20faa682017-10-18 17:44:22 -07004621 /* Always emit an opcode event if we're tracing all opcodes. */
4622 if (frame->f_trace_opcodes) {
4623 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4624 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004625 *instr_prev = frame->f_lasti;
4626 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004627}
4628
Fred Drake5755ce62001-06-27 19:19:46 +00004629void
4630PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004631{
Steve Dowerb82e17e2019-05-23 08:45:22 -07004632 if (PySys_Audit("sys.setprofile", NULL) < 0) {
4633 return;
4634 }
4635
Victor Stinner50b48572018-11-01 01:51:40 +01004636 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004637 PyObject *temp = tstate->c_profileobj;
4638 Py_XINCREF(arg);
4639 tstate->c_profilefunc = NULL;
4640 tstate->c_profileobj = NULL;
4641 /* Must make sure that tracing is not ignored if 'temp' is freed */
4642 tstate->use_tracing = tstate->c_tracefunc != NULL;
4643 Py_XDECREF(temp);
4644 tstate->c_profilefunc = func;
4645 tstate->c_profileobj = arg;
4646 /* Flag that tracing or profiling is turned on */
4647 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004648}
4649
4650void
4651PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4652{
Steve Dowerb82e17e2019-05-23 08:45:22 -07004653 if (PySys_Audit("sys.settrace", NULL) < 0) {
4654 return;
4655 }
4656
Victor Stinner09532fe2019-05-10 23:39:09 +02004657 _PyRuntimeState *runtime = &_PyRuntime;
4658 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004659 PyObject *temp = tstate->c_traceobj;
Victor Stinner09532fe2019-05-10 23:39:09 +02004660 runtime->ceval.tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004661 Py_XINCREF(arg);
4662 tstate->c_tracefunc = NULL;
4663 tstate->c_traceobj = NULL;
4664 /* Must make sure that profiling is not ignored if 'temp' is freed */
4665 tstate->use_tracing = tstate->c_profilefunc != NULL;
4666 Py_XDECREF(temp);
4667 tstate->c_tracefunc = func;
4668 tstate->c_traceobj = arg;
4669 /* Flag that tracing or profiling is turned on */
4670 tstate->use_tracing = ((func != NULL)
4671 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004672}
4673
Yury Selivanov75445082015-05-11 22:57:16 -04004674void
Victor Stinner838f2642019-06-13 22:41:23 +02004675_PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004676{
4677 assert(new_depth >= 0);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004678 tstate->coroutine_origin_tracking_depth = new_depth;
4679}
4680
4681int
4682_PyEval_GetCoroutineOriginTrackingDepth(void)
4683{
Victor Stinner50b48572018-11-01 01:51:40 +01004684 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004685 return tstate->coroutine_origin_tracking_depth;
4686}
4687
4688void
Yury Selivanoveb636452016-09-08 22:01:51 -07004689_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4690{
Victor Stinner50b48572018-11-01 01:51:40 +01004691 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004692
4693 if (PySys_Audit("sys.set_asyncgen_hook_firstiter", NULL) < 0) {
4694 return;
4695 }
4696
Yury Selivanoveb636452016-09-08 22:01:51 -07004697 Py_XINCREF(firstiter);
4698 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4699}
4700
4701PyObject *
4702_PyEval_GetAsyncGenFirstiter(void)
4703{
Victor Stinner50b48572018-11-01 01:51:40 +01004704 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004705 return tstate->async_gen_firstiter;
4706}
4707
4708void
4709_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4710{
Victor Stinner50b48572018-11-01 01:51:40 +01004711 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004712
4713 if (PySys_Audit("sys.set_asyncgen_hook_finalizer", NULL) < 0) {
4714 return;
4715 }
4716
Yury Selivanoveb636452016-09-08 22:01:51 -07004717 Py_XINCREF(finalizer);
4718 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4719}
4720
4721PyObject *
4722_PyEval_GetAsyncGenFinalizer(void)
4723{
Victor Stinner50b48572018-11-01 01:51:40 +01004724 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004725 return tstate->async_gen_finalizer;
4726}
4727
Victor Stinner438a12d2019-05-24 17:01:38 +02004728static PyFrameObject *
4729_PyEval_GetFrame(PyThreadState *tstate)
4730{
Victor Stinner01b1cc12019-11-20 02:27:56 +01004731 _PyRuntimeState *runtime = tstate->interp->runtime;
4732 return runtime->gilstate.getframe(tstate);
Victor Stinner438a12d2019-05-24 17:01:38 +02004733}
4734
4735PyFrameObject *
4736PyEval_GetFrame(void)
4737{
4738 PyThreadState *tstate = _PyThreadState_GET();
4739 return _PyEval_GetFrame(tstate);
4740}
4741
Guido van Rossumb209a111997-04-29 18:18:01 +00004742PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004743PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004744{
Victor Stinner438a12d2019-05-24 17:01:38 +02004745 PyThreadState *tstate = _PyThreadState_GET();
4746 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004747 if (current_frame == NULL)
Victor Stinner438a12d2019-05-24 17:01:38 +02004748 return tstate->interp->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004749 else
4750 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004751}
4752
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004753/* Convenience function to get a builtin from its name */
4754PyObject *
4755_PyEval_GetBuiltinId(_Py_Identifier *name)
4756{
Victor Stinner438a12d2019-05-24 17:01:38 +02004757 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004758 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4759 if (attr) {
4760 Py_INCREF(attr);
4761 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004762 else if (!_PyErr_Occurred(tstate)) {
4763 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004764 }
4765 return attr;
4766}
4767
Guido van Rossumb209a111997-04-29 18:18:01 +00004768PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004769PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004770{
Victor Stinner438a12d2019-05-24 17:01:38 +02004771 PyThreadState *tstate = _PyThreadState_GET();
4772 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Victor Stinner41bb43a2013-10-29 01:19:37 +01004773 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004774 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004775 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004776 }
4777
Victor Stinner438a12d2019-05-24 17:01:38 +02004778 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01004779 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004780 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004781
4782 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004783 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004784}
4785
Guido van Rossumb209a111997-04-29 18:18:01 +00004786PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004787PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004788{
Victor Stinner438a12d2019-05-24 17:01:38 +02004789 PyThreadState *tstate = _PyThreadState_GET();
4790 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
4791 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004792 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004793 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004794
4795 assert(current_frame->f_globals != NULL);
4796 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004797}
4798
Guido van Rossum6135a871995-01-09 17:53:26 +00004799int
Tim Peters5ba58662001-07-16 02:29:45 +00004800PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004801{
Victor Stinner438a12d2019-05-24 17:01:38 +02004802 PyThreadState *tstate = _PyThreadState_GET();
4803 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004804 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004805
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004806 if (current_frame != NULL) {
4807 const int codeflags = current_frame->f_code->co_flags;
4808 const int compilerflags = codeflags & PyCF_MASK;
4809 if (compilerflags) {
4810 result = 1;
4811 cf->cf_flags |= compilerflags;
4812 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004813#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004814 if (codeflags & CO_GENERATOR_ALLOWED) {
4815 result = 1;
4816 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4817 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004818#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004819 }
4820 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004821}
4822
Guido van Rossum3f5da241990-12-20 15:06:42 +00004823
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004824const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004825PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004826{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004827 if (PyMethod_Check(func))
4828 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4829 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004830 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004831 else if (PyCFunction_Check(func))
4832 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4833 else
Victor Stinnera102ed72020-02-07 02:24:48 +01004834 return Py_TYPE(func)->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004835}
4836
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004837const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004838PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004839{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004840 if (PyMethod_Check(func))
4841 return "()";
4842 else if (PyFunction_Check(func))
4843 return "()";
4844 else if (PyCFunction_Check(func))
4845 return "()";
4846 else
4847 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004848}
4849
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004850#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004851if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004852 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4853 tstate, tstate->frame, \
4854 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004855 x = NULL; \
4856 } \
4857 else { \
4858 x = call; \
4859 if (tstate->c_profilefunc != NULL) { \
4860 if (x == NULL) { \
4861 call_trace_protected(tstate->c_profilefunc, \
4862 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004863 tstate, tstate->frame, \
4864 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004865 /* XXX should pass (type, value, tb) */ \
4866 } else { \
4867 if (call_trace(tstate->c_profilefunc, \
4868 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004869 tstate, tstate->frame, \
4870 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004871 Py_DECREF(x); \
4872 x = NULL; \
4873 } \
4874 } \
4875 } \
4876 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004877} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004878 x = call; \
4879 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004880
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004881
4882static PyObject *
4883trace_call_function(PyThreadState *tstate,
4884 PyObject *func,
4885 PyObject **args, Py_ssize_t nargs,
4886 PyObject *kwnames)
4887{
4888 PyObject *x;
4889 if (PyCFunction_Check(func)) {
Petr Viktorinffd97532020-02-11 17:46:57 +01004890 C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004891 return x;
4892 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06004893 else if (Py_IS_TYPE(func, &PyMethodDescr_Type) && nargs > 0) {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004894 /* We need to create a temporary bound method as argument
4895 for profiling.
4896
4897 If nargs == 0, then this cannot work because we have no
4898 "self". In any case, the call itself would raise
4899 TypeError (foo needs an argument), so we just skip
4900 profiling. */
4901 PyObject *self = args[0];
4902 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4903 if (func == NULL) {
4904 return NULL;
4905 }
Petr Viktorinffd97532020-02-11 17:46:57 +01004906 C_TRACE(x, PyObject_Vectorcall(func,
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02004907 args+1, nargs-1,
4908 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004909 Py_DECREF(func);
4910 return x;
4911 }
Petr Viktorinffd97532020-02-11 17:46:57 +01004912 return PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004913}
4914
Victor Stinner415c5102017-01-11 00:54:57 +01004915/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4916 to reduce the stack consumption. */
4917Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Victor Stinner09532fe2019-05-10 23:39:09 +02004918call_function(PyThreadState *tstate, PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004919{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004920 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004921 PyObject *func = *pfunc;
4922 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004923 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4924 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004925 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004926
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004927 if (tstate->use_tracing) {
4928 x = trace_call_function(tstate, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09004929 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004930 else {
Petr Viktorinffd97532020-02-11 17:46:57 +01004931 x = PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004932 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004933
Victor Stinner438a12d2019-05-24 17:01:38 +02004934 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004935
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004936 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004937 while ((*pp_stack) > pfunc) {
4938 w = EXT_POP(*pp_stack);
4939 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004940 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004941
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004942 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004943}
4944
Jeremy Hylton52820442001-01-03 23:52:36 +00004945static PyObject *
Victor Stinner09532fe2019-05-10 23:39:09 +02004946do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004947{
jdemeyere89de732018-09-19 12:06:20 +02004948 PyObject *result;
4949
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004950 if (PyCFunction_Check(func)) {
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +02004951 C_TRACE(result, PyObject_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004952 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004953 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06004954 else if (Py_IS_TYPE(func, &PyMethodDescr_Type)) {
jdemeyere89de732018-09-19 12:06:20 +02004955 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
4956 if (nargs > 0 && tstate->use_tracing) {
4957 /* We need to create a temporary bound method as argument
4958 for profiling.
4959
4960 If nargs == 0, then this cannot work because we have no
4961 "self". In any case, the call itself would raise
4962 TypeError (foo needs an argument), so we just skip
4963 profiling. */
4964 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
4965 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4966 if (func == NULL) {
4967 return NULL;
4968 }
4969
Victor Stinner4d231bc2019-11-14 13:36:21 +01004970 C_TRACE(result, _PyObject_FastCallDictTstate(
4971 tstate, func,
4972 &_PyTuple_ITEMS(callargs)[1],
4973 nargs - 1,
4974 kwdict));
jdemeyere89de732018-09-19 12:06:20 +02004975 Py_DECREF(func);
4976 return result;
4977 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004978 }
jdemeyere89de732018-09-19 12:06:20 +02004979 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00004980}
4981
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004982/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004983 nb_index slot defined, and store in *pi.
4984 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004985 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004986 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004987*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004988int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004989_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004990{
Victor Stinner438a12d2019-05-24 17:01:38 +02004991 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004992 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004993 Py_ssize_t x;
4994 if (PyIndex_Check(v)) {
4995 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02004996 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004997 return 0;
4998 }
4999 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005000 _PyErr_SetString(tstate, PyExc_TypeError,
5001 "slice indices must be integers or "
5002 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005003 return 0;
5004 }
5005 *pi = x;
5006 }
5007 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005008}
5009
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005010int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005011_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005012{
Victor Stinner438a12d2019-05-24 17:01:38 +02005013 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005014 Py_ssize_t x;
5015 if (PyIndex_Check(v)) {
5016 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005017 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005018 return 0;
5019 }
5020 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005021 _PyErr_SetString(tstate, PyExc_TypeError,
5022 "slice indices must be integers or "
5023 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005024 return 0;
5025 }
5026 *pi = x;
5027 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005028}
5029
Thomas Wouters52152252000-08-17 22:55:00 +00005030static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005031import_name(PyThreadState *tstate, PyFrameObject *f,
5032 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005033{
5034 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005035 PyObject *import_func, *res;
5036 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005037
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005038 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005039 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005040 if (!_PyErr_Occurred(tstate)) {
5041 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005042 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005043 return NULL;
5044 }
5045
5046 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005047 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005048 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02005049 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005050 return NULL;
5051 }
5052 res = PyImport_ImportModuleLevelObject(
5053 name,
5054 f->f_globals,
5055 f->f_locals == NULL ? Py_None : f->f_locals,
5056 fromlist,
5057 ilevel);
5058 return res;
5059 }
5060
5061 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005062
5063 stack[0] = name;
5064 stack[1] = f->f_globals;
5065 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5066 stack[3] = fromlist;
5067 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005068 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005069 Py_DECREF(import_func);
5070 return res;
5071}
5072
5073static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005074import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005075{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005076 PyObject *x;
Xiang Zhang4830f582017-03-21 11:13:42 +08005077 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005078
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005079 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005080 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005081 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005082 /* Issue #17636: in case this failed because of a circular relative
5083 import, try to fallback on reading the module directly from
5084 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02005085 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005086 if (pkgname == NULL) {
5087 goto error;
5088 }
Oren Milman6db70332017-09-19 14:23:01 +03005089 if (!PyUnicode_Check(pkgname)) {
5090 Py_CLEAR(pkgname);
5091 goto error;
5092 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005093 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005094 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005095 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005096 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005097 }
Eric Snow3f9eee62017-09-15 16:35:20 -06005098 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005099 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02005100 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07005101 goto error;
5102 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005103 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005104 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005105 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005106 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005107 if (pkgname == NULL) {
5108 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5109 if (pkgname_or_unknown == NULL) {
5110 Py_XDECREF(pkgpath);
5111 return NULL;
5112 }
5113 } else {
5114 pkgname_or_unknown = pkgname;
5115 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005116
5117 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005118 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08005119 errmsg = PyUnicode_FromFormat(
5120 "cannot import name %R from %R (unknown location)",
5121 name, pkgname_or_unknown
5122 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005123 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005124 PyErr_SetImportError(errmsg, pkgname, NULL);
5125 }
5126 else {
Anthony Sottile65366bc2019-09-09 08:17:50 -07005127 _Py_IDENTIFIER(__spec__);
5128 PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
Anthony Sottile65366bc2019-09-09 08:17:50 -07005129 const char *fmt =
5130 _PyModuleSpec_IsInitializing(spec) ?
5131 "cannot import name %R from partially initialized module %R "
5132 "(most likely due to a circular import) (%S)" :
5133 "cannot import name %R from %R (%S)";
5134 Py_XDECREF(spec);
5135
5136 errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
Stefan Krah027b09c2019-03-25 21:50:58 +01005137 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005138 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005139 }
5140
Xiang Zhang4830f582017-03-21 11:13:42 +08005141 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005142 Py_XDECREF(pkgname_or_unknown);
5143 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005144 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005145}
Guido van Rossumac7be682001-01-17 15:42:30 +00005146
Thomas Wouters52152252000-08-17 22:55:00 +00005147static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005148import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00005149{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005150 _Py_IDENTIFIER(__all__);
5151 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005152 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005153 int skip_leading_underscores = 0;
5154 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005155
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005156 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5157 return -1; /* Unexpected error */
5158 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005159 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005160 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5161 return -1;
5162 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005163 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005164 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005165 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005166 return -1;
5167 }
5168 all = PyMapping_Keys(dict);
5169 Py_DECREF(dict);
5170 if (all == NULL)
5171 return -1;
5172 skip_leading_underscores = 1;
5173 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005175 for (pos = 0, err = 0; ; pos++) {
5176 name = PySequence_GetItem(all, pos);
5177 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005178 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005179 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02005180 }
5181 else {
5182 _PyErr_Clear(tstate);
5183 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005184 break;
5185 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005186 if (!PyUnicode_Check(name)) {
5187 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5188 if (modname == NULL) {
5189 Py_DECREF(name);
5190 err = -1;
5191 break;
5192 }
5193 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005194 _PyErr_Format(tstate, PyExc_TypeError,
5195 "module __name__ must be a string, not %.100s",
5196 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005197 }
5198 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005199 _PyErr_Format(tstate, PyExc_TypeError,
5200 "%s in %U.%s must be str, not %.100s",
5201 skip_leading_underscores ? "Key" : "Item",
5202 modname,
5203 skip_leading_underscores ? "__dict__" : "__all__",
5204 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005205 }
5206 Py_DECREF(modname);
5207 Py_DECREF(name);
5208 err = -1;
5209 break;
5210 }
5211 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005212 if (PyUnicode_READY(name) == -1) {
5213 Py_DECREF(name);
5214 err = -1;
5215 break;
5216 }
5217 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5218 Py_DECREF(name);
5219 continue;
5220 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005221 }
5222 value = PyObject_GetAttr(v, name);
5223 if (value == NULL)
5224 err = -1;
5225 else if (PyDict_CheckExact(locals))
5226 err = PyDict_SetItem(locals, name, value);
5227 else
5228 err = PyObject_SetItem(locals, name, value);
5229 Py_DECREF(name);
5230 Py_XDECREF(value);
5231 if (err != 0)
5232 break;
5233 }
5234 Py_DECREF(all);
5235 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005236}
5237
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005238static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005239check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005240{
Victor Stinnera102ed72020-02-07 02:24:48 +01005241 if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005242 /* check_args_iterable() may be called with a live exception:
5243 * clear it to prevent calling _PyObject_FunctionStr() with an
5244 * exception set. */
Victor Stinner61f4db82020-01-28 03:37:45 +01005245 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005246 PyObject *funcstr = _PyObject_FunctionStr(func);
5247 if (funcstr != NULL) {
5248 _PyErr_Format(tstate, PyExc_TypeError,
5249 "%U argument after * must be an iterable, not %.200s",
5250 funcstr, Py_TYPE(args)->tp_name);
5251 Py_DECREF(funcstr);
5252 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005253 return -1;
5254 }
5255 return 0;
5256}
5257
5258static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005259format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005260{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005261 /* _PyDict_MergeEx raises attribute
5262 * error (percolated from an attempt
5263 * to get 'keys' attribute) instead of
5264 * a type error if its second argument
5265 * is not a mapping.
5266 */
Victor Stinner438a12d2019-05-24 17:01:38 +02005267 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005268 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005269 PyObject *funcstr = _PyObject_FunctionStr(func);
5270 if (funcstr != NULL) {
5271 _PyErr_Format(
5272 tstate, PyExc_TypeError,
5273 "%U argument after ** must be a mapping, not %.200s",
5274 funcstr, Py_TYPE(kwargs)->tp_name);
5275 Py_DECREF(funcstr);
5276 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005277 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005278 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005279 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02005280 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005281 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005282 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005283 PyObject *funcstr = _PyObject_FunctionStr(func);
5284 if (funcstr != NULL) {
5285 PyObject *key = PyTuple_GET_ITEM(val, 0);
5286 _PyErr_Format(
5287 tstate, PyExc_TypeError,
5288 "%U got multiple values for keyword argument '%S'",
5289 funcstr, key);
5290 Py_DECREF(funcstr);
5291 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005292 Py_XDECREF(exc);
5293 Py_XDECREF(val);
5294 Py_XDECREF(tb);
5295 }
5296 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005297 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005298 }
5299 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005300}
5301
Guido van Rossumac7be682001-01-17 15:42:30 +00005302static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005303format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
5304 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005305{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005306 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005308 if (!obj)
5309 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005310
Serhiy Storchaka06515832016-11-20 09:13:07 +02005311 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005312 if (!obj_str)
5313 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005314
Victor Stinner438a12d2019-05-24 17:01:38 +02005315 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005316}
Guido van Rossum950361c1997-01-24 13:49:28 +00005317
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005318static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005319format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005320{
5321 PyObject *name;
5322 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02005323 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005324 return;
5325 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5326 name = PyTuple_GET_ITEM(co->co_cellvars,
5327 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005328 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005329 PyExc_UnboundLocalError,
5330 UNBOUNDLOCAL_ERROR_MSG,
5331 name);
5332 } else {
5333 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5334 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02005335 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005336 UNBOUNDFREE_ERROR_MSG, name);
5337 }
5338}
5339
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005340static void
Mark Shannonfee55262019-11-21 09:11:43 +00005341format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevprevopcode, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005342{
5343 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5344 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005345 _PyErr_Format(tstate, PyExc_TypeError,
5346 "'async with' received an object from __aenter__ "
5347 "that does not implement __await__: %.100s",
5348 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005349 }
Mark Shannonfee55262019-11-21 09:11:43 +00005350 else if (prevopcode == WITH_EXCEPT_START || (prevopcode == CALL_FUNCTION && prevprevopcode == DUP_TOP)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005351 _PyErr_Format(tstate, PyExc_TypeError,
5352 "'async with' received an object from __aexit__ "
5353 "that does not implement __await__: %.100s",
5354 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005355 }
5356 }
5357}
5358
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005359static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005360unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005361 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005362{
5363 PyObject *res;
5364 if (Py_REFCNT(v) == 2) {
5365 /* In the common case, there are 2 references to the value
5366 * stored in 'variable' when the += is performed: one on the
5367 * value stack (in 'v') and one still stored in the
5368 * 'variable'. We try to delete the variable now to reduce
5369 * the refcnt to 1.
5370 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005371 int opcode, oparg;
5372 NEXTOPARG();
5373 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005374 case STORE_FAST:
5375 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005376 PyObject **fastlocals = f->f_localsplus;
5377 if (GETLOCAL(oparg) == v)
5378 SETLOCAL(oparg, NULL);
5379 break;
5380 }
5381 case STORE_DEREF:
5382 {
5383 PyObject **freevars = (f->f_localsplus +
5384 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005385 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005386 if (PyCell_GET(c) == v) {
5387 PyCell_SET(c, NULL);
5388 Py_DECREF(v);
5389 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005390 break;
5391 }
5392 case STORE_NAME:
5393 {
5394 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005395 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005396 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005397 if (locals && PyDict_CheckExact(locals)) {
5398 PyObject *w = PyDict_GetItemWithError(locals, name);
5399 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02005400 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005401 {
5402 Py_DECREF(v);
5403 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005404 }
5405 }
5406 break;
5407 }
5408 }
5409 }
5410 res = v;
5411 PyUnicode_Append(&res, w);
5412 return res;
5413}
5414
Guido van Rossum950361c1997-01-24 13:49:28 +00005415#ifdef DYNAMIC_EXECUTION_PROFILE
5416
Skip Montanarof118cb12001-10-15 20:51:38 +00005417static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005418getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005419{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005420 int i;
5421 PyObject *l = PyList_New(256);
5422 if (l == NULL) return NULL;
5423 for (i = 0; i < 256; i++) {
5424 PyObject *x = PyLong_FromLong(a[i]);
5425 if (x == NULL) {
5426 Py_DECREF(l);
5427 return NULL;
5428 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005429 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005430 }
5431 for (i = 0; i < 256; i++)
5432 a[i] = 0;
5433 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005434}
5435
5436PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005437_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005438{
5439#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005440 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005441#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005442 int i;
5443 PyObject *l = PyList_New(257);
5444 if (l == NULL) return NULL;
5445 for (i = 0; i < 257; i++) {
5446 PyObject *x = getarray(dxpairs[i]);
5447 if (x == NULL) {
5448 Py_DECREF(l);
5449 return NULL;
5450 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005451 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005452 }
5453 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005454#endif
5455}
5456
5457#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005458
5459Py_ssize_t
5460_PyEval_RequestCodeExtraIndex(freefunc free)
5461{
Victor Stinnercaba55b2018-08-03 15:33:52 +02005462 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -07005463 Py_ssize_t new_index;
5464
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005465 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005466 return -1;
5467 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005468 new_index = interp->co_extra_user_count++;
5469 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005470 return new_index;
5471}
Łukasz Langaa785c872016-09-09 17:37:37 -07005472
5473static void
5474dtrace_function_entry(PyFrameObject *f)
5475{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005476 const char *filename;
5477 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005478 int lineno;
5479
5480 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5481 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5482 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5483
Andy Lestere6be9b52020-02-11 20:28:35 -06005484 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005485}
5486
5487static void
5488dtrace_function_return(PyFrameObject *f)
5489{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005490 const char *filename;
5491 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005492 int lineno;
5493
5494 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5495 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5496 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5497
Andy Lestere6be9b52020-02-11 20:28:35 -06005498 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005499}
5500
5501/* DTrace equivalent of maybe_call_line_trace. */
5502static void
5503maybe_dtrace_line(PyFrameObject *frame,
5504 int *instr_lb, int *instr_ub, int *instr_prev)
5505{
5506 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005507 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005508
5509 /* If the last instruction executed isn't in the current
5510 instruction window, reset the window.
5511 */
5512 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5513 PyAddrPair bounds;
5514 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5515 &bounds);
5516 *instr_lb = bounds.ap_lower;
5517 *instr_ub = bounds.ap_upper;
5518 }
5519 /* If the last instruction falls at the start of a line or if
5520 it represents a jump backwards, update the frame's line
5521 number and call the trace function. */
5522 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5523 frame->f_lineno = line;
5524 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5525 if (!co_filename)
5526 co_filename = "?";
5527 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5528 if (!co_name)
5529 co_name = "?";
Andy Lestere6be9b52020-02-11 20:28:35 -06005530 PyDTrace_LINE(co_filename, co_name, line);
Łukasz Langaa785c872016-09-09 17:37:37 -07005531 }
5532 *instr_prev = frame->f_lasti;
5533}
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005534
5535
5536/* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions
5537 for the limited API. */
5538
5539#undef Py_EnterRecursiveCall
5540
5541int Py_EnterRecursiveCall(const char *where)
5542{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005543 return _Py_EnterRecursiveCall_inline(where);
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005544}
5545
5546#undef Py_LeaveRecursiveCall
5547
5548void Py_LeaveRecursiveCall(void)
5549{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005550 _Py_LeaveRecursiveCall_inline();
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005551}