blob: fc4f718de28dde6c56963712b64071d67fcbffd9 [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
Victor Stinner175a7042020-03-10 00:37:48 +0100202_PyEval_ThreadsInitialized(_PyRuntimeState *runtime)
203{
204 return gil_created(&runtime->ceval.gil);
205}
206
207int
Tim Peters7f468f22004-10-11 02:40:51 +0000208PyEval_ThreadsInitialized(void)
209{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100210 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner175a7042020-03-10 00:37:48 +0100211 return _PyEval_ThreadsInitialized(runtime);
Tim Peters7f468f22004-10-11 02:40:51 +0000212}
213
Victor Stinner111e4ee2020-03-09 21:24:14 +0100214PyStatus
215_PyEval_InitThreads(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000216{
Victor Stinner111e4ee2020-03-09 21:24:14 +0100217 if (tstate == NULL) {
218 return _PyStatus_ERR("tstate is NULL");
219 }
220
221 struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
Victor Stinnere225beb2019-06-03 18:14:24 +0200222 struct _gil_runtime_state *gil = &ceval->gil;
Victor Stinner09532fe2019-05-10 23:39:09 +0200223 if (gil_created(gil)) {
Victor Stinner111e4ee2020-03-09 21:24:14 +0100224 return _PyStatus_OK();
Victor Stinnera7126792019-03-19 14:19:38 +0100225 }
226
Inada Naoki001fee12019-02-20 10:00:09 +0900227 PyThread_init_thread();
Victor Stinner09532fe2019-05-10 23:39:09 +0200228 create_gil(gil);
Victor Stinner85f5a692020-03-09 22:12:04 +0100229
230 take_gil(tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700231
Victor Stinnere225beb2019-06-03 18:14:24 +0200232 struct _pending_calls *pending = &ceval->pending;
233 pending->lock = PyThread_allocate_lock();
234 if (pending->lock == NULL) {
Victor Stinner111e4ee2020-03-09 21:24:14 +0100235 return _PyStatus_NO_MEMORY();
236 }
Victor Stinner85f5a692020-03-09 22:12:04 +0100237
Victor Stinner111e4ee2020-03-09 21:24:14 +0100238 return _PyStatus_OK();
239}
240
241void
242PyEval_InitThreads(void)
243{
Victor Stinnerb4698ec2020-03-10 01:28:54 +0100244 /* Do nothing: kept for backward compatibility */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000245}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000246
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000247void
Victor Stinnere225beb2019-06-03 18:14:24 +0200248_PyEval_FiniThreads(struct _ceval_runtime_state *ceval)
Antoine Pitrou1df15362010-09-13 14:16:46 +0000249{
Victor Stinnere225beb2019-06-03 18:14:24 +0200250 struct _gil_runtime_state *gil = &ceval->gil;
Victor Stinner09532fe2019-05-10 23:39:09 +0200251 if (!gil_created(gil)) {
Antoine Pitrou1df15362010-09-13 14:16:46 +0000252 return;
Victor Stinnera7126792019-03-19 14:19:38 +0100253 }
254
Victor Stinner09532fe2019-05-10 23:39:09 +0200255 destroy_gil(gil);
256 assert(!gil_created(gil));
Victor Stinner99fcc612019-04-29 13:04:07 +0200257
Victor Stinnere225beb2019-06-03 18:14:24 +0200258 struct _pending_calls *pending = &ceval->pending;
259 if (pending->lock != NULL) {
260 PyThread_free_lock(pending->lock);
261 pending->lock = NULL;
262 }
Antoine Pitrou1df15362010-09-13 14:16:46 +0000263}
264
265void
Inada Naoki91234a12019-06-03 21:30:58 +0900266_PyEval_Fini(void)
267{
268#if OPCACHE_STATS
269 fprintf(stderr, "-- Opcode cache number of objects = %zd\n",
270 opcache_code_objects);
271
272 fprintf(stderr, "-- Opcode cache total extra mem = %zd\n",
273 opcache_code_objects_extra_mem);
274
275 fprintf(stderr, "\n");
276
277 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL hits = %zd (%d%%)\n",
278 opcache_global_hits,
279 (int) (100.0 * opcache_global_hits /
280 (opcache_global_hits + opcache_global_misses)));
281
282 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL misses = %zd (%d%%)\n",
283 opcache_global_misses,
284 (int) (100.0 * opcache_global_misses /
285 (opcache_global_hits + opcache_global_misses)));
286
287 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL opts = %zd\n",
288 opcache_global_opts);
289
290 fprintf(stderr, "\n");
291#endif
292}
293
294void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000295PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000296{
Victor Stinner09532fe2019-05-10 23:39:09 +0200297 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200298 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100299 ensure_tstate_not_null(__func__, tstate);
300
Victor Stinner85f5a692020-03-09 22:12:04 +0100301 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000302}
303
304void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000305PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000306{
Victor Stinner09532fe2019-05-10 23:39:09 +0200307 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200308 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100310 We therefore avoid PyThreadState_Get() which dumps a fatal error
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 in debug mode.
312 */
Victor Stinnere225beb2019-06-03 18:14:24 +0200313 drop_gil(&runtime->ceval, tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000314}
315
316void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000317PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000318{
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100319 ensure_tstate_not_null(__func__, tstate);
320
Victor Stinner85f5a692020-03-09 22:12:04 +0100321 take_gil(tstate);
Victor Stinnere225beb2019-06-03 18:14:24 +0200322
Victor Stinner85f5a692020-03-09 22:12:04 +0100323 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
324 if (_PyThreadState_Swap(gilstate, tstate) != NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100325 Py_FatalError("non-NULL old thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200326 }
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000327}
328
329void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000330PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000331{
Victor Stinner17c68b82020-01-30 12:20:48 +0100332 assert(tstate != NULL);
Victor Stinner09532fe2019-05-10 23:39:09 +0200333
Victor Stinner01b1cc12019-11-20 02:27:56 +0100334 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200335 PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
336 if (new_tstate != tstate) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100337 Py_FatalError("wrong thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200338 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200339 drop_gil(&runtime->ceval, tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000340}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000341
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200342/* This function is called from PyOS_AfterFork_Child to destroy all threads
343 * which are not running in the child process, and clear internal locks
344 * which might be held by those threads.
345 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000346
347void
Victor Stinnerd5d9e812019-05-13 12:35:37 +0200348_PyEval_ReInitThreads(_PyRuntimeState *runtime)
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000349{
Victor Stinnere225beb2019-06-03 18:14:24 +0200350 struct _ceval_runtime_state *ceval = &runtime->ceval;
351 if (!gil_created(&ceval->gil)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 return;
Victor Stinner09532fe2019-05-10 23:39:09 +0200353 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200354 recreate_gil(&ceval->gil);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100355 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
356 ensure_tstate_not_null(__func__, tstate);
Victor Stinner85f5a692020-03-09 22:12:04 +0100357
358 take_gil(tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700359
Victor Stinnere225beb2019-06-03 18:14:24 +0200360 struct _pending_calls *pending = &ceval->pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200361 pending->lock = PyThread_allocate_lock();
362 if (pending->lock == NULL) {
Eric Snow8479a342019-03-08 23:44:33 -0700363 Py_FatalError("Can't initialize threads for pending calls");
364 }
Jesse Nollera8513972008-07-17 16:49:17 +0000365
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200366 /* Destroy all threads except the current one */
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100367 _PyThreadState_DeleteExcept(runtime, tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000368}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000369
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000370/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600371 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000372
373void
Victor Stinnere225beb2019-06-03 18:14:24 +0200374_PyEval_SignalAsyncExc(struct _ceval_runtime_state *ceval)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000375{
Victor Stinnere225beb2019-06-03 18:14:24 +0200376 SIGNAL_ASYNC_EXC(ceval);
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000377}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000378
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000379PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000380PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000381{
Victor Stinner09532fe2019-05-10 23:39:09 +0200382 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200383 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinner09532fe2019-05-10 23:39:09 +0200384 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
385 if (tstate == NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100386 Py_FatalError("NULL tstate");
Victor Stinner09532fe2019-05-10 23:39:09 +0200387 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200388 assert(gil_created(&ceval->gil));
389 drop_gil(ceval, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000391}
392
393void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000394PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000395{
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100396 ensure_tstate_not_null(__func__, tstate);
397
Victor Stinner85f5a692020-03-09 22:12:04 +0100398 take_gil(tstate);
Victor Stinner17c68b82020-01-30 12:20:48 +0100399
Victor Stinner85f5a692020-03-09 22:12:04 +0100400 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
401 _PyThreadState_Swap(gilstate, tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000402}
403
404
Guido van Rossuma9672091994-09-14 13:31:22 +0000405/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
406 signal handlers or Mac I/O completion routines) can schedule calls
407 to a function to be called synchronously.
408 The synchronous function is called with one void* argument.
409 It should return 0 for success or -1 for failure -- failure should
410 be accompanied by an exception.
411
412 If registry succeeds, the registry function returns 0; if it fails
413 (e.g. due to too many pending calls) it returns -1 (without setting
414 an exception condition).
415
416 Note that because registry may occur from within signal handlers,
417 or other asynchronous events, calling malloc() is unsafe!
418
Guido van Rossuma9672091994-09-14 13:31:22 +0000419 Any thread can schedule pending calls, but only the main thread
420 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000421 There is no facility to schedule calls to a particular thread, but
422 that should be easy to change, should that ever be required. In
423 that case, the static variables here should go into the python
424 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000425*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000426
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200427void
Victor Stinnere225beb2019-06-03 18:14:24 +0200428_PyEval_SignalReceived(struct _ceval_runtime_state *ceval)
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200429{
430 /* bpo-30703: Function called when the C signal handler of Python gets a
431 signal. We cannot queue a callback using Py_AddPendingCall() since
432 that function is not async-signal-safe. */
Victor Stinnere225beb2019-06-03 18:14:24 +0200433 SIGNAL_PENDING_SIGNALS(ceval);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200434}
435
Eric Snow5be45a62019-03-08 22:47:07 -0700436/* Push one item onto the queue while holding the lock. */
437static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200438_push_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600439 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700440{
Eric Snow842a2f02019-03-15 15:47:51 -0600441 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700442 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600443 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700444 return -1; /* Queue full */
445 }
Eric Snow842a2f02019-03-15 15:47:51 -0600446 pending->calls[i].func = func;
447 pending->calls[i].arg = arg;
448 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700449 return 0;
450}
451
452/* Pop one item off the queue while holding the lock. */
453static void
Victor Stinnere225beb2019-06-03 18:14:24 +0200454_pop_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600455 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700456{
Eric Snow842a2f02019-03-15 15:47:51 -0600457 int i = pending->first;
458 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700459 return; /* Queue empty */
460 }
461
Eric Snow842a2f02019-03-15 15:47:51 -0600462 *func = pending->calls[i].func;
463 *arg = pending->calls[i].arg;
464 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700465}
466
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200467/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000468 scheduling to be made from any thread, and even from an executing
469 callback.
470 */
471
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000472int
Victor Stinner438a12d2019-05-24 17:01:38 +0200473_PyEval_AddPendingCall(PyThreadState *tstate,
Victor Stinnere225beb2019-06-03 18:14:24 +0200474 struct _ceval_runtime_state *ceval,
Victor Stinner09532fe2019-05-10 23:39:09 +0200475 int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000476{
Victor Stinnere225beb2019-06-03 18:14:24 +0200477 struct _pending_calls *pending = &ceval->pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600478
479 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
480 if (pending->finishing) {
481 PyThread_release_lock(pending->lock);
482
483 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +0200484 _PyErr_Fetch(tstate, &exc, &val, &tb);
485 _PyErr_SetString(tstate, PyExc_SystemError,
Eric Snow842a2f02019-03-15 15:47:51 -0600486 "Py_AddPendingCall: cannot add pending calls "
487 "(Python shutting down)");
Victor Stinner438a12d2019-05-24 17:01:38 +0200488 _PyErr_Print(tstate);
489 _PyErr_Restore(tstate, exc, val, tb);
Eric Snow842a2f02019-03-15 15:47:51 -0600490 return -1;
491 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200492 int result = _push_pending_call(pending, func, arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600493 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700494
Victor Stinnere225beb2019-06-03 18:14:24 +0200495 /* signal main loop */
496 SIGNAL_PENDING_CALLS(ceval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000498}
499
Victor Stinner09532fe2019-05-10 23:39:09 +0200500int
501Py_AddPendingCall(int (*func)(void *), void *arg)
502{
Victor Stinner438a12d2019-05-24 17:01:38 +0200503 _PyRuntimeState *runtime = &_PyRuntime;
504 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinnere225beb2019-06-03 18:14:24 +0200505 return _PyEval_AddPendingCall(tstate, &runtime->ceval, func, arg);
Victor Stinner09532fe2019-05-10 23:39:09 +0200506}
507
Eric Snowfdf282d2019-01-11 14:26:55 -0700508static int
Victor Stinner09532fe2019-05-10 23:39:09 +0200509handle_signals(_PyRuntimeState *runtime)
Eric Snowfdf282d2019-01-11 14:26:55 -0700510{
Victor Stinner3225b9f2020-03-09 20:56:57 +0100511 /* Only handle signals on main thread */
Victor Stinner09532fe2019-05-10 23:39:09 +0200512 if (PyThread_get_thread_ident() != runtime->main_thread) {
Eric Snowfdf282d2019-01-11 14:26:55 -0700513 return 0;
514 }
Eric Snow64d6cc82019-02-23 15:40:43 -0700515 /*
516 * Ensure that the thread isn't currently running some other
517 * interpreter.
518 */
Victor Stinner09532fe2019-05-10 23:39:09 +0200519 PyInterpreterState *interp = _PyRuntimeState_GetThreadState(runtime)->interp;
520 if (interp != runtime->interpreters.main) {
Eric Snow64d6cc82019-02-23 15:40:43 -0700521 return 0;
522 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700523
Victor Stinnere225beb2019-06-03 18:14:24 +0200524 struct _ceval_runtime_state *ceval = &runtime->ceval;
525 UNSIGNAL_PENDING_SIGNALS(ceval);
Eric Snow64d6cc82019-02-23 15:40:43 -0700526 if (_PyErr_CheckSignals() < 0) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200527 SIGNAL_PENDING_SIGNALS(ceval); /* We're not done yet */
Eric Snowfdf282d2019-01-11 14:26:55 -0700528 return -1;
529 }
530 return 0;
531}
532
533static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200534make_pending_calls(_PyRuntimeState *runtime)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000535{
Eric Snow6a150bc2019-06-01 15:39:46 -0600536 static int busy = 0;
Eric Snowb75b1a352019-04-12 10:20:10 -0600537
Victor Stinnere225beb2019-06-03 18:14:24 +0200538 /* only service pending calls on main thread */
539 if (PyThread_get_thread_ident() != runtime->main_thread) {
540 return 0;
541 }
542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 /* don't perform recursive pending calls */
Eric Snowfdf282d2019-01-11 14:26:55 -0700544 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700546 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200547 busy = 1;
Victor Stinnere225beb2019-06-03 18:14:24 +0200548 struct _ceval_runtime_state *ceval = &runtime->ceval;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200549 /* unsignal before starting to call callbacks, so that any callback
550 added in-between re-signals */
Victor Stinnere225beb2019-06-03 18:14:24 +0200551 UNSIGNAL_PENDING_CALLS(ceval);
Eric Snowfdf282d2019-01-11 14:26:55 -0700552 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 /* perform a bounded number of calls, in case of recursion */
Victor Stinnere225beb2019-06-03 18:14:24 +0200555 struct _pending_calls *pending = &ceval->pending;
Eric Snowfdf282d2019-01-11 14:26:55 -0700556 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700557 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 void *arg = NULL;
559
560 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600561 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200562 _pop_pending_call(pending, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600563 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700564
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100565 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700566 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100567 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700568 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700569 res = func(arg);
570 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200571 goto error;
572 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200574
Charles-François Natalif23339a2011-07-23 18:15:43 +0200575 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700576 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200577
578error:
579 busy = 0;
Victor Stinnere225beb2019-06-03 18:14:24 +0200580 SIGNAL_PENDING_CALLS(ceval);
Eric Snowfdf282d2019-01-11 14:26:55 -0700581 return res;
582}
583
Eric Snow842a2f02019-03-15 15:47:51 -0600584void
Victor Stinner2b1df452020-01-13 18:46:59 +0100585_Py_FinishPendingCalls(PyThreadState *tstate)
Eric Snow842a2f02019-03-15 15:47:51 -0600586{
Eric Snow842a2f02019-03-15 15:47:51 -0600587 assert(PyGILState_Check());
588
Victor Stinner2b1df452020-01-13 18:46:59 +0100589 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200590 struct _pending_calls *pending = &runtime->ceval.pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200591
Eric Snow842a2f02019-03-15 15:47:51 -0600592 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
593 pending->finishing = 1;
594 PyThread_release_lock(pending->lock);
595
596 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
597 return;
598 }
599
Victor Stinnere225beb2019-06-03 18:14:24 +0200600 if (make_pending_calls(runtime) < 0) {
601 PyObject *exc, *val, *tb;
602 _PyErr_Fetch(tstate, &exc, &val, &tb);
603 PyErr_BadInternalCall();
604 _PyErr_ChainExceptions(exc, val, tb);
605 _PyErr_Print(tstate);
Eric Snow842a2f02019-03-15 15:47:51 -0600606 }
607}
608
Eric Snowfdf282d2019-01-11 14:26:55 -0700609/* Py_MakePendingCalls() is a simple wrapper for the sake
610 of backward-compatibility. */
611int
612Py_MakePendingCalls(void)
613{
614 assert(PyGILState_Check());
615
616 /* Python signal handler doesn't really queue a callback: it only signals
617 that a signal was received, see _PyEval_SignalReceived(). */
Victor Stinner09532fe2019-05-10 23:39:09 +0200618 _PyRuntimeState *runtime = &_PyRuntime;
619 int res = handle_signals(runtime);
Eric Snowfdf282d2019-01-11 14:26:55 -0700620 if (res != 0) {
621 return res;
622 }
623
Victor Stinnere225beb2019-06-03 18:14:24 +0200624 res = make_pending_calls(runtime);
Eric Snowb75b1a352019-04-12 10:20:10 -0600625 if (res != 0) {
626 return res;
627 }
628
629 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000630}
631
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000632/* The interpreter's recursion limit */
633
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000634#ifndef Py_DEFAULT_RECURSION_LIMIT
635#define Py_DEFAULT_RECURSION_LIMIT 1000
636#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600637
Eric Snow05351c12017-09-05 21:43:08 -0700638int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000639
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600640void
Victor Stinnere225beb2019-06-03 18:14:24 +0200641_PyEval_Initialize(struct _ceval_runtime_state *state)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600642{
Victor Stinnere225beb2019-06-03 18:14:24 +0200643 state->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600644 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Victor Stinnere225beb2019-06-03 18:14:24 +0200645 _gil_initialize(&state->gil);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600646}
647
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000648int
649Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000650{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100651 struct _ceval_runtime_state *ceval = &_PyRuntime.ceval;
652 return ceval->recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000653}
654
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000655void
656Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000657{
Victor Stinnere225beb2019-06-03 18:14:24 +0200658 struct _ceval_runtime_state *ceval = &_PyRuntime.ceval;
659 ceval->recursion_limit = new_limit;
660 _Py_CheckRecursionLimit = ceval->recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000661}
662
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100663/* The function _Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
Armin Rigo2b3eb402003-10-28 12:05:48 +0000664 if the recursion_depth reaches _Py_CheckRecursionLimit.
665 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
666 to guarantee that _Py_CheckRecursiveCall() is regularly called.
667 Without USE_STACKCHECK, there is no need for this. */
668int
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100669_Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000670{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100671 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200672 int recursion_limit = runtime->ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000673
674#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700675 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 if (PyOS_CheckStack()) {
677 --tstate->recursion_depth;
Victor Stinner438a12d2019-05-24 17:01:38 +0200678 _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 return -1;
680 }
pdox18967932017-10-25 23:03:01 -0700681 /* Needed for ABI backwards-compatibility (see bpo-31857) */
Eric Snow05351c12017-09-05 21:43:08 -0700682 _Py_CheckRecursionLimit = recursion_limit;
pdox18967932017-10-25 23:03:01 -0700683#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 if (tstate->recursion_critical)
685 /* Somebody asked that we don't check for recursion. */
686 return 0;
687 if (tstate->overflowed) {
688 if (tstate->recursion_depth > recursion_limit + 50) {
689 /* Overflowing while handling an overflow. Give up. */
690 Py_FatalError("Cannot recover from stack overflow.");
691 }
692 return 0;
693 }
694 if (tstate->recursion_depth > recursion_limit) {
695 --tstate->recursion_depth;
696 tstate->overflowed = 1;
Victor Stinner438a12d2019-05-24 17:01:38 +0200697 _PyErr_Format(tstate, PyExc_RecursionError,
698 "maximum recursion depth exceeded%s",
699 where);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 return -1;
701 }
702 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000703}
704
Victor Stinner09532fe2019-05-10 23:39:09 +0200705static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
Victor Stinner438a12d2019-05-24 17:01:38 +0200706static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000707
Victor Stinnere225beb2019-06-03 18:14:24 +0200708#define _Py_TracingPossible(ceval) ((ceval)->tracing_possible)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000709
Guido van Rossum374a9221991-04-04 10:40:29 +0000710
Guido van Rossumb209a111997-04-29 18:18:01 +0000711PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000712PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000713{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 return PyEval_EvalCodeEx(co,
715 globals, locals,
716 (PyObject **)NULL, 0,
717 (PyObject **)NULL, 0,
718 (PyObject **)NULL, 0,
719 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000720}
721
722
723/* Interpreter main loop */
724
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000725PyObject *
Victor Stinnerb9e68122019-11-14 12:20:46 +0100726PyEval_EvalFrame(PyFrameObject *f)
727{
Victor Stinner0b72b232020-03-12 23:18:39 +0100728 /* Function kept for backward compatibility */
Victor Stinnerb9e68122019-11-14 12:20:46 +0100729 PyThreadState *tstate = _PyThreadState_GET();
730 return _PyEval_EvalFrame(tstate, f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000731}
732
733PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000734PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000735{
Victor Stinnerb9e68122019-11-14 12:20:46 +0100736 PyThreadState *tstate = _PyThreadState_GET();
737 return _PyEval_EvalFrame(tstate, f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700738}
739
Victor Stinnerc6944e72016-11-11 02:13:35 +0100740PyObject* _Py_HOT_FUNCTION
Victor Stinner0b72b232020-03-12 23:18:39 +0100741_PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
Brett Cannon3cebf932016-09-05 15:33:46 -0700742{
Victor Stinner0b72b232020-03-12 23:18:39 +0100743 ensure_tstate_not_null(__func__, tstate);
744
Guido van Rossum950361c1997-01-24 13:49:28 +0000745#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000746 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000747#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200748 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300749 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200750 int opcode; /* Current opcode */
751 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200752 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 PyObject *retval = NULL; /* Return value */
Victor Stinner09532fe2019-05-10 23:39:09 +0200754 _PyRuntimeState * const runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200755 struct _ceval_runtime_state * const ceval = &runtime->ceval;
756 _Py_atomic_int * const eval_breaker = &ceval->eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000758
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000760
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 is true when the line being executed has changed. The
764 initial values are such as to make this false the first
765 time it is tested. */
766 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000767
Serhiy Storchakaab874002016-09-11 13:48:15 +0300768 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 PyObject *names;
770 PyObject *consts;
Inada Naoki91234a12019-06-03 21:30:58 +0900771 _PyOpcache *co_opcache;
Guido van Rossum374a9221991-04-04 10:40:29 +0000772
Brett Cannon368b4b72012-04-02 12:17:59 -0400773#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200774 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400775#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200776
Antoine Pitroub52ec782009-01-25 16:34:23 +0000777/* Computed GOTOs, or
778 the-optimization-commonly-but-improperly-known-as-"threaded code"
779 using gcc's labels-as-values extension
780 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
781
782 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000784 combined with a lookup table of jump addresses. However, since the
785 indirect jump instruction is shared by all opcodes, the CPU will have a
786 hard time making the right prediction for where to jump next (actually,
787 it will be always wrong except in the uncommon case of a sequence of
788 several identical opcodes).
789
790 "Threaded code" in contrast, uses an explicit jump table and an explicit
791 indirect jump instruction at the end of each opcode. Since the jump
792 instruction is at a different address for each opcode, the CPU will make a
793 separate prediction for each of these instructions, which is equivalent to
794 predicting the second opcode of each opcode pair. These predictions have
795 a much better chance to turn out valid, especially in small bytecode loops.
796
797 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000799 and potentially many more instructions (depending on the pipeline width).
800 A correctly predicted branch, however, is nearly free.
801
802 At the time of this writing, the "threaded code" version is up to 15-20%
803 faster than the normal "switch" version, depending on the compiler and the
804 CPU architecture.
805
806 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
807 because it would render the measurements invalid.
808
809
810 NOTE: care must be taken that the compiler doesn't try to "optimize" the
811 indirect jumps by sharing them between all opcodes. Such optimizations
812 can be disabled on gcc by using the -fno-gcse flag (or possibly
813 -fno-crossjumping).
814*/
815
Antoine Pitrou042b1282010-08-13 21:15:58 +0000816#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000817#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000818#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000819#endif
820
Antoine Pitrou042b1282010-08-13 21:15:58 +0000821#ifdef HAVE_COMPUTED_GOTOS
822 #ifndef USE_COMPUTED_GOTOS
823 #define USE_COMPUTED_GOTOS 1
824 #endif
825#else
826 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
827 #error "Computed gotos are not supported on this compiler."
828 #endif
829 #undef USE_COMPUTED_GOTOS
830 #define USE_COMPUTED_GOTOS 0
831#endif
832
833#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000834/* Import the static jump table */
835#include "opcode_targets.h"
836
Antoine Pitroub52ec782009-01-25 16:34:23 +0000837#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -0700838 op: \
839 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000840
Antoine Pitroub52ec782009-01-25 16:34:23 +0000841#ifdef LLTRACE
842#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200844 if (!lltrace && !_Py_TracingPossible(ceval) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300846 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300847 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 } \
849 goto fast_next_opcode; \
850 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000851#else
852#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200854 if (!_Py_TracingPossible(ceval) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300856 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300857 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 } \
859 goto fast_next_opcode; \
860 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000861#endif
862
Victor Stinner09532fe2019-05-10 23:39:09 +0200863#define DISPATCH() \
864 { \
865 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
866 FAST_DISPATCH(); \
867 } \
868 continue; \
869 }
870
Antoine Pitroub52ec782009-01-25 16:34:23 +0000871#else
Benjamin Petersonddd19492018-09-16 22:38:02 -0700872#define TARGET(op) op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000873#define FAST_DISPATCH() goto fast_next_opcode
Victor Stinner09532fe2019-05-10 23:39:09 +0200874#define DISPATCH() continue
Antoine Pitroub52ec782009-01-25 16:34:23 +0000875#endif
876
877
Neal Norwitza81d2202002-07-14 00:27:26 +0000878/* Tuple access macros */
879
880#ifndef Py_DEBUG
881#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
882#else
883#define GETITEM(v, i) PyTuple_GetItem((v), (i))
884#endif
885
Guido van Rossum374a9221991-04-04 10:40:29 +0000886/* Code access macros */
887
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300888/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600889#define INSTR_OFFSET() \
890 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300891#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300892 _Py_CODEUNIT word = *next_instr; \
893 opcode = _Py_OPCODE(word); \
894 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300895 next_instr++; \
896 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300897#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
898#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000899
Raymond Hettingerf606f872003-03-16 03:11:04 +0000900/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 Some opcodes tend to come in pairs thus making it possible to
902 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300903 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000904
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 Verifying the prediction costs a single high-speed test of a register
906 variable against a constant. If the pairing was good, then the
907 processor's own internal branch predication has a high likelihood of
908 success, resulting in a nearly zero-overhead transition to the
909 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300910 including its unpredictable switch-case branch. Combined with the
911 processor's internal branch prediction, a successful PREDICT has the
912 effect of making the two opcodes run as if they were a single new opcode
913 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000914
Georg Brandl86b2fb92008-07-16 03:43:04 +0000915 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 predictions turned-on and interpret the results as if some opcodes
917 had been combined or turn-off predictions so that the opcode frequency
918 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000919
920 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 the CPU to record separate branch prediction information for each
922 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000923
Raymond Hettingerf606f872003-03-16 03:11:04 +0000924*/
925
Denis Chernikovbaf29b22020-02-21 12:17:50 +0300926#define PREDICT_ID(op) PRED_##op
927
Antoine Pitrou042b1282010-08-13 21:15:58 +0000928#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Denis Chernikovbaf29b22020-02-21 12:17:50 +0300929#define PREDICT(op) if (0) goto PREDICT_ID(op)
Raymond Hettingera7216982004-02-08 19:59:27 +0000930#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300931#define PREDICT(op) \
Denis Chernikovbaf29b22020-02-21 12:17:50 +0300932 do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300933 _Py_CODEUNIT word = *next_instr; \
934 opcode = _Py_OPCODE(word); \
Denis Chernikovbaf29b22020-02-21 12:17:50 +0300935 if (opcode == op) { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300936 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300937 next_instr++; \
Denis Chernikovbaf29b22020-02-21 12:17:50 +0300938 goto PREDICT_ID(op); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300939 } \
940 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000941#endif
Denis Chernikovbaf29b22020-02-21 12:17:50 +0300942#define PREDICTED(op) PREDICT_ID(op):
Antoine Pitroub52ec782009-01-25 16:34:23 +0000943
Raymond Hettingerf606f872003-03-16 03:11:04 +0000944
Guido van Rossum374a9221991-04-04 10:40:29 +0000945/* Stack manipulation macros */
946
Martin v. Löwis18e16552006-02-15 17:27:45 +0000947/* The stack can grow at most MAXINT deep, as co_nlocals and
948 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000949#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
950#define EMPTY() (STACK_LEVEL() == 0)
951#define TOP() (stack_pointer[-1])
952#define SECOND() (stack_pointer[-2])
953#define THIRD() (stack_pointer[-3])
954#define FOURTH() (stack_pointer[-4])
955#define PEEK(n) (stack_pointer[-(n)])
956#define SET_TOP(v) (stack_pointer[-1] = (v))
957#define SET_SECOND(v) (stack_pointer[-2] = (v))
958#define SET_THIRD(v) (stack_pointer[-3] = (v))
959#define SET_FOURTH(v) (stack_pointer[-4] = (v))
960#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
961#define BASIC_STACKADJ(n) (stack_pointer += n)
962#define BASIC_PUSH(v) (*stack_pointer++ = (v))
963#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000964
Guido van Rossum96a42c81992-01-12 02:29:51 +0000965#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +0200967 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000968 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +0200969#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000970 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +0000971#define STACK_GROW(n) do { \
972 assert(n >= 0); \
973 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +0200974 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +0000975 assert(STACK_LEVEL() <= co->co_stacksize); \
976 } while (0)
977#define STACK_SHRINK(n) do { \
978 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +0200979 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +0000980 (void)(BASIC_STACKADJ(-n)); \
981 assert(STACK_LEVEL() <= co->co_stacksize); \
982 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +0000983#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +0200984 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000985 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000986#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000987#define PUSH(v) BASIC_PUSH(v)
988#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +0000989#define STACK_GROW(n) BASIC_STACKADJ(n)
990#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000991#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000992#endif
993
Guido van Rossum681d79a1995-07-18 14:51:37 +0000994/* Local variable macros */
995
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000997
998/* The SETLOCAL() macro must not DECREF the local variable in-place and
999 then store the new value; it must copy the old value to a temporary
1000 value, then store the new value, and then DECREF the temporary value.
1001 This is because it is possible that during the DECREF the frame is
1002 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1003 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001005 GETLOCAL(i) = value; \
1006 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001007
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001008
1009#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 while (STACK_LEVEL() > (b)->b_level) { \
1011 PyObject *v = POP(); \
1012 Py_XDECREF(v); \
1013 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001014
1015#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001016 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +01001018 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1020 while (STACK_LEVEL() > (b)->b_level + 3) { \
1021 value = POP(); \
1022 Py_XDECREF(value); \
1023 } \
Mark Shannonae3087c2017-10-22 22:41:51 +01001024 exc_info = tstate->exc_info; \
1025 type = exc_info->exc_type; \
1026 value = exc_info->exc_value; \
1027 traceback = exc_info->exc_traceback; \
1028 exc_info->exc_type = POP(); \
1029 exc_info->exc_value = POP(); \
1030 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 Py_XDECREF(type); \
1032 Py_XDECREF(value); \
1033 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001034 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001035
Inada Naoki91234a12019-06-03 21:30:58 +09001036 /* macros for opcode cache */
1037#define OPCACHE_CHECK() \
1038 do { \
1039 co_opcache = NULL; \
1040 if (co->co_opcache != NULL) { \
1041 unsigned char co_opt_offset = \
1042 co->co_opcache_map[next_instr - first_instr]; \
1043 if (co_opt_offset > 0) { \
1044 assert(co_opt_offset <= co->co_opcache_size); \
1045 co_opcache = &co->co_opcache[co_opt_offset - 1]; \
1046 assert(co_opcache != NULL); \
Inada Naoki91234a12019-06-03 21:30:58 +09001047 } \
1048 } \
1049 } while (0)
1050
1051#if OPCACHE_STATS
1052
1053#define OPCACHE_STAT_GLOBAL_HIT() \
1054 do { \
1055 if (co->co_opcache != NULL) opcache_global_hits++; \
1056 } while (0)
1057
1058#define OPCACHE_STAT_GLOBAL_MISS() \
1059 do { \
1060 if (co->co_opcache != NULL) opcache_global_misses++; \
1061 } while (0)
1062
1063#define OPCACHE_STAT_GLOBAL_OPT() \
1064 do { \
1065 if (co->co_opcache != NULL) opcache_global_opts++; \
1066 } while (0)
1067
1068#else /* OPCACHE_STATS */
1069
1070#define OPCACHE_STAT_GLOBAL_HIT()
1071#define OPCACHE_STAT_GLOBAL_MISS()
1072#define OPCACHE_STAT_GLOBAL_OPT()
1073
1074#endif
1075
Guido van Rossuma027efa1997-05-05 20:56:21 +00001076/* Start of code */
1077
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 /* push frame */
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001079 if (_Py_EnterRecursiveCall(tstate, "")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001081 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001082
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 if (tstate->use_tracing) {
1086 if (tstate->c_tracefunc != NULL) {
1087 /* tstate->c_tracefunc, if defined, is a
1088 function that will be called on *every* entry
1089 to a code block. Its return value, if not
1090 None, is a function that will be called at
1091 the start of each executed line of code.
1092 (Actually, the function must return itself
1093 in order to continue tracing.) The trace
1094 functions are called with three arguments:
1095 a pointer to the current frame, a string
1096 indicating why the function is called, and
1097 an argument which depends on the situation.
1098 The global trace function is also called
1099 whenever an exception is detected. */
1100 if (call_trace_protected(tstate->c_tracefunc,
1101 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001102 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 /* Trace function raised an error */
1104 goto exit_eval_frame;
1105 }
1106 }
1107 if (tstate->c_profilefunc != NULL) {
1108 /* Similar for c_profilefunc, except it needn't
1109 return itself and isn't called for "line" events */
1110 if (call_trace_protected(tstate->c_profilefunc,
1111 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001112 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 /* Profile function raised an error */
1114 goto exit_eval_frame;
1115 }
1116 }
1117 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001118
Łukasz Langaa785c872016-09-09 17:37:37 -07001119 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1120 dtrace_function_entry(f);
1121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 co = f->f_code;
1123 names = co->co_names;
1124 consts = co->co_consts;
1125 fastlocals = f->f_localsplus;
1126 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001127 assert(PyBytes_Check(co->co_code));
1128 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001129 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1130 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1131 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001132 /*
1133 f->f_lasti refers to the index of the last instruction,
1134 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001135
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001136 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001137 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 When the PREDICT() macros are enabled, some opcode pairs follow in
1140 direct succession without updating f->f_lasti. A successful
1141 prediction effectively links the two codes together as if they
1142 were a single new opcode; accordingly,f->f_lasti will point to
1143 the first code in the pair (for instance, GET_ITER followed by
1144 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001145 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001147 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001148 next_instr = first_instr;
1149 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001150 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1151 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001152 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 stack_pointer = f->f_stacktop;
1154 assert(stack_pointer != NULL);
1155 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001156 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001157
Inada Naoki91234a12019-06-03 21:30:58 +09001158 if (co->co_opcache_flag < OPCACHE_MIN_RUNS) {
1159 co->co_opcache_flag++;
1160 if (co->co_opcache_flag == OPCACHE_MIN_RUNS) {
1161 if (_PyCode_InitOpcache(co) < 0) {
1162 return NULL;
1163 }
1164#if OPCACHE_STATS
1165 opcache_code_objects_extra_mem +=
1166 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1167 sizeof(_PyOpcache) * co->co_opcache_size;
1168 opcache_code_objects++;
1169#endif
1170 }
1171 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001172
Tim Peters5ca576e2001-06-18 22:08:13 +00001173#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001174 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001175#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001176
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001177 if (throwflag) /* support for generator.throw() */
1178 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001179
Victor Stinnerace47d72013-07-18 01:41:08 +02001180#ifdef Py_DEBUG
Victor Stinner0b72b232020-03-12 23:18:39 +01001181 /* _PyEval_EvalFrameDefault() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001182 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001183 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001184 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001185#endif
1186
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001187main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1190 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001191 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 /* Do periodic things. Doing this every time through
1194 the loop would add too much overhead, so we do it
1195 only every Nth instruction. We also do it if
1196 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1197 event needs attention (e.g. a signal handler or
1198 async I/O handler); see Py_AddPendingCall() and
1199 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001200
Eric Snow7bda9de2019-03-08 17:25:54 -07001201 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001202 opcode = _Py_OPCODE(*next_instr);
1203 if (opcode == SETUP_FINALLY ||
1204 opcode == SETUP_WITH ||
1205 opcode == BEFORE_ASYNC_WITH ||
1206 opcode == YIELD_FROM) {
1207 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001208 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001209 - If we're about to enter the 'with:'. It will prevent
1210 emitting a resource warning in the common idiom
1211 'with open(path) as file:'.
1212 - If we're about to enter the 'async with:'.
1213 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001214 *very* useful, but might help in some cases and it's
1215 traditional)
1216 - If we're resuming a chain of nested 'yield from' or
1217 'await' calls, then each frame is parked with YIELD_FROM
1218 as its next opcode. If the user hit control-C we want to
1219 wait until we've reached the innermost frame before
1220 running the signal handler and raising KeyboardInterrupt
1221 (see bpo-30039).
1222 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 goto fast_next_opcode;
1224 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001225
Victor Stinnere225beb2019-06-03 18:14:24 +02001226 if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
Victor Stinner09532fe2019-05-10 23:39:09 +02001227 if (handle_signals(runtime) != 0) {
Eric Snowfdf282d2019-01-11 14:26:55 -07001228 goto error;
1229 }
1230 }
Victor Stinnere225beb2019-06-03 18:14:24 +02001231 if (_Py_atomic_load_relaxed(&ceval->pending.calls_to_do)) {
1232 if (make_pending_calls(runtime) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001233 goto error;
Eric Snowfdf282d2019-01-11 14:26:55 -07001234 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001236
Victor Stinnere225beb2019-06-03 18:14:24 +02001237 if (_Py_atomic_load_relaxed(&ceval->gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001238 /* Give another thread a chance */
Victor Stinner09532fe2019-05-10 23:39:09 +02001239 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +01001240 Py_FatalError("tstate mix-up");
Victor Stinner09532fe2019-05-10 23:39:09 +02001241 }
Victor Stinnere225beb2019-06-03 18:14:24 +02001242 drop_gil(ceval, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243
1244 /* Other threads may run now */
1245
Victor Stinner85f5a692020-03-09 22:12:04 +01001246 take_gil(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +01001247
Victor Stinner09532fe2019-05-10 23:39:09 +02001248 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +01001249 Py_FatalError("orphan tstate");
Victor Stinner09532fe2019-05-10 23:39:09 +02001250 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 }
1252 /* Check for asynchronous exceptions. */
1253 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001254 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 tstate->async_exc = NULL;
Victor Stinnere225beb2019-06-03 18:14:24 +02001256 UNSIGNAL_ASYNC_EXC(ceval);
Victor Stinner438a12d2019-05-24 17:01:38 +02001257 _PyErr_SetNone(tstate, exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001258 Py_DECREF(exc);
1259 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 }
1261 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 fast_next_opcode:
1264 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001265
Łukasz Langaa785c872016-09-09 17:37:37 -07001266 if (PyDTrace_LINE_ENABLED())
1267 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001270
Victor Stinnere225beb2019-06-03 18:14:24 +02001271 if (_Py_TracingPossible(ceval) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001272 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001273 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 /* see maybe_call_line_trace
1275 for expository comments */
1276 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 err = maybe_call_line_trace(tstate->c_tracefunc,
1279 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001280 tstate, f,
1281 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 /* Reload possibly changed frame fields */
1283 JUMPTO(f->f_lasti);
1284 if (f->f_stacktop != NULL) {
1285 stack_pointer = f->f_stacktop;
1286 f->f_stacktop = NULL;
1287 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001288 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001290 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001294
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001295 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001296 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001297#ifdef DYNAMIC_EXECUTION_PROFILE
1298#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 dxpairs[lastopcode][opcode]++;
1300 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001301#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001302 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001303#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001304
Guido van Rossum96a42c81992-01-12 02:29:51 +00001305#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 if (lltrace) {
1309 if (HAS_ARG(opcode)) {
1310 printf("%d: %d, %d\n",
1311 f->f_lasti, opcode, oparg);
1312 }
1313 else {
1314 printf("%d: %d\n",
1315 f->f_lasti, opcode);
1316 }
1317 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001318#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001323 It is essential that any operation that fails must goto error
1324 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001325
Benjamin Petersonddd19492018-09-16 22:38:02 -07001326 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001328 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001329
Benjamin Petersonddd19492018-09-16 22:38:02 -07001330 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001331 PyObject *value = GETLOCAL(oparg);
1332 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001333 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001334 UNBOUNDLOCAL_ERROR_MSG,
1335 PyTuple_GetItem(co->co_varnames, oparg));
1336 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001338 Py_INCREF(value);
1339 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001341 }
1342
Benjamin Petersonddd19492018-09-16 22:38:02 -07001343 case TARGET(LOAD_CONST): {
1344 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001345 PyObject *value = GETITEM(consts, oparg);
1346 Py_INCREF(value);
1347 PUSH(value);
1348 FAST_DISPATCH();
1349 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001350
Benjamin Petersonddd19492018-09-16 22:38:02 -07001351 case TARGET(STORE_FAST): {
1352 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001353 PyObject *value = POP();
1354 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001356 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001357
Benjamin Petersonddd19492018-09-16 22:38:02 -07001358 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001359 PyObject *value = POP();
1360 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001361 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001362 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001363
Benjamin Petersonddd19492018-09-16 22:38:02 -07001364 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001365 PyObject *top = TOP();
1366 PyObject *second = SECOND();
1367 SET_TOP(second);
1368 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001370 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001371
Benjamin Petersonddd19492018-09-16 22:38:02 -07001372 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001373 PyObject *top = TOP();
1374 PyObject *second = SECOND();
1375 PyObject *third = THIRD();
1376 SET_TOP(second);
1377 SET_SECOND(third);
1378 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001379 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001380 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001381
Benjamin Petersonddd19492018-09-16 22:38:02 -07001382 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001383 PyObject *top = TOP();
1384 PyObject *second = SECOND();
1385 PyObject *third = THIRD();
1386 PyObject *fourth = FOURTH();
1387 SET_TOP(second);
1388 SET_SECOND(third);
1389 SET_THIRD(fourth);
1390 SET_FOURTH(top);
1391 FAST_DISPATCH();
1392 }
1393
Benjamin Petersonddd19492018-09-16 22:38:02 -07001394 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001395 PyObject *top = TOP();
1396 Py_INCREF(top);
1397 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001399 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001400
Benjamin Petersonddd19492018-09-16 22:38:02 -07001401 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001402 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001403 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001404 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001405 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001406 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001407 SET_TOP(top);
1408 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001409 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001410 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001411
Benjamin Petersonddd19492018-09-16 22:38:02 -07001412 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001413 PyObject *value = TOP();
1414 PyObject *res = PyNumber_Positive(value);
1415 Py_DECREF(value);
1416 SET_TOP(res);
1417 if (res == NULL)
1418 goto error;
1419 DISPATCH();
1420 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001421
Benjamin Petersonddd19492018-09-16 22:38:02 -07001422 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001423 PyObject *value = TOP();
1424 PyObject *res = PyNumber_Negative(value);
1425 Py_DECREF(value);
1426 SET_TOP(res);
1427 if (res == NULL)
1428 goto error;
1429 DISPATCH();
1430 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001431
Benjamin Petersonddd19492018-09-16 22:38:02 -07001432 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001433 PyObject *value = TOP();
1434 int err = PyObject_IsTrue(value);
1435 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 if (err == 0) {
1437 Py_INCREF(Py_True);
1438 SET_TOP(Py_True);
1439 DISPATCH();
1440 }
1441 else if (err > 0) {
1442 Py_INCREF(Py_False);
1443 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001444 DISPATCH();
1445 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001446 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001447 goto error;
1448 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001449
Benjamin Petersonddd19492018-09-16 22:38:02 -07001450 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001451 PyObject *value = TOP();
1452 PyObject *res = PyNumber_Invert(value);
1453 Py_DECREF(value);
1454 SET_TOP(res);
1455 if (res == NULL)
1456 goto error;
1457 DISPATCH();
1458 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001459
Benjamin Petersonddd19492018-09-16 22:38:02 -07001460 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001461 PyObject *exp = POP();
1462 PyObject *base = TOP();
1463 PyObject *res = PyNumber_Power(base, exp, Py_None);
1464 Py_DECREF(base);
1465 Py_DECREF(exp);
1466 SET_TOP(res);
1467 if (res == NULL)
1468 goto error;
1469 DISPATCH();
1470 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001471
Benjamin Petersonddd19492018-09-16 22:38:02 -07001472 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001473 PyObject *right = POP();
1474 PyObject *left = TOP();
1475 PyObject *res = PyNumber_Multiply(left, right);
1476 Py_DECREF(left);
1477 Py_DECREF(right);
1478 SET_TOP(res);
1479 if (res == NULL)
1480 goto error;
1481 DISPATCH();
1482 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001483
Benjamin Petersonddd19492018-09-16 22:38:02 -07001484 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001485 PyObject *right = POP();
1486 PyObject *left = TOP();
1487 PyObject *res = PyNumber_MatrixMultiply(left, right);
1488 Py_DECREF(left);
1489 Py_DECREF(right);
1490 SET_TOP(res);
1491 if (res == NULL)
1492 goto error;
1493 DISPATCH();
1494 }
1495
Benjamin Petersonddd19492018-09-16 22:38:02 -07001496 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001497 PyObject *divisor = POP();
1498 PyObject *dividend = TOP();
1499 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1500 Py_DECREF(dividend);
1501 Py_DECREF(divisor);
1502 SET_TOP(quotient);
1503 if (quotient == NULL)
1504 goto error;
1505 DISPATCH();
1506 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001507
Benjamin Petersonddd19492018-09-16 22:38:02 -07001508 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001509 PyObject *divisor = POP();
1510 PyObject *dividend = TOP();
1511 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1512 Py_DECREF(dividend);
1513 Py_DECREF(divisor);
1514 SET_TOP(quotient);
1515 if (quotient == NULL)
1516 goto error;
1517 DISPATCH();
1518 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001519
Benjamin Petersonddd19492018-09-16 22:38:02 -07001520 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001521 PyObject *divisor = POP();
1522 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001523 PyObject *res;
1524 if (PyUnicode_CheckExact(dividend) && (
1525 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1526 // fast path; string formatting, but not if the RHS is a str subclass
1527 // (see issue28598)
1528 res = PyUnicode_Format(dividend, divisor);
1529 } else {
1530 res = PyNumber_Remainder(dividend, divisor);
1531 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001532 Py_DECREF(divisor);
1533 Py_DECREF(dividend);
1534 SET_TOP(res);
1535 if (res == NULL)
1536 goto error;
1537 DISPATCH();
1538 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001539
Benjamin Petersonddd19492018-09-16 22:38:02 -07001540 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001541 PyObject *right = POP();
1542 PyObject *left = TOP();
1543 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001544 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1545 CPython using bytecode, it is simply worthless.
1546 See http://bugs.python.org/issue21955 and
1547 http://bugs.python.org/issue10044 for the discussion. In short,
1548 no patch shown any impact on a realistic benchmark, only a minor
1549 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001550 if (PyUnicode_CheckExact(left) &&
1551 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001552 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001553 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001554 }
1555 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001556 sum = PyNumber_Add(left, right);
1557 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001558 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001559 Py_DECREF(right);
1560 SET_TOP(sum);
1561 if (sum == NULL)
1562 goto error;
1563 DISPATCH();
1564 }
1565
Benjamin Petersonddd19492018-09-16 22:38:02 -07001566 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001567 PyObject *right = POP();
1568 PyObject *left = TOP();
1569 PyObject *diff = PyNumber_Subtract(left, right);
1570 Py_DECREF(right);
1571 Py_DECREF(left);
1572 SET_TOP(diff);
1573 if (diff == NULL)
1574 goto error;
1575 DISPATCH();
1576 }
1577
Benjamin Petersonddd19492018-09-16 22:38:02 -07001578 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001579 PyObject *sub = POP();
1580 PyObject *container = TOP();
1581 PyObject *res = PyObject_GetItem(container, sub);
1582 Py_DECREF(container);
1583 Py_DECREF(sub);
1584 SET_TOP(res);
1585 if (res == NULL)
1586 goto error;
1587 DISPATCH();
1588 }
1589
Benjamin Petersonddd19492018-09-16 22:38:02 -07001590 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001591 PyObject *right = POP();
1592 PyObject *left = TOP();
1593 PyObject *res = PyNumber_Lshift(left, right);
1594 Py_DECREF(left);
1595 Py_DECREF(right);
1596 SET_TOP(res);
1597 if (res == NULL)
1598 goto error;
1599 DISPATCH();
1600 }
1601
Benjamin Petersonddd19492018-09-16 22:38:02 -07001602 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001603 PyObject *right = POP();
1604 PyObject *left = TOP();
1605 PyObject *res = PyNumber_Rshift(left, right);
1606 Py_DECREF(left);
1607 Py_DECREF(right);
1608 SET_TOP(res);
1609 if (res == NULL)
1610 goto error;
1611 DISPATCH();
1612 }
1613
Benjamin Petersonddd19492018-09-16 22:38:02 -07001614 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001615 PyObject *right = POP();
1616 PyObject *left = TOP();
1617 PyObject *res = PyNumber_And(left, right);
1618 Py_DECREF(left);
1619 Py_DECREF(right);
1620 SET_TOP(res);
1621 if (res == NULL)
1622 goto error;
1623 DISPATCH();
1624 }
1625
Benjamin Petersonddd19492018-09-16 22:38:02 -07001626 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001627 PyObject *right = POP();
1628 PyObject *left = TOP();
1629 PyObject *res = PyNumber_Xor(left, right);
1630 Py_DECREF(left);
1631 Py_DECREF(right);
1632 SET_TOP(res);
1633 if (res == NULL)
1634 goto error;
1635 DISPATCH();
1636 }
1637
Benjamin Petersonddd19492018-09-16 22:38:02 -07001638 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001639 PyObject *right = POP();
1640 PyObject *left = TOP();
1641 PyObject *res = PyNumber_Or(left, right);
1642 Py_DECREF(left);
1643 Py_DECREF(right);
1644 SET_TOP(res);
1645 if (res == NULL)
1646 goto error;
1647 DISPATCH();
1648 }
1649
Benjamin Petersonddd19492018-09-16 22:38:02 -07001650 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001651 PyObject *v = POP();
1652 PyObject *list = PEEK(oparg);
1653 int err;
1654 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001655 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001656 if (err != 0)
1657 goto error;
1658 PREDICT(JUMP_ABSOLUTE);
1659 DISPATCH();
1660 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001661
Benjamin Petersonddd19492018-09-16 22:38:02 -07001662 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001663 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001664 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001665 int err;
1666 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001667 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001668 if (err != 0)
1669 goto error;
1670 PREDICT(JUMP_ABSOLUTE);
1671 DISPATCH();
1672 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001673
Benjamin Petersonddd19492018-09-16 22:38:02 -07001674 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001675 PyObject *exp = POP();
1676 PyObject *base = TOP();
1677 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1678 Py_DECREF(base);
1679 Py_DECREF(exp);
1680 SET_TOP(res);
1681 if (res == NULL)
1682 goto error;
1683 DISPATCH();
1684 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001685
Benjamin Petersonddd19492018-09-16 22:38:02 -07001686 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001687 PyObject *right = POP();
1688 PyObject *left = TOP();
1689 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1690 Py_DECREF(left);
1691 Py_DECREF(right);
1692 SET_TOP(res);
1693 if (res == NULL)
1694 goto error;
1695 DISPATCH();
1696 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001697
Benjamin Petersonddd19492018-09-16 22:38:02 -07001698 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001699 PyObject *right = POP();
1700 PyObject *left = TOP();
1701 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1702 Py_DECREF(left);
1703 Py_DECREF(right);
1704 SET_TOP(res);
1705 if (res == NULL)
1706 goto error;
1707 DISPATCH();
1708 }
1709
Benjamin Petersonddd19492018-09-16 22:38:02 -07001710 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001711 PyObject *divisor = POP();
1712 PyObject *dividend = TOP();
1713 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1714 Py_DECREF(dividend);
1715 Py_DECREF(divisor);
1716 SET_TOP(quotient);
1717 if (quotient == NULL)
1718 goto error;
1719 DISPATCH();
1720 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001721
Benjamin Petersonddd19492018-09-16 22:38:02 -07001722 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001723 PyObject *divisor = POP();
1724 PyObject *dividend = TOP();
1725 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1726 Py_DECREF(dividend);
1727 Py_DECREF(divisor);
1728 SET_TOP(quotient);
1729 if (quotient == NULL)
1730 goto error;
1731 DISPATCH();
1732 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001733
Benjamin Petersonddd19492018-09-16 22:38:02 -07001734 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001735 PyObject *right = POP();
1736 PyObject *left = TOP();
1737 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1738 Py_DECREF(left);
1739 Py_DECREF(right);
1740 SET_TOP(mod);
1741 if (mod == NULL)
1742 goto error;
1743 DISPATCH();
1744 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001745
Benjamin Petersonddd19492018-09-16 22:38:02 -07001746 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001747 PyObject *right = POP();
1748 PyObject *left = TOP();
1749 PyObject *sum;
1750 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001751 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001752 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001753 }
1754 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001755 sum = PyNumber_InPlaceAdd(left, right);
1756 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001757 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001758 Py_DECREF(right);
1759 SET_TOP(sum);
1760 if (sum == NULL)
1761 goto error;
1762 DISPATCH();
1763 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001764
Benjamin Petersonddd19492018-09-16 22:38:02 -07001765 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001766 PyObject *right = POP();
1767 PyObject *left = TOP();
1768 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1769 Py_DECREF(left);
1770 Py_DECREF(right);
1771 SET_TOP(diff);
1772 if (diff == NULL)
1773 goto error;
1774 DISPATCH();
1775 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001776
Benjamin Petersonddd19492018-09-16 22:38:02 -07001777 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001778 PyObject *right = POP();
1779 PyObject *left = TOP();
1780 PyObject *res = PyNumber_InPlaceLshift(left, right);
1781 Py_DECREF(left);
1782 Py_DECREF(right);
1783 SET_TOP(res);
1784 if (res == NULL)
1785 goto error;
1786 DISPATCH();
1787 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001788
Benjamin Petersonddd19492018-09-16 22:38:02 -07001789 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001790 PyObject *right = POP();
1791 PyObject *left = TOP();
1792 PyObject *res = PyNumber_InPlaceRshift(left, right);
1793 Py_DECREF(left);
1794 Py_DECREF(right);
1795 SET_TOP(res);
1796 if (res == NULL)
1797 goto error;
1798 DISPATCH();
1799 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001800
Benjamin Petersonddd19492018-09-16 22:38:02 -07001801 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001802 PyObject *right = POP();
1803 PyObject *left = TOP();
1804 PyObject *res = PyNumber_InPlaceAnd(left, right);
1805 Py_DECREF(left);
1806 Py_DECREF(right);
1807 SET_TOP(res);
1808 if (res == NULL)
1809 goto error;
1810 DISPATCH();
1811 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001812
Benjamin Petersonddd19492018-09-16 22:38:02 -07001813 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001814 PyObject *right = POP();
1815 PyObject *left = TOP();
1816 PyObject *res = PyNumber_InPlaceXor(left, right);
1817 Py_DECREF(left);
1818 Py_DECREF(right);
1819 SET_TOP(res);
1820 if (res == NULL)
1821 goto error;
1822 DISPATCH();
1823 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001824
Benjamin Petersonddd19492018-09-16 22:38:02 -07001825 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001826 PyObject *right = POP();
1827 PyObject *left = TOP();
1828 PyObject *res = PyNumber_InPlaceOr(left, right);
1829 Py_DECREF(left);
1830 Py_DECREF(right);
1831 SET_TOP(res);
1832 if (res == NULL)
1833 goto error;
1834 DISPATCH();
1835 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001836
Benjamin Petersonddd19492018-09-16 22:38:02 -07001837 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001838 PyObject *sub = TOP();
1839 PyObject *container = SECOND();
1840 PyObject *v = THIRD();
1841 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001842 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001843 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001844 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001845 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001846 Py_DECREF(container);
1847 Py_DECREF(sub);
1848 if (err != 0)
1849 goto error;
1850 DISPATCH();
1851 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001852
Benjamin Petersonddd19492018-09-16 22:38:02 -07001853 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001854 PyObject *sub = TOP();
1855 PyObject *container = SECOND();
1856 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001857 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00001858 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001859 err = PyObject_DelItem(container, sub);
1860 Py_DECREF(container);
1861 Py_DECREF(sub);
1862 if (err != 0)
1863 goto error;
1864 DISPATCH();
1865 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001866
Benjamin Petersonddd19492018-09-16 22:38:02 -07001867 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01001868 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001869 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001870 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001871 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001872 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001873 _PyErr_SetString(tstate, PyExc_RuntimeError,
1874 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001875 Py_DECREF(value);
1876 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001877 }
Petr Viktorinffd97532020-02-11 17:46:57 +01001878 res = PyObject_CallOneArg(hook, value);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001879 Py_DECREF(value);
1880 if (res == NULL)
1881 goto error;
1882 Py_DECREF(res);
1883 DISPATCH();
1884 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001885
Benjamin Petersonddd19492018-09-16 22:38:02 -07001886 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001887 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 switch (oparg) {
1889 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001890 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001891 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001893 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001894 /* fall through */
1895 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02001896 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001897 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001898 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001899 break;
1900 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02001901 _PyErr_SetString(tstate, PyExc_SystemError,
1902 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903 break;
1904 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001905 goto error;
1906 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001907
Benjamin Petersonddd19492018-09-16 22:38:02 -07001908 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001909 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001910 assert(f->f_iblock == 0);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00001911 assert(EMPTY());
1912 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001913 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001914
Benjamin Petersonddd19492018-09-16 22:38:02 -07001915 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001916 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001917 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001918 PyObject *obj = TOP();
1919 PyTypeObject *type = Py_TYPE(obj);
1920
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001921 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001922 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001923 }
Yury Selivanov75445082015-05-11 22:57:16 -04001924
1925 if (getter != NULL) {
1926 iter = (*getter)(obj);
1927 Py_DECREF(obj);
1928 if (iter == NULL) {
1929 SET_TOP(NULL);
1930 goto error;
1931 }
1932 }
1933 else {
1934 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02001935 _PyErr_Format(tstate, PyExc_TypeError,
1936 "'async for' requires an object with "
1937 "__aiter__ method, got %.100s",
1938 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001939 Py_DECREF(obj);
1940 goto error;
1941 }
1942
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001943 if (Py_TYPE(iter)->tp_as_async == NULL ||
1944 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001945
Yury Selivanov398ff912017-03-02 22:20:00 -05001946 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02001947 _PyErr_Format(tstate, PyExc_TypeError,
1948 "'async for' received an object from __aiter__ "
1949 "that does not implement __anext__: %.100s",
1950 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001951 Py_DECREF(iter);
1952 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001953 }
1954
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001955 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001956 DISPATCH();
1957 }
1958
Benjamin Petersonddd19492018-09-16 22:38:02 -07001959 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001960 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001961 PyObject *next_iter = NULL;
1962 PyObject *awaitable = NULL;
1963 PyObject *aiter = TOP();
1964 PyTypeObject *type = Py_TYPE(aiter);
1965
Yury Selivanoveb636452016-09-08 22:01:51 -07001966 if (PyAsyncGen_CheckExact(aiter)) {
1967 awaitable = type->tp_as_async->am_anext(aiter);
1968 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001969 goto error;
1970 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001971 } else {
1972 if (type->tp_as_async != NULL){
1973 getter = type->tp_as_async->am_anext;
1974 }
Yury Selivanov75445082015-05-11 22:57:16 -04001975
Yury Selivanoveb636452016-09-08 22:01:51 -07001976 if (getter != NULL) {
1977 next_iter = (*getter)(aiter);
1978 if (next_iter == NULL) {
1979 goto error;
1980 }
1981 }
1982 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02001983 _PyErr_Format(tstate, PyExc_TypeError,
1984 "'async for' requires an iterator with "
1985 "__anext__ method, got %.100s",
1986 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07001987 goto error;
1988 }
Yury Selivanov75445082015-05-11 22:57:16 -04001989
Yury Selivanoveb636452016-09-08 22:01:51 -07001990 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1991 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001992 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001993 PyExc_TypeError,
1994 "'async for' received an invalid object "
1995 "from __anext__: %.100s",
1996 Py_TYPE(next_iter)->tp_name);
1997
1998 Py_DECREF(next_iter);
1999 goto error;
2000 } else {
2001 Py_DECREF(next_iter);
2002 }
2003 }
Yury Selivanov75445082015-05-11 22:57:16 -04002004
2005 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002006 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002007 DISPATCH();
2008 }
2009
Benjamin Petersonddd19492018-09-16 22:38:02 -07002010 case TARGET(GET_AWAITABLE): {
2011 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002012 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002013 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002014
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002015 if (iter == NULL) {
Mark Shannonfee55262019-11-21 09:11:43 +00002016 int opcode_at_minus_3 = 0;
2017 if ((next_instr - first_instr) > 2) {
2018 opcode_at_minus_3 = _Py_OPCODE(next_instr[-3]);
2019 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002020 format_awaitable_error(tstate, Py_TYPE(iterable),
Mark Shannonfee55262019-11-21 09:11:43 +00002021 opcode_at_minus_3,
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002022 _Py_OPCODE(next_instr[-2]));
2023 }
2024
Yury Selivanov75445082015-05-11 22:57:16 -04002025 Py_DECREF(iterable);
2026
Yury Selivanovc724bae2016-03-02 11:30:46 -05002027 if (iter != NULL && PyCoro_CheckExact(iter)) {
2028 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2029 if (yf != NULL) {
2030 /* `iter` is a coroutine object that is being
2031 awaited, `yf` is a pointer to the current awaitable
2032 being awaited on. */
2033 Py_DECREF(yf);
2034 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002035 _PyErr_SetString(tstate, PyExc_RuntimeError,
2036 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002037 /* The code below jumps to `error` if `iter` is NULL. */
2038 }
2039 }
2040
Yury Selivanov75445082015-05-11 22:57:16 -04002041 SET_TOP(iter); /* Even if it's NULL */
2042
2043 if (iter == NULL) {
2044 goto error;
2045 }
2046
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002047 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002048 DISPATCH();
2049 }
2050
Benjamin Petersonddd19492018-09-16 22:38:02 -07002051 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002052 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002053 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002054 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002055 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
2056 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002057 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002058 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002059 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002060 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002061 else
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02002062 retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002063 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002064 Py_DECREF(v);
2065 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002066 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002067 if (tstate->c_tracefunc != NULL
Victor Stinner438a12d2019-05-24 17:01:38 +02002068 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002069 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002070 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002071 if (err < 0)
2072 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002073 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002074 SET_TOP(val);
2075 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002076 }
Martin Panter95f53c12016-07-18 08:23:26 +00002077 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002078 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002079 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002080 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002081 f->f_lasti -= sizeof(_Py_CODEUNIT);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002082 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002083 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002084
Benjamin Petersonddd19492018-09-16 22:38:02 -07002085 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002086 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002087
2088 if (co->co_flags & CO_ASYNC_GENERATOR) {
2089 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2090 Py_DECREF(retval);
2091 if (w == NULL) {
2092 retval = NULL;
2093 goto error;
2094 }
2095 retval = w;
2096 }
2097
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002098 f->f_stacktop = stack_pointer;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002099 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002100 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002101
Benjamin Petersonddd19492018-09-16 22:38:02 -07002102 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002103 PyObject *type, *value, *traceback;
2104 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002105 PyTryBlock *b = PyFrame_BlockPop(f);
2106 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002107 _PyErr_SetString(tstate, PyExc_SystemError,
2108 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002109 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002110 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002111 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2112 STACK_LEVEL() <= (b)->b_level + 4);
2113 exc_info = tstate->exc_info;
2114 type = exc_info->exc_type;
2115 value = exc_info->exc_value;
2116 traceback = exc_info->exc_traceback;
2117 exc_info->exc_type = POP();
2118 exc_info->exc_value = POP();
2119 exc_info->exc_traceback = POP();
2120 Py_XDECREF(type);
2121 Py_XDECREF(value);
2122 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002123 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002124 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002125
Benjamin Petersonddd19492018-09-16 22:38:02 -07002126 case TARGET(POP_BLOCK): {
2127 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002128 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002129 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002130 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002131
Mark Shannonfee55262019-11-21 09:11:43 +00002132 case TARGET(RERAISE): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002133 PyObject *exc = POP();
Mark Shannonfee55262019-11-21 09:11:43 +00002134 PyObject *val = POP();
2135 PyObject *tb = POP();
2136 assert(PyExceptionClass_Check(exc));
Victor Stinner61f4db82020-01-28 03:37:45 +01002137 _PyErr_Restore(tstate, exc, val, tb);
Mark Shannonfee55262019-11-21 09:11:43 +00002138 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002139 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002140
Benjamin Petersonddd19492018-09-16 22:38:02 -07002141 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002142 PyObject *exc = POP();
2143 assert(PyExceptionClass_Check(exc));
2144 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2145 PyTryBlock *b = PyFrame_BlockPop(f);
2146 assert(b->b_type == EXCEPT_HANDLER);
2147 Py_DECREF(exc);
2148 UNWIND_EXCEPT_HANDLER(b);
2149 Py_DECREF(POP());
2150 JUMPBY(oparg);
2151 FAST_DISPATCH();
2152 }
2153 else {
2154 PyObject *val = POP();
2155 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002156 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002157 goto exception_unwind;
2158 }
2159 }
2160
Zackery Spytzce6a0702019-08-25 03:44:09 -06002161 case TARGET(LOAD_ASSERTION_ERROR): {
2162 PyObject *value = PyExc_AssertionError;
2163 Py_INCREF(value);
2164 PUSH(value);
2165 FAST_DISPATCH();
2166 }
2167
Benjamin Petersonddd19492018-09-16 22:38:02 -07002168 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002169 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002170
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002171 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002172 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002173 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002174 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002175 if (!_PyErr_Occurred(tstate)) {
2176 _PyErr_SetString(tstate, PyExc_NameError,
2177 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002178 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002179 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002180 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002181 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002182 }
2183 else {
2184 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2185 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002186 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002187 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2188 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002189 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2190 _PyErr_SetString(tstate, PyExc_NameError,
2191 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002192 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002193 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002194 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002195 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002196 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002197 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002198
Benjamin Petersonddd19492018-09-16 22:38:02 -07002199 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002200 PyObject *name = GETITEM(names, oparg);
2201 PyObject *v = POP();
2202 PyObject *ns = f->f_locals;
2203 int err;
2204 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002205 _PyErr_Format(tstate, PyExc_SystemError,
2206 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002207 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002208 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002209 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002210 if (PyDict_CheckExact(ns))
2211 err = PyDict_SetItem(ns, name, v);
2212 else
2213 err = PyObject_SetItem(ns, name, v);
2214 Py_DECREF(v);
2215 if (err != 0)
2216 goto error;
2217 DISPATCH();
2218 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002219
Benjamin Petersonddd19492018-09-16 22:38:02 -07002220 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002221 PyObject *name = GETITEM(names, oparg);
2222 PyObject *ns = f->f_locals;
2223 int err;
2224 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002225 _PyErr_Format(tstate, PyExc_SystemError,
2226 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002227 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002228 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002229 err = PyObject_DelItem(ns, name);
2230 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002231 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002232 NAME_ERROR_MSG,
2233 name);
2234 goto error;
2235 }
2236 DISPATCH();
2237 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002238
Benjamin Petersonddd19492018-09-16 22:38:02 -07002239 case TARGET(UNPACK_SEQUENCE): {
2240 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002241 PyObject *seq = POP(), *item, **items;
2242 if (PyTuple_CheckExact(seq) &&
2243 PyTuple_GET_SIZE(seq) == oparg) {
2244 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002245 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002246 item = items[oparg];
2247 Py_INCREF(item);
2248 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002249 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002250 } else if (PyList_CheckExact(seq) &&
2251 PyList_GET_SIZE(seq) == oparg) {
2252 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002253 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002254 item = items[oparg];
2255 Py_INCREF(item);
2256 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002257 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002258 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002259 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002260 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002261 } else {
2262 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002263 Py_DECREF(seq);
2264 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002265 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002266 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002267 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002268 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002269
Benjamin Petersonddd19492018-09-16 22:38:02 -07002270 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002271 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2272 PyObject *seq = POP();
2273
Victor Stinner438a12d2019-05-24 17:01:38 +02002274 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002275 stack_pointer + totalargs)) {
2276 stack_pointer += totalargs;
2277 } else {
2278 Py_DECREF(seq);
2279 goto error;
2280 }
2281 Py_DECREF(seq);
2282 DISPATCH();
2283 }
2284
Benjamin Petersonddd19492018-09-16 22:38:02 -07002285 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002286 PyObject *name = GETITEM(names, oparg);
2287 PyObject *owner = TOP();
2288 PyObject *v = SECOND();
2289 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002290 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002291 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002292 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002293 Py_DECREF(owner);
2294 if (err != 0)
2295 goto error;
2296 DISPATCH();
2297 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002298
Benjamin Petersonddd19492018-09-16 22:38:02 -07002299 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002300 PyObject *name = GETITEM(names, oparg);
2301 PyObject *owner = POP();
2302 int err;
2303 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2304 Py_DECREF(owner);
2305 if (err != 0)
2306 goto error;
2307 DISPATCH();
2308 }
2309
Benjamin Petersonddd19492018-09-16 22:38:02 -07002310 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002311 PyObject *name = GETITEM(names, oparg);
2312 PyObject *v = POP();
2313 int err;
2314 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002315 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002316 if (err != 0)
2317 goto error;
2318 DISPATCH();
2319 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002320
Benjamin Petersonddd19492018-09-16 22:38:02 -07002321 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002322 PyObject *name = GETITEM(names, oparg);
2323 int err;
2324 err = PyDict_DelItem(f->f_globals, name);
2325 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002326 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2327 format_exc_check_arg(tstate, PyExc_NameError,
2328 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002329 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002330 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002331 }
2332 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002333 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002334
Benjamin Petersonddd19492018-09-16 22:38:02 -07002335 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002336 PyObject *name = GETITEM(names, oparg);
2337 PyObject *locals = f->f_locals;
2338 PyObject *v;
2339 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002340 _PyErr_Format(tstate, PyExc_SystemError,
2341 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002342 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002343 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002344 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002345 v = PyDict_GetItemWithError(locals, name);
2346 if (v != NULL) {
2347 Py_INCREF(v);
2348 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002349 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002350 goto error;
2351 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002352 }
2353 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002354 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002355 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002356 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002357 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002358 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002359 }
2360 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002361 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002362 v = PyDict_GetItemWithError(f->f_globals, name);
2363 if (v != NULL) {
2364 Py_INCREF(v);
2365 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002366 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002367 goto error;
2368 }
2369 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002370 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002371 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002372 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002373 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002374 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002375 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002376 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002377 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002378 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002379 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002380 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002381 }
2382 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002383 v = PyObject_GetItem(f->f_builtins, name);
2384 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002385 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002386 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002387 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002388 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002389 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002390 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002391 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002392 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002393 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002394 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002395 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002396 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002397 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002398
Benjamin Petersonddd19492018-09-16 22:38:02 -07002399 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002400 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002401 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002402 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002403 && PyDict_CheckExact(f->f_builtins))
2404 {
Inada Naoki91234a12019-06-03 21:30:58 +09002405 OPCACHE_CHECK();
2406 if (co_opcache != NULL && co_opcache->optimized > 0) {
2407 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2408
2409 if (lg->globals_ver ==
2410 ((PyDictObject *)f->f_globals)->ma_version_tag
2411 && lg->builtins_ver ==
2412 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2413 {
2414 PyObject *ptr = lg->ptr;
2415 OPCACHE_STAT_GLOBAL_HIT();
2416 assert(ptr != NULL);
2417 Py_INCREF(ptr);
2418 PUSH(ptr);
2419 DISPATCH();
2420 }
2421 }
2422
2423 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002424 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002425 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002426 name);
2427 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002428 if (!_PyErr_OCCURRED()) {
2429 /* _PyDict_LoadGlobal() returns NULL without raising
2430 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002431 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002432 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002433 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002434 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002435 }
Inada Naoki91234a12019-06-03 21:30:58 +09002436
2437 if (co_opcache != NULL) {
2438 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2439
2440 if (co_opcache->optimized == 0) {
2441 /* Wasn't optimized before. */
2442 OPCACHE_STAT_GLOBAL_OPT();
2443 } else {
2444 OPCACHE_STAT_GLOBAL_MISS();
2445 }
2446
2447 co_opcache->optimized = 1;
2448 lg->globals_ver =
2449 ((PyDictObject *)f->f_globals)->ma_version_tag;
2450 lg->builtins_ver =
2451 ((PyDictObject *)f->f_builtins)->ma_version_tag;
2452 lg->ptr = v; /* borrowed */
2453 }
2454
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002455 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002456 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002457 else {
2458 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002459
2460 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09002461 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002462 v = PyObject_GetItem(f->f_globals, name);
2463 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002464 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002465 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002466 }
2467 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002468
Victor Stinnerb4efc962015-11-20 09:24:02 +01002469 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002470 v = PyObject_GetItem(f->f_builtins, name);
2471 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002472 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002473 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002474 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002475 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002476 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002477 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002478 }
2479 }
2480 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002481 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002482 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002483 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002484
Benjamin Petersonddd19492018-09-16 22:38:02 -07002485 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002486 PyObject *v = GETLOCAL(oparg);
2487 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002488 SETLOCAL(oparg, NULL);
2489 DISPATCH();
2490 }
2491 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002492 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002493 UNBOUNDLOCAL_ERROR_MSG,
2494 PyTuple_GetItem(co->co_varnames, oparg)
2495 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002496 goto error;
2497 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002498
Benjamin Petersonddd19492018-09-16 22:38:02 -07002499 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002500 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002501 PyObject *oldobj = PyCell_GET(cell);
2502 if (oldobj != NULL) {
2503 PyCell_SET(cell, NULL);
2504 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002505 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002506 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002507 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002508 goto error;
2509 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002510
Benjamin Petersonddd19492018-09-16 22:38:02 -07002511 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002512 PyObject *cell = freevars[oparg];
2513 Py_INCREF(cell);
2514 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002515 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002516 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002517
Benjamin Petersonddd19492018-09-16 22:38:02 -07002518 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002519 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002520 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002521 assert(locals);
2522 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2523 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2524 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2525 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2526 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002527 value = PyDict_GetItemWithError(locals, name);
2528 if (value != NULL) {
2529 Py_INCREF(value);
2530 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002531 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002532 goto error;
2533 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002534 }
2535 else {
2536 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002537 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002538 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002539 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002540 }
2541 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002542 }
2543 }
2544 if (!value) {
2545 PyObject *cell = freevars[oparg];
2546 value = PyCell_GET(cell);
2547 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002548 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002549 goto error;
2550 }
2551 Py_INCREF(value);
2552 }
2553 PUSH(value);
2554 DISPATCH();
2555 }
2556
Benjamin Petersonddd19492018-09-16 22:38:02 -07002557 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002558 PyObject *cell = freevars[oparg];
2559 PyObject *value = PyCell_GET(cell);
2560 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002561 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002562 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002563 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002564 Py_INCREF(value);
2565 PUSH(value);
2566 DISPATCH();
2567 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002568
Benjamin Petersonddd19492018-09-16 22:38:02 -07002569 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002570 PyObject *v = POP();
2571 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002572 PyObject *oldobj = PyCell_GET(cell);
2573 PyCell_SET(cell, v);
2574 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002575 DISPATCH();
2576 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002577
Benjamin Petersonddd19492018-09-16 22:38:02 -07002578 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002579 PyObject *str;
2580 PyObject *empty = PyUnicode_New(0, 0);
2581 if (empty == NULL) {
2582 goto error;
2583 }
2584 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2585 Py_DECREF(empty);
2586 if (str == NULL)
2587 goto error;
2588 while (--oparg >= 0) {
2589 PyObject *item = POP();
2590 Py_DECREF(item);
2591 }
2592 PUSH(str);
2593 DISPATCH();
2594 }
2595
Benjamin Petersonddd19492018-09-16 22:38:02 -07002596 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002597 PyObject *tup = PyTuple_New(oparg);
2598 if (tup == NULL)
2599 goto error;
2600 while (--oparg >= 0) {
2601 PyObject *item = POP();
2602 PyTuple_SET_ITEM(tup, oparg, item);
2603 }
2604 PUSH(tup);
2605 DISPATCH();
2606 }
2607
Benjamin Petersonddd19492018-09-16 22:38:02 -07002608 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002609 PyObject *list = PyList_New(oparg);
2610 if (list == NULL)
2611 goto error;
2612 while (--oparg >= 0) {
2613 PyObject *item = POP();
2614 PyList_SET_ITEM(list, oparg, item);
2615 }
2616 PUSH(list);
2617 DISPATCH();
2618 }
2619
Mark Shannon13bc1392020-01-23 09:25:17 +00002620 case TARGET(LIST_TO_TUPLE): {
2621 PyObject *list = POP();
2622 PyObject *tuple = PyList_AsTuple(list);
2623 Py_DECREF(list);
2624 if (tuple == NULL) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002625 goto error;
Mark Shannon13bc1392020-01-23 09:25:17 +00002626 }
2627 PUSH(tuple);
2628 DISPATCH();
2629 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002630
Mark Shannon13bc1392020-01-23 09:25:17 +00002631 case TARGET(LIST_EXTEND): {
2632 PyObject *iterable = POP();
2633 PyObject *list = PEEK(oparg);
2634 PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
2635 if (none_val == NULL) {
2636 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01002637 (Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable)))
Mark Shannon13bc1392020-01-23 09:25:17 +00002638 {
Victor Stinner61f4db82020-01-28 03:37:45 +01002639 _PyErr_Clear(tstate);
Mark Shannon13bc1392020-01-23 09:25:17 +00002640 _PyErr_Format(tstate, PyExc_TypeError,
2641 "Value after * must be an iterable, not %.200s",
2642 Py_TYPE(iterable)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002643 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002644 Py_DECREF(iterable);
2645 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002646 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002647 Py_DECREF(none_val);
2648 Py_DECREF(iterable);
2649 DISPATCH();
2650 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002651
Mark Shannon13bc1392020-01-23 09:25:17 +00002652 case TARGET(SET_UPDATE): {
2653 PyObject *iterable = POP();
2654 PyObject *set = PEEK(oparg);
2655 int err = _PySet_Update(set, iterable);
2656 Py_DECREF(iterable);
2657 if (err < 0) {
2658 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002659 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002660 DISPATCH();
2661 }
2662
Benjamin Petersonddd19492018-09-16 22:38:02 -07002663 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002664 PyObject *set = PySet_New(NULL);
2665 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002666 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002667 if (set == NULL)
2668 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002669 for (i = oparg; i > 0; i--) {
2670 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002671 if (err == 0)
2672 err = PySet_Add(set, item);
2673 Py_DECREF(item);
2674 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002675 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002676 if (err != 0) {
2677 Py_DECREF(set);
2678 goto error;
2679 }
2680 PUSH(set);
2681 DISPATCH();
2682 }
2683
Benjamin Petersonddd19492018-09-16 22:38:02 -07002684 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002685 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002686 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2687 if (map == NULL)
2688 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002689 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002690 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002691 PyObject *key = PEEK(2*i);
2692 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002693 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002694 if (err != 0) {
2695 Py_DECREF(map);
2696 goto error;
2697 }
2698 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002699
2700 while (oparg--) {
2701 Py_DECREF(POP());
2702 Py_DECREF(POP());
2703 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002704 PUSH(map);
2705 DISPATCH();
2706 }
2707
Benjamin Petersonddd19492018-09-16 22:38:02 -07002708 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002709 _Py_IDENTIFIER(__annotations__);
2710 int err;
2711 PyObject *ann_dict;
2712 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002713 _PyErr_Format(tstate, PyExc_SystemError,
2714 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002715 goto error;
2716 }
2717 /* check if __annotations__ in locals()... */
2718 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002719 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002720 &PyId___annotations__);
2721 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002722 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002723 goto error;
2724 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002725 /* ...if not, create a new one */
2726 ann_dict = PyDict_New();
2727 if (ann_dict == NULL) {
2728 goto error;
2729 }
2730 err = _PyDict_SetItemId(f->f_locals,
2731 &PyId___annotations__, ann_dict);
2732 Py_DECREF(ann_dict);
2733 if (err != 0) {
2734 goto error;
2735 }
2736 }
2737 }
2738 else {
2739 /* do the same if locals() is not a dict */
2740 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2741 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002742 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002743 }
2744 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2745 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002746 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002747 goto error;
2748 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002749 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002750 ann_dict = PyDict_New();
2751 if (ann_dict == NULL) {
2752 goto error;
2753 }
2754 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2755 Py_DECREF(ann_dict);
2756 if (err != 0) {
2757 goto error;
2758 }
2759 }
2760 else {
2761 Py_DECREF(ann_dict);
2762 }
2763 }
2764 DISPATCH();
2765 }
2766
Benjamin Petersonddd19492018-09-16 22:38:02 -07002767 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002768 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002769 PyObject *map;
2770 PyObject *keys = TOP();
2771 if (!PyTuple_CheckExact(keys) ||
2772 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002773 _PyErr_SetString(tstate, PyExc_SystemError,
2774 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002775 goto error;
2776 }
2777 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2778 if (map == NULL) {
2779 goto error;
2780 }
2781 for (i = oparg; i > 0; i--) {
2782 int err;
2783 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2784 PyObject *value = PEEK(i + 1);
2785 err = PyDict_SetItem(map, key, value);
2786 if (err != 0) {
2787 Py_DECREF(map);
2788 goto error;
2789 }
2790 }
2791
2792 Py_DECREF(POP());
2793 while (oparg--) {
2794 Py_DECREF(POP());
2795 }
2796 PUSH(map);
2797 DISPATCH();
2798 }
2799
Mark Shannon8a4cd702020-01-27 09:57:45 +00002800 case TARGET(DICT_UPDATE): {
2801 PyObject *update = POP();
2802 PyObject *dict = PEEK(oparg);
2803 if (PyDict_Update(dict, update) < 0) {
2804 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
2805 _PyErr_Format(tstate, PyExc_TypeError,
2806 "'%.200s' object is not a mapping",
Victor Stinnera102ed72020-02-07 02:24:48 +01002807 Py_TYPE(update)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002808 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002809 Py_DECREF(update);
2810 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002811 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002812 Py_DECREF(update);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002813 DISPATCH();
2814 }
2815
Mark Shannon8a4cd702020-01-27 09:57:45 +00002816 case TARGET(DICT_MERGE): {
2817 PyObject *update = POP();
2818 PyObject *dict = PEEK(oparg);
2819
2820 if (_PyDict_MergeEx(dict, update, 2) < 0) {
2821 format_kwargs_error(tstate, PEEK(2 + oparg), update);
2822 Py_DECREF(update);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002823 goto error;
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002824 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002825 Py_DECREF(update);
Brandt Bucherf185a732019-09-28 17:12:49 -07002826 PREDICT(CALL_FUNCTION_EX);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002827 DISPATCH();
2828 }
2829
Benjamin Petersonddd19492018-09-16 22:38:02 -07002830 case TARGET(MAP_ADD): {
Jörn Heisslerc8a35412019-06-22 16:40:55 +02002831 PyObject *value = TOP();
2832 PyObject *key = SECOND();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002833 PyObject *map;
2834 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002835 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002836 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002837 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002838 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002839 Py_DECREF(value);
2840 Py_DECREF(key);
2841 if (err != 0)
2842 goto error;
2843 PREDICT(JUMP_ABSOLUTE);
2844 DISPATCH();
2845 }
2846
Benjamin Petersonddd19492018-09-16 22:38:02 -07002847 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002848 PyObject *name = GETITEM(names, oparg);
2849 PyObject *owner = TOP();
2850 PyObject *res = PyObject_GetAttr(owner, name);
2851 Py_DECREF(owner);
2852 SET_TOP(res);
2853 if (res == NULL)
2854 goto error;
2855 DISPATCH();
2856 }
2857
Benjamin Petersonddd19492018-09-16 22:38:02 -07002858 case TARGET(COMPARE_OP): {
Mark Shannon9af0e472020-01-14 10:12:45 +00002859 assert(oparg <= Py_GE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002860 PyObject *right = POP();
2861 PyObject *left = TOP();
Mark Shannon9af0e472020-01-14 10:12:45 +00002862 PyObject *res = PyObject_RichCompare(left, right, oparg);
2863 SET_TOP(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002864 Py_DECREF(left);
2865 Py_DECREF(right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002866 if (res == NULL)
2867 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002868 PREDICT(POP_JUMP_IF_FALSE);
2869 PREDICT(POP_JUMP_IF_TRUE);
2870 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002871 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002872
Mark Shannon9af0e472020-01-14 10:12:45 +00002873 case TARGET(IS_OP): {
2874 PyObject *right = POP();
2875 PyObject *left = TOP();
2876 int res = (left == right)^oparg;
2877 PyObject *b = res ? Py_True : Py_False;
2878 Py_INCREF(b);
2879 SET_TOP(b);
2880 Py_DECREF(left);
2881 Py_DECREF(right);
2882 PREDICT(POP_JUMP_IF_FALSE);
2883 PREDICT(POP_JUMP_IF_TRUE);
2884 FAST_DISPATCH();
2885 }
2886
2887 case TARGET(CONTAINS_OP): {
2888 PyObject *right = POP();
2889 PyObject *left = POP();
2890 int res = PySequence_Contains(right, left);
2891 Py_DECREF(left);
2892 Py_DECREF(right);
2893 if (res < 0) {
2894 goto error;
2895 }
2896 PyObject *b = (res^oparg) ? Py_True : Py_False;
2897 Py_INCREF(b);
2898 PUSH(b);
2899 PREDICT(POP_JUMP_IF_FALSE);
2900 PREDICT(POP_JUMP_IF_TRUE);
2901 FAST_DISPATCH();
2902 }
2903
2904#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
2905 "BaseException is not allowed"
2906
2907 case TARGET(JUMP_IF_NOT_EXC_MATCH): {
2908 PyObject *right = POP();
2909 PyObject *left = POP();
2910 if (PyTuple_Check(right)) {
2911 Py_ssize_t i, length;
2912 length = PyTuple_GET_SIZE(right);
2913 for (i = 0; i < length; i++) {
2914 PyObject *exc = PyTuple_GET_ITEM(right, i);
2915 if (!PyExceptionClass_Check(exc)) {
2916 _PyErr_SetString(tstate, PyExc_TypeError,
2917 CANNOT_CATCH_MSG);
2918 Py_DECREF(left);
2919 Py_DECREF(right);
2920 goto error;
2921 }
2922 }
2923 }
2924 else {
2925 if (!PyExceptionClass_Check(right)) {
2926 _PyErr_SetString(tstate, PyExc_TypeError,
2927 CANNOT_CATCH_MSG);
2928 Py_DECREF(left);
2929 Py_DECREF(right);
2930 goto error;
2931 }
2932 }
2933 int res = PyErr_GivenExceptionMatches(left, right);
2934 Py_DECREF(left);
2935 Py_DECREF(right);
2936 if (res > 0) {
2937 /* Exception matches -- Do nothing */;
2938 }
2939 else if (res == 0) {
2940 JUMPTO(oparg);
2941 }
2942 else {
2943 goto error;
2944 }
2945 DISPATCH();
2946 }
2947
Benjamin Petersonddd19492018-09-16 22:38:02 -07002948 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002949 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002950 PyObject *fromlist = POP();
2951 PyObject *level = TOP();
2952 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02002953 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002954 Py_DECREF(level);
2955 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002956 SET_TOP(res);
2957 if (res == NULL)
2958 goto error;
2959 DISPATCH();
2960 }
2961
Benjamin Petersonddd19492018-09-16 22:38:02 -07002962 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002963 PyObject *from = POP(), *locals;
2964 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002965 if (PyFrame_FastToLocalsWithError(f) < 0) {
2966 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002967 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002968 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002969
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002970 locals = f->f_locals;
2971 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002972 _PyErr_SetString(tstate, PyExc_SystemError,
2973 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002974 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002975 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002976 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002977 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002978 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002979 Py_DECREF(from);
2980 if (err != 0)
2981 goto error;
2982 DISPATCH();
2983 }
Guido van Rossum25831651993-05-19 14:50:45 +00002984
Benjamin Petersonddd19492018-09-16 22:38:02 -07002985 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002986 PyObject *name = GETITEM(names, oparg);
2987 PyObject *from = TOP();
2988 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02002989 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002990 PUSH(res);
2991 if (res == NULL)
2992 goto error;
2993 DISPATCH();
2994 }
Thomas Wouters52152252000-08-17 22:55:00 +00002995
Benjamin Petersonddd19492018-09-16 22:38:02 -07002996 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002997 JUMPBY(oparg);
2998 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002999 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003000
Benjamin Petersonddd19492018-09-16 22:38:02 -07003001 case TARGET(POP_JUMP_IF_FALSE): {
3002 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003003 PyObject *cond = POP();
3004 int err;
3005 if (cond == Py_True) {
3006 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003007 FAST_DISPATCH();
3008 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003009 if (cond == Py_False) {
3010 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003011 JUMPTO(oparg);
3012 FAST_DISPATCH();
3013 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003014 err = PyObject_IsTrue(cond);
3015 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003016 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003017 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003018 else if (err == 0)
3019 JUMPTO(oparg);
3020 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003021 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003022 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003023 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003024
Benjamin Petersonddd19492018-09-16 22:38:02 -07003025 case TARGET(POP_JUMP_IF_TRUE): {
3026 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003027 PyObject *cond = POP();
3028 int err;
3029 if (cond == Py_False) {
3030 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003031 FAST_DISPATCH();
3032 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003033 if (cond == Py_True) {
3034 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003035 JUMPTO(oparg);
3036 FAST_DISPATCH();
3037 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003038 err = PyObject_IsTrue(cond);
3039 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003040 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003041 JUMPTO(oparg);
3042 }
3043 else if (err == 0)
3044 ;
3045 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003046 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003047 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003048 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003049
Benjamin Petersonddd19492018-09-16 22:38:02 -07003050 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003051 PyObject *cond = TOP();
3052 int err;
3053 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003054 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003055 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003056 FAST_DISPATCH();
3057 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003058 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003059 JUMPTO(oparg);
3060 FAST_DISPATCH();
3061 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003062 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003063 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003064 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003065 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003066 }
3067 else if (err == 0)
3068 JUMPTO(oparg);
3069 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003070 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003071 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003072 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003073
Benjamin Petersonddd19492018-09-16 22:38:02 -07003074 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003075 PyObject *cond = TOP();
3076 int err;
3077 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003078 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003079 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003080 FAST_DISPATCH();
3081 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003082 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003083 JUMPTO(oparg);
3084 FAST_DISPATCH();
3085 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003086 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003087 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003088 JUMPTO(oparg);
3089 }
3090 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003091 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003092 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003093 }
3094 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003095 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003096 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003097 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003098
Benjamin Petersonddd19492018-09-16 22:38:02 -07003099 case TARGET(JUMP_ABSOLUTE): {
3100 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003101 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003102#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003103 /* Enabling this path speeds-up all while and for-loops by bypassing
3104 the per-loop checks for signals. By default, this should be turned-off
3105 because it prevents detection of a control-break in tight loops like
3106 "while 1: pass". Compile with this option turned-on when you need
3107 the speed-up and do not need break checking inside tight loops (ones
3108 that contain only instructions ending with FAST_DISPATCH).
3109 */
3110 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003111#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003112 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003113#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003114 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003115
Benjamin Petersonddd19492018-09-16 22:38:02 -07003116 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003117 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003118 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003119 PyObject *iter = PyObject_GetIter(iterable);
3120 Py_DECREF(iterable);
3121 SET_TOP(iter);
3122 if (iter == NULL)
3123 goto error;
3124 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003125 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003126 DISPATCH();
3127 }
3128
Benjamin Petersonddd19492018-09-16 22:38:02 -07003129 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04003130 /* before: [obj]; after [getiter(obj)] */
3131 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003132 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003133 if (PyCoro_CheckExact(iterable)) {
3134 /* `iterable` is a coroutine */
3135 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3136 /* and it is used in a 'yield from' expression of a
3137 regular generator. */
3138 Py_DECREF(iterable);
3139 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003140 _PyErr_SetString(tstate, PyExc_TypeError,
3141 "cannot 'yield from' a coroutine object "
3142 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04003143 goto error;
3144 }
3145 }
3146 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003147 /* `iterable` is not a generator. */
3148 iter = PyObject_GetIter(iterable);
3149 Py_DECREF(iterable);
3150 SET_TOP(iter);
3151 if (iter == NULL)
3152 goto error;
3153 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003154 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003155 DISPATCH();
3156 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003157
Benjamin Petersonddd19492018-09-16 22:38:02 -07003158 case TARGET(FOR_ITER): {
3159 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003160 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003161 PyObject *iter = TOP();
Victor Stinnera102ed72020-02-07 02:24:48 +01003162 PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003163 if (next != NULL) {
3164 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003165 PREDICT(STORE_FAST);
3166 PREDICT(UNPACK_SEQUENCE);
3167 DISPATCH();
3168 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003169 if (_PyErr_Occurred(tstate)) {
3170 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003171 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003172 }
3173 else if (tstate->c_tracefunc != NULL) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003174 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Victor Stinner438a12d2019-05-24 17:01:38 +02003175 }
3176 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003177 }
3178 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003179 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003180 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003181 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003182 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003183 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003184 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003185
Benjamin Petersonddd19492018-09-16 22:38:02 -07003186 case TARGET(SETUP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003187 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003188 STACK_LEVEL());
3189 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003190 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003191
Benjamin Petersonddd19492018-09-16 22:38:02 -07003192 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003193 _Py_IDENTIFIER(__aenter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003194 _Py_IDENTIFIER(__aexit__);
Yury Selivanov75445082015-05-11 22:57:16 -04003195 PyObject *mgr = TOP();
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003196 PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04003197 PyObject *res;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003198 if (enter == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003199 goto error;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003200 }
3201 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__);
3202 if (exit == NULL) {
3203 Py_DECREF(enter);
3204 goto error;
3205 }
Yury Selivanov75445082015-05-11 22:57:16 -04003206 SET_TOP(exit);
Yury Selivanov75445082015-05-11 22:57:16 -04003207 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003208 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003209 Py_DECREF(enter);
3210 if (res == NULL)
3211 goto error;
3212 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003213 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003214 DISPATCH();
3215 }
3216
Benjamin Petersonddd19492018-09-16 22:38:02 -07003217 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003218 PyObject *res = POP();
3219 /* Setup the finally block before pushing the result
3220 of __aenter__ on the stack. */
3221 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3222 STACK_LEVEL());
3223 PUSH(res);
3224 DISPATCH();
3225 }
3226
Benjamin Petersonddd19492018-09-16 22:38:02 -07003227 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003228 _Py_IDENTIFIER(__enter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003229 _Py_IDENTIFIER(__exit__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003230 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003231 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003232 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003233 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08003234 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003235 }
3236 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003237 if (exit == NULL) {
3238 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003239 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003240 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003241 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003242 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003243 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003244 Py_DECREF(enter);
3245 if (res == NULL)
3246 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003247 /* Setup the finally block before pushing the result
3248 of __enter__ on the stack. */
3249 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3250 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003251
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003252 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003253 DISPATCH();
3254 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003255
Mark Shannonfee55262019-11-21 09:11:43 +00003256 case TARGET(WITH_EXCEPT_START): {
3257 /* At the top of the stack are 7 values:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003258 - (TOP, SECOND, THIRD) = exc_info()
Mark Shannonfee55262019-11-21 09:11:43 +00003259 - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER
3260 - SEVENTH: the context.__exit__ bound method
3261 We call SEVENTH(TOP, SECOND, THIRD).
3262 Then we push again the TOP exception and the __exit__
3263 return value.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003264 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003265 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003266 PyObject *exc, *val, *tb, *res;
3267
Victor Stinner842cfff2016-12-01 14:45:31 +01003268 exc = TOP();
Mark Shannonfee55262019-11-21 09:11:43 +00003269 val = SECOND();
3270 tb = THIRD();
3271 assert(exc != Py_None);
3272 assert(!PyLong_Check(exc));
3273 exit_func = PEEK(7);
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003274 PyObject *stack[4] = {NULL, exc, val, tb};
Petr Viktorinffd97532020-02-11 17:46:57 +01003275 res = PyObject_Vectorcall(exit_func, stack + 1,
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003276 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003277 if (res == NULL)
3278 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003279
Yury Selivanov75445082015-05-11 22:57:16 -04003280 PUSH(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003281 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003282 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003283
Benjamin Petersonddd19492018-09-16 22:38:02 -07003284 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10003285 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003286 PyObject *name = GETITEM(names, oparg);
3287 PyObject *obj = TOP();
3288 PyObject *meth = NULL;
3289
3290 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3291
Yury Selivanovf2392132016-12-13 19:03:51 -05003292 if (meth == NULL) {
3293 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003294 goto error;
3295 }
3296
3297 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003298 /* We can bypass temporary bound method object.
3299 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003300
INADA Naoki015bce62017-01-16 17:23:30 +09003301 meth | self | arg1 | ... | argN
3302 */
3303 SET_TOP(meth);
3304 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003305 }
3306 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003307 /* meth is not an unbound method (but a regular attr, or
3308 something was returned by a descriptor protocol). Set
3309 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003310 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003311
3312 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003313 */
INADA Naoki015bce62017-01-16 17:23:30 +09003314 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003315 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003316 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003317 }
3318 DISPATCH();
3319 }
3320
Benjamin Petersonddd19492018-09-16 22:38:02 -07003321 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003322 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003323 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003324
3325 sp = stack_pointer;
3326
INADA Naoki015bce62017-01-16 17:23:30 +09003327 meth = PEEK(oparg + 2);
3328 if (meth == NULL) {
3329 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3330 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003331
3332 Stack layout:
3333
INADA Naoki015bce62017-01-16 17:23:30 +09003334 ... | NULL | callable | arg1 | ... | argN
3335 ^- TOP()
3336 ^- (-oparg)
3337 ^- (-oparg-1)
3338 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003339
Ville Skyttä49b27342017-08-03 09:00:59 +03003340 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003341 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003342 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003343 res = call_function(tstate, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003344 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003345 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003346 }
3347 else {
3348 /* This is a method call. Stack layout:
3349
INADA Naoki015bce62017-01-16 17:23:30 +09003350 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003351 ^- TOP()
3352 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003353 ^- (-oparg-1)
3354 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003355
INADA Naoki015bce62017-01-16 17:23:30 +09003356 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003357 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003358 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003359 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003360 res = call_function(tstate, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003361 stack_pointer = sp;
3362 }
3363
3364 PUSH(res);
3365 if (res == NULL)
3366 goto error;
3367 DISPATCH();
3368 }
3369
Benjamin Petersonddd19492018-09-16 22:38:02 -07003370 case TARGET(CALL_FUNCTION): {
3371 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003372 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003373 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003374 res = call_function(tstate, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003375 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003376 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003377 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003378 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003379 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003380 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003381 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003382
Benjamin Petersonddd19492018-09-16 22:38:02 -07003383 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003384 PyObject **sp, *res, *names;
3385
3386 names = POP();
Jeroen Demeyer05677862019-08-16 12:41:27 +02003387 assert(PyTuple_Check(names));
3388 assert(PyTuple_GET_SIZE(names) <= oparg);
3389 /* We assume without checking that names contains only strings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003390 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003391 res = call_function(tstate, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003392 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003393 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003394 Py_DECREF(names);
3395
3396 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003397 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003398 }
3399 DISPATCH();
3400 }
3401
Benjamin Petersonddd19492018-09-16 22:38:02 -07003402 case TARGET(CALL_FUNCTION_EX): {
Brandt Bucherf185a732019-09-28 17:12:49 -07003403 PREDICTED(CALL_FUNCTION_EX);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003404 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003405 if (oparg & 0x01) {
3406 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003407 if (!PyDict_CheckExact(kwargs)) {
3408 PyObject *d = PyDict_New();
3409 if (d == NULL)
3410 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003411 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003412 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02003413 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003414 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003415 goto error;
3416 }
3417 Py_DECREF(kwargs);
3418 kwargs = d;
3419 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003420 assert(PyDict_CheckExact(kwargs));
3421 }
3422 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003423 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003424 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003425 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003426 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003427 goto error;
3428 }
3429 Py_SETREF(callargs, PySequence_Tuple(callargs));
3430 if (callargs == NULL) {
3431 goto error;
3432 }
3433 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003434 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003435
Victor Stinner09532fe2019-05-10 23:39:09 +02003436 result = do_call_core(tstate, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003437 Py_DECREF(func);
3438 Py_DECREF(callargs);
3439 Py_XDECREF(kwargs);
3440
3441 SET_TOP(result);
3442 if (result == NULL) {
3443 goto error;
3444 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003445 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003446 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003447
Benjamin Petersonddd19492018-09-16 22:38:02 -07003448 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003449 PyObject *qualname = POP();
3450 PyObject *codeobj = POP();
3451 PyFunctionObject *func = (PyFunctionObject *)
3452 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003453
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003454 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003455 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003456 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003457 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003458 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003459
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003460 if (oparg & 0x08) {
3461 assert(PyTuple_CheckExact(TOP()));
3462 func ->func_closure = POP();
3463 }
3464 if (oparg & 0x04) {
3465 assert(PyDict_CheckExact(TOP()));
3466 func->func_annotations = POP();
3467 }
3468 if (oparg & 0x02) {
3469 assert(PyDict_CheckExact(TOP()));
3470 func->func_kwdefaults = POP();
3471 }
3472 if (oparg & 0x01) {
3473 assert(PyTuple_CheckExact(TOP()));
3474 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003475 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003476
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003477 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003478 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003479 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003480
Benjamin Petersonddd19492018-09-16 22:38:02 -07003481 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003482 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003483 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003484 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003485 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003486 step = NULL;
3487 stop = POP();
3488 start = TOP();
3489 slice = PySlice_New(start, stop, step);
3490 Py_DECREF(start);
3491 Py_DECREF(stop);
3492 Py_XDECREF(step);
3493 SET_TOP(slice);
3494 if (slice == NULL)
3495 goto error;
3496 DISPATCH();
3497 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003498
Benjamin Petersonddd19492018-09-16 22:38:02 -07003499 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003500 /* Handles f-string value formatting. */
3501 PyObject *result;
3502 PyObject *fmt_spec;
3503 PyObject *value;
3504 PyObject *(*conv_fn)(PyObject *);
3505 int which_conversion = oparg & FVC_MASK;
3506 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3507
3508 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003509 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003510
3511 /* See if any conversion is specified. */
3512 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003513 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003514 case FVC_STR: conv_fn = PyObject_Str; break;
3515 case FVC_REPR: conv_fn = PyObject_Repr; break;
3516 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003517 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02003518 _PyErr_Format(tstate, PyExc_SystemError,
3519 "unexpected conversion flag %d",
3520 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003521 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003522 }
3523
3524 /* If there's a conversion function, call it and replace
3525 value with that result. Otherwise, just use value,
3526 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003527 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003528 result = conv_fn(value);
3529 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003530 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003531 Py_XDECREF(fmt_spec);
3532 goto error;
3533 }
3534 value = result;
3535 }
3536
3537 /* If value is a unicode object, and there's no fmt_spec,
3538 then we know the result of format(value) is value
3539 itself. In that case, skip calling format(). I plan to
3540 move this optimization in to PyObject_Format()
3541 itself. */
3542 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3543 /* Do nothing, just transfer ownership to result. */
3544 result = value;
3545 } else {
3546 /* Actually call format(). */
3547 result = PyObject_Format(value, fmt_spec);
3548 Py_DECREF(value);
3549 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003550 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003551 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003552 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003553 }
3554
Eric V. Smith135d5f42016-02-05 18:23:08 -05003555 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003556 DISPATCH();
3557 }
3558
Benjamin Petersonddd19492018-09-16 22:38:02 -07003559 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003560 int oldoparg = oparg;
3561 NEXTOPARG();
3562 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003563 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003564 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003565
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003566
Antoine Pitrou042b1282010-08-13 21:15:58 +00003567#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003568 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003569#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003570 default:
3571 fprintf(stderr,
3572 "XXX lineno: %d, opcode: %d\n",
3573 PyFrame_GetLineNumber(f),
3574 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02003575 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003576 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003578 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003579
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003580 /* This should never be reached. Every opcode should end with DISPATCH()
3581 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003582 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003583
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003584error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003585 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003586#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02003587 if (!_PyErr_Occurred(tstate)) {
3588 _PyErr_SetString(tstate, PyExc_SystemError,
3589 "error return without exception set");
3590 }
Victor Stinner365b6932013-07-12 00:11:58 +02003591#else
Victor Stinner438a12d2019-05-24 17:01:38 +02003592 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02003593#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003594
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003595 /* Log traceback info. */
3596 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003597
Benjamin Peterson51f46162013-01-23 08:38:47 -05003598 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003599 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3600 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003601
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003602exception_unwind:
3603 /* Unwind stacks if an exception occurred */
3604 while (f->f_iblock > 0) {
3605 /* Pop the current block. */
3606 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003607
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003608 if (b->b_type == EXCEPT_HANDLER) {
3609 UNWIND_EXCEPT_HANDLER(b);
3610 continue;
3611 }
3612 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003613 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003614 PyObject *exc, *val, *tb;
3615 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003616 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003617 /* Beware, this invalidates all b->b_* fields */
3618 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003619 PUSH(exc_info->exc_traceback);
3620 PUSH(exc_info->exc_value);
3621 if (exc_info->exc_type != NULL) {
3622 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003623 }
3624 else {
3625 Py_INCREF(Py_None);
3626 PUSH(Py_None);
3627 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003628 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003629 /* Make the raw exception data
3630 available to the handler,
3631 so a program can emulate the
3632 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02003633 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003634 if (tb != NULL)
3635 PyException_SetTraceback(val, tb);
3636 else
3637 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003638 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003639 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003640 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003641 exc_info->exc_value = val;
3642 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003643 if (tb == NULL)
3644 tb = Py_None;
3645 Py_INCREF(tb);
3646 PUSH(tb);
3647 PUSH(val);
3648 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003649 JUMPTO(handler);
Pablo Galindo4c53e632020-01-10 09:24:22 +00003650 if (_Py_TracingPossible(ceval)) {
3651 int needs_new_execution_window = (f->f_lasti < instr_lb || f->f_lasti >= instr_ub);
3652 int needs_line_update = (f->f_lasti == instr_lb || f->f_lasti < instr_prev);
3653 /* Make sure that we trace line after exception if we are in a new execution
3654 * window or we don't need a line update and we are not in the first instruction
3655 * of the line. */
3656 if (needs_new_execution_window || (!needs_line_update && instr_lb > 0)) {
3657 instr_prev = INT_MAX;
3658 }
Mark Shannonfee55262019-11-21 09:11:43 +00003659 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003660 /* Resume normal execution */
3661 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003662 }
3663 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003664
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003665 /* End the loop as we still have an error */
3666 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003667 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003668
Pablo Galindof00828a2019-05-09 16:52:02 +01003669 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003670 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01003671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003672 /* Pop remaining stack entries. */
3673 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003674 PyObject *o = POP();
3675 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003676 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003677
Mark Shannone7c9f4a2020-01-13 12:51:26 +00003678exiting:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003679 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003680 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003681 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3682 tstate, f, PyTrace_RETURN, retval)) {
3683 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003684 }
3685 }
3686 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003687 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3688 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003689 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003690 }
3691 }
3692 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003693
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003694 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003695exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003696 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3697 dtrace_function_return(f);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01003698 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrou58720d62013-08-05 23:26:40 +02003699 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003700 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003701
Victor Stinner0b72b232020-03-12 23:18:39 +01003702 return _Py_CheckFunctionResult(tstate, NULL, retval, __func__);
Guido van Rossum374a9221991-04-04 10:40:29 +00003703}
3704
Benjamin Petersonb204a422011-06-05 22:04:07 -05003705static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003706format_missing(PyThreadState *tstate, const char *kind,
3707 PyCodeObject *co, PyObject *names)
Benjamin Petersone109c702011-06-24 09:37:26 -05003708{
3709 int err;
3710 Py_ssize_t len = PyList_GET_SIZE(names);
3711 PyObject *name_str, *comma, *tail, *tmp;
3712
3713 assert(PyList_CheckExact(names));
3714 assert(len >= 1);
3715 /* Deal with the joys of natural language. */
3716 switch (len) {
3717 case 1:
3718 name_str = PyList_GET_ITEM(names, 0);
3719 Py_INCREF(name_str);
3720 break;
3721 case 2:
3722 name_str = PyUnicode_FromFormat("%U and %U",
3723 PyList_GET_ITEM(names, len - 2),
3724 PyList_GET_ITEM(names, len - 1));
3725 break;
3726 default:
3727 tail = PyUnicode_FromFormat(", %U, and %U",
3728 PyList_GET_ITEM(names, len - 2),
3729 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003730 if (tail == NULL)
3731 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003732 /* Chop off the last two objects in the list. This shouldn't actually
3733 fail, but we can't be too careful. */
3734 err = PyList_SetSlice(names, len - 2, len, NULL);
3735 if (err == -1) {
3736 Py_DECREF(tail);
3737 return;
3738 }
3739 /* Stitch everything up into a nice comma-separated list. */
3740 comma = PyUnicode_FromString(", ");
3741 if (comma == NULL) {
3742 Py_DECREF(tail);
3743 return;
3744 }
3745 tmp = PyUnicode_Join(comma, names);
3746 Py_DECREF(comma);
3747 if (tmp == NULL) {
3748 Py_DECREF(tail);
3749 return;
3750 }
3751 name_str = PyUnicode_Concat(tmp, tail);
3752 Py_DECREF(tmp);
3753 Py_DECREF(tail);
3754 break;
3755 }
3756 if (name_str == NULL)
3757 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02003758 _PyErr_Format(tstate, PyExc_TypeError,
3759 "%U() missing %i required %s argument%s: %U",
3760 co->co_name,
3761 len,
3762 kind,
3763 len == 1 ? "" : "s",
3764 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05003765 Py_DECREF(name_str);
3766}
3767
3768static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003769missing_arguments(PyThreadState *tstate, PyCodeObject *co,
3770 Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003771 PyObject **fastlocals)
3772{
Victor Stinner74319ae2016-08-25 00:04:09 +02003773 Py_ssize_t i, j = 0;
3774 Py_ssize_t start, end;
3775 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003776 const char *kind = positional ? "positional" : "keyword-only";
3777 PyObject *missing_names;
3778
3779 /* Compute the names of the arguments that are missing. */
3780 missing_names = PyList_New(missing);
3781 if (missing_names == NULL)
3782 return;
3783 if (positional) {
3784 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01003785 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003786 }
3787 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003788 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003789 end = start + co->co_kwonlyargcount;
3790 }
3791 for (i = start; i < end; i++) {
3792 if (GETLOCAL(i) == NULL) {
3793 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3794 PyObject *name = PyObject_Repr(raw);
3795 if (name == NULL) {
3796 Py_DECREF(missing_names);
3797 return;
3798 }
3799 PyList_SET_ITEM(missing_names, j++, name);
3800 }
3801 }
3802 assert(j == missing);
Victor Stinner438a12d2019-05-24 17:01:38 +02003803 format_missing(tstate, kind, co, missing_names);
Benjamin Petersone109c702011-06-24 09:37:26 -05003804 Py_DECREF(missing_names);
3805}
3806
3807static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003808too_many_positional(PyThreadState *tstate, PyCodeObject *co,
3809 Py_ssize_t given, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003810 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003811{
3812 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003813 Py_ssize_t kwonly_given = 0;
3814 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003815 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003816 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003817
Benjamin Petersone109c702011-06-24 09:37:26 -05003818 assert((co->co_flags & CO_VARARGS) == 0);
3819 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01003820 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003821 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003822 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003823 }
3824 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003825 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01003826 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003827 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01003828 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003829 }
3830 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003831 plural = (co_argcount != 1);
3832 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003833 }
3834 if (sig == NULL)
3835 return;
3836 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003837 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3838 kwonly_sig = PyUnicode_FromFormat(format,
3839 given != 1 ? "s" : "",
3840 kwonly_given,
3841 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003842 if (kwonly_sig == NULL) {
3843 Py_DECREF(sig);
3844 return;
3845 }
3846 }
3847 else {
3848 /* This will not fail. */
3849 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003850 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003851 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003852 _PyErr_Format(tstate, PyExc_TypeError,
3853 "%U() takes %U positional argument%s but %zd%U %s given",
3854 co->co_name,
3855 sig,
3856 plural ? "s" : "",
3857 given,
3858 kwonly_sig,
3859 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003860 Py_DECREF(sig);
3861 Py_DECREF(kwonly_sig);
3862}
3863
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003864static int
Victor Stinner438a12d2019-05-24 17:01:38 +02003865positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
3866 Py_ssize_t kwcount, PyObject* const* kwnames)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003867{
3868 int posonly_conflicts = 0;
3869 PyObject* posonly_names = PyList_New(0);
3870
3871 for(int k=0; k < co->co_posonlyargcount; k++){
3872 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
3873
3874 for (int k2=0; k2<kwcount; k2++){
3875 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
3876 PyObject* kwname = kwnames[k2];
3877 if (kwname == posonly_name){
3878 if(PyList_Append(posonly_names, kwname) != 0) {
3879 goto fail;
3880 }
3881 posonly_conflicts++;
3882 continue;
3883 }
3884
3885 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
3886
3887 if ( cmp > 0) {
3888 if(PyList_Append(posonly_names, kwname) != 0) {
3889 goto fail;
3890 }
3891 posonly_conflicts++;
3892 } else if (cmp < 0) {
3893 goto fail;
3894 }
3895
3896 }
3897 }
3898 if (posonly_conflicts) {
3899 PyObject* comma = PyUnicode_FromString(", ");
3900 if (comma == NULL) {
3901 goto fail;
3902 }
3903 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
3904 Py_DECREF(comma);
3905 if (error_names == NULL) {
3906 goto fail;
3907 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003908 _PyErr_Format(tstate, PyExc_TypeError,
3909 "%U() got some positional-only arguments passed"
3910 " as keyword arguments: '%U'",
3911 co->co_name, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003912 Py_DECREF(error_names);
3913 goto fail;
3914 }
3915
3916 Py_DECREF(posonly_names);
3917 return 0;
3918
3919fail:
3920 Py_XDECREF(posonly_names);
3921 return 1;
3922
3923}
3924
Guido van Rossumc2e20742006-02-27 22:32:47 +00003925/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02003926 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003927 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003928
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003929PyObject *
Victor Stinnerb5e170f2019-11-16 01:03:22 +01003930_PyEval_EvalCode(PyThreadState *tstate,
3931 PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003932 PyObject *const *args, Py_ssize_t argcount,
3933 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003934 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003935 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003936 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003937 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003938{
Victor Stinnerb5e170f2019-11-16 01:03:22 +01003939 assert(tstate != NULL);
3940
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003941 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003942 PyFrameObject *f;
3943 PyObject *retval = NULL;
3944 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003945 PyObject *x, *u;
Pablo Galindocd74e662019-06-01 18:08:04 +01003946 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003947 Py_ssize_t i, j, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003948 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003949
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003950 if (globals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003951 _PyErr_SetString(tstate, PyExc_SystemError,
3952 "PyEval_EvalCodeEx: NULL globals");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003953 return NULL;
3954 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003955
Victor Stinnerc7020012016-08-16 23:40:29 +02003956 /* Create the frame */
INADA Naoki5a625d02016-12-24 20:19:08 +09003957 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003958 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003959 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003960 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003961 fastlocals = f->f_localsplus;
3962 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003963
Victor Stinnerc7020012016-08-16 23:40:29 +02003964 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003965 if (co->co_flags & CO_VARKEYWORDS) {
3966 kwdict = PyDict_New();
3967 if (kwdict == NULL)
3968 goto fail;
3969 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003970 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003971 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003972 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003973 SETLOCAL(i, kwdict);
3974 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003975 else {
3976 kwdict = NULL;
3977 }
3978
Pablo Galindocd74e662019-06-01 18:08:04 +01003979 /* Copy all positional arguments into local variables */
3980 if (argcount > co->co_argcount) {
3981 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003982 }
3983 else {
3984 n = argcount;
3985 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003986 for (j = 0; j < n; j++) {
3987 x = args[j];
3988 Py_INCREF(x);
3989 SETLOCAL(j, x);
3990 }
3991
Victor Stinnerc7020012016-08-16 23:40:29 +02003992 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003993 if (co->co_flags & CO_VARARGS) {
Sergey Fedoseev234531b2019-02-25 21:59:12 +05003994 u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003995 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003996 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003997 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003998 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003999 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004000
Serhiy Storchakab7281052016-09-12 00:52:40 +03004001 /* Handle keyword arguments passed as two strided arrays */
4002 kwcount *= kwstep;
4003 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004004 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03004005 PyObject *keyword = kwnames[i];
4006 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02004007 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004008
Benjamin Petersonb204a422011-06-05 22:04:07 -05004009 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004010 _PyErr_Format(tstate, PyExc_TypeError,
4011 "%U() keywords must be strings",
4012 co->co_name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004013 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004014 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004015
Benjamin Petersonb204a422011-06-05 22:04:07 -05004016 /* Speed hack: do raw pointer compares. As names are
4017 normally interned this should almost always hit. */
4018 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004019 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004020 PyObject *name = co_varnames[j];
4021 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004022 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004023 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004024 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004025
Benjamin Petersonb204a422011-06-05 22:04:07 -05004026 /* Slow fallback, just in case */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004027 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004028 PyObject *name = co_varnames[j];
4029 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
4030 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004031 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004032 }
4033 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004034 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004035 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004036 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004037
Victor Stinner231d1f32017-01-11 02:12:06 +01004038 assert(j >= total_args);
4039 if (kwdict == NULL) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004040
Victor Stinner438a12d2019-05-24 17:01:38 +02004041 if (co->co_posonlyargcount
4042 && positional_only_passed_as_keyword(tstate, co,
4043 kwcount, kwnames))
4044 {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004045 goto fail;
4046 }
4047
Victor Stinner438a12d2019-05-24 17:01:38 +02004048 _PyErr_Format(tstate, PyExc_TypeError,
4049 "%U() got an unexpected keyword argument '%S'",
4050 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004051 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004052 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004053
Christian Heimes0bd447f2013-07-20 14:48:10 +02004054 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4055 goto fail;
4056 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004057 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004058
Benjamin Petersonb204a422011-06-05 22:04:07 -05004059 kw_found:
4060 if (GETLOCAL(j) != NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004061 _PyErr_Format(tstate, PyExc_TypeError,
4062 "%U() got multiple values for argument '%S'",
4063 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004064 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004065 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004066 Py_INCREF(value);
4067 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004068 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004069
4070 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004071 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004072 too_many_positional(tstate, co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004073 goto fail;
4074 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004075
4076 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004077 if (argcount < co->co_argcount) {
4078 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004079 Py_ssize_t missing = 0;
4080 for (i = argcount; i < m; i++) {
4081 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004082 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004083 }
4084 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004085 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004086 missing_arguments(tstate, co, missing, defcount, fastlocals);
Benjamin Petersone109c702011-06-24 09:37:26 -05004087 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004088 }
4089 if (n > m)
4090 i = n - m;
4091 else
4092 i = 0;
4093 for (; i < defcount; i++) {
4094 if (GETLOCAL(m+i) == NULL) {
4095 PyObject *def = defs[i];
4096 Py_INCREF(def);
4097 SETLOCAL(m+i, def);
4098 }
4099 }
4100 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004101
4102 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004103 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004104 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004105 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004106 PyObject *name;
4107 if (GETLOCAL(i) != NULL)
4108 continue;
4109 name = PyTuple_GET_ITEM(co->co_varnames, i);
4110 if (kwdefs != NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004111 PyObject *def = PyDict_GetItemWithError(kwdefs, name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004112 if (def) {
4113 Py_INCREF(def);
4114 SETLOCAL(i, def);
4115 continue;
4116 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004117 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004118 goto fail;
4119 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004120 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004121 missing++;
4122 }
4123 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004124 missing_arguments(tstate, co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004125 goto fail;
4126 }
4127 }
4128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004129 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004130 vars into frame. */
4131 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004132 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004133 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004134 /* Possibly account for the cell variable being an argument. */
4135 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004136 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004137 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004138 /* Clear the local copy. */
4139 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004140 }
4141 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004142 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004143 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004144 if (c == NULL)
4145 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004146 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004147 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004148
4149 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004150 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4151 PyObject *o = PyTuple_GET_ITEM(closure, i);
4152 Py_INCREF(o);
4153 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004154 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004155
Yury Selivanoveb636452016-09-08 22:01:51 -07004156 /* Handle generator/coroutine/asynchronous generator */
4157 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004158 PyObject *gen;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004159 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004160
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004161 /* Don't need to keep the reference to f_back, it will be set
4162 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004163 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004165 /* Create a new generator that owns the ready to run frame
4166 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004167 if (is_coro) {
4168 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004169 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4170 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004171 } else {
4172 gen = PyGen_NewWithQualName(f, name, qualname);
4173 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004174 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004175 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004176 }
INADA Naoki9c157762016-12-26 18:52:46 +09004177
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004178 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004179
Yury Selivanov75445082015-05-11 22:57:16 -04004180 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004181 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004182
Victor Stinnerb9e68122019-11-14 12:20:46 +01004183 retval = _PyEval_EvalFrame(tstate, f, 0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004184
Thomas Woutersce272b62007-09-19 21:19:28 +00004185fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004186
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004187 /* decref'ing the frame can cause __del__ methods to get invoked,
4188 which can call back into Python. While we're done with the
4189 current Python frame (f), the associated C stack is still in use,
4190 so recursion_depth must be boosted for the duration.
4191 */
INADA Naoki5a625d02016-12-24 20:19:08 +09004192 if (Py_REFCNT(f) > 1) {
4193 Py_DECREF(f);
4194 _PyObject_GC_TRACK(f);
4195 }
4196 else {
4197 ++tstate->recursion_depth;
4198 Py_DECREF(f);
4199 --tstate->recursion_depth;
4200 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004201 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004202}
4203
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004204
4205PyObject *
4206_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
4207 PyObject *const *args, Py_ssize_t argcount,
4208 PyObject *const *kwnames, PyObject *const *kwargs,
4209 Py_ssize_t kwcount, int kwstep,
4210 PyObject *const *defs, Py_ssize_t defcount,
4211 PyObject *kwdefs, PyObject *closure,
4212 PyObject *name, PyObject *qualname)
4213{
4214 PyThreadState *tstate = _PyThreadState_GET();
4215 return _PyEval_EvalCode(tstate, _co, globals, locals,
4216 args, argcount,
4217 kwnames, kwargs,
4218 kwcount, kwstep,
4219 defs, defcount,
4220 kwdefs, closure,
4221 name, qualname);
4222}
4223
Victor Stinner40ee3012014-06-16 15:59:28 +02004224PyObject *
4225PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004226 PyObject *const *args, int argcount,
4227 PyObject *const *kws, int kwcount,
4228 PyObject *const *defs, int defcount,
4229 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004230{
4231 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004232 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004233 kws, kws != NULL ? kws + 1 : NULL,
4234 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004235 defs, defcount,
4236 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004237 NULL, NULL);
4238}
Tim Peters5ca576e2001-06-18 22:08:13 +00004239
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004240static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004241special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004242{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004243 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004244 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02004245 if (res == NULL && !_PyErr_Occurred(tstate)) {
4246 _PyErr_SetObject(tstate, PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004247 return NULL;
4248 }
4249 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004250}
4251
4252
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004253/* Logic for the raise statement (too complicated for inlining).
4254 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004255static int
Victor Stinner09532fe2019-05-10 23:39:09 +02004256do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004257{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004258 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004260 if (exc == NULL) {
4261 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01004262 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004263 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004264 type = exc_info->exc_type;
4265 value = exc_info->exc_value;
4266 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004267 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004268 _PyErr_SetString(tstate, PyExc_RuntimeError,
4269 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004270 return 0;
4271 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004272 Py_XINCREF(type);
4273 Py_XINCREF(value);
4274 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02004275 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004276 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004277 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004279 /* We support the following forms of raise:
4280 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004281 raise <instance>
4282 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004284 if (PyExceptionClass_Check(exc)) {
4285 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004286 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004287 if (value == NULL)
4288 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004289 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004290 _PyErr_Format(tstate, PyExc_TypeError,
4291 "calling %R should have returned an instance of "
4292 "BaseException, not %R",
4293 type, Py_TYPE(value));
4294 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004295 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004296 }
4297 else if (PyExceptionInstance_Check(exc)) {
4298 value = exc;
4299 type = PyExceptionInstance_Class(exc);
4300 Py_INCREF(type);
4301 }
4302 else {
4303 /* Not something you can raise. You get an exception
4304 anyway, just not what you specified :-) */
4305 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02004306 _PyErr_SetString(tstate, PyExc_TypeError,
4307 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004308 goto raise_error;
4309 }
Collin Winter828f04a2007-08-31 00:04:24 +00004310
Serhiy Storchakac0191582016-09-27 11:37:10 +03004311 assert(type != NULL);
4312 assert(value != NULL);
4313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004314 if (cause) {
4315 PyObject *fixed_cause;
4316 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004317 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004318 if (fixed_cause == NULL)
4319 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004320 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004321 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004322 else if (PyExceptionInstance_Check(cause)) {
4323 fixed_cause = cause;
4324 }
4325 else if (cause == Py_None) {
4326 Py_DECREF(cause);
4327 fixed_cause = NULL;
4328 }
4329 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004330 _PyErr_SetString(tstate, PyExc_TypeError,
4331 "exception causes must derive from "
4332 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004333 goto raise_error;
4334 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004335 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004336 }
Collin Winter828f04a2007-08-31 00:04:24 +00004337
Victor Stinner438a12d2019-05-24 17:01:38 +02004338 _PyErr_SetObject(tstate, type, value);
Victor Stinner61f4db82020-01-28 03:37:45 +01004339 /* _PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004340 Py_DECREF(value);
4341 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004342 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004343
4344raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004345 Py_XDECREF(value);
4346 Py_XDECREF(type);
4347 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004348 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004349}
4350
Tim Petersd6d010b2001-06-21 02:49:55 +00004351/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004352 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004353
Guido van Rossum0368b722007-05-11 16:50:42 +00004354 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4355 with a variable target.
4356*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004357
Barry Warsawe42b18f1997-08-25 22:13:04 +00004358static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004359unpack_iterable(PyThreadState *tstate, PyObject *v,
4360 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004361{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004362 int i = 0, j = 0;
4363 Py_ssize_t ll = 0;
4364 PyObject *it; /* iter(v) */
4365 PyObject *w;
4366 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004368 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004370 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004371 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004372 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01004373 Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v))
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004374 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004375 _PyErr_Format(tstate, PyExc_TypeError,
4376 "cannot unpack non-iterable %.200s object",
Victor Stinnera102ed72020-02-07 02:24:48 +01004377 Py_TYPE(v)->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004378 }
4379 return 0;
4380 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004382 for (; i < argcnt; i++) {
4383 w = PyIter_Next(it);
4384 if (w == NULL) {
4385 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004386 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04004387 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004388 _PyErr_Format(tstate, PyExc_ValueError,
4389 "not enough values to unpack "
4390 "(expected %d, got %d)",
4391 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004392 }
4393 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004394 _PyErr_Format(tstate, PyExc_ValueError,
4395 "not enough values to unpack "
4396 "(expected at least %d, got %d)",
4397 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004398 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004399 }
4400 goto Error;
4401 }
4402 *--sp = w;
4403 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004405 if (argcntafter == -1) {
4406 /* We better have exhausted the iterator now. */
4407 w = PyIter_Next(it);
4408 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004409 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004410 goto Error;
4411 Py_DECREF(it);
4412 return 1;
4413 }
4414 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02004415 _PyErr_Format(tstate, PyExc_ValueError,
4416 "too many values to unpack (expected %d)",
4417 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004418 goto Error;
4419 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004421 l = PySequence_List(it);
4422 if (l == NULL)
4423 goto Error;
4424 *--sp = l;
4425 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004427 ll = PyList_GET_SIZE(l);
4428 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004429 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04004430 "not enough values to unpack (expected at least %d, got %zd)",
4431 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004432 goto Error;
4433 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004435 /* Pop the "after-variable" args off the list. */
4436 for (j = argcntafter; j > 0; j--, i++) {
4437 *--sp = PyList_GET_ITEM(l, ll - j);
4438 }
4439 /* Resize the list. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +01004440 Py_SET_SIZE(l, ll - argcntafter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004441 Py_DECREF(it);
4442 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004443
Tim Petersd6d010b2001-06-21 02:49:55 +00004444Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004445 for (; i > 0; i--, sp++)
4446 Py_DECREF(*sp);
4447 Py_XDECREF(it);
4448 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004449}
4450
4451
Guido van Rossum96a42c81992-01-12 02:29:51 +00004452#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004453static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004454prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004455{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004456 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02004457 if (PyObject_Print(v, stdout, 0) != 0) {
4458 /* Don't know what else to do */
4459 _PyErr_Clear(tstate);
4460 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004461 printf("\n");
4462 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004463}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004464#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004465
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004466static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004467call_exc_trace(Py_tracefunc func, PyObject *self,
4468 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004469{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004470 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004471 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004472 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004473 if (value == NULL) {
4474 value = Py_None;
4475 Py_INCREF(value);
4476 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004477 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01004478 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004479 arg = PyTuple_Pack(3, type, value, traceback);
4480 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004481 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004482 return;
4483 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004484 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004485 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02004486 if (err == 0) {
4487 _PyErr_Restore(tstate, type, value, orig_traceback);
4488 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004489 else {
4490 Py_XDECREF(type);
4491 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004492 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004493 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004494}
4495
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004496static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004497call_trace_protected(Py_tracefunc func, PyObject *obj,
4498 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004499 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004500{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004501 PyObject *type, *value, *traceback;
4502 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004503 _PyErr_Fetch(tstate, &type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004504 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004505 if (err == 0)
4506 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004507 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004508 return 0;
4509 }
4510 else {
4511 Py_XDECREF(type);
4512 Py_XDECREF(value);
4513 Py_XDECREF(traceback);
4514 return -1;
4515 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004516}
4517
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004518static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004519call_trace(Py_tracefunc func, PyObject *obj,
4520 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004521 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004522{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004523 int result;
4524 if (tstate->tracing)
4525 return 0;
4526 tstate->tracing++;
4527 tstate->use_tracing = 0;
4528 result = func(obj, frame, what, arg);
4529 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4530 || (tstate->c_profilefunc != NULL));
4531 tstate->tracing--;
4532 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004533}
4534
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004535PyObject *
4536_PyEval_CallTracing(PyObject *func, PyObject *args)
4537{
Victor Stinner50b48572018-11-01 01:51:40 +01004538 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004539 int save_tracing = tstate->tracing;
4540 int save_use_tracing = tstate->use_tracing;
4541 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004543 tstate->tracing = 0;
4544 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4545 || (tstate->c_profilefunc != NULL));
4546 result = PyObject_Call(func, args, NULL);
4547 tstate->tracing = save_tracing;
4548 tstate->use_tracing = save_use_tracing;
4549 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004550}
4551
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004552/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004553static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004554maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004555 PyThreadState *tstate, PyFrameObject *frame,
4556 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004557{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004558 int result = 0;
4559 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004561 /* If the last instruction executed isn't in the current
4562 instruction window, reset the window.
4563 */
4564 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4565 PyAddrPair bounds;
4566 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4567 &bounds);
4568 *instr_lb = bounds.ap_lower;
4569 *instr_ub = bounds.ap_upper;
4570 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004571 /* If the last instruction falls at the start of a line or if it
4572 represents a jump backwards, update the frame's line number and
4573 then call the trace function if we're tracing source lines.
4574 */
4575 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004576 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004577 if (frame->f_trace_lines) {
4578 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4579 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004580 }
George King20faa682017-10-18 17:44:22 -07004581 /* Always emit an opcode event if we're tracing all opcodes. */
4582 if (frame->f_trace_opcodes) {
4583 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4584 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004585 *instr_prev = frame->f_lasti;
4586 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004587}
4588
Victor Stinner309d7cc2020-03-13 16:39:12 +01004589int
4590_PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
4591{
4592 assert(tstate != NULL);
4593 /* The caller must hold the GIL */
4594 assert(PyGILState_Check());
4595
4596 /* Call PySys_Audit() in the context of the current thread state,
4597 even if tstate is not the current thread state. */
4598 if (PySys_Audit("sys.setprofile", NULL) < 0) {
4599 return -1;
4600 }
4601
4602 PyObject *profileobj = tstate->c_profileobj;
4603
4604 tstate->c_profilefunc = NULL;
4605 tstate->c_profileobj = NULL;
4606 /* Must make sure that tracing is not ignored if 'profileobj' is freed */
4607 tstate->use_tracing = tstate->c_tracefunc != NULL;
4608 Py_XDECREF(profileobj);
4609
4610 Py_XINCREF(arg);
4611 tstate->c_profileobj = arg;
4612 tstate->c_profilefunc = func;
4613
4614 /* Flag that tracing or profiling is turned on */
4615 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
4616 return 0;
4617}
4618
Fred Drake5755ce62001-06-27 19:19:46 +00004619void
4620PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004621{
Victor Stinner309d7cc2020-03-13 16:39:12 +01004622 PyThreadState *tstate = _PyThreadState_GET();
4623 (void)_PyEval_SetProfile(tstate, func, arg);
4624}
4625
4626int
4627_PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
4628{
4629 assert(tstate != NULL);
4630 /* The caller must hold the GIL */
4631 assert(PyGILState_Check());
4632
4633 /* Call PySys_Audit() in the context of the current thread state,
4634 even if tstate is not the current thread state. */
4635 if (PySys_Audit("sys.settrace", NULL) < 0) {
4636 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004637 }
4638
Victor Stinner309d7cc2020-03-13 16:39:12 +01004639 struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
4640 PyObject *traceobj = tstate->c_traceobj;
4641 ceval->tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
4642
4643 tstate->c_tracefunc = NULL;
4644 tstate->c_traceobj = NULL;
4645 /* Must make sure that profiling is not ignored if 'traceobj' is freed */
4646 tstate->use_tracing = (tstate->c_profilefunc != NULL);
4647 Py_XDECREF(traceobj);
4648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004649 Py_XINCREF(arg);
Victor Stinner309d7cc2020-03-13 16:39:12 +01004650 tstate->c_traceobj = arg;
4651 tstate->c_tracefunc = func;
4652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004653 /* Flag that tracing or profiling is turned on */
Victor Stinner309d7cc2020-03-13 16:39:12 +01004654 tstate->use_tracing = ((func != NULL)
4655 || (tstate->c_profilefunc != NULL));
4656
4657 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +00004658}
4659
4660void
4661PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4662{
Victor Stinner309d7cc2020-03-13 16:39:12 +01004663 PyThreadState *tstate = _PyThreadState_GET();
4664 (void)_PyEval_SetTrace(tstate, func, arg);
Fred Draked0838392001-06-16 21:02:31 +00004665}
4666
Victor Stinner309d7cc2020-03-13 16:39:12 +01004667
Yury Selivanov75445082015-05-11 22:57:16 -04004668void
Victor Stinner838f2642019-06-13 22:41:23 +02004669_PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004670{
4671 assert(new_depth >= 0);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004672 tstate->coroutine_origin_tracking_depth = new_depth;
4673}
4674
4675int
4676_PyEval_GetCoroutineOriginTrackingDepth(void)
4677{
Victor Stinner50b48572018-11-01 01:51:40 +01004678 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004679 return tstate->coroutine_origin_tracking_depth;
4680}
4681
4682void
Yury Selivanoveb636452016-09-08 22:01:51 -07004683_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4684{
Victor Stinner50b48572018-11-01 01:51:40 +01004685 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004686
4687 if (PySys_Audit("sys.set_asyncgen_hook_firstiter", NULL) < 0) {
4688 return;
4689 }
4690
Yury Selivanoveb636452016-09-08 22:01:51 -07004691 Py_XINCREF(firstiter);
4692 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4693}
4694
4695PyObject *
4696_PyEval_GetAsyncGenFirstiter(void)
4697{
Victor Stinner50b48572018-11-01 01:51:40 +01004698 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004699 return tstate->async_gen_firstiter;
4700}
4701
4702void
4703_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4704{
Victor Stinner50b48572018-11-01 01:51:40 +01004705 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004706
4707 if (PySys_Audit("sys.set_asyncgen_hook_finalizer", NULL) < 0) {
4708 return;
4709 }
4710
Yury Selivanoveb636452016-09-08 22:01:51 -07004711 Py_XINCREF(finalizer);
4712 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4713}
4714
4715PyObject *
4716_PyEval_GetAsyncGenFinalizer(void)
4717{
Victor Stinner50b48572018-11-01 01:51:40 +01004718 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004719 return tstate->async_gen_finalizer;
4720}
4721
Victor Stinner438a12d2019-05-24 17:01:38 +02004722static PyFrameObject *
4723_PyEval_GetFrame(PyThreadState *tstate)
4724{
Victor Stinner01b1cc12019-11-20 02:27:56 +01004725 _PyRuntimeState *runtime = tstate->interp->runtime;
4726 return runtime->gilstate.getframe(tstate);
Victor Stinner438a12d2019-05-24 17:01:38 +02004727}
4728
4729PyFrameObject *
4730PyEval_GetFrame(void)
4731{
4732 PyThreadState *tstate = _PyThreadState_GET();
4733 return _PyEval_GetFrame(tstate);
4734}
4735
Guido van Rossumb209a111997-04-29 18:18:01 +00004736PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004737PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004738{
Victor Stinner438a12d2019-05-24 17:01:38 +02004739 PyThreadState *tstate = _PyThreadState_GET();
4740 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004741 if (current_frame == NULL)
Victor Stinner438a12d2019-05-24 17:01:38 +02004742 return tstate->interp->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004743 else
4744 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004745}
4746
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004747/* Convenience function to get a builtin from its name */
4748PyObject *
4749_PyEval_GetBuiltinId(_Py_Identifier *name)
4750{
Victor Stinner438a12d2019-05-24 17:01:38 +02004751 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004752 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4753 if (attr) {
4754 Py_INCREF(attr);
4755 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004756 else if (!_PyErr_Occurred(tstate)) {
4757 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004758 }
4759 return attr;
4760}
4761
Guido van Rossumb209a111997-04-29 18:18:01 +00004762PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004763PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004764{
Victor Stinner438a12d2019-05-24 17:01:38 +02004765 PyThreadState *tstate = _PyThreadState_GET();
4766 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Victor Stinner41bb43a2013-10-29 01:19:37 +01004767 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004768 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004769 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004770 }
4771
Victor Stinner438a12d2019-05-24 17:01:38 +02004772 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01004773 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004774 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004775
4776 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004777 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004778}
4779
Guido van Rossumb209a111997-04-29 18:18:01 +00004780PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004781PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004782{
Victor Stinner438a12d2019-05-24 17:01:38 +02004783 PyThreadState *tstate = _PyThreadState_GET();
4784 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
4785 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004786 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004787 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004788
4789 assert(current_frame->f_globals != NULL);
4790 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004791}
4792
Guido van Rossum6135a871995-01-09 17:53:26 +00004793int
Tim Peters5ba58662001-07-16 02:29:45 +00004794PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004795{
Victor Stinner438a12d2019-05-24 17:01:38 +02004796 PyThreadState *tstate = _PyThreadState_GET();
4797 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004798 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004799
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004800 if (current_frame != NULL) {
4801 const int codeflags = current_frame->f_code->co_flags;
4802 const int compilerflags = codeflags & PyCF_MASK;
4803 if (compilerflags) {
4804 result = 1;
4805 cf->cf_flags |= compilerflags;
4806 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004807#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004808 if (codeflags & CO_GENERATOR_ALLOWED) {
4809 result = 1;
4810 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4811 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004812#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004813 }
4814 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004815}
4816
Guido van Rossum3f5da241990-12-20 15:06:42 +00004817
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004818const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004819PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004820{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004821 if (PyMethod_Check(func))
4822 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4823 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004824 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004825 else if (PyCFunction_Check(func))
4826 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4827 else
Victor Stinnera102ed72020-02-07 02:24:48 +01004828 return Py_TYPE(func)->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004829}
4830
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004831const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004832PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004833{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004834 if (PyMethod_Check(func))
4835 return "()";
4836 else if (PyFunction_Check(func))
4837 return "()";
4838 else if (PyCFunction_Check(func))
4839 return "()";
4840 else
4841 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004842}
4843
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004844#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004845if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004846 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4847 tstate, tstate->frame, \
4848 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004849 x = NULL; \
4850 } \
4851 else { \
4852 x = call; \
4853 if (tstate->c_profilefunc != NULL) { \
4854 if (x == NULL) { \
4855 call_trace_protected(tstate->c_profilefunc, \
4856 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004857 tstate, tstate->frame, \
4858 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004859 /* XXX should pass (type, value, tb) */ \
4860 } else { \
4861 if (call_trace(tstate->c_profilefunc, \
4862 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004863 tstate, tstate->frame, \
4864 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004865 Py_DECREF(x); \
4866 x = NULL; \
4867 } \
4868 } \
4869 } \
4870 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004871} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004872 x = call; \
4873 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004874
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004875
4876static PyObject *
4877trace_call_function(PyThreadState *tstate,
4878 PyObject *func,
4879 PyObject **args, Py_ssize_t nargs,
4880 PyObject *kwnames)
4881{
4882 PyObject *x;
4883 if (PyCFunction_Check(func)) {
Petr Viktorinffd97532020-02-11 17:46:57 +01004884 C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004885 return x;
4886 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06004887 else if (Py_IS_TYPE(func, &PyMethodDescr_Type) && nargs > 0) {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004888 /* We need to create a temporary bound method as argument
4889 for profiling.
4890
4891 If nargs == 0, then this cannot work because we have no
4892 "self". In any case, the call itself would raise
4893 TypeError (foo needs an argument), so we just skip
4894 profiling. */
4895 PyObject *self = args[0];
4896 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4897 if (func == NULL) {
4898 return NULL;
4899 }
Petr Viktorinffd97532020-02-11 17:46:57 +01004900 C_TRACE(x, PyObject_Vectorcall(func,
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02004901 args+1, nargs-1,
4902 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004903 Py_DECREF(func);
4904 return x;
4905 }
Petr Viktorinffd97532020-02-11 17:46:57 +01004906 return PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004907}
4908
Victor Stinner415c5102017-01-11 00:54:57 +01004909/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4910 to reduce the stack consumption. */
4911Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Victor Stinner09532fe2019-05-10 23:39:09 +02004912call_function(PyThreadState *tstate, PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004913{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004914 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004915 PyObject *func = *pfunc;
4916 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004917 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4918 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004919 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004920
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004921 if (tstate->use_tracing) {
4922 x = trace_call_function(tstate, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09004923 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004924 else {
Petr Viktorinffd97532020-02-11 17:46:57 +01004925 x = PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004926 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004927
Victor Stinner438a12d2019-05-24 17:01:38 +02004928 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004929
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004930 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004931 while ((*pp_stack) > pfunc) {
4932 w = EXT_POP(*pp_stack);
4933 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004934 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004935
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004936 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004937}
4938
Jeremy Hylton52820442001-01-03 23:52:36 +00004939static PyObject *
Victor Stinner09532fe2019-05-10 23:39:09 +02004940do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004941{
jdemeyere89de732018-09-19 12:06:20 +02004942 PyObject *result;
4943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004944 if (PyCFunction_Check(func)) {
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +02004945 C_TRACE(result, PyObject_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004946 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004947 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06004948 else if (Py_IS_TYPE(func, &PyMethodDescr_Type)) {
jdemeyere89de732018-09-19 12:06:20 +02004949 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
4950 if (nargs > 0 && tstate->use_tracing) {
4951 /* We need to create a temporary bound method as argument
4952 for profiling.
4953
4954 If nargs == 0, then this cannot work because we have no
4955 "self". In any case, the call itself would raise
4956 TypeError (foo needs an argument), so we just skip
4957 profiling. */
4958 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
4959 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4960 if (func == NULL) {
4961 return NULL;
4962 }
4963
Victor Stinner4d231bc2019-11-14 13:36:21 +01004964 C_TRACE(result, _PyObject_FastCallDictTstate(
4965 tstate, func,
4966 &_PyTuple_ITEMS(callargs)[1],
4967 nargs - 1,
4968 kwdict));
jdemeyere89de732018-09-19 12:06:20 +02004969 Py_DECREF(func);
4970 return result;
4971 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004972 }
jdemeyere89de732018-09-19 12:06:20 +02004973 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00004974}
4975
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004976/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004977 nb_index slot defined, and store in *pi.
4978 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004979 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004980 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004981*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004982int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004983_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004984{
Victor Stinner438a12d2019-05-24 17:01:38 +02004985 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004986 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004987 Py_ssize_t x;
4988 if (PyIndex_Check(v)) {
4989 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02004990 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004991 return 0;
4992 }
4993 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004994 _PyErr_SetString(tstate, PyExc_TypeError,
4995 "slice indices must be integers or "
4996 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004997 return 0;
4998 }
4999 *pi = x;
5000 }
5001 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005002}
5003
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005004int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005005_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005006{
Victor Stinner438a12d2019-05-24 17:01:38 +02005007 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005008 Py_ssize_t x;
5009 if (PyIndex_Check(v)) {
5010 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005011 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005012 return 0;
5013 }
5014 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005015 _PyErr_SetString(tstate, PyExc_TypeError,
5016 "slice indices must be integers or "
5017 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005018 return 0;
5019 }
5020 *pi = x;
5021 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005022}
5023
Thomas Wouters52152252000-08-17 22:55:00 +00005024static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005025import_name(PyThreadState *tstate, PyFrameObject *f,
5026 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005027{
5028 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005029 PyObject *import_func, *res;
5030 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005031
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005032 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005033 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005034 if (!_PyErr_Occurred(tstate)) {
5035 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005036 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005037 return NULL;
5038 }
5039
5040 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005041 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005042 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02005043 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005044 return NULL;
5045 }
5046 res = PyImport_ImportModuleLevelObject(
5047 name,
5048 f->f_globals,
5049 f->f_locals == NULL ? Py_None : f->f_locals,
5050 fromlist,
5051 ilevel);
5052 return res;
5053 }
5054
5055 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005056
5057 stack[0] = name;
5058 stack[1] = f->f_globals;
5059 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5060 stack[3] = fromlist;
5061 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005062 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005063 Py_DECREF(import_func);
5064 return res;
5065}
5066
5067static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005068import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005069{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005070 PyObject *x;
Xiang Zhang4830f582017-03-21 11:13:42 +08005071 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005072
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005073 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005074 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005075 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005076 /* Issue #17636: in case this failed because of a circular relative
5077 import, try to fallback on reading the module directly from
5078 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02005079 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005080 if (pkgname == NULL) {
5081 goto error;
5082 }
Oren Milman6db70332017-09-19 14:23:01 +03005083 if (!PyUnicode_Check(pkgname)) {
5084 Py_CLEAR(pkgname);
5085 goto error;
5086 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005087 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005088 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005089 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005090 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005091 }
Eric Snow3f9eee62017-09-15 16:35:20 -06005092 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005093 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02005094 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07005095 goto error;
5096 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005097 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005098 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005099 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005100 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005101 if (pkgname == NULL) {
5102 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5103 if (pkgname_or_unknown == NULL) {
5104 Py_XDECREF(pkgpath);
5105 return NULL;
5106 }
5107 } else {
5108 pkgname_or_unknown = pkgname;
5109 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005110
5111 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005112 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08005113 errmsg = PyUnicode_FromFormat(
5114 "cannot import name %R from %R (unknown location)",
5115 name, pkgname_or_unknown
5116 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005117 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005118 PyErr_SetImportError(errmsg, pkgname, NULL);
5119 }
5120 else {
Anthony Sottile65366bc2019-09-09 08:17:50 -07005121 _Py_IDENTIFIER(__spec__);
5122 PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
Anthony Sottile65366bc2019-09-09 08:17:50 -07005123 const char *fmt =
5124 _PyModuleSpec_IsInitializing(spec) ?
5125 "cannot import name %R from partially initialized module %R "
5126 "(most likely due to a circular import) (%S)" :
5127 "cannot import name %R from %R (%S)";
5128 Py_XDECREF(spec);
5129
5130 errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
Stefan Krah027b09c2019-03-25 21:50:58 +01005131 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005132 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005133 }
5134
Xiang Zhang4830f582017-03-21 11:13:42 +08005135 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005136 Py_XDECREF(pkgname_or_unknown);
5137 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005138 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005139}
Guido van Rossumac7be682001-01-17 15:42:30 +00005140
Thomas Wouters52152252000-08-17 22:55:00 +00005141static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005142import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00005143{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005144 _Py_IDENTIFIER(__all__);
5145 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005146 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005147 int skip_leading_underscores = 0;
5148 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005149
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005150 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5151 return -1; /* Unexpected error */
5152 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005153 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005154 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5155 return -1;
5156 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005157 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005158 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005159 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005160 return -1;
5161 }
5162 all = PyMapping_Keys(dict);
5163 Py_DECREF(dict);
5164 if (all == NULL)
5165 return -1;
5166 skip_leading_underscores = 1;
5167 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005168
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005169 for (pos = 0, err = 0; ; pos++) {
5170 name = PySequence_GetItem(all, pos);
5171 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005172 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005173 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02005174 }
5175 else {
5176 _PyErr_Clear(tstate);
5177 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005178 break;
5179 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005180 if (!PyUnicode_Check(name)) {
5181 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5182 if (modname == NULL) {
5183 Py_DECREF(name);
5184 err = -1;
5185 break;
5186 }
5187 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005188 _PyErr_Format(tstate, PyExc_TypeError,
5189 "module __name__ must be a string, not %.100s",
5190 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005191 }
5192 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005193 _PyErr_Format(tstate, PyExc_TypeError,
5194 "%s in %U.%s must be str, not %.100s",
5195 skip_leading_underscores ? "Key" : "Item",
5196 modname,
5197 skip_leading_underscores ? "__dict__" : "__all__",
5198 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005199 }
5200 Py_DECREF(modname);
5201 Py_DECREF(name);
5202 err = -1;
5203 break;
5204 }
5205 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005206 if (PyUnicode_READY(name) == -1) {
5207 Py_DECREF(name);
5208 err = -1;
5209 break;
5210 }
5211 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5212 Py_DECREF(name);
5213 continue;
5214 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005215 }
5216 value = PyObject_GetAttr(v, name);
5217 if (value == NULL)
5218 err = -1;
5219 else if (PyDict_CheckExact(locals))
5220 err = PyDict_SetItem(locals, name, value);
5221 else
5222 err = PyObject_SetItem(locals, name, value);
5223 Py_DECREF(name);
5224 Py_XDECREF(value);
5225 if (err != 0)
5226 break;
5227 }
5228 Py_DECREF(all);
5229 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005230}
5231
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005232static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005233check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005234{
Victor Stinnera102ed72020-02-07 02:24:48 +01005235 if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005236 /* check_args_iterable() may be called with a live exception:
5237 * clear it to prevent calling _PyObject_FunctionStr() with an
5238 * exception set. */
Victor Stinner61f4db82020-01-28 03:37:45 +01005239 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005240 PyObject *funcstr = _PyObject_FunctionStr(func);
5241 if (funcstr != NULL) {
5242 _PyErr_Format(tstate, PyExc_TypeError,
5243 "%U argument after * must be an iterable, not %.200s",
5244 funcstr, Py_TYPE(args)->tp_name);
5245 Py_DECREF(funcstr);
5246 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005247 return -1;
5248 }
5249 return 0;
5250}
5251
5252static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005253format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005254{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005255 /* _PyDict_MergeEx raises attribute
5256 * error (percolated from an attempt
5257 * to get 'keys' attribute) instead of
5258 * a type error if its second argument
5259 * is not a mapping.
5260 */
Victor Stinner438a12d2019-05-24 17:01:38 +02005261 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005262 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005263 PyObject *funcstr = _PyObject_FunctionStr(func);
5264 if (funcstr != NULL) {
5265 _PyErr_Format(
5266 tstate, PyExc_TypeError,
5267 "%U argument after ** must be a mapping, not %.200s",
5268 funcstr, Py_TYPE(kwargs)->tp_name);
5269 Py_DECREF(funcstr);
5270 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005271 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005272 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005273 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02005274 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005275 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005276 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005277 PyObject *funcstr = _PyObject_FunctionStr(func);
5278 if (funcstr != NULL) {
5279 PyObject *key = PyTuple_GET_ITEM(val, 0);
5280 _PyErr_Format(
5281 tstate, PyExc_TypeError,
5282 "%U got multiple values for keyword argument '%S'",
5283 funcstr, key);
5284 Py_DECREF(funcstr);
5285 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005286 Py_XDECREF(exc);
5287 Py_XDECREF(val);
5288 Py_XDECREF(tb);
5289 }
5290 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005291 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005292 }
5293 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005294}
5295
Guido van Rossumac7be682001-01-17 15:42:30 +00005296static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005297format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
5298 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005299{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005300 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005302 if (!obj)
5303 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005304
Serhiy Storchaka06515832016-11-20 09:13:07 +02005305 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005306 if (!obj_str)
5307 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005308
Victor Stinner438a12d2019-05-24 17:01:38 +02005309 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005310}
Guido van Rossum950361c1997-01-24 13:49:28 +00005311
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005312static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005313format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005314{
5315 PyObject *name;
5316 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02005317 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005318 return;
5319 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5320 name = PyTuple_GET_ITEM(co->co_cellvars,
5321 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005322 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005323 PyExc_UnboundLocalError,
5324 UNBOUNDLOCAL_ERROR_MSG,
5325 name);
5326 } else {
5327 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5328 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02005329 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005330 UNBOUNDFREE_ERROR_MSG, name);
5331 }
5332}
5333
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005334static void
Mark Shannonfee55262019-11-21 09:11:43 +00005335format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevprevopcode, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005336{
5337 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5338 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005339 _PyErr_Format(tstate, PyExc_TypeError,
5340 "'async with' received an object from __aenter__ "
5341 "that does not implement __await__: %.100s",
5342 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005343 }
Mark Shannonfee55262019-11-21 09:11:43 +00005344 else if (prevopcode == WITH_EXCEPT_START || (prevopcode == CALL_FUNCTION && prevprevopcode == DUP_TOP)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005345 _PyErr_Format(tstate, PyExc_TypeError,
5346 "'async with' received an object from __aexit__ "
5347 "that does not implement __await__: %.100s",
5348 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005349 }
5350 }
5351}
5352
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005353static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005354unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005355 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005356{
5357 PyObject *res;
5358 if (Py_REFCNT(v) == 2) {
5359 /* In the common case, there are 2 references to the value
5360 * stored in 'variable' when the += is performed: one on the
5361 * value stack (in 'v') and one still stored in the
5362 * 'variable'. We try to delete the variable now to reduce
5363 * the refcnt to 1.
5364 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005365 int opcode, oparg;
5366 NEXTOPARG();
5367 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005368 case STORE_FAST:
5369 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005370 PyObject **fastlocals = f->f_localsplus;
5371 if (GETLOCAL(oparg) == v)
5372 SETLOCAL(oparg, NULL);
5373 break;
5374 }
5375 case STORE_DEREF:
5376 {
5377 PyObject **freevars = (f->f_localsplus +
5378 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005379 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005380 if (PyCell_GET(c) == v) {
5381 PyCell_SET(c, NULL);
5382 Py_DECREF(v);
5383 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005384 break;
5385 }
5386 case STORE_NAME:
5387 {
5388 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005389 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005390 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005391 if (locals && PyDict_CheckExact(locals)) {
5392 PyObject *w = PyDict_GetItemWithError(locals, name);
5393 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02005394 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005395 {
5396 Py_DECREF(v);
5397 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005398 }
5399 }
5400 break;
5401 }
5402 }
5403 }
5404 res = v;
5405 PyUnicode_Append(&res, w);
5406 return res;
5407}
5408
Guido van Rossum950361c1997-01-24 13:49:28 +00005409#ifdef DYNAMIC_EXECUTION_PROFILE
5410
Skip Montanarof118cb12001-10-15 20:51:38 +00005411static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005412getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005413{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005414 int i;
5415 PyObject *l = PyList_New(256);
5416 if (l == NULL) return NULL;
5417 for (i = 0; i < 256; i++) {
5418 PyObject *x = PyLong_FromLong(a[i]);
5419 if (x == NULL) {
5420 Py_DECREF(l);
5421 return NULL;
5422 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005423 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005424 }
5425 for (i = 0; i < 256; i++)
5426 a[i] = 0;
5427 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005428}
5429
5430PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005431_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005432{
5433#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005434 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005435#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005436 int i;
5437 PyObject *l = PyList_New(257);
5438 if (l == NULL) return NULL;
5439 for (i = 0; i < 257; i++) {
5440 PyObject *x = getarray(dxpairs[i]);
5441 if (x == NULL) {
5442 Py_DECREF(l);
5443 return NULL;
5444 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005445 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005446 }
5447 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005448#endif
5449}
5450
5451#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005452
5453Py_ssize_t
5454_PyEval_RequestCodeExtraIndex(freefunc free)
5455{
Victor Stinnercaba55b2018-08-03 15:33:52 +02005456 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -07005457 Py_ssize_t new_index;
5458
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005459 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005460 return -1;
5461 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005462 new_index = interp->co_extra_user_count++;
5463 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005464 return new_index;
5465}
Łukasz Langaa785c872016-09-09 17:37:37 -07005466
5467static void
5468dtrace_function_entry(PyFrameObject *f)
5469{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005470 const char *filename;
5471 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005472 int lineno;
5473
5474 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5475 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5476 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5477
Andy Lestere6be9b52020-02-11 20:28:35 -06005478 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005479}
5480
5481static void
5482dtrace_function_return(PyFrameObject *f)
5483{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005484 const char *filename;
5485 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005486 int lineno;
5487
5488 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5489 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5490 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5491
Andy Lestere6be9b52020-02-11 20:28:35 -06005492 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005493}
5494
5495/* DTrace equivalent of maybe_call_line_trace. */
5496static void
5497maybe_dtrace_line(PyFrameObject *frame,
5498 int *instr_lb, int *instr_ub, int *instr_prev)
5499{
5500 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005501 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005502
5503 /* If the last instruction executed isn't in the current
5504 instruction window, reset the window.
5505 */
5506 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5507 PyAddrPair bounds;
5508 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5509 &bounds);
5510 *instr_lb = bounds.ap_lower;
5511 *instr_ub = bounds.ap_upper;
5512 }
5513 /* If the last instruction falls at the start of a line or if
5514 it represents a jump backwards, update the frame's line
5515 number and call the trace function. */
5516 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5517 frame->f_lineno = line;
5518 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5519 if (!co_filename)
5520 co_filename = "?";
5521 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5522 if (!co_name)
5523 co_name = "?";
Andy Lestere6be9b52020-02-11 20:28:35 -06005524 PyDTrace_LINE(co_filename, co_name, line);
Łukasz Langaa785c872016-09-09 17:37:37 -07005525 }
5526 *instr_prev = frame->f_lasti;
5527}
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005528
5529
5530/* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions
5531 for the limited API. */
5532
5533#undef Py_EnterRecursiveCall
5534
5535int Py_EnterRecursiveCall(const char *where)
5536{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005537 return _Py_EnterRecursiveCall_inline(where);
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005538}
5539
5540#undef Py_LeaveRecursiveCall
5541
5542void Py_LeaveRecursiveCall(void)
5543{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005544 _Py_LeaveRecursiveCall_inline();
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005545}