blob: 42f08c4534c64375a52714ce382c0bdd2a38f68d [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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 /* Check someone has called PyEval_InitThreads() 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{
Eric Snow5be45a62019-03-08 22:47:07 -0700544 /* Only handle signals on main thread. PyEval_InitThreads must
545 * have been called already.
546 */
Victor Stinner09532fe2019-05-10 23:39:09 +0200547 if (PyThread_get_thread_ident() != runtime->main_thread) {
Eric Snowfdf282d2019-01-11 14:26:55 -0700548 return 0;
549 }
Eric Snow64d6cc82019-02-23 15:40:43 -0700550 /*
551 * Ensure that the thread isn't currently running some other
552 * interpreter.
553 */
Victor Stinner09532fe2019-05-10 23:39:09 +0200554 PyInterpreterState *interp = _PyRuntimeState_GetThreadState(runtime)->interp;
555 if (interp != runtime->interpreters.main) {
Eric Snow64d6cc82019-02-23 15:40:43 -0700556 return 0;
557 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700558
Victor Stinnere225beb2019-06-03 18:14:24 +0200559 struct _ceval_runtime_state *ceval = &runtime->ceval;
560 UNSIGNAL_PENDING_SIGNALS(ceval);
Eric Snow64d6cc82019-02-23 15:40:43 -0700561 if (_PyErr_CheckSignals() < 0) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200562 SIGNAL_PENDING_SIGNALS(ceval); /* We're not done yet */
Eric Snowfdf282d2019-01-11 14:26:55 -0700563 return -1;
564 }
565 return 0;
566}
567
568static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200569make_pending_calls(_PyRuntimeState *runtime)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000570{
Eric Snow6a150bc2019-06-01 15:39:46 -0600571 static int busy = 0;
Eric Snowb75b1a352019-04-12 10:20:10 -0600572
Victor Stinnere225beb2019-06-03 18:14:24 +0200573 /* only service pending calls on main thread */
574 if (PyThread_get_thread_ident() != runtime->main_thread) {
575 return 0;
576 }
577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 /* don't perform recursive pending calls */
Eric Snowfdf282d2019-01-11 14:26:55 -0700579 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700581 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200582 busy = 1;
Victor Stinnere225beb2019-06-03 18:14:24 +0200583 struct _ceval_runtime_state *ceval = &runtime->ceval;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200584 /* unsignal before starting to call callbacks, so that any callback
585 added in-between re-signals */
Victor Stinnere225beb2019-06-03 18:14:24 +0200586 UNSIGNAL_PENDING_CALLS(ceval);
Eric Snowfdf282d2019-01-11 14:26:55 -0700587 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200588
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 /* perform a bounded number of calls, in case of recursion */
Victor Stinnere225beb2019-06-03 18:14:24 +0200590 struct _pending_calls *pending = &ceval->pending;
Eric Snowfdf282d2019-01-11 14:26:55 -0700591 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700592 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 void *arg = NULL;
594
595 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600596 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200597 _pop_pending_call(pending, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600598 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700599
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100600 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700601 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100602 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700603 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700604 res = func(arg);
605 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200606 goto error;
607 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000608 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200609
Charles-François Natalif23339a2011-07-23 18:15:43 +0200610 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700611 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200612
613error:
614 busy = 0;
Victor Stinnere225beb2019-06-03 18:14:24 +0200615 SIGNAL_PENDING_CALLS(ceval);
Eric Snowfdf282d2019-01-11 14:26:55 -0700616 return res;
617}
618
Eric Snow842a2f02019-03-15 15:47:51 -0600619void
Victor Stinner2b1df452020-01-13 18:46:59 +0100620_Py_FinishPendingCalls(PyThreadState *tstate)
Eric Snow842a2f02019-03-15 15:47:51 -0600621{
Eric Snow842a2f02019-03-15 15:47:51 -0600622 assert(PyGILState_Check());
623
Victor Stinner2b1df452020-01-13 18:46:59 +0100624 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200625 struct _pending_calls *pending = &runtime->ceval.pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200626
Eric Snow842a2f02019-03-15 15:47:51 -0600627 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
628 pending->finishing = 1;
629 PyThread_release_lock(pending->lock);
630
631 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
632 return;
633 }
634
Victor Stinnere225beb2019-06-03 18:14:24 +0200635 if (make_pending_calls(runtime) < 0) {
636 PyObject *exc, *val, *tb;
637 _PyErr_Fetch(tstate, &exc, &val, &tb);
638 PyErr_BadInternalCall();
639 _PyErr_ChainExceptions(exc, val, tb);
640 _PyErr_Print(tstate);
Eric Snow842a2f02019-03-15 15:47:51 -0600641 }
642}
643
Eric Snowfdf282d2019-01-11 14:26:55 -0700644/* Py_MakePendingCalls() is a simple wrapper for the sake
645 of backward-compatibility. */
646int
647Py_MakePendingCalls(void)
648{
649 assert(PyGILState_Check());
650
651 /* Python signal handler doesn't really queue a callback: it only signals
652 that a signal was received, see _PyEval_SignalReceived(). */
Victor Stinner09532fe2019-05-10 23:39:09 +0200653 _PyRuntimeState *runtime = &_PyRuntime;
654 int res = handle_signals(runtime);
Eric Snowfdf282d2019-01-11 14:26:55 -0700655 if (res != 0) {
656 return res;
657 }
658
Victor Stinnere225beb2019-06-03 18:14:24 +0200659 res = make_pending_calls(runtime);
Eric Snowb75b1a352019-04-12 10:20:10 -0600660 if (res != 0) {
661 return res;
662 }
663
664 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000665}
666
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000667/* The interpreter's recursion limit */
668
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000669#ifndef Py_DEFAULT_RECURSION_LIMIT
670#define Py_DEFAULT_RECURSION_LIMIT 1000
671#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600672
Eric Snow05351c12017-09-05 21:43:08 -0700673int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000674
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600675void
Victor Stinnere225beb2019-06-03 18:14:24 +0200676_PyEval_Initialize(struct _ceval_runtime_state *state)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600677{
Victor Stinnere225beb2019-06-03 18:14:24 +0200678 state->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600679 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Victor Stinnere225beb2019-06-03 18:14:24 +0200680 _gil_initialize(&state->gil);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600681}
682
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000683int
684Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000685{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100686 struct _ceval_runtime_state *ceval = &_PyRuntime.ceval;
687 return ceval->recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000688}
689
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000690void
691Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000692{
Victor Stinnere225beb2019-06-03 18:14:24 +0200693 struct _ceval_runtime_state *ceval = &_PyRuntime.ceval;
694 ceval->recursion_limit = new_limit;
695 _Py_CheckRecursionLimit = ceval->recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000696}
697
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100698/* The function _Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
Armin Rigo2b3eb402003-10-28 12:05:48 +0000699 if the recursion_depth reaches _Py_CheckRecursionLimit.
700 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
701 to guarantee that _Py_CheckRecursiveCall() is regularly called.
702 Without USE_STACKCHECK, there is no need for this. */
703int
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100704_Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000705{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100706 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200707 int recursion_limit = runtime->ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000708
709#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700710 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 if (PyOS_CheckStack()) {
712 --tstate->recursion_depth;
Victor Stinner438a12d2019-05-24 17:01:38 +0200713 _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 return -1;
715 }
pdox18967932017-10-25 23:03:01 -0700716 /* Needed for ABI backwards-compatibility (see bpo-31857) */
Eric Snow05351c12017-09-05 21:43:08 -0700717 _Py_CheckRecursionLimit = recursion_limit;
pdox18967932017-10-25 23:03:01 -0700718#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 if (tstate->recursion_critical)
720 /* Somebody asked that we don't check for recursion. */
721 return 0;
722 if (tstate->overflowed) {
723 if (tstate->recursion_depth > recursion_limit + 50) {
724 /* Overflowing while handling an overflow. Give up. */
725 Py_FatalError("Cannot recover from stack overflow.");
726 }
727 return 0;
728 }
729 if (tstate->recursion_depth > recursion_limit) {
730 --tstate->recursion_depth;
731 tstate->overflowed = 1;
Victor Stinner438a12d2019-05-24 17:01:38 +0200732 _PyErr_Format(tstate, PyExc_RecursionError,
733 "maximum recursion depth exceeded%s",
734 where);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 return -1;
736 }
737 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000738}
739
Victor Stinner09532fe2019-05-10 23:39:09 +0200740static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
Victor Stinner438a12d2019-05-24 17:01:38 +0200741static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000742
Victor Stinnere225beb2019-06-03 18:14:24 +0200743#define _Py_TracingPossible(ceval) ((ceval)->tracing_possible)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000744
Guido van Rossum374a9221991-04-04 10:40:29 +0000745
Guido van Rossumb209a111997-04-29 18:18:01 +0000746PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000747PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000748{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 return PyEval_EvalCodeEx(co,
750 globals, locals,
751 (PyObject **)NULL, 0,
752 (PyObject **)NULL, 0,
753 (PyObject **)NULL, 0,
754 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000755}
756
757
758/* Interpreter main loop */
759
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000760PyObject *
Victor Stinnerb9e68122019-11-14 12:20:46 +0100761PyEval_EvalFrame(PyFrameObject *f)
762{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 /* This is for backward compatibility with extension modules that
764 used this API; core interpreter code should call
765 PyEval_EvalFrameEx() */
Victor Stinnerb9e68122019-11-14 12:20:46 +0100766 PyThreadState *tstate = _PyThreadState_GET();
767 return _PyEval_EvalFrame(tstate, f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000768}
769
770PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000771PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000772{
Victor Stinnerb9e68122019-11-14 12:20:46 +0100773 PyThreadState *tstate = _PyThreadState_GET();
774 return _PyEval_EvalFrame(tstate, f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700775}
776
Victor Stinnerc6944e72016-11-11 02:13:35 +0100777PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700778_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
779{
Guido van Rossum950361c1997-01-24 13:49:28 +0000780#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000782#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200783 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300784 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200785 int opcode; /* Current opcode */
786 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200787 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 PyObject *retval = NULL; /* Return value */
Victor Stinner09532fe2019-05-10 23:39:09 +0200789 _PyRuntimeState * const runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200790 struct _ceval_runtime_state * const ceval = &runtime->ceval;
791 _Py_atomic_int * const eval_breaker = &ceval->eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000793
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100794 PyThreadState * const tstate = _PyRuntimeState_GetThreadState(runtime);
795 ensure_tstate_not_null(__func__, tstate);
796 assert(is_tstate_valid(tstate));
797
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000799
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000801
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 is true when the line being executed has changed. The
803 initial values are such as to make this false the first
804 time it is tested. */
805 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000806
Serhiy Storchakaab874002016-09-11 13:48:15 +0300807 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 PyObject *names;
809 PyObject *consts;
Inada Naoki91234a12019-06-03 21:30:58 +0900810 _PyOpcache *co_opcache;
Guido van Rossum374a9221991-04-04 10:40:29 +0000811
Brett Cannon368b4b72012-04-02 12:17:59 -0400812#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200813 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400814#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200815
Antoine Pitroub52ec782009-01-25 16:34:23 +0000816/* Computed GOTOs, or
817 the-optimization-commonly-but-improperly-known-as-"threaded code"
818 using gcc's labels-as-values extension
819 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
820
821 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000823 combined with a lookup table of jump addresses. However, since the
824 indirect jump instruction is shared by all opcodes, the CPU will have a
825 hard time making the right prediction for where to jump next (actually,
826 it will be always wrong except in the uncommon case of a sequence of
827 several identical opcodes).
828
829 "Threaded code" in contrast, uses an explicit jump table and an explicit
830 indirect jump instruction at the end of each opcode. Since the jump
831 instruction is at a different address for each opcode, the CPU will make a
832 separate prediction for each of these instructions, which is equivalent to
833 predicting the second opcode of each opcode pair. These predictions have
834 a much better chance to turn out valid, especially in small bytecode loops.
835
836 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000837 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000838 and potentially many more instructions (depending on the pipeline width).
839 A correctly predicted branch, however, is nearly free.
840
841 At the time of this writing, the "threaded code" version is up to 15-20%
842 faster than the normal "switch" version, depending on the compiler and the
843 CPU architecture.
844
845 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
846 because it would render the measurements invalid.
847
848
849 NOTE: care must be taken that the compiler doesn't try to "optimize" the
850 indirect jumps by sharing them between all opcodes. Such optimizations
851 can be disabled on gcc by using the -fno-gcse flag (or possibly
852 -fno-crossjumping).
853*/
854
Antoine Pitrou042b1282010-08-13 21:15:58 +0000855#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000856#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000857#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000858#endif
859
Antoine Pitrou042b1282010-08-13 21:15:58 +0000860#ifdef HAVE_COMPUTED_GOTOS
861 #ifndef USE_COMPUTED_GOTOS
862 #define USE_COMPUTED_GOTOS 1
863 #endif
864#else
865 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
866 #error "Computed gotos are not supported on this compiler."
867 #endif
868 #undef USE_COMPUTED_GOTOS
869 #define USE_COMPUTED_GOTOS 0
870#endif
871
872#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000873/* Import the static jump table */
874#include "opcode_targets.h"
875
Antoine Pitroub52ec782009-01-25 16:34:23 +0000876#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -0700877 op: \
878 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000879
Antoine Pitroub52ec782009-01-25 16:34:23 +0000880#ifdef LLTRACE
881#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200883 if (!lltrace && !_Py_TracingPossible(ceval) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300885 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300886 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 } \
888 goto fast_next_opcode; \
889 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000890#else
891#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200893 if (!_Py_TracingPossible(ceval) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300895 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300896 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 } \
898 goto fast_next_opcode; \
899 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000900#endif
901
Victor Stinner09532fe2019-05-10 23:39:09 +0200902#define DISPATCH() \
903 { \
904 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
905 FAST_DISPATCH(); \
906 } \
907 continue; \
908 }
909
Antoine Pitroub52ec782009-01-25 16:34:23 +0000910#else
Benjamin Petersonddd19492018-09-16 22:38:02 -0700911#define TARGET(op) op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000912#define FAST_DISPATCH() goto fast_next_opcode
Victor Stinner09532fe2019-05-10 23:39:09 +0200913#define DISPATCH() continue
Antoine Pitroub52ec782009-01-25 16:34:23 +0000914#endif
915
916
Neal Norwitza81d2202002-07-14 00:27:26 +0000917/* Tuple access macros */
918
919#ifndef Py_DEBUG
920#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
921#else
922#define GETITEM(v, i) PyTuple_GetItem((v), (i))
923#endif
924
Guido van Rossum374a9221991-04-04 10:40:29 +0000925/* Code access macros */
926
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300927/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600928#define INSTR_OFFSET() \
929 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300930#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300931 _Py_CODEUNIT word = *next_instr; \
932 opcode = _Py_OPCODE(word); \
933 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300934 next_instr++; \
935 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300936#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
937#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000938
Raymond Hettingerf606f872003-03-16 03:11:04 +0000939/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 Some opcodes tend to come in pairs thus making it possible to
941 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300942 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 Verifying the prediction costs a single high-speed test of a register
945 variable against a constant. If the pairing was good, then the
946 processor's own internal branch predication has a high likelihood of
947 success, resulting in a nearly zero-overhead transition to the
948 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300949 including its unpredictable switch-case branch. Combined with the
950 processor's internal branch prediction, a successful PREDICT has the
951 effect of making the two opcodes run as if they were a single new opcode
952 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000953
Georg Brandl86b2fb92008-07-16 03:43:04 +0000954 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 predictions turned-on and interpret the results as if some opcodes
956 had been combined or turn-off predictions so that the opcode frequency
957 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000958
959 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 the CPU to record separate branch prediction information for each
961 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000962
Raymond Hettingerf606f872003-03-16 03:11:04 +0000963*/
964
Denis Chernikovbaf29b22020-02-21 12:17:50 +0300965#define PREDICT_ID(op) PRED_##op
966
Antoine Pitrou042b1282010-08-13 21:15:58 +0000967#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Denis Chernikovbaf29b22020-02-21 12:17:50 +0300968#define PREDICT(op) if (0) goto PREDICT_ID(op)
Raymond Hettingera7216982004-02-08 19:59:27 +0000969#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300970#define PREDICT(op) \
Denis Chernikovbaf29b22020-02-21 12:17:50 +0300971 do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300972 _Py_CODEUNIT word = *next_instr; \
973 opcode = _Py_OPCODE(word); \
Denis Chernikovbaf29b22020-02-21 12:17:50 +0300974 if (opcode == op) { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300975 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300976 next_instr++; \
Denis Chernikovbaf29b22020-02-21 12:17:50 +0300977 goto PREDICT_ID(op); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300978 } \
979 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000980#endif
Denis Chernikovbaf29b22020-02-21 12:17:50 +0300981#define PREDICTED(op) PREDICT_ID(op):
Antoine Pitroub52ec782009-01-25 16:34:23 +0000982
Raymond Hettingerf606f872003-03-16 03:11:04 +0000983
Guido van Rossum374a9221991-04-04 10:40:29 +0000984/* Stack manipulation macros */
985
Martin v. Löwis18e16552006-02-15 17:27:45 +0000986/* The stack can grow at most MAXINT deep, as co_nlocals and
987 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000988#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
989#define EMPTY() (STACK_LEVEL() == 0)
990#define TOP() (stack_pointer[-1])
991#define SECOND() (stack_pointer[-2])
992#define THIRD() (stack_pointer[-3])
993#define FOURTH() (stack_pointer[-4])
994#define PEEK(n) (stack_pointer[-(n)])
995#define SET_TOP(v) (stack_pointer[-1] = (v))
996#define SET_SECOND(v) (stack_pointer[-2] = (v))
997#define SET_THIRD(v) (stack_pointer[-3] = (v))
998#define SET_FOURTH(v) (stack_pointer[-4] = (v))
999#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
1000#define BASIC_STACKADJ(n) (stack_pointer += n)
1001#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1002#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001003
Guido van Rossum96a42c81992-01-12 02:29:51 +00001004#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001006 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001007 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +02001008#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001009 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +00001010#define STACK_GROW(n) do { \
1011 assert(n >= 0); \
1012 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001013 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001014 assert(STACK_LEVEL() <= co->co_stacksize); \
1015 } while (0)
1016#define STACK_SHRINK(n) do { \
1017 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +02001018 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001019 (void)(BASIC_STACKADJ(-n)); \
1020 assert(STACK_LEVEL() <= co->co_stacksize); \
1021 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +00001022#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +02001023 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001024 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001025#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001026#define PUSH(v) BASIC_PUSH(v)
1027#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +00001028#define STACK_GROW(n) BASIC_STACKADJ(n)
1029#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001030#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001031#endif
1032
Guido van Rossum681d79a1995-07-18 14:51:37 +00001033/* Local variable macros */
1034
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001036
1037/* The SETLOCAL() macro must not DECREF the local variable in-place and
1038 then store the new value; it must copy the old value to a temporary
1039 value, then store the new value, and then DECREF the temporary value.
1040 This is because it is possible that during the DECREF the frame is
1041 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1042 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001044 GETLOCAL(i) = value; \
1045 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001046
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001047
1048#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 while (STACK_LEVEL() > (b)->b_level) { \
1050 PyObject *v = POP(); \
1051 Py_XDECREF(v); \
1052 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001053
1054#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001055 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +01001057 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1059 while (STACK_LEVEL() > (b)->b_level + 3) { \
1060 value = POP(); \
1061 Py_XDECREF(value); \
1062 } \
Mark Shannonae3087c2017-10-22 22:41:51 +01001063 exc_info = tstate->exc_info; \
1064 type = exc_info->exc_type; \
1065 value = exc_info->exc_value; \
1066 traceback = exc_info->exc_traceback; \
1067 exc_info->exc_type = POP(); \
1068 exc_info->exc_value = POP(); \
1069 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 Py_XDECREF(type); \
1071 Py_XDECREF(value); \
1072 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001073 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001074
Inada Naoki91234a12019-06-03 21:30:58 +09001075 /* macros for opcode cache */
1076#define OPCACHE_CHECK() \
1077 do { \
1078 co_opcache = NULL; \
1079 if (co->co_opcache != NULL) { \
1080 unsigned char co_opt_offset = \
1081 co->co_opcache_map[next_instr - first_instr]; \
1082 if (co_opt_offset > 0) { \
1083 assert(co_opt_offset <= co->co_opcache_size); \
1084 co_opcache = &co->co_opcache[co_opt_offset - 1]; \
1085 assert(co_opcache != NULL); \
Inada Naoki91234a12019-06-03 21:30:58 +09001086 } \
1087 } \
1088 } while (0)
1089
1090#if OPCACHE_STATS
1091
1092#define OPCACHE_STAT_GLOBAL_HIT() \
1093 do { \
1094 if (co->co_opcache != NULL) opcache_global_hits++; \
1095 } while (0)
1096
1097#define OPCACHE_STAT_GLOBAL_MISS() \
1098 do { \
1099 if (co->co_opcache != NULL) opcache_global_misses++; \
1100 } while (0)
1101
1102#define OPCACHE_STAT_GLOBAL_OPT() \
1103 do { \
1104 if (co->co_opcache != NULL) opcache_global_opts++; \
1105 } while (0)
1106
1107#else /* OPCACHE_STATS */
1108
1109#define OPCACHE_STAT_GLOBAL_HIT()
1110#define OPCACHE_STAT_GLOBAL_MISS()
1111#define OPCACHE_STAT_GLOBAL_OPT()
1112
1113#endif
1114
Guido van Rossuma027efa1997-05-05 20:56:21 +00001115/* Start of code */
1116
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 /* push frame */
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001118 if (_Py_EnterRecursiveCall(tstate, "")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001120 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001123
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 if (tstate->use_tracing) {
1125 if (tstate->c_tracefunc != NULL) {
1126 /* tstate->c_tracefunc, if defined, is a
1127 function that will be called on *every* entry
1128 to a code block. Its return value, if not
1129 None, is a function that will be called at
1130 the start of each executed line of code.
1131 (Actually, the function must return itself
1132 in order to continue tracing.) The trace
1133 functions are called with three arguments:
1134 a pointer to the current frame, a string
1135 indicating why the function is called, and
1136 an argument which depends on the situation.
1137 The global trace function is also called
1138 whenever an exception is detected. */
1139 if (call_trace_protected(tstate->c_tracefunc,
1140 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001141 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 /* Trace function raised an error */
1143 goto exit_eval_frame;
1144 }
1145 }
1146 if (tstate->c_profilefunc != NULL) {
1147 /* Similar for c_profilefunc, except it needn't
1148 return itself and isn't called for "line" events */
1149 if (call_trace_protected(tstate->c_profilefunc,
1150 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001151 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 /* Profile function raised an error */
1153 goto exit_eval_frame;
1154 }
1155 }
1156 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001157
Łukasz Langaa785c872016-09-09 17:37:37 -07001158 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1159 dtrace_function_entry(f);
1160
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 co = f->f_code;
1162 names = co->co_names;
1163 consts = co->co_consts;
1164 fastlocals = f->f_localsplus;
1165 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001166 assert(PyBytes_Check(co->co_code));
1167 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001168 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1169 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1170 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001171 /*
1172 f->f_lasti refers to the index of the last instruction,
1173 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001174
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001175 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001176 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001178 When the PREDICT() macros are enabled, some opcode pairs follow in
1179 direct succession without updating f->f_lasti. A successful
1180 prediction effectively links the two codes together as if they
1181 were a single new opcode; accordingly,f->f_lasti will point to
1182 the first code in the pair (for instance, GET_ITER followed by
1183 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001184 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001186 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001187 next_instr = first_instr;
1188 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001189 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1190 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001191 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 stack_pointer = f->f_stacktop;
1193 assert(stack_pointer != NULL);
1194 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001195 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001196
Inada Naoki91234a12019-06-03 21:30:58 +09001197 if (co->co_opcache_flag < OPCACHE_MIN_RUNS) {
1198 co->co_opcache_flag++;
1199 if (co->co_opcache_flag == OPCACHE_MIN_RUNS) {
1200 if (_PyCode_InitOpcache(co) < 0) {
1201 return NULL;
1202 }
1203#if OPCACHE_STATS
1204 opcache_code_objects_extra_mem +=
1205 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1206 sizeof(_PyOpcache) * co->co_opcache_size;
1207 opcache_code_objects++;
1208#endif
1209 }
1210 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001211
Tim Peters5ca576e2001-06-18 22:08:13 +00001212#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001213 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001214#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001215
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001216 if (throwflag) /* support for generator.throw() */
1217 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001218
Victor Stinnerace47d72013-07-18 01:41:08 +02001219#ifdef Py_DEBUG
1220 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001221 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001222 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001223 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001224#endif
1225
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001226main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001228 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1229 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001230 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 /* Do periodic things. Doing this every time through
1233 the loop would add too much overhead, so we do it
1234 only every Nth instruction. We also do it if
1235 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1236 event needs attention (e.g. a signal handler or
1237 async I/O handler); see Py_AddPendingCall() and
1238 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001239
Eric Snow7bda9de2019-03-08 17:25:54 -07001240 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001241 opcode = _Py_OPCODE(*next_instr);
1242 if (opcode == SETUP_FINALLY ||
1243 opcode == SETUP_WITH ||
1244 opcode == BEFORE_ASYNC_WITH ||
1245 opcode == YIELD_FROM) {
1246 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001247 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001248 - If we're about to enter the 'with:'. It will prevent
1249 emitting a resource warning in the common idiom
1250 'with open(path) as file:'.
1251 - If we're about to enter the 'async with:'.
1252 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001253 *very* useful, but might help in some cases and it's
1254 traditional)
1255 - If we're resuming a chain of nested 'yield from' or
1256 'await' calls, then each frame is parked with YIELD_FROM
1257 as its next opcode. If the user hit control-C we want to
1258 wait until we've reached the innermost frame before
1259 running the signal handler and raising KeyboardInterrupt
1260 (see bpo-30039).
1261 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001262 goto fast_next_opcode;
1263 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001264
Victor Stinnere225beb2019-06-03 18:14:24 +02001265 if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
Victor Stinner09532fe2019-05-10 23:39:09 +02001266 if (handle_signals(runtime) != 0) {
Eric Snowfdf282d2019-01-11 14:26:55 -07001267 goto error;
1268 }
1269 }
Victor Stinnere225beb2019-06-03 18:14:24 +02001270 if (_Py_atomic_load_relaxed(&ceval->pending.calls_to_do)) {
1271 if (make_pending_calls(runtime) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001272 goto error;
Eric Snowfdf282d2019-01-11 14:26:55 -07001273 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001275
Victor Stinnere225beb2019-06-03 18:14:24 +02001276 if (_Py_atomic_load_relaxed(&ceval->gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 /* Give another thread a chance */
Victor Stinner09532fe2019-05-10 23:39:09 +02001278 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +01001279 Py_FatalError("tstate mix-up");
Victor Stinner09532fe2019-05-10 23:39:09 +02001280 }
Victor Stinnere225beb2019-06-03 18:14:24 +02001281 drop_gil(ceval, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282
1283 /* Other threads may run now */
1284
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001285 /* Check if we should make a quick exit. */
Victor Stinner01b1cc12019-11-20 02:27:56 +01001286 exit_thread_if_finalizing(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001287
Victor Stinnereb4e2ae2020-03-08 11:57:45 +01001288 take_gil(ceval, tstate);
1289
Victor Stinner09532fe2019-05-10 23:39:09 +02001290 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +01001291 Py_FatalError("orphan tstate");
Victor Stinner09532fe2019-05-10 23:39:09 +02001292 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 }
1294 /* Check for asynchronous exceptions. */
1295 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001296 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 tstate->async_exc = NULL;
Victor Stinnere225beb2019-06-03 18:14:24 +02001298 UNSIGNAL_ASYNC_EXC(ceval);
Victor Stinner438a12d2019-05-24 17:01:38 +02001299 _PyErr_SetNone(tstate, exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001300 Py_DECREF(exc);
1301 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001302 }
1303 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 fast_next_opcode:
1306 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001307
Łukasz Langaa785c872016-09-09 17:37:37 -07001308 if (PyDTrace_LINE_ENABLED())
1309 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001312
Victor Stinnere225beb2019-06-03 18:14:24 +02001313 if (_Py_TracingPossible(ceval) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001314 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001315 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 /* see maybe_call_line_trace
1317 for expository comments */
1318 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 err = maybe_call_line_trace(tstate->c_tracefunc,
1321 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001322 tstate, f,
1323 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 /* Reload possibly changed frame fields */
1325 JUMPTO(f->f_lasti);
1326 if (f->f_stacktop != NULL) {
1327 stack_pointer = f->f_stacktop;
1328 f->f_stacktop = NULL;
1329 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001330 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001331 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001332 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001336
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001337 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001338 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001339#ifdef DYNAMIC_EXECUTION_PROFILE
1340#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 dxpairs[lastopcode][opcode]++;
1342 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001343#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001345#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001346
Guido van Rossum96a42c81992-01-12 02:29:51 +00001347#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001348 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 if (lltrace) {
1351 if (HAS_ARG(opcode)) {
1352 printf("%d: %d, %d\n",
1353 f->f_lasti, opcode, oparg);
1354 }
1355 else {
1356 printf("%d: %d\n",
1357 f->f_lasti, opcode);
1358 }
1359 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001360#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001365 It is essential that any operation that fails must goto error
1366 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001367
Benjamin Petersonddd19492018-09-16 22:38:02 -07001368 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001370 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001371
Benjamin Petersonddd19492018-09-16 22:38:02 -07001372 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001373 PyObject *value = GETLOCAL(oparg);
1374 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001375 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001376 UNBOUNDLOCAL_ERROR_MSG,
1377 PyTuple_GetItem(co->co_varnames, oparg));
1378 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001379 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001380 Py_INCREF(value);
1381 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001383 }
1384
Benjamin Petersonddd19492018-09-16 22:38:02 -07001385 case TARGET(LOAD_CONST): {
1386 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001387 PyObject *value = GETITEM(consts, oparg);
1388 Py_INCREF(value);
1389 PUSH(value);
1390 FAST_DISPATCH();
1391 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001392
Benjamin Petersonddd19492018-09-16 22:38:02 -07001393 case TARGET(STORE_FAST): {
1394 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001395 PyObject *value = POP();
1396 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001398 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001399
Benjamin Petersonddd19492018-09-16 22:38:02 -07001400 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001401 PyObject *value = POP();
1402 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001404 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001405
Benjamin Petersonddd19492018-09-16 22:38:02 -07001406 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001407 PyObject *top = TOP();
1408 PyObject *second = SECOND();
1409 SET_TOP(second);
1410 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001412 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001413
Benjamin Petersonddd19492018-09-16 22:38:02 -07001414 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001415 PyObject *top = TOP();
1416 PyObject *second = SECOND();
1417 PyObject *third = THIRD();
1418 SET_TOP(second);
1419 SET_SECOND(third);
1420 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001421 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001422 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001423
Benjamin Petersonddd19492018-09-16 22:38:02 -07001424 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001425 PyObject *top = TOP();
1426 PyObject *second = SECOND();
1427 PyObject *third = THIRD();
1428 PyObject *fourth = FOURTH();
1429 SET_TOP(second);
1430 SET_SECOND(third);
1431 SET_THIRD(fourth);
1432 SET_FOURTH(top);
1433 FAST_DISPATCH();
1434 }
1435
Benjamin Petersonddd19492018-09-16 22:38:02 -07001436 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001437 PyObject *top = TOP();
1438 Py_INCREF(top);
1439 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001441 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001442
Benjamin Petersonddd19492018-09-16 22:38:02 -07001443 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001444 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001445 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001446 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001447 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001448 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001449 SET_TOP(top);
1450 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001451 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001452 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001453
Benjamin Petersonddd19492018-09-16 22:38:02 -07001454 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001455 PyObject *value = TOP();
1456 PyObject *res = PyNumber_Positive(value);
1457 Py_DECREF(value);
1458 SET_TOP(res);
1459 if (res == NULL)
1460 goto error;
1461 DISPATCH();
1462 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001463
Benjamin Petersonddd19492018-09-16 22:38:02 -07001464 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001465 PyObject *value = TOP();
1466 PyObject *res = PyNumber_Negative(value);
1467 Py_DECREF(value);
1468 SET_TOP(res);
1469 if (res == NULL)
1470 goto error;
1471 DISPATCH();
1472 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001473
Benjamin Petersonddd19492018-09-16 22:38:02 -07001474 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001475 PyObject *value = TOP();
1476 int err = PyObject_IsTrue(value);
1477 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001478 if (err == 0) {
1479 Py_INCREF(Py_True);
1480 SET_TOP(Py_True);
1481 DISPATCH();
1482 }
1483 else if (err > 0) {
1484 Py_INCREF(Py_False);
1485 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001486 DISPATCH();
1487 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001488 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001489 goto error;
1490 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001491
Benjamin Petersonddd19492018-09-16 22:38:02 -07001492 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001493 PyObject *value = TOP();
1494 PyObject *res = PyNumber_Invert(value);
1495 Py_DECREF(value);
1496 SET_TOP(res);
1497 if (res == NULL)
1498 goto error;
1499 DISPATCH();
1500 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001501
Benjamin Petersonddd19492018-09-16 22:38:02 -07001502 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001503 PyObject *exp = POP();
1504 PyObject *base = TOP();
1505 PyObject *res = PyNumber_Power(base, exp, Py_None);
1506 Py_DECREF(base);
1507 Py_DECREF(exp);
1508 SET_TOP(res);
1509 if (res == NULL)
1510 goto error;
1511 DISPATCH();
1512 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001513
Benjamin Petersonddd19492018-09-16 22:38:02 -07001514 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001515 PyObject *right = POP();
1516 PyObject *left = TOP();
1517 PyObject *res = PyNumber_Multiply(left, right);
1518 Py_DECREF(left);
1519 Py_DECREF(right);
1520 SET_TOP(res);
1521 if (res == NULL)
1522 goto error;
1523 DISPATCH();
1524 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001525
Benjamin Petersonddd19492018-09-16 22:38:02 -07001526 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001527 PyObject *right = POP();
1528 PyObject *left = TOP();
1529 PyObject *res = PyNumber_MatrixMultiply(left, right);
1530 Py_DECREF(left);
1531 Py_DECREF(right);
1532 SET_TOP(res);
1533 if (res == NULL)
1534 goto error;
1535 DISPATCH();
1536 }
1537
Benjamin Petersonddd19492018-09-16 22:38:02 -07001538 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001539 PyObject *divisor = POP();
1540 PyObject *dividend = TOP();
1541 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1542 Py_DECREF(dividend);
1543 Py_DECREF(divisor);
1544 SET_TOP(quotient);
1545 if (quotient == NULL)
1546 goto error;
1547 DISPATCH();
1548 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001549
Benjamin Petersonddd19492018-09-16 22:38:02 -07001550 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001551 PyObject *divisor = POP();
1552 PyObject *dividend = TOP();
1553 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1554 Py_DECREF(dividend);
1555 Py_DECREF(divisor);
1556 SET_TOP(quotient);
1557 if (quotient == NULL)
1558 goto error;
1559 DISPATCH();
1560 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001561
Benjamin Petersonddd19492018-09-16 22:38:02 -07001562 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001563 PyObject *divisor = POP();
1564 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001565 PyObject *res;
1566 if (PyUnicode_CheckExact(dividend) && (
1567 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1568 // fast path; string formatting, but not if the RHS is a str subclass
1569 // (see issue28598)
1570 res = PyUnicode_Format(dividend, divisor);
1571 } else {
1572 res = PyNumber_Remainder(dividend, divisor);
1573 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001574 Py_DECREF(divisor);
1575 Py_DECREF(dividend);
1576 SET_TOP(res);
1577 if (res == NULL)
1578 goto error;
1579 DISPATCH();
1580 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001581
Benjamin Petersonddd19492018-09-16 22:38:02 -07001582 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001583 PyObject *right = POP();
1584 PyObject *left = TOP();
1585 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001586 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1587 CPython using bytecode, it is simply worthless.
1588 See http://bugs.python.org/issue21955 and
1589 http://bugs.python.org/issue10044 for the discussion. In short,
1590 no patch shown any impact on a realistic benchmark, only a minor
1591 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001592 if (PyUnicode_CheckExact(left) &&
1593 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001594 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001595 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001596 }
1597 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001598 sum = PyNumber_Add(left, right);
1599 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001600 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001601 Py_DECREF(right);
1602 SET_TOP(sum);
1603 if (sum == NULL)
1604 goto error;
1605 DISPATCH();
1606 }
1607
Benjamin Petersonddd19492018-09-16 22:38:02 -07001608 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001609 PyObject *right = POP();
1610 PyObject *left = TOP();
1611 PyObject *diff = PyNumber_Subtract(left, right);
1612 Py_DECREF(right);
1613 Py_DECREF(left);
1614 SET_TOP(diff);
1615 if (diff == NULL)
1616 goto error;
1617 DISPATCH();
1618 }
1619
Benjamin Petersonddd19492018-09-16 22:38:02 -07001620 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001621 PyObject *sub = POP();
1622 PyObject *container = TOP();
1623 PyObject *res = PyObject_GetItem(container, sub);
1624 Py_DECREF(container);
1625 Py_DECREF(sub);
1626 SET_TOP(res);
1627 if (res == NULL)
1628 goto error;
1629 DISPATCH();
1630 }
1631
Benjamin Petersonddd19492018-09-16 22:38:02 -07001632 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001633 PyObject *right = POP();
1634 PyObject *left = TOP();
1635 PyObject *res = PyNumber_Lshift(left, right);
1636 Py_DECREF(left);
1637 Py_DECREF(right);
1638 SET_TOP(res);
1639 if (res == NULL)
1640 goto error;
1641 DISPATCH();
1642 }
1643
Benjamin Petersonddd19492018-09-16 22:38:02 -07001644 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001645 PyObject *right = POP();
1646 PyObject *left = TOP();
1647 PyObject *res = PyNumber_Rshift(left, right);
1648 Py_DECREF(left);
1649 Py_DECREF(right);
1650 SET_TOP(res);
1651 if (res == NULL)
1652 goto error;
1653 DISPATCH();
1654 }
1655
Benjamin Petersonddd19492018-09-16 22:38:02 -07001656 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001657 PyObject *right = POP();
1658 PyObject *left = TOP();
1659 PyObject *res = PyNumber_And(left, right);
1660 Py_DECREF(left);
1661 Py_DECREF(right);
1662 SET_TOP(res);
1663 if (res == NULL)
1664 goto error;
1665 DISPATCH();
1666 }
1667
Benjamin Petersonddd19492018-09-16 22:38:02 -07001668 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001669 PyObject *right = POP();
1670 PyObject *left = TOP();
1671 PyObject *res = PyNumber_Xor(left, right);
1672 Py_DECREF(left);
1673 Py_DECREF(right);
1674 SET_TOP(res);
1675 if (res == NULL)
1676 goto error;
1677 DISPATCH();
1678 }
1679
Benjamin Petersonddd19492018-09-16 22:38:02 -07001680 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001681 PyObject *right = POP();
1682 PyObject *left = TOP();
1683 PyObject *res = PyNumber_Or(left, right);
1684 Py_DECREF(left);
1685 Py_DECREF(right);
1686 SET_TOP(res);
1687 if (res == NULL)
1688 goto error;
1689 DISPATCH();
1690 }
1691
Benjamin Petersonddd19492018-09-16 22:38:02 -07001692 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001693 PyObject *v = POP();
1694 PyObject *list = PEEK(oparg);
1695 int err;
1696 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001698 if (err != 0)
1699 goto error;
1700 PREDICT(JUMP_ABSOLUTE);
1701 DISPATCH();
1702 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001703
Benjamin Petersonddd19492018-09-16 22:38:02 -07001704 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001705 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001706 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001707 int err;
1708 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001709 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001710 if (err != 0)
1711 goto error;
1712 PREDICT(JUMP_ABSOLUTE);
1713 DISPATCH();
1714 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001715
Benjamin Petersonddd19492018-09-16 22:38:02 -07001716 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001717 PyObject *exp = POP();
1718 PyObject *base = TOP();
1719 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1720 Py_DECREF(base);
1721 Py_DECREF(exp);
1722 SET_TOP(res);
1723 if (res == NULL)
1724 goto error;
1725 DISPATCH();
1726 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001727
Benjamin Petersonddd19492018-09-16 22:38:02 -07001728 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001729 PyObject *right = POP();
1730 PyObject *left = TOP();
1731 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1732 Py_DECREF(left);
1733 Py_DECREF(right);
1734 SET_TOP(res);
1735 if (res == NULL)
1736 goto error;
1737 DISPATCH();
1738 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001739
Benjamin Petersonddd19492018-09-16 22:38:02 -07001740 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001741 PyObject *right = POP();
1742 PyObject *left = TOP();
1743 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1744 Py_DECREF(left);
1745 Py_DECREF(right);
1746 SET_TOP(res);
1747 if (res == NULL)
1748 goto error;
1749 DISPATCH();
1750 }
1751
Benjamin Petersonddd19492018-09-16 22:38:02 -07001752 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001753 PyObject *divisor = POP();
1754 PyObject *dividend = TOP();
1755 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1756 Py_DECREF(dividend);
1757 Py_DECREF(divisor);
1758 SET_TOP(quotient);
1759 if (quotient == NULL)
1760 goto error;
1761 DISPATCH();
1762 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001763
Benjamin Petersonddd19492018-09-16 22:38:02 -07001764 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001765 PyObject *divisor = POP();
1766 PyObject *dividend = TOP();
1767 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1768 Py_DECREF(dividend);
1769 Py_DECREF(divisor);
1770 SET_TOP(quotient);
1771 if (quotient == NULL)
1772 goto error;
1773 DISPATCH();
1774 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001775
Benjamin Petersonddd19492018-09-16 22:38:02 -07001776 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001777 PyObject *right = POP();
1778 PyObject *left = TOP();
1779 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1780 Py_DECREF(left);
1781 Py_DECREF(right);
1782 SET_TOP(mod);
1783 if (mod == NULL)
1784 goto error;
1785 DISPATCH();
1786 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001787
Benjamin Petersonddd19492018-09-16 22:38:02 -07001788 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001789 PyObject *right = POP();
1790 PyObject *left = TOP();
1791 PyObject *sum;
1792 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001793 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001794 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001795 }
1796 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001797 sum = PyNumber_InPlaceAdd(left, right);
1798 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001799 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001800 Py_DECREF(right);
1801 SET_TOP(sum);
1802 if (sum == NULL)
1803 goto error;
1804 DISPATCH();
1805 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001806
Benjamin Petersonddd19492018-09-16 22:38:02 -07001807 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001808 PyObject *right = POP();
1809 PyObject *left = TOP();
1810 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1811 Py_DECREF(left);
1812 Py_DECREF(right);
1813 SET_TOP(diff);
1814 if (diff == NULL)
1815 goto error;
1816 DISPATCH();
1817 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001818
Benjamin Petersonddd19492018-09-16 22:38:02 -07001819 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001820 PyObject *right = POP();
1821 PyObject *left = TOP();
1822 PyObject *res = PyNumber_InPlaceLshift(left, right);
1823 Py_DECREF(left);
1824 Py_DECREF(right);
1825 SET_TOP(res);
1826 if (res == NULL)
1827 goto error;
1828 DISPATCH();
1829 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001830
Benjamin Petersonddd19492018-09-16 22:38:02 -07001831 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001832 PyObject *right = POP();
1833 PyObject *left = TOP();
1834 PyObject *res = PyNumber_InPlaceRshift(left, right);
1835 Py_DECREF(left);
1836 Py_DECREF(right);
1837 SET_TOP(res);
1838 if (res == NULL)
1839 goto error;
1840 DISPATCH();
1841 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001842
Benjamin Petersonddd19492018-09-16 22:38:02 -07001843 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001844 PyObject *right = POP();
1845 PyObject *left = TOP();
1846 PyObject *res = PyNumber_InPlaceAnd(left, right);
1847 Py_DECREF(left);
1848 Py_DECREF(right);
1849 SET_TOP(res);
1850 if (res == NULL)
1851 goto error;
1852 DISPATCH();
1853 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001854
Benjamin Petersonddd19492018-09-16 22:38:02 -07001855 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001856 PyObject *right = POP();
1857 PyObject *left = TOP();
1858 PyObject *res = PyNumber_InPlaceXor(left, right);
1859 Py_DECREF(left);
1860 Py_DECREF(right);
1861 SET_TOP(res);
1862 if (res == NULL)
1863 goto error;
1864 DISPATCH();
1865 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001866
Benjamin Petersonddd19492018-09-16 22:38:02 -07001867 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001868 PyObject *right = POP();
1869 PyObject *left = TOP();
1870 PyObject *res = PyNumber_InPlaceOr(left, right);
1871 Py_DECREF(left);
1872 Py_DECREF(right);
1873 SET_TOP(res);
1874 if (res == NULL)
1875 goto error;
1876 DISPATCH();
1877 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001878
Benjamin Petersonddd19492018-09-16 22:38:02 -07001879 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001880 PyObject *sub = TOP();
1881 PyObject *container = SECOND();
1882 PyObject *v = THIRD();
1883 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001884 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001885 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001886 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001887 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001888 Py_DECREF(container);
1889 Py_DECREF(sub);
1890 if (err != 0)
1891 goto error;
1892 DISPATCH();
1893 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001894
Benjamin Petersonddd19492018-09-16 22:38:02 -07001895 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001896 PyObject *sub = TOP();
1897 PyObject *container = SECOND();
1898 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001899 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00001900 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001901 err = PyObject_DelItem(container, sub);
1902 Py_DECREF(container);
1903 Py_DECREF(sub);
1904 if (err != 0)
1905 goto error;
1906 DISPATCH();
1907 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001908
Benjamin Petersonddd19492018-09-16 22:38:02 -07001909 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01001910 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001911 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001912 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001913 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001914 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001915 _PyErr_SetString(tstate, PyExc_RuntimeError,
1916 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001917 Py_DECREF(value);
1918 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001919 }
Petr Viktorinffd97532020-02-11 17:46:57 +01001920 res = PyObject_CallOneArg(hook, value);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001921 Py_DECREF(value);
1922 if (res == NULL)
1923 goto error;
1924 Py_DECREF(res);
1925 DISPATCH();
1926 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001927
Benjamin Petersonddd19492018-09-16 22:38:02 -07001928 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001929 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930 switch (oparg) {
1931 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001932 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001933 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001934 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001935 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001936 /* fall through */
1937 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02001938 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001939 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001940 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001941 break;
1942 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02001943 _PyErr_SetString(tstate, PyExc_SystemError,
1944 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001945 break;
1946 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001947 goto error;
1948 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001949
Benjamin Petersonddd19492018-09-16 22:38:02 -07001950 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001951 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001952 assert(f->f_iblock == 0);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00001953 assert(EMPTY());
1954 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001955 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001956
Benjamin Petersonddd19492018-09-16 22:38:02 -07001957 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001958 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001959 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001960 PyObject *obj = TOP();
1961 PyTypeObject *type = Py_TYPE(obj);
1962
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001963 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001964 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001965 }
Yury Selivanov75445082015-05-11 22:57:16 -04001966
1967 if (getter != NULL) {
1968 iter = (*getter)(obj);
1969 Py_DECREF(obj);
1970 if (iter == NULL) {
1971 SET_TOP(NULL);
1972 goto error;
1973 }
1974 }
1975 else {
1976 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02001977 _PyErr_Format(tstate, PyExc_TypeError,
1978 "'async for' requires an object with "
1979 "__aiter__ method, got %.100s",
1980 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001981 Py_DECREF(obj);
1982 goto error;
1983 }
1984
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001985 if (Py_TYPE(iter)->tp_as_async == NULL ||
1986 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001987
Yury Selivanov398ff912017-03-02 22:20:00 -05001988 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02001989 _PyErr_Format(tstate, PyExc_TypeError,
1990 "'async for' received an object from __aiter__ "
1991 "that does not implement __anext__: %.100s",
1992 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001993 Py_DECREF(iter);
1994 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001995 }
1996
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001997 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001998 DISPATCH();
1999 }
2000
Benjamin Petersonddd19492018-09-16 22:38:02 -07002001 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002002 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002003 PyObject *next_iter = NULL;
2004 PyObject *awaitable = NULL;
2005 PyObject *aiter = TOP();
2006 PyTypeObject *type = Py_TYPE(aiter);
2007
Yury Selivanoveb636452016-09-08 22:01:51 -07002008 if (PyAsyncGen_CheckExact(aiter)) {
2009 awaitable = type->tp_as_async->am_anext(aiter);
2010 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002011 goto error;
2012 }
Yury Selivanoveb636452016-09-08 22:01:51 -07002013 } else {
2014 if (type->tp_as_async != NULL){
2015 getter = type->tp_as_async->am_anext;
2016 }
Yury Selivanov75445082015-05-11 22:57:16 -04002017
Yury Selivanoveb636452016-09-08 22:01:51 -07002018 if (getter != NULL) {
2019 next_iter = (*getter)(aiter);
2020 if (next_iter == NULL) {
2021 goto error;
2022 }
2023 }
2024 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02002025 _PyErr_Format(tstate, PyExc_TypeError,
2026 "'async for' requires an iterator with "
2027 "__anext__ method, got %.100s",
2028 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07002029 goto error;
2030 }
Yury Selivanov75445082015-05-11 22:57:16 -04002031
Yury Selivanoveb636452016-09-08 22:01:51 -07002032 awaitable = _PyCoro_GetAwaitableIter(next_iter);
2033 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05002034 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07002035 PyExc_TypeError,
2036 "'async for' received an invalid object "
2037 "from __anext__: %.100s",
2038 Py_TYPE(next_iter)->tp_name);
2039
2040 Py_DECREF(next_iter);
2041 goto error;
2042 } else {
2043 Py_DECREF(next_iter);
2044 }
2045 }
Yury Selivanov75445082015-05-11 22:57:16 -04002046
2047 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002048 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002049 DISPATCH();
2050 }
2051
Benjamin Petersonddd19492018-09-16 22:38:02 -07002052 case TARGET(GET_AWAITABLE): {
2053 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002054 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002055 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002056
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002057 if (iter == NULL) {
Mark Shannonfee55262019-11-21 09:11:43 +00002058 int opcode_at_minus_3 = 0;
2059 if ((next_instr - first_instr) > 2) {
2060 opcode_at_minus_3 = _Py_OPCODE(next_instr[-3]);
2061 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002062 format_awaitable_error(tstate, Py_TYPE(iterable),
Mark Shannonfee55262019-11-21 09:11:43 +00002063 opcode_at_minus_3,
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002064 _Py_OPCODE(next_instr[-2]));
2065 }
2066
Yury Selivanov75445082015-05-11 22:57:16 -04002067 Py_DECREF(iterable);
2068
Yury Selivanovc724bae2016-03-02 11:30:46 -05002069 if (iter != NULL && PyCoro_CheckExact(iter)) {
2070 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2071 if (yf != NULL) {
2072 /* `iter` is a coroutine object that is being
2073 awaited, `yf` is a pointer to the current awaitable
2074 being awaited on. */
2075 Py_DECREF(yf);
2076 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002077 _PyErr_SetString(tstate, PyExc_RuntimeError,
2078 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002079 /* The code below jumps to `error` if `iter` is NULL. */
2080 }
2081 }
2082
Yury Selivanov75445082015-05-11 22:57:16 -04002083 SET_TOP(iter); /* Even if it's NULL */
2084
2085 if (iter == NULL) {
2086 goto error;
2087 }
2088
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002089 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002090 DISPATCH();
2091 }
2092
Benjamin Petersonddd19492018-09-16 22:38:02 -07002093 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002094 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002095 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002096 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002097 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
2098 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002099 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002100 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002101 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002102 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002103 else
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02002104 retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002105 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002106 Py_DECREF(v);
2107 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002108 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002109 if (tstate->c_tracefunc != NULL
Victor Stinner438a12d2019-05-24 17:01:38 +02002110 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002111 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002112 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002113 if (err < 0)
2114 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002115 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002116 SET_TOP(val);
2117 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002118 }
Martin Panter95f53c12016-07-18 08:23:26 +00002119 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002120 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002121 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002122 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002123 f->f_lasti -= sizeof(_Py_CODEUNIT);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002124 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002125 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002126
Benjamin Petersonddd19492018-09-16 22:38:02 -07002127 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002128 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002129
2130 if (co->co_flags & CO_ASYNC_GENERATOR) {
2131 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2132 Py_DECREF(retval);
2133 if (w == NULL) {
2134 retval = NULL;
2135 goto error;
2136 }
2137 retval = w;
2138 }
2139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002140 f->f_stacktop = stack_pointer;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002141 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002142 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002143
Benjamin Petersonddd19492018-09-16 22:38:02 -07002144 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002145 PyObject *type, *value, *traceback;
2146 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002147 PyTryBlock *b = PyFrame_BlockPop(f);
2148 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002149 _PyErr_SetString(tstate, PyExc_SystemError,
2150 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002151 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002152 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002153 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2154 STACK_LEVEL() <= (b)->b_level + 4);
2155 exc_info = tstate->exc_info;
2156 type = exc_info->exc_type;
2157 value = exc_info->exc_value;
2158 traceback = exc_info->exc_traceback;
2159 exc_info->exc_type = POP();
2160 exc_info->exc_value = POP();
2161 exc_info->exc_traceback = POP();
2162 Py_XDECREF(type);
2163 Py_XDECREF(value);
2164 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002165 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002166 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002167
Benjamin Petersonddd19492018-09-16 22:38:02 -07002168 case TARGET(POP_BLOCK): {
2169 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002170 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002171 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002172 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002173
Mark Shannonfee55262019-11-21 09:11:43 +00002174 case TARGET(RERAISE): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002175 PyObject *exc = POP();
Mark Shannonfee55262019-11-21 09:11:43 +00002176 PyObject *val = POP();
2177 PyObject *tb = POP();
2178 assert(PyExceptionClass_Check(exc));
Victor Stinner61f4db82020-01-28 03:37:45 +01002179 _PyErr_Restore(tstate, exc, val, tb);
Mark Shannonfee55262019-11-21 09:11:43 +00002180 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002181 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002182
Benjamin Petersonddd19492018-09-16 22:38:02 -07002183 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002184 PyObject *exc = POP();
2185 assert(PyExceptionClass_Check(exc));
2186 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2187 PyTryBlock *b = PyFrame_BlockPop(f);
2188 assert(b->b_type == EXCEPT_HANDLER);
2189 Py_DECREF(exc);
2190 UNWIND_EXCEPT_HANDLER(b);
2191 Py_DECREF(POP());
2192 JUMPBY(oparg);
2193 FAST_DISPATCH();
2194 }
2195 else {
2196 PyObject *val = POP();
2197 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002198 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002199 goto exception_unwind;
2200 }
2201 }
2202
Zackery Spytzce6a0702019-08-25 03:44:09 -06002203 case TARGET(LOAD_ASSERTION_ERROR): {
2204 PyObject *value = PyExc_AssertionError;
2205 Py_INCREF(value);
2206 PUSH(value);
2207 FAST_DISPATCH();
2208 }
2209
Benjamin Petersonddd19492018-09-16 22:38:02 -07002210 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002211 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002212
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002213 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002214 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002215 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002216 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002217 if (!_PyErr_Occurred(tstate)) {
2218 _PyErr_SetString(tstate, PyExc_NameError,
2219 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002220 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002221 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002222 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002223 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002224 }
2225 else {
2226 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2227 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002228 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002229 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2230 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002231 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2232 _PyErr_SetString(tstate, PyExc_NameError,
2233 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002234 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002235 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002236 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002237 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002238 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002239 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002240
Benjamin Petersonddd19492018-09-16 22:38:02 -07002241 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002242 PyObject *name = GETITEM(names, oparg);
2243 PyObject *v = POP();
2244 PyObject *ns = f->f_locals;
2245 int err;
2246 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002247 _PyErr_Format(tstate, PyExc_SystemError,
2248 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002249 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002250 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002251 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002252 if (PyDict_CheckExact(ns))
2253 err = PyDict_SetItem(ns, name, v);
2254 else
2255 err = PyObject_SetItem(ns, name, v);
2256 Py_DECREF(v);
2257 if (err != 0)
2258 goto error;
2259 DISPATCH();
2260 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002261
Benjamin Petersonddd19492018-09-16 22:38:02 -07002262 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002263 PyObject *name = GETITEM(names, oparg);
2264 PyObject *ns = f->f_locals;
2265 int err;
2266 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002267 _PyErr_Format(tstate, PyExc_SystemError,
2268 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002269 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002270 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002271 err = PyObject_DelItem(ns, name);
2272 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002273 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002274 NAME_ERROR_MSG,
2275 name);
2276 goto error;
2277 }
2278 DISPATCH();
2279 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002280
Benjamin Petersonddd19492018-09-16 22:38:02 -07002281 case TARGET(UNPACK_SEQUENCE): {
2282 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002283 PyObject *seq = POP(), *item, **items;
2284 if (PyTuple_CheckExact(seq) &&
2285 PyTuple_GET_SIZE(seq) == oparg) {
2286 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002287 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002288 item = items[oparg];
2289 Py_INCREF(item);
2290 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002291 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002292 } else if (PyList_CheckExact(seq) &&
2293 PyList_GET_SIZE(seq) == oparg) {
2294 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002295 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002296 item = items[oparg];
2297 Py_INCREF(item);
2298 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002299 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002300 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002301 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002302 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002303 } else {
2304 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002305 Py_DECREF(seq);
2306 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002307 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002308 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002309 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002310 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002311
Benjamin Petersonddd19492018-09-16 22:38:02 -07002312 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002313 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2314 PyObject *seq = POP();
2315
Victor Stinner438a12d2019-05-24 17:01:38 +02002316 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002317 stack_pointer + totalargs)) {
2318 stack_pointer += totalargs;
2319 } else {
2320 Py_DECREF(seq);
2321 goto error;
2322 }
2323 Py_DECREF(seq);
2324 DISPATCH();
2325 }
2326
Benjamin Petersonddd19492018-09-16 22:38:02 -07002327 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002328 PyObject *name = GETITEM(names, oparg);
2329 PyObject *owner = TOP();
2330 PyObject *v = SECOND();
2331 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002332 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002333 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002334 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002335 Py_DECREF(owner);
2336 if (err != 0)
2337 goto error;
2338 DISPATCH();
2339 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002340
Benjamin Petersonddd19492018-09-16 22:38:02 -07002341 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002342 PyObject *name = GETITEM(names, oparg);
2343 PyObject *owner = POP();
2344 int err;
2345 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2346 Py_DECREF(owner);
2347 if (err != 0)
2348 goto error;
2349 DISPATCH();
2350 }
2351
Benjamin Petersonddd19492018-09-16 22:38:02 -07002352 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002353 PyObject *name = GETITEM(names, oparg);
2354 PyObject *v = POP();
2355 int err;
2356 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002357 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002358 if (err != 0)
2359 goto error;
2360 DISPATCH();
2361 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002362
Benjamin Petersonddd19492018-09-16 22:38:02 -07002363 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002364 PyObject *name = GETITEM(names, oparg);
2365 int err;
2366 err = PyDict_DelItem(f->f_globals, name);
2367 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002368 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2369 format_exc_check_arg(tstate, PyExc_NameError,
2370 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002371 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002372 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002373 }
2374 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002375 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002376
Benjamin Petersonddd19492018-09-16 22:38:02 -07002377 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002378 PyObject *name = GETITEM(names, oparg);
2379 PyObject *locals = f->f_locals;
2380 PyObject *v;
2381 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002382 _PyErr_Format(tstate, PyExc_SystemError,
2383 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002384 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002385 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002386 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002387 v = PyDict_GetItemWithError(locals, name);
2388 if (v != NULL) {
2389 Py_INCREF(v);
2390 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002391 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002392 goto error;
2393 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002394 }
2395 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002396 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002397 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002398 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002399 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002400 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 }
2402 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002403 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002404 v = PyDict_GetItemWithError(f->f_globals, name);
2405 if (v != NULL) {
2406 Py_INCREF(v);
2407 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002408 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002409 goto error;
2410 }
2411 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002412 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002413 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002414 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002415 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002416 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002417 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002418 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002419 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002420 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002421 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002422 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002423 }
2424 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002425 v = PyObject_GetItem(f->f_builtins, name);
2426 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002427 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002428 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002429 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002430 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002431 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002432 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002433 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002434 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002435 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002436 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002437 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002438 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002439 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002440
Benjamin Petersonddd19492018-09-16 22:38:02 -07002441 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002442 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002443 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002444 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002445 && PyDict_CheckExact(f->f_builtins))
2446 {
Inada Naoki91234a12019-06-03 21:30:58 +09002447 OPCACHE_CHECK();
2448 if (co_opcache != NULL && co_opcache->optimized > 0) {
2449 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2450
2451 if (lg->globals_ver ==
2452 ((PyDictObject *)f->f_globals)->ma_version_tag
2453 && lg->builtins_ver ==
2454 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2455 {
2456 PyObject *ptr = lg->ptr;
2457 OPCACHE_STAT_GLOBAL_HIT();
2458 assert(ptr != NULL);
2459 Py_INCREF(ptr);
2460 PUSH(ptr);
2461 DISPATCH();
2462 }
2463 }
2464
2465 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002466 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002467 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002468 name);
2469 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002470 if (!_PyErr_OCCURRED()) {
2471 /* _PyDict_LoadGlobal() returns NULL without raising
2472 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002473 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002474 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002475 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002476 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002477 }
Inada Naoki91234a12019-06-03 21:30:58 +09002478
2479 if (co_opcache != NULL) {
2480 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2481
2482 if (co_opcache->optimized == 0) {
2483 /* Wasn't optimized before. */
2484 OPCACHE_STAT_GLOBAL_OPT();
2485 } else {
2486 OPCACHE_STAT_GLOBAL_MISS();
2487 }
2488
2489 co_opcache->optimized = 1;
2490 lg->globals_ver =
2491 ((PyDictObject *)f->f_globals)->ma_version_tag;
2492 lg->builtins_ver =
2493 ((PyDictObject *)f->f_builtins)->ma_version_tag;
2494 lg->ptr = v; /* borrowed */
2495 }
2496
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002497 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002498 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002499 else {
2500 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002501
2502 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09002503 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002504 v = PyObject_GetItem(f->f_globals, name);
2505 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002506 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002507 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002508 }
2509 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002510
Victor Stinnerb4efc962015-11-20 09:24:02 +01002511 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002512 v = PyObject_GetItem(f->f_builtins, name);
2513 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002514 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002515 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002516 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002517 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002518 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002519 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002520 }
2521 }
2522 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002523 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002524 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002525 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002526
Benjamin Petersonddd19492018-09-16 22:38:02 -07002527 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002528 PyObject *v = GETLOCAL(oparg);
2529 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002530 SETLOCAL(oparg, NULL);
2531 DISPATCH();
2532 }
2533 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002534 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002535 UNBOUNDLOCAL_ERROR_MSG,
2536 PyTuple_GetItem(co->co_varnames, oparg)
2537 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002538 goto error;
2539 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002540
Benjamin Petersonddd19492018-09-16 22:38:02 -07002541 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002542 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002543 PyObject *oldobj = PyCell_GET(cell);
2544 if (oldobj != NULL) {
2545 PyCell_SET(cell, NULL);
2546 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002547 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002548 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002549 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002550 goto error;
2551 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002552
Benjamin Petersonddd19492018-09-16 22:38:02 -07002553 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002554 PyObject *cell = freevars[oparg];
2555 Py_INCREF(cell);
2556 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002557 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002558 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002559
Benjamin Petersonddd19492018-09-16 22:38:02 -07002560 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002561 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002562 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002563 assert(locals);
2564 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2565 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2566 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2567 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2568 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002569 value = PyDict_GetItemWithError(locals, name);
2570 if (value != NULL) {
2571 Py_INCREF(value);
2572 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002573 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002574 goto error;
2575 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002576 }
2577 else {
2578 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002579 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002580 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002581 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002582 }
2583 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002584 }
2585 }
2586 if (!value) {
2587 PyObject *cell = freevars[oparg];
2588 value = PyCell_GET(cell);
2589 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002590 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002591 goto error;
2592 }
2593 Py_INCREF(value);
2594 }
2595 PUSH(value);
2596 DISPATCH();
2597 }
2598
Benjamin Petersonddd19492018-09-16 22:38:02 -07002599 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002600 PyObject *cell = freevars[oparg];
2601 PyObject *value = PyCell_GET(cell);
2602 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002603 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002604 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002605 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002606 Py_INCREF(value);
2607 PUSH(value);
2608 DISPATCH();
2609 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002610
Benjamin Petersonddd19492018-09-16 22:38:02 -07002611 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002612 PyObject *v = POP();
2613 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002614 PyObject *oldobj = PyCell_GET(cell);
2615 PyCell_SET(cell, v);
2616 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002617 DISPATCH();
2618 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002619
Benjamin Petersonddd19492018-09-16 22:38:02 -07002620 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002621 PyObject *str;
2622 PyObject *empty = PyUnicode_New(0, 0);
2623 if (empty == NULL) {
2624 goto error;
2625 }
2626 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2627 Py_DECREF(empty);
2628 if (str == NULL)
2629 goto error;
2630 while (--oparg >= 0) {
2631 PyObject *item = POP();
2632 Py_DECREF(item);
2633 }
2634 PUSH(str);
2635 DISPATCH();
2636 }
2637
Benjamin Petersonddd19492018-09-16 22:38:02 -07002638 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002639 PyObject *tup = PyTuple_New(oparg);
2640 if (tup == NULL)
2641 goto error;
2642 while (--oparg >= 0) {
2643 PyObject *item = POP();
2644 PyTuple_SET_ITEM(tup, oparg, item);
2645 }
2646 PUSH(tup);
2647 DISPATCH();
2648 }
2649
Benjamin Petersonddd19492018-09-16 22:38:02 -07002650 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002651 PyObject *list = PyList_New(oparg);
2652 if (list == NULL)
2653 goto error;
2654 while (--oparg >= 0) {
2655 PyObject *item = POP();
2656 PyList_SET_ITEM(list, oparg, item);
2657 }
2658 PUSH(list);
2659 DISPATCH();
2660 }
2661
Mark Shannon13bc1392020-01-23 09:25:17 +00002662 case TARGET(LIST_TO_TUPLE): {
2663 PyObject *list = POP();
2664 PyObject *tuple = PyList_AsTuple(list);
2665 Py_DECREF(list);
2666 if (tuple == NULL) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002667 goto error;
Mark Shannon13bc1392020-01-23 09:25:17 +00002668 }
2669 PUSH(tuple);
2670 DISPATCH();
2671 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002672
Mark Shannon13bc1392020-01-23 09:25:17 +00002673 case TARGET(LIST_EXTEND): {
2674 PyObject *iterable = POP();
2675 PyObject *list = PEEK(oparg);
2676 PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
2677 if (none_val == NULL) {
2678 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01002679 (Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable)))
Mark Shannon13bc1392020-01-23 09:25:17 +00002680 {
Victor Stinner61f4db82020-01-28 03:37:45 +01002681 _PyErr_Clear(tstate);
Mark Shannon13bc1392020-01-23 09:25:17 +00002682 _PyErr_Format(tstate, PyExc_TypeError,
2683 "Value after * must be an iterable, not %.200s",
2684 Py_TYPE(iterable)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002685 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002686 Py_DECREF(iterable);
2687 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002688 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002689 Py_DECREF(none_val);
2690 Py_DECREF(iterable);
2691 DISPATCH();
2692 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002693
Mark Shannon13bc1392020-01-23 09:25:17 +00002694 case TARGET(SET_UPDATE): {
2695 PyObject *iterable = POP();
2696 PyObject *set = PEEK(oparg);
2697 int err = _PySet_Update(set, iterable);
2698 Py_DECREF(iterable);
2699 if (err < 0) {
2700 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002701 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002702 DISPATCH();
2703 }
2704
Benjamin Petersonddd19492018-09-16 22:38:02 -07002705 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002706 PyObject *set = PySet_New(NULL);
2707 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002708 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002709 if (set == NULL)
2710 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002711 for (i = oparg; i > 0; i--) {
2712 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002713 if (err == 0)
2714 err = PySet_Add(set, item);
2715 Py_DECREF(item);
2716 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002717 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002718 if (err != 0) {
2719 Py_DECREF(set);
2720 goto error;
2721 }
2722 PUSH(set);
2723 DISPATCH();
2724 }
2725
Benjamin Petersonddd19492018-09-16 22:38:02 -07002726 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002727 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002728 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2729 if (map == NULL)
2730 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002731 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002732 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002733 PyObject *key = PEEK(2*i);
2734 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002735 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002736 if (err != 0) {
2737 Py_DECREF(map);
2738 goto error;
2739 }
2740 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002741
2742 while (oparg--) {
2743 Py_DECREF(POP());
2744 Py_DECREF(POP());
2745 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002746 PUSH(map);
2747 DISPATCH();
2748 }
2749
Benjamin Petersonddd19492018-09-16 22:38:02 -07002750 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002751 _Py_IDENTIFIER(__annotations__);
2752 int err;
2753 PyObject *ann_dict;
2754 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002755 _PyErr_Format(tstate, PyExc_SystemError,
2756 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002757 goto error;
2758 }
2759 /* check if __annotations__ in locals()... */
2760 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002761 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002762 &PyId___annotations__);
2763 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002764 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002765 goto error;
2766 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002767 /* ...if not, create a new one */
2768 ann_dict = PyDict_New();
2769 if (ann_dict == NULL) {
2770 goto error;
2771 }
2772 err = _PyDict_SetItemId(f->f_locals,
2773 &PyId___annotations__, ann_dict);
2774 Py_DECREF(ann_dict);
2775 if (err != 0) {
2776 goto error;
2777 }
2778 }
2779 }
2780 else {
2781 /* do the same if locals() is not a dict */
2782 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2783 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002784 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002785 }
2786 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2787 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002788 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002789 goto error;
2790 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002791 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002792 ann_dict = PyDict_New();
2793 if (ann_dict == NULL) {
2794 goto error;
2795 }
2796 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2797 Py_DECREF(ann_dict);
2798 if (err != 0) {
2799 goto error;
2800 }
2801 }
2802 else {
2803 Py_DECREF(ann_dict);
2804 }
2805 }
2806 DISPATCH();
2807 }
2808
Benjamin Petersonddd19492018-09-16 22:38:02 -07002809 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002810 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002811 PyObject *map;
2812 PyObject *keys = TOP();
2813 if (!PyTuple_CheckExact(keys) ||
2814 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002815 _PyErr_SetString(tstate, PyExc_SystemError,
2816 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002817 goto error;
2818 }
2819 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2820 if (map == NULL) {
2821 goto error;
2822 }
2823 for (i = oparg; i > 0; i--) {
2824 int err;
2825 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2826 PyObject *value = PEEK(i + 1);
2827 err = PyDict_SetItem(map, key, value);
2828 if (err != 0) {
2829 Py_DECREF(map);
2830 goto error;
2831 }
2832 }
2833
2834 Py_DECREF(POP());
2835 while (oparg--) {
2836 Py_DECREF(POP());
2837 }
2838 PUSH(map);
2839 DISPATCH();
2840 }
2841
Mark Shannon8a4cd702020-01-27 09:57:45 +00002842 case TARGET(DICT_UPDATE): {
2843 PyObject *update = POP();
2844 PyObject *dict = PEEK(oparg);
2845 if (PyDict_Update(dict, update) < 0) {
2846 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
2847 _PyErr_Format(tstate, PyExc_TypeError,
2848 "'%.200s' object is not a mapping",
Victor Stinnera102ed72020-02-07 02:24:48 +01002849 Py_TYPE(update)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002850 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002851 Py_DECREF(update);
2852 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002853 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002854 Py_DECREF(update);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002855 DISPATCH();
2856 }
2857
Mark Shannon8a4cd702020-01-27 09:57:45 +00002858 case TARGET(DICT_MERGE): {
2859 PyObject *update = POP();
2860 PyObject *dict = PEEK(oparg);
2861
2862 if (_PyDict_MergeEx(dict, update, 2) < 0) {
2863 format_kwargs_error(tstate, PEEK(2 + oparg), update);
2864 Py_DECREF(update);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002865 goto error;
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002866 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002867 Py_DECREF(update);
Brandt Bucherf185a732019-09-28 17:12:49 -07002868 PREDICT(CALL_FUNCTION_EX);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002869 DISPATCH();
2870 }
2871
Benjamin Petersonddd19492018-09-16 22:38:02 -07002872 case TARGET(MAP_ADD): {
Jörn Heisslerc8a35412019-06-22 16:40:55 +02002873 PyObject *value = TOP();
2874 PyObject *key = SECOND();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002875 PyObject *map;
2876 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002877 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002878 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002879 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002880 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002881 Py_DECREF(value);
2882 Py_DECREF(key);
2883 if (err != 0)
2884 goto error;
2885 PREDICT(JUMP_ABSOLUTE);
2886 DISPATCH();
2887 }
2888
Benjamin Petersonddd19492018-09-16 22:38:02 -07002889 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002890 PyObject *name = GETITEM(names, oparg);
2891 PyObject *owner = TOP();
2892 PyObject *res = PyObject_GetAttr(owner, name);
2893 Py_DECREF(owner);
2894 SET_TOP(res);
2895 if (res == NULL)
2896 goto error;
2897 DISPATCH();
2898 }
2899
Benjamin Petersonddd19492018-09-16 22:38:02 -07002900 case TARGET(COMPARE_OP): {
Mark Shannon9af0e472020-01-14 10:12:45 +00002901 assert(oparg <= Py_GE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002902 PyObject *right = POP();
2903 PyObject *left = TOP();
Mark Shannon9af0e472020-01-14 10:12:45 +00002904 PyObject *res = PyObject_RichCompare(left, right, oparg);
2905 SET_TOP(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002906 Py_DECREF(left);
2907 Py_DECREF(right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002908 if (res == NULL)
2909 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002910 PREDICT(POP_JUMP_IF_FALSE);
2911 PREDICT(POP_JUMP_IF_TRUE);
2912 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002913 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002914
Mark Shannon9af0e472020-01-14 10:12:45 +00002915 case TARGET(IS_OP): {
2916 PyObject *right = POP();
2917 PyObject *left = TOP();
2918 int res = (left == right)^oparg;
2919 PyObject *b = res ? Py_True : Py_False;
2920 Py_INCREF(b);
2921 SET_TOP(b);
2922 Py_DECREF(left);
2923 Py_DECREF(right);
2924 PREDICT(POP_JUMP_IF_FALSE);
2925 PREDICT(POP_JUMP_IF_TRUE);
2926 FAST_DISPATCH();
2927 }
2928
2929 case TARGET(CONTAINS_OP): {
2930 PyObject *right = POP();
2931 PyObject *left = POP();
2932 int res = PySequence_Contains(right, left);
2933 Py_DECREF(left);
2934 Py_DECREF(right);
2935 if (res < 0) {
2936 goto error;
2937 }
2938 PyObject *b = (res^oparg) ? Py_True : Py_False;
2939 Py_INCREF(b);
2940 PUSH(b);
2941 PREDICT(POP_JUMP_IF_FALSE);
2942 PREDICT(POP_JUMP_IF_TRUE);
2943 FAST_DISPATCH();
2944 }
2945
2946#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
2947 "BaseException is not allowed"
2948
2949 case TARGET(JUMP_IF_NOT_EXC_MATCH): {
2950 PyObject *right = POP();
2951 PyObject *left = POP();
2952 if (PyTuple_Check(right)) {
2953 Py_ssize_t i, length;
2954 length = PyTuple_GET_SIZE(right);
2955 for (i = 0; i < length; i++) {
2956 PyObject *exc = PyTuple_GET_ITEM(right, i);
2957 if (!PyExceptionClass_Check(exc)) {
2958 _PyErr_SetString(tstate, PyExc_TypeError,
2959 CANNOT_CATCH_MSG);
2960 Py_DECREF(left);
2961 Py_DECREF(right);
2962 goto error;
2963 }
2964 }
2965 }
2966 else {
2967 if (!PyExceptionClass_Check(right)) {
2968 _PyErr_SetString(tstate, PyExc_TypeError,
2969 CANNOT_CATCH_MSG);
2970 Py_DECREF(left);
2971 Py_DECREF(right);
2972 goto error;
2973 }
2974 }
2975 int res = PyErr_GivenExceptionMatches(left, right);
2976 Py_DECREF(left);
2977 Py_DECREF(right);
2978 if (res > 0) {
2979 /* Exception matches -- Do nothing */;
2980 }
2981 else if (res == 0) {
2982 JUMPTO(oparg);
2983 }
2984 else {
2985 goto error;
2986 }
2987 DISPATCH();
2988 }
2989
Benjamin Petersonddd19492018-09-16 22:38:02 -07002990 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002991 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002992 PyObject *fromlist = POP();
2993 PyObject *level = TOP();
2994 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02002995 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002996 Py_DECREF(level);
2997 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002998 SET_TOP(res);
2999 if (res == NULL)
3000 goto error;
3001 DISPATCH();
3002 }
3003
Benjamin Petersonddd19492018-09-16 22:38:02 -07003004 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003005 PyObject *from = POP(), *locals;
3006 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003007 if (PyFrame_FastToLocalsWithError(f) < 0) {
3008 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01003009 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003010 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01003011
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003012 locals = f->f_locals;
3013 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003014 _PyErr_SetString(tstate, PyExc_SystemError,
3015 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003016 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003017 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003018 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003019 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003020 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003021 Py_DECREF(from);
3022 if (err != 0)
3023 goto error;
3024 DISPATCH();
3025 }
Guido van Rossum25831651993-05-19 14:50:45 +00003026
Benjamin Petersonddd19492018-09-16 22:38:02 -07003027 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003028 PyObject *name = GETITEM(names, oparg);
3029 PyObject *from = TOP();
3030 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003031 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003032 PUSH(res);
3033 if (res == NULL)
3034 goto error;
3035 DISPATCH();
3036 }
Thomas Wouters52152252000-08-17 22:55:00 +00003037
Benjamin Petersonddd19492018-09-16 22:38:02 -07003038 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003039 JUMPBY(oparg);
3040 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003041 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003042
Benjamin Petersonddd19492018-09-16 22:38:02 -07003043 case TARGET(POP_JUMP_IF_FALSE): {
3044 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003045 PyObject *cond = POP();
3046 int err;
3047 if (cond == Py_True) {
3048 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003049 FAST_DISPATCH();
3050 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003051 if (cond == Py_False) {
3052 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003053 JUMPTO(oparg);
3054 FAST_DISPATCH();
3055 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003056 err = PyObject_IsTrue(cond);
3057 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003058 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003059 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003060 else if (err == 0)
3061 JUMPTO(oparg);
3062 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003063 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003064 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003065 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003066
Benjamin Petersonddd19492018-09-16 22:38:02 -07003067 case TARGET(POP_JUMP_IF_TRUE): {
3068 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003069 PyObject *cond = POP();
3070 int err;
3071 if (cond == Py_False) {
3072 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003073 FAST_DISPATCH();
3074 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003075 if (cond == Py_True) {
3076 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003077 JUMPTO(oparg);
3078 FAST_DISPATCH();
3079 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003080 err = PyObject_IsTrue(cond);
3081 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003082 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003083 JUMPTO(oparg);
3084 }
3085 else if (err == 0)
3086 ;
3087 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003088 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003089 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003090 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003091
Benjamin Petersonddd19492018-09-16 22:38:02 -07003092 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003093 PyObject *cond = TOP();
3094 int err;
3095 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003096 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003097 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003098 FAST_DISPATCH();
3099 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003100 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003101 JUMPTO(oparg);
3102 FAST_DISPATCH();
3103 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003104 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003105 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003106 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003107 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003108 }
3109 else if (err == 0)
3110 JUMPTO(oparg);
3111 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003112 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003113 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003114 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003115
Benjamin Petersonddd19492018-09-16 22:38:02 -07003116 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003117 PyObject *cond = TOP();
3118 int err;
3119 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003120 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003121 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003122 FAST_DISPATCH();
3123 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003124 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003125 JUMPTO(oparg);
3126 FAST_DISPATCH();
3127 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003128 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003129 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003130 JUMPTO(oparg);
3131 }
3132 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003133 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003134 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003135 }
3136 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003137 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003138 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003139 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003140
Benjamin Petersonddd19492018-09-16 22:38:02 -07003141 case TARGET(JUMP_ABSOLUTE): {
3142 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003143 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003144#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003145 /* Enabling this path speeds-up all while and for-loops by bypassing
3146 the per-loop checks for signals. By default, this should be turned-off
3147 because it prevents detection of a control-break in tight loops like
3148 "while 1: pass". Compile with this option turned-on when you need
3149 the speed-up and do not need break checking inside tight loops (ones
3150 that contain only instructions ending with FAST_DISPATCH).
3151 */
3152 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003153#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003154 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003155#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003156 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003157
Benjamin Petersonddd19492018-09-16 22:38:02 -07003158 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003159 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003160 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003161 PyObject *iter = PyObject_GetIter(iterable);
3162 Py_DECREF(iterable);
3163 SET_TOP(iter);
3164 if (iter == NULL)
3165 goto error;
3166 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003167 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003168 DISPATCH();
3169 }
3170
Benjamin Petersonddd19492018-09-16 22:38:02 -07003171 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04003172 /* before: [obj]; after [getiter(obj)] */
3173 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003174 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003175 if (PyCoro_CheckExact(iterable)) {
3176 /* `iterable` is a coroutine */
3177 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3178 /* and it is used in a 'yield from' expression of a
3179 regular generator. */
3180 Py_DECREF(iterable);
3181 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003182 _PyErr_SetString(tstate, PyExc_TypeError,
3183 "cannot 'yield from' a coroutine object "
3184 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04003185 goto error;
3186 }
3187 }
3188 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003189 /* `iterable` is not a generator. */
3190 iter = PyObject_GetIter(iterable);
3191 Py_DECREF(iterable);
3192 SET_TOP(iter);
3193 if (iter == NULL)
3194 goto error;
3195 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003196 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003197 DISPATCH();
3198 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003199
Benjamin Petersonddd19492018-09-16 22:38:02 -07003200 case TARGET(FOR_ITER): {
3201 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003202 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003203 PyObject *iter = TOP();
Victor Stinnera102ed72020-02-07 02:24:48 +01003204 PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003205 if (next != NULL) {
3206 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003207 PREDICT(STORE_FAST);
3208 PREDICT(UNPACK_SEQUENCE);
3209 DISPATCH();
3210 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003211 if (_PyErr_Occurred(tstate)) {
3212 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003213 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003214 }
3215 else if (tstate->c_tracefunc != NULL) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003216 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Victor Stinner438a12d2019-05-24 17:01:38 +02003217 }
3218 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003219 }
3220 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003221 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003222 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003223 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003224 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003225 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003226 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003227
Benjamin Petersonddd19492018-09-16 22:38:02 -07003228 case TARGET(SETUP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003229 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003230 STACK_LEVEL());
3231 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003232 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003233
Benjamin Petersonddd19492018-09-16 22:38:02 -07003234 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003235 _Py_IDENTIFIER(__aenter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003236 _Py_IDENTIFIER(__aexit__);
Yury Selivanov75445082015-05-11 22:57:16 -04003237 PyObject *mgr = TOP();
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003238 PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04003239 PyObject *res;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003240 if (enter == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003241 goto error;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003242 }
3243 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__);
3244 if (exit == NULL) {
3245 Py_DECREF(enter);
3246 goto error;
3247 }
Yury Selivanov75445082015-05-11 22:57:16 -04003248 SET_TOP(exit);
Yury Selivanov75445082015-05-11 22:57:16 -04003249 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003250 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003251 Py_DECREF(enter);
3252 if (res == NULL)
3253 goto error;
3254 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003255 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003256 DISPATCH();
3257 }
3258
Benjamin Petersonddd19492018-09-16 22:38:02 -07003259 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003260 PyObject *res = POP();
3261 /* Setup the finally block before pushing the result
3262 of __aenter__ on the stack. */
3263 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3264 STACK_LEVEL());
3265 PUSH(res);
3266 DISPATCH();
3267 }
3268
Benjamin Petersonddd19492018-09-16 22:38:02 -07003269 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003270 _Py_IDENTIFIER(__enter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003271 _Py_IDENTIFIER(__exit__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003272 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003273 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003274 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003275 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08003276 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003277 }
3278 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003279 if (exit == NULL) {
3280 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003281 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003282 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003283 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003284 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003285 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003286 Py_DECREF(enter);
3287 if (res == NULL)
3288 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003289 /* Setup the finally block before pushing the result
3290 of __enter__ on the stack. */
3291 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3292 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003293
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003294 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003295 DISPATCH();
3296 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003297
Mark Shannonfee55262019-11-21 09:11:43 +00003298 case TARGET(WITH_EXCEPT_START): {
3299 /* At the top of the stack are 7 values:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003300 - (TOP, SECOND, THIRD) = exc_info()
Mark Shannonfee55262019-11-21 09:11:43 +00003301 - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER
3302 - SEVENTH: the context.__exit__ bound method
3303 We call SEVENTH(TOP, SECOND, THIRD).
3304 Then we push again the TOP exception and the __exit__
3305 return value.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003306 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003307 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003308 PyObject *exc, *val, *tb, *res;
3309
Victor Stinner842cfff2016-12-01 14:45:31 +01003310 exc = TOP();
Mark Shannonfee55262019-11-21 09:11:43 +00003311 val = SECOND();
3312 tb = THIRD();
3313 assert(exc != Py_None);
3314 assert(!PyLong_Check(exc));
3315 exit_func = PEEK(7);
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003316 PyObject *stack[4] = {NULL, exc, val, tb};
Petr Viktorinffd97532020-02-11 17:46:57 +01003317 res = PyObject_Vectorcall(exit_func, stack + 1,
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003318 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003319 if (res == NULL)
3320 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003321
Yury Selivanov75445082015-05-11 22:57:16 -04003322 PUSH(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003323 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003324 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003325
Benjamin Petersonddd19492018-09-16 22:38:02 -07003326 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10003327 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003328 PyObject *name = GETITEM(names, oparg);
3329 PyObject *obj = TOP();
3330 PyObject *meth = NULL;
3331
3332 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3333
Yury Selivanovf2392132016-12-13 19:03:51 -05003334 if (meth == NULL) {
3335 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003336 goto error;
3337 }
3338
3339 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003340 /* We can bypass temporary bound method object.
3341 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003342
INADA Naoki015bce62017-01-16 17:23:30 +09003343 meth | self | arg1 | ... | argN
3344 */
3345 SET_TOP(meth);
3346 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003347 }
3348 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003349 /* meth is not an unbound method (but a regular attr, or
3350 something was returned by a descriptor protocol). Set
3351 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003352 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003353
3354 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003355 */
INADA Naoki015bce62017-01-16 17:23:30 +09003356 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003357 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003358 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003359 }
3360 DISPATCH();
3361 }
3362
Benjamin Petersonddd19492018-09-16 22:38:02 -07003363 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003364 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003365 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003366
3367 sp = stack_pointer;
3368
INADA Naoki015bce62017-01-16 17:23:30 +09003369 meth = PEEK(oparg + 2);
3370 if (meth == NULL) {
3371 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3372 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003373
3374 Stack layout:
3375
INADA Naoki015bce62017-01-16 17:23:30 +09003376 ... | NULL | callable | arg1 | ... | argN
3377 ^- TOP()
3378 ^- (-oparg)
3379 ^- (-oparg-1)
3380 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003381
Ville Skyttä49b27342017-08-03 09:00:59 +03003382 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003383 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003384 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003385 res = call_function(tstate, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003386 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003387 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003388 }
3389 else {
3390 /* This is a method call. Stack layout:
3391
INADA Naoki015bce62017-01-16 17:23:30 +09003392 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003393 ^- TOP()
3394 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003395 ^- (-oparg-1)
3396 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003397
INADA Naoki015bce62017-01-16 17:23:30 +09003398 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003399 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003400 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003401 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003402 res = call_function(tstate, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003403 stack_pointer = sp;
3404 }
3405
3406 PUSH(res);
3407 if (res == NULL)
3408 goto error;
3409 DISPATCH();
3410 }
3411
Benjamin Petersonddd19492018-09-16 22:38:02 -07003412 case TARGET(CALL_FUNCTION): {
3413 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003414 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003415 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003416 res = call_function(tstate, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003417 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003418 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003419 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003420 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003421 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003422 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003423 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003424
Benjamin Petersonddd19492018-09-16 22:38:02 -07003425 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003426 PyObject **sp, *res, *names;
3427
3428 names = POP();
Jeroen Demeyer05677862019-08-16 12:41:27 +02003429 assert(PyTuple_Check(names));
3430 assert(PyTuple_GET_SIZE(names) <= oparg);
3431 /* We assume without checking that names contains only strings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003432 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003433 res = call_function(tstate, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003434 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003435 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003436 Py_DECREF(names);
3437
3438 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003439 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003440 }
3441 DISPATCH();
3442 }
3443
Benjamin Petersonddd19492018-09-16 22:38:02 -07003444 case TARGET(CALL_FUNCTION_EX): {
Brandt Bucherf185a732019-09-28 17:12:49 -07003445 PREDICTED(CALL_FUNCTION_EX);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003446 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003447 if (oparg & 0x01) {
3448 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003449 if (!PyDict_CheckExact(kwargs)) {
3450 PyObject *d = PyDict_New();
3451 if (d == NULL)
3452 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003453 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003454 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02003455 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003456 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003457 goto error;
3458 }
3459 Py_DECREF(kwargs);
3460 kwargs = d;
3461 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003462 assert(PyDict_CheckExact(kwargs));
3463 }
3464 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003465 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003466 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003467 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003468 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003469 goto error;
3470 }
3471 Py_SETREF(callargs, PySequence_Tuple(callargs));
3472 if (callargs == NULL) {
3473 goto error;
3474 }
3475 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003476 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003477
Victor Stinner09532fe2019-05-10 23:39:09 +02003478 result = do_call_core(tstate, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003479 Py_DECREF(func);
3480 Py_DECREF(callargs);
3481 Py_XDECREF(kwargs);
3482
3483 SET_TOP(result);
3484 if (result == NULL) {
3485 goto error;
3486 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003487 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003488 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003489
Benjamin Petersonddd19492018-09-16 22:38:02 -07003490 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003491 PyObject *qualname = POP();
3492 PyObject *codeobj = POP();
3493 PyFunctionObject *func = (PyFunctionObject *)
3494 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003495
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003496 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003497 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003498 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003499 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003500 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003501
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003502 if (oparg & 0x08) {
3503 assert(PyTuple_CheckExact(TOP()));
3504 func ->func_closure = POP();
3505 }
3506 if (oparg & 0x04) {
3507 assert(PyDict_CheckExact(TOP()));
3508 func->func_annotations = POP();
3509 }
3510 if (oparg & 0x02) {
3511 assert(PyDict_CheckExact(TOP()));
3512 func->func_kwdefaults = POP();
3513 }
3514 if (oparg & 0x01) {
3515 assert(PyTuple_CheckExact(TOP()));
3516 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003517 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003518
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003519 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003520 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003521 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003522
Benjamin Petersonddd19492018-09-16 22:38:02 -07003523 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003524 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003525 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003526 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003527 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003528 step = NULL;
3529 stop = POP();
3530 start = TOP();
3531 slice = PySlice_New(start, stop, step);
3532 Py_DECREF(start);
3533 Py_DECREF(stop);
3534 Py_XDECREF(step);
3535 SET_TOP(slice);
3536 if (slice == NULL)
3537 goto error;
3538 DISPATCH();
3539 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003540
Benjamin Petersonddd19492018-09-16 22:38:02 -07003541 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003542 /* Handles f-string value formatting. */
3543 PyObject *result;
3544 PyObject *fmt_spec;
3545 PyObject *value;
3546 PyObject *(*conv_fn)(PyObject *);
3547 int which_conversion = oparg & FVC_MASK;
3548 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3549
3550 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003551 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003552
3553 /* See if any conversion is specified. */
3554 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003555 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003556 case FVC_STR: conv_fn = PyObject_Str; break;
3557 case FVC_REPR: conv_fn = PyObject_Repr; break;
3558 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003559 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02003560 _PyErr_Format(tstate, PyExc_SystemError,
3561 "unexpected conversion flag %d",
3562 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003563 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003564 }
3565
3566 /* If there's a conversion function, call it and replace
3567 value with that result. Otherwise, just use value,
3568 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003569 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003570 result = conv_fn(value);
3571 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003572 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003573 Py_XDECREF(fmt_spec);
3574 goto error;
3575 }
3576 value = result;
3577 }
3578
3579 /* If value is a unicode object, and there's no fmt_spec,
3580 then we know the result of format(value) is value
3581 itself. In that case, skip calling format(). I plan to
3582 move this optimization in to PyObject_Format()
3583 itself. */
3584 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3585 /* Do nothing, just transfer ownership to result. */
3586 result = value;
3587 } else {
3588 /* Actually call format(). */
3589 result = PyObject_Format(value, fmt_spec);
3590 Py_DECREF(value);
3591 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003592 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003593 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003594 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003595 }
3596
Eric V. Smith135d5f42016-02-05 18:23:08 -05003597 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003598 DISPATCH();
3599 }
3600
Benjamin Petersonddd19492018-09-16 22:38:02 -07003601 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003602 int oldoparg = oparg;
3603 NEXTOPARG();
3604 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003605 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003606 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003607
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003608
Antoine Pitrou042b1282010-08-13 21:15:58 +00003609#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003610 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003611#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003612 default:
3613 fprintf(stderr,
3614 "XXX lineno: %d, opcode: %d\n",
3615 PyFrame_GetLineNumber(f),
3616 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02003617 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003618 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003620 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003621
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003622 /* This should never be reached. Every opcode should end with DISPATCH()
3623 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003624 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003625
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003626error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003627 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003628#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02003629 if (!_PyErr_Occurred(tstate)) {
3630 _PyErr_SetString(tstate, PyExc_SystemError,
3631 "error return without exception set");
3632 }
Victor Stinner365b6932013-07-12 00:11:58 +02003633#else
Victor Stinner438a12d2019-05-24 17:01:38 +02003634 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02003635#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003636
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003637 /* Log traceback info. */
3638 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003639
Benjamin Peterson51f46162013-01-23 08:38:47 -05003640 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003641 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3642 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003643
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003644exception_unwind:
3645 /* Unwind stacks if an exception occurred */
3646 while (f->f_iblock > 0) {
3647 /* Pop the current block. */
3648 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003649
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003650 if (b->b_type == EXCEPT_HANDLER) {
3651 UNWIND_EXCEPT_HANDLER(b);
3652 continue;
3653 }
3654 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003655 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003656 PyObject *exc, *val, *tb;
3657 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003658 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003659 /* Beware, this invalidates all b->b_* fields */
3660 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003661 PUSH(exc_info->exc_traceback);
3662 PUSH(exc_info->exc_value);
3663 if (exc_info->exc_type != NULL) {
3664 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003665 }
3666 else {
3667 Py_INCREF(Py_None);
3668 PUSH(Py_None);
3669 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003670 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003671 /* Make the raw exception data
3672 available to the handler,
3673 so a program can emulate the
3674 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02003675 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003676 if (tb != NULL)
3677 PyException_SetTraceback(val, tb);
3678 else
3679 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003680 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003681 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003682 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003683 exc_info->exc_value = val;
3684 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003685 if (tb == NULL)
3686 tb = Py_None;
3687 Py_INCREF(tb);
3688 PUSH(tb);
3689 PUSH(val);
3690 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003691 JUMPTO(handler);
Pablo Galindo4c53e632020-01-10 09:24:22 +00003692 if (_Py_TracingPossible(ceval)) {
3693 int needs_new_execution_window = (f->f_lasti < instr_lb || f->f_lasti >= instr_ub);
3694 int needs_line_update = (f->f_lasti == instr_lb || f->f_lasti < instr_prev);
3695 /* Make sure that we trace line after exception if we are in a new execution
3696 * window or we don't need a line update and we are not in the first instruction
3697 * of the line. */
3698 if (needs_new_execution_window || (!needs_line_update && instr_lb > 0)) {
3699 instr_prev = INT_MAX;
3700 }
Mark Shannonfee55262019-11-21 09:11:43 +00003701 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003702 /* Resume normal execution */
3703 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003704 }
3705 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003706
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003707 /* End the loop as we still have an error */
3708 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003709 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003710
Pablo Galindof00828a2019-05-09 16:52:02 +01003711 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003712 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01003713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003714 /* Pop remaining stack entries. */
3715 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003716 PyObject *o = POP();
3717 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003718 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003719
Mark Shannone7c9f4a2020-01-13 12:51:26 +00003720exiting:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003721 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003722 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003723 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3724 tstate, f, PyTrace_RETURN, retval)) {
3725 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003726 }
3727 }
3728 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003729 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3730 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003731 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003732 }
3733 }
3734 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003735
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003736 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003737exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003738 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3739 dtrace_function_return(f);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01003740 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrou58720d62013-08-05 23:26:40 +02003741 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003742 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003743
Victor Stinner17269092019-11-05 01:22:12 +01003744 return _Py_CheckFunctionResult(tstate, NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003745}
3746
Benjamin Petersonb204a422011-06-05 22:04:07 -05003747static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003748format_missing(PyThreadState *tstate, const char *kind,
3749 PyCodeObject *co, PyObject *names)
Benjamin Petersone109c702011-06-24 09:37:26 -05003750{
3751 int err;
3752 Py_ssize_t len = PyList_GET_SIZE(names);
3753 PyObject *name_str, *comma, *tail, *tmp;
3754
3755 assert(PyList_CheckExact(names));
3756 assert(len >= 1);
3757 /* Deal with the joys of natural language. */
3758 switch (len) {
3759 case 1:
3760 name_str = PyList_GET_ITEM(names, 0);
3761 Py_INCREF(name_str);
3762 break;
3763 case 2:
3764 name_str = PyUnicode_FromFormat("%U and %U",
3765 PyList_GET_ITEM(names, len - 2),
3766 PyList_GET_ITEM(names, len - 1));
3767 break;
3768 default:
3769 tail = PyUnicode_FromFormat(", %U, and %U",
3770 PyList_GET_ITEM(names, len - 2),
3771 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003772 if (tail == NULL)
3773 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003774 /* Chop off the last two objects in the list. This shouldn't actually
3775 fail, but we can't be too careful. */
3776 err = PyList_SetSlice(names, len - 2, len, NULL);
3777 if (err == -1) {
3778 Py_DECREF(tail);
3779 return;
3780 }
3781 /* Stitch everything up into a nice comma-separated list. */
3782 comma = PyUnicode_FromString(", ");
3783 if (comma == NULL) {
3784 Py_DECREF(tail);
3785 return;
3786 }
3787 tmp = PyUnicode_Join(comma, names);
3788 Py_DECREF(comma);
3789 if (tmp == NULL) {
3790 Py_DECREF(tail);
3791 return;
3792 }
3793 name_str = PyUnicode_Concat(tmp, tail);
3794 Py_DECREF(tmp);
3795 Py_DECREF(tail);
3796 break;
3797 }
3798 if (name_str == NULL)
3799 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02003800 _PyErr_Format(tstate, PyExc_TypeError,
3801 "%U() missing %i required %s argument%s: %U",
3802 co->co_name,
3803 len,
3804 kind,
3805 len == 1 ? "" : "s",
3806 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05003807 Py_DECREF(name_str);
3808}
3809
3810static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003811missing_arguments(PyThreadState *tstate, PyCodeObject *co,
3812 Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003813 PyObject **fastlocals)
3814{
Victor Stinner74319ae2016-08-25 00:04:09 +02003815 Py_ssize_t i, j = 0;
3816 Py_ssize_t start, end;
3817 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003818 const char *kind = positional ? "positional" : "keyword-only";
3819 PyObject *missing_names;
3820
3821 /* Compute the names of the arguments that are missing. */
3822 missing_names = PyList_New(missing);
3823 if (missing_names == NULL)
3824 return;
3825 if (positional) {
3826 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01003827 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003828 }
3829 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003830 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003831 end = start + co->co_kwonlyargcount;
3832 }
3833 for (i = start; i < end; i++) {
3834 if (GETLOCAL(i) == NULL) {
3835 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3836 PyObject *name = PyObject_Repr(raw);
3837 if (name == NULL) {
3838 Py_DECREF(missing_names);
3839 return;
3840 }
3841 PyList_SET_ITEM(missing_names, j++, name);
3842 }
3843 }
3844 assert(j == missing);
Victor Stinner438a12d2019-05-24 17:01:38 +02003845 format_missing(tstate, kind, co, missing_names);
Benjamin Petersone109c702011-06-24 09:37:26 -05003846 Py_DECREF(missing_names);
3847}
3848
3849static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003850too_many_positional(PyThreadState *tstate, PyCodeObject *co,
3851 Py_ssize_t given, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003852 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003853{
3854 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003855 Py_ssize_t kwonly_given = 0;
3856 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003857 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003858 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003859
Benjamin Petersone109c702011-06-24 09:37:26 -05003860 assert((co->co_flags & CO_VARARGS) == 0);
3861 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01003862 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003863 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003864 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003865 }
3866 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003867 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01003868 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003869 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01003870 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003871 }
3872 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003873 plural = (co_argcount != 1);
3874 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003875 }
3876 if (sig == NULL)
3877 return;
3878 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003879 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3880 kwonly_sig = PyUnicode_FromFormat(format,
3881 given != 1 ? "s" : "",
3882 kwonly_given,
3883 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003884 if (kwonly_sig == NULL) {
3885 Py_DECREF(sig);
3886 return;
3887 }
3888 }
3889 else {
3890 /* This will not fail. */
3891 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003892 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003893 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003894 _PyErr_Format(tstate, PyExc_TypeError,
3895 "%U() takes %U positional argument%s but %zd%U %s given",
3896 co->co_name,
3897 sig,
3898 plural ? "s" : "",
3899 given,
3900 kwonly_sig,
3901 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003902 Py_DECREF(sig);
3903 Py_DECREF(kwonly_sig);
3904}
3905
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003906static int
Victor Stinner438a12d2019-05-24 17:01:38 +02003907positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
3908 Py_ssize_t kwcount, PyObject* const* kwnames)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003909{
3910 int posonly_conflicts = 0;
3911 PyObject* posonly_names = PyList_New(0);
3912
3913 for(int k=0; k < co->co_posonlyargcount; k++){
3914 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
3915
3916 for (int k2=0; k2<kwcount; k2++){
3917 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
3918 PyObject* kwname = kwnames[k2];
3919 if (kwname == posonly_name){
3920 if(PyList_Append(posonly_names, kwname) != 0) {
3921 goto fail;
3922 }
3923 posonly_conflicts++;
3924 continue;
3925 }
3926
3927 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
3928
3929 if ( cmp > 0) {
3930 if(PyList_Append(posonly_names, kwname) != 0) {
3931 goto fail;
3932 }
3933 posonly_conflicts++;
3934 } else if (cmp < 0) {
3935 goto fail;
3936 }
3937
3938 }
3939 }
3940 if (posonly_conflicts) {
3941 PyObject* comma = PyUnicode_FromString(", ");
3942 if (comma == NULL) {
3943 goto fail;
3944 }
3945 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
3946 Py_DECREF(comma);
3947 if (error_names == NULL) {
3948 goto fail;
3949 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003950 _PyErr_Format(tstate, PyExc_TypeError,
3951 "%U() got some positional-only arguments passed"
3952 " as keyword arguments: '%U'",
3953 co->co_name, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003954 Py_DECREF(error_names);
3955 goto fail;
3956 }
3957
3958 Py_DECREF(posonly_names);
3959 return 0;
3960
3961fail:
3962 Py_XDECREF(posonly_names);
3963 return 1;
3964
3965}
3966
Guido van Rossumc2e20742006-02-27 22:32:47 +00003967/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02003968 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003969 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003970
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003971PyObject *
Victor Stinnerb5e170f2019-11-16 01:03:22 +01003972_PyEval_EvalCode(PyThreadState *tstate,
3973 PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003974 PyObject *const *args, Py_ssize_t argcount,
3975 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003976 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003977 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003978 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003979 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003980{
Victor Stinnerb5e170f2019-11-16 01:03:22 +01003981 assert(tstate != NULL);
3982
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003983 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003984 PyFrameObject *f;
3985 PyObject *retval = NULL;
3986 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003987 PyObject *x, *u;
Pablo Galindocd74e662019-06-01 18:08:04 +01003988 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003989 Py_ssize_t i, j, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003990 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003991
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003992 if (globals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003993 _PyErr_SetString(tstate, PyExc_SystemError,
3994 "PyEval_EvalCodeEx: NULL globals");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003995 return NULL;
3996 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003997
Victor Stinnerc7020012016-08-16 23:40:29 +02003998 /* Create the frame */
INADA Naoki5a625d02016-12-24 20:19:08 +09003999 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02004000 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004001 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02004002 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004003 fastlocals = f->f_localsplus;
4004 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00004005
Victor Stinnerc7020012016-08-16 23:40:29 +02004006 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004007 if (co->co_flags & CO_VARKEYWORDS) {
4008 kwdict = PyDict_New();
4009 if (kwdict == NULL)
4010 goto fail;
4011 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02004012 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004013 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02004014 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004015 SETLOCAL(i, kwdict);
4016 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004017 else {
4018 kwdict = NULL;
4019 }
4020
Pablo Galindocd74e662019-06-01 18:08:04 +01004021 /* Copy all positional arguments into local variables */
4022 if (argcount > co->co_argcount) {
4023 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02004024 }
4025 else {
4026 n = argcount;
4027 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004028 for (j = 0; j < n; j++) {
4029 x = args[j];
4030 Py_INCREF(x);
4031 SETLOCAL(j, x);
4032 }
4033
Victor Stinnerc7020012016-08-16 23:40:29 +02004034 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004035 if (co->co_flags & CO_VARARGS) {
Sergey Fedoseev234531b2019-02-25 21:59:12 +05004036 u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02004037 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004038 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004039 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004040 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004041 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004042
Serhiy Storchakab7281052016-09-12 00:52:40 +03004043 /* Handle keyword arguments passed as two strided arrays */
4044 kwcount *= kwstep;
4045 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004046 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03004047 PyObject *keyword = kwnames[i];
4048 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02004049 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004050
Benjamin Petersonb204a422011-06-05 22:04:07 -05004051 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004052 _PyErr_Format(tstate, PyExc_TypeError,
4053 "%U() keywords must be strings",
4054 co->co_name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004055 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004056 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004057
Benjamin Petersonb204a422011-06-05 22:04:07 -05004058 /* Speed hack: do raw pointer compares. As names are
4059 normally interned this should almost always hit. */
4060 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004061 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004062 PyObject *name = co_varnames[j];
4063 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004064 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004065 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004066 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004067
Benjamin Petersonb204a422011-06-05 22:04:07 -05004068 /* Slow fallback, just in case */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004069 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004070 PyObject *name = co_varnames[j];
4071 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
4072 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004073 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004074 }
4075 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004076 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004077 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004078 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004079
Victor Stinner231d1f32017-01-11 02:12:06 +01004080 assert(j >= total_args);
4081 if (kwdict == NULL) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004082
Victor Stinner438a12d2019-05-24 17:01:38 +02004083 if (co->co_posonlyargcount
4084 && positional_only_passed_as_keyword(tstate, co,
4085 kwcount, kwnames))
4086 {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004087 goto fail;
4088 }
4089
Victor Stinner438a12d2019-05-24 17:01:38 +02004090 _PyErr_Format(tstate, PyExc_TypeError,
4091 "%U() got an unexpected keyword argument '%S'",
4092 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004093 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004094 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004095
Christian Heimes0bd447f2013-07-20 14:48:10 +02004096 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4097 goto fail;
4098 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004099 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004100
Benjamin Petersonb204a422011-06-05 22:04:07 -05004101 kw_found:
4102 if (GETLOCAL(j) != NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004103 _PyErr_Format(tstate, PyExc_TypeError,
4104 "%U() got multiple values for argument '%S'",
4105 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004106 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004107 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004108 Py_INCREF(value);
4109 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004110 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004111
4112 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004113 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004114 too_many_positional(tstate, co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004115 goto fail;
4116 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004117
4118 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004119 if (argcount < co->co_argcount) {
4120 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004121 Py_ssize_t missing = 0;
4122 for (i = argcount; i < m; i++) {
4123 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004124 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004125 }
4126 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004127 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004128 missing_arguments(tstate, co, missing, defcount, fastlocals);
Benjamin Petersone109c702011-06-24 09:37:26 -05004129 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004130 }
4131 if (n > m)
4132 i = n - m;
4133 else
4134 i = 0;
4135 for (; i < defcount; i++) {
4136 if (GETLOCAL(m+i) == NULL) {
4137 PyObject *def = defs[i];
4138 Py_INCREF(def);
4139 SETLOCAL(m+i, def);
4140 }
4141 }
4142 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004143
4144 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004145 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004146 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004147 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004148 PyObject *name;
4149 if (GETLOCAL(i) != NULL)
4150 continue;
4151 name = PyTuple_GET_ITEM(co->co_varnames, i);
4152 if (kwdefs != NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004153 PyObject *def = PyDict_GetItemWithError(kwdefs, name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004154 if (def) {
4155 Py_INCREF(def);
4156 SETLOCAL(i, def);
4157 continue;
4158 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004159 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004160 goto fail;
4161 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004162 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004163 missing++;
4164 }
4165 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004166 missing_arguments(tstate, co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004167 goto fail;
4168 }
4169 }
4170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004171 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004172 vars into frame. */
4173 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004174 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004175 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004176 /* Possibly account for the cell variable being an argument. */
4177 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004178 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004179 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004180 /* Clear the local copy. */
4181 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004182 }
4183 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004184 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004185 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004186 if (c == NULL)
4187 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004188 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004189 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004190
4191 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004192 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4193 PyObject *o = PyTuple_GET_ITEM(closure, i);
4194 Py_INCREF(o);
4195 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004196 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004197
Yury Selivanoveb636452016-09-08 22:01:51 -07004198 /* Handle generator/coroutine/asynchronous generator */
4199 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004200 PyObject *gen;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004201 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004202
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004203 /* Don't need to keep the reference to f_back, it will be set
4204 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004205 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004207 /* Create a new generator that owns the ready to run frame
4208 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004209 if (is_coro) {
4210 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004211 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4212 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004213 } else {
4214 gen = PyGen_NewWithQualName(f, name, qualname);
4215 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004216 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004217 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004218 }
INADA Naoki9c157762016-12-26 18:52:46 +09004219
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004220 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004221
Yury Selivanov75445082015-05-11 22:57:16 -04004222 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004223 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004224
Victor Stinnerb9e68122019-11-14 12:20:46 +01004225 retval = _PyEval_EvalFrame(tstate, f, 0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004226
Thomas Woutersce272b62007-09-19 21:19:28 +00004227fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004229 /* decref'ing the frame can cause __del__ methods to get invoked,
4230 which can call back into Python. While we're done with the
4231 current Python frame (f), the associated C stack is still in use,
4232 so recursion_depth must be boosted for the duration.
4233 */
INADA Naoki5a625d02016-12-24 20:19:08 +09004234 if (Py_REFCNT(f) > 1) {
4235 Py_DECREF(f);
4236 _PyObject_GC_TRACK(f);
4237 }
4238 else {
4239 ++tstate->recursion_depth;
4240 Py_DECREF(f);
4241 --tstate->recursion_depth;
4242 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004243 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004244}
4245
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004246
4247PyObject *
4248_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
4249 PyObject *const *args, Py_ssize_t argcount,
4250 PyObject *const *kwnames, PyObject *const *kwargs,
4251 Py_ssize_t kwcount, int kwstep,
4252 PyObject *const *defs, Py_ssize_t defcount,
4253 PyObject *kwdefs, PyObject *closure,
4254 PyObject *name, PyObject *qualname)
4255{
4256 PyThreadState *tstate = _PyThreadState_GET();
4257 return _PyEval_EvalCode(tstate, _co, globals, locals,
4258 args, argcount,
4259 kwnames, kwargs,
4260 kwcount, kwstep,
4261 defs, defcount,
4262 kwdefs, closure,
4263 name, qualname);
4264}
4265
Victor Stinner40ee3012014-06-16 15:59:28 +02004266PyObject *
4267PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004268 PyObject *const *args, int argcount,
4269 PyObject *const *kws, int kwcount,
4270 PyObject *const *defs, int defcount,
4271 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004272{
4273 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004274 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004275 kws, kws != NULL ? kws + 1 : NULL,
4276 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004277 defs, defcount,
4278 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004279 NULL, NULL);
4280}
Tim Peters5ca576e2001-06-18 22:08:13 +00004281
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004282static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004283special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004285 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004286 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02004287 if (res == NULL && !_PyErr_Occurred(tstate)) {
4288 _PyErr_SetObject(tstate, PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004289 return NULL;
4290 }
4291 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004292}
4293
4294
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004295/* Logic for the raise statement (too complicated for inlining).
4296 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004297static int
Victor Stinner09532fe2019-05-10 23:39:09 +02004298do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004299{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004300 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004302 if (exc == NULL) {
4303 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01004304 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004305 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004306 type = exc_info->exc_type;
4307 value = exc_info->exc_value;
4308 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004309 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004310 _PyErr_SetString(tstate, PyExc_RuntimeError,
4311 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004312 return 0;
4313 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004314 Py_XINCREF(type);
4315 Py_XINCREF(value);
4316 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02004317 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004318 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004319 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004321 /* We support the following forms of raise:
4322 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004323 raise <instance>
4324 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004326 if (PyExceptionClass_Check(exc)) {
4327 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004328 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004329 if (value == NULL)
4330 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004331 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004332 _PyErr_Format(tstate, PyExc_TypeError,
4333 "calling %R should have returned an instance of "
4334 "BaseException, not %R",
4335 type, Py_TYPE(value));
4336 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004337 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004338 }
4339 else if (PyExceptionInstance_Check(exc)) {
4340 value = exc;
4341 type = PyExceptionInstance_Class(exc);
4342 Py_INCREF(type);
4343 }
4344 else {
4345 /* Not something you can raise. You get an exception
4346 anyway, just not what you specified :-) */
4347 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02004348 _PyErr_SetString(tstate, PyExc_TypeError,
4349 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004350 goto raise_error;
4351 }
Collin Winter828f04a2007-08-31 00:04:24 +00004352
Serhiy Storchakac0191582016-09-27 11:37:10 +03004353 assert(type != NULL);
4354 assert(value != NULL);
4355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004356 if (cause) {
4357 PyObject *fixed_cause;
4358 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004359 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004360 if (fixed_cause == NULL)
4361 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004362 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004363 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004364 else if (PyExceptionInstance_Check(cause)) {
4365 fixed_cause = cause;
4366 }
4367 else if (cause == Py_None) {
4368 Py_DECREF(cause);
4369 fixed_cause = NULL;
4370 }
4371 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004372 _PyErr_SetString(tstate, PyExc_TypeError,
4373 "exception causes must derive from "
4374 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004375 goto raise_error;
4376 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004377 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004378 }
Collin Winter828f04a2007-08-31 00:04:24 +00004379
Victor Stinner438a12d2019-05-24 17:01:38 +02004380 _PyErr_SetObject(tstate, type, value);
Victor Stinner61f4db82020-01-28 03:37:45 +01004381 /* _PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004382 Py_DECREF(value);
4383 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004384 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004385
4386raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004387 Py_XDECREF(value);
4388 Py_XDECREF(type);
4389 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004390 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004391}
4392
Tim Petersd6d010b2001-06-21 02:49:55 +00004393/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004394 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004395
Guido van Rossum0368b722007-05-11 16:50:42 +00004396 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4397 with a variable target.
4398*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004399
Barry Warsawe42b18f1997-08-25 22:13:04 +00004400static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004401unpack_iterable(PyThreadState *tstate, PyObject *v,
4402 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004403{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004404 int i = 0, j = 0;
4405 Py_ssize_t ll = 0;
4406 PyObject *it; /* iter(v) */
4407 PyObject *w;
4408 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004410 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004412 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004413 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004414 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01004415 Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v))
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004416 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004417 _PyErr_Format(tstate, PyExc_TypeError,
4418 "cannot unpack non-iterable %.200s object",
Victor Stinnera102ed72020-02-07 02:24:48 +01004419 Py_TYPE(v)->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004420 }
4421 return 0;
4422 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004424 for (; i < argcnt; i++) {
4425 w = PyIter_Next(it);
4426 if (w == NULL) {
4427 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004428 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04004429 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004430 _PyErr_Format(tstate, PyExc_ValueError,
4431 "not enough values to unpack "
4432 "(expected %d, got %d)",
4433 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004434 }
4435 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004436 _PyErr_Format(tstate, PyExc_ValueError,
4437 "not enough values to unpack "
4438 "(expected at least %d, got %d)",
4439 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004440 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004441 }
4442 goto Error;
4443 }
4444 *--sp = w;
4445 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004447 if (argcntafter == -1) {
4448 /* We better have exhausted the iterator now. */
4449 w = PyIter_Next(it);
4450 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004451 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004452 goto Error;
4453 Py_DECREF(it);
4454 return 1;
4455 }
4456 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02004457 _PyErr_Format(tstate, PyExc_ValueError,
4458 "too many values to unpack (expected %d)",
4459 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004460 goto Error;
4461 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004463 l = PySequence_List(it);
4464 if (l == NULL)
4465 goto Error;
4466 *--sp = l;
4467 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004469 ll = PyList_GET_SIZE(l);
4470 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004471 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04004472 "not enough values to unpack (expected at least %d, got %zd)",
4473 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004474 goto Error;
4475 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004477 /* Pop the "after-variable" args off the list. */
4478 for (j = argcntafter; j > 0; j--, i++) {
4479 *--sp = PyList_GET_ITEM(l, ll - j);
4480 }
4481 /* Resize the list. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +01004482 Py_SET_SIZE(l, ll - argcntafter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004483 Py_DECREF(it);
4484 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004485
Tim Petersd6d010b2001-06-21 02:49:55 +00004486Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004487 for (; i > 0; i--, sp++)
4488 Py_DECREF(*sp);
4489 Py_XDECREF(it);
4490 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004491}
4492
4493
Guido van Rossum96a42c81992-01-12 02:29:51 +00004494#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004495static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004496prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004497{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004498 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02004499 if (PyObject_Print(v, stdout, 0) != 0) {
4500 /* Don't know what else to do */
4501 _PyErr_Clear(tstate);
4502 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004503 printf("\n");
4504 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004505}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004506#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004507
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004508static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004509call_exc_trace(Py_tracefunc func, PyObject *self,
4510 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004511{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004512 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004513 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004514 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004515 if (value == NULL) {
4516 value = Py_None;
4517 Py_INCREF(value);
4518 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004519 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01004520 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004521 arg = PyTuple_Pack(3, type, value, traceback);
4522 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004523 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004524 return;
4525 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004526 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004527 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02004528 if (err == 0) {
4529 _PyErr_Restore(tstate, type, value, orig_traceback);
4530 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004531 else {
4532 Py_XDECREF(type);
4533 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004534 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004535 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004536}
4537
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004538static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004539call_trace_protected(Py_tracefunc func, PyObject *obj,
4540 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004541 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004542{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004543 PyObject *type, *value, *traceback;
4544 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004545 _PyErr_Fetch(tstate, &type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004546 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004547 if (err == 0)
4548 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004549 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004550 return 0;
4551 }
4552 else {
4553 Py_XDECREF(type);
4554 Py_XDECREF(value);
4555 Py_XDECREF(traceback);
4556 return -1;
4557 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004558}
4559
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004560static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004561call_trace(Py_tracefunc func, PyObject *obj,
4562 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004563 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004564{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004565 int result;
4566 if (tstate->tracing)
4567 return 0;
4568 tstate->tracing++;
4569 tstate->use_tracing = 0;
4570 result = func(obj, frame, what, arg);
4571 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4572 || (tstate->c_profilefunc != NULL));
4573 tstate->tracing--;
4574 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004575}
4576
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004577PyObject *
4578_PyEval_CallTracing(PyObject *func, PyObject *args)
4579{
Victor Stinner50b48572018-11-01 01:51:40 +01004580 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004581 int save_tracing = tstate->tracing;
4582 int save_use_tracing = tstate->use_tracing;
4583 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004585 tstate->tracing = 0;
4586 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4587 || (tstate->c_profilefunc != NULL));
4588 result = PyObject_Call(func, args, NULL);
4589 tstate->tracing = save_tracing;
4590 tstate->use_tracing = save_use_tracing;
4591 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004592}
4593
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004594/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004595static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004596maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004597 PyThreadState *tstate, PyFrameObject *frame,
4598 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004599{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004600 int result = 0;
4601 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004603 /* If the last instruction executed isn't in the current
4604 instruction window, reset the window.
4605 */
4606 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4607 PyAddrPair bounds;
4608 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4609 &bounds);
4610 *instr_lb = bounds.ap_lower;
4611 *instr_ub = bounds.ap_upper;
4612 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004613 /* If the last instruction falls at the start of a line or if it
4614 represents a jump backwards, update the frame's line number and
4615 then call the trace function if we're tracing source lines.
4616 */
4617 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004618 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004619 if (frame->f_trace_lines) {
4620 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4621 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004622 }
George King20faa682017-10-18 17:44:22 -07004623 /* Always emit an opcode event if we're tracing all opcodes. */
4624 if (frame->f_trace_opcodes) {
4625 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4626 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004627 *instr_prev = frame->f_lasti;
4628 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004629}
4630
Fred Drake5755ce62001-06-27 19:19:46 +00004631void
4632PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004633{
Steve Dowerb82e17e2019-05-23 08:45:22 -07004634 if (PySys_Audit("sys.setprofile", NULL) < 0) {
4635 return;
4636 }
4637
Victor Stinner50b48572018-11-01 01:51:40 +01004638 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004639 PyObject *temp = tstate->c_profileobj;
4640 Py_XINCREF(arg);
4641 tstate->c_profilefunc = NULL;
4642 tstate->c_profileobj = NULL;
4643 /* Must make sure that tracing is not ignored if 'temp' is freed */
4644 tstate->use_tracing = tstate->c_tracefunc != NULL;
4645 Py_XDECREF(temp);
4646 tstate->c_profilefunc = func;
4647 tstate->c_profileobj = arg;
4648 /* Flag that tracing or profiling is turned on */
4649 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004650}
4651
4652void
4653PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4654{
Steve Dowerb82e17e2019-05-23 08:45:22 -07004655 if (PySys_Audit("sys.settrace", NULL) < 0) {
4656 return;
4657 }
4658
Victor Stinner09532fe2019-05-10 23:39:09 +02004659 _PyRuntimeState *runtime = &_PyRuntime;
4660 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004661 PyObject *temp = tstate->c_traceobj;
Victor Stinner09532fe2019-05-10 23:39:09 +02004662 runtime->ceval.tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004663 Py_XINCREF(arg);
4664 tstate->c_tracefunc = NULL;
4665 tstate->c_traceobj = NULL;
4666 /* Must make sure that profiling is not ignored if 'temp' is freed */
4667 tstate->use_tracing = tstate->c_profilefunc != NULL;
4668 Py_XDECREF(temp);
4669 tstate->c_tracefunc = func;
4670 tstate->c_traceobj = arg;
4671 /* Flag that tracing or profiling is turned on */
4672 tstate->use_tracing = ((func != NULL)
4673 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004674}
4675
Yury Selivanov75445082015-05-11 22:57:16 -04004676void
Victor Stinner838f2642019-06-13 22:41:23 +02004677_PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004678{
4679 assert(new_depth >= 0);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004680 tstate->coroutine_origin_tracking_depth = new_depth;
4681}
4682
4683int
4684_PyEval_GetCoroutineOriginTrackingDepth(void)
4685{
Victor Stinner50b48572018-11-01 01:51:40 +01004686 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004687 return tstate->coroutine_origin_tracking_depth;
4688}
4689
4690void
Yury Selivanoveb636452016-09-08 22:01:51 -07004691_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4692{
Victor Stinner50b48572018-11-01 01:51:40 +01004693 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004694
4695 if (PySys_Audit("sys.set_asyncgen_hook_firstiter", NULL) < 0) {
4696 return;
4697 }
4698
Yury Selivanoveb636452016-09-08 22:01:51 -07004699 Py_XINCREF(firstiter);
4700 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4701}
4702
4703PyObject *
4704_PyEval_GetAsyncGenFirstiter(void)
4705{
Victor Stinner50b48572018-11-01 01:51:40 +01004706 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004707 return tstate->async_gen_firstiter;
4708}
4709
4710void
4711_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4712{
Victor Stinner50b48572018-11-01 01:51:40 +01004713 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004714
4715 if (PySys_Audit("sys.set_asyncgen_hook_finalizer", NULL) < 0) {
4716 return;
4717 }
4718
Yury Selivanoveb636452016-09-08 22:01:51 -07004719 Py_XINCREF(finalizer);
4720 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4721}
4722
4723PyObject *
4724_PyEval_GetAsyncGenFinalizer(void)
4725{
Victor Stinner50b48572018-11-01 01:51:40 +01004726 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004727 return tstate->async_gen_finalizer;
4728}
4729
Victor Stinner438a12d2019-05-24 17:01:38 +02004730static PyFrameObject *
4731_PyEval_GetFrame(PyThreadState *tstate)
4732{
Victor Stinner01b1cc12019-11-20 02:27:56 +01004733 _PyRuntimeState *runtime = tstate->interp->runtime;
4734 return runtime->gilstate.getframe(tstate);
Victor Stinner438a12d2019-05-24 17:01:38 +02004735}
4736
4737PyFrameObject *
4738PyEval_GetFrame(void)
4739{
4740 PyThreadState *tstate = _PyThreadState_GET();
4741 return _PyEval_GetFrame(tstate);
4742}
4743
Guido van Rossumb209a111997-04-29 18:18:01 +00004744PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004745PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004746{
Victor Stinner438a12d2019-05-24 17:01:38 +02004747 PyThreadState *tstate = _PyThreadState_GET();
4748 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004749 if (current_frame == NULL)
Victor Stinner438a12d2019-05-24 17:01:38 +02004750 return tstate->interp->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004751 else
4752 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004753}
4754
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004755/* Convenience function to get a builtin from its name */
4756PyObject *
4757_PyEval_GetBuiltinId(_Py_Identifier *name)
4758{
Victor Stinner438a12d2019-05-24 17:01:38 +02004759 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004760 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4761 if (attr) {
4762 Py_INCREF(attr);
4763 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004764 else if (!_PyErr_Occurred(tstate)) {
4765 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004766 }
4767 return attr;
4768}
4769
Guido van Rossumb209a111997-04-29 18:18:01 +00004770PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004771PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004772{
Victor Stinner438a12d2019-05-24 17:01:38 +02004773 PyThreadState *tstate = _PyThreadState_GET();
4774 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Victor Stinner41bb43a2013-10-29 01:19:37 +01004775 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004776 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004777 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004778 }
4779
Victor Stinner438a12d2019-05-24 17:01:38 +02004780 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01004781 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004782 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004783
4784 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004785 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004786}
4787
Guido van Rossumb209a111997-04-29 18:18:01 +00004788PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004789PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004790{
Victor Stinner438a12d2019-05-24 17:01:38 +02004791 PyThreadState *tstate = _PyThreadState_GET();
4792 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
4793 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004794 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004795 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004796
4797 assert(current_frame->f_globals != NULL);
4798 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004799}
4800
Guido van Rossum6135a871995-01-09 17:53:26 +00004801int
Tim Peters5ba58662001-07-16 02:29:45 +00004802PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004803{
Victor Stinner438a12d2019-05-24 17:01:38 +02004804 PyThreadState *tstate = _PyThreadState_GET();
4805 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004806 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004808 if (current_frame != NULL) {
4809 const int codeflags = current_frame->f_code->co_flags;
4810 const int compilerflags = codeflags & PyCF_MASK;
4811 if (compilerflags) {
4812 result = 1;
4813 cf->cf_flags |= compilerflags;
4814 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004815#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004816 if (codeflags & CO_GENERATOR_ALLOWED) {
4817 result = 1;
4818 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4819 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004820#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004821 }
4822 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004823}
4824
Guido van Rossum3f5da241990-12-20 15:06:42 +00004825
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004826const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004827PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004828{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004829 if (PyMethod_Check(func))
4830 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4831 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004832 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004833 else if (PyCFunction_Check(func))
4834 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4835 else
Victor Stinnera102ed72020-02-07 02:24:48 +01004836 return Py_TYPE(func)->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004837}
4838
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004839const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004840PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004841{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004842 if (PyMethod_Check(func))
4843 return "()";
4844 else if (PyFunction_Check(func))
4845 return "()";
4846 else if (PyCFunction_Check(func))
4847 return "()";
4848 else
4849 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004850}
4851
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004852#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004853if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004854 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4855 tstate, tstate->frame, \
4856 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004857 x = NULL; \
4858 } \
4859 else { \
4860 x = call; \
4861 if (tstate->c_profilefunc != NULL) { \
4862 if (x == NULL) { \
4863 call_trace_protected(tstate->c_profilefunc, \
4864 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004865 tstate, tstate->frame, \
4866 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004867 /* XXX should pass (type, value, tb) */ \
4868 } else { \
4869 if (call_trace(tstate->c_profilefunc, \
4870 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004871 tstate, tstate->frame, \
4872 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004873 Py_DECREF(x); \
4874 x = NULL; \
4875 } \
4876 } \
4877 } \
4878 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004879} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004880 x = call; \
4881 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004882
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004883
4884static PyObject *
4885trace_call_function(PyThreadState *tstate,
4886 PyObject *func,
4887 PyObject **args, Py_ssize_t nargs,
4888 PyObject *kwnames)
4889{
4890 PyObject *x;
4891 if (PyCFunction_Check(func)) {
Petr Viktorinffd97532020-02-11 17:46:57 +01004892 C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004893 return x;
4894 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06004895 else if (Py_IS_TYPE(func, &PyMethodDescr_Type) && nargs > 0) {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004896 /* We need to create a temporary bound method as argument
4897 for profiling.
4898
4899 If nargs == 0, then this cannot work because we have no
4900 "self". In any case, the call itself would raise
4901 TypeError (foo needs an argument), so we just skip
4902 profiling. */
4903 PyObject *self = args[0];
4904 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4905 if (func == NULL) {
4906 return NULL;
4907 }
Petr Viktorinffd97532020-02-11 17:46:57 +01004908 C_TRACE(x, PyObject_Vectorcall(func,
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02004909 args+1, nargs-1,
4910 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004911 Py_DECREF(func);
4912 return x;
4913 }
Petr Viktorinffd97532020-02-11 17:46:57 +01004914 return PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004915}
4916
Victor Stinner415c5102017-01-11 00:54:57 +01004917/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4918 to reduce the stack consumption. */
4919Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Victor Stinner09532fe2019-05-10 23:39:09 +02004920call_function(PyThreadState *tstate, PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004921{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004922 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004923 PyObject *func = *pfunc;
4924 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004925 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4926 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004927 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004928
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004929 if (tstate->use_tracing) {
4930 x = trace_call_function(tstate, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09004931 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004932 else {
Petr Viktorinffd97532020-02-11 17:46:57 +01004933 x = PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004934 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004935
Victor Stinner438a12d2019-05-24 17:01:38 +02004936 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004937
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004938 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004939 while ((*pp_stack) > pfunc) {
4940 w = EXT_POP(*pp_stack);
4941 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004942 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004944 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004945}
4946
Jeremy Hylton52820442001-01-03 23:52:36 +00004947static PyObject *
Victor Stinner09532fe2019-05-10 23:39:09 +02004948do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004949{
jdemeyere89de732018-09-19 12:06:20 +02004950 PyObject *result;
4951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004952 if (PyCFunction_Check(func)) {
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +02004953 C_TRACE(result, PyObject_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004954 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004955 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06004956 else if (Py_IS_TYPE(func, &PyMethodDescr_Type)) {
jdemeyere89de732018-09-19 12:06:20 +02004957 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
4958 if (nargs > 0 && tstate->use_tracing) {
4959 /* We need to create a temporary bound method as argument
4960 for profiling.
4961
4962 If nargs == 0, then this cannot work because we have no
4963 "self". In any case, the call itself would raise
4964 TypeError (foo needs an argument), so we just skip
4965 profiling. */
4966 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
4967 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4968 if (func == NULL) {
4969 return NULL;
4970 }
4971
Victor Stinner4d231bc2019-11-14 13:36:21 +01004972 C_TRACE(result, _PyObject_FastCallDictTstate(
4973 tstate, func,
4974 &_PyTuple_ITEMS(callargs)[1],
4975 nargs - 1,
4976 kwdict));
jdemeyere89de732018-09-19 12:06:20 +02004977 Py_DECREF(func);
4978 return result;
4979 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004980 }
jdemeyere89de732018-09-19 12:06:20 +02004981 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00004982}
4983
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004984/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004985 nb_index slot defined, and store in *pi.
4986 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004987 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004988 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004989*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004990int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004991_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004992{
Victor Stinner438a12d2019-05-24 17:01:38 +02004993 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004994 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004995 Py_ssize_t x;
4996 if (PyIndex_Check(v)) {
4997 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02004998 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004999 return 0;
5000 }
5001 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005002 _PyErr_SetString(tstate, PyExc_TypeError,
5003 "slice indices must be integers or "
5004 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005005 return 0;
5006 }
5007 *pi = x;
5008 }
5009 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005010}
5011
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005012int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005013_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005014{
Victor Stinner438a12d2019-05-24 17:01:38 +02005015 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005016 Py_ssize_t x;
5017 if (PyIndex_Check(v)) {
5018 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005019 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005020 return 0;
5021 }
5022 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005023 _PyErr_SetString(tstate, PyExc_TypeError,
5024 "slice indices must be integers or "
5025 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005026 return 0;
5027 }
5028 *pi = x;
5029 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005030}
5031
Thomas Wouters52152252000-08-17 22:55:00 +00005032static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005033import_name(PyThreadState *tstate, PyFrameObject *f,
5034 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005035{
5036 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005037 PyObject *import_func, *res;
5038 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005039
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005040 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005041 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005042 if (!_PyErr_Occurred(tstate)) {
5043 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005044 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005045 return NULL;
5046 }
5047
5048 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005049 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005050 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02005051 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005052 return NULL;
5053 }
5054 res = PyImport_ImportModuleLevelObject(
5055 name,
5056 f->f_globals,
5057 f->f_locals == NULL ? Py_None : f->f_locals,
5058 fromlist,
5059 ilevel);
5060 return res;
5061 }
5062
5063 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005064
5065 stack[0] = name;
5066 stack[1] = f->f_globals;
5067 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5068 stack[3] = fromlist;
5069 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005070 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005071 Py_DECREF(import_func);
5072 return res;
5073}
5074
5075static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005076import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005077{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005078 PyObject *x;
Xiang Zhang4830f582017-03-21 11:13:42 +08005079 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005080
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005081 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005082 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005083 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005084 /* Issue #17636: in case this failed because of a circular relative
5085 import, try to fallback on reading the module directly from
5086 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02005087 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005088 if (pkgname == NULL) {
5089 goto error;
5090 }
Oren Milman6db70332017-09-19 14:23:01 +03005091 if (!PyUnicode_Check(pkgname)) {
5092 Py_CLEAR(pkgname);
5093 goto error;
5094 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005095 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005096 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005097 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005098 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005099 }
Eric Snow3f9eee62017-09-15 16:35:20 -06005100 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005101 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02005102 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07005103 goto error;
5104 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005105 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005106 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005107 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005108 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005109 if (pkgname == NULL) {
5110 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5111 if (pkgname_or_unknown == NULL) {
5112 Py_XDECREF(pkgpath);
5113 return NULL;
5114 }
5115 } else {
5116 pkgname_or_unknown = pkgname;
5117 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005118
5119 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005120 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08005121 errmsg = PyUnicode_FromFormat(
5122 "cannot import name %R from %R (unknown location)",
5123 name, pkgname_or_unknown
5124 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005125 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005126 PyErr_SetImportError(errmsg, pkgname, NULL);
5127 }
5128 else {
Anthony Sottile65366bc2019-09-09 08:17:50 -07005129 _Py_IDENTIFIER(__spec__);
5130 PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
Anthony Sottile65366bc2019-09-09 08:17:50 -07005131 const char *fmt =
5132 _PyModuleSpec_IsInitializing(spec) ?
5133 "cannot import name %R from partially initialized module %R "
5134 "(most likely due to a circular import) (%S)" :
5135 "cannot import name %R from %R (%S)";
5136 Py_XDECREF(spec);
5137
5138 errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
Stefan Krah027b09c2019-03-25 21:50:58 +01005139 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005140 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005141 }
5142
Xiang Zhang4830f582017-03-21 11:13:42 +08005143 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005144 Py_XDECREF(pkgname_or_unknown);
5145 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005146 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005147}
Guido van Rossumac7be682001-01-17 15:42:30 +00005148
Thomas Wouters52152252000-08-17 22:55:00 +00005149static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005150import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00005151{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005152 _Py_IDENTIFIER(__all__);
5153 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005154 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005155 int skip_leading_underscores = 0;
5156 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005157
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005158 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5159 return -1; /* Unexpected error */
5160 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005161 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005162 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5163 return -1;
5164 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005165 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005166 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005167 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005168 return -1;
5169 }
5170 all = PyMapping_Keys(dict);
5171 Py_DECREF(dict);
5172 if (all == NULL)
5173 return -1;
5174 skip_leading_underscores = 1;
5175 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005177 for (pos = 0, err = 0; ; pos++) {
5178 name = PySequence_GetItem(all, pos);
5179 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005180 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005181 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02005182 }
5183 else {
5184 _PyErr_Clear(tstate);
5185 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005186 break;
5187 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005188 if (!PyUnicode_Check(name)) {
5189 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5190 if (modname == NULL) {
5191 Py_DECREF(name);
5192 err = -1;
5193 break;
5194 }
5195 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005196 _PyErr_Format(tstate, PyExc_TypeError,
5197 "module __name__ must be a string, not %.100s",
5198 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005199 }
5200 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005201 _PyErr_Format(tstate, PyExc_TypeError,
5202 "%s in %U.%s must be str, not %.100s",
5203 skip_leading_underscores ? "Key" : "Item",
5204 modname,
5205 skip_leading_underscores ? "__dict__" : "__all__",
5206 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005207 }
5208 Py_DECREF(modname);
5209 Py_DECREF(name);
5210 err = -1;
5211 break;
5212 }
5213 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005214 if (PyUnicode_READY(name) == -1) {
5215 Py_DECREF(name);
5216 err = -1;
5217 break;
5218 }
5219 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5220 Py_DECREF(name);
5221 continue;
5222 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005223 }
5224 value = PyObject_GetAttr(v, name);
5225 if (value == NULL)
5226 err = -1;
5227 else if (PyDict_CheckExact(locals))
5228 err = PyDict_SetItem(locals, name, value);
5229 else
5230 err = PyObject_SetItem(locals, name, value);
5231 Py_DECREF(name);
5232 Py_XDECREF(value);
5233 if (err != 0)
5234 break;
5235 }
5236 Py_DECREF(all);
5237 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005238}
5239
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005240static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005241check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005242{
Victor Stinnera102ed72020-02-07 02:24:48 +01005243 if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005244 /* check_args_iterable() may be called with a live exception:
5245 * clear it to prevent calling _PyObject_FunctionStr() with an
5246 * exception set. */
Victor Stinner61f4db82020-01-28 03:37:45 +01005247 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005248 PyObject *funcstr = _PyObject_FunctionStr(func);
5249 if (funcstr != NULL) {
5250 _PyErr_Format(tstate, PyExc_TypeError,
5251 "%U argument after * must be an iterable, not %.200s",
5252 funcstr, Py_TYPE(args)->tp_name);
5253 Py_DECREF(funcstr);
5254 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005255 return -1;
5256 }
5257 return 0;
5258}
5259
5260static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005261format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005262{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005263 /* _PyDict_MergeEx raises attribute
5264 * error (percolated from an attempt
5265 * to get 'keys' attribute) instead of
5266 * a type error if its second argument
5267 * is not a mapping.
5268 */
Victor Stinner438a12d2019-05-24 17:01:38 +02005269 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005270 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005271 PyObject *funcstr = _PyObject_FunctionStr(func);
5272 if (funcstr != NULL) {
5273 _PyErr_Format(
5274 tstate, PyExc_TypeError,
5275 "%U argument after ** must be a mapping, not %.200s",
5276 funcstr, Py_TYPE(kwargs)->tp_name);
5277 Py_DECREF(funcstr);
5278 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005279 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005280 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005281 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02005282 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005283 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005284 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005285 PyObject *funcstr = _PyObject_FunctionStr(func);
5286 if (funcstr != NULL) {
5287 PyObject *key = PyTuple_GET_ITEM(val, 0);
5288 _PyErr_Format(
5289 tstate, PyExc_TypeError,
5290 "%U got multiple values for keyword argument '%S'",
5291 funcstr, key);
5292 Py_DECREF(funcstr);
5293 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005294 Py_XDECREF(exc);
5295 Py_XDECREF(val);
5296 Py_XDECREF(tb);
5297 }
5298 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005299 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005300 }
5301 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005302}
5303
Guido van Rossumac7be682001-01-17 15:42:30 +00005304static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005305format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
5306 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005307{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005308 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005310 if (!obj)
5311 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005312
Serhiy Storchaka06515832016-11-20 09:13:07 +02005313 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005314 if (!obj_str)
5315 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005316
Victor Stinner438a12d2019-05-24 17:01:38 +02005317 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005318}
Guido van Rossum950361c1997-01-24 13:49:28 +00005319
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005320static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005321format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005322{
5323 PyObject *name;
5324 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02005325 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005326 return;
5327 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5328 name = PyTuple_GET_ITEM(co->co_cellvars,
5329 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005330 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005331 PyExc_UnboundLocalError,
5332 UNBOUNDLOCAL_ERROR_MSG,
5333 name);
5334 } else {
5335 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5336 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02005337 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005338 UNBOUNDFREE_ERROR_MSG, name);
5339 }
5340}
5341
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005342static void
Mark Shannonfee55262019-11-21 09:11:43 +00005343format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevprevopcode, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005344{
5345 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5346 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005347 _PyErr_Format(tstate, PyExc_TypeError,
5348 "'async with' received an object from __aenter__ "
5349 "that does not implement __await__: %.100s",
5350 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005351 }
Mark Shannonfee55262019-11-21 09:11:43 +00005352 else if (prevopcode == WITH_EXCEPT_START || (prevopcode == CALL_FUNCTION && prevprevopcode == DUP_TOP)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005353 _PyErr_Format(tstate, PyExc_TypeError,
5354 "'async with' received an object from __aexit__ "
5355 "that does not implement __await__: %.100s",
5356 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005357 }
5358 }
5359}
5360
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005361static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005362unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005363 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005364{
5365 PyObject *res;
5366 if (Py_REFCNT(v) == 2) {
5367 /* In the common case, there are 2 references to the value
5368 * stored in 'variable' when the += is performed: one on the
5369 * value stack (in 'v') and one still stored in the
5370 * 'variable'. We try to delete the variable now to reduce
5371 * the refcnt to 1.
5372 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005373 int opcode, oparg;
5374 NEXTOPARG();
5375 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005376 case STORE_FAST:
5377 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005378 PyObject **fastlocals = f->f_localsplus;
5379 if (GETLOCAL(oparg) == v)
5380 SETLOCAL(oparg, NULL);
5381 break;
5382 }
5383 case STORE_DEREF:
5384 {
5385 PyObject **freevars = (f->f_localsplus +
5386 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005387 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005388 if (PyCell_GET(c) == v) {
5389 PyCell_SET(c, NULL);
5390 Py_DECREF(v);
5391 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005392 break;
5393 }
5394 case STORE_NAME:
5395 {
5396 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005397 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005398 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005399 if (locals && PyDict_CheckExact(locals)) {
5400 PyObject *w = PyDict_GetItemWithError(locals, name);
5401 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02005402 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005403 {
5404 Py_DECREF(v);
5405 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005406 }
5407 }
5408 break;
5409 }
5410 }
5411 }
5412 res = v;
5413 PyUnicode_Append(&res, w);
5414 return res;
5415}
5416
Guido van Rossum950361c1997-01-24 13:49:28 +00005417#ifdef DYNAMIC_EXECUTION_PROFILE
5418
Skip Montanarof118cb12001-10-15 20:51:38 +00005419static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005420getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005421{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005422 int i;
5423 PyObject *l = PyList_New(256);
5424 if (l == NULL) return NULL;
5425 for (i = 0; i < 256; i++) {
5426 PyObject *x = PyLong_FromLong(a[i]);
5427 if (x == NULL) {
5428 Py_DECREF(l);
5429 return NULL;
5430 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005431 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005432 }
5433 for (i = 0; i < 256; i++)
5434 a[i] = 0;
5435 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005436}
5437
5438PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005439_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005440{
5441#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005442 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005443#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005444 int i;
5445 PyObject *l = PyList_New(257);
5446 if (l == NULL) return NULL;
5447 for (i = 0; i < 257; i++) {
5448 PyObject *x = getarray(dxpairs[i]);
5449 if (x == NULL) {
5450 Py_DECREF(l);
5451 return NULL;
5452 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005453 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005454 }
5455 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005456#endif
5457}
5458
5459#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005460
5461Py_ssize_t
5462_PyEval_RequestCodeExtraIndex(freefunc free)
5463{
Victor Stinnercaba55b2018-08-03 15:33:52 +02005464 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -07005465 Py_ssize_t new_index;
5466
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005467 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005468 return -1;
5469 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005470 new_index = interp->co_extra_user_count++;
5471 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005472 return new_index;
5473}
Łukasz Langaa785c872016-09-09 17:37:37 -07005474
5475static void
5476dtrace_function_entry(PyFrameObject *f)
5477{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005478 const char *filename;
5479 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005480 int lineno;
5481
5482 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5483 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5484 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5485
Andy Lestere6be9b52020-02-11 20:28:35 -06005486 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005487}
5488
5489static void
5490dtrace_function_return(PyFrameObject *f)
5491{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005492 const char *filename;
5493 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005494 int lineno;
5495
5496 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5497 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5498 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5499
Andy Lestere6be9b52020-02-11 20:28:35 -06005500 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005501}
5502
5503/* DTrace equivalent of maybe_call_line_trace. */
5504static void
5505maybe_dtrace_line(PyFrameObject *frame,
5506 int *instr_lb, int *instr_ub, int *instr_prev)
5507{
5508 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005509 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005510
5511 /* If the last instruction executed isn't in the current
5512 instruction window, reset the window.
5513 */
5514 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5515 PyAddrPair bounds;
5516 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5517 &bounds);
5518 *instr_lb = bounds.ap_lower;
5519 *instr_ub = bounds.ap_upper;
5520 }
5521 /* If the last instruction falls at the start of a line or if
5522 it represents a jump backwards, update the frame's line
5523 number and call the trace function. */
5524 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5525 frame->f_lineno = line;
5526 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5527 if (!co_filename)
5528 co_filename = "?";
5529 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5530 if (!co_name)
5531 co_name = "?";
Andy Lestere6be9b52020-02-11 20:28:35 -06005532 PyDTrace_LINE(co_filename, co_name, line);
Łukasz Langaa785c872016-09-09 17:37:37 -07005533 }
5534 *instr_prev = frame->f_lasti;
5535}
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005536
5537
5538/* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions
5539 for the limited API. */
5540
5541#undef Py_EnterRecursiveCall
5542
5543int Py_EnterRecursiveCall(const char *where)
5544{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005545 return _Py_EnterRecursiveCall_inline(where);
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005546}
5547
5548#undef Py_LeaveRecursiveCall
5549
5550void Py_LeaveRecursiveCall(void)
5551{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005552 _Py_LeaveRecursiveCall_inline();
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005553}