blob: d9a3ee0fabc9ef08e493e7be6ee9ff977dcf7ace [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 Stinner5a3a71d2020-03-19 17:40:12 +0100123
124/* Only handle signals on the main thread of the main interpreter. */
125static int
126thread_can_handle_signals(void)
127{
128 return (PyThread_get_thread_ident() == _PyRuntime.main_thread);
129}
130
131
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000132/* This can set eval_breaker to 0 even though gil_drop_request became
133 1. We believe this is all right because the eval loop will release
134 the GIL eventually anyway. */
Victor Stinner50e6e992020-03-19 02:41:21 +0100135#define COMPUTE_EVAL_BREAKER(ceval, ceval2) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000136 _Py_atomic_store_relaxed( \
Victor Stinner50e6e992020-03-19 02:41:21 +0100137 &(ceval2)->eval_breaker, \
138 _Py_atomic_load_relaxed(&(ceval)->gil_drop_request) | \
Victor Stinner5a3a71d2020-03-19 17:40:12 +0100139 (_Py_atomic_load_relaxed(&(ceval)->signals_pending) \
140 && thread_can_handle_signals()) | \
Victor Stinner50e6e992020-03-19 02:41:21 +0100141 _Py_atomic_load_relaxed(&(ceval2)->pending.calls_to_do) | \
142 (ceval2)->pending.async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000143
Victor Stinnere225beb2019-06-03 18:14:24 +0200144#define SET_GIL_DROP_REQUEST(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200146 _Py_atomic_store_relaxed(&(ceval)->gil_drop_request, 1); \
Victor Stinner50e6e992020-03-19 02:41:21 +0100147 _Py_atomic_store_relaxed(&(ceval2)->eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000149
Victor Stinner50e6e992020-03-19 02:41:21 +0100150#define RESET_GIL_DROP_REQUEST(ceval, ceval2) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200152 _Py_atomic_store_relaxed(&(ceval)->gil_drop_request, 0); \
Victor Stinner50e6e992020-03-19 02:41:21 +0100153 COMPUTE_EVAL_BREAKER(ceval, ceval2); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000154 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000155
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000156/* Pending calls are only modified under pending_lock */
Victor Stinner50e6e992020-03-19 02:41:21 +0100157#define SIGNAL_PENDING_CALLS(ceval2) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 do { \
Victor Stinner50e6e992020-03-19 02:41:21 +0100159 _Py_atomic_store_relaxed(&(ceval2)->pending.calls_to_do, 1); \
160 _Py_atomic_store_relaxed(&(ceval2)->eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000161 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000162
Victor Stinner50e6e992020-03-19 02:41:21 +0100163#define UNSIGNAL_PENDING_CALLS(ceval, ceval2) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164 do { \
Victor Stinner50e6e992020-03-19 02:41:21 +0100165 _Py_atomic_store_relaxed(&(ceval2)->pending.calls_to_do, 0); \
166 COMPUTE_EVAL_BREAKER(ceval, ceval2); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000168
Victor Stinner5a3a71d2020-03-19 17:40:12 +0100169/* eval_breaker is not set to 1 if thread_can_handle_signals() is false. */
Victor Stinner50e6e992020-03-19 02:41:21 +0100170#define SIGNAL_PENDING_SIGNALS(ceval, ceval2) \
Eric Snowfdf282d2019-01-11 14:26:55 -0700171 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200172 _Py_atomic_store_relaxed(&(ceval)->signals_pending, 1); \
Victor Stinner5a3a71d2020-03-19 17:40:12 +0100173 COMPUTE_EVAL_BREAKER(ceval, ceval2); \
Eric Snowfdf282d2019-01-11 14:26:55 -0700174 } while (0)
175
Victor Stinner50e6e992020-03-19 02:41:21 +0100176#define UNSIGNAL_PENDING_SIGNALS(ceval, ceval2) \
Eric Snowfdf282d2019-01-11 14:26:55 -0700177 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200178 _Py_atomic_store_relaxed(&(ceval)->signals_pending, 0); \
Victor Stinner50e6e992020-03-19 02:41:21 +0100179 COMPUTE_EVAL_BREAKER(ceval, ceval2); \
Eric Snowfdf282d2019-01-11 14:26:55 -0700180 } while (0)
181
Victor Stinner50e6e992020-03-19 02:41:21 +0100182#define SIGNAL_ASYNC_EXC(ceval2) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000183 do { \
Victor Stinner50e6e992020-03-19 02:41:21 +0100184 (ceval2)->pending.async_exc = 1; \
185 _Py_atomic_store_relaxed(&(ceval2)->eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000186 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000187
Victor Stinner50e6e992020-03-19 02:41:21 +0100188#define UNSIGNAL_ASYNC_EXC(ceval, ceval2) \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600189 do { \
Victor Stinner50e6e992020-03-19 02:41:21 +0100190 (ceval2)->pending.async_exc = 0; \
191 COMPUTE_EVAL_BREAKER(ceval, ceval2); \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600192 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000193
194
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000195#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000196#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000197#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000198#include "pythread.h"
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000199#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000200
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100201static void
202ensure_tstate_not_null(const char *func, PyThreadState *tstate)
203{
204 if (tstate == NULL) {
Victor Stinner23ef89d2020-03-18 02:26:04 +0100205 _Py_FatalErrorFunc(func,
206 "current thread state is NULL (released GIL?)");
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100207 }
208}
209
210
Tim Peters7f468f22004-10-11 02:40:51 +0000211int
Victor Stinner175a7042020-03-10 00:37:48 +0100212_PyEval_ThreadsInitialized(_PyRuntimeState *runtime)
213{
214 return gil_created(&runtime->ceval.gil);
215}
216
217int
Tim Peters7f468f22004-10-11 02:40:51 +0000218PyEval_ThreadsInitialized(void)
219{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100220 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner175a7042020-03-10 00:37:48 +0100221 return _PyEval_ThreadsInitialized(runtime);
Tim Peters7f468f22004-10-11 02:40:51 +0000222}
223
Victor Stinner111e4ee2020-03-09 21:24:14 +0100224PyStatus
225_PyEval_InitThreads(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000226{
Victor Stinner50e6e992020-03-19 02:41:21 +0100227 assert(tstate != NULL);
228 if (_Py_IsMainInterpreter(tstate)) {
229 struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil;
230 if (gil_created(gil)) {
231 return _PyStatus_OK();
232 }
233
234 PyThread_init_thread();
235 create_gil(gil);
236
237 take_gil(tstate);
Victor Stinner111e4ee2020-03-09 21:24:14 +0100238 }
239
Victor Stinner50e6e992020-03-19 02:41:21 +0100240 struct _pending_calls *pending = &tstate->interp->ceval.pending;
241 assert(pending->lock == NULL);
Victor Stinnere225beb2019-06-03 18:14:24 +0200242 pending->lock = PyThread_allocate_lock();
243 if (pending->lock == NULL) {
Victor Stinner111e4ee2020-03-09 21:24:14 +0100244 return _PyStatus_NO_MEMORY();
245 }
Victor Stinner85f5a692020-03-09 22:12:04 +0100246
Victor Stinner111e4ee2020-03-09 21:24:14 +0100247 return _PyStatus_OK();
248}
249
250void
251PyEval_InitThreads(void)
252{
Victor Stinnerb4698ec2020-03-10 01:28:54 +0100253 /* Do nothing: kept for backward compatibility */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000254}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000255
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000256void
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100257_PyEval_FiniThreads(PyThreadState *tstate)
Antoine Pitrou1df15362010-09-13 14:16:46 +0000258{
Victor Stinner50e6e992020-03-19 02:41:21 +0100259 struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil;
Victor Stinner09532fe2019-05-10 23:39:09 +0200260 if (!gil_created(gil)) {
Antoine Pitrou1df15362010-09-13 14:16:46 +0000261 return;
Victor Stinnera7126792019-03-19 14:19:38 +0100262 }
263
Victor Stinner09532fe2019-05-10 23:39:09 +0200264 destroy_gil(gil);
265 assert(!gil_created(gil));
Victor Stinner99fcc612019-04-29 13:04:07 +0200266
Victor Stinner50e6e992020-03-19 02:41:21 +0100267 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Victor Stinnere225beb2019-06-03 18:14:24 +0200268 if (pending->lock != NULL) {
269 PyThread_free_lock(pending->lock);
270 pending->lock = NULL;
271 }
Antoine Pitrou1df15362010-09-13 14:16:46 +0000272}
273
274void
Inada Naoki91234a12019-06-03 21:30:58 +0900275_PyEval_Fini(void)
276{
277#if OPCACHE_STATS
278 fprintf(stderr, "-- Opcode cache number of objects = %zd\n",
279 opcache_code_objects);
280
281 fprintf(stderr, "-- Opcode cache total extra mem = %zd\n",
282 opcache_code_objects_extra_mem);
283
284 fprintf(stderr, "\n");
285
286 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL hits = %zd (%d%%)\n",
287 opcache_global_hits,
288 (int) (100.0 * opcache_global_hits /
289 (opcache_global_hits + opcache_global_misses)));
290
291 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL misses = %zd (%d%%)\n",
292 opcache_global_misses,
293 (int) (100.0 * opcache_global_misses /
294 (opcache_global_hits + opcache_global_misses)));
295
296 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL opts = %zd\n",
297 opcache_global_opts);
298
299 fprintf(stderr, "\n");
300#endif
301}
302
303void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000304PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000305{
Victor Stinner09532fe2019-05-10 23:39:09 +0200306 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200307 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100308 ensure_tstate_not_null(__func__, tstate);
309
Victor Stinner85f5a692020-03-09 22:12:04 +0100310 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000311}
312
313void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000314PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000315{
Victor Stinner09532fe2019-05-10 23:39:09 +0200316 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200317 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinner50e6e992020-03-19 02:41:21 +0100318 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000319 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100320 We therefore avoid PyThreadState_Get() which dumps a fatal error
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 in debug mode.
322 */
Victor Stinner50e6e992020-03-19 02:41:21 +0100323 drop_gil(&runtime->ceval, ceval2, tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000324}
325
326void
Victor Stinner23ef89d2020-03-18 02:26:04 +0100327_PyEval_ReleaseLock(PyThreadState *tstate)
328{
329 struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
Victor Stinner50e6e992020-03-19 02:41:21 +0100330 struct _ceval_state *ceval2 = &tstate->interp->ceval;
331 drop_gil(ceval, ceval2, tstate);
Victor Stinner23ef89d2020-03-18 02:26:04 +0100332}
333
334void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000335PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000336{
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100337 ensure_tstate_not_null(__func__, tstate);
338
Victor Stinner85f5a692020-03-09 22:12:04 +0100339 take_gil(tstate);
Victor Stinnere225beb2019-06-03 18:14:24 +0200340
Victor Stinner85f5a692020-03-09 22:12:04 +0100341 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
342 if (_PyThreadState_Swap(gilstate, tstate) != NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100343 Py_FatalError("non-NULL old thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200344 }
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000345}
346
347void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000348PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000349{
Victor Stinner17c68b82020-01-30 12:20:48 +0100350 assert(tstate != NULL);
Victor Stinner09532fe2019-05-10 23:39:09 +0200351
Victor Stinner01b1cc12019-11-20 02:27:56 +0100352 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200353 PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
354 if (new_tstate != tstate) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100355 Py_FatalError("wrong thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200356 }
Victor Stinner50e6e992020-03-19 02:41:21 +0100357 struct _ceval_state *ceval2 = &tstate->interp->ceval;
358 drop_gil(&runtime->ceval, ceval2, tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000359}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000360
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200361/* This function is called from PyOS_AfterFork_Child to destroy all threads
362 * which are not running in the child process, and clear internal locks
363 * which might be held by those threads.
364 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000365
366void
Victor Stinnerd5d9e812019-05-13 12:35:37 +0200367_PyEval_ReInitThreads(_PyRuntimeState *runtime)
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000368{
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100369 struct _gil_runtime_state *gil = &runtime->ceval.gil;
370 if (!gil_created(gil)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 return;
Victor Stinner09532fe2019-05-10 23:39:09 +0200372 }
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100373 recreate_gil(gil);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100374 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
375 ensure_tstate_not_null(__func__, tstate);
Victor Stinner85f5a692020-03-09 22:12:04 +0100376
377 take_gil(tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700378
Victor Stinner50e6e992020-03-19 02:41:21 +0100379 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200380 pending->lock = PyThread_allocate_lock();
381 if (pending->lock == NULL) {
Eric Snow8479a342019-03-08 23:44:33 -0700382 Py_FatalError("Can't initialize threads for pending calls");
383 }
Jesse Nollera8513972008-07-17 16:49:17 +0000384
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200385 /* Destroy all threads except the current one */
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100386 _PyThreadState_DeleteExcept(runtime, tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000387}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000388
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000389/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600390 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000391
392void
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100393_PyEval_SignalAsyncExc(PyThreadState *tstate)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000394{
Victor Stinner50e6e992020-03-19 02:41:21 +0100395 struct _ceval_state *ceval2 = &tstate->interp->ceval;
396 SIGNAL_ASYNC_EXC(ceval2);
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000397}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000398
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000399PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000400PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000401{
Victor Stinner09532fe2019-05-10 23:39:09 +0200402 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200403 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinner09532fe2019-05-10 23:39:09 +0200404 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
405 if (tstate == NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100406 Py_FatalError("NULL tstate");
Victor Stinner09532fe2019-05-10 23:39:09 +0200407 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200408 assert(gil_created(&ceval->gil));
Victor Stinner50e6e992020-03-19 02:41:21 +0100409 struct _ceval_state *ceval2 = &tstate->interp->ceval;
410 drop_gil(ceval, ceval2, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000412}
413
414void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000415PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000416{
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100417 ensure_tstate_not_null(__func__, tstate);
418
Victor Stinner85f5a692020-03-09 22:12:04 +0100419 take_gil(tstate);
Victor Stinner17c68b82020-01-30 12:20:48 +0100420
Victor Stinner85f5a692020-03-09 22:12:04 +0100421 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
422 _PyThreadState_Swap(gilstate, tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000423}
424
425
Guido van Rossuma9672091994-09-14 13:31:22 +0000426/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
427 signal handlers or Mac I/O completion routines) can schedule calls
428 to a function to be called synchronously.
429 The synchronous function is called with one void* argument.
430 It should return 0 for success or -1 for failure -- failure should
431 be accompanied by an exception.
432
433 If registry succeeds, the registry function returns 0; if it fails
434 (e.g. due to too many pending calls) it returns -1 (without setting
435 an exception condition).
436
437 Note that because registry may occur from within signal handlers,
438 or other asynchronous events, calling malloc() is unsafe!
439
Guido van Rossuma9672091994-09-14 13:31:22 +0000440 Any thread can schedule pending calls, but only the main thread
441 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000442 There is no facility to schedule calls to a particular thread, but
443 that should be easy to change, should that ever be required. In
444 that case, the static variables here should go into the python
445 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000446*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000447
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200448void
Victor Stinner8849e592020-03-18 19:28:53 +0100449_PyEval_SignalReceived(PyThreadState *tstate)
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200450{
Victor Stinner8849e592020-03-18 19:28:53 +0100451 struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
Victor Stinner50e6e992020-03-19 02:41:21 +0100452 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200453 /* bpo-30703: Function called when the C signal handler of Python gets a
Victor Stinner50e6e992020-03-19 02:41:21 +0100454 signal. We cannot queue a callback using _PyEval_AddPendingCall() since
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200455 that function is not async-signal-safe. */
Victor Stinner50e6e992020-03-19 02:41:21 +0100456 SIGNAL_PENDING_SIGNALS(ceval, ceval2);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200457}
458
Eric Snow5be45a62019-03-08 22:47:07 -0700459/* Push one item onto the queue while holding the lock. */
460static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200461_push_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600462 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700463{
Eric Snow842a2f02019-03-15 15:47:51 -0600464 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700465 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600466 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700467 return -1; /* Queue full */
468 }
Eric Snow842a2f02019-03-15 15:47:51 -0600469 pending->calls[i].func = func;
470 pending->calls[i].arg = arg;
471 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700472 return 0;
473}
474
475/* Pop one item off the queue while holding the lock. */
476static void
Victor Stinnere225beb2019-06-03 18:14:24 +0200477_pop_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600478 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700479{
Eric Snow842a2f02019-03-15 15:47:51 -0600480 int i = pending->first;
481 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700482 return; /* Queue empty */
483 }
484
Eric Snow842a2f02019-03-15 15:47:51 -0600485 *func = pending->calls[i].func;
486 *arg = pending->calls[i].arg;
487 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700488}
489
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200490/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000491 scheduling to be made from any thread, and even from an executing
492 callback.
493 */
494
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000495int
Victor Stinner438a12d2019-05-24 17:01:38 +0200496_PyEval_AddPendingCall(PyThreadState *tstate,
Victor Stinner09532fe2019-05-10 23:39:09 +0200497 int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000498{
Victor Stinner50e6e992020-03-19 02:41:21 +0100499 struct _ceval_state *ceval2 = &tstate->interp->ceval;
500 struct _pending_calls *pending = &ceval2->pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600501
502 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
503 if (pending->finishing) {
504 PyThread_release_lock(pending->lock);
505
506 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +0200507 _PyErr_Fetch(tstate, &exc, &val, &tb);
508 _PyErr_SetString(tstate, PyExc_SystemError,
Victor Stinner50e6e992020-03-19 02:41:21 +0100509 "Py_AddPendingCall: cannot add pending calls "
510 "(Python shutting down)");
Victor Stinner438a12d2019-05-24 17:01:38 +0200511 _PyErr_Print(tstate);
512 _PyErr_Restore(tstate, exc, val, tb);
Eric Snow842a2f02019-03-15 15:47:51 -0600513 return -1;
514 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200515 int result = _push_pending_call(pending, func, arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600516 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700517
Victor Stinnere225beb2019-06-03 18:14:24 +0200518 /* signal main loop */
Victor Stinner50e6e992020-03-19 02:41:21 +0100519 SIGNAL_PENDING_CALLS(ceval2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000521}
522
Victor Stinner09532fe2019-05-10 23:39:09 +0200523int
524Py_AddPendingCall(int (*func)(void *), void *arg)
525{
Victor Stinner50e6e992020-03-19 02:41:21 +0100526 /* Best-effort to support subinterpreters and calls with the GIL released.
527
528 First attempt _PyThreadState_GET() since it supports subinterpreters.
529
530 If the GIL is released, _PyThreadState_GET() returns NULL . In this
531 case, use PyGILState_GetThisThreadState() which works even if the GIL
532 is released.
533
534 Sadly, PyGILState_GetThisThreadState() doesn't support subinterpreters:
535 see bpo-10915 and bpo-15751.
536
Victor Stinner8849e592020-03-18 19:28:53 +0100537 Py_AddPendingCall() doesn't require the caller to hold the GIL. */
Victor Stinner50e6e992020-03-19 02:41:21 +0100538 PyThreadState *tstate = _PyThreadState_GET();
539 if (tstate == NULL) {
540 tstate = PyGILState_GetThisThreadState();
541 }
542 /* tstate can be NULL if Py_AddPendingCall() is called in a thread
543 which is no Python thread state. Fail with a fatal error in this
544 case. */
545 ensure_tstate_not_null(__func__, tstate);
Victor Stinner8849e592020-03-18 19:28:53 +0100546 return _PyEval_AddPendingCall(tstate, func, arg);
Victor Stinner09532fe2019-05-10 23:39:09 +0200547}
548
Eric Snowfdf282d2019-01-11 14:26:55 -0700549static int
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100550handle_signals(PyThreadState *tstate)
Eric Snowfdf282d2019-01-11 14:26:55 -0700551{
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100552 _PyRuntimeState *runtime = tstate->interp->runtime;
553
Victor Stinner5a3a71d2020-03-19 17:40:12 +0100554 if (!thread_can_handle_signals()) {
Eric Snowfdf282d2019-01-11 14:26:55 -0700555 return 0;
556 }
Eric Snow64d6cc82019-02-23 15:40:43 -0700557 /*
558 * Ensure that the thread isn't currently running some other
559 * interpreter.
560 */
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100561 PyInterpreterState *interp = tstate->interp;
Victor Stinner09532fe2019-05-10 23:39:09 +0200562 if (interp != runtime->interpreters.main) {
Eric Snow64d6cc82019-02-23 15:40:43 -0700563 return 0;
564 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700565
Victor Stinnere225beb2019-06-03 18:14:24 +0200566 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinner50e6e992020-03-19 02:41:21 +0100567 struct _ceval_state *ceval2 = &interp->ceval;
568 UNSIGNAL_PENDING_SIGNALS(ceval, ceval2);
Eric Snow64d6cc82019-02-23 15:40:43 -0700569 if (_PyErr_CheckSignals() < 0) {
Victor Stinner50e6e992020-03-19 02:41:21 +0100570 SIGNAL_PENDING_SIGNALS(ceval, ceval2); /* We're not done yet */
Eric Snowfdf282d2019-01-11 14:26:55 -0700571 return -1;
572 }
573 return 0;
574}
575
576static int
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100577make_pending_calls(PyThreadState *tstate)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000578{
Eric Snow6a150bc2019-06-01 15:39:46 -0600579 static int busy = 0;
Eric Snowb75b1a352019-04-12 10:20:10 -0600580
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100581 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner50e6e992020-03-19 02:41:21 +0100582 struct _ceval_state * ceval2 = &tstate->interp->ceval;
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100583
Victor Stinnere225beb2019-06-03 18:14:24 +0200584 /* only service pending calls on main thread */
585 if (PyThread_get_thread_ident() != runtime->main_thread) {
586 return 0;
587 }
588
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 /* don't perform recursive pending calls */
Eric Snowfdf282d2019-01-11 14:26:55 -0700590 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700592 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200593 busy = 1;
Victor Stinnere225beb2019-06-03 18:14:24 +0200594 struct _ceval_runtime_state *ceval = &runtime->ceval;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200595 /* unsignal before starting to call callbacks, so that any callback
596 added in-between re-signals */
Victor Stinner50e6e992020-03-19 02:41:21 +0100597 UNSIGNAL_PENDING_CALLS(ceval, ceval2);
Eric Snowfdf282d2019-01-11 14:26:55 -0700598 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200599
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 /* perform a bounded number of calls, in case of recursion */
Victor Stinner50e6e992020-03-19 02:41:21 +0100601 struct _pending_calls *pending = &ceval2->pending;
Eric Snowfdf282d2019-01-11 14:26:55 -0700602 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700603 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 void *arg = NULL;
605
606 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600607 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200608 _pop_pending_call(pending, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600609 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700610
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100611 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700612 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100613 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700614 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700615 res = func(arg);
616 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200617 goto error;
618 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200620
Charles-François Natalif23339a2011-07-23 18:15:43 +0200621 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700622 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200623
624error:
625 busy = 0;
Victor Stinner50e6e992020-03-19 02:41:21 +0100626 SIGNAL_PENDING_CALLS(ceval2);
Eric Snowfdf282d2019-01-11 14:26:55 -0700627 return res;
628}
629
Eric Snow842a2f02019-03-15 15:47:51 -0600630void
Victor Stinner2b1df452020-01-13 18:46:59 +0100631_Py_FinishPendingCalls(PyThreadState *tstate)
Eric Snow842a2f02019-03-15 15:47:51 -0600632{
Eric Snow842a2f02019-03-15 15:47:51 -0600633 assert(PyGILState_Check());
634
Victor Stinner50e6e992020-03-19 02:41:21 +0100635 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200636
Eric Snow842a2f02019-03-15 15:47:51 -0600637 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
638 pending->finishing = 1;
639 PyThread_release_lock(pending->lock);
640
641 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
642 return;
643 }
644
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100645 if (make_pending_calls(tstate) < 0) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200646 PyObject *exc, *val, *tb;
647 _PyErr_Fetch(tstate, &exc, &val, &tb);
648 PyErr_BadInternalCall();
649 _PyErr_ChainExceptions(exc, val, tb);
650 _PyErr_Print(tstate);
Eric Snow842a2f02019-03-15 15:47:51 -0600651 }
652}
653
Eric Snowfdf282d2019-01-11 14:26:55 -0700654/* Py_MakePendingCalls() is a simple wrapper for the sake
655 of backward-compatibility. */
656int
657Py_MakePendingCalls(void)
658{
659 assert(PyGILState_Check());
660
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100661 PyThreadState *tstate = _PyThreadState_GET();
662
Eric Snowfdf282d2019-01-11 14:26:55 -0700663 /* Python signal handler doesn't really queue a callback: it only signals
664 that a signal was received, see _PyEval_SignalReceived(). */
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100665 int res = handle_signals(tstate);
Eric Snowfdf282d2019-01-11 14:26:55 -0700666 if (res != 0) {
667 return res;
668 }
669
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100670 res = make_pending_calls(tstate);
Eric Snowb75b1a352019-04-12 10:20:10 -0600671 if (res != 0) {
672 return res;
673 }
674
675 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000676}
677
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000678/* The interpreter's recursion limit */
679
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000680#ifndef Py_DEFAULT_RECURSION_LIMIT
681#define Py_DEFAULT_RECURSION_LIMIT 1000
682#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600683
Eric Snow05351c12017-09-05 21:43:08 -0700684int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000685
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600686void
Victor Stinnerdab84232020-03-17 18:56:44 +0100687_PyEval_InitRuntimeState(struct _ceval_runtime_state *ceval)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600688{
Victor Stinnerdab84232020-03-17 18:56:44 +0100689 ceval->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600690 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Victor Stinnerdab84232020-03-17 18:56:44 +0100691 _gil_initialize(&ceval->gil);
692}
693
694void
695_PyEval_InitState(struct _ceval_state *ceval)
696{
697 /* PyInterpreterState_New() initializes ceval to zero */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600698}
699
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000700int
701Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000702{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100703 struct _ceval_runtime_state *ceval = &_PyRuntime.ceval;
704 return ceval->recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000705}
706
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000707void
708Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000709{
Victor Stinnere225beb2019-06-03 18:14:24 +0200710 struct _ceval_runtime_state *ceval = &_PyRuntime.ceval;
711 ceval->recursion_limit = new_limit;
Victor Stinnerdab84232020-03-17 18:56:44 +0100712 _Py_CheckRecursionLimit = new_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000713}
714
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100715/* The function _Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
Armin Rigo2b3eb402003-10-28 12:05:48 +0000716 if the recursion_depth reaches _Py_CheckRecursionLimit.
717 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
718 to guarantee that _Py_CheckRecursiveCall() is regularly called.
719 Without USE_STACKCHECK, there is no need for this. */
720int
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100721_Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000722{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100723 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200724 int recursion_limit = runtime->ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000725
726#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700727 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 if (PyOS_CheckStack()) {
729 --tstate->recursion_depth;
Victor Stinner438a12d2019-05-24 17:01:38 +0200730 _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 return -1;
732 }
pdox18967932017-10-25 23:03:01 -0700733 /* Needed for ABI backwards-compatibility (see bpo-31857) */
Eric Snow05351c12017-09-05 21:43:08 -0700734 _Py_CheckRecursionLimit = recursion_limit;
pdox18967932017-10-25 23:03:01 -0700735#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 if (tstate->recursion_critical)
737 /* Somebody asked that we don't check for recursion. */
738 return 0;
739 if (tstate->overflowed) {
740 if (tstate->recursion_depth > recursion_limit + 50) {
741 /* Overflowing while handling an overflow. Give up. */
742 Py_FatalError("Cannot recover from stack overflow.");
743 }
744 return 0;
745 }
746 if (tstate->recursion_depth > recursion_limit) {
747 --tstate->recursion_depth;
748 tstate->overflowed = 1;
Victor Stinner438a12d2019-05-24 17:01:38 +0200749 _PyErr_Format(tstate, PyExc_RecursionError,
750 "maximum recursion depth exceeded%s",
751 where);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 return -1;
753 }
754 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000755}
756
Victor Stinner09532fe2019-05-10 23:39:09 +0200757static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
Victor Stinner438a12d2019-05-24 17:01:38 +0200758static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000759
Victor Stinnere225beb2019-06-03 18:14:24 +0200760#define _Py_TracingPossible(ceval) ((ceval)->tracing_possible)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000761
Guido van Rossum374a9221991-04-04 10:40:29 +0000762
Guido van Rossumb209a111997-04-29 18:18:01 +0000763PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000764PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000765{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 return PyEval_EvalCodeEx(co,
767 globals, locals,
768 (PyObject **)NULL, 0,
769 (PyObject **)NULL, 0,
770 (PyObject **)NULL, 0,
771 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000772}
773
774
775/* Interpreter main loop */
776
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000777PyObject *
Victor Stinnerb9e68122019-11-14 12:20:46 +0100778PyEval_EvalFrame(PyFrameObject *f)
779{
Victor Stinner0b72b232020-03-12 23:18:39 +0100780 /* Function kept for backward compatibility */
Victor Stinnerb9e68122019-11-14 12:20:46 +0100781 PyThreadState *tstate = _PyThreadState_GET();
782 return _PyEval_EvalFrame(tstate, f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000783}
784
785PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000786PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000787{
Victor Stinnerb9e68122019-11-14 12:20:46 +0100788 PyThreadState *tstate = _PyThreadState_GET();
789 return _PyEval_EvalFrame(tstate, f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700790}
791
Victor Stinnerc6944e72016-11-11 02:13:35 +0100792PyObject* _Py_HOT_FUNCTION
Victor Stinner0b72b232020-03-12 23:18:39 +0100793_PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
Brett Cannon3cebf932016-09-05 15:33:46 -0700794{
Victor Stinner0b72b232020-03-12 23:18:39 +0100795 ensure_tstate_not_null(__func__, tstate);
796
Guido van Rossum950361c1997-01-24 13:49:28 +0000797#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000799#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200800 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300801 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200802 int opcode; /* Current opcode */
803 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200804 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 PyObject *retval = NULL; /* Return value */
Victor Stinner09532fe2019-05-10 23:39:09 +0200806 _PyRuntimeState * const runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200807 struct _ceval_runtime_state * const ceval = &runtime->ceval;
Victor Stinnerdab84232020-03-17 18:56:44 +0100808 struct _ceval_state * const ceval2 = &tstate->interp->ceval;
Victor Stinner50e6e992020-03-19 02:41:21 +0100809 _Py_atomic_int * const eval_breaker = &ceval2->eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000811
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 is true when the line being executed has changed. The
817 initial values are such as to make this false the first
818 time it is tested. */
819 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000820
Serhiy Storchakaab874002016-09-11 13:48:15 +0300821 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 PyObject *names;
823 PyObject *consts;
Inada Naoki91234a12019-06-03 21:30:58 +0900824 _PyOpcache *co_opcache;
Guido van Rossum374a9221991-04-04 10:40:29 +0000825
Brett Cannon368b4b72012-04-02 12:17:59 -0400826#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200827 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400828#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200829
Antoine Pitroub52ec782009-01-25 16:34:23 +0000830/* Computed GOTOs, or
831 the-optimization-commonly-but-improperly-known-as-"threaded code"
832 using gcc's labels-as-values extension
833 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
834
835 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000837 combined with a lookup table of jump addresses. However, since the
838 indirect jump instruction is shared by all opcodes, the CPU will have a
839 hard time making the right prediction for where to jump next (actually,
840 it will be always wrong except in the uncommon case of a sequence of
841 several identical opcodes).
842
843 "Threaded code" in contrast, uses an explicit jump table and an explicit
844 indirect jump instruction at the end of each opcode. Since the jump
845 instruction is at a different address for each opcode, the CPU will make a
846 separate prediction for each of these instructions, which is equivalent to
847 predicting the second opcode of each opcode pair. These predictions have
848 a much better chance to turn out valid, especially in small bytecode loops.
849
850 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000852 and potentially many more instructions (depending on the pipeline width).
853 A correctly predicted branch, however, is nearly free.
854
855 At the time of this writing, the "threaded code" version is up to 15-20%
856 faster than the normal "switch" version, depending on the compiler and the
857 CPU architecture.
858
859 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
860 because it would render the measurements invalid.
861
862
863 NOTE: care must be taken that the compiler doesn't try to "optimize" the
864 indirect jumps by sharing them between all opcodes. Such optimizations
865 can be disabled on gcc by using the -fno-gcse flag (or possibly
866 -fno-crossjumping).
867*/
868
Antoine Pitrou042b1282010-08-13 21:15:58 +0000869#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000870#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000871#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000872#endif
873
Antoine Pitrou042b1282010-08-13 21:15:58 +0000874#ifdef HAVE_COMPUTED_GOTOS
875 #ifndef USE_COMPUTED_GOTOS
876 #define USE_COMPUTED_GOTOS 1
877 #endif
878#else
879 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
880 #error "Computed gotos are not supported on this compiler."
881 #endif
882 #undef USE_COMPUTED_GOTOS
883 #define USE_COMPUTED_GOTOS 0
884#endif
885
886#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000887/* Import the static jump table */
888#include "opcode_targets.h"
889
Antoine Pitroub52ec782009-01-25 16:34:23 +0000890#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -0700891 op: \
892 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000893
Antoine Pitroub52ec782009-01-25 16:34:23 +0000894#ifdef LLTRACE
895#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 { \
Victor Stinnerdab84232020-03-17 18:56:44 +0100897 if (!lltrace && !_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300899 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300900 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 } \
902 goto fast_next_opcode; \
903 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000904#else
905#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 { \
Victor Stinnerdab84232020-03-17 18:56:44 +0100907 if (!_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300909 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300910 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 } \
912 goto fast_next_opcode; \
913 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000914#endif
915
Victor Stinner09532fe2019-05-10 23:39:09 +0200916#define DISPATCH() \
917 { \
918 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
919 FAST_DISPATCH(); \
920 } \
921 continue; \
922 }
923
Antoine Pitroub52ec782009-01-25 16:34:23 +0000924#else
Benjamin Petersonddd19492018-09-16 22:38:02 -0700925#define TARGET(op) op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000926#define FAST_DISPATCH() goto fast_next_opcode
Victor Stinner09532fe2019-05-10 23:39:09 +0200927#define DISPATCH() continue
Antoine Pitroub52ec782009-01-25 16:34:23 +0000928#endif
929
930
Neal Norwitza81d2202002-07-14 00:27:26 +0000931/* Tuple access macros */
932
933#ifndef Py_DEBUG
934#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
935#else
936#define GETITEM(v, i) PyTuple_GetItem((v), (i))
937#endif
938
Guido van Rossum374a9221991-04-04 10:40:29 +0000939/* Code access macros */
940
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300941/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600942#define INSTR_OFFSET() \
943 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300944#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300945 _Py_CODEUNIT word = *next_instr; \
946 opcode = _Py_OPCODE(word); \
947 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300948 next_instr++; \
949 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300950#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
951#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000952
Raymond Hettingerf606f872003-03-16 03:11:04 +0000953/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 Some opcodes tend to come in pairs thus making it possible to
955 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300956 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 Verifying the prediction costs a single high-speed test of a register
959 variable against a constant. If the pairing was good, then the
960 processor's own internal branch predication has a high likelihood of
961 success, resulting in a nearly zero-overhead transition to the
962 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300963 including its unpredictable switch-case branch. Combined with the
964 processor's internal branch prediction, a successful PREDICT has the
965 effect of making the two opcodes run as if they were a single new opcode
966 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000967
Georg Brandl86b2fb92008-07-16 03:43:04 +0000968 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 predictions turned-on and interpret the results as if some opcodes
970 had been combined or turn-off predictions so that the opcode frequency
971 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000972
973 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 the CPU to record separate branch prediction information for each
975 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000976
Raymond Hettingerf606f872003-03-16 03:11:04 +0000977*/
978
Denis Chernikovbaf29b22020-02-21 12:17:50 +0300979#define PREDICT_ID(op) PRED_##op
980
Antoine Pitrou042b1282010-08-13 21:15:58 +0000981#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Denis Chernikovbaf29b22020-02-21 12:17:50 +0300982#define PREDICT(op) if (0) goto PREDICT_ID(op)
Raymond Hettingera7216982004-02-08 19:59:27 +0000983#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300984#define PREDICT(op) \
Denis Chernikovbaf29b22020-02-21 12:17:50 +0300985 do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300986 _Py_CODEUNIT word = *next_instr; \
987 opcode = _Py_OPCODE(word); \
Denis Chernikovbaf29b22020-02-21 12:17:50 +0300988 if (opcode == op) { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300989 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300990 next_instr++; \
Denis Chernikovbaf29b22020-02-21 12:17:50 +0300991 goto PREDICT_ID(op); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300992 } \
993 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000994#endif
Denis Chernikovbaf29b22020-02-21 12:17:50 +0300995#define PREDICTED(op) PREDICT_ID(op):
Antoine Pitroub52ec782009-01-25 16:34:23 +0000996
Raymond Hettingerf606f872003-03-16 03:11:04 +0000997
Guido van Rossum374a9221991-04-04 10:40:29 +0000998/* Stack manipulation macros */
999
Martin v. Löwis18e16552006-02-15 17:27:45 +00001000/* The stack can grow at most MAXINT deep, as co_nlocals and
1001 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001002#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1003#define EMPTY() (STACK_LEVEL() == 0)
1004#define TOP() (stack_pointer[-1])
1005#define SECOND() (stack_pointer[-2])
1006#define THIRD() (stack_pointer[-3])
1007#define FOURTH() (stack_pointer[-4])
1008#define PEEK(n) (stack_pointer[-(n)])
1009#define SET_TOP(v) (stack_pointer[-1] = (v))
1010#define SET_SECOND(v) (stack_pointer[-2] = (v))
1011#define SET_THIRD(v) (stack_pointer[-3] = (v))
1012#define SET_FOURTH(v) (stack_pointer[-4] = (v))
1013#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
1014#define BASIC_STACKADJ(n) (stack_pointer += n)
1015#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1016#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001017
Guido van Rossum96a42c81992-01-12 02:29:51 +00001018#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001020 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001021 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +02001022#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001023 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +00001024#define STACK_GROW(n) do { \
1025 assert(n >= 0); \
1026 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001027 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001028 assert(STACK_LEVEL() <= co->co_stacksize); \
1029 } while (0)
1030#define STACK_SHRINK(n) do { \
1031 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +02001032 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001033 (void)(BASIC_STACKADJ(-n)); \
1034 assert(STACK_LEVEL() <= co->co_stacksize); \
1035 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +00001036#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +02001037 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001038 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001039#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001040#define PUSH(v) BASIC_PUSH(v)
1041#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +00001042#define STACK_GROW(n) BASIC_STACKADJ(n)
1043#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001044#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001045#endif
1046
Guido van Rossum681d79a1995-07-18 14:51:37 +00001047/* Local variable macros */
1048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001050
1051/* The SETLOCAL() macro must not DECREF the local variable in-place and
1052 then store the new value; it must copy the old value to a temporary
1053 value, then store the new value, and then DECREF the temporary value.
1054 This is because it is possible that during the DECREF the frame is
1055 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1056 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001058 GETLOCAL(i) = value; \
1059 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001060
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001061
1062#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 while (STACK_LEVEL() > (b)->b_level) { \
1064 PyObject *v = POP(); \
1065 Py_XDECREF(v); \
1066 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001067
1068#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001069 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +01001071 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1073 while (STACK_LEVEL() > (b)->b_level + 3) { \
1074 value = POP(); \
1075 Py_XDECREF(value); \
1076 } \
Mark Shannonae3087c2017-10-22 22:41:51 +01001077 exc_info = tstate->exc_info; \
1078 type = exc_info->exc_type; \
1079 value = exc_info->exc_value; \
1080 traceback = exc_info->exc_traceback; \
1081 exc_info->exc_type = POP(); \
1082 exc_info->exc_value = POP(); \
1083 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 Py_XDECREF(type); \
1085 Py_XDECREF(value); \
1086 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001087 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001088
Inada Naoki91234a12019-06-03 21:30:58 +09001089 /* macros for opcode cache */
1090#define OPCACHE_CHECK() \
1091 do { \
1092 co_opcache = NULL; \
1093 if (co->co_opcache != NULL) { \
1094 unsigned char co_opt_offset = \
1095 co->co_opcache_map[next_instr - first_instr]; \
1096 if (co_opt_offset > 0) { \
1097 assert(co_opt_offset <= co->co_opcache_size); \
1098 co_opcache = &co->co_opcache[co_opt_offset - 1]; \
1099 assert(co_opcache != NULL); \
Inada Naoki91234a12019-06-03 21:30:58 +09001100 } \
1101 } \
1102 } while (0)
1103
1104#if OPCACHE_STATS
1105
1106#define OPCACHE_STAT_GLOBAL_HIT() \
1107 do { \
1108 if (co->co_opcache != NULL) opcache_global_hits++; \
1109 } while (0)
1110
1111#define OPCACHE_STAT_GLOBAL_MISS() \
1112 do { \
1113 if (co->co_opcache != NULL) opcache_global_misses++; \
1114 } while (0)
1115
1116#define OPCACHE_STAT_GLOBAL_OPT() \
1117 do { \
1118 if (co->co_opcache != NULL) opcache_global_opts++; \
1119 } while (0)
1120
1121#else /* OPCACHE_STATS */
1122
1123#define OPCACHE_STAT_GLOBAL_HIT()
1124#define OPCACHE_STAT_GLOBAL_MISS()
1125#define OPCACHE_STAT_GLOBAL_OPT()
1126
1127#endif
1128
Guido van Rossuma027efa1997-05-05 20:56:21 +00001129/* Start of code */
1130
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 /* push frame */
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001132 if (_Py_EnterRecursiveCall(tstate, "")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001134 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001135
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001136 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001137
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 if (tstate->use_tracing) {
1139 if (tstate->c_tracefunc != NULL) {
1140 /* tstate->c_tracefunc, if defined, is a
1141 function that will be called on *every* entry
1142 to a code block. Its return value, if not
1143 None, is a function that will be called at
1144 the start of each executed line of code.
1145 (Actually, the function must return itself
1146 in order to continue tracing.) The trace
1147 functions are called with three arguments:
1148 a pointer to the current frame, a string
1149 indicating why the function is called, and
1150 an argument which depends on the situation.
1151 The global trace function is also called
1152 whenever an exception is detected. */
1153 if (call_trace_protected(tstate->c_tracefunc,
1154 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001155 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 /* Trace function raised an error */
1157 goto exit_eval_frame;
1158 }
1159 }
1160 if (tstate->c_profilefunc != NULL) {
1161 /* Similar for c_profilefunc, except it needn't
1162 return itself and isn't called for "line" events */
1163 if (call_trace_protected(tstate->c_profilefunc,
1164 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001165 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 /* Profile function raised an error */
1167 goto exit_eval_frame;
1168 }
1169 }
1170 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001171
Łukasz Langaa785c872016-09-09 17:37:37 -07001172 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1173 dtrace_function_entry(f);
1174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 co = f->f_code;
1176 names = co->co_names;
1177 consts = co->co_consts;
1178 fastlocals = f->f_localsplus;
1179 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001180 assert(PyBytes_Check(co->co_code));
1181 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001182 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1183 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1184 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001185 /*
1186 f->f_lasti refers to the index of the last instruction,
1187 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001188
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001189 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001190 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 When the PREDICT() macros are enabled, some opcode pairs follow in
1193 direct succession without updating f->f_lasti. A successful
1194 prediction effectively links the two codes together as if they
1195 were a single new opcode; accordingly,f->f_lasti will point to
1196 the first code in the pair (for instance, GET_ITER followed by
1197 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001198 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001200 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001201 next_instr = first_instr;
1202 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001203 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1204 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001205 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 stack_pointer = f->f_stacktop;
1207 assert(stack_pointer != NULL);
1208 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001209 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001210
Inada Naoki91234a12019-06-03 21:30:58 +09001211 if (co->co_opcache_flag < OPCACHE_MIN_RUNS) {
1212 co->co_opcache_flag++;
1213 if (co->co_opcache_flag == OPCACHE_MIN_RUNS) {
1214 if (_PyCode_InitOpcache(co) < 0) {
1215 return NULL;
1216 }
1217#if OPCACHE_STATS
1218 opcache_code_objects_extra_mem +=
1219 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1220 sizeof(_PyOpcache) * co->co_opcache_size;
1221 opcache_code_objects++;
1222#endif
1223 }
1224 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001225
Tim Peters5ca576e2001-06-18 22:08:13 +00001226#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001227 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001228#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001229
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001230 if (throwflag) /* support for generator.throw() */
1231 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001232
Victor Stinnerace47d72013-07-18 01:41:08 +02001233#ifdef Py_DEBUG
Victor Stinner0b72b232020-03-12 23:18:39 +01001234 /* _PyEval_EvalFrameDefault() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001235 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001236 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001237 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001238#endif
1239
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001240main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1243 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001244 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001245
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001246 /* Do periodic things. Doing this every time through
1247 the loop would add too much overhead, so we do it
1248 only every Nth instruction. We also do it if
1249 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1250 event needs attention (e.g. a signal handler or
1251 async I/O handler); see Py_AddPendingCall() and
1252 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001253
Eric Snow7bda9de2019-03-08 17:25:54 -07001254 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001255 opcode = _Py_OPCODE(*next_instr);
1256 if (opcode == SETUP_FINALLY ||
1257 opcode == SETUP_WITH ||
1258 opcode == BEFORE_ASYNC_WITH ||
1259 opcode == YIELD_FROM) {
1260 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001261 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001262 - If we're about to enter the 'with:'. It will prevent
1263 emitting a resource warning in the common idiom
1264 'with open(path) as file:'.
1265 - If we're about to enter the 'async with:'.
1266 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001267 *very* useful, but might help in some cases and it's
1268 traditional)
1269 - If we're resuming a chain of nested 'yield from' or
1270 'await' calls, then each frame is parked with YIELD_FROM
1271 as its next opcode. If the user hit control-C we want to
1272 wait until we've reached the innermost frame before
1273 running the signal handler and raising KeyboardInterrupt
1274 (see bpo-30039).
1275 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 goto fast_next_opcode;
1277 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001278
Victor Stinnere225beb2019-06-03 18:14:24 +02001279 if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
Victor Stinnerd7fabc12020-03-18 01:56:21 +01001280 if (handle_signals(tstate) != 0) {
Eric Snowfdf282d2019-01-11 14:26:55 -07001281 goto error;
1282 }
1283 }
Victor Stinner50e6e992020-03-19 02:41:21 +01001284 if (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)) {
Victor Stinnerd7fabc12020-03-18 01:56:21 +01001285 if (make_pending_calls(tstate) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001286 goto error;
Eric Snowfdf282d2019-01-11 14:26:55 -07001287 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001289
Victor Stinnere225beb2019-06-03 18:14:24 +02001290 if (_Py_atomic_load_relaxed(&ceval->gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 /* Give another thread a chance */
Victor Stinner09532fe2019-05-10 23:39:09 +02001292 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +01001293 Py_FatalError("tstate mix-up");
Victor Stinner09532fe2019-05-10 23:39:09 +02001294 }
Victor Stinner50e6e992020-03-19 02:41:21 +01001295 drop_gil(ceval, ceval2, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296
1297 /* Other threads may run now */
1298
Victor Stinner85f5a692020-03-09 22:12:04 +01001299 take_gil(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +01001300
Victor Stinner09532fe2019-05-10 23:39:09 +02001301 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +01001302 Py_FatalError("orphan tstate");
Victor Stinner09532fe2019-05-10 23:39:09 +02001303 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 }
1305 /* Check for asynchronous exceptions. */
1306 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001307 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 tstate->async_exc = NULL;
Victor Stinner50e6e992020-03-19 02:41:21 +01001309 UNSIGNAL_ASYNC_EXC(ceval, ceval2);
Victor Stinner438a12d2019-05-24 17:01:38 +02001310 _PyErr_SetNone(tstate, exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001311 Py_DECREF(exc);
1312 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 }
1314 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 fast_next_opcode:
1317 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001318
Łukasz Langaa785c872016-09-09 17:37:37 -07001319 if (PyDTrace_LINE_ENABLED())
1320 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001323
Victor Stinnerdab84232020-03-17 18:56:44 +01001324 if (_Py_TracingPossible(ceval2) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001325 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001326 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 /* see maybe_call_line_trace
1328 for expository comments */
1329 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001331 err = maybe_call_line_trace(tstate->c_tracefunc,
1332 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001333 tstate, f,
1334 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 /* Reload possibly changed frame fields */
1336 JUMPTO(f->f_lasti);
1337 if (f->f_stacktop != NULL) {
1338 stack_pointer = f->f_stacktop;
1339 f->f_stacktop = NULL;
1340 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001341 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001343 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001347
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001348 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001349 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001350#ifdef DYNAMIC_EXECUTION_PROFILE
1351#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 dxpairs[lastopcode][opcode]++;
1353 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001354#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001356#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001357
Guido van Rossum96a42c81992-01-12 02:29:51 +00001358#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001359 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001361 if (lltrace) {
1362 if (HAS_ARG(opcode)) {
1363 printf("%d: %d, %d\n",
1364 f->f_lasti, opcode, oparg);
1365 }
1366 else {
1367 printf("%d: %d\n",
1368 f->f_lasti, opcode);
1369 }
1370 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001371#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001376 It is essential that any operation that fails must goto error
1377 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001378
Benjamin Petersonddd19492018-09-16 22:38:02 -07001379 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001381 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001382
Benjamin Petersonddd19492018-09-16 22:38:02 -07001383 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001384 PyObject *value = GETLOCAL(oparg);
1385 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001386 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001387 UNBOUNDLOCAL_ERROR_MSG,
1388 PyTuple_GetItem(co->co_varnames, oparg));
1389 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001391 Py_INCREF(value);
1392 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001394 }
1395
Benjamin Petersonddd19492018-09-16 22:38:02 -07001396 case TARGET(LOAD_CONST): {
1397 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001398 PyObject *value = GETITEM(consts, oparg);
1399 Py_INCREF(value);
1400 PUSH(value);
1401 FAST_DISPATCH();
1402 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001403
Benjamin Petersonddd19492018-09-16 22:38:02 -07001404 case TARGET(STORE_FAST): {
1405 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001406 PyObject *value = POP();
1407 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001408 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001409 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001410
Benjamin Petersonddd19492018-09-16 22:38:02 -07001411 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001412 PyObject *value = POP();
1413 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001414 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001415 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001416
Benjamin Petersonddd19492018-09-16 22:38:02 -07001417 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001418 PyObject *top = TOP();
1419 PyObject *second = SECOND();
1420 SET_TOP(second);
1421 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001423 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001424
Benjamin Petersonddd19492018-09-16 22:38:02 -07001425 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001426 PyObject *top = TOP();
1427 PyObject *second = SECOND();
1428 PyObject *third = THIRD();
1429 SET_TOP(second);
1430 SET_SECOND(third);
1431 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001433 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001434
Benjamin Petersonddd19492018-09-16 22:38:02 -07001435 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001436 PyObject *top = TOP();
1437 PyObject *second = SECOND();
1438 PyObject *third = THIRD();
1439 PyObject *fourth = FOURTH();
1440 SET_TOP(second);
1441 SET_SECOND(third);
1442 SET_THIRD(fourth);
1443 SET_FOURTH(top);
1444 FAST_DISPATCH();
1445 }
1446
Benjamin Petersonddd19492018-09-16 22:38:02 -07001447 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001448 PyObject *top = TOP();
1449 Py_INCREF(top);
1450 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001451 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001452 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001453
Benjamin Petersonddd19492018-09-16 22:38:02 -07001454 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001455 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001456 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001457 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001458 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001459 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001460 SET_TOP(top);
1461 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001462 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001463 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001464
Benjamin Petersonddd19492018-09-16 22:38:02 -07001465 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001466 PyObject *value = TOP();
1467 PyObject *res = PyNumber_Positive(value);
1468 Py_DECREF(value);
1469 SET_TOP(res);
1470 if (res == NULL)
1471 goto error;
1472 DISPATCH();
1473 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001474
Benjamin Petersonddd19492018-09-16 22:38:02 -07001475 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001476 PyObject *value = TOP();
1477 PyObject *res = PyNumber_Negative(value);
1478 Py_DECREF(value);
1479 SET_TOP(res);
1480 if (res == NULL)
1481 goto error;
1482 DISPATCH();
1483 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001484
Benjamin Petersonddd19492018-09-16 22:38:02 -07001485 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001486 PyObject *value = TOP();
1487 int err = PyObject_IsTrue(value);
1488 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001489 if (err == 0) {
1490 Py_INCREF(Py_True);
1491 SET_TOP(Py_True);
1492 DISPATCH();
1493 }
1494 else if (err > 0) {
1495 Py_INCREF(Py_False);
1496 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 DISPATCH();
1498 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001499 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001500 goto error;
1501 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001502
Benjamin Petersonddd19492018-09-16 22:38:02 -07001503 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001504 PyObject *value = TOP();
1505 PyObject *res = PyNumber_Invert(value);
1506 Py_DECREF(value);
1507 SET_TOP(res);
1508 if (res == NULL)
1509 goto error;
1510 DISPATCH();
1511 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001512
Benjamin Petersonddd19492018-09-16 22:38:02 -07001513 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001514 PyObject *exp = POP();
1515 PyObject *base = TOP();
1516 PyObject *res = PyNumber_Power(base, exp, Py_None);
1517 Py_DECREF(base);
1518 Py_DECREF(exp);
1519 SET_TOP(res);
1520 if (res == NULL)
1521 goto error;
1522 DISPATCH();
1523 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001524
Benjamin Petersonddd19492018-09-16 22:38:02 -07001525 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001526 PyObject *right = POP();
1527 PyObject *left = TOP();
1528 PyObject *res = PyNumber_Multiply(left, right);
1529 Py_DECREF(left);
1530 Py_DECREF(right);
1531 SET_TOP(res);
1532 if (res == NULL)
1533 goto error;
1534 DISPATCH();
1535 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001536
Benjamin Petersonddd19492018-09-16 22:38:02 -07001537 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001538 PyObject *right = POP();
1539 PyObject *left = TOP();
1540 PyObject *res = PyNumber_MatrixMultiply(left, right);
1541 Py_DECREF(left);
1542 Py_DECREF(right);
1543 SET_TOP(res);
1544 if (res == NULL)
1545 goto error;
1546 DISPATCH();
1547 }
1548
Benjamin Petersonddd19492018-09-16 22:38:02 -07001549 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001550 PyObject *divisor = POP();
1551 PyObject *dividend = TOP();
1552 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1553 Py_DECREF(dividend);
1554 Py_DECREF(divisor);
1555 SET_TOP(quotient);
1556 if (quotient == NULL)
1557 goto error;
1558 DISPATCH();
1559 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001560
Benjamin Petersonddd19492018-09-16 22:38:02 -07001561 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001562 PyObject *divisor = POP();
1563 PyObject *dividend = TOP();
1564 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1565 Py_DECREF(dividend);
1566 Py_DECREF(divisor);
1567 SET_TOP(quotient);
1568 if (quotient == NULL)
1569 goto error;
1570 DISPATCH();
1571 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001572
Benjamin Petersonddd19492018-09-16 22:38:02 -07001573 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001574 PyObject *divisor = POP();
1575 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001576 PyObject *res;
1577 if (PyUnicode_CheckExact(dividend) && (
1578 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1579 // fast path; string formatting, but not if the RHS is a str subclass
1580 // (see issue28598)
1581 res = PyUnicode_Format(dividend, divisor);
1582 } else {
1583 res = PyNumber_Remainder(dividend, divisor);
1584 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001585 Py_DECREF(divisor);
1586 Py_DECREF(dividend);
1587 SET_TOP(res);
1588 if (res == NULL)
1589 goto error;
1590 DISPATCH();
1591 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001592
Benjamin Petersonddd19492018-09-16 22:38:02 -07001593 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001594 PyObject *right = POP();
1595 PyObject *left = TOP();
1596 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001597 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1598 CPython using bytecode, it is simply worthless.
1599 See http://bugs.python.org/issue21955 and
1600 http://bugs.python.org/issue10044 for the discussion. In short,
1601 no patch shown any impact on a realistic benchmark, only a minor
1602 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001603 if (PyUnicode_CheckExact(left) &&
1604 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001605 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001606 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001607 }
1608 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001609 sum = PyNumber_Add(left, right);
1610 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001611 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001612 Py_DECREF(right);
1613 SET_TOP(sum);
1614 if (sum == NULL)
1615 goto error;
1616 DISPATCH();
1617 }
1618
Benjamin Petersonddd19492018-09-16 22:38:02 -07001619 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001620 PyObject *right = POP();
1621 PyObject *left = TOP();
1622 PyObject *diff = PyNumber_Subtract(left, right);
1623 Py_DECREF(right);
1624 Py_DECREF(left);
1625 SET_TOP(diff);
1626 if (diff == NULL)
1627 goto error;
1628 DISPATCH();
1629 }
1630
Benjamin Petersonddd19492018-09-16 22:38:02 -07001631 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001632 PyObject *sub = POP();
1633 PyObject *container = TOP();
1634 PyObject *res = PyObject_GetItem(container, sub);
1635 Py_DECREF(container);
1636 Py_DECREF(sub);
1637 SET_TOP(res);
1638 if (res == NULL)
1639 goto error;
1640 DISPATCH();
1641 }
1642
Benjamin Petersonddd19492018-09-16 22:38:02 -07001643 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001644 PyObject *right = POP();
1645 PyObject *left = TOP();
1646 PyObject *res = PyNumber_Lshift(left, right);
1647 Py_DECREF(left);
1648 Py_DECREF(right);
1649 SET_TOP(res);
1650 if (res == NULL)
1651 goto error;
1652 DISPATCH();
1653 }
1654
Benjamin Petersonddd19492018-09-16 22:38:02 -07001655 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001656 PyObject *right = POP();
1657 PyObject *left = TOP();
1658 PyObject *res = PyNumber_Rshift(left, right);
1659 Py_DECREF(left);
1660 Py_DECREF(right);
1661 SET_TOP(res);
1662 if (res == NULL)
1663 goto error;
1664 DISPATCH();
1665 }
1666
Benjamin Petersonddd19492018-09-16 22:38:02 -07001667 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001668 PyObject *right = POP();
1669 PyObject *left = TOP();
1670 PyObject *res = PyNumber_And(left, right);
1671 Py_DECREF(left);
1672 Py_DECREF(right);
1673 SET_TOP(res);
1674 if (res == NULL)
1675 goto error;
1676 DISPATCH();
1677 }
1678
Benjamin Petersonddd19492018-09-16 22:38:02 -07001679 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001680 PyObject *right = POP();
1681 PyObject *left = TOP();
1682 PyObject *res = PyNumber_Xor(left, right);
1683 Py_DECREF(left);
1684 Py_DECREF(right);
1685 SET_TOP(res);
1686 if (res == NULL)
1687 goto error;
1688 DISPATCH();
1689 }
1690
Benjamin Petersonddd19492018-09-16 22:38:02 -07001691 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001692 PyObject *right = POP();
1693 PyObject *left = TOP();
1694 PyObject *res = PyNumber_Or(left, right);
1695 Py_DECREF(left);
1696 Py_DECREF(right);
1697 SET_TOP(res);
1698 if (res == NULL)
1699 goto error;
1700 DISPATCH();
1701 }
1702
Benjamin Petersonddd19492018-09-16 22:38:02 -07001703 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001704 PyObject *v = POP();
1705 PyObject *list = PEEK(oparg);
1706 int err;
1707 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001708 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001709 if (err != 0)
1710 goto error;
1711 PREDICT(JUMP_ABSOLUTE);
1712 DISPATCH();
1713 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001714
Benjamin Petersonddd19492018-09-16 22:38:02 -07001715 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001716 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001717 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001718 int err;
1719 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001720 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001721 if (err != 0)
1722 goto error;
1723 PREDICT(JUMP_ABSOLUTE);
1724 DISPATCH();
1725 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001726
Benjamin Petersonddd19492018-09-16 22:38:02 -07001727 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001728 PyObject *exp = POP();
1729 PyObject *base = TOP();
1730 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1731 Py_DECREF(base);
1732 Py_DECREF(exp);
1733 SET_TOP(res);
1734 if (res == NULL)
1735 goto error;
1736 DISPATCH();
1737 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001738
Benjamin Petersonddd19492018-09-16 22:38:02 -07001739 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001740 PyObject *right = POP();
1741 PyObject *left = TOP();
1742 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1743 Py_DECREF(left);
1744 Py_DECREF(right);
1745 SET_TOP(res);
1746 if (res == NULL)
1747 goto error;
1748 DISPATCH();
1749 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001750
Benjamin Petersonddd19492018-09-16 22:38:02 -07001751 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001752 PyObject *right = POP();
1753 PyObject *left = TOP();
1754 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1755 Py_DECREF(left);
1756 Py_DECREF(right);
1757 SET_TOP(res);
1758 if (res == NULL)
1759 goto error;
1760 DISPATCH();
1761 }
1762
Benjamin Petersonddd19492018-09-16 22:38:02 -07001763 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001764 PyObject *divisor = POP();
1765 PyObject *dividend = TOP();
1766 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1767 Py_DECREF(dividend);
1768 Py_DECREF(divisor);
1769 SET_TOP(quotient);
1770 if (quotient == NULL)
1771 goto error;
1772 DISPATCH();
1773 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001774
Benjamin Petersonddd19492018-09-16 22:38:02 -07001775 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001776 PyObject *divisor = POP();
1777 PyObject *dividend = TOP();
1778 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1779 Py_DECREF(dividend);
1780 Py_DECREF(divisor);
1781 SET_TOP(quotient);
1782 if (quotient == NULL)
1783 goto error;
1784 DISPATCH();
1785 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001786
Benjamin Petersonddd19492018-09-16 22:38:02 -07001787 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001788 PyObject *right = POP();
1789 PyObject *left = TOP();
1790 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1791 Py_DECREF(left);
1792 Py_DECREF(right);
1793 SET_TOP(mod);
1794 if (mod == NULL)
1795 goto error;
1796 DISPATCH();
1797 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001798
Benjamin Petersonddd19492018-09-16 22:38:02 -07001799 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001800 PyObject *right = POP();
1801 PyObject *left = TOP();
1802 PyObject *sum;
1803 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001804 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001805 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001806 }
1807 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001808 sum = PyNumber_InPlaceAdd(left, right);
1809 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001810 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001811 Py_DECREF(right);
1812 SET_TOP(sum);
1813 if (sum == NULL)
1814 goto error;
1815 DISPATCH();
1816 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001817
Benjamin Petersonddd19492018-09-16 22:38:02 -07001818 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001819 PyObject *right = POP();
1820 PyObject *left = TOP();
1821 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1822 Py_DECREF(left);
1823 Py_DECREF(right);
1824 SET_TOP(diff);
1825 if (diff == NULL)
1826 goto error;
1827 DISPATCH();
1828 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001829
Benjamin Petersonddd19492018-09-16 22:38:02 -07001830 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001831 PyObject *right = POP();
1832 PyObject *left = TOP();
1833 PyObject *res = PyNumber_InPlaceLshift(left, right);
1834 Py_DECREF(left);
1835 Py_DECREF(right);
1836 SET_TOP(res);
1837 if (res == NULL)
1838 goto error;
1839 DISPATCH();
1840 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001841
Benjamin Petersonddd19492018-09-16 22:38:02 -07001842 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001843 PyObject *right = POP();
1844 PyObject *left = TOP();
1845 PyObject *res = PyNumber_InPlaceRshift(left, right);
1846 Py_DECREF(left);
1847 Py_DECREF(right);
1848 SET_TOP(res);
1849 if (res == NULL)
1850 goto error;
1851 DISPATCH();
1852 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001853
Benjamin Petersonddd19492018-09-16 22:38:02 -07001854 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001855 PyObject *right = POP();
1856 PyObject *left = TOP();
1857 PyObject *res = PyNumber_InPlaceAnd(left, right);
1858 Py_DECREF(left);
1859 Py_DECREF(right);
1860 SET_TOP(res);
1861 if (res == NULL)
1862 goto error;
1863 DISPATCH();
1864 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001865
Benjamin Petersonddd19492018-09-16 22:38:02 -07001866 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001867 PyObject *right = POP();
1868 PyObject *left = TOP();
1869 PyObject *res = PyNumber_InPlaceXor(left, right);
1870 Py_DECREF(left);
1871 Py_DECREF(right);
1872 SET_TOP(res);
1873 if (res == NULL)
1874 goto error;
1875 DISPATCH();
1876 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001877
Benjamin Petersonddd19492018-09-16 22:38:02 -07001878 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001879 PyObject *right = POP();
1880 PyObject *left = TOP();
1881 PyObject *res = PyNumber_InPlaceOr(left, right);
1882 Py_DECREF(left);
1883 Py_DECREF(right);
1884 SET_TOP(res);
1885 if (res == NULL)
1886 goto error;
1887 DISPATCH();
1888 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001889
Benjamin Petersonddd19492018-09-16 22:38:02 -07001890 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001891 PyObject *sub = TOP();
1892 PyObject *container = SECOND();
1893 PyObject *v = THIRD();
1894 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001895 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001896 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001897 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001899 Py_DECREF(container);
1900 Py_DECREF(sub);
1901 if (err != 0)
1902 goto error;
1903 DISPATCH();
1904 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001905
Benjamin Petersonddd19492018-09-16 22:38:02 -07001906 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001907 PyObject *sub = TOP();
1908 PyObject *container = SECOND();
1909 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001910 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00001911 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001912 err = PyObject_DelItem(container, sub);
1913 Py_DECREF(container);
1914 Py_DECREF(sub);
1915 if (err != 0)
1916 goto error;
1917 DISPATCH();
1918 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001919
Benjamin Petersonddd19492018-09-16 22:38:02 -07001920 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01001921 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001922 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001923 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001924 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001925 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001926 _PyErr_SetString(tstate, PyExc_RuntimeError,
1927 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001928 Py_DECREF(value);
1929 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930 }
Petr Viktorinffd97532020-02-11 17:46:57 +01001931 res = PyObject_CallOneArg(hook, value);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001932 Py_DECREF(value);
1933 if (res == NULL)
1934 goto error;
1935 Py_DECREF(res);
1936 DISPATCH();
1937 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001938
Benjamin Petersonddd19492018-09-16 22:38:02 -07001939 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001940 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001941 switch (oparg) {
1942 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001943 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001944 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001945 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001946 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001947 /* fall through */
1948 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02001949 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001950 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001951 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001952 break;
1953 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02001954 _PyErr_SetString(tstate, PyExc_SystemError,
1955 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001956 break;
1957 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001958 goto error;
1959 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001960
Benjamin Petersonddd19492018-09-16 22:38:02 -07001961 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001962 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001963 assert(f->f_iblock == 0);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00001964 assert(EMPTY());
1965 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001966 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001967
Benjamin Petersonddd19492018-09-16 22:38:02 -07001968 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001969 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001970 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001971 PyObject *obj = TOP();
1972 PyTypeObject *type = Py_TYPE(obj);
1973
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001974 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001975 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001976 }
Yury Selivanov75445082015-05-11 22:57:16 -04001977
1978 if (getter != NULL) {
1979 iter = (*getter)(obj);
1980 Py_DECREF(obj);
1981 if (iter == NULL) {
1982 SET_TOP(NULL);
1983 goto error;
1984 }
1985 }
1986 else {
1987 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02001988 _PyErr_Format(tstate, PyExc_TypeError,
1989 "'async for' requires an object with "
1990 "__aiter__ method, got %.100s",
1991 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001992 Py_DECREF(obj);
1993 goto error;
1994 }
1995
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001996 if (Py_TYPE(iter)->tp_as_async == NULL ||
1997 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001998
Yury Selivanov398ff912017-03-02 22:20:00 -05001999 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002000 _PyErr_Format(tstate, PyExc_TypeError,
2001 "'async for' received an object from __aiter__ "
2002 "that does not implement __anext__: %.100s",
2003 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002004 Py_DECREF(iter);
2005 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002006 }
2007
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002008 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04002009 DISPATCH();
2010 }
2011
Benjamin Petersonddd19492018-09-16 22:38:02 -07002012 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002013 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002014 PyObject *next_iter = NULL;
2015 PyObject *awaitable = NULL;
2016 PyObject *aiter = TOP();
2017 PyTypeObject *type = Py_TYPE(aiter);
2018
Yury Selivanoveb636452016-09-08 22:01:51 -07002019 if (PyAsyncGen_CheckExact(aiter)) {
2020 awaitable = type->tp_as_async->am_anext(aiter);
2021 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002022 goto error;
2023 }
Yury Selivanoveb636452016-09-08 22:01:51 -07002024 } else {
2025 if (type->tp_as_async != NULL){
2026 getter = type->tp_as_async->am_anext;
2027 }
Yury Selivanov75445082015-05-11 22:57:16 -04002028
Yury Selivanoveb636452016-09-08 22:01:51 -07002029 if (getter != NULL) {
2030 next_iter = (*getter)(aiter);
2031 if (next_iter == NULL) {
2032 goto error;
2033 }
2034 }
2035 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02002036 _PyErr_Format(tstate, PyExc_TypeError,
2037 "'async for' requires an iterator with "
2038 "__anext__ method, got %.100s",
2039 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07002040 goto error;
2041 }
Yury Selivanov75445082015-05-11 22:57:16 -04002042
Yury Selivanoveb636452016-09-08 22:01:51 -07002043 awaitable = _PyCoro_GetAwaitableIter(next_iter);
2044 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05002045 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07002046 PyExc_TypeError,
2047 "'async for' received an invalid object "
2048 "from __anext__: %.100s",
2049 Py_TYPE(next_iter)->tp_name);
2050
2051 Py_DECREF(next_iter);
2052 goto error;
2053 } else {
2054 Py_DECREF(next_iter);
2055 }
2056 }
Yury Selivanov75445082015-05-11 22:57:16 -04002057
2058 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002059 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002060 DISPATCH();
2061 }
2062
Benjamin Petersonddd19492018-09-16 22:38:02 -07002063 case TARGET(GET_AWAITABLE): {
2064 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002065 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002066 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002067
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002068 if (iter == NULL) {
Mark Shannonfee55262019-11-21 09:11:43 +00002069 int opcode_at_minus_3 = 0;
2070 if ((next_instr - first_instr) > 2) {
2071 opcode_at_minus_3 = _Py_OPCODE(next_instr[-3]);
2072 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002073 format_awaitable_error(tstate, Py_TYPE(iterable),
Mark Shannonfee55262019-11-21 09:11:43 +00002074 opcode_at_minus_3,
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002075 _Py_OPCODE(next_instr[-2]));
2076 }
2077
Yury Selivanov75445082015-05-11 22:57:16 -04002078 Py_DECREF(iterable);
2079
Yury Selivanovc724bae2016-03-02 11:30:46 -05002080 if (iter != NULL && PyCoro_CheckExact(iter)) {
2081 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2082 if (yf != NULL) {
2083 /* `iter` is a coroutine object that is being
2084 awaited, `yf` is a pointer to the current awaitable
2085 being awaited on. */
2086 Py_DECREF(yf);
2087 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002088 _PyErr_SetString(tstate, PyExc_RuntimeError,
2089 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002090 /* The code below jumps to `error` if `iter` is NULL. */
2091 }
2092 }
2093
Yury Selivanov75445082015-05-11 22:57:16 -04002094 SET_TOP(iter); /* Even if it's NULL */
2095
2096 if (iter == NULL) {
2097 goto error;
2098 }
2099
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002100 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002101 DISPATCH();
2102 }
2103
Benjamin Petersonddd19492018-09-16 22:38:02 -07002104 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002105 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002106 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002107 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002108 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
2109 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002110 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002111 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002112 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002113 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002114 else
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02002115 retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002116 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002117 Py_DECREF(v);
2118 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002119 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002120 if (tstate->c_tracefunc != NULL
Victor Stinner438a12d2019-05-24 17:01:38 +02002121 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002122 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002123 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002124 if (err < 0)
2125 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002126 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002127 SET_TOP(val);
2128 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002129 }
Martin Panter95f53c12016-07-18 08:23:26 +00002130 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002131 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002132 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002133 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002134 f->f_lasti -= sizeof(_Py_CODEUNIT);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002135 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002136 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002137
Benjamin Petersonddd19492018-09-16 22:38:02 -07002138 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002139 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002140
2141 if (co->co_flags & CO_ASYNC_GENERATOR) {
2142 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2143 Py_DECREF(retval);
2144 if (w == NULL) {
2145 retval = NULL;
2146 goto error;
2147 }
2148 retval = w;
2149 }
2150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002151 f->f_stacktop = stack_pointer;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002152 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002153 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002154
Benjamin Petersonddd19492018-09-16 22:38:02 -07002155 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002156 PyObject *type, *value, *traceback;
2157 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002158 PyTryBlock *b = PyFrame_BlockPop(f);
2159 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002160 _PyErr_SetString(tstate, PyExc_SystemError,
2161 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002162 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002163 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002164 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2165 STACK_LEVEL() <= (b)->b_level + 4);
2166 exc_info = tstate->exc_info;
2167 type = exc_info->exc_type;
2168 value = exc_info->exc_value;
2169 traceback = exc_info->exc_traceback;
2170 exc_info->exc_type = POP();
2171 exc_info->exc_value = POP();
2172 exc_info->exc_traceback = POP();
2173 Py_XDECREF(type);
2174 Py_XDECREF(value);
2175 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002176 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002177 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002178
Benjamin Petersonddd19492018-09-16 22:38:02 -07002179 case TARGET(POP_BLOCK): {
2180 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002181 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002182 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002183 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002184
Mark Shannonfee55262019-11-21 09:11:43 +00002185 case TARGET(RERAISE): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002186 PyObject *exc = POP();
Mark Shannonfee55262019-11-21 09:11:43 +00002187 PyObject *val = POP();
2188 PyObject *tb = POP();
2189 assert(PyExceptionClass_Check(exc));
Victor Stinner61f4db82020-01-28 03:37:45 +01002190 _PyErr_Restore(tstate, exc, val, tb);
Mark Shannonfee55262019-11-21 09:11:43 +00002191 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002192 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002193
Benjamin Petersonddd19492018-09-16 22:38:02 -07002194 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002195 PyObject *exc = POP();
2196 assert(PyExceptionClass_Check(exc));
2197 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2198 PyTryBlock *b = PyFrame_BlockPop(f);
2199 assert(b->b_type == EXCEPT_HANDLER);
2200 Py_DECREF(exc);
2201 UNWIND_EXCEPT_HANDLER(b);
2202 Py_DECREF(POP());
2203 JUMPBY(oparg);
2204 FAST_DISPATCH();
2205 }
2206 else {
2207 PyObject *val = POP();
2208 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002209 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002210 goto exception_unwind;
2211 }
2212 }
2213
Zackery Spytzce6a0702019-08-25 03:44:09 -06002214 case TARGET(LOAD_ASSERTION_ERROR): {
2215 PyObject *value = PyExc_AssertionError;
2216 Py_INCREF(value);
2217 PUSH(value);
2218 FAST_DISPATCH();
2219 }
2220
Benjamin Petersonddd19492018-09-16 22:38:02 -07002221 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002222 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002223
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002224 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002225 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002226 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002227 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002228 if (!_PyErr_Occurred(tstate)) {
2229 _PyErr_SetString(tstate, PyExc_NameError,
2230 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002231 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002232 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002233 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002234 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002235 }
2236 else {
2237 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2238 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002239 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002240 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2241 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002242 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2243 _PyErr_SetString(tstate, PyExc_NameError,
2244 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002245 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002246 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002247 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002248 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002249 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002250 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002251
Benjamin Petersonddd19492018-09-16 22:38:02 -07002252 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002253 PyObject *name = GETITEM(names, oparg);
2254 PyObject *v = POP();
2255 PyObject *ns = f->f_locals;
2256 int err;
2257 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002258 _PyErr_Format(tstate, PyExc_SystemError,
2259 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002260 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002261 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002262 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002263 if (PyDict_CheckExact(ns))
2264 err = PyDict_SetItem(ns, name, v);
2265 else
2266 err = PyObject_SetItem(ns, name, v);
2267 Py_DECREF(v);
2268 if (err != 0)
2269 goto error;
2270 DISPATCH();
2271 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002272
Benjamin Petersonddd19492018-09-16 22:38:02 -07002273 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002274 PyObject *name = GETITEM(names, oparg);
2275 PyObject *ns = f->f_locals;
2276 int err;
2277 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002278 _PyErr_Format(tstate, PyExc_SystemError,
2279 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002280 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002281 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002282 err = PyObject_DelItem(ns, name);
2283 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002284 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002285 NAME_ERROR_MSG,
2286 name);
2287 goto error;
2288 }
2289 DISPATCH();
2290 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002291
Benjamin Petersonddd19492018-09-16 22:38:02 -07002292 case TARGET(UNPACK_SEQUENCE): {
2293 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002294 PyObject *seq = POP(), *item, **items;
2295 if (PyTuple_CheckExact(seq) &&
2296 PyTuple_GET_SIZE(seq) == oparg) {
2297 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002298 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002299 item = items[oparg];
2300 Py_INCREF(item);
2301 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002302 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002303 } else if (PyList_CheckExact(seq) &&
2304 PyList_GET_SIZE(seq) == oparg) {
2305 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002306 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002307 item = items[oparg];
2308 Py_INCREF(item);
2309 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002310 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002311 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002312 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002313 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002314 } else {
2315 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002316 Py_DECREF(seq);
2317 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002318 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002319 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002320 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002321 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002322
Benjamin Petersonddd19492018-09-16 22:38:02 -07002323 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002324 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2325 PyObject *seq = POP();
2326
Victor Stinner438a12d2019-05-24 17:01:38 +02002327 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002328 stack_pointer + totalargs)) {
2329 stack_pointer += totalargs;
2330 } else {
2331 Py_DECREF(seq);
2332 goto error;
2333 }
2334 Py_DECREF(seq);
2335 DISPATCH();
2336 }
2337
Benjamin Petersonddd19492018-09-16 22:38:02 -07002338 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002339 PyObject *name = GETITEM(names, oparg);
2340 PyObject *owner = TOP();
2341 PyObject *v = SECOND();
2342 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002343 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002344 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002345 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002346 Py_DECREF(owner);
2347 if (err != 0)
2348 goto error;
2349 DISPATCH();
2350 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002351
Benjamin Petersonddd19492018-09-16 22:38:02 -07002352 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002353 PyObject *name = GETITEM(names, oparg);
2354 PyObject *owner = POP();
2355 int err;
2356 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2357 Py_DECREF(owner);
2358 if (err != 0)
2359 goto error;
2360 DISPATCH();
2361 }
2362
Benjamin Petersonddd19492018-09-16 22:38:02 -07002363 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002364 PyObject *name = GETITEM(names, oparg);
2365 PyObject *v = POP();
2366 int err;
2367 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002368 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002369 if (err != 0)
2370 goto error;
2371 DISPATCH();
2372 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002373
Benjamin Petersonddd19492018-09-16 22:38:02 -07002374 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002375 PyObject *name = GETITEM(names, oparg);
2376 int err;
2377 err = PyDict_DelItem(f->f_globals, name);
2378 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002379 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2380 format_exc_check_arg(tstate, PyExc_NameError,
2381 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002382 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002383 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002384 }
2385 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002386 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002387
Benjamin Petersonddd19492018-09-16 22:38:02 -07002388 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002389 PyObject *name = GETITEM(names, oparg);
2390 PyObject *locals = f->f_locals;
2391 PyObject *v;
2392 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002393 _PyErr_Format(tstate, PyExc_SystemError,
2394 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002395 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002396 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002397 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002398 v = PyDict_GetItemWithError(locals, name);
2399 if (v != NULL) {
2400 Py_INCREF(v);
2401 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002402 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002403 goto error;
2404 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002405 }
2406 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002407 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002408 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002409 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002410 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002411 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002412 }
2413 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002414 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002415 v = PyDict_GetItemWithError(f->f_globals, name);
2416 if (v != NULL) {
2417 Py_INCREF(v);
2418 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002419 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002420 goto error;
2421 }
2422 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002423 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002424 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002425 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002426 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002427 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002428 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002429 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002430 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002431 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002432 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002433 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002434 }
2435 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002436 v = PyObject_GetItem(f->f_builtins, name);
2437 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002438 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002439 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002440 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002441 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002442 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002443 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002444 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002445 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002446 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002447 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002448 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002449 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002450 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002451
Benjamin Petersonddd19492018-09-16 22:38:02 -07002452 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002453 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002454 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002455 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002456 && PyDict_CheckExact(f->f_builtins))
2457 {
Inada Naoki91234a12019-06-03 21:30:58 +09002458 OPCACHE_CHECK();
2459 if (co_opcache != NULL && co_opcache->optimized > 0) {
2460 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2461
2462 if (lg->globals_ver ==
2463 ((PyDictObject *)f->f_globals)->ma_version_tag
2464 && lg->builtins_ver ==
2465 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2466 {
2467 PyObject *ptr = lg->ptr;
2468 OPCACHE_STAT_GLOBAL_HIT();
2469 assert(ptr != NULL);
2470 Py_INCREF(ptr);
2471 PUSH(ptr);
2472 DISPATCH();
2473 }
2474 }
2475
2476 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002477 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002478 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002479 name);
2480 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002481 if (!_PyErr_OCCURRED()) {
2482 /* _PyDict_LoadGlobal() returns NULL without raising
2483 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002484 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002485 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002486 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002487 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002488 }
Inada Naoki91234a12019-06-03 21:30:58 +09002489
2490 if (co_opcache != NULL) {
2491 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2492
2493 if (co_opcache->optimized == 0) {
2494 /* Wasn't optimized before. */
2495 OPCACHE_STAT_GLOBAL_OPT();
2496 } else {
2497 OPCACHE_STAT_GLOBAL_MISS();
2498 }
2499
2500 co_opcache->optimized = 1;
2501 lg->globals_ver =
2502 ((PyDictObject *)f->f_globals)->ma_version_tag;
2503 lg->builtins_ver =
2504 ((PyDictObject *)f->f_builtins)->ma_version_tag;
2505 lg->ptr = v; /* borrowed */
2506 }
2507
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002508 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002509 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002510 else {
2511 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002512
2513 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09002514 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002515 v = PyObject_GetItem(f->f_globals, name);
2516 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002517 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002518 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002519 }
2520 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002521
Victor Stinnerb4efc962015-11-20 09:24:02 +01002522 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002523 v = PyObject_GetItem(f->f_builtins, name);
2524 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002525 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002526 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002527 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002528 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002529 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002530 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002531 }
2532 }
2533 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002534 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002535 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002536 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002537
Benjamin Petersonddd19492018-09-16 22:38:02 -07002538 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002539 PyObject *v = GETLOCAL(oparg);
2540 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002541 SETLOCAL(oparg, NULL);
2542 DISPATCH();
2543 }
2544 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002545 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002546 UNBOUNDLOCAL_ERROR_MSG,
2547 PyTuple_GetItem(co->co_varnames, oparg)
2548 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002549 goto error;
2550 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002551
Benjamin Petersonddd19492018-09-16 22:38:02 -07002552 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002553 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002554 PyObject *oldobj = PyCell_GET(cell);
2555 if (oldobj != NULL) {
2556 PyCell_SET(cell, NULL);
2557 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002558 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002559 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002560 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002561 goto error;
2562 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002563
Benjamin Petersonddd19492018-09-16 22:38:02 -07002564 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002565 PyObject *cell = freevars[oparg];
2566 Py_INCREF(cell);
2567 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002568 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002569 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002570
Benjamin Petersonddd19492018-09-16 22:38:02 -07002571 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002572 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002573 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002574 assert(locals);
2575 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2576 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2577 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2578 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2579 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002580 value = PyDict_GetItemWithError(locals, name);
2581 if (value != NULL) {
2582 Py_INCREF(value);
2583 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002584 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002585 goto error;
2586 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002587 }
2588 else {
2589 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002590 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002591 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002592 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002593 }
2594 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002595 }
2596 }
2597 if (!value) {
2598 PyObject *cell = freevars[oparg];
2599 value = PyCell_GET(cell);
2600 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002601 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002602 goto error;
2603 }
2604 Py_INCREF(value);
2605 }
2606 PUSH(value);
2607 DISPATCH();
2608 }
2609
Benjamin Petersonddd19492018-09-16 22:38:02 -07002610 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002611 PyObject *cell = freevars[oparg];
2612 PyObject *value = PyCell_GET(cell);
2613 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002614 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002615 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002616 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002617 Py_INCREF(value);
2618 PUSH(value);
2619 DISPATCH();
2620 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002621
Benjamin Petersonddd19492018-09-16 22:38:02 -07002622 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002623 PyObject *v = POP();
2624 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002625 PyObject *oldobj = PyCell_GET(cell);
2626 PyCell_SET(cell, v);
2627 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002628 DISPATCH();
2629 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002630
Benjamin Petersonddd19492018-09-16 22:38:02 -07002631 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002632 PyObject *str;
2633 PyObject *empty = PyUnicode_New(0, 0);
2634 if (empty == NULL) {
2635 goto error;
2636 }
2637 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2638 Py_DECREF(empty);
2639 if (str == NULL)
2640 goto error;
2641 while (--oparg >= 0) {
2642 PyObject *item = POP();
2643 Py_DECREF(item);
2644 }
2645 PUSH(str);
2646 DISPATCH();
2647 }
2648
Benjamin Petersonddd19492018-09-16 22:38:02 -07002649 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002650 PyObject *tup = PyTuple_New(oparg);
2651 if (tup == NULL)
2652 goto error;
2653 while (--oparg >= 0) {
2654 PyObject *item = POP();
2655 PyTuple_SET_ITEM(tup, oparg, item);
2656 }
2657 PUSH(tup);
2658 DISPATCH();
2659 }
2660
Benjamin Petersonddd19492018-09-16 22:38:02 -07002661 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002662 PyObject *list = PyList_New(oparg);
2663 if (list == NULL)
2664 goto error;
2665 while (--oparg >= 0) {
2666 PyObject *item = POP();
2667 PyList_SET_ITEM(list, oparg, item);
2668 }
2669 PUSH(list);
2670 DISPATCH();
2671 }
2672
Mark Shannon13bc1392020-01-23 09:25:17 +00002673 case TARGET(LIST_TO_TUPLE): {
2674 PyObject *list = POP();
2675 PyObject *tuple = PyList_AsTuple(list);
2676 Py_DECREF(list);
2677 if (tuple == NULL) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002678 goto error;
Mark Shannon13bc1392020-01-23 09:25:17 +00002679 }
2680 PUSH(tuple);
2681 DISPATCH();
2682 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002683
Mark Shannon13bc1392020-01-23 09:25:17 +00002684 case TARGET(LIST_EXTEND): {
2685 PyObject *iterable = POP();
2686 PyObject *list = PEEK(oparg);
2687 PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
2688 if (none_val == NULL) {
2689 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01002690 (Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable)))
Mark Shannon13bc1392020-01-23 09:25:17 +00002691 {
Victor Stinner61f4db82020-01-28 03:37:45 +01002692 _PyErr_Clear(tstate);
Mark Shannon13bc1392020-01-23 09:25:17 +00002693 _PyErr_Format(tstate, PyExc_TypeError,
2694 "Value after * must be an iterable, not %.200s",
2695 Py_TYPE(iterable)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002696 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002697 Py_DECREF(iterable);
2698 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002699 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002700 Py_DECREF(none_val);
2701 Py_DECREF(iterable);
2702 DISPATCH();
2703 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002704
Mark Shannon13bc1392020-01-23 09:25:17 +00002705 case TARGET(SET_UPDATE): {
2706 PyObject *iterable = POP();
2707 PyObject *set = PEEK(oparg);
2708 int err = _PySet_Update(set, iterable);
2709 Py_DECREF(iterable);
2710 if (err < 0) {
2711 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002712 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002713 DISPATCH();
2714 }
2715
Benjamin Petersonddd19492018-09-16 22:38:02 -07002716 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002717 PyObject *set = PySet_New(NULL);
2718 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002719 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002720 if (set == NULL)
2721 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002722 for (i = oparg; i > 0; i--) {
2723 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002724 if (err == 0)
2725 err = PySet_Add(set, item);
2726 Py_DECREF(item);
2727 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002728 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002729 if (err != 0) {
2730 Py_DECREF(set);
2731 goto error;
2732 }
2733 PUSH(set);
2734 DISPATCH();
2735 }
2736
Benjamin Petersonddd19492018-09-16 22:38:02 -07002737 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002738 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002739 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2740 if (map == NULL)
2741 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002742 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002743 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002744 PyObject *key = PEEK(2*i);
2745 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002746 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002747 if (err != 0) {
2748 Py_DECREF(map);
2749 goto error;
2750 }
2751 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002752
2753 while (oparg--) {
2754 Py_DECREF(POP());
2755 Py_DECREF(POP());
2756 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002757 PUSH(map);
2758 DISPATCH();
2759 }
2760
Benjamin Petersonddd19492018-09-16 22:38:02 -07002761 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002762 _Py_IDENTIFIER(__annotations__);
2763 int err;
2764 PyObject *ann_dict;
2765 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002766 _PyErr_Format(tstate, PyExc_SystemError,
2767 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002768 goto error;
2769 }
2770 /* check if __annotations__ in locals()... */
2771 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002772 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002773 &PyId___annotations__);
2774 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002775 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002776 goto error;
2777 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002778 /* ...if not, create a new one */
2779 ann_dict = PyDict_New();
2780 if (ann_dict == NULL) {
2781 goto error;
2782 }
2783 err = _PyDict_SetItemId(f->f_locals,
2784 &PyId___annotations__, ann_dict);
2785 Py_DECREF(ann_dict);
2786 if (err != 0) {
2787 goto error;
2788 }
2789 }
2790 }
2791 else {
2792 /* do the same if locals() is not a dict */
2793 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2794 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002795 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002796 }
2797 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2798 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002799 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002800 goto error;
2801 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002802 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002803 ann_dict = PyDict_New();
2804 if (ann_dict == NULL) {
2805 goto error;
2806 }
2807 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2808 Py_DECREF(ann_dict);
2809 if (err != 0) {
2810 goto error;
2811 }
2812 }
2813 else {
2814 Py_DECREF(ann_dict);
2815 }
2816 }
2817 DISPATCH();
2818 }
2819
Benjamin Petersonddd19492018-09-16 22:38:02 -07002820 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002821 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002822 PyObject *map;
2823 PyObject *keys = TOP();
2824 if (!PyTuple_CheckExact(keys) ||
2825 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002826 _PyErr_SetString(tstate, PyExc_SystemError,
2827 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002828 goto error;
2829 }
2830 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2831 if (map == NULL) {
2832 goto error;
2833 }
2834 for (i = oparg; i > 0; i--) {
2835 int err;
2836 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2837 PyObject *value = PEEK(i + 1);
2838 err = PyDict_SetItem(map, key, value);
2839 if (err != 0) {
2840 Py_DECREF(map);
2841 goto error;
2842 }
2843 }
2844
2845 Py_DECREF(POP());
2846 while (oparg--) {
2847 Py_DECREF(POP());
2848 }
2849 PUSH(map);
2850 DISPATCH();
2851 }
2852
Mark Shannon8a4cd702020-01-27 09:57:45 +00002853 case TARGET(DICT_UPDATE): {
2854 PyObject *update = POP();
2855 PyObject *dict = PEEK(oparg);
2856 if (PyDict_Update(dict, update) < 0) {
2857 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
2858 _PyErr_Format(tstate, PyExc_TypeError,
2859 "'%.200s' object is not a mapping",
Victor Stinnera102ed72020-02-07 02:24:48 +01002860 Py_TYPE(update)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002861 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002862 Py_DECREF(update);
2863 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002864 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002865 Py_DECREF(update);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002866 DISPATCH();
2867 }
2868
Mark Shannon8a4cd702020-01-27 09:57:45 +00002869 case TARGET(DICT_MERGE): {
2870 PyObject *update = POP();
2871 PyObject *dict = PEEK(oparg);
2872
2873 if (_PyDict_MergeEx(dict, update, 2) < 0) {
2874 format_kwargs_error(tstate, PEEK(2 + oparg), update);
2875 Py_DECREF(update);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002876 goto error;
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002877 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002878 Py_DECREF(update);
Brandt Bucherf185a732019-09-28 17:12:49 -07002879 PREDICT(CALL_FUNCTION_EX);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002880 DISPATCH();
2881 }
2882
Benjamin Petersonddd19492018-09-16 22:38:02 -07002883 case TARGET(MAP_ADD): {
Jörn Heisslerc8a35412019-06-22 16:40:55 +02002884 PyObject *value = TOP();
2885 PyObject *key = SECOND();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002886 PyObject *map;
2887 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002888 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002889 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002890 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002891 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002892 Py_DECREF(value);
2893 Py_DECREF(key);
2894 if (err != 0)
2895 goto error;
2896 PREDICT(JUMP_ABSOLUTE);
2897 DISPATCH();
2898 }
2899
Benjamin Petersonddd19492018-09-16 22:38:02 -07002900 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002901 PyObject *name = GETITEM(names, oparg);
2902 PyObject *owner = TOP();
2903 PyObject *res = PyObject_GetAttr(owner, name);
2904 Py_DECREF(owner);
2905 SET_TOP(res);
2906 if (res == NULL)
2907 goto error;
2908 DISPATCH();
2909 }
2910
Benjamin Petersonddd19492018-09-16 22:38:02 -07002911 case TARGET(COMPARE_OP): {
Mark Shannon9af0e472020-01-14 10:12:45 +00002912 assert(oparg <= Py_GE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002913 PyObject *right = POP();
2914 PyObject *left = TOP();
Mark Shannon9af0e472020-01-14 10:12:45 +00002915 PyObject *res = PyObject_RichCompare(left, right, oparg);
2916 SET_TOP(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002917 Py_DECREF(left);
2918 Py_DECREF(right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002919 if (res == NULL)
2920 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002921 PREDICT(POP_JUMP_IF_FALSE);
2922 PREDICT(POP_JUMP_IF_TRUE);
2923 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002924 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002925
Mark Shannon9af0e472020-01-14 10:12:45 +00002926 case TARGET(IS_OP): {
2927 PyObject *right = POP();
2928 PyObject *left = TOP();
2929 int res = (left == right)^oparg;
2930 PyObject *b = res ? Py_True : Py_False;
2931 Py_INCREF(b);
2932 SET_TOP(b);
2933 Py_DECREF(left);
2934 Py_DECREF(right);
2935 PREDICT(POP_JUMP_IF_FALSE);
2936 PREDICT(POP_JUMP_IF_TRUE);
2937 FAST_DISPATCH();
2938 }
2939
2940 case TARGET(CONTAINS_OP): {
2941 PyObject *right = POP();
2942 PyObject *left = POP();
2943 int res = PySequence_Contains(right, left);
2944 Py_DECREF(left);
2945 Py_DECREF(right);
2946 if (res < 0) {
2947 goto error;
2948 }
2949 PyObject *b = (res^oparg) ? Py_True : Py_False;
2950 Py_INCREF(b);
2951 PUSH(b);
2952 PREDICT(POP_JUMP_IF_FALSE);
2953 PREDICT(POP_JUMP_IF_TRUE);
2954 FAST_DISPATCH();
2955 }
2956
2957#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
2958 "BaseException is not allowed"
2959
2960 case TARGET(JUMP_IF_NOT_EXC_MATCH): {
2961 PyObject *right = POP();
2962 PyObject *left = POP();
2963 if (PyTuple_Check(right)) {
2964 Py_ssize_t i, length;
2965 length = PyTuple_GET_SIZE(right);
2966 for (i = 0; i < length; i++) {
2967 PyObject *exc = PyTuple_GET_ITEM(right, i);
2968 if (!PyExceptionClass_Check(exc)) {
2969 _PyErr_SetString(tstate, PyExc_TypeError,
2970 CANNOT_CATCH_MSG);
2971 Py_DECREF(left);
2972 Py_DECREF(right);
2973 goto error;
2974 }
2975 }
2976 }
2977 else {
2978 if (!PyExceptionClass_Check(right)) {
2979 _PyErr_SetString(tstate, PyExc_TypeError,
2980 CANNOT_CATCH_MSG);
2981 Py_DECREF(left);
2982 Py_DECREF(right);
2983 goto error;
2984 }
2985 }
2986 int res = PyErr_GivenExceptionMatches(left, right);
2987 Py_DECREF(left);
2988 Py_DECREF(right);
2989 if (res > 0) {
2990 /* Exception matches -- Do nothing */;
2991 }
2992 else if (res == 0) {
2993 JUMPTO(oparg);
2994 }
2995 else {
2996 goto error;
2997 }
2998 DISPATCH();
2999 }
3000
Benjamin Petersonddd19492018-09-16 22:38:02 -07003001 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003002 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003003 PyObject *fromlist = POP();
3004 PyObject *level = TOP();
3005 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003006 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003007 Py_DECREF(level);
3008 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003009 SET_TOP(res);
3010 if (res == NULL)
3011 goto error;
3012 DISPATCH();
3013 }
3014
Benjamin Petersonddd19492018-09-16 22:38:02 -07003015 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003016 PyObject *from = POP(), *locals;
3017 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003018 if (PyFrame_FastToLocalsWithError(f) < 0) {
3019 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01003020 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003021 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01003022
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003023 locals = f->f_locals;
3024 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003025 _PyErr_SetString(tstate, PyExc_SystemError,
3026 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003027 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003028 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003029 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003030 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003031 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003032 Py_DECREF(from);
3033 if (err != 0)
3034 goto error;
3035 DISPATCH();
3036 }
Guido van Rossum25831651993-05-19 14:50:45 +00003037
Benjamin Petersonddd19492018-09-16 22:38:02 -07003038 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003039 PyObject *name = GETITEM(names, oparg);
3040 PyObject *from = TOP();
3041 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003042 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003043 PUSH(res);
3044 if (res == NULL)
3045 goto error;
3046 DISPATCH();
3047 }
Thomas Wouters52152252000-08-17 22:55:00 +00003048
Benjamin Petersonddd19492018-09-16 22:38:02 -07003049 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003050 JUMPBY(oparg);
3051 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003052 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003053
Benjamin Petersonddd19492018-09-16 22:38:02 -07003054 case TARGET(POP_JUMP_IF_FALSE): {
3055 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003056 PyObject *cond = POP();
3057 int err;
3058 if (cond == Py_True) {
3059 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003060 FAST_DISPATCH();
3061 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003062 if (cond == Py_False) {
3063 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003064 JUMPTO(oparg);
3065 FAST_DISPATCH();
3066 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003067 err = PyObject_IsTrue(cond);
3068 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003069 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003070 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003071 else if (err == 0)
3072 JUMPTO(oparg);
3073 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003074 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003075 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003076 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003077
Benjamin Petersonddd19492018-09-16 22:38:02 -07003078 case TARGET(POP_JUMP_IF_TRUE): {
3079 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003080 PyObject *cond = POP();
3081 int err;
3082 if (cond == Py_False) {
3083 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003084 FAST_DISPATCH();
3085 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003086 if (cond == Py_True) {
3087 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003088 JUMPTO(oparg);
3089 FAST_DISPATCH();
3090 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003091 err = PyObject_IsTrue(cond);
3092 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003093 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003094 JUMPTO(oparg);
3095 }
3096 else if (err == 0)
3097 ;
3098 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003099 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003100 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003101 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003102
Benjamin Petersonddd19492018-09-16 22:38:02 -07003103 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003104 PyObject *cond = TOP();
3105 int err;
3106 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003107 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003108 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003109 FAST_DISPATCH();
3110 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003111 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003112 JUMPTO(oparg);
3113 FAST_DISPATCH();
3114 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003115 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003116 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003117 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003118 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003119 }
3120 else if (err == 0)
3121 JUMPTO(oparg);
3122 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003123 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003124 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003125 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003126
Benjamin Petersonddd19492018-09-16 22:38:02 -07003127 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003128 PyObject *cond = TOP();
3129 int err;
3130 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003131 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003132 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003133 FAST_DISPATCH();
3134 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003135 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003136 JUMPTO(oparg);
3137 FAST_DISPATCH();
3138 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003139 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003140 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003141 JUMPTO(oparg);
3142 }
3143 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003144 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003145 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003146 }
3147 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003148 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003149 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003150 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003151
Benjamin Petersonddd19492018-09-16 22:38:02 -07003152 case TARGET(JUMP_ABSOLUTE): {
3153 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003154 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003155#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003156 /* Enabling this path speeds-up all while and for-loops by bypassing
3157 the per-loop checks for signals. By default, this should be turned-off
3158 because it prevents detection of a control-break in tight loops like
3159 "while 1: pass". Compile with this option turned-on when you need
3160 the speed-up and do not need break checking inside tight loops (ones
3161 that contain only instructions ending with FAST_DISPATCH).
3162 */
3163 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003164#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003165 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003166#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003167 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003168
Benjamin Petersonddd19492018-09-16 22:38:02 -07003169 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003170 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003171 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003172 PyObject *iter = PyObject_GetIter(iterable);
3173 Py_DECREF(iterable);
3174 SET_TOP(iter);
3175 if (iter == NULL)
3176 goto error;
3177 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003178 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003179 DISPATCH();
3180 }
3181
Benjamin Petersonddd19492018-09-16 22:38:02 -07003182 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04003183 /* before: [obj]; after [getiter(obj)] */
3184 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003185 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003186 if (PyCoro_CheckExact(iterable)) {
3187 /* `iterable` is a coroutine */
3188 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3189 /* and it is used in a 'yield from' expression of a
3190 regular generator. */
3191 Py_DECREF(iterable);
3192 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003193 _PyErr_SetString(tstate, PyExc_TypeError,
3194 "cannot 'yield from' a coroutine object "
3195 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04003196 goto error;
3197 }
3198 }
3199 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003200 /* `iterable` is not a generator. */
3201 iter = PyObject_GetIter(iterable);
3202 Py_DECREF(iterable);
3203 SET_TOP(iter);
3204 if (iter == NULL)
3205 goto error;
3206 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003207 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003208 DISPATCH();
3209 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003210
Benjamin Petersonddd19492018-09-16 22:38:02 -07003211 case TARGET(FOR_ITER): {
3212 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003213 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003214 PyObject *iter = TOP();
Victor Stinnera102ed72020-02-07 02:24:48 +01003215 PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003216 if (next != NULL) {
3217 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003218 PREDICT(STORE_FAST);
3219 PREDICT(UNPACK_SEQUENCE);
3220 DISPATCH();
3221 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003222 if (_PyErr_Occurred(tstate)) {
3223 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003224 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003225 }
3226 else if (tstate->c_tracefunc != NULL) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003227 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Victor Stinner438a12d2019-05-24 17:01:38 +02003228 }
3229 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003230 }
3231 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003232 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003233 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003234 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003235 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003236 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003237 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003238
Benjamin Petersonddd19492018-09-16 22:38:02 -07003239 case TARGET(SETUP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003240 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003241 STACK_LEVEL());
3242 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003243 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003244
Benjamin Petersonddd19492018-09-16 22:38:02 -07003245 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003246 _Py_IDENTIFIER(__aenter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003247 _Py_IDENTIFIER(__aexit__);
Yury Selivanov75445082015-05-11 22:57:16 -04003248 PyObject *mgr = TOP();
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003249 PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04003250 PyObject *res;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003251 if (enter == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003252 goto error;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003253 }
3254 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__);
3255 if (exit == NULL) {
3256 Py_DECREF(enter);
3257 goto error;
3258 }
Yury Selivanov75445082015-05-11 22:57:16 -04003259 SET_TOP(exit);
Yury Selivanov75445082015-05-11 22:57:16 -04003260 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003261 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003262 Py_DECREF(enter);
3263 if (res == NULL)
3264 goto error;
3265 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003266 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003267 DISPATCH();
3268 }
3269
Benjamin Petersonddd19492018-09-16 22:38:02 -07003270 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003271 PyObject *res = POP();
3272 /* Setup the finally block before pushing the result
3273 of __aenter__ on the stack. */
3274 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3275 STACK_LEVEL());
3276 PUSH(res);
3277 DISPATCH();
3278 }
3279
Benjamin Petersonddd19492018-09-16 22:38:02 -07003280 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003281 _Py_IDENTIFIER(__enter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003282 _Py_IDENTIFIER(__exit__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003283 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003284 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003285 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003286 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08003287 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003288 }
3289 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003290 if (exit == NULL) {
3291 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003292 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003293 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003294 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003295 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003296 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003297 Py_DECREF(enter);
3298 if (res == NULL)
3299 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003300 /* Setup the finally block before pushing the result
3301 of __enter__ on the stack. */
3302 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3303 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003304
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003305 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003306 DISPATCH();
3307 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003308
Mark Shannonfee55262019-11-21 09:11:43 +00003309 case TARGET(WITH_EXCEPT_START): {
3310 /* At the top of the stack are 7 values:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003311 - (TOP, SECOND, THIRD) = exc_info()
Mark Shannonfee55262019-11-21 09:11:43 +00003312 - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER
3313 - SEVENTH: the context.__exit__ bound method
3314 We call SEVENTH(TOP, SECOND, THIRD).
3315 Then we push again the TOP exception and the __exit__
3316 return value.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003317 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003318 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003319 PyObject *exc, *val, *tb, *res;
3320
Victor Stinner842cfff2016-12-01 14:45:31 +01003321 exc = TOP();
Mark Shannonfee55262019-11-21 09:11:43 +00003322 val = SECOND();
3323 tb = THIRD();
3324 assert(exc != Py_None);
3325 assert(!PyLong_Check(exc));
3326 exit_func = PEEK(7);
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003327 PyObject *stack[4] = {NULL, exc, val, tb};
Petr Viktorinffd97532020-02-11 17:46:57 +01003328 res = PyObject_Vectorcall(exit_func, stack + 1,
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003329 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003330 if (res == NULL)
3331 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003332
Yury Selivanov75445082015-05-11 22:57:16 -04003333 PUSH(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003334 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003335 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003336
Benjamin Petersonddd19492018-09-16 22:38:02 -07003337 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10003338 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003339 PyObject *name = GETITEM(names, oparg);
3340 PyObject *obj = TOP();
3341 PyObject *meth = NULL;
3342
3343 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3344
Yury Selivanovf2392132016-12-13 19:03:51 -05003345 if (meth == NULL) {
3346 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003347 goto error;
3348 }
3349
3350 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003351 /* We can bypass temporary bound method object.
3352 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003353
INADA Naoki015bce62017-01-16 17:23:30 +09003354 meth | self | arg1 | ... | argN
3355 */
3356 SET_TOP(meth);
3357 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003358 }
3359 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003360 /* meth is not an unbound method (but a regular attr, or
3361 something was returned by a descriptor protocol). Set
3362 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003363 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003364
3365 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003366 */
INADA Naoki015bce62017-01-16 17:23:30 +09003367 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003368 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003369 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003370 }
3371 DISPATCH();
3372 }
3373
Benjamin Petersonddd19492018-09-16 22:38:02 -07003374 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003375 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003376 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003377
3378 sp = stack_pointer;
3379
INADA Naoki015bce62017-01-16 17:23:30 +09003380 meth = PEEK(oparg + 2);
3381 if (meth == NULL) {
3382 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3383 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003384
3385 Stack layout:
3386
INADA Naoki015bce62017-01-16 17:23:30 +09003387 ... | NULL | callable | arg1 | ... | argN
3388 ^- TOP()
3389 ^- (-oparg)
3390 ^- (-oparg-1)
3391 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003392
Ville Skyttä49b27342017-08-03 09:00:59 +03003393 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003394 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003395 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003396 res = call_function(tstate, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003397 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003398 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003399 }
3400 else {
3401 /* This is a method call. Stack layout:
3402
INADA Naoki015bce62017-01-16 17:23:30 +09003403 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003404 ^- TOP()
3405 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003406 ^- (-oparg-1)
3407 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003408
INADA Naoki015bce62017-01-16 17:23:30 +09003409 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003410 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003411 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003412 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003413 res = call_function(tstate, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003414 stack_pointer = sp;
3415 }
3416
3417 PUSH(res);
3418 if (res == NULL)
3419 goto error;
3420 DISPATCH();
3421 }
3422
Benjamin Petersonddd19492018-09-16 22:38:02 -07003423 case TARGET(CALL_FUNCTION): {
3424 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003425 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003426 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003427 res = call_function(tstate, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003428 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003429 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003430 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003431 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003432 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003433 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003434 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003435
Benjamin Petersonddd19492018-09-16 22:38:02 -07003436 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003437 PyObject **sp, *res, *names;
3438
3439 names = POP();
Jeroen Demeyer05677862019-08-16 12:41:27 +02003440 assert(PyTuple_Check(names));
3441 assert(PyTuple_GET_SIZE(names) <= oparg);
3442 /* We assume without checking that names contains only strings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003443 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003444 res = call_function(tstate, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003445 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003446 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003447 Py_DECREF(names);
3448
3449 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003450 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003451 }
3452 DISPATCH();
3453 }
3454
Benjamin Petersonddd19492018-09-16 22:38:02 -07003455 case TARGET(CALL_FUNCTION_EX): {
Brandt Bucherf185a732019-09-28 17:12:49 -07003456 PREDICTED(CALL_FUNCTION_EX);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003457 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003458 if (oparg & 0x01) {
3459 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003460 if (!PyDict_CheckExact(kwargs)) {
3461 PyObject *d = PyDict_New();
3462 if (d == NULL)
3463 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003464 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003465 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02003466 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003467 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003468 goto error;
3469 }
3470 Py_DECREF(kwargs);
3471 kwargs = d;
3472 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003473 assert(PyDict_CheckExact(kwargs));
3474 }
3475 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003476 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003477 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003478 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003479 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003480 goto error;
3481 }
3482 Py_SETREF(callargs, PySequence_Tuple(callargs));
3483 if (callargs == NULL) {
3484 goto error;
3485 }
3486 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003487 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003488
Victor Stinner09532fe2019-05-10 23:39:09 +02003489 result = do_call_core(tstate, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003490 Py_DECREF(func);
3491 Py_DECREF(callargs);
3492 Py_XDECREF(kwargs);
3493
3494 SET_TOP(result);
3495 if (result == NULL) {
3496 goto error;
3497 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003498 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003499 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003500
Benjamin Petersonddd19492018-09-16 22:38:02 -07003501 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003502 PyObject *qualname = POP();
3503 PyObject *codeobj = POP();
3504 PyFunctionObject *func = (PyFunctionObject *)
3505 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003506
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003507 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003508 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003509 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003510 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003511 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003512
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003513 if (oparg & 0x08) {
3514 assert(PyTuple_CheckExact(TOP()));
3515 func ->func_closure = POP();
3516 }
3517 if (oparg & 0x04) {
3518 assert(PyDict_CheckExact(TOP()));
3519 func->func_annotations = POP();
3520 }
3521 if (oparg & 0x02) {
3522 assert(PyDict_CheckExact(TOP()));
3523 func->func_kwdefaults = POP();
3524 }
3525 if (oparg & 0x01) {
3526 assert(PyTuple_CheckExact(TOP()));
3527 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003528 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003529
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003530 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003531 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003532 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003533
Benjamin Petersonddd19492018-09-16 22:38:02 -07003534 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003535 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003536 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003537 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003538 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003539 step = NULL;
3540 stop = POP();
3541 start = TOP();
3542 slice = PySlice_New(start, stop, step);
3543 Py_DECREF(start);
3544 Py_DECREF(stop);
3545 Py_XDECREF(step);
3546 SET_TOP(slice);
3547 if (slice == NULL)
3548 goto error;
3549 DISPATCH();
3550 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003551
Benjamin Petersonddd19492018-09-16 22:38:02 -07003552 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003553 /* Handles f-string value formatting. */
3554 PyObject *result;
3555 PyObject *fmt_spec;
3556 PyObject *value;
3557 PyObject *(*conv_fn)(PyObject *);
3558 int which_conversion = oparg & FVC_MASK;
3559 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3560
3561 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003562 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003563
3564 /* See if any conversion is specified. */
3565 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003566 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003567 case FVC_STR: conv_fn = PyObject_Str; break;
3568 case FVC_REPR: conv_fn = PyObject_Repr; break;
3569 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003570 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02003571 _PyErr_Format(tstate, PyExc_SystemError,
3572 "unexpected conversion flag %d",
3573 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003574 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003575 }
3576
3577 /* If there's a conversion function, call it and replace
3578 value with that result. Otherwise, just use value,
3579 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003580 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003581 result = conv_fn(value);
3582 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003583 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003584 Py_XDECREF(fmt_spec);
3585 goto error;
3586 }
3587 value = result;
3588 }
3589
3590 /* If value is a unicode object, and there's no fmt_spec,
3591 then we know the result of format(value) is value
3592 itself. In that case, skip calling format(). I plan to
3593 move this optimization in to PyObject_Format()
3594 itself. */
3595 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3596 /* Do nothing, just transfer ownership to result. */
3597 result = value;
3598 } else {
3599 /* Actually call format(). */
3600 result = PyObject_Format(value, fmt_spec);
3601 Py_DECREF(value);
3602 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003603 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003604 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003605 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003606 }
3607
Eric V. Smith135d5f42016-02-05 18:23:08 -05003608 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003609 DISPATCH();
3610 }
3611
Benjamin Petersonddd19492018-09-16 22:38:02 -07003612 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003613 int oldoparg = oparg;
3614 NEXTOPARG();
3615 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003616 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003617 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003618
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003619
Antoine Pitrou042b1282010-08-13 21:15:58 +00003620#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003621 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003622#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003623 default:
3624 fprintf(stderr,
3625 "XXX lineno: %d, opcode: %d\n",
3626 PyFrame_GetLineNumber(f),
3627 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02003628 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003629 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003630
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003631 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003632
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003633 /* This should never be reached. Every opcode should end with DISPATCH()
3634 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003635 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003636
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003637error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003638 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003639#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02003640 if (!_PyErr_Occurred(tstate)) {
3641 _PyErr_SetString(tstate, PyExc_SystemError,
3642 "error return without exception set");
3643 }
Victor Stinner365b6932013-07-12 00:11:58 +02003644#else
Victor Stinner438a12d2019-05-24 17:01:38 +02003645 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02003646#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003647
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003648 /* Log traceback info. */
3649 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003650
Benjamin Peterson51f46162013-01-23 08:38:47 -05003651 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003652 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3653 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003654
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003655exception_unwind:
3656 /* Unwind stacks if an exception occurred */
3657 while (f->f_iblock > 0) {
3658 /* Pop the current block. */
3659 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003661 if (b->b_type == EXCEPT_HANDLER) {
3662 UNWIND_EXCEPT_HANDLER(b);
3663 continue;
3664 }
3665 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003666 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003667 PyObject *exc, *val, *tb;
3668 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003669 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003670 /* Beware, this invalidates all b->b_* fields */
3671 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003672 PUSH(exc_info->exc_traceback);
3673 PUSH(exc_info->exc_value);
3674 if (exc_info->exc_type != NULL) {
3675 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003676 }
3677 else {
3678 Py_INCREF(Py_None);
3679 PUSH(Py_None);
3680 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003681 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003682 /* Make the raw exception data
3683 available to the handler,
3684 so a program can emulate the
3685 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02003686 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003687 if (tb != NULL)
3688 PyException_SetTraceback(val, tb);
3689 else
3690 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003691 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003692 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003693 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003694 exc_info->exc_value = val;
3695 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003696 if (tb == NULL)
3697 tb = Py_None;
3698 Py_INCREF(tb);
3699 PUSH(tb);
3700 PUSH(val);
3701 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003702 JUMPTO(handler);
Victor Stinnerdab84232020-03-17 18:56:44 +01003703 if (_Py_TracingPossible(ceval2)) {
Pablo Galindo4c53e632020-01-10 09:24:22 +00003704 int needs_new_execution_window = (f->f_lasti < instr_lb || f->f_lasti >= instr_ub);
3705 int needs_line_update = (f->f_lasti == instr_lb || f->f_lasti < instr_prev);
3706 /* Make sure that we trace line after exception if we are in a new execution
3707 * window or we don't need a line update and we are not in the first instruction
3708 * of the line. */
3709 if (needs_new_execution_window || (!needs_line_update && instr_lb > 0)) {
3710 instr_prev = INT_MAX;
3711 }
Mark Shannonfee55262019-11-21 09:11:43 +00003712 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003713 /* Resume normal execution */
3714 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003715 }
3716 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003717
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003718 /* End the loop as we still have an error */
3719 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003720 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003721
Pablo Galindof00828a2019-05-09 16:52:02 +01003722 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003723 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01003724
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003725 /* Pop remaining stack entries. */
3726 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003727 PyObject *o = POP();
3728 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003729 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003730
Mark Shannone7c9f4a2020-01-13 12:51:26 +00003731exiting:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003732 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003733 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003734 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3735 tstate, f, PyTrace_RETURN, retval)) {
3736 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003737 }
3738 }
3739 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003740 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3741 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003742 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003743 }
3744 }
3745 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003746
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003747 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003748exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003749 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3750 dtrace_function_return(f);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01003751 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrou58720d62013-08-05 23:26:40 +02003752 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003753 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003754
Victor Stinner0b72b232020-03-12 23:18:39 +01003755 return _Py_CheckFunctionResult(tstate, NULL, retval, __func__);
Guido van Rossum374a9221991-04-04 10:40:29 +00003756}
3757
Benjamin Petersonb204a422011-06-05 22:04:07 -05003758static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003759format_missing(PyThreadState *tstate, const char *kind,
3760 PyCodeObject *co, PyObject *names)
Benjamin Petersone109c702011-06-24 09:37:26 -05003761{
3762 int err;
3763 Py_ssize_t len = PyList_GET_SIZE(names);
3764 PyObject *name_str, *comma, *tail, *tmp;
3765
3766 assert(PyList_CheckExact(names));
3767 assert(len >= 1);
3768 /* Deal with the joys of natural language. */
3769 switch (len) {
3770 case 1:
3771 name_str = PyList_GET_ITEM(names, 0);
3772 Py_INCREF(name_str);
3773 break;
3774 case 2:
3775 name_str = PyUnicode_FromFormat("%U and %U",
3776 PyList_GET_ITEM(names, len - 2),
3777 PyList_GET_ITEM(names, len - 1));
3778 break;
3779 default:
3780 tail = PyUnicode_FromFormat(", %U, and %U",
3781 PyList_GET_ITEM(names, len - 2),
3782 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003783 if (tail == NULL)
3784 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003785 /* Chop off the last two objects in the list. This shouldn't actually
3786 fail, but we can't be too careful. */
3787 err = PyList_SetSlice(names, len - 2, len, NULL);
3788 if (err == -1) {
3789 Py_DECREF(tail);
3790 return;
3791 }
3792 /* Stitch everything up into a nice comma-separated list. */
3793 comma = PyUnicode_FromString(", ");
3794 if (comma == NULL) {
3795 Py_DECREF(tail);
3796 return;
3797 }
3798 tmp = PyUnicode_Join(comma, names);
3799 Py_DECREF(comma);
3800 if (tmp == NULL) {
3801 Py_DECREF(tail);
3802 return;
3803 }
3804 name_str = PyUnicode_Concat(tmp, tail);
3805 Py_DECREF(tmp);
3806 Py_DECREF(tail);
3807 break;
3808 }
3809 if (name_str == NULL)
3810 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02003811 _PyErr_Format(tstate, PyExc_TypeError,
3812 "%U() missing %i required %s argument%s: %U",
3813 co->co_name,
3814 len,
3815 kind,
3816 len == 1 ? "" : "s",
3817 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05003818 Py_DECREF(name_str);
3819}
3820
3821static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003822missing_arguments(PyThreadState *tstate, PyCodeObject *co,
3823 Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003824 PyObject **fastlocals)
3825{
Victor Stinner74319ae2016-08-25 00:04:09 +02003826 Py_ssize_t i, j = 0;
3827 Py_ssize_t start, end;
3828 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003829 const char *kind = positional ? "positional" : "keyword-only";
3830 PyObject *missing_names;
3831
3832 /* Compute the names of the arguments that are missing. */
3833 missing_names = PyList_New(missing);
3834 if (missing_names == NULL)
3835 return;
3836 if (positional) {
3837 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01003838 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003839 }
3840 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003841 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003842 end = start + co->co_kwonlyargcount;
3843 }
3844 for (i = start; i < end; i++) {
3845 if (GETLOCAL(i) == NULL) {
3846 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3847 PyObject *name = PyObject_Repr(raw);
3848 if (name == NULL) {
3849 Py_DECREF(missing_names);
3850 return;
3851 }
3852 PyList_SET_ITEM(missing_names, j++, name);
3853 }
3854 }
3855 assert(j == missing);
Victor Stinner438a12d2019-05-24 17:01:38 +02003856 format_missing(tstate, kind, co, missing_names);
Benjamin Petersone109c702011-06-24 09:37:26 -05003857 Py_DECREF(missing_names);
3858}
3859
3860static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003861too_many_positional(PyThreadState *tstate, PyCodeObject *co,
3862 Py_ssize_t given, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003863 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003864{
3865 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003866 Py_ssize_t kwonly_given = 0;
3867 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003868 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003869 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003870
Benjamin Petersone109c702011-06-24 09:37:26 -05003871 assert((co->co_flags & CO_VARARGS) == 0);
3872 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01003873 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003874 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003875 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003876 }
3877 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003878 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01003879 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003880 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01003881 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003882 }
3883 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003884 plural = (co_argcount != 1);
3885 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003886 }
3887 if (sig == NULL)
3888 return;
3889 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003890 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3891 kwonly_sig = PyUnicode_FromFormat(format,
3892 given != 1 ? "s" : "",
3893 kwonly_given,
3894 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003895 if (kwonly_sig == NULL) {
3896 Py_DECREF(sig);
3897 return;
3898 }
3899 }
3900 else {
3901 /* This will not fail. */
3902 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003903 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003904 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003905 _PyErr_Format(tstate, PyExc_TypeError,
3906 "%U() takes %U positional argument%s but %zd%U %s given",
3907 co->co_name,
3908 sig,
3909 plural ? "s" : "",
3910 given,
3911 kwonly_sig,
3912 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003913 Py_DECREF(sig);
3914 Py_DECREF(kwonly_sig);
3915}
3916
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003917static int
Victor Stinner438a12d2019-05-24 17:01:38 +02003918positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
3919 Py_ssize_t kwcount, PyObject* const* kwnames)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003920{
3921 int posonly_conflicts = 0;
3922 PyObject* posonly_names = PyList_New(0);
3923
3924 for(int k=0; k < co->co_posonlyargcount; k++){
3925 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
3926
3927 for (int k2=0; k2<kwcount; k2++){
3928 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
3929 PyObject* kwname = kwnames[k2];
3930 if (kwname == posonly_name){
3931 if(PyList_Append(posonly_names, kwname) != 0) {
3932 goto fail;
3933 }
3934 posonly_conflicts++;
3935 continue;
3936 }
3937
3938 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
3939
3940 if ( cmp > 0) {
3941 if(PyList_Append(posonly_names, kwname) != 0) {
3942 goto fail;
3943 }
3944 posonly_conflicts++;
3945 } else if (cmp < 0) {
3946 goto fail;
3947 }
3948
3949 }
3950 }
3951 if (posonly_conflicts) {
3952 PyObject* comma = PyUnicode_FromString(", ");
3953 if (comma == NULL) {
3954 goto fail;
3955 }
3956 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
3957 Py_DECREF(comma);
3958 if (error_names == NULL) {
3959 goto fail;
3960 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003961 _PyErr_Format(tstate, PyExc_TypeError,
3962 "%U() got some positional-only arguments passed"
3963 " as keyword arguments: '%U'",
3964 co->co_name, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003965 Py_DECREF(error_names);
3966 goto fail;
3967 }
3968
3969 Py_DECREF(posonly_names);
3970 return 0;
3971
3972fail:
3973 Py_XDECREF(posonly_names);
3974 return 1;
3975
3976}
3977
Guido van Rossumc2e20742006-02-27 22:32:47 +00003978/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02003979 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003980 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003981
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003982PyObject *
Victor Stinnerb5e170f2019-11-16 01:03:22 +01003983_PyEval_EvalCode(PyThreadState *tstate,
3984 PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003985 PyObject *const *args, Py_ssize_t argcount,
3986 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003987 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003988 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003989 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003990 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003991{
Victor Stinnerb5e170f2019-11-16 01:03:22 +01003992 assert(tstate != NULL);
3993
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003994 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003995 PyFrameObject *f;
3996 PyObject *retval = NULL;
3997 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003998 PyObject *x, *u;
Pablo Galindocd74e662019-06-01 18:08:04 +01003999 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004000 Py_ssize_t i, j, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02004001 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00004002
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004003 if (globals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004004 _PyErr_SetString(tstate, PyExc_SystemError,
4005 "PyEval_EvalCodeEx: NULL globals");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004006 return NULL;
4007 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004008
Victor Stinnerc7020012016-08-16 23:40:29 +02004009 /* Create the frame */
INADA Naoki5a625d02016-12-24 20:19:08 +09004010 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02004011 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004012 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02004013 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004014 fastlocals = f->f_localsplus;
4015 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00004016
Victor Stinnerc7020012016-08-16 23:40:29 +02004017 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004018 if (co->co_flags & CO_VARKEYWORDS) {
4019 kwdict = PyDict_New();
4020 if (kwdict == NULL)
4021 goto fail;
4022 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02004023 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004024 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02004025 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004026 SETLOCAL(i, kwdict);
4027 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004028 else {
4029 kwdict = NULL;
4030 }
4031
Pablo Galindocd74e662019-06-01 18:08:04 +01004032 /* Copy all positional arguments into local variables */
4033 if (argcount > co->co_argcount) {
4034 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02004035 }
4036 else {
4037 n = argcount;
4038 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004039 for (j = 0; j < n; j++) {
4040 x = args[j];
4041 Py_INCREF(x);
4042 SETLOCAL(j, x);
4043 }
4044
Victor Stinnerc7020012016-08-16 23:40:29 +02004045 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004046 if (co->co_flags & CO_VARARGS) {
Sergey Fedoseev234531b2019-02-25 21:59:12 +05004047 u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02004048 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004049 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004050 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004051 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004052 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004053
Serhiy Storchakab7281052016-09-12 00:52:40 +03004054 /* Handle keyword arguments passed as two strided arrays */
4055 kwcount *= kwstep;
4056 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004057 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03004058 PyObject *keyword = kwnames[i];
4059 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02004060 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004061
Benjamin Petersonb204a422011-06-05 22:04:07 -05004062 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004063 _PyErr_Format(tstate, PyExc_TypeError,
4064 "%U() keywords must be strings",
4065 co->co_name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004066 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004067 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004068
Benjamin Petersonb204a422011-06-05 22:04:07 -05004069 /* Speed hack: do raw pointer compares. As names are
4070 normally interned this should almost always hit. */
4071 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004072 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004073 PyObject *name = co_varnames[j];
4074 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004075 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004076 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004077 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004078
Benjamin Petersonb204a422011-06-05 22:04:07 -05004079 /* Slow fallback, just in case */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004080 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004081 PyObject *name = co_varnames[j];
4082 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
4083 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004084 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004085 }
4086 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004087 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004088 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004089 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004090
Victor Stinner231d1f32017-01-11 02:12:06 +01004091 assert(j >= total_args);
4092 if (kwdict == NULL) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004093
Victor Stinner438a12d2019-05-24 17:01:38 +02004094 if (co->co_posonlyargcount
4095 && positional_only_passed_as_keyword(tstate, co,
4096 kwcount, kwnames))
4097 {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004098 goto fail;
4099 }
4100
Victor Stinner438a12d2019-05-24 17:01:38 +02004101 _PyErr_Format(tstate, PyExc_TypeError,
4102 "%U() got an unexpected keyword argument '%S'",
4103 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004104 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004105 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004106
Christian Heimes0bd447f2013-07-20 14:48:10 +02004107 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4108 goto fail;
4109 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004110 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004111
Benjamin Petersonb204a422011-06-05 22:04:07 -05004112 kw_found:
4113 if (GETLOCAL(j) != NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004114 _PyErr_Format(tstate, PyExc_TypeError,
4115 "%U() got multiple values for argument '%S'",
4116 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004117 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004118 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004119 Py_INCREF(value);
4120 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004121 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004122
4123 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004124 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004125 too_many_positional(tstate, co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004126 goto fail;
4127 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004128
4129 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004130 if (argcount < co->co_argcount) {
4131 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004132 Py_ssize_t missing = 0;
4133 for (i = argcount; i < m; i++) {
4134 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004135 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004136 }
4137 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004138 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004139 missing_arguments(tstate, co, missing, defcount, fastlocals);
Benjamin Petersone109c702011-06-24 09:37:26 -05004140 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004141 }
4142 if (n > m)
4143 i = n - m;
4144 else
4145 i = 0;
4146 for (; i < defcount; i++) {
4147 if (GETLOCAL(m+i) == NULL) {
4148 PyObject *def = defs[i];
4149 Py_INCREF(def);
4150 SETLOCAL(m+i, def);
4151 }
4152 }
4153 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004154
4155 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004156 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004157 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004158 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004159 PyObject *name;
4160 if (GETLOCAL(i) != NULL)
4161 continue;
4162 name = PyTuple_GET_ITEM(co->co_varnames, i);
4163 if (kwdefs != NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004164 PyObject *def = PyDict_GetItemWithError(kwdefs, name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004165 if (def) {
4166 Py_INCREF(def);
4167 SETLOCAL(i, def);
4168 continue;
4169 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004170 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004171 goto fail;
4172 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004173 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004174 missing++;
4175 }
4176 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004177 missing_arguments(tstate, co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004178 goto fail;
4179 }
4180 }
4181
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004182 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004183 vars into frame. */
4184 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004185 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004186 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004187 /* Possibly account for the cell variable being an argument. */
4188 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004189 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004190 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004191 /* Clear the local copy. */
4192 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004193 }
4194 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004195 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004196 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004197 if (c == NULL)
4198 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004199 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004200 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004201
4202 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004203 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4204 PyObject *o = PyTuple_GET_ITEM(closure, i);
4205 Py_INCREF(o);
4206 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004207 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004208
Yury Selivanoveb636452016-09-08 22:01:51 -07004209 /* Handle generator/coroutine/asynchronous generator */
4210 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004211 PyObject *gen;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004212 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004214 /* Don't need to keep the reference to f_back, it will be set
4215 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004216 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004218 /* Create a new generator that owns the ready to run frame
4219 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004220 if (is_coro) {
4221 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004222 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4223 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004224 } else {
4225 gen = PyGen_NewWithQualName(f, name, qualname);
4226 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004227 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004228 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004229 }
INADA Naoki9c157762016-12-26 18:52:46 +09004230
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004231 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004232
Yury Selivanov75445082015-05-11 22:57:16 -04004233 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004234 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004235
Victor Stinnerb9e68122019-11-14 12:20:46 +01004236 retval = _PyEval_EvalFrame(tstate, f, 0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004237
Thomas Woutersce272b62007-09-19 21:19:28 +00004238fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004240 /* decref'ing the frame can cause __del__ methods to get invoked,
4241 which can call back into Python. While we're done with the
4242 current Python frame (f), the associated C stack is still in use,
4243 so recursion_depth must be boosted for the duration.
4244 */
INADA Naoki5a625d02016-12-24 20:19:08 +09004245 if (Py_REFCNT(f) > 1) {
4246 Py_DECREF(f);
4247 _PyObject_GC_TRACK(f);
4248 }
4249 else {
4250 ++tstate->recursion_depth;
4251 Py_DECREF(f);
4252 --tstate->recursion_depth;
4253 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004254 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004255}
4256
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004257
4258PyObject *
4259_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
4260 PyObject *const *args, Py_ssize_t argcount,
4261 PyObject *const *kwnames, PyObject *const *kwargs,
4262 Py_ssize_t kwcount, int kwstep,
4263 PyObject *const *defs, Py_ssize_t defcount,
4264 PyObject *kwdefs, PyObject *closure,
4265 PyObject *name, PyObject *qualname)
4266{
4267 PyThreadState *tstate = _PyThreadState_GET();
4268 return _PyEval_EvalCode(tstate, _co, globals, locals,
4269 args, argcount,
4270 kwnames, kwargs,
4271 kwcount, kwstep,
4272 defs, defcount,
4273 kwdefs, closure,
4274 name, qualname);
4275}
4276
Victor Stinner40ee3012014-06-16 15:59:28 +02004277PyObject *
4278PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004279 PyObject *const *args, int argcount,
4280 PyObject *const *kws, int kwcount,
4281 PyObject *const *defs, int defcount,
4282 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004283{
4284 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004285 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004286 kws, kws != NULL ? kws + 1 : NULL,
4287 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004288 defs, defcount,
4289 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004290 NULL, NULL);
4291}
Tim Peters5ca576e2001-06-18 22:08:13 +00004292
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004293static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004294special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004295{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004296 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004297 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02004298 if (res == NULL && !_PyErr_Occurred(tstate)) {
4299 _PyErr_SetObject(tstate, PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004300 return NULL;
4301 }
4302 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004303}
4304
4305
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004306/* Logic for the raise statement (too complicated for inlining).
4307 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004308static int
Victor Stinner09532fe2019-05-10 23:39:09 +02004309do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004310{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004311 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004313 if (exc == NULL) {
4314 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01004315 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004316 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004317 type = exc_info->exc_type;
4318 value = exc_info->exc_value;
4319 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004320 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004321 _PyErr_SetString(tstate, PyExc_RuntimeError,
4322 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004323 return 0;
4324 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004325 Py_XINCREF(type);
4326 Py_XINCREF(value);
4327 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02004328 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004329 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004330 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004332 /* We support the following forms of raise:
4333 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004334 raise <instance>
4335 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004337 if (PyExceptionClass_Check(exc)) {
4338 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004339 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004340 if (value == NULL)
4341 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004342 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004343 _PyErr_Format(tstate, PyExc_TypeError,
4344 "calling %R should have returned an instance of "
4345 "BaseException, not %R",
4346 type, Py_TYPE(value));
4347 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004348 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004349 }
4350 else if (PyExceptionInstance_Check(exc)) {
4351 value = exc;
4352 type = PyExceptionInstance_Class(exc);
4353 Py_INCREF(type);
4354 }
4355 else {
4356 /* Not something you can raise. You get an exception
4357 anyway, just not what you specified :-) */
4358 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02004359 _PyErr_SetString(tstate, PyExc_TypeError,
4360 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004361 goto raise_error;
4362 }
Collin Winter828f04a2007-08-31 00:04:24 +00004363
Serhiy Storchakac0191582016-09-27 11:37:10 +03004364 assert(type != NULL);
4365 assert(value != NULL);
4366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004367 if (cause) {
4368 PyObject *fixed_cause;
4369 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004370 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004371 if (fixed_cause == NULL)
4372 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004373 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004374 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004375 else if (PyExceptionInstance_Check(cause)) {
4376 fixed_cause = cause;
4377 }
4378 else if (cause == Py_None) {
4379 Py_DECREF(cause);
4380 fixed_cause = NULL;
4381 }
4382 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004383 _PyErr_SetString(tstate, PyExc_TypeError,
4384 "exception causes must derive from "
4385 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004386 goto raise_error;
4387 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004388 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004389 }
Collin Winter828f04a2007-08-31 00:04:24 +00004390
Victor Stinner438a12d2019-05-24 17:01:38 +02004391 _PyErr_SetObject(tstate, type, value);
Victor Stinner61f4db82020-01-28 03:37:45 +01004392 /* _PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004393 Py_DECREF(value);
4394 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004395 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004396
4397raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004398 Py_XDECREF(value);
4399 Py_XDECREF(type);
4400 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004401 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004402}
4403
Tim Petersd6d010b2001-06-21 02:49:55 +00004404/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004405 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004406
Guido van Rossum0368b722007-05-11 16:50:42 +00004407 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4408 with a variable target.
4409*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004410
Barry Warsawe42b18f1997-08-25 22:13:04 +00004411static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004412unpack_iterable(PyThreadState *tstate, PyObject *v,
4413 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004414{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004415 int i = 0, j = 0;
4416 Py_ssize_t ll = 0;
4417 PyObject *it; /* iter(v) */
4418 PyObject *w;
4419 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004421 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004423 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004424 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004425 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01004426 Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v))
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004427 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004428 _PyErr_Format(tstate, PyExc_TypeError,
4429 "cannot unpack non-iterable %.200s object",
Victor Stinnera102ed72020-02-07 02:24:48 +01004430 Py_TYPE(v)->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004431 }
4432 return 0;
4433 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004435 for (; i < argcnt; i++) {
4436 w = PyIter_Next(it);
4437 if (w == NULL) {
4438 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004439 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04004440 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004441 _PyErr_Format(tstate, PyExc_ValueError,
4442 "not enough values to unpack "
4443 "(expected %d, got %d)",
4444 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004445 }
4446 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004447 _PyErr_Format(tstate, PyExc_ValueError,
4448 "not enough values to unpack "
4449 "(expected at least %d, got %d)",
4450 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004451 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004452 }
4453 goto Error;
4454 }
4455 *--sp = w;
4456 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004458 if (argcntafter == -1) {
4459 /* We better have exhausted the iterator now. */
4460 w = PyIter_Next(it);
4461 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004462 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004463 goto Error;
4464 Py_DECREF(it);
4465 return 1;
4466 }
4467 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02004468 _PyErr_Format(tstate, PyExc_ValueError,
4469 "too many values to unpack (expected %d)",
4470 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004471 goto Error;
4472 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004474 l = PySequence_List(it);
4475 if (l == NULL)
4476 goto Error;
4477 *--sp = l;
4478 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004480 ll = PyList_GET_SIZE(l);
4481 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004482 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04004483 "not enough values to unpack (expected at least %d, got %zd)",
4484 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004485 goto Error;
4486 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004488 /* Pop the "after-variable" args off the list. */
4489 for (j = argcntafter; j > 0; j--, i++) {
4490 *--sp = PyList_GET_ITEM(l, ll - j);
4491 }
4492 /* Resize the list. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +01004493 Py_SET_SIZE(l, ll - argcntafter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004494 Py_DECREF(it);
4495 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004496
Tim Petersd6d010b2001-06-21 02:49:55 +00004497Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004498 for (; i > 0; i--, sp++)
4499 Py_DECREF(*sp);
4500 Py_XDECREF(it);
4501 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004502}
4503
4504
Guido van Rossum96a42c81992-01-12 02:29:51 +00004505#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004506static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004507prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004508{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004509 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02004510 if (PyObject_Print(v, stdout, 0) != 0) {
4511 /* Don't know what else to do */
4512 _PyErr_Clear(tstate);
4513 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004514 printf("\n");
4515 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004516}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004517#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004518
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004519static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004520call_exc_trace(Py_tracefunc func, PyObject *self,
4521 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004522{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004523 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004524 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004525 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004526 if (value == NULL) {
4527 value = Py_None;
4528 Py_INCREF(value);
4529 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004530 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01004531 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004532 arg = PyTuple_Pack(3, type, value, traceback);
4533 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004534 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004535 return;
4536 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004537 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004538 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02004539 if (err == 0) {
4540 _PyErr_Restore(tstate, type, value, orig_traceback);
4541 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004542 else {
4543 Py_XDECREF(type);
4544 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004545 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004546 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004547}
4548
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004549static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004550call_trace_protected(Py_tracefunc func, PyObject *obj,
4551 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004552 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004553{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004554 PyObject *type, *value, *traceback;
4555 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004556 _PyErr_Fetch(tstate, &type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004557 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004558 if (err == 0)
4559 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004560 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004561 return 0;
4562 }
4563 else {
4564 Py_XDECREF(type);
4565 Py_XDECREF(value);
4566 Py_XDECREF(traceback);
4567 return -1;
4568 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004569}
4570
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004571static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004572call_trace(Py_tracefunc func, PyObject *obj,
4573 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004574 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004575{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004576 int result;
4577 if (tstate->tracing)
4578 return 0;
4579 tstate->tracing++;
4580 tstate->use_tracing = 0;
4581 result = func(obj, frame, what, arg);
4582 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4583 || (tstate->c_profilefunc != NULL));
4584 tstate->tracing--;
4585 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004586}
4587
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004588PyObject *
4589_PyEval_CallTracing(PyObject *func, PyObject *args)
4590{
Victor Stinner50b48572018-11-01 01:51:40 +01004591 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004592 int save_tracing = tstate->tracing;
4593 int save_use_tracing = tstate->use_tracing;
4594 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004595
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004596 tstate->tracing = 0;
4597 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4598 || (tstate->c_profilefunc != NULL));
4599 result = PyObject_Call(func, args, NULL);
4600 tstate->tracing = save_tracing;
4601 tstate->use_tracing = save_use_tracing;
4602 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004603}
4604
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004605/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004606static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004607maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004608 PyThreadState *tstate, PyFrameObject *frame,
4609 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004610{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004611 int result = 0;
4612 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004614 /* If the last instruction executed isn't in the current
4615 instruction window, reset the window.
4616 */
4617 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4618 PyAddrPair bounds;
4619 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4620 &bounds);
4621 *instr_lb = bounds.ap_lower;
4622 *instr_ub = bounds.ap_upper;
4623 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004624 /* If the last instruction falls at the start of a line or if it
4625 represents a jump backwards, update the frame's line number and
4626 then call the trace function if we're tracing source lines.
4627 */
4628 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004629 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004630 if (frame->f_trace_lines) {
4631 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4632 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004633 }
George King20faa682017-10-18 17:44:22 -07004634 /* Always emit an opcode event if we're tracing all opcodes. */
4635 if (frame->f_trace_opcodes) {
4636 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4637 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004638 *instr_prev = frame->f_lasti;
4639 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004640}
4641
Victor Stinner309d7cc2020-03-13 16:39:12 +01004642int
4643_PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
4644{
4645 assert(tstate != NULL);
4646 /* The caller must hold the GIL */
4647 assert(PyGILState_Check());
4648
4649 /* Call PySys_Audit() in the context of the current thread state,
4650 even if tstate is not the current thread state. */
4651 if (PySys_Audit("sys.setprofile", NULL) < 0) {
4652 return -1;
4653 }
4654
4655 PyObject *profileobj = tstate->c_profileobj;
4656
4657 tstate->c_profilefunc = NULL;
4658 tstate->c_profileobj = NULL;
4659 /* Must make sure that tracing is not ignored if 'profileobj' is freed */
4660 tstate->use_tracing = tstate->c_tracefunc != NULL;
4661 Py_XDECREF(profileobj);
4662
4663 Py_XINCREF(arg);
4664 tstate->c_profileobj = arg;
4665 tstate->c_profilefunc = func;
4666
4667 /* Flag that tracing or profiling is turned on */
4668 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
4669 return 0;
4670}
4671
Fred Drake5755ce62001-06-27 19:19:46 +00004672void
4673PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004674{
Victor Stinner309d7cc2020-03-13 16:39:12 +01004675 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01004676 if (_PyEval_SetProfile(tstate, func, arg) < 0) {
4677 /* Log PySys_Audit() error */
4678 _PyErr_WriteUnraisableMsg("in PyEval_SetProfile", NULL);
4679 }
Victor Stinner309d7cc2020-03-13 16:39:12 +01004680}
4681
4682int
4683_PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
4684{
4685 assert(tstate != NULL);
4686 /* The caller must hold the GIL */
4687 assert(PyGILState_Check());
4688
4689 /* Call PySys_Audit() in the context of the current thread state,
4690 even if tstate is not the current thread state. */
4691 if (PySys_Audit("sys.settrace", NULL) < 0) {
4692 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004693 }
4694
Victor Stinnerdab84232020-03-17 18:56:44 +01004695 struct _ceval_state *ceval = &tstate->interp->ceval;
Victor Stinner309d7cc2020-03-13 16:39:12 +01004696 PyObject *traceobj = tstate->c_traceobj;
4697 ceval->tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
4698
4699 tstate->c_tracefunc = NULL;
4700 tstate->c_traceobj = NULL;
4701 /* Must make sure that profiling is not ignored if 'traceobj' is freed */
4702 tstate->use_tracing = (tstate->c_profilefunc != NULL);
4703 Py_XDECREF(traceobj);
4704
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004705 Py_XINCREF(arg);
Victor Stinner309d7cc2020-03-13 16:39:12 +01004706 tstate->c_traceobj = arg;
4707 tstate->c_tracefunc = func;
4708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004709 /* Flag that tracing or profiling is turned on */
Victor Stinner309d7cc2020-03-13 16:39:12 +01004710 tstate->use_tracing = ((func != NULL)
4711 || (tstate->c_profilefunc != NULL));
4712
4713 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +00004714}
4715
4716void
4717PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4718{
Victor Stinner309d7cc2020-03-13 16:39:12 +01004719 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01004720 if (_PyEval_SetTrace(tstate, func, arg) < 0) {
4721 /* Log PySys_Audit() error */
4722 _PyErr_WriteUnraisableMsg("in PyEval_SetTrace", NULL);
4723 }
Fred Draked0838392001-06-16 21:02:31 +00004724}
4725
Victor Stinner309d7cc2020-03-13 16:39:12 +01004726
Yury Selivanov75445082015-05-11 22:57:16 -04004727void
Victor Stinner838f2642019-06-13 22:41:23 +02004728_PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004729{
4730 assert(new_depth >= 0);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004731 tstate->coroutine_origin_tracking_depth = new_depth;
4732}
4733
4734int
4735_PyEval_GetCoroutineOriginTrackingDepth(void)
4736{
Victor Stinner50b48572018-11-01 01:51:40 +01004737 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004738 return tstate->coroutine_origin_tracking_depth;
4739}
4740
4741void
Yury Selivanoveb636452016-09-08 22:01:51 -07004742_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4743{
Victor Stinner50b48572018-11-01 01:51:40 +01004744 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004745
4746 if (PySys_Audit("sys.set_asyncgen_hook_firstiter", NULL) < 0) {
4747 return;
4748 }
4749
Yury Selivanoveb636452016-09-08 22:01:51 -07004750 Py_XINCREF(firstiter);
4751 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4752}
4753
4754PyObject *
4755_PyEval_GetAsyncGenFirstiter(void)
4756{
Victor Stinner50b48572018-11-01 01:51:40 +01004757 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004758 return tstate->async_gen_firstiter;
4759}
4760
4761void
4762_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4763{
Victor Stinner50b48572018-11-01 01:51:40 +01004764 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004765
4766 if (PySys_Audit("sys.set_asyncgen_hook_finalizer", NULL) < 0) {
4767 return;
4768 }
4769
Yury Selivanoveb636452016-09-08 22:01:51 -07004770 Py_XINCREF(finalizer);
4771 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4772}
4773
4774PyObject *
4775_PyEval_GetAsyncGenFinalizer(void)
4776{
Victor Stinner50b48572018-11-01 01:51:40 +01004777 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004778 return tstate->async_gen_finalizer;
4779}
4780
Victor Stinner438a12d2019-05-24 17:01:38 +02004781static PyFrameObject *
4782_PyEval_GetFrame(PyThreadState *tstate)
4783{
Victor Stinner01b1cc12019-11-20 02:27:56 +01004784 _PyRuntimeState *runtime = tstate->interp->runtime;
4785 return runtime->gilstate.getframe(tstate);
Victor Stinner438a12d2019-05-24 17:01:38 +02004786}
4787
4788PyFrameObject *
4789PyEval_GetFrame(void)
4790{
4791 PyThreadState *tstate = _PyThreadState_GET();
4792 return _PyEval_GetFrame(tstate);
4793}
4794
Guido van Rossumb209a111997-04-29 18:18:01 +00004795PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004796PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004797{
Victor Stinner438a12d2019-05-24 17:01:38 +02004798 PyThreadState *tstate = _PyThreadState_GET();
4799 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004800 if (current_frame == NULL)
Victor Stinner438a12d2019-05-24 17:01:38 +02004801 return tstate->interp->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004802 else
4803 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004804}
4805
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004806/* Convenience function to get a builtin from its name */
4807PyObject *
4808_PyEval_GetBuiltinId(_Py_Identifier *name)
4809{
Victor Stinner438a12d2019-05-24 17:01:38 +02004810 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004811 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4812 if (attr) {
4813 Py_INCREF(attr);
4814 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004815 else if (!_PyErr_Occurred(tstate)) {
4816 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004817 }
4818 return attr;
4819}
4820
Guido van Rossumb209a111997-04-29 18:18:01 +00004821PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004822PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004823{
Victor Stinner438a12d2019-05-24 17:01:38 +02004824 PyThreadState *tstate = _PyThreadState_GET();
4825 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Victor Stinner41bb43a2013-10-29 01:19:37 +01004826 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004827 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004828 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004829 }
4830
Victor Stinner438a12d2019-05-24 17:01:38 +02004831 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01004832 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004833 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004834
4835 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004836 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004837}
4838
Guido van Rossumb209a111997-04-29 18:18:01 +00004839PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004840PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004841{
Victor Stinner438a12d2019-05-24 17:01:38 +02004842 PyThreadState *tstate = _PyThreadState_GET();
4843 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
4844 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004845 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004846 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004847
4848 assert(current_frame->f_globals != NULL);
4849 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004850}
4851
Guido van Rossum6135a871995-01-09 17:53:26 +00004852int
Tim Peters5ba58662001-07-16 02:29:45 +00004853PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004854{
Victor Stinner438a12d2019-05-24 17:01:38 +02004855 PyThreadState *tstate = _PyThreadState_GET();
4856 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004857 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004859 if (current_frame != NULL) {
4860 const int codeflags = current_frame->f_code->co_flags;
4861 const int compilerflags = codeflags & PyCF_MASK;
4862 if (compilerflags) {
4863 result = 1;
4864 cf->cf_flags |= compilerflags;
4865 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004866#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004867 if (codeflags & CO_GENERATOR_ALLOWED) {
4868 result = 1;
4869 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4870 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004871#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004872 }
4873 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004874}
4875
Guido van Rossum3f5da241990-12-20 15:06:42 +00004876
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004877const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004878PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004879{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004880 if (PyMethod_Check(func))
4881 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4882 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004883 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004884 else if (PyCFunction_Check(func))
4885 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4886 else
Victor Stinnera102ed72020-02-07 02:24:48 +01004887 return Py_TYPE(func)->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004888}
4889
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004890const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004891PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004892{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004893 if (PyMethod_Check(func))
4894 return "()";
4895 else if (PyFunction_Check(func))
4896 return "()";
4897 else if (PyCFunction_Check(func))
4898 return "()";
4899 else
4900 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004901}
4902
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004903#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004904if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004905 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4906 tstate, tstate->frame, \
4907 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004908 x = NULL; \
4909 } \
4910 else { \
4911 x = call; \
4912 if (tstate->c_profilefunc != NULL) { \
4913 if (x == NULL) { \
4914 call_trace_protected(tstate->c_profilefunc, \
4915 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004916 tstate, tstate->frame, \
4917 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004918 /* XXX should pass (type, value, tb) */ \
4919 } else { \
4920 if (call_trace(tstate->c_profilefunc, \
4921 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004922 tstate, tstate->frame, \
4923 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004924 Py_DECREF(x); \
4925 x = NULL; \
4926 } \
4927 } \
4928 } \
4929 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004930} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004931 x = call; \
4932 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004933
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004934
4935static PyObject *
4936trace_call_function(PyThreadState *tstate,
4937 PyObject *func,
4938 PyObject **args, Py_ssize_t nargs,
4939 PyObject *kwnames)
4940{
4941 PyObject *x;
4942 if (PyCFunction_Check(func)) {
Petr Viktorinffd97532020-02-11 17:46:57 +01004943 C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004944 return x;
4945 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06004946 else if (Py_IS_TYPE(func, &PyMethodDescr_Type) && nargs > 0) {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004947 /* We need to create a temporary bound method as argument
4948 for profiling.
4949
4950 If nargs == 0, then this cannot work because we have no
4951 "self". In any case, the call itself would raise
4952 TypeError (foo needs an argument), so we just skip
4953 profiling. */
4954 PyObject *self = args[0];
4955 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4956 if (func == NULL) {
4957 return NULL;
4958 }
Petr Viktorinffd97532020-02-11 17:46:57 +01004959 C_TRACE(x, PyObject_Vectorcall(func,
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02004960 args+1, nargs-1,
4961 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004962 Py_DECREF(func);
4963 return x;
4964 }
Petr Viktorinffd97532020-02-11 17:46:57 +01004965 return PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004966}
4967
Victor Stinner415c5102017-01-11 00:54:57 +01004968/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4969 to reduce the stack consumption. */
4970Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Victor Stinner09532fe2019-05-10 23:39:09 +02004971call_function(PyThreadState *tstate, PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004972{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004973 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004974 PyObject *func = *pfunc;
4975 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004976 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4977 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004978 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004979
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004980 if (tstate->use_tracing) {
4981 x = trace_call_function(tstate, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09004982 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004983 else {
Petr Viktorinffd97532020-02-11 17:46:57 +01004984 x = PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004985 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004986
Victor Stinner438a12d2019-05-24 17:01:38 +02004987 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004988
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004989 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004990 while ((*pp_stack) > pfunc) {
4991 w = EXT_POP(*pp_stack);
4992 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004993 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004995 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004996}
4997
Jeremy Hylton52820442001-01-03 23:52:36 +00004998static PyObject *
Victor Stinner09532fe2019-05-10 23:39:09 +02004999do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005000{
jdemeyere89de732018-09-19 12:06:20 +02005001 PyObject *result;
5002
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005003 if (PyCFunction_Check(func)) {
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +02005004 C_TRACE(result, PyObject_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005005 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005006 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005007 else if (Py_IS_TYPE(func, &PyMethodDescr_Type)) {
jdemeyere89de732018-09-19 12:06:20 +02005008 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
5009 if (nargs > 0 && tstate->use_tracing) {
5010 /* We need to create a temporary bound method as argument
5011 for profiling.
5012
5013 If nargs == 0, then this cannot work because we have no
5014 "self". In any case, the call itself would raise
5015 TypeError (foo needs an argument), so we just skip
5016 profiling. */
5017 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
5018 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5019 if (func == NULL) {
5020 return NULL;
5021 }
5022
Victor Stinner4d231bc2019-11-14 13:36:21 +01005023 C_TRACE(result, _PyObject_FastCallDictTstate(
5024 tstate, func,
5025 &_PyTuple_ITEMS(callargs)[1],
5026 nargs - 1,
5027 kwdict));
jdemeyere89de732018-09-19 12:06:20 +02005028 Py_DECREF(func);
5029 return result;
5030 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005031 }
jdemeyere89de732018-09-19 12:06:20 +02005032 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00005033}
5034
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005035/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005036 nb_index slot defined, and store in *pi.
5037 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08005038 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00005039 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005040*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005041int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005042_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005043{
Victor Stinner438a12d2019-05-24 17:01:38 +02005044 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005045 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005046 Py_ssize_t x;
5047 if (PyIndex_Check(v)) {
5048 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005049 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005050 return 0;
5051 }
5052 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005053 _PyErr_SetString(tstate, PyExc_TypeError,
5054 "slice indices must be integers or "
5055 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005056 return 0;
5057 }
5058 *pi = x;
5059 }
5060 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005061}
5062
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005063int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005064_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005065{
Victor Stinner438a12d2019-05-24 17:01:38 +02005066 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005067 Py_ssize_t x;
5068 if (PyIndex_Check(v)) {
5069 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005070 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005071 return 0;
5072 }
5073 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005074 _PyErr_SetString(tstate, PyExc_TypeError,
5075 "slice indices must be integers or "
5076 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005077 return 0;
5078 }
5079 *pi = x;
5080 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005081}
5082
Thomas Wouters52152252000-08-17 22:55:00 +00005083static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005084import_name(PyThreadState *tstate, PyFrameObject *f,
5085 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005086{
5087 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005088 PyObject *import_func, *res;
5089 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005090
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005091 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005092 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005093 if (!_PyErr_Occurred(tstate)) {
5094 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005095 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005096 return NULL;
5097 }
5098
5099 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005100 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005101 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02005102 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005103 return NULL;
5104 }
5105 res = PyImport_ImportModuleLevelObject(
5106 name,
5107 f->f_globals,
5108 f->f_locals == NULL ? Py_None : f->f_locals,
5109 fromlist,
5110 ilevel);
5111 return res;
5112 }
5113
5114 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005115
5116 stack[0] = name;
5117 stack[1] = f->f_globals;
5118 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5119 stack[3] = fromlist;
5120 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005121 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005122 Py_DECREF(import_func);
5123 return res;
5124}
5125
5126static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005127import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005128{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005129 PyObject *x;
Xiang Zhang4830f582017-03-21 11:13:42 +08005130 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005131
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005132 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005133 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005134 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005135 /* Issue #17636: in case this failed because of a circular relative
5136 import, try to fallback on reading the module directly from
5137 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02005138 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005139 if (pkgname == NULL) {
5140 goto error;
5141 }
Oren Milman6db70332017-09-19 14:23:01 +03005142 if (!PyUnicode_Check(pkgname)) {
5143 Py_CLEAR(pkgname);
5144 goto error;
5145 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005146 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005147 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005148 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005149 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005150 }
Eric Snow3f9eee62017-09-15 16:35:20 -06005151 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005152 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02005153 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07005154 goto error;
5155 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005156 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005157 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005158 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005159 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005160 if (pkgname == NULL) {
5161 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5162 if (pkgname_or_unknown == NULL) {
5163 Py_XDECREF(pkgpath);
5164 return NULL;
5165 }
5166 } else {
5167 pkgname_or_unknown = pkgname;
5168 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005169
5170 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005171 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08005172 errmsg = PyUnicode_FromFormat(
5173 "cannot import name %R from %R (unknown location)",
5174 name, pkgname_or_unknown
5175 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005176 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005177 PyErr_SetImportError(errmsg, pkgname, NULL);
5178 }
5179 else {
Anthony Sottile65366bc2019-09-09 08:17:50 -07005180 _Py_IDENTIFIER(__spec__);
5181 PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
Anthony Sottile65366bc2019-09-09 08:17:50 -07005182 const char *fmt =
5183 _PyModuleSpec_IsInitializing(spec) ?
5184 "cannot import name %R from partially initialized module %R "
5185 "(most likely due to a circular import) (%S)" :
5186 "cannot import name %R from %R (%S)";
5187 Py_XDECREF(spec);
5188
5189 errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
Stefan Krah027b09c2019-03-25 21:50:58 +01005190 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005191 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005192 }
5193
Xiang Zhang4830f582017-03-21 11:13:42 +08005194 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005195 Py_XDECREF(pkgname_or_unknown);
5196 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005197 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005198}
Guido van Rossumac7be682001-01-17 15:42:30 +00005199
Thomas Wouters52152252000-08-17 22:55:00 +00005200static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005201import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00005202{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005203 _Py_IDENTIFIER(__all__);
5204 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005205 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005206 int skip_leading_underscores = 0;
5207 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005208
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005209 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5210 return -1; /* Unexpected error */
5211 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005212 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005213 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5214 return -1;
5215 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005216 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005217 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005218 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005219 return -1;
5220 }
5221 all = PyMapping_Keys(dict);
5222 Py_DECREF(dict);
5223 if (all == NULL)
5224 return -1;
5225 skip_leading_underscores = 1;
5226 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005227
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005228 for (pos = 0, err = 0; ; pos++) {
5229 name = PySequence_GetItem(all, pos);
5230 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005231 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005232 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02005233 }
5234 else {
5235 _PyErr_Clear(tstate);
5236 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005237 break;
5238 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005239 if (!PyUnicode_Check(name)) {
5240 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5241 if (modname == NULL) {
5242 Py_DECREF(name);
5243 err = -1;
5244 break;
5245 }
5246 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005247 _PyErr_Format(tstate, PyExc_TypeError,
5248 "module __name__ must be a string, not %.100s",
5249 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005250 }
5251 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005252 _PyErr_Format(tstate, PyExc_TypeError,
5253 "%s in %U.%s must be str, not %.100s",
5254 skip_leading_underscores ? "Key" : "Item",
5255 modname,
5256 skip_leading_underscores ? "__dict__" : "__all__",
5257 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005258 }
5259 Py_DECREF(modname);
5260 Py_DECREF(name);
5261 err = -1;
5262 break;
5263 }
5264 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005265 if (PyUnicode_READY(name) == -1) {
5266 Py_DECREF(name);
5267 err = -1;
5268 break;
5269 }
5270 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5271 Py_DECREF(name);
5272 continue;
5273 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005274 }
5275 value = PyObject_GetAttr(v, name);
5276 if (value == NULL)
5277 err = -1;
5278 else if (PyDict_CheckExact(locals))
5279 err = PyDict_SetItem(locals, name, value);
5280 else
5281 err = PyObject_SetItem(locals, name, value);
5282 Py_DECREF(name);
5283 Py_XDECREF(value);
5284 if (err != 0)
5285 break;
5286 }
5287 Py_DECREF(all);
5288 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005289}
5290
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005291static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005292check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005293{
Victor Stinnera102ed72020-02-07 02:24:48 +01005294 if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005295 /* check_args_iterable() may be called with a live exception:
5296 * clear it to prevent calling _PyObject_FunctionStr() with an
5297 * exception set. */
Victor Stinner61f4db82020-01-28 03:37:45 +01005298 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005299 PyObject *funcstr = _PyObject_FunctionStr(func);
5300 if (funcstr != NULL) {
5301 _PyErr_Format(tstate, PyExc_TypeError,
5302 "%U argument after * must be an iterable, not %.200s",
5303 funcstr, Py_TYPE(args)->tp_name);
5304 Py_DECREF(funcstr);
5305 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005306 return -1;
5307 }
5308 return 0;
5309}
5310
5311static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005312format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005313{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005314 /* _PyDict_MergeEx raises attribute
5315 * error (percolated from an attempt
5316 * to get 'keys' attribute) instead of
5317 * a type error if its second argument
5318 * is not a mapping.
5319 */
Victor Stinner438a12d2019-05-24 17:01:38 +02005320 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005321 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005322 PyObject *funcstr = _PyObject_FunctionStr(func);
5323 if (funcstr != NULL) {
5324 _PyErr_Format(
5325 tstate, PyExc_TypeError,
5326 "%U argument after ** must be a mapping, not %.200s",
5327 funcstr, Py_TYPE(kwargs)->tp_name);
5328 Py_DECREF(funcstr);
5329 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005330 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005331 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005332 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02005333 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005334 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005335 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005336 PyObject *funcstr = _PyObject_FunctionStr(func);
5337 if (funcstr != NULL) {
5338 PyObject *key = PyTuple_GET_ITEM(val, 0);
5339 _PyErr_Format(
5340 tstate, PyExc_TypeError,
5341 "%U got multiple values for keyword argument '%S'",
5342 funcstr, key);
5343 Py_DECREF(funcstr);
5344 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005345 Py_XDECREF(exc);
5346 Py_XDECREF(val);
5347 Py_XDECREF(tb);
5348 }
5349 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005350 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005351 }
5352 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005353}
5354
Guido van Rossumac7be682001-01-17 15:42:30 +00005355static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005356format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
5357 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005358{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005359 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005361 if (!obj)
5362 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005363
Serhiy Storchaka06515832016-11-20 09:13:07 +02005364 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005365 if (!obj_str)
5366 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005367
Victor Stinner438a12d2019-05-24 17:01:38 +02005368 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005369}
Guido van Rossum950361c1997-01-24 13:49:28 +00005370
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005371static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005372format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005373{
5374 PyObject *name;
5375 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02005376 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005377 return;
5378 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5379 name = PyTuple_GET_ITEM(co->co_cellvars,
5380 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005381 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005382 PyExc_UnboundLocalError,
5383 UNBOUNDLOCAL_ERROR_MSG,
5384 name);
5385 } else {
5386 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5387 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02005388 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005389 UNBOUNDFREE_ERROR_MSG, name);
5390 }
5391}
5392
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005393static void
Mark Shannonfee55262019-11-21 09:11:43 +00005394format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevprevopcode, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005395{
5396 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5397 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005398 _PyErr_Format(tstate, PyExc_TypeError,
5399 "'async with' received an object from __aenter__ "
5400 "that does not implement __await__: %.100s",
5401 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005402 }
Mark Shannonfee55262019-11-21 09:11:43 +00005403 else if (prevopcode == WITH_EXCEPT_START || (prevopcode == CALL_FUNCTION && prevprevopcode == DUP_TOP)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005404 _PyErr_Format(tstate, PyExc_TypeError,
5405 "'async with' received an object from __aexit__ "
5406 "that does not implement __await__: %.100s",
5407 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005408 }
5409 }
5410}
5411
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005412static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005413unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005414 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005415{
5416 PyObject *res;
5417 if (Py_REFCNT(v) == 2) {
5418 /* In the common case, there are 2 references to the value
5419 * stored in 'variable' when the += is performed: one on the
5420 * value stack (in 'v') and one still stored in the
5421 * 'variable'. We try to delete the variable now to reduce
5422 * the refcnt to 1.
5423 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005424 int opcode, oparg;
5425 NEXTOPARG();
5426 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005427 case STORE_FAST:
5428 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005429 PyObject **fastlocals = f->f_localsplus;
5430 if (GETLOCAL(oparg) == v)
5431 SETLOCAL(oparg, NULL);
5432 break;
5433 }
5434 case STORE_DEREF:
5435 {
5436 PyObject **freevars = (f->f_localsplus +
5437 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005438 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005439 if (PyCell_GET(c) == v) {
5440 PyCell_SET(c, NULL);
5441 Py_DECREF(v);
5442 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005443 break;
5444 }
5445 case STORE_NAME:
5446 {
5447 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005448 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005449 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005450 if (locals && PyDict_CheckExact(locals)) {
5451 PyObject *w = PyDict_GetItemWithError(locals, name);
5452 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02005453 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005454 {
5455 Py_DECREF(v);
5456 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005457 }
5458 }
5459 break;
5460 }
5461 }
5462 }
5463 res = v;
5464 PyUnicode_Append(&res, w);
5465 return res;
5466}
5467
Guido van Rossum950361c1997-01-24 13:49:28 +00005468#ifdef DYNAMIC_EXECUTION_PROFILE
5469
Skip Montanarof118cb12001-10-15 20:51:38 +00005470static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005471getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005472{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005473 int i;
5474 PyObject *l = PyList_New(256);
5475 if (l == NULL) return NULL;
5476 for (i = 0; i < 256; i++) {
5477 PyObject *x = PyLong_FromLong(a[i]);
5478 if (x == NULL) {
5479 Py_DECREF(l);
5480 return NULL;
5481 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005482 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005483 }
5484 for (i = 0; i < 256; i++)
5485 a[i] = 0;
5486 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005487}
5488
5489PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005490_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005491{
5492#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005493 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005494#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005495 int i;
5496 PyObject *l = PyList_New(257);
5497 if (l == NULL) return NULL;
5498 for (i = 0; i < 257; i++) {
5499 PyObject *x = getarray(dxpairs[i]);
5500 if (x == NULL) {
5501 Py_DECREF(l);
5502 return NULL;
5503 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005504 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005505 }
5506 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005507#endif
5508}
5509
5510#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005511
5512Py_ssize_t
5513_PyEval_RequestCodeExtraIndex(freefunc free)
5514{
Victor Stinnercaba55b2018-08-03 15:33:52 +02005515 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -07005516 Py_ssize_t new_index;
5517
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005518 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005519 return -1;
5520 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005521 new_index = interp->co_extra_user_count++;
5522 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005523 return new_index;
5524}
Łukasz Langaa785c872016-09-09 17:37:37 -07005525
5526static void
5527dtrace_function_entry(PyFrameObject *f)
5528{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005529 const char *filename;
5530 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005531 int lineno;
5532
5533 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5534 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5535 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5536
Andy Lestere6be9b52020-02-11 20:28:35 -06005537 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005538}
5539
5540static void
5541dtrace_function_return(PyFrameObject *f)
5542{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005543 const char *filename;
5544 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005545 int lineno;
5546
5547 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5548 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5549 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5550
Andy Lestere6be9b52020-02-11 20:28:35 -06005551 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005552}
5553
5554/* DTrace equivalent of maybe_call_line_trace. */
5555static void
5556maybe_dtrace_line(PyFrameObject *frame,
5557 int *instr_lb, int *instr_ub, int *instr_prev)
5558{
5559 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005560 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005561
5562 /* If the last instruction executed isn't in the current
5563 instruction window, reset the window.
5564 */
5565 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5566 PyAddrPair bounds;
5567 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5568 &bounds);
5569 *instr_lb = bounds.ap_lower;
5570 *instr_ub = bounds.ap_upper;
5571 }
5572 /* If the last instruction falls at the start of a line or if
5573 it represents a jump backwards, update the frame's line
5574 number and call the trace function. */
5575 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5576 frame->f_lineno = line;
5577 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5578 if (!co_filename)
5579 co_filename = "?";
5580 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5581 if (!co_name)
5582 co_name = "?";
Andy Lestere6be9b52020-02-11 20:28:35 -06005583 PyDTrace_LINE(co_filename, co_name, line);
Łukasz Langaa785c872016-09-09 17:37:37 -07005584 }
5585 *instr_prev = frame->f_lasti;
5586}
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005587
5588
5589/* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions
5590 for the limited API. */
5591
5592#undef Py_EnterRecursiveCall
5593
5594int Py_EnterRecursiveCall(const char *where)
5595{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005596 return _Py_EnterRecursiveCall_inline(where);
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005597}
5598
5599#undef Py_LeaveRecursiveCall
5600
5601void Py_LeaveRecursiveCall(void)
5602{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005603 _Py_LeaveRecursiveCall_inline();
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005604}