blob: 72a830468faa6f8cdb1a44450797b28328571b56 [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 Stinner111e4ee2020-03-09 21:24:14 +010016#include "pycore_initconfig.h"
Victor Stinnerbcda8f12018-11-21 22:27:47 +010017#include "pycore_object.h"
Victor Stinner438a12d2019-05-24 17:01:38 +020018#include "pycore_pyerrors.h"
19#include "pycore_pylifecycle.h"
Victor Stinner621cebe2018-11-12 16:53:38 +010020#include "pycore_pystate.h"
Victor Stinnerec13b932018-11-25 23:56:17 +010021#include "pycore_tupleobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000022
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000023#include "code.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040024#include "dictobject.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000025#include "frameobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000026#include "opcode.h"
Łukasz Langaa785c872016-09-09 17:37:37 -070027#include "pydtrace.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040028#include "setobject.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000029#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000030
Guido van Rossumc6004111993-11-05 10:22:19 +000031#include <ctype.h>
32
Guido van Rossum408027e1996-12-30 16:17:54 +000033#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000034/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000035#define LLTRACE 1 /* Low-level trace feature */
36#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000037#endif
38
Victor Stinner5c75f372019-04-17 23:02:26 +020039#if !defined(Py_BUILD_CORE)
40# error "ceval.c must be build with Py_BUILD_CORE define for best performance"
41#endif
42
Hai Shi46874c22020-01-30 17:20:25 -060043_Py_IDENTIFIER(__name__);
Guido van Rossum5b722181993-03-30 17:46:03 +000044
Guido van Rossum374a9221991-04-04 10:40:29 +000045/* Forward declarations */
Victor Stinner09532fe2019-05-10 23:39:09 +020046Py_LOCAL_INLINE(PyObject *) call_function(
47 PyThreadState *tstate, PyObject ***pp_stack,
48 Py_ssize_t oparg, PyObject *kwnames);
49static PyObject * do_call_core(
50 PyThreadState *tstate, PyObject *func,
51 PyObject *callargs, PyObject *kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +000052
Guido van Rossum0a066c01992-03-27 17:29:15 +000053#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000054static int lltrace;
Victor Stinner438a12d2019-05-24 17:01:38 +020055static int prtrace(PyThreadState *, PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000056#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010057static int call_trace(Py_tracefunc, PyObject *,
58 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000060static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010061 PyThreadState *, PyFrameObject *,
62 int, PyObject *);
63static void call_exc_trace(Py_tracefunc, PyObject *,
64 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000065static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Eric Snow2ebc5ce2017-09-07 23:51:28 -060066 PyThreadState *, PyFrameObject *,
67 int *, int *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070068static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *);
69static void dtrace_function_entry(PyFrameObject *);
70static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000071
Victor Stinner438a12d2019-05-24 17:01:38 +020072static PyObject * import_name(PyThreadState *, PyFrameObject *,
73 PyObject *, PyObject *, PyObject *);
74static PyObject * import_from(PyThreadState *, PyObject *, PyObject *);
75static int import_all_from(PyThreadState *, PyObject *, PyObject *);
76static void format_exc_check_arg(PyThreadState *, PyObject *, const char *, PyObject *);
77static void format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg);
78static PyObject * unicode_concatenate(PyThreadState *, PyObject *, PyObject *,
Serhiy Storchakaab874002016-09-11 13:48:15 +030079 PyFrameObject *, const _Py_CODEUNIT *);
Victor Stinner438a12d2019-05-24 17:01:38 +020080static PyObject * special_lookup(PyThreadState *, PyObject *, _Py_Identifier *);
81static int check_args_iterable(PyThreadState *, PyObject *func, PyObject *vararg);
82static void format_kwargs_error(PyThreadState *, PyObject *func, PyObject *kwargs);
Mark Shannonfee55262019-11-21 09:11:43 +000083static void format_awaitable_error(PyThreadState *, PyTypeObject *, int, int);
Guido van Rossum374a9221991-04-04 10:40:29 +000084
Paul Prescode68140d2000-08-30 20:25:01 +000085#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000087#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000089#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 "free variable '%.200s' referenced before assignment" \
91 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000092
Guido van Rossum950361c1997-01-24 13:49:28 +000093/* Dynamic execution profile */
94#ifdef DYNAMIC_EXECUTION_PROFILE
95#ifdef DXPAIRS
96static long dxpairs[257][256];
97#define dxp dxpairs[256]
98#else
99static long dxp[256];
100#endif
101#endif
102
Inada Naoki91234a12019-06-03 21:30:58 +0900103/* per opcode cache */
Inada Naokieddef862019-06-04 07:38:10 +0900104#ifdef Py_DEBUG
105// --with-pydebug is used to find memory leak. opcache makes it harder.
106// So we disable opcache when Py_DEBUG is defined.
107// See bpo-37146
108#define OPCACHE_MIN_RUNS 0 /* disable opcache */
109#else
Inada Naoki91234a12019-06-03 21:30:58 +0900110#define OPCACHE_MIN_RUNS 1024 /* create opcache when code executed this time */
Inada Naokieddef862019-06-04 07:38:10 +0900111#endif
Inada Naoki91234a12019-06-03 21:30:58 +0900112#define OPCACHE_STATS 0 /* Enable stats */
113
114#if OPCACHE_STATS
115static size_t opcache_code_objects = 0;
116static size_t opcache_code_objects_extra_mem = 0;
117
118static size_t opcache_global_opts = 0;
119static size_t opcache_global_hits = 0;
120static size_t opcache_global_misses = 0;
121#endif
122
Victor Stinnere225beb2019-06-03 18:14:24 +0200123#define GIL_REQUEST _Py_atomic_load_relaxed(&ceval->gil_drop_request)
Inada Naoki91234a12019-06-03 21:30:58 +0900124
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000125/* This can set eval_breaker to 0 even though gil_drop_request became
126 1. We believe this is all right because the eval loop will release
127 the GIL eventually anyway. */
Victor Stinnere225beb2019-06-03 18:14:24 +0200128#define COMPUTE_EVAL_BREAKER(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 _Py_atomic_store_relaxed( \
Victor Stinnere225beb2019-06-03 18:14:24 +0200130 &(ceval)->eval_breaker, \
131 GIL_REQUEST | \
132 _Py_atomic_load_relaxed(&(ceval)->signals_pending) | \
133 _Py_atomic_load_relaxed(&(ceval)->pending.calls_to_do) | \
134 (ceval)->pending.async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000135
Victor Stinnere225beb2019-06-03 18:14:24 +0200136#define SET_GIL_DROP_REQUEST(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200138 _Py_atomic_store_relaxed(&(ceval)->gil_drop_request, 1); \
139 _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000141
Victor Stinnere225beb2019-06-03 18:14:24 +0200142#define RESET_GIL_DROP_REQUEST(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000143 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200144 _Py_atomic_store_relaxed(&(ceval)->gil_drop_request, 0); \
145 COMPUTE_EVAL_BREAKER(ceval); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000147
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000148/* Pending calls are only modified under pending_lock */
Victor Stinnere225beb2019-06-03 18:14:24 +0200149#define SIGNAL_PENDING_CALLS(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200151 _Py_atomic_store_relaxed(&(ceval)->pending.calls_to_do, 1); \
152 _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000154
Victor Stinnere225beb2019-06-03 18:14:24 +0200155#define UNSIGNAL_PENDING_CALLS(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200157 _Py_atomic_store_relaxed(&(ceval)->pending.calls_to_do, 0); \
158 COMPUTE_EVAL_BREAKER(ceval); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000159 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000160
Victor Stinnere225beb2019-06-03 18:14:24 +0200161#define SIGNAL_PENDING_SIGNALS(ceval) \
Eric Snowfdf282d2019-01-11 14:26:55 -0700162 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200163 _Py_atomic_store_relaxed(&(ceval)->signals_pending, 1); \
164 _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \
Eric Snowfdf282d2019-01-11 14:26:55 -0700165 } while (0)
166
Victor Stinnere225beb2019-06-03 18:14:24 +0200167#define UNSIGNAL_PENDING_SIGNALS(ceval) \
Eric Snowfdf282d2019-01-11 14:26:55 -0700168 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200169 _Py_atomic_store_relaxed(&(ceval)->signals_pending, 0); \
170 COMPUTE_EVAL_BREAKER(ceval); \
Eric Snowfdf282d2019-01-11 14:26:55 -0700171 } while (0)
172
Victor Stinnere225beb2019-06-03 18:14:24 +0200173#define SIGNAL_ASYNC_EXC(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200175 (ceval)->pending.async_exc = 1; \
176 _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000178
Victor Stinnere225beb2019-06-03 18:14:24 +0200179#define UNSIGNAL_ASYNC_EXC(ceval) \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600180 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200181 (ceval)->pending.async_exc = 0; \
182 COMPUTE_EVAL_BREAKER(ceval); \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600183 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000184
185
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000186#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000187#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000188#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000189#include "pythread.h"
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000190#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000191
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100192static void
193ensure_tstate_not_null(const char *func, PyThreadState *tstate)
194{
195 if (tstate == NULL) {
196 _Py_FatalErrorFunc(func, "current thread state is NULL");
197 }
198}
199
200
Tim Peters7f468f22004-10-11 02:40:51 +0000201int
202PyEval_ThreadsInitialized(void)
203{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100204 _PyRuntimeState *runtime = &_PyRuntime;
205 return gil_created(&runtime->ceval.gil);
Tim Peters7f468f22004-10-11 02:40:51 +0000206}
207
Victor Stinner111e4ee2020-03-09 21:24:14 +0100208PyStatus
209_PyEval_InitThreads(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000210{
Victor Stinner111e4ee2020-03-09 21:24:14 +0100211 if (tstate == NULL) {
212 return _PyStatus_ERR("tstate is NULL");
213 }
214
215 struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
Victor Stinnere225beb2019-06-03 18:14:24 +0200216 struct _gil_runtime_state *gil = &ceval->gil;
Victor Stinner09532fe2019-05-10 23:39:09 +0200217 if (gil_created(gil)) {
Victor Stinner111e4ee2020-03-09 21:24:14 +0100218 return _PyStatus_OK();
Victor Stinnera7126792019-03-19 14:19:38 +0100219 }
220
Inada Naoki001fee12019-02-20 10:00:09 +0900221 PyThread_init_thread();
Victor Stinner09532fe2019-05-10 23:39:09 +0200222 create_gil(gil);
Victor Stinner85f5a692020-03-09 22:12:04 +0100223
224 take_gil(tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700225
Victor Stinnere225beb2019-06-03 18:14:24 +0200226 struct _pending_calls *pending = &ceval->pending;
227 pending->lock = PyThread_allocate_lock();
228 if (pending->lock == NULL) {
Victor Stinner111e4ee2020-03-09 21:24:14 +0100229 return _PyStatus_NO_MEMORY();
230 }
Victor Stinner85f5a692020-03-09 22:12:04 +0100231
Victor Stinner111e4ee2020-03-09 21:24:14 +0100232 return _PyStatus_OK();
233}
234
235void
236PyEval_InitThreads(void)
237{
238 PyThreadState *tstate = _PyThreadState_GET();
239
240 PyStatus status = _PyEval_InitThreads(tstate);
241 if (_PyStatus_EXCEPTION(status)) {
242 Py_ExitStatusException(status);
Victor Stinnere225beb2019-06-03 18:14:24 +0200243 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000244}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000245
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000246void
Victor Stinnere225beb2019-06-03 18:14:24 +0200247_PyEval_FiniThreads(struct _ceval_runtime_state *ceval)
Antoine Pitrou1df15362010-09-13 14:16:46 +0000248{
Victor Stinnere225beb2019-06-03 18:14:24 +0200249 struct _gil_runtime_state *gil = &ceval->gil;
Victor Stinner09532fe2019-05-10 23:39:09 +0200250 if (!gil_created(gil)) {
Antoine Pitrou1df15362010-09-13 14:16:46 +0000251 return;
Victor Stinnera7126792019-03-19 14:19:38 +0100252 }
253
Victor Stinner09532fe2019-05-10 23:39:09 +0200254 destroy_gil(gil);
255 assert(!gil_created(gil));
Victor Stinner99fcc612019-04-29 13:04:07 +0200256
Victor Stinnere225beb2019-06-03 18:14:24 +0200257 struct _pending_calls *pending = &ceval->pending;
258 if (pending->lock != NULL) {
259 PyThread_free_lock(pending->lock);
260 pending->lock = NULL;
261 }
Antoine Pitrou1df15362010-09-13 14:16:46 +0000262}
263
264void
Inada Naoki91234a12019-06-03 21:30:58 +0900265_PyEval_Fini(void)
266{
267#if OPCACHE_STATS
268 fprintf(stderr, "-- Opcode cache number of objects = %zd\n",
269 opcache_code_objects);
270
271 fprintf(stderr, "-- Opcode cache total extra mem = %zd\n",
272 opcache_code_objects_extra_mem);
273
274 fprintf(stderr, "\n");
275
276 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL hits = %zd (%d%%)\n",
277 opcache_global_hits,
278 (int) (100.0 * opcache_global_hits /
279 (opcache_global_hits + opcache_global_misses)));
280
281 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL misses = %zd (%d%%)\n",
282 opcache_global_misses,
283 (int) (100.0 * opcache_global_misses /
284 (opcache_global_hits + opcache_global_misses)));
285
286 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL opts = %zd\n",
287 opcache_global_opts);
288
289 fprintf(stderr, "\n");
290#endif
291}
292
293void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000294PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000295{
Victor Stinner09532fe2019-05-10 23:39:09 +0200296 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200297 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100298 ensure_tstate_not_null(__func__, tstate);
299
Victor Stinner85f5a692020-03-09 22:12:04 +0100300 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000301}
302
303void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000304PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000305{
Victor Stinner09532fe2019-05-10 23:39:09 +0200306 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200307 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100309 We therefore avoid PyThreadState_Get() which dumps a fatal error
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 in debug mode.
311 */
Victor Stinnere225beb2019-06-03 18:14:24 +0200312 drop_gil(&runtime->ceval, tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000313}
314
315void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000316PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000317{
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100318 ensure_tstate_not_null(__func__, tstate);
319
Victor Stinner85f5a692020-03-09 22:12:04 +0100320 take_gil(tstate);
Victor Stinnere225beb2019-06-03 18:14:24 +0200321
Victor Stinner85f5a692020-03-09 22:12:04 +0100322 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
323 if (_PyThreadState_Swap(gilstate, tstate) != NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100324 Py_FatalError("non-NULL old thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200325 }
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000326}
327
328void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000329PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000330{
Victor Stinner17c68b82020-01-30 12:20:48 +0100331 assert(tstate != NULL);
Victor Stinner09532fe2019-05-10 23:39:09 +0200332
Victor Stinner01b1cc12019-11-20 02:27:56 +0100333 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200334 PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
335 if (new_tstate != tstate) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100336 Py_FatalError("wrong thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200337 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200338 drop_gil(&runtime->ceval, tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000339}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000340
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200341/* This function is called from PyOS_AfterFork_Child to destroy all threads
342 * which are not running in the child process, and clear internal locks
343 * which might be held by those threads.
344 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000345
346void
Victor Stinnerd5d9e812019-05-13 12:35:37 +0200347_PyEval_ReInitThreads(_PyRuntimeState *runtime)
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000348{
Victor Stinnere225beb2019-06-03 18:14:24 +0200349 struct _ceval_runtime_state *ceval = &runtime->ceval;
350 if (!gil_created(&ceval->gil)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 return;
Victor Stinner09532fe2019-05-10 23:39:09 +0200352 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200353 recreate_gil(&ceval->gil);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100354 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
355 ensure_tstate_not_null(__func__, tstate);
Victor Stinner85f5a692020-03-09 22:12:04 +0100356
357 take_gil(tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700358
Victor Stinnere225beb2019-06-03 18:14:24 +0200359 struct _pending_calls *pending = &ceval->pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200360 pending->lock = PyThread_allocate_lock();
361 if (pending->lock == NULL) {
Eric Snow8479a342019-03-08 23:44:33 -0700362 Py_FatalError("Can't initialize threads for pending calls");
363 }
Jesse Nollera8513972008-07-17 16:49:17 +0000364
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200365 /* Destroy all threads except the current one */
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100366 _PyThreadState_DeleteExcept(runtime, tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000367}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000368
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000369/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600370 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000371
372void
Victor Stinnere225beb2019-06-03 18:14:24 +0200373_PyEval_SignalAsyncExc(struct _ceval_runtime_state *ceval)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000374{
Victor Stinnere225beb2019-06-03 18:14:24 +0200375 SIGNAL_ASYNC_EXC(ceval);
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000376}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000377
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000378PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000379PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000380{
Victor Stinner09532fe2019-05-10 23:39:09 +0200381 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200382 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinner09532fe2019-05-10 23:39:09 +0200383 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
384 if (tstate == NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100385 Py_FatalError("NULL tstate");
Victor Stinner09532fe2019-05-10 23:39:09 +0200386 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200387 assert(gil_created(&ceval->gil));
388 drop_gil(ceval, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000390}
391
392void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000393PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000394{
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100395 ensure_tstate_not_null(__func__, tstate);
396
Victor Stinner85f5a692020-03-09 22:12:04 +0100397 take_gil(tstate);
Victor Stinner17c68b82020-01-30 12:20:48 +0100398
Victor Stinner85f5a692020-03-09 22:12:04 +0100399 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
400 _PyThreadState_Swap(gilstate, tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000401}
402
403
Guido van Rossuma9672091994-09-14 13:31:22 +0000404/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
405 signal handlers or Mac I/O completion routines) can schedule calls
406 to a function to be called synchronously.
407 The synchronous function is called with one void* argument.
408 It should return 0 for success or -1 for failure -- failure should
409 be accompanied by an exception.
410
411 If registry succeeds, the registry function returns 0; if it fails
412 (e.g. due to too many pending calls) it returns -1 (without setting
413 an exception condition).
414
415 Note that because registry may occur from within signal handlers,
416 or other asynchronous events, calling malloc() is unsafe!
417
Guido van Rossuma9672091994-09-14 13:31:22 +0000418 Any thread can schedule pending calls, but only the main thread
419 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000420 There is no facility to schedule calls to a particular thread, but
421 that should be easy to change, should that ever be required. In
422 that case, the static variables here should go into the python
423 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000424*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000425
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200426void
Victor Stinnere225beb2019-06-03 18:14:24 +0200427_PyEval_SignalReceived(struct _ceval_runtime_state *ceval)
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200428{
429 /* bpo-30703: Function called when the C signal handler of Python gets a
430 signal. We cannot queue a callback using Py_AddPendingCall() since
431 that function is not async-signal-safe. */
Victor Stinnere225beb2019-06-03 18:14:24 +0200432 SIGNAL_PENDING_SIGNALS(ceval);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200433}
434
Eric Snow5be45a62019-03-08 22:47:07 -0700435/* Push one item onto the queue while holding the lock. */
436static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200437_push_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600438 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700439{
Eric Snow842a2f02019-03-15 15:47:51 -0600440 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700441 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600442 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700443 return -1; /* Queue full */
444 }
Eric Snow842a2f02019-03-15 15:47:51 -0600445 pending->calls[i].func = func;
446 pending->calls[i].arg = arg;
447 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700448 return 0;
449}
450
451/* Pop one item off the queue while holding the lock. */
452static void
Victor Stinnere225beb2019-06-03 18:14:24 +0200453_pop_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600454 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700455{
Eric Snow842a2f02019-03-15 15:47:51 -0600456 int i = pending->first;
457 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700458 return; /* Queue empty */
459 }
460
Eric Snow842a2f02019-03-15 15:47:51 -0600461 *func = pending->calls[i].func;
462 *arg = pending->calls[i].arg;
463 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700464}
465
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200466/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000467 scheduling to be made from any thread, and even from an executing
468 callback.
469 */
470
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000471int
Victor Stinner438a12d2019-05-24 17:01:38 +0200472_PyEval_AddPendingCall(PyThreadState *tstate,
Victor Stinnere225beb2019-06-03 18:14:24 +0200473 struct _ceval_runtime_state *ceval,
Victor Stinner09532fe2019-05-10 23:39:09 +0200474 int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000475{
Victor Stinnere225beb2019-06-03 18:14:24 +0200476 struct _pending_calls *pending = &ceval->pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600477
478 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
479 if (pending->finishing) {
480 PyThread_release_lock(pending->lock);
481
482 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +0200483 _PyErr_Fetch(tstate, &exc, &val, &tb);
484 _PyErr_SetString(tstate, PyExc_SystemError,
Eric Snow842a2f02019-03-15 15:47:51 -0600485 "Py_AddPendingCall: cannot add pending calls "
486 "(Python shutting down)");
Victor Stinner438a12d2019-05-24 17:01:38 +0200487 _PyErr_Print(tstate);
488 _PyErr_Restore(tstate, exc, val, tb);
Eric Snow842a2f02019-03-15 15:47:51 -0600489 return -1;
490 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200491 int result = _push_pending_call(pending, func, arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600492 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700493
Victor Stinnere225beb2019-06-03 18:14:24 +0200494 /* signal main loop */
495 SIGNAL_PENDING_CALLS(ceval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000497}
498
Victor Stinner09532fe2019-05-10 23:39:09 +0200499int
500Py_AddPendingCall(int (*func)(void *), void *arg)
501{
Victor Stinner438a12d2019-05-24 17:01:38 +0200502 _PyRuntimeState *runtime = &_PyRuntime;
503 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinnere225beb2019-06-03 18:14:24 +0200504 return _PyEval_AddPendingCall(tstate, &runtime->ceval, func, arg);
Victor Stinner09532fe2019-05-10 23:39:09 +0200505}
506
Eric Snowfdf282d2019-01-11 14:26:55 -0700507static int
Victor Stinner09532fe2019-05-10 23:39:09 +0200508handle_signals(_PyRuntimeState *runtime)
Eric Snowfdf282d2019-01-11 14:26:55 -0700509{
Victor Stinner3225b9f2020-03-09 20:56:57 +0100510 /* Only handle signals on main thread */
Victor Stinner09532fe2019-05-10 23:39:09 +0200511 if (PyThread_get_thread_ident() != runtime->main_thread) {
Eric Snowfdf282d2019-01-11 14:26:55 -0700512 return 0;
513 }
Eric Snow64d6cc82019-02-23 15:40:43 -0700514 /*
515 * Ensure that the thread isn't currently running some other
516 * interpreter.
517 */
Victor Stinner09532fe2019-05-10 23:39:09 +0200518 PyInterpreterState *interp = _PyRuntimeState_GetThreadState(runtime)->interp;
519 if (interp != runtime->interpreters.main) {
Eric Snow64d6cc82019-02-23 15:40:43 -0700520 return 0;
521 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700522
Victor Stinnere225beb2019-06-03 18:14:24 +0200523 struct _ceval_runtime_state *ceval = &runtime->ceval;
524 UNSIGNAL_PENDING_SIGNALS(ceval);
Eric Snow64d6cc82019-02-23 15:40:43 -0700525 if (_PyErr_CheckSignals() < 0) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200526 SIGNAL_PENDING_SIGNALS(ceval); /* We're not done yet */
Eric Snowfdf282d2019-01-11 14:26:55 -0700527 return -1;
528 }
529 return 0;
530}
531
532static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200533make_pending_calls(_PyRuntimeState *runtime)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000534{
Eric Snow6a150bc2019-06-01 15:39:46 -0600535 static int busy = 0;
Eric Snowb75b1a352019-04-12 10:20:10 -0600536
Victor Stinnere225beb2019-06-03 18:14:24 +0200537 /* only service pending calls on main thread */
538 if (PyThread_get_thread_ident() != runtime->main_thread) {
539 return 0;
540 }
541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 /* don't perform recursive pending calls */
Eric Snowfdf282d2019-01-11 14:26:55 -0700543 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700545 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200546 busy = 1;
Victor Stinnere225beb2019-06-03 18:14:24 +0200547 struct _ceval_runtime_state *ceval = &runtime->ceval;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200548 /* unsignal before starting to call callbacks, so that any callback
549 added in-between re-signals */
Victor Stinnere225beb2019-06-03 18:14:24 +0200550 UNSIGNAL_PENDING_CALLS(ceval);
Eric Snowfdf282d2019-01-11 14:26:55 -0700551 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 /* perform a bounded number of calls, in case of recursion */
Victor Stinnere225beb2019-06-03 18:14:24 +0200554 struct _pending_calls *pending = &ceval->pending;
Eric Snowfdf282d2019-01-11 14:26:55 -0700555 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700556 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 void *arg = NULL;
558
559 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600560 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200561 _pop_pending_call(pending, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600562 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700563
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100564 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700565 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100566 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700567 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700568 res = func(arg);
569 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200570 goto error;
571 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200573
Charles-François Natalif23339a2011-07-23 18:15:43 +0200574 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700575 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200576
577error:
578 busy = 0;
Victor Stinnere225beb2019-06-03 18:14:24 +0200579 SIGNAL_PENDING_CALLS(ceval);
Eric Snowfdf282d2019-01-11 14:26:55 -0700580 return res;
581}
582
Eric Snow842a2f02019-03-15 15:47:51 -0600583void
Victor Stinner2b1df452020-01-13 18:46:59 +0100584_Py_FinishPendingCalls(PyThreadState *tstate)
Eric Snow842a2f02019-03-15 15:47:51 -0600585{
Eric Snow842a2f02019-03-15 15:47:51 -0600586 assert(PyGILState_Check());
587
Victor Stinner2b1df452020-01-13 18:46:59 +0100588 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200589 struct _pending_calls *pending = &runtime->ceval.pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200590
Eric Snow842a2f02019-03-15 15:47:51 -0600591 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
592 pending->finishing = 1;
593 PyThread_release_lock(pending->lock);
594
595 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
596 return;
597 }
598
Victor Stinnere225beb2019-06-03 18:14:24 +0200599 if (make_pending_calls(runtime) < 0) {
600 PyObject *exc, *val, *tb;
601 _PyErr_Fetch(tstate, &exc, &val, &tb);
602 PyErr_BadInternalCall();
603 _PyErr_ChainExceptions(exc, val, tb);
604 _PyErr_Print(tstate);
Eric Snow842a2f02019-03-15 15:47:51 -0600605 }
606}
607
Eric Snowfdf282d2019-01-11 14:26:55 -0700608/* Py_MakePendingCalls() is a simple wrapper for the sake
609 of backward-compatibility. */
610int
611Py_MakePendingCalls(void)
612{
613 assert(PyGILState_Check());
614
615 /* Python signal handler doesn't really queue a callback: it only signals
616 that a signal was received, see _PyEval_SignalReceived(). */
Victor Stinner09532fe2019-05-10 23:39:09 +0200617 _PyRuntimeState *runtime = &_PyRuntime;
618 int res = handle_signals(runtime);
Eric Snowfdf282d2019-01-11 14:26:55 -0700619 if (res != 0) {
620 return res;
621 }
622
Victor Stinnere225beb2019-06-03 18:14:24 +0200623 res = make_pending_calls(runtime);
Eric Snowb75b1a352019-04-12 10:20:10 -0600624 if (res != 0) {
625 return res;
626 }
627
628 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000629}
630
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000631/* The interpreter's recursion limit */
632
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000633#ifndef Py_DEFAULT_RECURSION_LIMIT
634#define Py_DEFAULT_RECURSION_LIMIT 1000
635#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600636
Eric Snow05351c12017-09-05 21:43:08 -0700637int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000638
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600639void
Victor Stinnere225beb2019-06-03 18:14:24 +0200640_PyEval_Initialize(struct _ceval_runtime_state *state)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600641{
Victor Stinnere225beb2019-06-03 18:14:24 +0200642 state->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600643 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Victor Stinnere225beb2019-06-03 18:14:24 +0200644 _gil_initialize(&state->gil);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600645}
646
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000647int
648Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000649{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100650 struct _ceval_runtime_state *ceval = &_PyRuntime.ceval;
651 return ceval->recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000652}
653
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000654void
655Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000656{
Victor Stinnere225beb2019-06-03 18:14:24 +0200657 struct _ceval_runtime_state *ceval = &_PyRuntime.ceval;
658 ceval->recursion_limit = new_limit;
659 _Py_CheckRecursionLimit = ceval->recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000660}
661
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100662/* The function _Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
Armin Rigo2b3eb402003-10-28 12:05:48 +0000663 if the recursion_depth reaches _Py_CheckRecursionLimit.
664 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
665 to guarantee that _Py_CheckRecursiveCall() is regularly called.
666 Without USE_STACKCHECK, there is no need for this. */
667int
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100668_Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000669{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100670 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200671 int recursion_limit = runtime->ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000672
673#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700674 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 if (PyOS_CheckStack()) {
676 --tstate->recursion_depth;
Victor Stinner438a12d2019-05-24 17:01:38 +0200677 _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 return -1;
679 }
pdox18967932017-10-25 23:03:01 -0700680 /* Needed for ABI backwards-compatibility (see bpo-31857) */
Eric Snow05351c12017-09-05 21:43:08 -0700681 _Py_CheckRecursionLimit = recursion_limit;
pdox18967932017-10-25 23:03:01 -0700682#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 if (tstate->recursion_critical)
684 /* Somebody asked that we don't check for recursion. */
685 return 0;
686 if (tstate->overflowed) {
687 if (tstate->recursion_depth > recursion_limit + 50) {
688 /* Overflowing while handling an overflow. Give up. */
689 Py_FatalError("Cannot recover from stack overflow.");
690 }
691 return 0;
692 }
693 if (tstate->recursion_depth > recursion_limit) {
694 --tstate->recursion_depth;
695 tstate->overflowed = 1;
Victor Stinner438a12d2019-05-24 17:01:38 +0200696 _PyErr_Format(tstate, PyExc_RecursionError,
697 "maximum recursion depth exceeded%s",
698 where);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 return -1;
700 }
701 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000702}
703
Victor Stinner09532fe2019-05-10 23:39:09 +0200704static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
Victor Stinner438a12d2019-05-24 17:01:38 +0200705static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000706
Victor Stinnere225beb2019-06-03 18:14:24 +0200707#define _Py_TracingPossible(ceval) ((ceval)->tracing_possible)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000708
Guido van Rossum374a9221991-04-04 10:40:29 +0000709
Guido van Rossumb209a111997-04-29 18:18:01 +0000710PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000711PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000712{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 return PyEval_EvalCodeEx(co,
714 globals, locals,
715 (PyObject **)NULL, 0,
716 (PyObject **)NULL, 0,
717 (PyObject **)NULL, 0,
718 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000719}
720
721
722/* Interpreter main loop */
723
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000724PyObject *
Victor Stinnerb9e68122019-11-14 12:20:46 +0100725PyEval_EvalFrame(PyFrameObject *f)
726{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 /* This is for backward compatibility with extension modules that
728 used this API; core interpreter code should call
729 PyEval_EvalFrameEx() */
Victor Stinnerb9e68122019-11-14 12:20:46 +0100730 PyThreadState *tstate = _PyThreadState_GET();
731 return _PyEval_EvalFrame(tstate, f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000732}
733
734PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000735PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000736{
Victor Stinnerb9e68122019-11-14 12:20:46 +0100737 PyThreadState *tstate = _PyThreadState_GET();
738 return _PyEval_EvalFrame(tstate, f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700739}
740
Victor Stinnerc6944e72016-11-11 02:13:35 +0100741PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700742_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
743{
Guido van Rossum950361c1997-01-24 13:49:28 +0000744#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000746#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200747 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300748 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200749 int opcode; /* Current opcode */
750 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200751 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 PyObject *retval = NULL; /* Return value */
Victor Stinner09532fe2019-05-10 23:39:09 +0200753 _PyRuntimeState * const runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200754 struct _ceval_runtime_state * const ceval = &runtime->ceval;
755 _Py_atomic_int * const eval_breaker = &ceval->eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000757
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100758 PyThreadState * const tstate = _PyRuntimeState_GetThreadState(runtime);
759 ensure_tstate_not_null(__func__, tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100760
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 is true when the line being executed has changed. The
766 initial values are such as to make this false the first
767 time it is tested. */
768 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000769
Serhiy Storchakaab874002016-09-11 13:48:15 +0300770 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 PyObject *names;
772 PyObject *consts;
Inada Naoki91234a12019-06-03 21:30:58 +0900773 _PyOpcache *co_opcache;
Guido van Rossum374a9221991-04-04 10:40:29 +0000774
Brett Cannon368b4b72012-04-02 12:17:59 -0400775#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200776 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400777#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200778
Antoine Pitroub52ec782009-01-25 16:34:23 +0000779/* Computed GOTOs, or
780 the-optimization-commonly-but-improperly-known-as-"threaded code"
781 using gcc's labels-as-values extension
782 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
783
784 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000786 combined with a lookup table of jump addresses. However, since the
787 indirect jump instruction is shared by all opcodes, the CPU will have a
788 hard time making the right prediction for where to jump next (actually,
789 it will be always wrong except in the uncommon case of a sequence of
790 several identical opcodes).
791
792 "Threaded code" in contrast, uses an explicit jump table and an explicit
793 indirect jump instruction at the end of each opcode. Since the jump
794 instruction is at a different address for each opcode, the CPU will make a
795 separate prediction for each of these instructions, which is equivalent to
796 predicting the second opcode of each opcode pair. These predictions have
797 a much better chance to turn out valid, especially in small bytecode loops.
798
799 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000801 and potentially many more instructions (depending on the pipeline width).
802 A correctly predicted branch, however, is nearly free.
803
804 At the time of this writing, the "threaded code" version is up to 15-20%
805 faster than the normal "switch" version, depending on the compiler and the
806 CPU architecture.
807
808 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
809 because it would render the measurements invalid.
810
811
812 NOTE: care must be taken that the compiler doesn't try to "optimize" the
813 indirect jumps by sharing them between all opcodes. Such optimizations
814 can be disabled on gcc by using the -fno-gcse flag (or possibly
815 -fno-crossjumping).
816*/
817
Antoine Pitrou042b1282010-08-13 21:15:58 +0000818#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000819#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000820#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000821#endif
822
Antoine Pitrou042b1282010-08-13 21:15:58 +0000823#ifdef HAVE_COMPUTED_GOTOS
824 #ifndef USE_COMPUTED_GOTOS
825 #define USE_COMPUTED_GOTOS 1
826 #endif
827#else
828 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
829 #error "Computed gotos are not supported on this compiler."
830 #endif
831 #undef USE_COMPUTED_GOTOS
832 #define USE_COMPUTED_GOTOS 0
833#endif
834
835#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000836/* Import the static jump table */
837#include "opcode_targets.h"
838
Antoine Pitroub52ec782009-01-25 16:34:23 +0000839#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -0700840 op: \
841 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000842
Antoine Pitroub52ec782009-01-25 16:34:23 +0000843#ifdef LLTRACE
844#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200846 if (!lltrace && !_Py_TracingPossible(ceval) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300848 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300849 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 } \
851 goto fast_next_opcode; \
852 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000853#else
854#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200856 if (!_Py_TracingPossible(ceval) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300858 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300859 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 } \
861 goto fast_next_opcode; \
862 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000863#endif
864
Victor Stinner09532fe2019-05-10 23:39:09 +0200865#define DISPATCH() \
866 { \
867 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
868 FAST_DISPATCH(); \
869 } \
870 continue; \
871 }
872
Antoine Pitroub52ec782009-01-25 16:34:23 +0000873#else
Benjamin Petersonddd19492018-09-16 22:38:02 -0700874#define TARGET(op) op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000875#define FAST_DISPATCH() goto fast_next_opcode
Victor Stinner09532fe2019-05-10 23:39:09 +0200876#define DISPATCH() continue
Antoine Pitroub52ec782009-01-25 16:34:23 +0000877#endif
878
879
Neal Norwitza81d2202002-07-14 00:27:26 +0000880/* Tuple access macros */
881
882#ifndef Py_DEBUG
883#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
884#else
885#define GETITEM(v, i) PyTuple_GetItem((v), (i))
886#endif
887
Guido van Rossum374a9221991-04-04 10:40:29 +0000888/* Code access macros */
889
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300890/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600891#define INSTR_OFFSET() \
892 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300893#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300894 _Py_CODEUNIT word = *next_instr; \
895 opcode = _Py_OPCODE(word); \
896 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300897 next_instr++; \
898 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300899#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
900#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000901
Raymond Hettingerf606f872003-03-16 03:11:04 +0000902/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 Some opcodes tend to come in pairs thus making it possible to
904 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300905 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000906
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 Verifying the prediction costs a single high-speed test of a register
908 variable against a constant. If the pairing was good, then the
909 processor's own internal branch predication has a high likelihood of
910 success, resulting in a nearly zero-overhead transition to the
911 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300912 including its unpredictable switch-case branch. Combined with the
913 processor's internal branch prediction, a successful PREDICT has the
914 effect of making the two opcodes run as if they were a single new opcode
915 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000916
Georg Brandl86b2fb92008-07-16 03:43:04 +0000917 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 predictions turned-on and interpret the results as if some opcodes
919 had been combined or turn-off predictions so that the opcode frequency
920 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000921
922 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 the CPU to record separate branch prediction information for each
924 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000925
Raymond Hettingerf606f872003-03-16 03:11:04 +0000926*/
927
Denis Chernikovbaf29b22020-02-21 12:17:50 +0300928#define PREDICT_ID(op) PRED_##op
929
Antoine Pitrou042b1282010-08-13 21:15:58 +0000930#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Denis Chernikovbaf29b22020-02-21 12:17:50 +0300931#define PREDICT(op) if (0) goto PREDICT_ID(op)
Raymond Hettingera7216982004-02-08 19:59:27 +0000932#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300933#define PREDICT(op) \
Denis Chernikovbaf29b22020-02-21 12:17:50 +0300934 do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300935 _Py_CODEUNIT word = *next_instr; \
936 opcode = _Py_OPCODE(word); \
Denis Chernikovbaf29b22020-02-21 12:17:50 +0300937 if (opcode == op) { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300938 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300939 next_instr++; \
Denis Chernikovbaf29b22020-02-21 12:17:50 +0300940 goto PREDICT_ID(op); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300941 } \
942 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000943#endif
Denis Chernikovbaf29b22020-02-21 12:17:50 +0300944#define PREDICTED(op) PREDICT_ID(op):
Antoine Pitroub52ec782009-01-25 16:34:23 +0000945
Raymond Hettingerf606f872003-03-16 03:11:04 +0000946
Guido van Rossum374a9221991-04-04 10:40:29 +0000947/* Stack manipulation macros */
948
Martin v. Löwis18e16552006-02-15 17:27:45 +0000949/* The stack can grow at most MAXINT deep, as co_nlocals and
950 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000951#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
952#define EMPTY() (STACK_LEVEL() == 0)
953#define TOP() (stack_pointer[-1])
954#define SECOND() (stack_pointer[-2])
955#define THIRD() (stack_pointer[-3])
956#define FOURTH() (stack_pointer[-4])
957#define PEEK(n) (stack_pointer[-(n)])
958#define SET_TOP(v) (stack_pointer[-1] = (v))
959#define SET_SECOND(v) (stack_pointer[-2] = (v))
960#define SET_THIRD(v) (stack_pointer[-3] = (v))
961#define SET_FOURTH(v) (stack_pointer[-4] = (v))
962#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
963#define BASIC_STACKADJ(n) (stack_pointer += n)
964#define BASIC_PUSH(v) (*stack_pointer++ = (v))
965#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000966
Guido van Rossum96a42c81992-01-12 02:29:51 +0000967#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +0200969 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000970 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +0200971#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000972 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +0000973#define STACK_GROW(n) do { \
974 assert(n >= 0); \
975 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +0200976 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +0000977 assert(STACK_LEVEL() <= co->co_stacksize); \
978 } while (0)
979#define STACK_SHRINK(n) do { \
980 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +0200981 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +0000982 (void)(BASIC_STACKADJ(-n)); \
983 assert(STACK_LEVEL() <= co->co_stacksize); \
984 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +0000985#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +0200986 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000987 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000988#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000989#define PUSH(v) BASIC_PUSH(v)
990#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +0000991#define STACK_GROW(n) BASIC_STACKADJ(n)
992#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000993#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000994#endif
995
Guido van Rossum681d79a1995-07-18 14:51:37 +0000996/* Local variable macros */
997
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000999
1000/* The SETLOCAL() macro must not DECREF the local variable in-place and
1001 then store the new value; it must copy the old value to a temporary
1002 value, then store the new value, and then DECREF the temporary value.
1003 This is because it is possible that during the DECREF the frame is
1004 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1005 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001007 GETLOCAL(i) = value; \
1008 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001009
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001010
1011#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 while (STACK_LEVEL() > (b)->b_level) { \
1013 PyObject *v = POP(); \
1014 Py_XDECREF(v); \
1015 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001016
1017#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001018 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +01001020 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1022 while (STACK_LEVEL() > (b)->b_level + 3) { \
1023 value = POP(); \
1024 Py_XDECREF(value); \
1025 } \
Mark Shannonae3087c2017-10-22 22:41:51 +01001026 exc_info = tstate->exc_info; \
1027 type = exc_info->exc_type; \
1028 value = exc_info->exc_value; \
1029 traceback = exc_info->exc_traceback; \
1030 exc_info->exc_type = POP(); \
1031 exc_info->exc_value = POP(); \
1032 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 Py_XDECREF(type); \
1034 Py_XDECREF(value); \
1035 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001036 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001037
Inada Naoki91234a12019-06-03 21:30:58 +09001038 /* macros for opcode cache */
1039#define OPCACHE_CHECK() \
1040 do { \
1041 co_opcache = NULL; \
1042 if (co->co_opcache != NULL) { \
1043 unsigned char co_opt_offset = \
1044 co->co_opcache_map[next_instr - first_instr]; \
1045 if (co_opt_offset > 0) { \
1046 assert(co_opt_offset <= co->co_opcache_size); \
1047 co_opcache = &co->co_opcache[co_opt_offset - 1]; \
1048 assert(co_opcache != NULL); \
Inada Naoki91234a12019-06-03 21:30:58 +09001049 } \
1050 } \
1051 } while (0)
1052
1053#if OPCACHE_STATS
1054
1055#define OPCACHE_STAT_GLOBAL_HIT() \
1056 do { \
1057 if (co->co_opcache != NULL) opcache_global_hits++; \
1058 } while (0)
1059
1060#define OPCACHE_STAT_GLOBAL_MISS() \
1061 do { \
1062 if (co->co_opcache != NULL) opcache_global_misses++; \
1063 } while (0)
1064
1065#define OPCACHE_STAT_GLOBAL_OPT() \
1066 do { \
1067 if (co->co_opcache != NULL) opcache_global_opts++; \
1068 } while (0)
1069
1070#else /* OPCACHE_STATS */
1071
1072#define OPCACHE_STAT_GLOBAL_HIT()
1073#define OPCACHE_STAT_GLOBAL_MISS()
1074#define OPCACHE_STAT_GLOBAL_OPT()
1075
1076#endif
1077
Guido van Rossuma027efa1997-05-05 20:56:21 +00001078/* Start of code */
1079
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 /* push frame */
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001081 if (_Py_EnterRecursiveCall(tstate, "")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001083 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001086
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 if (tstate->use_tracing) {
1088 if (tstate->c_tracefunc != NULL) {
1089 /* tstate->c_tracefunc, if defined, is a
1090 function that will be called on *every* entry
1091 to a code block. Its return value, if not
1092 None, is a function that will be called at
1093 the start of each executed line of code.
1094 (Actually, the function must return itself
1095 in order to continue tracing.) The trace
1096 functions are called with three arguments:
1097 a pointer to the current frame, a string
1098 indicating why the function is called, and
1099 an argument which depends on the situation.
1100 The global trace function is also called
1101 whenever an exception is detected. */
1102 if (call_trace_protected(tstate->c_tracefunc,
1103 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001104 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 /* Trace function raised an error */
1106 goto exit_eval_frame;
1107 }
1108 }
1109 if (tstate->c_profilefunc != NULL) {
1110 /* Similar for c_profilefunc, except it needn't
1111 return itself and isn't called for "line" events */
1112 if (call_trace_protected(tstate->c_profilefunc,
1113 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001114 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 /* Profile function raised an error */
1116 goto exit_eval_frame;
1117 }
1118 }
1119 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001120
Łukasz Langaa785c872016-09-09 17:37:37 -07001121 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1122 dtrace_function_entry(f);
1123
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 co = f->f_code;
1125 names = co->co_names;
1126 consts = co->co_consts;
1127 fastlocals = f->f_localsplus;
1128 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001129 assert(PyBytes_Check(co->co_code));
1130 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001131 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1132 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1133 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001134 /*
1135 f->f_lasti refers to the index of the last instruction,
1136 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001137
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001138 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001139 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001140
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001141 When the PREDICT() macros are enabled, some opcode pairs follow in
1142 direct succession without updating f->f_lasti. A successful
1143 prediction effectively links the two codes together as if they
1144 were a single new opcode; accordingly,f->f_lasti will point to
1145 the first code in the pair (for instance, GET_ITER followed by
1146 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001147 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001149 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001150 next_instr = first_instr;
1151 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001152 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1153 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001154 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 stack_pointer = f->f_stacktop;
1156 assert(stack_pointer != NULL);
1157 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001158 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001159
Inada Naoki91234a12019-06-03 21:30:58 +09001160 if (co->co_opcache_flag < OPCACHE_MIN_RUNS) {
1161 co->co_opcache_flag++;
1162 if (co->co_opcache_flag == OPCACHE_MIN_RUNS) {
1163 if (_PyCode_InitOpcache(co) < 0) {
1164 return NULL;
1165 }
1166#if OPCACHE_STATS
1167 opcache_code_objects_extra_mem +=
1168 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1169 sizeof(_PyOpcache) * co->co_opcache_size;
1170 opcache_code_objects++;
1171#endif
1172 }
1173 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001174
Tim Peters5ca576e2001-06-18 22:08:13 +00001175#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001176 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001177#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001178
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001179 if (throwflag) /* support for generator.throw() */
1180 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001181
Victor Stinnerace47d72013-07-18 01:41:08 +02001182#ifdef Py_DEBUG
1183 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001184 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001185 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001186 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001187#endif
1188
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001189main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1192 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001193 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 /* Do periodic things. Doing this every time through
1196 the loop would add too much overhead, so we do it
1197 only every Nth instruction. We also do it if
1198 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1199 event needs attention (e.g. a signal handler or
1200 async I/O handler); see Py_AddPendingCall() and
1201 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001202
Eric Snow7bda9de2019-03-08 17:25:54 -07001203 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001204 opcode = _Py_OPCODE(*next_instr);
1205 if (opcode == SETUP_FINALLY ||
1206 opcode == SETUP_WITH ||
1207 opcode == BEFORE_ASYNC_WITH ||
1208 opcode == YIELD_FROM) {
1209 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001210 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001211 - If we're about to enter the 'with:'. It will prevent
1212 emitting a resource warning in the common idiom
1213 'with open(path) as file:'.
1214 - If we're about to enter the 'async with:'.
1215 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001216 *very* useful, but might help in some cases and it's
1217 traditional)
1218 - If we're resuming a chain of nested 'yield from' or
1219 'await' calls, then each frame is parked with YIELD_FROM
1220 as its next opcode. If the user hit control-C we want to
1221 wait until we've reached the innermost frame before
1222 running the signal handler and raising KeyboardInterrupt
1223 (see bpo-30039).
1224 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 goto fast_next_opcode;
1226 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001227
Victor Stinnere225beb2019-06-03 18:14:24 +02001228 if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
Victor Stinner09532fe2019-05-10 23:39:09 +02001229 if (handle_signals(runtime) != 0) {
Eric Snowfdf282d2019-01-11 14:26:55 -07001230 goto error;
1231 }
1232 }
Victor Stinnere225beb2019-06-03 18:14:24 +02001233 if (_Py_atomic_load_relaxed(&ceval->pending.calls_to_do)) {
1234 if (make_pending_calls(runtime) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001235 goto error;
Eric Snowfdf282d2019-01-11 14:26:55 -07001236 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001238
Victor Stinnere225beb2019-06-03 18:14:24 +02001239 if (_Py_atomic_load_relaxed(&ceval->gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 /* Give another thread a chance */
Victor Stinner09532fe2019-05-10 23:39:09 +02001241 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +01001242 Py_FatalError("tstate mix-up");
Victor Stinner09532fe2019-05-10 23:39:09 +02001243 }
Victor Stinnere225beb2019-06-03 18:14:24 +02001244 drop_gil(ceval, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245
1246 /* Other threads may run now */
1247
Victor Stinner85f5a692020-03-09 22:12:04 +01001248 take_gil(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +01001249
Victor Stinner09532fe2019-05-10 23:39:09 +02001250 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +01001251 Py_FatalError("orphan tstate");
Victor Stinner09532fe2019-05-10 23:39:09 +02001252 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 }
1254 /* Check for asynchronous exceptions. */
1255 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001256 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 tstate->async_exc = NULL;
Victor Stinnere225beb2019-06-03 18:14:24 +02001258 UNSIGNAL_ASYNC_EXC(ceval);
Victor Stinner438a12d2019-05-24 17:01:38 +02001259 _PyErr_SetNone(tstate, exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001260 Py_DECREF(exc);
1261 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001262 }
1263 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 fast_next_opcode:
1266 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001267
Łukasz Langaa785c872016-09-09 17:37:37 -07001268 if (PyDTrace_LINE_ENABLED())
1269 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001272
Victor Stinnere225beb2019-06-03 18:14:24 +02001273 if (_Py_TracingPossible(ceval) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001274 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001275 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 /* see maybe_call_line_trace
1277 for expository comments */
1278 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 err = maybe_call_line_trace(tstate->c_tracefunc,
1281 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001282 tstate, f,
1283 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 /* Reload possibly changed frame fields */
1285 JUMPTO(f->f_lasti);
1286 if (f->f_stacktop != NULL) {
1287 stack_pointer = f->f_stacktop;
1288 f->f_stacktop = NULL;
1289 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001290 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001292 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001295 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001296
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001297 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001298 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001299#ifdef DYNAMIC_EXECUTION_PROFILE
1300#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001301 dxpairs[lastopcode][opcode]++;
1302 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001303#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001305#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001306
Guido van Rossum96a42c81992-01-12 02:29:51 +00001307#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 if (lltrace) {
1311 if (HAS_ARG(opcode)) {
1312 printf("%d: %d, %d\n",
1313 f->f_lasti, opcode, oparg);
1314 }
1315 else {
1316 printf("%d: %d\n",
1317 f->f_lasti, opcode);
1318 }
1319 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001320#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001325 It is essential that any operation that fails must goto error
1326 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001327
Benjamin Petersonddd19492018-09-16 22:38:02 -07001328 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001330 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001331
Benjamin Petersonddd19492018-09-16 22:38:02 -07001332 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001333 PyObject *value = GETLOCAL(oparg);
1334 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001335 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001336 UNBOUNDLOCAL_ERROR_MSG,
1337 PyTuple_GetItem(co->co_varnames, oparg));
1338 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001340 Py_INCREF(value);
1341 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001343 }
1344
Benjamin Petersonddd19492018-09-16 22:38:02 -07001345 case TARGET(LOAD_CONST): {
1346 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001347 PyObject *value = GETITEM(consts, oparg);
1348 Py_INCREF(value);
1349 PUSH(value);
1350 FAST_DISPATCH();
1351 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001352
Benjamin Petersonddd19492018-09-16 22:38:02 -07001353 case TARGET(STORE_FAST): {
1354 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001355 PyObject *value = POP();
1356 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001358 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001359
Benjamin Petersonddd19492018-09-16 22:38:02 -07001360 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001361 PyObject *value = POP();
1362 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001364 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001365
Benjamin Petersonddd19492018-09-16 22:38:02 -07001366 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001367 PyObject *top = TOP();
1368 PyObject *second = SECOND();
1369 SET_TOP(second);
1370 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001372 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001373
Benjamin Petersonddd19492018-09-16 22:38:02 -07001374 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001375 PyObject *top = TOP();
1376 PyObject *second = SECOND();
1377 PyObject *third = THIRD();
1378 SET_TOP(second);
1379 SET_SECOND(third);
1380 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001382 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001383
Benjamin Petersonddd19492018-09-16 22:38:02 -07001384 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001385 PyObject *top = TOP();
1386 PyObject *second = SECOND();
1387 PyObject *third = THIRD();
1388 PyObject *fourth = FOURTH();
1389 SET_TOP(second);
1390 SET_SECOND(third);
1391 SET_THIRD(fourth);
1392 SET_FOURTH(top);
1393 FAST_DISPATCH();
1394 }
1395
Benjamin Petersonddd19492018-09-16 22:38:02 -07001396 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001397 PyObject *top = TOP();
1398 Py_INCREF(top);
1399 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001401 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001402
Benjamin Petersonddd19492018-09-16 22:38:02 -07001403 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001404 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001405 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001406 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001407 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001408 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001409 SET_TOP(top);
1410 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001411 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001412 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001413
Benjamin Petersonddd19492018-09-16 22:38:02 -07001414 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001415 PyObject *value = TOP();
1416 PyObject *res = PyNumber_Positive(value);
1417 Py_DECREF(value);
1418 SET_TOP(res);
1419 if (res == NULL)
1420 goto error;
1421 DISPATCH();
1422 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001423
Benjamin Petersonddd19492018-09-16 22:38:02 -07001424 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001425 PyObject *value = TOP();
1426 PyObject *res = PyNumber_Negative(value);
1427 Py_DECREF(value);
1428 SET_TOP(res);
1429 if (res == NULL)
1430 goto error;
1431 DISPATCH();
1432 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001433
Benjamin Petersonddd19492018-09-16 22:38:02 -07001434 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001435 PyObject *value = TOP();
1436 int err = PyObject_IsTrue(value);
1437 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438 if (err == 0) {
1439 Py_INCREF(Py_True);
1440 SET_TOP(Py_True);
1441 DISPATCH();
1442 }
1443 else if (err > 0) {
1444 Py_INCREF(Py_False);
1445 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001446 DISPATCH();
1447 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001448 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001449 goto error;
1450 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001451
Benjamin Petersonddd19492018-09-16 22:38:02 -07001452 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001453 PyObject *value = TOP();
1454 PyObject *res = PyNumber_Invert(value);
1455 Py_DECREF(value);
1456 SET_TOP(res);
1457 if (res == NULL)
1458 goto error;
1459 DISPATCH();
1460 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001461
Benjamin Petersonddd19492018-09-16 22:38:02 -07001462 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001463 PyObject *exp = POP();
1464 PyObject *base = TOP();
1465 PyObject *res = PyNumber_Power(base, exp, Py_None);
1466 Py_DECREF(base);
1467 Py_DECREF(exp);
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(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001475 PyObject *right = POP();
1476 PyObject *left = TOP();
1477 PyObject *res = PyNumber_Multiply(left, right);
1478 Py_DECREF(left);
1479 Py_DECREF(right);
1480 SET_TOP(res);
1481 if (res == NULL)
1482 goto error;
1483 DISPATCH();
1484 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001485
Benjamin Petersonddd19492018-09-16 22:38:02 -07001486 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001487 PyObject *right = POP();
1488 PyObject *left = TOP();
1489 PyObject *res = PyNumber_MatrixMultiply(left, right);
1490 Py_DECREF(left);
1491 Py_DECREF(right);
1492 SET_TOP(res);
1493 if (res == NULL)
1494 goto error;
1495 DISPATCH();
1496 }
1497
Benjamin Petersonddd19492018-09-16 22:38:02 -07001498 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001499 PyObject *divisor = POP();
1500 PyObject *dividend = TOP();
1501 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1502 Py_DECREF(dividend);
1503 Py_DECREF(divisor);
1504 SET_TOP(quotient);
1505 if (quotient == NULL)
1506 goto error;
1507 DISPATCH();
1508 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001509
Benjamin Petersonddd19492018-09-16 22:38:02 -07001510 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001511 PyObject *divisor = POP();
1512 PyObject *dividend = TOP();
1513 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1514 Py_DECREF(dividend);
1515 Py_DECREF(divisor);
1516 SET_TOP(quotient);
1517 if (quotient == NULL)
1518 goto error;
1519 DISPATCH();
1520 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001521
Benjamin Petersonddd19492018-09-16 22:38:02 -07001522 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001523 PyObject *divisor = POP();
1524 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001525 PyObject *res;
1526 if (PyUnicode_CheckExact(dividend) && (
1527 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1528 // fast path; string formatting, but not if the RHS is a str subclass
1529 // (see issue28598)
1530 res = PyUnicode_Format(dividend, divisor);
1531 } else {
1532 res = PyNumber_Remainder(dividend, divisor);
1533 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001534 Py_DECREF(divisor);
1535 Py_DECREF(dividend);
1536 SET_TOP(res);
1537 if (res == NULL)
1538 goto error;
1539 DISPATCH();
1540 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001541
Benjamin Petersonddd19492018-09-16 22:38:02 -07001542 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001543 PyObject *right = POP();
1544 PyObject *left = TOP();
1545 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001546 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1547 CPython using bytecode, it is simply worthless.
1548 See http://bugs.python.org/issue21955 and
1549 http://bugs.python.org/issue10044 for the discussion. In short,
1550 no patch shown any impact on a realistic benchmark, only a minor
1551 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001552 if (PyUnicode_CheckExact(left) &&
1553 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001554 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001555 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001556 }
1557 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001558 sum = PyNumber_Add(left, right);
1559 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001560 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001561 Py_DECREF(right);
1562 SET_TOP(sum);
1563 if (sum == NULL)
1564 goto error;
1565 DISPATCH();
1566 }
1567
Benjamin Petersonddd19492018-09-16 22:38:02 -07001568 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001569 PyObject *right = POP();
1570 PyObject *left = TOP();
1571 PyObject *diff = PyNumber_Subtract(left, right);
1572 Py_DECREF(right);
1573 Py_DECREF(left);
1574 SET_TOP(diff);
1575 if (diff == NULL)
1576 goto error;
1577 DISPATCH();
1578 }
1579
Benjamin Petersonddd19492018-09-16 22:38:02 -07001580 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001581 PyObject *sub = POP();
1582 PyObject *container = TOP();
1583 PyObject *res = PyObject_GetItem(container, sub);
1584 Py_DECREF(container);
1585 Py_DECREF(sub);
1586 SET_TOP(res);
1587 if (res == NULL)
1588 goto error;
1589 DISPATCH();
1590 }
1591
Benjamin Petersonddd19492018-09-16 22:38:02 -07001592 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001593 PyObject *right = POP();
1594 PyObject *left = TOP();
1595 PyObject *res = PyNumber_Lshift(left, right);
1596 Py_DECREF(left);
1597 Py_DECREF(right);
1598 SET_TOP(res);
1599 if (res == NULL)
1600 goto error;
1601 DISPATCH();
1602 }
1603
Benjamin Petersonddd19492018-09-16 22:38:02 -07001604 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001605 PyObject *right = POP();
1606 PyObject *left = TOP();
1607 PyObject *res = PyNumber_Rshift(left, right);
1608 Py_DECREF(left);
1609 Py_DECREF(right);
1610 SET_TOP(res);
1611 if (res == NULL)
1612 goto error;
1613 DISPATCH();
1614 }
1615
Benjamin Petersonddd19492018-09-16 22:38:02 -07001616 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001617 PyObject *right = POP();
1618 PyObject *left = TOP();
1619 PyObject *res = PyNumber_And(left, right);
1620 Py_DECREF(left);
1621 Py_DECREF(right);
1622 SET_TOP(res);
1623 if (res == NULL)
1624 goto error;
1625 DISPATCH();
1626 }
1627
Benjamin Petersonddd19492018-09-16 22:38:02 -07001628 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001629 PyObject *right = POP();
1630 PyObject *left = TOP();
1631 PyObject *res = PyNumber_Xor(left, right);
1632 Py_DECREF(left);
1633 Py_DECREF(right);
1634 SET_TOP(res);
1635 if (res == NULL)
1636 goto error;
1637 DISPATCH();
1638 }
1639
Benjamin Petersonddd19492018-09-16 22:38:02 -07001640 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001641 PyObject *right = POP();
1642 PyObject *left = TOP();
1643 PyObject *res = PyNumber_Or(left, right);
1644 Py_DECREF(left);
1645 Py_DECREF(right);
1646 SET_TOP(res);
1647 if (res == NULL)
1648 goto error;
1649 DISPATCH();
1650 }
1651
Benjamin Petersonddd19492018-09-16 22:38:02 -07001652 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001653 PyObject *v = POP();
1654 PyObject *list = PEEK(oparg);
1655 int err;
1656 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001658 if (err != 0)
1659 goto error;
1660 PREDICT(JUMP_ABSOLUTE);
1661 DISPATCH();
1662 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001663
Benjamin Petersonddd19492018-09-16 22:38:02 -07001664 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001665 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001666 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001667 int err;
1668 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001670 if (err != 0)
1671 goto error;
1672 PREDICT(JUMP_ABSOLUTE);
1673 DISPATCH();
1674 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001675
Benjamin Petersonddd19492018-09-16 22:38:02 -07001676 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001677 PyObject *exp = POP();
1678 PyObject *base = TOP();
1679 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1680 Py_DECREF(base);
1681 Py_DECREF(exp);
1682 SET_TOP(res);
1683 if (res == NULL)
1684 goto error;
1685 DISPATCH();
1686 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001687
Benjamin Petersonddd19492018-09-16 22:38:02 -07001688 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001689 PyObject *right = POP();
1690 PyObject *left = TOP();
1691 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1692 Py_DECREF(left);
1693 Py_DECREF(right);
1694 SET_TOP(res);
1695 if (res == NULL)
1696 goto error;
1697 DISPATCH();
1698 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001699
Benjamin Petersonddd19492018-09-16 22:38:02 -07001700 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001701 PyObject *right = POP();
1702 PyObject *left = TOP();
1703 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1704 Py_DECREF(left);
1705 Py_DECREF(right);
1706 SET_TOP(res);
1707 if (res == NULL)
1708 goto error;
1709 DISPATCH();
1710 }
1711
Benjamin Petersonddd19492018-09-16 22:38:02 -07001712 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001713 PyObject *divisor = POP();
1714 PyObject *dividend = TOP();
1715 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1716 Py_DECREF(dividend);
1717 Py_DECREF(divisor);
1718 SET_TOP(quotient);
1719 if (quotient == NULL)
1720 goto error;
1721 DISPATCH();
1722 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001723
Benjamin Petersonddd19492018-09-16 22:38:02 -07001724 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001725 PyObject *divisor = POP();
1726 PyObject *dividend = TOP();
1727 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1728 Py_DECREF(dividend);
1729 Py_DECREF(divisor);
1730 SET_TOP(quotient);
1731 if (quotient == NULL)
1732 goto error;
1733 DISPATCH();
1734 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001735
Benjamin Petersonddd19492018-09-16 22:38:02 -07001736 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001737 PyObject *right = POP();
1738 PyObject *left = TOP();
1739 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1740 Py_DECREF(left);
1741 Py_DECREF(right);
1742 SET_TOP(mod);
1743 if (mod == NULL)
1744 goto error;
1745 DISPATCH();
1746 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001747
Benjamin Petersonddd19492018-09-16 22:38:02 -07001748 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001749 PyObject *right = POP();
1750 PyObject *left = TOP();
1751 PyObject *sum;
1752 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001753 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001754 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001755 }
1756 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001757 sum = PyNumber_InPlaceAdd(left, right);
1758 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001759 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001760 Py_DECREF(right);
1761 SET_TOP(sum);
1762 if (sum == NULL)
1763 goto error;
1764 DISPATCH();
1765 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001766
Benjamin Petersonddd19492018-09-16 22:38:02 -07001767 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001768 PyObject *right = POP();
1769 PyObject *left = TOP();
1770 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1771 Py_DECREF(left);
1772 Py_DECREF(right);
1773 SET_TOP(diff);
1774 if (diff == NULL)
1775 goto error;
1776 DISPATCH();
1777 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001778
Benjamin Petersonddd19492018-09-16 22:38:02 -07001779 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001780 PyObject *right = POP();
1781 PyObject *left = TOP();
1782 PyObject *res = PyNumber_InPlaceLshift(left, right);
1783 Py_DECREF(left);
1784 Py_DECREF(right);
1785 SET_TOP(res);
1786 if (res == NULL)
1787 goto error;
1788 DISPATCH();
1789 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001790
Benjamin Petersonddd19492018-09-16 22:38:02 -07001791 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001792 PyObject *right = POP();
1793 PyObject *left = TOP();
1794 PyObject *res = PyNumber_InPlaceRshift(left, right);
1795 Py_DECREF(left);
1796 Py_DECREF(right);
1797 SET_TOP(res);
1798 if (res == NULL)
1799 goto error;
1800 DISPATCH();
1801 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001802
Benjamin Petersonddd19492018-09-16 22:38:02 -07001803 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001804 PyObject *right = POP();
1805 PyObject *left = TOP();
1806 PyObject *res = PyNumber_InPlaceAnd(left, right);
1807 Py_DECREF(left);
1808 Py_DECREF(right);
1809 SET_TOP(res);
1810 if (res == NULL)
1811 goto error;
1812 DISPATCH();
1813 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001814
Benjamin Petersonddd19492018-09-16 22:38:02 -07001815 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001816 PyObject *right = POP();
1817 PyObject *left = TOP();
1818 PyObject *res = PyNumber_InPlaceXor(left, right);
1819 Py_DECREF(left);
1820 Py_DECREF(right);
1821 SET_TOP(res);
1822 if (res == NULL)
1823 goto error;
1824 DISPATCH();
1825 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001826
Benjamin Petersonddd19492018-09-16 22:38:02 -07001827 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001828 PyObject *right = POP();
1829 PyObject *left = TOP();
1830 PyObject *res = PyNumber_InPlaceOr(left, right);
1831 Py_DECREF(left);
1832 Py_DECREF(right);
1833 SET_TOP(res);
1834 if (res == NULL)
1835 goto error;
1836 DISPATCH();
1837 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001838
Benjamin Petersonddd19492018-09-16 22:38:02 -07001839 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001840 PyObject *sub = TOP();
1841 PyObject *container = SECOND();
1842 PyObject *v = THIRD();
1843 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001844 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001845 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001846 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001847 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001848 Py_DECREF(container);
1849 Py_DECREF(sub);
1850 if (err != 0)
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(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001856 PyObject *sub = TOP();
1857 PyObject *container = SECOND();
1858 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001859 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00001860 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001861 err = PyObject_DelItem(container, sub);
1862 Py_DECREF(container);
1863 Py_DECREF(sub);
1864 if (err != 0)
1865 goto error;
1866 DISPATCH();
1867 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001868
Benjamin Petersonddd19492018-09-16 22:38:02 -07001869 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01001870 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001871 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001872 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001873 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001874 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001875 _PyErr_SetString(tstate, PyExc_RuntimeError,
1876 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001877 Py_DECREF(value);
1878 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001879 }
Petr Viktorinffd97532020-02-11 17:46:57 +01001880 res = PyObject_CallOneArg(hook, value);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001881 Py_DECREF(value);
1882 if (res == NULL)
1883 goto error;
1884 Py_DECREF(res);
1885 DISPATCH();
1886 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001887
Benjamin Petersonddd19492018-09-16 22:38:02 -07001888 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001889 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001890 switch (oparg) {
1891 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001892 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001893 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001894 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001895 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001896 /* fall through */
1897 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02001898 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001899 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001900 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001901 break;
1902 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02001903 _PyErr_SetString(tstate, PyExc_SystemError,
1904 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905 break;
1906 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001907 goto error;
1908 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001909
Benjamin Petersonddd19492018-09-16 22:38:02 -07001910 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001911 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001912 assert(f->f_iblock == 0);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00001913 assert(EMPTY());
1914 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001915 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001916
Benjamin Petersonddd19492018-09-16 22:38:02 -07001917 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001918 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001919 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001920 PyObject *obj = TOP();
1921 PyTypeObject *type = Py_TYPE(obj);
1922
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001923 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001924 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001925 }
Yury Selivanov75445082015-05-11 22:57:16 -04001926
1927 if (getter != NULL) {
1928 iter = (*getter)(obj);
1929 Py_DECREF(obj);
1930 if (iter == NULL) {
1931 SET_TOP(NULL);
1932 goto error;
1933 }
1934 }
1935 else {
1936 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02001937 _PyErr_Format(tstate, PyExc_TypeError,
1938 "'async for' requires an object with "
1939 "__aiter__ method, got %.100s",
1940 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001941 Py_DECREF(obj);
1942 goto error;
1943 }
1944
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001945 if (Py_TYPE(iter)->tp_as_async == NULL ||
1946 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001947
Yury Selivanov398ff912017-03-02 22:20:00 -05001948 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02001949 _PyErr_Format(tstate, PyExc_TypeError,
1950 "'async for' received an object from __aiter__ "
1951 "that does not implement __anext__: %.100s",
1952 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001953 Py_DECREF(iter);
1954 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001955 }
1956
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001957 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001958 DISPATCH();
1959 }
1960
Benjamin Petersonddd19492018-09-16 22:38:02 -07001961 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001962 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001963 PyObject *next_iter = NULL;
1964 PyObject *awaitable = NULL;
1965 PyObject *aiter = TOP();
1966 PyTypeObject *type = Py_TYPE(aiter);
1967
Yury Selivanoveb636452016-09-08 22:01:51 -07001968 if (PyAsyncGen_CheckExact(aiter)) {
1969 awaitable = type->tp_as_async->am_anext(aiter);
1970 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001971 goto error;
1972 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001973 } else {
1974 if (type->tp_as_async != NULL){
1975 getter = type->tp_as_async->am_anext;
1976 }
Yury Selivanov75445082015-05-11 22:57:16 -04001977
Yury Selivanoveb636452016-09-08 22:01:51 -07001978 if (getter != NULL) {
1979 next_iter = (*getter)(aiter);
1980 if (next_iter == NULL) {
1981 goto error;
1982 }
1983 }
1984 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02001985 _PyErr_Format(tstate, PyExc_TypeError,
1986 "'async for' requires an iterator with "
1987 "__anext__ method, got %.100s",
1988 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07001989 goto error;
1990 }
Yury Selivanov75445082015-05-11 22:57:16 -04001991
Yury Selivanoveb636452016-09-08 22:01:51 -07001992 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1993 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001994 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001995 PyExc_TypeError,
1996 "'async for' received an invalid object "
1997 "from __anext__: %.100s",
1998 Py_TYPE(next_iter)->tp_name);
1999
2000 Py_DECREF(next_iter);
2001 goto error;
2002 } else {
2003 Py_DECREF(next_iter);
2004 }
2005 }
Yury Selivanov75445082015-05-11 22:57:16 -04002006
2007 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002008 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002009 DISPATCH();
2010 }
2011
Benjamin Petersonddd19492018-09-16 22:38:02 -07002012 case TARGET(GET_AWAITABLE): {
2013 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002014 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002015 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002016
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002017 if (iter == NULL) {
Mark Shannonfee55262019-11-21 09:11:43 +00002018 int opcode_at_minus_3 = 0;
2019 if ((next_instr - first_instr) > 2) {
2020 opcode_at_minus_3 = _Py_OPCODE(next_instr[-3]);
2021 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002022 format_awaitable_error(tstate, Py_TYPE(iterable),
Mark Shannonfee55262019-11-21 09:11:43 +00002023 opcode_at_minus_3,
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002024 _Py_OPCODE(next_instr[-2]));
2025 }
2026
Yury Selivanov75445082015-05-11 22:57:16 -04002027 Py_DECREF(iterable);
2028
Yury Selivanovc724bae2016-03-02 11:30:46 -05002029 if (iter != NULL && PyCoro_CheckExact(iter)) {
2030 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2031 if (yf != NULL) {
2032 /* `iter` is a coroutine object that is being
2033 awaited, `yf` is a pointer to the current awaitable
2034 being awaited on. */
2035 Py_DECREF(yf);
2036 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002037 _PyErr_SetString(tstate, PyExc_RuntimeError,
2038 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002039 /* The code below jumps to `error` if `iter` is NULL. */
2040 }
2041 }
2042
Yury Selivanov75445082015-05-11 22:57:16 -04002043 SET_TOP(iter); /* Even if it's NULL */
2044
2045 if (iter == NULL) {
2046 goto error;
2047 }
2048
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002049 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002050 DISPATCH();
2051 }
2052
Benjamin Petersonddd19492018-09-16 22:38:02 -07002053 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002054 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002055 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002056 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002057 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
2058 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002059 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002060 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002061 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002062 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002063 else
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02002064 retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002065 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002066 Py_DECREF(v);
2067 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002068 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002069 if (tstate->c_tracefunc != NULL
Victor Stinner438a12d2019-05-24 17:01:38 +02002070 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002071 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002072 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002073 if (err < 0)
2074 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002075 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002076 SET_TOP(val);
2077 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002078 }
Martin Panter95f53c12016-07-18 08:23:26 +00002079 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002080 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002081 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002082 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002083 f->f_lasti -= sizeof(_Py_CODEUNIT);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002084 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002085 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002086
Benjamin Petersonddd19492018-09-16 22:38:02 -07002087 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002088 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002089
2090 if (co->co_flags & CO_ASYNC_GENERATOR) {
2091 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2092 Py_DECREF(retval);
2093 if (w == NULL) {
2094 retval = NULL;
2095 goto error;
2096 }
2097 retval = w;
2098 }
2099
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002100 f->f_stacktop = stack_pointer;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002101 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002102 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002103
Benjamin Petersonddd19492018-09-16 22:38:02 -07002104 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002105 PyObject *type, *value, *traceback;
2106 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002107 PyTryBlock *b = PyFrame_BlockPop(f);
2108 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002109 _PyErr_SetString(tstate, PyExc_SystemError,
2110 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002111 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002112 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002113 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2114 STACK_LEVEL() <= (b)->b_level + 4);
2115 exc_info = tstate->exc_info;
2116 type = exc_info->exc_type;
2117 value = exc_info->exc_value;
2118 traceback = exc_info->exc_traceback;
2119 exc_info->exc_type = POP();
2120 exc_info->exc_value = POP();
2121 exc_info->exc_traceback = POP();
2122 Py_XDECREF(type);
2123 Py_XDECREF(value);
2124 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002125 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002126 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002127
Benjamin Petersonddd19492018-09-16 22:38:02 -07002128 case TARGET(POP_BLOCK): {
2129 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002130 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002131 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002132 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002133
Mark Shannonfee55262019-11-21 09:11:43 +00002134 case TARGET(RERAISE): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002135 PyObject *exc = POP();
Mark Shannonfee55262019-11-21 09:11:43 +00002136 PyObject *val = POP();
2137 PyObject *tb = POP();
2138 assert(PyExceptionClass_Check(exc));
Victor Stinner61f4db82020-01-28 03:37:45 +01002139 _PyErr_Restore(tstate, exc, val, tb);
Mark Shannonfee55262019-11-21 09:11:43 +00002140 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002141 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002142
Benjamin Petersonddd19492018-09-16 22:38:02 -07002143 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002144 PyObject *exc = POP();
2145 assert(PyExceptionClass_Check(exc));
2146 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2147 PyTryBlock *b = PyFrame_BlockPop(f);
2148 assert(b->b_type == EXCEPT_HANDLER);
2149 Py_DECREF(exc);
2150 UNWIND_EXCEPT_HANDLER(b);
2151 Py_DECREF(POP());
2152 JUMPBY(oparg);
2153 FAST_DISPATCH();
2154 }
2155 else {
2156 PyObject *val = POP();
2157 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002158 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002159 goto exception_unwind;
2160 }
2161 }
2162
Zackery Spytzce6a0702019-08-25 03:44:09 -06002163 case TARGET(LOAD_ASSERTION_ERROR): {
2164 PyObject *value = PyExc_AssertionError;
2165 Py_INCREF(value);
2166 PUSH(value);
2167 FAST_DISPATCH();
2168 }
2169
Benjamin Petersonddd19492018-09-16 22:38:02 -07002170 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002171 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002172
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002173 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002174 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002175 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002176 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002177 if (!_PyErr_Occurred(tstate)) {
2178 _PyErr_SetString(tstate, PyExc_NameError,
2179 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002180 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002181 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002182 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002183 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002184 }
2185 else {
2186 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2187 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002188 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002189 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2190 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002191 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2192 _PyErr_SetString(tstate, PyExc_NameError,
2193 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002194 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002195 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002196 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002197 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002198 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002199 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002200
Benjamin Petersonddd19492018-09-16 22:38:02 -07002201 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002202 PyObject *name = GETITEM(names, oparg);
2203 PyObject *v = POP();
2204 PyObject *ns = f->f_locals;
2205 int err;
2206 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002207 _PyErr_Format(tstate, PyExc_SystemError,
2208 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002209 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002210 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002211 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002212 if (PyDict_CheckExact(ns))
2213 err = PyDict_SetItem(ns, name, v);
2214 else
2215 err = PyObject_SetItem(ns, name, v);
2216 Py_DECREF(v);
2217 if (err != 0)
2218 goto error;
2219 DISPATCH();
2220 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002221
Benjamin Petersonddd19492018-09-16 22:38:02 -07002222 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002223 PyObject *name = GETITEM(names, oparg);
2224 PyObject *ns = f->f_locals;
2225 int err;
2226 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002227 _PyErr_Format(tstate, PyExc_SystemError,
2228 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002229 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002230 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002231 err = PyObject_DelItem(ns, name);
2232 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002233 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002234 NAME_ERROR_MSG,
2235 name);
2236 goto error;
2237 }
2238 DISPATCH();
2239 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002240
Benjamin Petersonddd19492018-09-16 22:38:02 -07002241 case TARGET(UNPACK_SEQUENCE): {
2242 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002243 PyObject *seq = POP(), *item, **items;
2244 if (PyTuple_CheckExact(seq) &&
2245 PyTuple_GET_SIZE(seq) == oparg) {
2246 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002247 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002248 item = items[oparg];
2249 Py_INCREF(item);
2250 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002251 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002252 } else if (PyList_CheckExact(seq) &&
2253 PyList_GET_SIZE(seq) == oparg) {
2254 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002255 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002256 item = items[oparg];
2257 Py_INCREF(item);
2258 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002259 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002260 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002261 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002262 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002263 } else {
2264 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002265 Py_DECREF(seq);
2266 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002267 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002268 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002269 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002270 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002271
Benjamin Petersonddd19492018-09-16 22:38:02 -07002272 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002273 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2274 PyObject *seq = POP();
2275
Victor Stinner438a12d2019-05-24 17:01:38 +02002276 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002277 stack_pointer + totalargs)) {
2278 stack_pointer += totalargs;
2279 } else {
2280 Py_DECREF(seq);
2281 goto error;
2282 }
2283 Py_DECREF(seq);
2284 DISPATCH();
2285 }
2286
Benjamin Petersonddd19492018-09-16 22:38:02 -07002287 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002288 PyObject *name = GETITEM(names, oparg);
2289 PyObject *owner = TOP();
2290 PyObject *v = SECOND();
2291 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002292 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002293 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002294 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002295 Py_DECREF(owner);
2296 if (err != 0)
2297 goto error;
2298 DISPATCH();
2299 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002300
Benjamin Petersonddd19492018-09-16 22:38:02 -07002301 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002302 PyObject *name = GETITEM(names, oparg);
2303 PyObject *owner = POP();
2304 int err;
2305 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2306 Py_DECREF(owner);
2307 if (err != 0)
2308 goto error;
2309 DISPATCH();
2310 }
2311
Benjamin Petersonddd19492018-09-16 22:38:02 -07002312 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002313 PyObject *name = GETITEM(names, oparg);
2314 PyObject *v = POP();
2315 int err;
2316 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002317 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002318 if (err != 0)
2319 goto error;
2320 DISPATCH();
2321 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002322
Benjamin Petersonddd19492018-09-16 22:38:02 -07002323 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002324 PyObject *name = GETITEM(names, oparg);
2325 int err;
2326 err = PyDict_DelItem(f->f_globals, name);
2327 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002328 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2329 format_exc_check_arg(tstate, PyExc_NameError,
2330 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002331 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002332 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002333 }
2334 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002335 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002336
Benjamin Petersonddd19492018-09-16 22:38:02 -07002337 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002338 PyObject *name = GETITEM(names, oparg);
2339 PyObject *locals = f->f_locals;
2340 PyObject *v;
2341 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002342 _PyErr_Format(tstate, PyExc_SystemError,
2343 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002344 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002345 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002346 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002347 v = PyDict_GetItemWithError(locals, name);
2348 if (v != NULL) {
2349 Py_INCREF(v);
2350 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002351 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002352 goto error;
2353 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002354 }
2355 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002356 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002357 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002358 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002359 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002360 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002361 }
2362 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002363 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002364 v = PyDict_GetItemWithError(f->f_globals, name);
2365 if (v != NULL) {
2366 Py_INCREF(v);
2367 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002368 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002369 goto error;
2370 }
2371 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002372 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002373 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002374 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002375 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002376 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002377 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002378 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002379 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002380 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002381 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002382 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002383 }
2384 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002385 v = PyObject_GetItem(f->f_builtins, name);
2386 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002387 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002388 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002389 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002390 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002391 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002392 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002393 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002394 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002396 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002397 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002398 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002399 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002400
Benjamin Petersonddd19492018-09-16 22:38:02 -07002401 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002402 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002403 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002404 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002405 && PyDict_CheckExact(f->f_builtins))
2406 {
Inada Naoki91234a12019-06-03 21:30:58 +09002407 OPCACHE_CHECK();
2408 if (co_opcache != NULL && co_opcache->optimized > 0) {
2409 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2410
2411 if (lg->globals_ver ==
2412 ((PyDictObject *)f->f_globals)->ma_version_tag
2413 && lg->builtins_ver ==
2414 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2415 {
2416 PyObject *ptr = lg->ptr;
2417 OPCACHE_STAT_GLOBAL_HIT();
2418 assert(ptr != NULL);
2419 Py_INCREF(ptr);
2420 PUSH(ptr);
2421 DISPATCH();
2422 }
2423 }
2424
2425 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002426 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002427 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002428 name);
2429 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002430 if (!_PyErr_OCCURRED()) {
2431 /* _PyDict_LoadGlobal() returns NULL without raising
2432 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002433 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002434 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002435 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002436 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002437 }
Inada Naoki91234a12019-06-03 21:30:58 +09002438
2439 if (co_opcache != NULL) {
2440 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2441
2442 if (co_opcache->optimized == 0) {
2443 /* Wasn't optimized before. */
2444 OPCACHE_STAT_GLOBAL_OPT();
2445 } else {
2446 OPCACHE_STAT_GLOBAL_MISS();
2447 }
2448
2449 co_opcache->optimized = 1;
2450 lg->globals_ver =
2451 ((PyDictObject *)f->f_globals)->ma_version_tag;
2452 lg->builtins_ver =
2453 ((PyDictObject *)f->f_builtins)->ma_version_tag;
2454 lg->ptr = v; /* borrowed */
2455 }
2456
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002457 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002458 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002459 else {
2460 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002461
2462 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09002463 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002464 v = PyObject_GetItem(f->f_globals, name);
2465 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002466 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002467 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002468 }
2469 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002470
Victor Stinnerb4efc962015-11-20 09:24:02 +01002471 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002472 v = PyObject_GetItem(f->f_builtins, name);
2473 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002474 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002475 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002476 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002477 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002478 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002479 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002480 }
2481 }
2482 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002483 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002484 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002485 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002486
Benjamin Petersonddd19492018-09-16 22:38:02 -07002487 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002488 PyObject *v = GETLOCAL(oparg);
2489 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002490 SETLOCAL(oparg, NULL);
2491 DISPATCH();
2492 }
2493 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002494 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002495 UNBOUNDLOCAL_ERROR_MSG,
2496 PyTuple_GetItem(co->co_varnames, oparg)
2497 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002498 goto error;
2499 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002500
Benjamin Petersonddd19492018-09-16 22:38:02 -07002501 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002502 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002503 PyObject *oldobj = PyCell_GET(cell);
2504 if (oldobj != NULL) {
2505 PyCell_SET(cell, NULL);
2506 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002507 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002508 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002509 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002510 goto error;
2511 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002512
Benjamin Petersonddd19492018-09-16 22:38:02 -07002513 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002514 PyObject *cell = freevars[oparg];
2515 Py_INCREF(cell);
2516 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002517 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002518 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002519
Benjamin Petersonddd19492018-09-16 22:38:02 -07002520 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002521 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002522 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002523 assert(locals);
2524 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2525 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2526 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2527 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2528 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002529 value = PyDict_GetItemWithError(locals, name);
2530 if (value != NULL) {
2531 Py_INCREF(value);
2532 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002533 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002534 goto error;
2535 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002536 }
2537 else {
2538 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002539 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002540 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002541 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002542 }
2543 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002544 }
2545 }
2546 if (!value) {
2547 PyObject *cell = freevars[oparg];
2548 value = PyCell_GET(cell);
2549 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002550 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002551 goto error;
2552 }
2553 Py_INCREF(value);
2554 }
2555 PUSH(value);
2556 DISPATCH();
2557 }
2558
Benjamin Petersonddd19492018-09-16 22:38:02 -07002559 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002560 PyObject *cell = freevars[oparg];
2561 PyObject *value = PyCell_GET(cell);
2562 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002563 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002564 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002565 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002566 Py_INCREF(value);
2567 PUSH(value);
2568 DISPATCH();
2569 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002570
Benjamin Petersonddd19492018-09-16 22:38:02 -07002571 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002572 PyObject *v = POP();
2573 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002574 PyObject *oldobj = PyCell_GET(cell);
2575 PyCell_SET(cell, v);
2576 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002577 DISPATCH();
2578 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002579
Benjamin Petersonddd19492018-09-16 22:38:02 -07002580 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002581 PyObject *str;
2582 PyObject *empty = PyUnicode_New(0, 0);
2583 if (empty == NULL) {
2584 goto error;
2585 }
2586 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2587 Py_DECREF(empty);
2588 if (str == NULL)
2589 goto error;
2590 while (--oparg >= 0) {
2591 PyObject *item = POP();
2592 Py_DECREF(item);
2593 }
2594 PUSH(str);
2595 DISPATCH();
2596 }
2597
Benjamin Petersonddd19492018-09-16 22:38:02 -07002598 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002599 PyObject *tup = PyTuple_New(oparg);
2600 if (tup == NULL)
2601 goto error;
2602 while (--oparg >= 0) {
2603 PyObject *item = POP();
2604 PyTuple_SET_ITEM(tup, oparg, item);
2605 }
2606 PUSH(tup);
2607 DISPATCH();
2608 }
2609
Benjamin Petersonddd19492018-09-16 22:38:02 -07002610 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002611 PyObject *list = PyList_New(oparg);
2612 if (list == NULL)
2613 goto error;
2614 while (--oparg >= 0) {
2615 PyObject *item = POP();
2616 PyList_SET_ITEM(list, oparg, item);
2617 }
2618 PUSH(list);
2619 DISPATCH();
2620 }
2621
Mark Shannon13bc1392020-01-23 09:25:17 +00002622 case TARGET(LIST_TO_TUPLE): {
2623 PyObject *list = POP();
2624 PyObject *tuple = PyList_AsTuple(list);
2625 Py_DECREF(list);
2626 if (tuple == NULL) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002627 goto error;
Mark Shannon13bc1392020-01-23 09:25:17 +00002628 }
2629 PUSH(tuple);
2630 DISPATCH();
2631 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002632
Mark Shannon13bc1392020-01-23 09:25:17 +00002633 case TARGET(LIST_EXTEND): {
2634 PyObject *iterable = POP();
2635 PyObject *list = PEEK(oparg);
2636 PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
2637 if (none_val == NULL) {
2638 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01002639 (Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable)))
Mark Shannon13bc1392020-01-23 09:25:17 +00002640 {
Victor Stinner61f4db82020-01-28 03:37:45 +01002641 _PyErr_Clear(tstate);
Mark Shannon13bc1392020-01-23 09:25:17 +00002642 _PyErr_Format(tstate, PyExc_TypeError,
2643 "Value after * must be an iterable, not %.200s",
2644 Py_TYPE(iterable)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002645 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002646 Py_DECREF(iterable);
2647 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002648 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002649 Py_DECREF(none_val);
2650 Py_DECREF(iterable);
2651 DISPATCH();
2652 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002653
Mark Shannon13bc1392020-01-23 09:25:17 +00002654 case TARGET(SET_UPDATE): {
2655 PyObject *iterable = POP();
2656 PyObject *set = PEEK(oparg);
2657 int err = _PySet_Update(set, iterable);
2658 Py_DECREF(iterable);
2659 if (err < 0) {
2660 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002661 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002662 DISPATCH();
2663 }
2664
Benjamin Petersonddd19492018-09-16 22:38:02 -07002665 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002666 PyObject *set = PySet_New(NULL);
2667 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002668 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002669 if (set == NULL)
2670 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002671 for (i = oparg; i > 0; i--) {
2672 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002673 if (err == 0)
2674 err = PySet_Add(set, item);
2675 Py_DECREF(item);
2676 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002677 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002678 if (err != 0) {
2679 Py_DECREF(set);
2680 goto error;
2681 }
2682 PUSH(set);
2683 DISPATCH();
2684 }
2685
Benjamin Petersonddd19492018-09-16 22:38:02 -07002686 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002687 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002688 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2689 if (map == NULL)
2690 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002691 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002692 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002693 PyObject *key = PEEK(2*i);
2694 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002695 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002696 if (err != 0) {
2697 Py_DECREF(map);
2698 goto error;
2699 }
2700 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002701
2702 while (oparg--) {
2703 Py_DECREF(POP());
2704 Py_DECREF(POP());
2705 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002706 PUSH(map);
2707 DISPATCH();
2708 }
2709
Benjamin Petersonddd19492018-09-16 22:38:02 -07002710 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002711 _Py_IDENTIFIER(__annotations__);
2712 int err;
2713 PyObject *ann_dict;
2714 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002715 _PyErr_Format(tstate, PyExc_SystemError,
2716 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002717 goto error;
2718 }
2719 /* check if __annotations__ in locals()... */
2720 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002721 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002722 &PyId___annotations__);
2723 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002724 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002725 goto error;
2726 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002727 /* ...if not, create a new one */
2728 ann_dict = PyDict_New();
2729 if (ann_dict == NULL) {
2730 goto error;
2731 }
2732 err = _PyDict_SetItemId(f->f_locals,
2733 &PyId___annotations__, ann_dict);
2734 Py_DECREF(ann_dict);
2735 if (err != 0) {
2736 goto error;
2737 }
2738 }
2739 }
2740 else {
2741 /* do the same if locals() is not a dict */
2742 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2743 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002744 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002745 }
2746 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2747 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002748 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002749 goto error;
2750 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002751 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002752 ann_dict = PyDict_New();
2753 if (ann_dict == NULL) {
2754 goto error;
2755 }
2756 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2757 Py_DECREF(ann_dict);
2758 if (err != 0) {
2759 goto error;
2760 }
2761 }
2762 else {
2763 Py_DECREF(ann_dict);
2764 }
2765 }
2766 DISPATCH();
2767 }
2768
Benjamin Petersonddd19492018-09-16 22:38:02 -07002769 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002770 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002771 PyObject *map;
2772 PyObject *keys = TOP();
2773 if (!PyTuple_CheckExact(keys) ||
2774 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002775 _PyErr_SetString(tstate, PyExc_SystemError,
2776 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002777 goto error;
2778 }
2779 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2780 if (map == NULL) {
2781 goto error;
2782 }
2783 for (i = oparg; i > 0; i--) {
2784 int err;
2785 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2786 PyObject *value = PEEK(i + 1);
2787 err = PyDict_SetItem(map, key, value);
2788 if (err != 0) {
2789 Py_DECREF(map);
2790 goto error;
2791 }
2792 }
2793
2794 Py_DECREF(POP());
2795 while (oparg--) {
2796 Py_DECREF(POP());
2797 }
2798 PUSH(map);
2799 DISPATCH();
2800 }
2801
Mark Shannon8a4cd702020-01-27 09:57:45 +00002802 case TARGET(DICT_UPDATE): {
2803 PyObject *update = POP();
2804 PyObject *dict = PEEK(oparg);
2805 if (PyDict_Update(dict, update) < 0) {
2806 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
2807 _PyErr_Format(tstate, PyExc_TypeError,
2808 "'%.200s' object is not a mapping",
Victor Stinnera102ed72020-02-07 02:24:48 +01002809 Py_TYPE(update)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002810 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002811 Py_DECREF(update);
2812 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002813 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002814 Py_DECREF(update);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002815 DISPATCH();
2816 }
2817
Mark Shannon8a4cd702020-01-27 09:57:45 +00002818 case TARGET(DICT_MERGE): {
2819 PyObject *update = POP();
2820 PyObject *dict = PEEK(oparg);
2821
2822 if (_PyDict_MergeEx(dict, update, 2) < 0) {
2823 format_kwargs_error(tstate, PEEK(2 + oparg), update);
2824 Py_DECREF(update);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002825 goto error;
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002826 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002827 Py_DECREF(update);
Brandt Bucherf185a732019-09-28 17:12:49 -07002828 PREDICT(CALL_FUNCTION_EX);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002829 DISPATCH();
2830 }
2831
Benjamin Petersonddd19492018-09-16 22:38:02 -07002832 case TARGET(MAP_ADD): {
Jörn Heisslerc8a35412019-06-22 16:40:55 +02002833 PyObject *value = TOP();
2834 PyObject *key = SECOND();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002835 PyObject *map;
2836 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002837 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002838 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002839 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002840 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002841 Py_DECREF(value);
2842 Py_DECREF(key);
2843 if (err != 0)
2844 goto error;
2845 PREDICT(JUMP_ABSOLUTE);
2846 DISPATCH();
2847 }
2848
Benjamin Petersonddd19492018-09-16 22:38:02 -07002849 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002850 PyObject *name = GETITEM(names, oparg);
2851 PyObject *owner = TOP();
2852 PyObject *res = PyObject_GetAttr(owner, name);
2853 Py_DECREF(owner);
2854 SET_TOP(res);
2855 if (res == NULL)
2856 goto error;
2857 DISPATCH();
2858 }
2859
Benjamin Petersonddd19492018-09-16 22:38:02 -07002860 case TARGET(COMPARE_OP): {
Mark Shannon9af0e472020-01-14 10:12:45 +00002861 assert(oparg <= Py_GE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002862 PyObject *right = POP();
2863 PyObject *left = TOP();
Mark Shannon9af0e472020-01-14 10:12:45 +00002864 PyObject *res = PyObject_RichCompare(left, right, oparg);
2865 SET_TOP(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002866 Py_DECREF(left);
2867 Py_DECREF(right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002868 if (res == NULL)
2869 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002870 PREDICT(POP_JUMP_IF_FALSE);
2871 PREDICT(POP_JUMP_IF_TRUE);
2872 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002873 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002874
Mark Shannon9af0e472020-01-14 10:12:45 +00002875 case TARGET(IS_OP): {
2876 PyObject *right = POP();
2877 PyObject *left = TOP();
2878 int res = (left == right)^oparg;
2879 PyObject *b = res ? Py_True : Py_False;
2880 Py_INCREF(b);
2881 SET_TOP(b);
2882 Py_DECREF(left);
2883 Py_DECREF(right);
2884 PREDICT(POP_JUMP_IF_FALSE);
2885 PREDICT(POP_JUMP_IF_TRUE);
2886 FAST_DISPATCH();
2887 }
2888
2889 case TARGET(CONTAINS_OP): {
2890 PyObject *right = POP();
2891 PyObject *left = POP();
2892 int res = PySequence_Contains(right, left);
2893 Py_DECREF(left);
2894 Py_DECREF(right);
2895 if (res < 0) {
2896 goto error;
2897 }
2898 PyObject *b = (res^oparg) ? Py_True : Py_False;
2899 Py_INCREF(b);
2900 PUSH(b);
2901 PREDICT(POP_JUMP_IF_FALSE);
2902 PREDICT(POP_JUMP_IF_TRUE);
2903 FAST_DISPATCH();
2904 }
2905
2906#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
2907 "BaseException is not allowed"
2908
2909 case TARGET(JUMP_IF_NOT_EXC_MATCH): {
2910 PyObject *right = POP();
2911 PyObject *left = POP();
2912 if (PyTuple_Check(right)) {
2913 Py_ssize_t i, length;
2914 length = PyTuple_GET_SIZE(right);
2915 for (i = 0; i < length; i++) {
2916 PyObject *exc = PyTuple_GET_ITEM(right, i);
2917 if (!PyExceptionClass_Check(exc)) {
2918 _PyErr_SetString(tstate, PyExc_TypeError,
2919 CANNOT_CATCH_MSG);
2920 Py_DECREF(left);
2921 Py_DECREF(right);
2922 goto error;
2923 }
2924 }
2925 }
2926 else {
2927 if (!PyExceptionClass_Check(right)) {
2928 _PyErr_SetString(tstate, PyExc_TypeError,
2929 CANNOT_CATCH_MSG);
2930 Py_DECREF(left);
2931 Py_DECREF(right);
2932 goto error;
2933 }
2934 }
2935 int res = PyErr_GivenExceptionMatches(left, right);
2936 Py_DECREF(left);
2937 Py_DECREF(right);
2938 if (res > 0) {
2939 /* Exception matches -- Do nothing */;
2940 }
2941 else if (res == 0) {
2942 JUMPTO(oparg);
2943 }
2944 else {
2945 goto error;
2946 }
2947 DISPATCH();
2948 }
2949
Benjamin Petersonddd19492018-09-16 22:38:02 -07002950 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002951 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002952 PyObject *fromlist = POP();
2953 PyObject *level = TOP();
2954 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02002955 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002956 Py_DECREF(level);
2957 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002958 SET_TOP(res);
2959 if (res == NULL)
2960 goto error;
2961 DISPATCH();
2962 }
2963
Benjamin Petersonddd19492018-09-16 22:38:02 -07002964 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002965 PyObject *from = POP(), *locals;
2966 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002967 if (PyFrame_FastToLocalsWithError(f) < 0) {
2968 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002969 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002970 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002971
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002972 locals = f->f_locals;
2973 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002974 _PyErr_SetString(tstate, PyExc_SystemError,
2975 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002976 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002977 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002978 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002979 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002980 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002981 Py_DECREF(from);
2982 if (err != 0)
2983 goto error;
2984 DISPATCH();
2985 }
Guido van Rossum25831651993-05-19 14:50:45 +00002986
Benjamin Petersonddd19492018-09-16 22:38:02 -07002987 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002988 PyObject *name = GETITEM(names, oparg);
2989 PyObject *from = TOP();
2990 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02002991 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002992 PUSH(res);
2993 if (res == NULL)
2994 goto error;
2995 DISPATCH();
2996 }
Thomas Wouters52152252000-08-17 22:55:00 +00002997
Benjamin Petersonddd19492018-09-16 22:38:02 -07002998 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002999 JUMPBY(oparg);
3000 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003001 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003002
Benjamin Petersonddd19492018-09-16 22:38:02 -07003003 case TARGET(POP_JUMP_IF_FALSE): {
3004 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003005 PyObject *cond = POP();
3006 int err;
3007 if (cond == Py_True) {
3008 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003009 FAST_DISPATCH();
3010 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003011 if (cond == Py_False) {
3012 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003013 JUMPTO(oparg);
3014 FAST_DISPATCH();
3015 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003016 err = PyObject_IsTrue(cond);
3017 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003018 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003019 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003020 else if (err == 0)
3021 JUMPTO(oparg);
3022 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003023 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003024 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003025 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003026
Benjamin Petersonddd19492018-09-16 22:38:02 -07003027 case TARGET(POP_JUMP_IF_TRUE): {
3028 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003029 PyObject *cond = POP();
3030 int err;
3031 if (cond == Py_False) {
3032 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003033 FAST_DISPATCH();
3034 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003035 if (cond == Py_True) {
3036 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003037 JUMPTO(oparg);
3038 FAST_DISPATCH();
3039 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003040 err = PyObject_IsTrue(cond);
3041 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003042 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003043 JUMPTO(oparg);
3044 }
3045 else if (err == 0)
3046 ;
3047 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003048 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003049 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003050 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003051
Benjamin Petersonddd19492018-09-16 22:38:02 -07003052 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003053 PyObject *cond = TOP();
3054 int err;
3055 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003056 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003057 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003058 FAST_DISPATCH();
3059 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003060 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003061 JUMPTO(oparg);
3062 FAST_DISPATCH();
3063 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003064 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003065 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003066 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003067 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003068 }
3069 else if (err == 0)
3070 JUMPTO(oparg);
3071 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003072 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003073 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003074 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003075
Benjamin Petersonddd19492018-09-16 22:38:02 -07003076 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003077 PyObject *cond = TOP();
3078 int err;
3079 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003080 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003081 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003082 FAST_DISPATCH();
3083 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003084 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003085 JUMPTO(oparg);
3086 FAST_DISPATCH();
3087 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003088 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003089 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003090 JUMPTO(oparg);
3091 }
3092 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003093 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003094 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003095 }
3096 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003097 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003098 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003099 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003100
Benjamin Petersonddd19492018-09-16 22:38:02 -07003101 case TARGET(JUMP_ABSOLUTE): {
3102 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003103 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003104#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003105 /* Enabling this path speeds-up all while and for-loops by bypassing
3106 the per-loop checks for signals. By default, this should be turned-off
3107 because it prevents detection of a control-break in tight loops like
3108 "while 1: pass". Compile with this option turned-on when you need
3109 the speed-up and do not need break checking inside tight loops (ones
3110 that contain only instructions ending with FAST_DISPATCH).
3111 */
3112 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003113#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003114 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003115#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003116 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003117
Benjamin Petersonddd19492018-09-16 22:38:02 -07003118 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003119 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003120 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003121 PyObject *iter = PyObject_GetIter(iterable);
3122 Py_DECREF(iterable);
3123 SET_TOP(iter);
3124 if (iter == NULL)
3125 goto error;
3126 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003127 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003128 DISPATCH();
3129 }
3130
Benjamin Petersonddd19492018-09-16 22:38:02 -07003131 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04003132 /* before: [obj]; after [getiter(obj)] */
3133 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003134 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003135 if (PyCoro_CheckExact(iterable)) {
3136 /* `iterable` is a coroutine */
3137 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3138 /* and it is used in a 'yield from' expression of a
3139 regular generator. */
3140 Py_DECREF(iterable);
3141 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003142 _PyErr_SetString(tstate, PyExc_TypeError,
3143 "cannot 'yield from' a coroutine object "
3144 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04003145 goto error;
3146 }
3147 }
3148 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003149 /* `iterable` is not a generator. */
3150 iter = PyObject_GetIter(iterable);
3151 Py_DECREF(iterable);
3152 SET_TOP(iter);
3153 if (iter == NULL)
3154 goto error;
3155 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003156 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003157 DISPATCH();
3158 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003159
Benjamin Petersonddd19492018-09-16 22:38:02 -07003160 case TARGET(FOR_ITER): {
3161 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003162 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003163 PyObject *iter = TOP();
Victor Stinnera102ed72020-02-07 02:24:48 +01003164 PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003165 if (next != NULL) {
3166 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003167 PREDICT(STORE_FAST);
3168 PREDICT(UNPACK_SEQUENCE);
3169 DISPATCH();
3170 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003171 if (_PyErr_Occurred(tstate)) {
3172 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003173 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003174 }
3175 else if (tstate->c_tracefunc != NULL) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003176 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Victor Stinner438a12d2019-05-24 17:01:38 +02003177 }
3178 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003179 }
3180 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003181 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003182 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003183 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003184 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003185 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003186 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003187
Benjamin Petersonddd19492018-09-16 22:38:02 -07003188 case TARGET(SETUP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003189 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003190 STACK_LEVEL());
3191 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003192 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003193
Benjamin Petersonddd19492018-09-16 22:38:02 -07003194 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003195 _Py_IDENTIFIER(__aenter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003196 _Py_IDENTIFIER(__aexit__);
Yury Selivanov75445082015-05-11 22:57:16 -04003197 PyObject *mgr = TOP();
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003198 PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04003199 PyObject *res;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003200 if (enter == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003201 goto error;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003202 }
3203 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__);
3204 if (exit == NULL) {
3205 Py_DECREF(enter);
3206 goto error;
3207 }
Yury Selivanov75445082015-05-11 22:57:16 -04003208 SET_TOP(exit);
Yury Selivanov75445082015-05-11 22:57:16 -04003209 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003210 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003211 Py_DECREF(enter);
3212 if (res == NULL)
3213 goto error;
3214 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003215 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003216 DISPATCH();
3217 }
3218
Benjamin Petersonddd19492018-09-16 22:38:02 -07003219 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003220 PyObject *res = POP();
3221 /* Setup the finally block before pushing the result
3222 of __aenter__ on the stack. */
3223 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3224 STACK_LEVEL());
3225 PUSH(res);
3226 DISPATCH();
3227 }
3228
Benjamin Petersonddd19492018-09-16 22:38:02 -07003229 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003230 _Py_IDENTIFIER(__enter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003231 _Py_IDENTIFIER(__exit__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003232 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003233 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003234 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003235 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08003236 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003237 }
3238 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003239 if (exit == NULL) {
3240 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003241 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003242 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003243 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003244 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003245 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003246 Py_DECREF(enter);
3247 if (res == NULL)
3248 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003249 /* Setup the finally block before pushing the result
3250 of __enter__ on the stack. */
3251 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3252 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003253
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003254 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003255 DISPATCH();
3256 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003257
Mark Shannonfee55262019-11-21 09:11:43 +00003258 case TARGET(WITH_EXCEPT_START): {
3259 /* At the top of the stack are 7 values:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003260 - (TOP, SECOND, THIRD) = exc_info()
Mark Shannonfee55262019-11-21 09:11:43 +00003261 - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER
3262 - SEVENTH: the context.__exit__ bound method
3263 We call SEVENTH(TOP, SECOND, THIRD).
3264 Then we push again the TOP exception and the __exit__
3265 return value.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003266 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003267 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003268 PyObject *exc, *val, *tb, *res;
3269
Victor Stinner842cfff2016-12-01 14:45:31 +01003270 exc = TOP();
Mark Shannonfee55262019-11-21 09:11:43 +00003271 val = SECOND();
3272 tb = THIRD();
3273 assert(exc != Py_None);
3274 assert(!PyLong_Check(exc));
3275 exit_func = PEEK(7);
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003276 PyObject *stack[4] = {NULL, exc, val, tb};
Petr Viktorinffd97532020-02-11 17:46:57 +01003277 res = PyObject_Vectorcall(exit_func, stack + 1,
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003278 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003279 if (res == NULL)
3280 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003281
Yury Selivanov75445082015-05-11 22:57:16 -04003282 PUSH(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003283 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003284 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003285
Benjamin Petersonddd19492018-09-16 22:38:02 -07003286 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10003287 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003288 PyObject *name = GETITEM(names, oparg);
3289 PyObject *obj = TOP();
3290 PyObject *meth = NULL;
3291
3292 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3293
Yury Selivanovf2392132016-12-13 19:03:51 -05003294 if (meth == NULL) {
3295 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003296 goto error;
3297 }
3298
3299 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003300 /* We can bypass temporary bound method object.
3301 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003302
INADA Naoki015bce62017-01-16 17:23:30 +09003303 meth | self | arg1 | ... | argN
3304 */
3305 SET_TOP(meth);
3306 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003307 }
3308 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003309 /* meth is not an unbound method (but a regular attr, or
3310 something was returned by a descriptor protocol). Set
3311 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003312 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003313
3314 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003315 */
INADA Naoki015bce62017-01-16 17:23:30 +09003316 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003317 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003318 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003319 }
3320 DISPATCH();
3321 }
3322
Benjamin Petersonddd19492018-09-16 22:38:02 -07003323 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003324 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003325 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003326
3327 sp = stack_pointer;
3328
INADA Naoki015bce62017-01-16 17:23:30 +09003329 meth = PEEK(oparg + 2);
3330 if (meth == NULL) {
3331 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3332 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003333
3334 Stack layout:
3335
INADA Naoki015bce62017-01-16 17:23:30 +09003336 ... | NULL | callable | arg1 | ... | argN
3337 ^- TOP()
3338 ^- (-oparg)
3339 ^- (-oparg-1)
3340 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003341
Ville Skyttä49b27342017-08-03 09:00:59 +03003342 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003343 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003344 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003345 res = call_function(tstate, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003346 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003347 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003348 }
3349 else {
3350 /* This is a method call. Stack layout:
3351
INADA Naoki015bce62017-01-16 17:23:30 +09003352 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003353 ^- TOP()
3354 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003355 ^- (-oparg-1)
3356 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003357
INADA Naoki015bce62017-01-16 17:23:30 +09003358 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003359 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003360 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003361 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003362 res = call_function(tstate, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003363 stack_pointer = sp;
3364 }
3365
3366 PUSH(res);
3367 if (res == NULL)
3368 goto error;
3369 DISPATCH();
3370 }
3371
Benjamin Petersonddd19492018-09-16 22:38:02 -07003372 case TARGET(CALL_FUNCTION): {
3373 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003374 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003375 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003376 res = call_function(tstate, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003377 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003378 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003379 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003380 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003381 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003382 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003383 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003384
Benjamin Petersonddd19492018-09-16 22:38:02 -07003385 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003386 PyObject **sp, *res, *names;
3387
3388 names = POP();
Jeroen Demeyer05677862019-08-16 12:41:27 +02003389 assert(PyTuple_Check(names));
3390 assert(PyTuple_GET_SIZE(names) <= oparg);
3391 /* We assume without checking that names contains only strings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003392 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003393 res = call_function(tstate, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003394 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003395 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003396 Py_DECREF(names);
3397
3398 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003399 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003400 }
3401 DISPATCH();
3402 }
3403
Benjamin Petersonddd19492018-09-16 22:38:02 -07003404 case TARGET(CALL_FUNCTION_EX): {
Brandt Bucherf185a732019-09-28 17:12:49 -07003405 PREDICTED(CALL_FUNCTION_EX);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003406 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003407 if (oparg & 0x01) {
3408 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003409 if (!PyDict_CheckExact(kwargs)) {
3410 PyObject *d = PyDict_New();
3411 if (d == NULL)
3412 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003413 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003414 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02003415 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003416 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003417 goto error;
3418 }
3419 Py_DECREF(kwargs);
3420 kwargs = d;
3421 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003422 assert(PyDict_CheckExact(kwargs));
3423 }
3424 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003425 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003426 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003427 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003428 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003429 goto error;
3430 }
3431 Py_SETREF(callargs, PySequence_Tuple(callargs));
3432 if (callargs == NULL) {
3433 goto error;
3434 }
3435 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003436 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003437
Victor Stinner09532fe2019-05-10 23:39:09 +02003438 result = do_call_core(tstate, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003439 Py_DECREF(func);
3440 Py_DECREF(callargs);
3441 Py_XDECREF(kwargs);
3442
3443 SET_TOP(result);
3444 if (result == NULL) {
3445 goto error;
3446 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003447 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003448 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003449
Benjamin Petersonddd19492018-09-16 22:38:02 -07003450 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003451 PyObject *qualname = POP();
3452 PyObject *codeobj = POP();
3453 PyFunctionObject *func = (PyFunctionObject *)
3454 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003455
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003456 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003457 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003458 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003459 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003460 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003461
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003462 if (oparg & 0x08) {
3463 assert(PyTuple_CheckExact(TOP()));
3464 func ->func_closure = POP();
3465 }
3466 if (oparg & 0x04) {
3467 assert(PyDict_CheckExact(TOP()));
3468 func->func_annotations = POP();
3469 }
3470 if (oparg & 0x02) {
3471 assert(PyDict_CheckExact(TOP()));
3472 func->func_kwdefaults = POP();
3473 }
3474 if (oparg & 0x01) {
3475 assert(PyTuple_CheckExact(TOP()));
3476 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003477 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003478
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003479 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003480 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003481 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003482
Benjamin Petersonddd19492018-09-16 22:38:02 -07003483 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003484 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003485 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003486 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003487 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003488 step = NULL;
3489 stop = POP();
3490 start = TOP();
3491 slice = PySlice_New(start, stop, step);
3492 Py_DECREF(start);
3493 Py_DECREF(stop);
3494 Py_XDECREF(step);
3495 SET_TOP(slice);
3496 if (slice == NULL)
3497 goto error;
3498 DISPATCH();
3499 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003500
Benjamin Petersonddd19492018-09-16 22:38:02 -07003501 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003502 /* Handles f-string value formatting. */
3503 PyObject *result;
3504 PyObject *fmt_spec;
3505 PyObject *value;
3506 PyObject *(*conv_fn)(PyObject *);
3507 int which_conversion = oparg & FVC_MASK;
3508 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3509
3510 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003511 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003512
3513 /* See if any conversion is specified. */
3514 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003515 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003516 case FVC_STR: conv_fn = PyObject_Str; break;
3517 case FVC_REPR: conv_fn = PyObject_Repr; break;
3518 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003519 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02003520 _PyErr_Format(tstate, PyExc_SystemError,
3521 "unexpected conversion flag %d",
3522 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003523 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003524 }
3525
3526 /* If there's a conversion function, call it and replace
3527 value with that result. Otherwise, just use value,
3528 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003529 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003530 result = conv_fn(value);
3531 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003532 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003533 Py_XDECREF(fmt_spec);
3534 goto error;
3535 }
3536 value = result;
3537 }
3538
3539 /* If value is a unicode object, and there's no fmt_spec,
3540 then we know the result of format(value) is value
3541 itself. In that case, skip calling format(). I plan to
3542 move this optimization in to PyObject_Format()
3543 itself. */
3544 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3545 /* Do nothing, just transfer ownership to result. */
3546 result = value;
3547 } else {
3548 /* Actually call format(). */
3549 result = PyObject_Format(value, fmt_spec);
3550 Py_DECREF(value);
3551 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003552 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003553 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003554 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003555 }
3556
Eric V. Smith135d5f42016-02-05 18:23:08 -05003557 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003558 DISPATCH();
3559 }
3560
Benjamin Petersonddd19492018-09-16 22:38:02 -07003561 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003562 int oldoparg = oparg;
3563 NEXTOPARG();
3564 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003565 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003566 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003567
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003568
Antoine Pitrou042b1282010-08-13 21:15:58 +00003569#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003570 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003571#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003572 default:
3573 fprintf(stderr,
3574 "XXX lineno: %d, opcode: %d\n",
3575 PyFrame_GetLineNumber(f),
3576 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02003577 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003578 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003580 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003581
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003582 /* This should never be reached. Every opcode should end with DISPATCH()
3583 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003584 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003585
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003586error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003587 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003588#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02003589 if (!_PyErr_Occurred(tstate)) {
3590 _PyErr_SetString(tstate, PyExc_SystemError,
3591 "error return without exception set");
3592 }
Victor Stinner365b6932013-07-12 00:11:58 +02003593#else
Victor Stinner438a12d2019-05-24 17:01:38 +02003594 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02003595#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003596
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003597 /* Log traceback info. */
3598 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003599
Benjamin Peterson51f46162013-01-23 08:38:47 -05003600 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003601 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3602 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003603
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003604exception_unwind:
3605 /* Unwind stacks if an exception occurred */
3606 while (f->f_iblock > 0) {
3607 /* Pop the current block. */
3608 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003609
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003610 if (b->b_type == EXCEPT_HANDLER) {
3611 UNWIND_EXCEPT_HANDLER(b);
3612 continue;
3613 }
3614 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003615 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003616 PyObject *exc, *val, *tb;
3617 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003618 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003619 /* Beware, this invalidates all b->b_* fields */
3620 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003621 PUSH(exc_info->exc_traceback);
3622 PUSH(exc_info->exc_value);
3623 if (exc_info->exc_type != NULL) {
3624 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003625 }
3626 else {
3627 Py_INCREF(Py_None);
3628 PUSH(Py_None);
3629 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003630 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003631 /* Make the raw exception data
3632 available to the handler,
3633 so a program can emulate the
3634 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02003635 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003636 if (tb != NULL)
3637 PyException_SetTraceback(val, tb);
3638 else
3639 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003640 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003641 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003642 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003643 exc_info->exc_value = val;
3644 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003645 if (tb == NULL)
3646 tb = Py_None;
3647 Py_INCREF(tb);
3648 PUSH(tb);
3649 PUSH(val);
3650 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003651 JUMPTO(handler);
Pablo Galindo4c53e632020-01-10 09:24:22 +00003652 if (_Py_TracingPossible(ceval)) {
3653 int needs_new_execution_window = (f->f_lasti < instr_lb || f->f_lasti >= instr_ub);
3654 int needs_line_update = (f->f_lasti == instr_lb || f->f_lasti < instr_prev);
3655 /* Make sure that we trace line after exception if we are in a new execution
3656 * window or we don't need a line update and we are not in the first instruction
3657 * of the line. */
3658 if (needs_new_execution_window || (!needs_line_update && instr_lb > 0)) {
3659 instr_prev = INT_MAX;
3660 }
Mark Shannonfee55262019-11-21 09:11:43 +00003661 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003662 /* Resume normal execution */
3663 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003664 }
3665 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003666
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003667 /* End the loop as we still have an error */
3668 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003669 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003670
Pablo Galindof00828a2019-05-09 16:52:02 +01003671 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003672 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01003673
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003674 /* Pop remaining stack entries. */
3675 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003676 PyObject *o = POP();
3677 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003678 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003679
Mark Shannone7c9f4a2020-01-13 12:51:26 +00003680exiting:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003681 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003682 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003683 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3684 tstate, f, PyTrace_RETURN, retval)) {
3685 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003686 }
3687 }
3688 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003689 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3690 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003691 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003692 }
3693 }
3694 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003695
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003696 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003697exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003698 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3699 dtrace_function_return(f);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01003700 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrou58720d62013-08-05 23:26:40 +02003701 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003702 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003703
Victor Stinner17269092019-11-05 01:22:12 +01003704 return _Py_CheckFunctionResult(tstate, NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003705}
3706
Benjamin Petersonb204a422011-06-05 22:04:07 -05003707static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003708format_missing(PyThreadState *tstate, const char *kind,
3709 PyCodeObject *co, PyObject *names)
Benjamin Petersone109c702011-06-24 09:37:26 -05003710{
3711 int err;
3712 Py_ssize_t len = PyList_GET_SIZE(names);
3713 PyObject *name_str, *comma, *tail, *tmp;
3714
3715 assert(PyList_CheckExact(names));
3716 assert(len >= 1);
3717 /* Deal with the joys of natural language. */
3718 switch (len) {
3719 case 1:
3720 name_str = PyList_GET_ITEM(names, 0);
3721 Py_INCREF(name_str);
3722 break;
3723 case 2:
3724 name_str = PyUnicode_FromFormat("%U and %U",
3725 PyList_GET_ITEM(names, len - 2),
3726 PyList_GET_ITEM(names, len - 1));
3727 break;
3728 default:
3729 tail = PyUnicode_FromFormat(", %U, and %U",
3730 PyList_GET_ITEM(names, len - 2),
3731 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003732 if (tail == NULL)
3733 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003734 /* Chop off the last two objects in the list. This shouldn't actually
3735 fail, but we can't be too careful. */
3736 err = PyList_SetSlice(names, len - 2, len, NULL);
3737 if (err == -1) {
3738 Py_DECREF(tail);
3739 return;
3740 }
3741 /* Stitch everything up into a nice comma-separated list. */
3742 comma = PyUnicode_FromString(", ");
3743 if (comma == NULL) {
3744 Py_DECREF(tail);
3745 return;
3746 }
3747 tmp = PyUnicode_Join(comma, names);
3748 Py_DECREF(comma);
3749 if (tmp == NULL) {
3750 Py_DECREF(tail);
3751 return;
3752 }
3753 name_str = PyUnicode_Concat(tmp, tail);
3754 Py_DECREF(tmp);
3755 Py_DECREF(tail);
3756 break;
3757 }
3758 if (name_str == NULL)
3759 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02003760 _PyErr_Format(tstate, PyExc_TypeError,
3761 "%U() missing %i required %s argument%s: %U",
3762 co->co_name,
3763 len,
3764 kind,
3765 len == 1 ? "" : "s",
3766 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05003767 Py_DECREF(name_str);
3768}
3769
3770static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003771missing_arguments(PyThreadState *tstate, PyCodeObject *co,
3772 Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003773 PyObject **fastlocals)
3774{
Victor Stinner74319ae2016-08-25 00:04:09 +02003775 Py_ssize_t i, j = 0;
3776 Py_ssize_t start, end;
3777 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003778 const char *kind = positional ? "positional" : "keyword-only";
3779 PyObject *missing_names;
3780
3781 /* Compute the names of the arguments that are missing. */
3782 missing_names = PyList_New(missing);
3783 if (missing_names == NULL)
3784 return;
3785 if (positional) {
3786 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01003787 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003788 }
3789 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003790 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003791 end = start + co->co_kwonlyargcount;
3792 }
3793 for (i = start; i < end; i++) {
3794 if (GETLOCAL(i) == NULL) {
3795 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3796 PyObject *name = PyObject_Repr(raw);
3797 if (name == NULL) {
3798 Py_DECREF(missing_names);
3799 return;
3800 }
3801 PyList_SET_ITEM(missing_names, j++, name);
3802 }
3803 }
3804 assert(j == missing);
Victor Stinner438a12d2019-05-24 17:01:38 +02003805 format_missing(tstate, kind, co, missing_names);
Benjamin Petersone109c702011-06-24 09:37:26 -05003806 Py_DECREF(missing_names);
3807}
3808
3809static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003810too_many_positional(PyThreadState *tstate, PyCodeObject *co,
3811 Py_ssize_t given, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003812 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003813{
3814 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003815 Py_ssize_t kwonly_given = 0;
3816 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003817 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003818 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003819
Benjamin Petersone109c702011-06-24 09:37:26 -05003820 assert((co->co_flags & CO_VARARGS) == 0);
3821 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01003822 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003823 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003824 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003825 }
3826 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003827 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01003828 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003829 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01003830 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003831 }
3832 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003833 plural = (co_argcount != 1);
3834 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003835 }
3836 if (sig == NULL)
3837 return;
3838 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003839 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3840 kwonly_sig = PyUnicode_FromFormat(format,
3841 given != 1 ? "s" : "",
3842 kwonly_given,
3843 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003844 if (kwonly_sig == NULL) {
3845 Py_DECREF(sig);
3846 return;
3847 }
3848 }
3849 else {
3850 /* This will not fail. */
3851 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003852 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003853 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003854 _PyErr_Format(tstate, PyExc_TypeError,
3855 "%U() takes %U positional argument%s but %zd%U %s given",
3856 co->co_name,
3857 sig,
3858 plural ? "s" : "",
3859 given,
3860 kwonly_sig,
3861 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003862 Py_DECREF(sig);
3863 Py_DECREF(kwonly_sig);
3864}
3865
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003866static int
Victor Stinner438a12d2019-05-24 17:01:38 +02003867positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
3868 Py_ssize_t kwcount, PyObject* const* kwnames)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003869{
3870 int posonly_conflicts = 0;
3871 PyObject* posonly_names = PyList_New(0);
3872
3873 for(int k=0; k < co->co_posonlyargcount; k++){
3874 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
3875
3876 for (int k2=0; k2<kwcount; k2++){
3877 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
3878 PyObject* kwname = kwnames[k2];
3879 if (kwname == posonly_name){
3880 if(PyList_Append(posonly_names, kwname) != 0) {
3881 goto fail;
3882 }
3883 posonly_conflicts++;
3884 continue;
3885 }
3886
3887 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
3888
3889 if ( cmp > 0) {
3890 if(PyList_Append(posonly_names, kwname) != 0) {
3891 goto fail;
3892 }
3893 posonly_conflicts++;
3894 } else if (cmp < 0) {
3895 goto fail;
3896 }
3897
3898 }
3899 }
3900 if (posonly_conflicts) {
3901 PyObject* comma = PyUnicode_FromString(", ");
3902 if (comma == NULL) {
3903 goto fail;
3904 }
3905 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
3906 Py_DECREF(comma);
3907 if (error_names == NULL) {
3908 goto fail;
3909 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003910 _PyErr_Format(tstate, PyExc_TypeError,
3911 "%U() got some positional-only arguments passed"
3912 " as keyword arguments: '%U'",
3913 co->co_name, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003914 Py_DECREF(error_names);
3915 goto fail;
3916 }
3917
3918 Py_DECREF(posonly_names);
3919 return 0;
3920
3921fail:
3922 Py_XDECREF(posonly_names);
3923 return 1;
3924
3925}
3926
Guido van Rossumc2e20742006-02-27 22:32:47 +00003927/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02003928 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003929 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003930
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003931PyObject *
Victor Stinnerb5e170f2019-11-16 01:03:22 +01003932_PyEval_EvalCode(PyThreadState *tstate,
3933 PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003934 PyObject *const *args, Py_ssize_t argcount,
3935 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003936 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003937 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003938 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003939 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003940{
Victor Stinnerb5e170f2019-11-16 01:03:22 +01003941 assert(tstate != NULL);
3942
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003943 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003944 PyFrameObject *f;
3945 PyObject *retval = NULL;
3946 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003947 PyObject *x, *u;
Pablo Galindocd74e662019-06-01 18:08:04 +01003948 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003949 Py_ssize_t i, j, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003950 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003952 if (globals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003953 _PyErr_SetString(tstate, PyExc_SystemError,
3954 "PyEval_EvalCodeEx: NULL globals");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003955 return NULL;
3956 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003957
Victor Stinnerc7020012016-08-16 23:40:29 +02003958 /* Create the frame */
INADA Naoki5a625d02016-12-24 20:19:08 +09003959 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003960 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003961 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003962 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003963 fastlocals = f->f_localsplus;
3964 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003965
Victor Stinnerc7020012016-08-16 23:40:29 +02003966 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003967 if (co->co_flags & CO_VARKEYWORDS) {
3968 kwdict = PyDict_New();
3969 if (kwdict == NULL)
3970 goto fail;
3971 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003972 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003973 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003974 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003975 SETLOCAL(i, kwdict);
3976 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003977 else {
3978 kwdict = NULL;
3979 }
3980
Pablo Galindocd74e662019-06-01 18:08:04 +01003981 /* Copy all positional arguments into local variables */
3982 if (argcount > co->co_argcount) {
3983 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003984 }
3985 else {
3986 n = argcount;
3987 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003988 for (j = 0; j < n; j++) {
3989 x = args[j];
3990 Py_INCREF(x);
3991 SETLOCAL(j, x);
3992 }
3993
Victor Stinnerc7020012016-08-16 23:40:29 +02003994 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003995 if (co->co_flags & CO_VARARGS) {
Sergey Fedoseev234531b2019-02-25 21:59:12 +05003996 u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003997 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003998 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003999 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004000 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004001 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004002
Serhiy Storchakab7281052016-09-12 00:52:40 +03004003 /* Handle keyword arguments passed as two strided arrays */
4004 kwcount *= kwstep;
4005 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004006 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03004007 PyObject *keyword = kwnames[i];
4008 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02004009 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004010
Benjamin Petersonb204a422011-06-05 22:04:07 -05004011 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004012 _PyErr_Format(tstate, PyExc_TypeError,
4013 "%U() keywords must be strings",
4014 co->co_name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004015 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004016 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004017
Benjamin Petersonb204a422011-06-05 22:04:07 -05004018 /* Speed hack: do raw pointer compares. As names are
4019 normally interned this should almost always hit. */
4020 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004021 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004022 PyObject *name = co_varnames[j];
4023 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004024 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004025 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004026 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004027
Benjamin Petersonb204a422011-06-05 22:04:07 -05004028 /* Slow fallback, just in case */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004029 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004030 PyObject *name = co_varnames[j];
4031 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
4032 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004033 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004034 }
4035 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004036 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004037 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004038 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004039
Victor Stinner231d1f32017-01-11 02:12:06 +01004040 assert(j >= total_args);
4041 if (kwdict == NULL) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004042
Victor Stinner438a12d2019-05-24 17:01:38 +02004043 if (co->co_posonlyargcount
4044 && positional_only_passed_as_keyword(tstate, co,
4045 kwcount, kwnames))
4046 {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004047 goto fail;
4048 }
4049
Victor Stinner438a12d2019-05-24 17:01:38 +02004050 _PyErr_Format(tstate, PyExc_TypeError,
4051 "%U() got an unexpected keyword argument '%S'",
4052 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004053 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004054 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004055
Christian Heimes0bd447f2013-07-20 14:48:10 +02004056 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4057 goto fail;
4058 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004059 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004060
Benjamin Petersonb204a422011-06-05 22:04:07 -05004061 kw_found:
4062 if (GETLOCAL(j) != NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004063 _PyErr_Format(tstate, PyExc_TypeError,
4064 "%U() got multiple values for argument '%S'",
4065 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004066 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004067 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004068 Py_INCREF(value);
4069 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004070 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004071
4072 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004073 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004074 too_many_positional(tstate, co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004075 goto fail;
4076 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004077
4078 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004079 if (argcount < co->co_argcount) {
4080 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004081 Py_ssize_t missing = 0;
4082 for (i = argcount; i < m; i++) {
4083 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004084 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004085 }
4086 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004087 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004088 missing_arguments(tstate, co, missing, defcount, fastlocals);
Benjamin Petersone109c702011-06-24 09:37:26 -05004089 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004090 }
4091 if (n > m)
4092 i = n - m;
4093 else
4094 i = 0;
4095 for (; i < defcount; i++) {
4096 if (GETLOCAL(m+i) == NULL) {
4097 PyObject *def = defs[i];
4098 Py_INCREF(def);
4099 SETLOCAL(m+i, def);
4100 }
4101 }
4102 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004103
4104 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004105 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004106 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004107 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004108 PyObject *name;
4109 if (GETLOCAL(i) != NULL)
4110 continue;
4111 name = PyTuple_GET_ITEM(co->co_varnames, i);
4112 if (kwdefs != NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004113 PyObject *def = PyDict_GetItemWithError(kwdefs, name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004114 if (def) {
4115 Py_INCREF(def);
4116 SETLOCAL(i, def);
4117 continue;
4118 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004119 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004120 goto fail;
4121 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004122 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004123 missing++;
4124 }
4125 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004126 missing_arguments(tstate, co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004127 goto fail;
4128 }
4129 }
4130
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004131 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004132 vars into frame. */
4133 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004134 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004135 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004136 /* Possibly account for the cell variable being an argument. */
4137 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004138 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004139 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004140 /* Clear the local copy. */
4141 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004142 }
4143 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004144 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004145 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004146 if (c == NULL)
4147 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004148 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004149 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004150
4151 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004152 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4153 PyObject *o = PyTuple_GET_ITEM(closure, i);
4154 Py_INCREF(o);
4155 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004156 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004157
Yury Selivanoveb636452016-09-08 22:01:51 -07004158 /* Handle generator/coroutine/asynchronous generator */
4159 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004160 PyObject *gen;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004161 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004162
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004163 /* Don't need to keep the reference to f_back, it will be set
4164 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004165 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004167 /* Create a new generator that owns the ready to run frame
4168 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004169 if (is_coro) {
4170 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004171 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4172 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004173 } else {
4174 gen = PyGen_NewWithQualName(f, name, qualname);
4175 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004176 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004177 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004178 }
INADA Naoki9c157762016-12-26 18:52:46 +09004179
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004180 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004181
Yury Selivanov75445082015-05-11 22:57:16 -04004182 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004183 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004184
Victor Stinnerb9e68122019-11-14 12:20:46 +01004185 retval = _PyEval_EvalFrame(tstate, f, 0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004186
Thomas Woutersce272b62007-09-19 21:19:28 +00004187fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004189 /* decref'ing the frame can cause __del__ methods to get invoked,
4190 which can call back into Python. While we're done with the
4191 current Python frame (f), the associated C stack is still in use,
4192 so recursion_depth must be boosted for the duration.
4193 */
INADA Naoki5a625d02016-12-24 20:19:08 +09004194 if (Py_REFCNT(f) > 1) {
4195 Py_DECREF(f);
4196 _PyObject_GC_TRACK(f);
4197 }
4198 else {
4199 ++tstate->recursion_depth;
4200 Py_DECREF(f);
4201 --tstate->recursion_depth;
4202 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004203 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004204}
4205
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004206
4207PyObject *
4208_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
4209 PyObject *const *args, Py_ssize_t argcount,
4210 PyObject *const *kwnames, PyObject *const *kwargs,
4211 Py_ssize_t kwcount, int kwstep,
4212 PyObject *const *defs, Py_ssize_t defcount,
4213 PyObject *kwdefs, PyObject *closure,
4214 PyObject *name, PyObject *qualname)
4215{
4216 PyThreadState *tstate = _PyThreadState_GET();
4217 return _PyEval_EvalCode(tstate, _co, globals, locals,
4218 args, argcount,
4219 kwnames, kwargs,
4220 kwcount, kwstep,
4221 defs, defcount,
4222 kwdefs, closure,
4223 name, qualname);
4224}
4225
Victor Stinner40ee3012014-06-16 15:59:28 +02004226PyObject *
4227PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004228 PyObject *const *args, int argcount,
4229 PyObject *const *kws, int kwcount,
4230 PyObject *const *defs, int defcount,
4231 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004232{
4233 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004234 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004235 kws, kws != NULL ? kws + 1 : NULL,
4236 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004237 defs, defcount,
4238 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004239 NULL, NULL);
4240}
Tim Peters5ca576e2001-06-18 22:08:13 +00004241
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004242static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004243special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004244{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004245 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004246 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02004247 if (res == NULL && !_PyErr_Occurred(tstate)) {
4248 _PyErr_SetObject(tstate, PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004249 return NULL;
4250 }
4251 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004252}
4253
4254
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004255/* Logic for the raise statement (too complicated for inlining).
4256 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004257static int
Victor Stinner09532fe2019-05-10 23:39:09 +02004258do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004259{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004260 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004261
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004262 if (exc == NULL) {
4263 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01004264 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004265 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004266 type = exc_info->exc_type;
4267 value = exc_info->exc_value;
4268 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004269 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004270 _PyErr_SetString(tstate, PyExc_RuntimeError,
4271 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004272 return 0;
4273 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004274 Py_XINCREF(type);
4275 Py_XINCREF(value);
4276 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02004277 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004278 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004279 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004281 /* We support the following forms of raise:
4282 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004283 raise <instance>
4284 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004285
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004286 if (PyExceptionClass_Check(exc)) {
4287 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004288 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004289 if (value == NULL)
4290 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004291 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004292 _PyErr_Format(tstate, PyExc_TypeError,
4293 "calling %R should have returned an instance of "
4294 "BaseException, not %R",
4295 type, Py_TYPE(value));
4296 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004297 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004298 }
4299 else if (PyExceptionInstance_Check(exc)) {
4300 value = exc;
4301 type = PyExceptionInstance_Class(exc);
4302 Py_INCREF(type);
4303 }
4304 else {
4305 /* Not something you can raise. You get an exception
4306 anyway, just not what you specified :-) */
4307 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02004308 _PyErr_SetString(tstate, PyExc_TypeError,
4309 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004310 goto raise_error;
4311 }
Collin Winter828f04a2007-08-31 00:04:24 +00004312
Serhiy Storchakac0191582016-09-27 11:37:10 +03004313 assert(type != NULL);
4314 assert(value != NULL);
4315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004316 if (cause) {
4317 PyObject *fixed_cause;
4318 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004319 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004320 if (fixed_cause == NULL)
4321 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004322 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004323 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004324 else if (PyExceptionInstance_Check(cause)) {
4325 fixed_cause = cause;
4326 }
4327 else if (cause == Py_None) {
4328 Py_DECREF(cause);
4329 fixed_cause = NULL;
4330 }
4331 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004332 _PyErr_SetString(tstate, PyExc_TypeError,
4333 "exception causes must derive from "
4334 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004335 goto raise_error;
4336 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004337 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004338 }
Collin Winter828f04a2007-08-31 00:04:24 +00004339
Victor Stinner438a12d2019-05-24 17:01:38 +02004340 _PyErr_SetObject(tstate, type, value);
Victor Stinner61f4db82020-01-28 03:37:45 +01004341 /* _PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004342 Py_DECREF(value);
4343 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004344 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004345
4346raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004347 Py_XDECREF(value);
4348 Py_XDECREF(type);
4349 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004350 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004351}
4352
Tim Petersd6d010b2001-06-21 02:49:55 +00004353/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004354 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004355
Guido van Rossum0368b722007-05-11 16:50:42 +00004356 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4357 with a variable target.
4358*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004359
Barry Warsawe42b18f1997-08-25 22:13:04 +00004360static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004361unpack_iterable(PyThreadState *tstate, PyObject *v,
4362 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004363{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004364 int i = 0, j = 0;
4365 Py_ssize_t ll = 0;
4366 PyObject *it; /* iter(v) */
4367 PyObject *w;
4368 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004370 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004372 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004373 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004374 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01004375 Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v))
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004376 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004377 _PyErr_Format(tstate, PyExc_TypeError,
4378 "cannot unpack non-iterable %.200s object",
Victor Stinnera102ed72020-02-07 02:24:48 +01004379 Py_TYPE(v)->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004380 }
4381 return 0;
4382 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004384 for (; i < argcnt; i++) {
4385 w = PyIter_Next(it);
4386 if (w == NULL) {
4387 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004388 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04004389 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004390 _PyErr_Format(tstate, PyExc_ValueError,
4391 "not enough values to unpack "
4392 "(expected %d, got %d)",
4393 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004394 }
4395 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004396 _PyErr_Format(tstate, PyExc_ValueError,
4397 "not enough values to unpack "
4398 "(expected at least %d, got %d)",
4399 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004400 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004401 }
4402 goto Error;
4403 }
4404 *--sp = w;
4405 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004407 if (argcntafter == -1) {
4408 /* We better have exhausted the iterator now. */
4409 w = PyIter_Next(it);
4410 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004411 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004412 goto Error;
4413 Py_DECREF(it);
4414 return 1;
4415 }
4416 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02004417 _PyErr_Format(tstate, PyExc_ValueError,
4418 "too many values to unpack (expected %d)",
4419 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004420 goto Error;
4421 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004423 l = PySequence_List(it);
4424 if (l == NULL)
4425 goto Error;
4426 *--sp = l;
4427 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004429 ll = PyList_GET_SIZE(l);
4430 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004431 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04004432 "not enough values to unpack (expected at least %d, got %zd)",
4433 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004434 goto Error;
4435 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004437 /* Pop the "after-variable" args off the list. */
4438 for (j = argcntafter; j > 0; j--, i++) {
4439 *--sp = PyList_GET_ITEM(l, ll - j);
4440 }
4441 /* Resize the list. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +01004442 Py_SET_SIZE(l, ll - argcntafter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004443 Py_DECREF(it);
4444 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004445
Tim Petersd6d010b2001-06-21 02:49:55 +00004446Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004447 for (; i > 0; i--, sp++)
4448 Py_DECREF(*sp);
4449 Py_XDECREF(it);
4450 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004451}
4452
4453
Guido van Rossum96a42c81992-01-12 02:29:51 +00004454#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004455static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004456prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004457{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004458 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02004459 if (PyObject_Print(v, stdout, 0) != 0) {
4460 /* Don't know what else to do */
4461 _PyErr_Clear(tstate);
4462 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004463 printf("\n");
4464 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004465}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004466#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004467
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004468static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004469call_exc_trace(Py_tracefunc func, PyObject *self,
4470 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004471{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004472 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004473 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004474 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004475 if (value == NULL) {
4476 value = Py_None;
4477 Py_INCREF(value);
4478 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004479 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01004480 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004481 arg = PyTuple_Pack(3, type, value, traceback);
4482 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004483 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004484 return;
4485 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004486 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004487 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02004488 if (err == 0) {
4489 _PyErr_Restore(tstate, type, value, orig_traceback);
4490 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004491 else {
4492 Py_XDECREF(type);
4493 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004494 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004495 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004496}
4497
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004498static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004499call_trace_protected(Py_tracefunc func, PyObject *obj,
4500 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004501 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004502{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004503 PyObject *type, *value, *traceback;
4504 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004505 _PyErr_Fetch(tstate, &type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004506 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004507 if (err == 0)
4508 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004509 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004510 return 0;
4511 }
4512 else {
4513 Py_XDECREF(type);
4514 Py_XDECREF(value);
4515 Py_XDECREF(traceback);
4516 return -1;
4517 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004518}
4519
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004520static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004521call_trace(Py_tracefunc func, PyObject *obj,
4522 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004523 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004524{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004525 int result;
4526 if (tstate->tracing)
4527 return 0;
4528 tstate->tracing++;
4529 tstate->use_tracing = 0;
4530 result = func(obj, frame, what, arg);
4531 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4532 || (tstate->c_profilefunc != NULL));
4533 tstate->tracing--;
4534 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004535}
4536
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004537PyObject *
4538_PyEval_CallTracing(PyObject *func, PyObject *args)
4539{
Victor Stinner50b48572018-11-01 01:51:40 +01004540 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004541 int save_tracing = tstate->tracing;
4542 int save_use_tracing = tstate->use_tracing;
4543 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004545 tstate->tracing = 0;
4546 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4547 || (tstate->c_profilefunc != NULL));
4548 result = PyObject_Call(func, args, NULL);
4549 tstate->tracing = save_tracing;
4550 tstate->use_tracing = save_use_tracing;
4551 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004552}
4553
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004554/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004555static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004556maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004557 PyThreadState *tstate, PyFrameObject *frame,
4558 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004559{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004560 int result = 0;
4561 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004563 /* If the last instruction executed isn't in the current
4564 instruction window, reset the window.
4565 */
4566 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4567 PyAddrPair bounds;
4568 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4569 &bounds);
4570 *instr_lb = bounds.ap_lower;
4571 *instr_ub = bounds.ap_upper;
4572 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004573 /* If the last instruction falls at the start of a line or if it
4574 represents a jump backwards, update the frame's line number and
4575 then call the trace function if we're tracing source lines.
4576 */
4577 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004578 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004579 if (frame->f_trace_lines) {
4580 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4581 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004582 }
George King20faa682017-10-18 17:44:22 -07004583 /* Always emit an opcode event if we're tracing all opcodes. */
4584 if (frame->f_trace_opcodes) {
4585 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4586 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004587 *instr_prev = frame->f_lasti;
4588 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004589}
4590
Fred Drake5755ce62001-06-27 19:19:46 +00004591void
4592PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004593{
Steve Dowerb82e17e2019-05-23 08:45:22 -07004594 if (PySys_Audit("sys.setprofile", NULL) < 0) {
4595 return;
4596 }
4597
Victor Stinner50b48572018-11-01 01:51:40 +01004598 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004599 PyObject *temp = tstate->c_profileobj;
4600 Py_XINCREF(arg);
4601 tstate->c_profilefunc = NULL;
4602 tstate->c_profileobj = NULL;
4603 /* Must make sure that tracing is not ignored if 'temp' is freed */
4604 tstate->use_tracing = tstate->c_tracefunc != NULL;
4605 Py_XDECREF(temp);
4606 tstate->c_profilefunc = func;
4607 tstate->c_profileobj = arg;
4608 /* Flag that tracing or profiling is turned on */
4609 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004610}
4611
4612void
4613PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4614{
Steve Dowerb82e17e2019-05-23 08:45:22 -07004615 if (PySys_Audit("sys.settrace", NULL) < 0) {
4616 return;
4617 }
4618
Victor Stinner09532fe2019-05-10 23:39:09 +02004619 _PyRuntimeState *runtime = &_PyRuntime;
4620 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004621 PyObject *temp = tstate->c_traceobj;
Victor Stinner09532fe2019-05-10 23:39:09 +02004622 runtime->ceval.tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004623 Py_XINCREF(arg);
4624 tstate->c_tracefunc = NULL;
4625 tstate->c_traceobj = NULL;
4626 /* Must make sure that profiling is not ignored if 'temp' is freed */
4627 tstate->use_tracing = tstate->c_profilefunc != NULL;
4628 Py_XDECREF(temp);
4629 tstate->c_tracefunc = func;
4630 tstate->c_traceobj = arg;
4631 /* Flag that tracing or profiling is turned on */
4632 tstate->use_tracing = ((func != NULL)
4633 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004634}
4635
Yury Selivanov75445082015-05-11 22:57:16 -04004636void
Victor Stinner838f2642019-06-13 22:41:23 +02004637_PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004638{
4639 assert(new_depth >= 0);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004640 tstate->coroutine_origin_tracking_depth = new_depth;
4641}
4642
4643int
4644_PyEval_GetCoroutineOriginTrackingDepth(void)
4645{
Victor Stinner50b48572018-11-01 01:51:40 +01004646 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004647 return tstate->coroutine_origin_tracking_depth;
4648}
4649
4650void
Yury Selivanoveb636452016-09-08 22:01:51 -07004651_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4652{
Victor Stinner50b48572018-11-01 01:51:40 +01004653 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004654
4655 if (PySys_Audit("sys.set_asyncgen_hook_firstiter", NULL) < 0) {
4656 return;
4657 }
4658
Yury Selivanoveb636452016-09-08 22:01:51 -07004659 Py_XINCREF(firstiter);
4660 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4661}
4662
4663PyObject *
4664_PyEval_GetAsyncGenFirstiter(void)
4665{
Victor Stinner50b48572018-11-01 01:51:40 +01004666 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004667 return tstate->async_gen_firstiter;
4668}
4669
4670void
4671_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4672{
Victor Stinner50b48572018-11-01 01:51:40 +01004673 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004674
4675 if (PySys_Audit("sys.set_asyncgen_hook_finalizer", NULL) < 0) {
4676 return;
4677 }
4678
Yury Selivanoveb636452016-09-08 22:01:51 -07004679 Py_XINCREF(finalizer);
4680 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4681}
4682
4683PyObject *
4684_PyEval_GetAsyncGenFinalizer(void)
4685{
Victor Stinner50b48572018-11-01 01:51:40 +01004686 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004687 return tstate->async_gen_finalizer;
4688}
4689
Victor Stinner438a12d2019-05-24 17:01:38 +02004690static PyFrameObject *
4691_PyEval_GetFrame(PyThreadState *tstate)
4692{
Victor Stinner01b1cc12019-11-20 02:27:56 +01004693 _PyRuntimeState *runtime = tstate->interp->runtime;
4694 return runtime->gilstate.getframe(tstate);
Victor Stinner438a12d2019-05-24 17:01:38 +02004695}
4696
4697PyFrameObject *
4698PyEval_GetFrame(void)
4699{
4700 PyThreadState *tstate = _PyThreadState_GET();
4701 return _PyEval_GetFrame(tstate);
4702}
4703
Guido van Rossumb209a111997-04-29 18:18:01 +00004704PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004705PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004706{
Victor Stinner438a12d2019-05-24 17:01:38 +02004707 PyThreadState *tstate = _PyThreadState_GET();
4708 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004709 if (current_frame == NULL)
Victor Stinner438a12d2019-05-24 17:01:38 +02004710 return tstate->interp->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004711 else
4712 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004713}
4714
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004715/* Convenience function to get a builtin from its name */
4716PyObject *
4717_PyEval_GetBuiltinId(_Py_Identifier *name)
4718{
Victor Stinner438a12d2019-05-24 17:01:38 +02004719 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004720 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4721 if (attr) {
4722 Py_INCREF(attr);
4723 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004724 else if (!_PyErr_Occurred(tstate)) {
4725 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004726 }
4727 return attr;
4728}
4729
Guido van Rossumb209a111997-04-29 18:18:01 +00004730PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004731PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004732{
Victor Stinner438a12d2019-05-24 17:01:38 +02004733 PyThreadState *tstate = _PyThreadState_GET();
4734 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Victor Stinner41bb43a2013-10-29 01:19:37 +01004735 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004736 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004737 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004738 }
4739
Victor Stinner438a12d2019-05-24 17:01:38 +02004740 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01004741 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004742 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004743
4744 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004745 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004746}
4747
Guido van Rossumb209a111997-04-29 18:18:01 +00004748PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004749PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004750{
Victor Stinner438a12d2019-05-24 17:01:38 +02004751 PyThreadState *tstate = _PyThreadState_GET();
4752 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
4753 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004754 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004755 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004756
4757 assert(current_frame->f_globals != NULL);
4758 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004759}
4760
Guido van Rossum6135a871995-01-09 17:53:26 +00004761int
Tim Peters5ba58662001-07-16 02:29:45 +00004762PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004763{
Victor Stinner438a12d2019-05-24 17:01:38 +02004764 PyThreadState *tstate = _PyThreadState_GET();
4765 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004766 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004767
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004768 if (current_frame != NULL) {
4769 const int codeflags = current_frame->f_code->co_flags;
4770 const int compilerflags = codeflags & PyCF_MASK;
4771 if (compilerflags) {
4772 result = 1;
4773 cf->cf_flags |= compilerflags;
4774 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004775#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004776 if (codeflags & CO_GENERATOR_ALLOWED) {
4777 result = 1;
4778 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4779 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004780#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004781 }
4782 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004783}
4784
Guido van Rossum3f5da241990-12-20 15:06:42 +00004785
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004786const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004787PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004788{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004789 if (PyMethod_Check(func))
4790 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4791 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004792 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004793 else if (PyCFunction_Check(func))
4794 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4795 else
Victor Stinnera102ed72020-02-07 02:24:48 +01004796 return Py_TYPE(func)->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004797}
4798
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004799const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004800PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004801{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004802 if (PyMethod_Check(func))
4803 return "()";
4804 else if (PyFunction_Check(func))
4805 return "()";
4806 else if (PyCFunction_Check(func))
4807 return "()";
4808 else
4809 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004810}
4811
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004812#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004813if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004814 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4815 tstate, tstate->frame, \
4816 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004817 x = NULL; \
4818 } \
4819 else { \
4820 x = call; \
4821 if (tstate->c_profilefunc != NULL) { \
4822 if (x == NULL) { \
4823 call_trace_protected(tstate->c_profilefunc, \
4824 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004825 tstate, tstate->frame, \
4826 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004827 /* XXX should pass (type, value, tb) */ \
4828 } else { \
4829 if (call_trace(tstate->c_profilefunc, \
4830 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004831 tstate, tstate->frame, \
4832 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004833 Py_DECREF(x); \
4834 x = NULL; \
4835 } \
4836 } \
4837 } \
4838 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004839} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004840 x = call; \
4841 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004842
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004843
4844static PyObject *
4845trace_call_function(PyThreadState *tstate,
4846 PyObject *func,
4847 PyObject **args, Py_ssize_t nargs,
4848 PyObject *kwnames)
4849{
4850 PyObject *x;
4851 if (PyCFunction_Check(func)) {
Petr Viktorinffd97532020-02-11 17:46:57 +01004852 C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004853 return x;
4854 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06004855 else if (Py_IS_TYPE(func, &PyMethodDescr_Type) && nargs > 0) {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004856 /* We need to create a temporary bound method as argument
4857 for profiling.
4858
4859 If nargs == 0, then this cannot work because we have no
4860 "self". In any case, the call itself would raise
4861 TypeError (foo needs an argument), so we just skip
4862 profiling. */
4863 PyObject *self = args[0];
4864 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4865 if (func == NULL) {
4866 return NULL;
4867 }
Petr Viktorinffd97532020-02-11 17:46:57 +01004868 C_TRACE(x, PyObject_Vectorcall(func,
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02004869 args+1, nargs-1,
4870 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004871 Py_DECREF(func);
4872 return x;
4873 }
Petr Viktorinffd97532020-02-11 17:46:57 +01004874 return PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004875}
4876
Victor Stinner415c5102017-01-11 00:54:57 +01004877/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4878 to reduce the stack consumption. */
4879Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Victor Stinner09532fe2019-05-10 23:39:09 +02004880call_function(PyThreadState *tstate, PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004881{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004882 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004883 PyObject *func = *pfunc;
4884 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004885 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4886 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004887 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004888
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004889 if (tstate->use_tracing) {
4890 x = trace_call_function(tstate, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09004891 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004892 else {
Petr Viktorinffd97532020-02-11 17:46:57 +01004893 x = PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004894 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004895
Victor Stinner438a12d2019-05-24 17:01:38 +02004896 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004897
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004898 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004899 while ((*pp_stack) > pfunc) {
4900 w = EXT_POP(*pp_stack);
4901 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004902 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004903
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004904 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004905}
4906
Jeremy Hylton52820442001-01-03 23:52:36 +00004907static PyObject *
Victor Stinner09532fe2019-05-10 23:39:09 +02004908do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004909{
jdemeyere89de732018-09-19 12:06:20 +02004910 PyObject *result;
4911
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004912 if (PyCFunction_Check(func)) {
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +02004913 C_TRACE(result, PyObject_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004914 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004915 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06004916 else if (Py_IS_TYPE(func, &PyMethodDescr_Type)) {
jdemeyere89de732018-09-19 12:06:20 +02004917 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
4918 if (nargs > 0 && tstate->use_tracing) {
4919 /* We need to create a temporary bound method as argument
4920 for profiling.
4921
4922 If nargs == 0, then this cannot work because we have no
4923 "self". In any case, the call itself would raise
4924 TypeError (foo needs an argument), so we just skip
4925 profiling. */
4926 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
4927 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4928 if (func == NULL) {
4929 return NULL;
4930 }
4931
Victor Stinner4d231bc2019-11-14 13:36:21 +01004932 C_TRACE(result, _PyObject_FastCallDictTstate(
4933 tstate, func,
4934 &_PyTuple_ITEMS(callargs)[1],
4935 nargs - 1,
4936 kwdict));
jdemeyere89de732018-09-19 12:06:20 +02004937 Py_DECREF(func);
4938 return result;
4939 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004940 }
jdemeyere89de732018-09-19 12:06:20 +02004941 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00004942}
4943
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004944/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004945 nb_index slot defined, and store in *pi.
4946 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004947 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004948 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004949*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004950int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004951_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004952{
Victor Stinner438a12d2019-05-24 17:01:38 +02004953 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004954 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004955 Py_ssize_t x;
4956 if (PyIndex_Check(v)) {
4957 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02004958 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004959 return 0;
4960 }
4961 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004962 _PyErr_SetString(tstate, PyExc_TypeError,
4963 "slice indices must be integers or "
4964 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004965 return 0;
4966 }
4967 *pi = x;
4968 }
4969 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004970}
4971
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004972int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004973_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004974{
Victor Stinner438a12d2019-05-24 17:01:38 +02004975 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004976 Py_ssize_t x;
4977 if (PyIndex_Check(v)) {
4978 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02004979 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004980 return 0;
4981 }
4982 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004983 _PyErr_SetString(tstate, PyExc_TypeError,
4984 "slice indices must be integers or "
4985 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004986 return 0;
4987 }
4988 *pi = x;
4989 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004990}
4991
Thomas Wouters52152252000-08-17 22:55:00 +00004992static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004993import_name(PyThreadState *tstate, PyFrameObject *f,
4994 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004995{
4996 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004997 PyObject *import_func, *res;
4998 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004999
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005000 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005001 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005002 if (!_PyErr_Occurred(tstate)) {
5003 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005004 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005005 return NULL;
5006 }
5007
5008 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005009 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005010 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02005011 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005012 return NULL;
5013 }
5014 res = PyImport_ImportModuleLevelObject(
5015 name,
5016 f->f_globals,
5017 f->f_locals == NULL ? Py_None : f->f_locals,
5018 fromlist,
5019 ilevel);
5020 return res;
5021 }
5022
5023 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005024
5025 stack[0] = name;
5026 stack[1] = f->f_globals;
5027 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5028 stack[3] = fromlist;
5029 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005030 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005031 Py_DECREF(import_func);
5032 return res;
5033}
5034
5035static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005036import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005037{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005038 PyObject *x;
Xiang Zhang4830f582017-03-21 11:13:42 +08005039 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005040
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005041 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005042 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005043 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005044 /* Issue #17636: in case this failed because of a circular relative
5045 import, try to fallback on reading the module directly from
5046 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02005047 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005048 if (pkgname == NULL) {
5049 goto error;
5050 }
Oren Milman6db70332017-09-19 14:23:01 +03005051 if (!PyUnicode_Check(pkgname)) {
5052 Py_CLEAR(pkgname);
5053 goto error;
5054 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005055 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005056 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005057 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005058 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005059 }
Eric Snow3f9eee62017-09-15 16:35:20 -06005060 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005061 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02005062 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07005063 goto error;
5064 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005065 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005066 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005067 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005068 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005069 if (pkgname == NULL) {
5070 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5071 if (pkgname_or_unknown == NULL) {
5072 Py_XDECREF(pkgpath);
5073 return NULL;
5074 }
5075 } else {
5076 pkgname_or_unknown = pkgname;
5077 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005078
5079 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005080 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08005081 errmsg = PyUnicode_FromFormat(
5082 "cannot import name %R from %R (unknown location)",
5083 name, pkgname_or_unknown
5084 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005085 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005086 PyErr_SetImportError(errmsg, pkgname, NULL);
5087 }
5088 else {
Anthony Sottile65366bc2019-09-09 08:17:50 -07005089 _Py_IDENTIFIER(__spec__);
5090 PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
Anthony Sottile65366bc2019-09-09 08:17:50 -07005091 const char *fmt =
5092 _PyModuleSpec_IsInitializing(spec) ?
5093 "cannot import name %R from partially initialized module %R "
5094 "(most likely due to a circular import) (%S)" :
5095 "cannot import name %R from %R (%S)";
5096 Py_XDECREF(spec);
5097
5098 errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
Stefan Krah027b09c2019-03-25 21:50:58 +01005099 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005100 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005101 }
5102
Xiang Zhang4830f582017-03-21 11:13:42 +08005103 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005104 Py_XDECREF(pkgname_or_unknown);
5105 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005106 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005107}
Guido van Rossumac7be682001-01-17 15:42:30 +00005108
Thomas Wouters52152252000-08-17 22:55:00 +00005109static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005110import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00005111{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005112 _Py_IDENTIFIER(__all__);
5113 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005114 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005115 int skip_leading_underscores = 0;
5116 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005117
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005118 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5119 return -1; /* Unexpected error */
5120 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005121 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005122 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5123 return -1;
5124 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005125 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005126 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005127 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005128 return -1;
5129 }
5130 all = PyMapping_Keys(dict);
5131 Py_DECREF(dict);
5132 if (all == NULL)
5133 return -1;
5134 skip_leading_underscores = 1;
5135 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005136
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005137 for (pos = 0, err = 0; ; pos++) {
5138 name = PySequence_GetItem(all, pos);
5139 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005140 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005141 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02005142 }
5143 else {
5144 _PyErr_Clear(tstate);
5145 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005146 break;
5147 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005148 if (!PyUnicode_Check(name)) {
5149 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5150 if (modname == NULL) {
5151 Py_DECREF(name);
5152 err = -1;
5153 break;
5154 }
5155 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005156 _PyErr_Format(tstate, PyExc_TypeError,
5157 "module __name__ must be a string, not %.100s",
5158 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005159 }
5160 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005161 _PyErr_Format(tstate, PyExc_TypeError,
5162 "%s in %U.%s must be str, not %.100s",
5163 skip_leading_underscores ? "Key" : "Item",
5164 modname,
5165 skip_leading_underscores ? "__dict__" : "__all__",
5166 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005167 }
5168 Py_DECREF(modname);
5169 Py_DECREF(name);
5170 err = -1;
5171 break;
5172 }
5173 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005174 if (PyUnicode_READY(name) == -1) {
5175 Py_DECREF(name);
5176 err = -1;
5177 break;
5178 }
5179 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5180 Py_DECREF(name);
5181 continue;
5182 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005183 }
5184 value = PyObject_GetAttr(v, name);
5185 if (value == NULL)
5186 err = -1;
5187 else if (PyDict_CheckExact(locals))
5188 err = PyDict_SetItem(locals, name, value);
5189 else
5190 err = PyObject_SetItem(locals, name, value);
5191 Py_DECREF(name);
5192 Py_XDECREF(value);
5193 if (err != 0)
5194 break;
5195 }
5196 Py_DECREF(all);
5197 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005198}
5199
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005200static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005201check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005202{
Victor Stinnera102ed72020-02-07 02:24:48 +01005203 if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005204 /* check_args_iterable() may be called with a live exception:
5205 * clear it to prevent calling _PyObject_FunctionStr() with an
5206 * exception set. */
Victor Stinner61f4db82020-01-28 03:37:45 +01005207 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005208 PyObject *funcstr = _PyObject_FunctionStr(func);
5209 if (funcstr != NULL) {
5210 _PyErr_Format(tstate, PyExc_TypeError,
5211 "%U argument after * must be an iterable, not %.200s",
5212 funcstr, Py_TYPE(args)->tp_name);
5213 Py_DECREF(funcstr);
5214 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005215 return -1;
5216 }
5217 return 0;
5218}
5219
5220static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005221format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005222{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005223 /* _PyDict_MergeEx raises attribute
5224 * error (percolated from an attempt
5225 * to get 'keys' attribute) instead of
5226 * a type error if its second argument
5227 * is not a mapping.
5228 */
Victor Stinner438a12d2019-05-24 17:01:38 +02005229 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005230 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005231 PyObject *funcstr = _PyObject_FunctionStr(func);
5232 if (funcstr != NULL) {
5233 _PyErr_Format(
5234 tstate, PyExc_TypeError,
5235 "%U argument after ** must be a mapping, not %.200s",
5236 funcstr, Py_TYPE(kwargs)->tp_name);
5237 Py_DECREF(funcstr);
5238 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005239 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005240 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005241 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02005242 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005243 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005244 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005245 PyObject *funcstr = _PyObject_FunctionStr(func);
5246 if (funcstr != NULL) {
5247 PyObject *key = PyTuple_GET_ITEM(val, 0);
5248 _PyErr_Format(
5249 tstate, PyExc_TypeError,
5250 "%U got multiple values for keyword argument '%S'",
5251 funcstr, key);
5252 Py_DECREF(funcstr);
5253 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005254 Py_XDECREF(exc);
5255 Py_XDECREF(val);
5256 Py_XDECREF(tb);
5257 }
5258 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005259 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005260 }
5261 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005262}
5263
Guido van Rossumac7be682001-01-17 15:42:30 +00005264static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005265format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
5266 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005267{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005268 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005270 if (!obj)
5271 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005272
Serhiy Storchaka06515832016-11-20 09:13:07 +02005273 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005274 if (!obj_str)
5275 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005276
Victor Stinner438a12d2019-05-24 17:01:38 +02005277 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005278}
Guido van Rossum950361c1997-01-24 13:49:28 +00005279
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005280static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005281format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005282{
5283 PyObject *name;
5284 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02005285 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005286 return;
5287 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5288 name = PyTuple_GET_ITEM(co->co_cellvars,
5289 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005290 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005291 PyExc_UnboundLocalError,
5292 UNBOUNDLOCAL_ERROR_MSG,
5293 name);
5294 } else {
5295 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5296 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02005297 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005298 UNBOUNDFREE_ERROR_MSG, name);
5299 }
5300}
5301
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005302static void
Mark Shannonfee55262019-11-21 09:11:43 +00005303format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevprevopcode, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005304{
5305 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5306 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005307 _PyErr_Format(tstate, PyExc_TypeError,
5308 "'async with' received an object from __aenter__ "
5309 "that does not implement __await__: %.100s",
5310 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005311 }
Mark Shannonfee55262019-11-21 09:11:43 +00005312 else if (prevopcode == WITH_EXCEPT_START || (prevopcode == CALL_FUNCTION && prevprevopcode == DUP_TOP)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005313 _PyErr_Format(tstate, PyExc_TypeError,
5314 "'async with' received an object from __aexit__ "
5315 "that does not implement __await__: %.100s",
5316 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005317 }
5318 }
5319}
5320
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005321static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005322unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005323 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005324{
5325 PyObject *res;
5326 if (Py_REFCNT(v) == 2) {
5327 /* In the common case, there are 2 references to the value
5328 * stored in 'variable' when the += is performed: one on the
5329 * value stack (in 'v') and one still stored in the
5330 * 'variable'. We try to delete the variable now to reduce
5331 * the refcnt to 1.
5332 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005333 int opcode, oparg;
5334 NEXTOPARG();
5335 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005336 case STORE_FAST:
5337 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005338 PyObject **fastlocals = f->f_localsplus;
5339 if (GETLOCAL(oparg) == v)
5340 SETLOCAL(oparg, NULL);
5341 break;
5342 }
5343 case STORE_DEREF:
5344 {
5345 PyObject **freevars = (f->f_localsplus +
5346 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005347 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005348 if (PyCell_GET(c) == v) {
5349 PyCell_SET(c, NULL);
5350 Py_DECREF(v);
5351 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005352 break;
5353 }
5354 case STORE_NAME:
5355 {
5356 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005357 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005358 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005359 if (locals && PyDict_CheckExact(locals)) {
5360 PyObject *w = PyDict_GetItemWithError(locals, name);
5361 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02005362 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005363 {
5364 Py_DECREF(v);
5365 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005366 }
5367 }
5368 break;
5369 }
5370 }
5371 }
5372 res = v;
5373 PyUnicode_Append(&res, w);
5374 return res;
5375}
5376
Guido van Rossum950361c1997-01-24 13:49:28 +00005377#ifdef DYNAMIC_EXECUTION_PROFILE
5378
Skip Montanarof118cb12001-10-15 20:51:38 +00005379static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005380getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005381{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005382 int i;
5383 PyObject *l = PyList_New(256);
5384 if (l == NULL) return NULL;
5385 for (i = 0; i < 256; i++) {
5386 PyObject *x = PyLong_FromLong(a[i]);
5387 if (x == NULL) {
5388 Py_DECREF(l);
5389 return NULL;
5390 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005391 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005392 }
5393 for (i = 0; i < 256; i++)
5394 a[i] = 0;
5395 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005396}
5397
5398PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005399_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005400{
5401#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005402 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005403#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005404 int i;
5405 PyObject *l = PyList_New(257);
5406 if (l == NULL) return NULL;
5407 for (i = 0; i < 257; i++) {
5408 PyObject *x = getarray(dxpairs[i]);
5409 if (x == NULL) {
5410 Py_DECREF(l);
5411 return NULL;
5412 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005413 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005414 }
5415 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005416#endif
5417}
5418
5419#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005420
5421Py_ssize_t
5422_PyEval_RequestCodeExtraIndex(freefunc free)
5423{
Victor Stinnercaba55b2018-08-03 15:33:52 +02005424 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -07005425 Py_ssize_t new_index;
5426
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005427 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005428 return -1;
5429 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005430 new_index = interp->co_extra_user_count++;
5431 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005432 return new_index;
5433}
Łukasz Langaa785c872016-09-09 17:37:37 -07005434
5435static void
5436dtrace_function_entry(PyFrameObject *f)
5437{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005438 const char *filename;
5439 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005440 int lineno;
5441
5442 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5443 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5444 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5445
Andy Lestere6be9b52020-02-11 20:28:35 -06005446 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005447}
5448
5449static void
5450dtrace_function_return(PyFrameObject *f)
5451{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005452 const char *filename;
5453 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005454 int lineno;
5455
5456 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5457 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5458 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5459
Andy Lestere6be9b52020-02-11 20:28:35 -06005460 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005461}
5462
5463/* DTrace equivalent of maybe_call_line_trace. */
5464static void
5465maybe_dtrace_line(PyFrameObject *frame,
5466 int *instr_lb, int *instr_ub, int *instr_prev)
5467{
5468 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005469 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005470
5471 /* If the last instruction executed isn't in the current
5472 instruction window, reset the window.
5473 */
5474 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5475 PyAddrPair bounds;
5476 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5477 &bounds);
5478 *instr_lb = bounds.ap_lower;
5479 *instr_ub = bounds.ap_upper;
5480 }
5481 /* If the last instruction falls at the start of a line or if
5482 it represents a jump backwards, update the frame's line
5483 number and call the trace function. */
5484 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5485 frame->f_lineno = line;
5486 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5487 if (!co_filename)
5488 co_filename = "?";
5489 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5490 if (!co_name)
5491 co_name = "?";
Andy Lestere6be9b52020-02-11 20:28:35 -06005492 PyDTrace_LINE(co_filename, co_name, line);
Łukasz Langaa785c872016-09-09 17:37:37 -07005493 }
5494 *instr_prev = frame->f_lasti;
5495}
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005496
5497
5498/* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions
5499 for the limited API. */
5500
5501#undef Py_EnterRecursiveCall
5502
5503int Py_EnterRecursiveCall(const char *where)
5504{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005505 return _Py_EnterRecursiveCall_inline(where);
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005506}
5507
5508#undef Py_LeaveRecursiveCall
5509
5510void Py_LeaveRecursiveCall(void)
5511{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005512 _Py_LeaveRecursiveCall_inline();
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005513}