blob: 2f65ea28e7abf4017828c460dea73245a9ea750e [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Thomas Wouters477c8d52006-05-27 19:21:47 +00009/* enable more aggressive intra-module optimizations, where available */
10#define PY_LOCAL_AGGRESSIVE
11
Guido van Rossumb209a111997-04-29 18:18:01 +000012#include "Python.h"
Victor Stinner4d231bc2019-11-14 13:36:21 +010013#include "pycore_call.h"
Victor Stinner09532fe2019-05-10 23:39:09 +020014#include "pycore_ceval.h"
Inada Naoki91234a12019-06-03 21:30:58 +090015#include "pycore_code.h"
Victor Stinner111e4ee2020-03-09 21:24:14 +010016#include "pycore_initconfig.h"
Victor Stinnerbcda8f12018-11-21 22:27:47 +010017#include "pycore_object.h"
Victor Stinner438a12d2019-05-24 17:01:38 +020018#include "pycore_pyerrors.h"
19#include "pycore_pylifecycle.h"
Victor Stinner621cebe2018-11-12 16:53:38 +010020#include "pycore_pystate.h"
Victor Stinnerec13b932018-11-25 23:56:17 +010021#include "pycore_tupleobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000022
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000023#include "code.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040024#include "dictobject.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000025#include "frameobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000026#include "opcode.h"
Łukasz Langaa785c872016-09-09 17:37:37 -070027#include "pydtrace.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040028#include "setobject.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000029#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000030
Guido van Rossumc6004111993-11-05 10:22:19 +000031#include <ctype.h>
32
Guido van Rossum408027e1996-12-30 16:17:54 +000033#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000034/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000035#define LLTRACE 1 /* Low-level trace feature */
36#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000037#endif
38
Victor Stinner5c75f372019-04-17 23:02:26 +020039#if !defined(Py_BUILD_CORE)
40# error "ceval.c must be build with Py_BUILD_CORE define for best performance"
41#endif
42
Hai Shi46874c22020-01-30 17:20:25 -060043_Py_IDENTIFIER(__name__);
Guido van Rossum5b722181993-03-30 17:46:03 +000044
Guido van Rossum374a9221991-04-04 10:40:29 +000045/* Forward declarations */
Victor Stinner09532fe2019-05-10 23:39:09 +020046Py_LOCAL_INLINE(PyObject *) call_function(
47 PyThreadState *tstate, PyObject ***pp_stack,
48 Py_ssize_t oparg, PyObject *kwnames);
49static PyObject * do_call_core(
50 PyThreadState *tstate, PyObject *func,
51 PyObject *callargs, PyObject *kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +000052
Guido van Rossum0a066c01992-03-27 17:29:15 +000053#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000054static int lltrace;
Victor Stinner438a12d2019-05-24 17:01:38 +020055static int prtrace(PyThreadState *, PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000056#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010057static int call_trace(Py_tracefunc, PyObject *,
58 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000060static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010061 PyThreadState *, PyFrameObject *,
62 int, PyObject *);
63static void call_exc_trace(Py_tracefunc, PyObject *,
64 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000065static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Eric Snow2ebc5ce2017-09-07 23:51:28 -060066 PyThreadState *, PyFrameObject *,
67 int *, int *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070068static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *);
69static void dtrace_function_entry(PyFrameObject *);
70static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000071
Victor Stinner438a12d2019-05-24 17:01:38 +020072static PyObject * import_name(PyThreadState *, PyFrameObject *,
73 PyObject *, PyObject *, PyObject *);
74static PyObject * import_from(PyThreadState *, PyObject *, PyObject *);
75static int import_all_from(PyThreadState *, PyObject *, PyObject *);
76static void format_exc_check_arg(PyThreadState *, PyObject *, const char *, PyObject *);
77static void format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg);
78static PyObject * unicode_concatenate(PyThreadState *, PyObject *, PyObject *,
Serhiy Storchakaab874002016-09-11 13:48:15 +030079 PyFrameObject *, const _Py_CODEUNIT *);
Victor Stinner438a12d2019-05-24 17:01:38 +020080static PyObject * special_lookup(PyThreadState *, PyObject *, _Py_Identifier *);
81static int check_args_iterable(PyThreadState *, PyObject *func, PyObject *vararg);
82static void format_kwargs_error(PyThreadState *, PyObject *func, PyObject *kwargs);
Mark Shannonfee55262019-11-21 09:11:43 +000083static void format_awaitable_error(PyThreadState *, PyTypeObject *, int, int);
Guido van Rossum374a9221991-04-04 10:40:29 +000084
Paul Prescode68140d2000-08-30 20:25:01 +000085#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000087#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000089#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 "free variable '%.200s' referenced before assignment" \
91 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000092
Guido van Rossum950361c1997-01-24 13:49:28 +000093/* Dynamic execution profile */
94#ifdef DYNAMIC_EXECUTION_PROFILE
95#ifdef DXPAIRS
96static long dxpairs[257][256];
97#define dxp dxpairs[256]
98#else
99static long dxp[256];
100#endif
101#endif
102
Inada Naoki91234a12019-06-03 21:30:58 +0900103/* per opcode cache */
Inada Naokieddef862019-06-04 07:38:10 +0900104#ifdef Py_DEBUG
105// --with-pydebug is used to find memory leak. opcache makes it harder.
106// So we disable opcache when Py_DEBUG is defined.
107// See bpo-37146
108#define OPCACHE_MIN_RUNS 0 /* disable opcache */
109#else
Inada Naoki91234a12019-06-03 21:30:58 +0900110#define OPCACHE_MIN_RUNS 1024 /* create opcache when code executed this time */
Inada Naokieddef862019-06-04 07:38:10 +0900111#endif
Inada Naoki91234a12019-06-03 21:30:58 +0900112#define OPCACHE_STATS 0 /* Enable stats */
113
114#if OPCACHE_STATS
115static size_t opcache_code_objects = 0;
116static size_t opcache_code_objects_extra_mem = 0;
117
118static size_t opcache_global_opts = 0;
119static size_t opcache_global_hits = 0;
120static size_t opcache_global_misses = 0;
121#endif
122
Victor Stinnere225beb2019-06-03 18:14:24 +0200123#define GIL_REQUEST _Py_atomic_load_relaxed(&ceval->gil_drop_request)
Inada Naoki91234a12019-06-03 21:30:58 +0900124
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000125/* This can set eval_breaker to 0 even though gil_drop_request became
126 1. We believe this is all right because the eval loop will release
127 the GIL eventually anyway. */
Victor Stinnere225beb2019-06-03 18:14:24 +0200128#define COMPUTE_EVAL_BREAKER(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 _Py_atomic_store_relaxed( \
Victor Stinnere225beb2019-06-03 18:14:24 +0200130 &(ceval)->eval_breaker, \
131 GIL_REQUEST | \
132 _Py_atomic_load_relaxed(&(ceval)->signals_pending) | \
133 _Py_atomic_load_relaxed(&(ceval)->pending.calls_to_do) | \
134 (ceval)->pending.async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000135
Victor Stinnere225beb2019-06-03 18:14:24 +0200136#define SET_GIL_DROP_REQUEST(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200138 _Py_atomic_store_relaxed(&(ceval)->gil_drop_request, 1); \
139 _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000141
Victor Stinnere225beb2019-06-03 18:14:24 +0200142#define RESET_GIL_DROP_REQUEST(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000143 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200144 _Py_atomic_store_relaxed(&(ceval)->gil_drop_request, 0); \
145 COMPUTE_EVAL_BREAKER(ceval); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000147
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000148/* Pending calls are only modified under pending_lock */
Victor Stinnere225beb2019-06-03 18:14:24 +0200149#define SIGNAL_PENDING_CALLS(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200151 _Py_atomic_store_relaxed(&(ceval)->pending.calls_to_do, 1); \
152 _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000154
Victor Stinnere225beb2019-06-03 18:14:24 +0200155#define UNSIGNAL_PENDING_CALLS(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200157 _Py_atomic_store_relaxed(&(ceval)->pending.calls_to_do, 0); \
158 COMPUTE_EVAL_BREAKER(ceval); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000159 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000160
Victor Stinnere225beb2019-06-03 18:14:24 +0200161#define SIGNAL_PENDING_SIGNALS(ceval) \
Eric Snowfdf282d2019-01-11 14:26:55 -0700162 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200163 _Py_atomic_store_relaxed(&(ceval)->signals_pending, 1); \
164 _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \
Eric Snowfdf282d2019-01-11 14:26:55 -0700165 } while (0)
166
Victor Stinnere225beb2019-06-03 18:14:24 +0200167#define UNSIGNAL_PENDING_SIGNALS(ceval) \
Eric Snowfdf282d2019-01-11 14:26:55 -0700168 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200169 _Py_atomic_store_relaxed(&(ceval)->signals_pending, 0); \
170 COMPUTE_EVAL_BREAKER(ceval); \
Eric Snowfdf282d2019-01-11 14:26:55 -0700171 } while (0)
172
Victor Stinnere225beb2019-06-03 18:14:24 +0200173#define SIGNAL_ASYNC_EXC(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200175 (ceval)->pending.async_exc = 1; \
176 _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000178
Victor Stinnere225beb2019-06-03 18:14:24 +0200179#define UNSIGNAL_ASYNC_EXC(ceval) \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600180 do { \
Victor Stinnere225beb2019-06-03 18:14:24 +0200181 (ceval)->pending.async_exc = 0; \
182 COMPUTE_EVAL_BREAKER(ceval); \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600183 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000184
185
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000186#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000187#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000188#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000189#include "pythread.h"
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000190#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000191
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100192static void
193ensure_tstate_not_null(const char *func, PyThreadState *tstate)
194{
195 if (tstate == NULL) {
Victor Stinner23ef89d2020-03-18 02:26:04 +0100196 _Py_FatalErrorFunc(func,
197 "current thread state is NULL (released GIL?)");
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100198 }
199}
200
201
Tim Peters7f468f22004-10-11 02:40:51 +0000202int
Victor Stinner175a7042020-03-10 00:37:48 +0100203_PyEval_ThreadsInitialized(_PyRuntimeState *runtime)
204{
205 return gil_created(&runtime->ceval.gil);
206}
207
208int
Tim Peters7f468f22004-10-11 02:40:51 +0000209PyEval_ThreadsInitialized(void)
210{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100211 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner175a7042020-03-10 00:37:48 +0100212 return _PyEval_ThreadsInitialized(runtime);
Tim Peters7f468f22004-10-11 02:40:51 +0000213}
214
Victor Stinner111e4ee2020-03-09 21:24:14 +0100215PyStatus
216_PyEval_InitThreads(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000217{
Victor Stinner111e4ee2020-03-09 21:24:14 +0100218 if (tstate == NULL) {
219 return _PyStatus_ERR("tstate is NULL");
220 }
221
222 struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
Victor Stinnere225beb2019-06-03 18:14:24 +0200223 struct _gil_runtime_state *gil = &ceval->gil;
Victor Stinner09532fe2019-05-10 23:39:09 +0200224 if (gil_created(gil)) {
Victor Stinner111e4ee2020-03-09 21:24:14 +0100225 return _PyStatus_OK();
Victor Stinnera7126792019-03-19 14:19:38 +0100226 }
227
Inada Naoki001fee12019-02-20 10:00:09 +0900228 PyThread_init_thread();
Victor Stinner09532fe2019-05-10 23:39:09 +0200229 create_gil(gil);
Victor Stinner85f5a692020-03-09 22:12:04 +0100230
231 take_gil(tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700232
Victor Stinnere225beb2019-06-03 18:14:24 +0200233 struct _pending_calls *pending = &ceval->pending;
234 pending->lock = PyThread_allocate_lock();
235 if (pending->lock == NULL) {
Victor Stinner111e4ee2020-03-09 21:24:14 +0100236 return _PyStatus_NO_MEMORY();
237 }
Victor Stinner85f5a692020-03-09 22:12:04 +0100238
Victor Stinner111e4ee2020-03-09 21:24:14 +0100239 return _PyStatus_OK();
240}
241
242void
243PyEval_InitThreads(void)
244{
Victor Stinnerb4698ec2020-03-10 01:28:54 +0100245 /* Do nothing: kept for backward compatibility */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000246}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000247
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000248void
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100249_PyEval_FiniThreads(PyThreadState *tstate)
Antoine Pitrou1df15362010-09-13 14:16:46 +0000250{
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100251 struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
Victor Stinnere225beb2019-06-03 18:14:24 +0200252 struct _gil_runtime_state *gil = &ceval->gil;
Victor Stinner09532fe2019-05-10 23:39:09 +0200253 if (!gil_created(gil)) {
Antoine Pitrou1df15362010-09-13 14:16:46 +0000254 return;
Victor Stinnera7126792019-03-19 14:19:38 +0100255 }
256
Victor Stinner09532fe2019-05-10 23:39:09 +0200257 destroy_gil(gil);
258 assert(!gil_created(gil));
Victor Stinner99fcc612019-04-29 13:04:07 +0200259
Victor Stinnere225beb2019-06-03 18:14:24 +0200260 struct _pending_calls *pending = &ceval->pending;
261 if (pending->lock != NULL) {
262 PyThread_free_lock(pending->lock);
263 pending->lock = NULL;
264 }
Antoine Pitrou1df15362010-09-13 14:16:46 +0000265}
266
267void
Inada Naoki91234a12019-06-03 21:30:58 +0900268_PyEval_Fini(void)
269{
270#if OPCACHE_STATS
271 fprintf(stderr, "-- Opcode cache number of objects = %zd\n",
272 opcache_code_objects);
273
274 fprintf(stderr, "-- Opcode cache total extra mem = %zd\n",
275 opcache_code_objects_extra_mem);
276
277 fprintf(stderr, "\n");
278
279 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL hits = %zd (%d%%)\n",
280 opcache_global_hits,
281 (int) (100.0 * opcache_global_hits /
282 (opcache_global_hits + opcache_global_misses)));
283
284 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL misses = %zd (%d%%)\n",
285 opcache_global_misses,
286 (int) (100.0 * opcache_global_misses /
287 (opcache_global_hits + opcache_global_misses)));
288
289 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL opts = %zd\n",
290 opcache_global_opts);
291
292 fprintf(stderr, "\n");
293#endif
294}
295
296void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000297PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000298{
Victor Stinner09532fe2019-05-10 23:39:09 +0200299 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200300 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100301 ensure_tstate_not_null(__func__, tstate);
302
Victor Stinner85f5a692020-03-09 22:12:04 +0100303 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000304}
305
306void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000307PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000308{
Victor Stinner09532fe2019-05-10 23:39:09 +0200309 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200310 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100312 We therefore avoid PyThreadState_Get() which dumps a fatal error
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 in debug mode.
314 */
Victor Stinnere225beb2019-06-03 18:14:24 +0200315 drop_gil(&runtime->ceval, tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000316}
317
318void
Victor Stinner23ef89d2020-03-18 02:26:04 +0100319_PyEval_ReleaseLock(PyThreadState *tstate)
320{
321 struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
322 drop_gil(ceval, tstate);
323}
324
325void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000326PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000327{
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100328 ensure_tstate_not_null(__func__, tstate);
329
Victor Stinner85f5a692020-03-09 22:12:04 +0100330 take_gil(tstate);
Victor Stinnere225beb2019-06-03 18:14:24 +0200331
Victor Stinner85f5a692020-03-09 22:12:04 +0100332 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
333 if (_PyThreadState_Swap(gilstate, tstate) != NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100334 Py_FatalError("non-NULL old thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200335 }
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000336}
337
338void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000339PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000340{
Victor Stinner17c68b82020-01-30 12:20:48 +0100341 assert(tstate != NULL);
Victor Stinner09532fe2019-05-10 23:39:09 +0200342
Victor Stinner01b1cc12019-11-20 02:27:56 +0100343 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200344 PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
345 if (new_tstate != tstate) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100346 Py_FatalError("wrong thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200347 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200348 drop_gil(&runtime->ceval, tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000349}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000350
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200351/* This function is called from PyOS_AfterFork_Child to destroy all threads
352 * which are not running in the child process, and clear internal locks
353 * which might be held by those threads.
354 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000355
356void
Victor Stinnerd5d9e812019-05-13 12:35:37 +0200357_PyEval_ReInitThreads(_PyRuntimeState *runtime)
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000358{
Victor Stinnere225beb2019-06-03 18:14:24 +0200359 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100360 struct _gil_runtime_state *gil = &runtime->ceval.gil;
361 if (!gil_created(gil)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 return;
Victor Stinner09532fe2019-05-10 23:39:09 +0200363 }
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100364 recreate_gil(gil);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100365 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
366 ensure_tstate_not_null(__func__, tstate);
Victor Stinner85f5a692020-03-09 22:12:04 +0100367
368 take_gil(tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700369
Victor Stinnere225beb2019-06-03 18:14:24 +0200370 struct _pending_calls *pending = &ceval->pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200371 pending->lock = PyThread_allocate_lock();
372 if (pending->lock == NULL) {
Eric Snow8479a342019-03-08 23:44:33 -0700373 Py_FatalError("Can't initialize threads for pending calls");
374 }
Jesse Nollera8513972008-07-17 16:49:17 +0000375
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200376 /* Destroy all threads except the current one */
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100377 _PyThreadState_DeleteExcept(runtime, tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000378}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000379
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000380/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600381 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000382
383void
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100384_PyEval_SignalAsyncExc(PyThreadState *tstate)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000385{
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100386 struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
Victor Stinnere225beb2019-06-03 18:14:24 +0200387 SIGNAL_ASYNC_EXC(ceval);
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000388}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000389
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000390PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000391PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000392{
Victor Stinner09532fe2019-05-10 23:39:09 +0200393 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200394 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinner09532fe2019-05-10 23:39:09 +0200395 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
396 if (tstate == NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100397 Py_FatalError("NULL tstate");
Victor Stinner09532fe2019-05-10 23:39:09 +0200398 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200399 assert(gil_created(&ceval->gil));
400 drop_gil(ceval, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000402}
403
404void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000405PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000406{
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100407 ensure_tstate_not_null(__func__, tstate);
408
Victor Stinner85f5a692020-03-09 22:12:04 +0100409 take_gil(tstate);
Victor Stinner17c68b82020-01-30 12:20:48 +0100410
Victor Stinner85f5a692020-03-09 22:12:04 +0100411 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
412 _PyThreadState_Swap(gilstate, tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000413}
414
415
Guido van Rossuma9672091994-09-14 13:31:22 +0000416/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
417 signal handlers or Mac I/O completion routines) can schedule calls
418 to a function to be called synchronously.
419 The synchronous function is called with one void* argument.
420 It should return 0 for success or -1 for failure -- failure should
421 be accompanied by an exception.
422
423 If registry succeeds, the registry function returns 0; if it fails
424 (e.g. due to too many pending calls) it returns -1 (without setting
425 an exception condition).
426
427 Note that because registry may occur from within signal handlers,
428 or other asynchronous events, calling malloc() is unsafe!
429
Guido van Rossuma9672091994-09-14 13:31:22 +0000430 Any thread can schedule pending calls, but only the main thread
431 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000432 There is no facility to schedule calls to a particular thread, but
433 that should be easy to change, should that ever be required. In
434 that case, the static variables here should go into the python
435 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000436*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000437
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200438void
Victor Stinner8849e592020-03-18 19:28:53 +0100439_PyEval_SignalReceived(PyThreadState *tstate)
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200440{
Victor Stinner8849e592020-03-18 19:28:53 +0100441 struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200442 /* bpo-30703: Function called when the C signal handler of Python gets a
443 signal. We cannot queue a callback using Py_AddPendingCall() since
444 that function is not async-signal-safe. */
Victor Stinnere225beb2019-06-03 18:14:24 +0200445 SIGNAL_PENDING_SIGNALS(ceval);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200446}
447
Eric Snow5be45a62019-03-08 22:47:07 -0700448/* Push one item onto the queue while holding the lock. */
449static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200450_push_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600451 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700452{
Eric Snow842a2f02019-03-15 15:47:51 -0600453 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700454 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600455 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700456 return -1; /* Queue full */
457 }
Eric Snow842a2f02019-03-15 15:47:51 -0600458 pending->calls[i].func = func;
459 pending->calls[i].arg = arg;
460 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700461 return 0;
462}
463
464/* Pop one item off the queue while holding the lock. */
465static void
Victor Stinnere225beb2019-06-03 18:14:24 +0200466_pop_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600467 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700468{
Eric Snow842a2f02019-03-15 15:47:51 -0600469 int i = pending->first;
470 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700471 return; /* Queue empty */
472 }
473
Eric Snow842a2f02019-03-15 15:47:51 -0600474 *func = pending->calls[i].func;
475 *arg = pending->calls[i].arg;
476 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700477}
478
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200479/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000480 scheduling to be made from any thread, and even from an executing
481 callback.
482 */
483
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000484int
Victor Stinner438a12d2019-05-24 17:01:38 +0200485_PyEval_AddPendingCall(PyThreadState *tstate,
Victor Stinner09532fe2019-05-10 23:39:09 +0200486 int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000487{
Victor Stinner8849e592020-03-18 19:28:53 +0100488 struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
Victor Stinnere225beb2019-06-03 18:14:24 +0200489 struct _pending_calls *pending = &ceval->pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600490
491 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
492 if (pending->finishing) {
493 PyThread_release_lock(pending->lock);
494
495 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +0200496 _PyErr_Fetch(tstate, &exc, &val, &tb);
497 _PyErr_SetString(tstate, PyExc_SystemError,
Eric Snow842a2f02019-03-15 15:47:51 -0600498 "Py_AddPendingCall: cannot add pending calls "
499 "(Python shutting down)");
Victor Stinner438a12d2019-05-24 17:01:38 +0200500 _PyErr_Print(tstate);
501 _PyErr_Restore(tstate, exc, val, tb);
Eric Snow842a2f02019-03-15 15:47:51 -0600502 return -1;
503 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200504 int result = _push_pending_call(pending, func, arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600505 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700506
Victor Stinnere225beb2019-06-03 18:14:24 +0200507 /* signal main loop */
508 SIGNAL_PENDING_CALLS(ceval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000510}
511
Victor Stinner09532fe2019-05-10 23:39:09 +0200512int
513Py_AddPendingCall(int (*func)(void *), void *arg)
514{
Victor Stinner8849e592020-03-18 19:28:53 +0100515 /* Get the Python thread state using PyGILState API, since
516 _PyThreadState_GET() returns NULL if the GIL is released.
517 Py_AddPendingCall() doesn't require the caller to hold the GIL. */
518 PyThreadState *tstate = PyGILState_GetThisThreadState();
519 assert(tstate != NULL);
520 return _PyEval_AddPendingCall(tstate, func, arg);
Victor Stinner09532fe2019-05-10 23:39:09 +0200521}
522
Eric Snowfdf282d2019-01-11 14:26:55 -0700523static int
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100524handle_signals(PyThreadState *tstate)
Eric Snowfdf282d2019-01-11 14:26:55 -0700525{
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100526 _PyRuntimeState *runtime = tstate->interp->runtime;
527
Victor Stinner3225b9f2020-03-09 20:56:57 +0100528 /* Only handle signals on main thread */
Victor Stinner09532fe2019-05-10 23:39:09 +0200529 if (PyThread_get_thread_ident() != runtime->main_thread) {
Eric Snowfdf282d2019-01-11 14:26:55 -0700530 return 0;
531 }
Eric Snow64d6cc82019-02-23 15:40:43 -0700532 /*
533 * Ensure that the thread isn't currently running some other
534 * interpreter.
535 */
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100536 PyInterpreterState *interp = tstate->interp;
Victor Stinner09532fe2019-05-10 23:39:09 +0200537 if (interp != runtime->interpreters.main) {
Eric Snow64d6cc82019-02-23 15:40:43 -0700538 return 0;
539 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700540
Victor Stinnere225beb2019-06-03 18:14:24 +0200541 struct _ceval_runtime_state *ceval = &runtime->ceval;
542 UNSIGNAL_PENDING_SIGNALS(ceval);
Eric Snow64d6cc82019-02-23 15:40:43 -0700543 if (_PyErr_CheckSignals() < 0) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200544 SIGNAL_PENDING_SIGNALS(ceval); /* We're not done yet */
Eric Snowfdf282d2019-01-11 14:26:55 -0700545 return -1;
546 }
547 return 0;
548}
549
550static int
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100551make_pending_calls(PyThreadState *tstate)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000552{
Eric Snow6a150bc2019-06-01 15:39:46 -0600553 static int busy = 0;
Eric Snowb75b1a352019-04-12 10:20:10 -0600554
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100555 _PyRuntimeState *runtime = tstate->interp->runtime;
556
Victor Stinnere225beb2019-06-03 18:14:24 +0200557 /* only service pending calls on main thread */
558 if (PyThread_get_thread_ident() != runtime->main_thread) {
559 return 0;
560 }
561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 /* don't perform recursive pending calls */
Eric Snowfdf282d2019-01-11 14:26:55 -0700563 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700565 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200566 busy = 1;
Victor Stinnere225beb2019-06-03 18:14:24 +0200567 struct _ceval_runtime_state *ceval = &runtime->ceval;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200568 /* unsignal before starting to call callbacks, so that any callback
569 added in-between re-signals */
Victor Stinnere225beb2019-06-03 18:14:24 +0200570 UNSIGNAL_PENDING_CALLS(ceval);
Eric Snowfdf282d2019-01-11 14:26:55 -0700571 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 /* perform a bounded number of calls, in case of recursion */
Victor Stinnere225beb2019-06-03 18:14:24 +0200574 struct _pending_calls *pending = &ceval->pending;
Eric Snowfdf282d2019-01-11 14:26:55 -0700575 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700576 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 void *arg = NULL;
578
579 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600580 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200581 _pop_pending_call(pending, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600582 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700583
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100584 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700585 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100586 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700587 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700588 res = func(arg);
589 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200590 goto error;
591 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000592 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200593
Charles-François Natalif23339a2011-07-23 18:15:43 +0200594 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700595 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200596
597error:
598 busy = 0;
Victor Stinnere225beb2019-06-03 18:14:24 +0200599 SIGNAL_PENDING_CALLS(ceval);
Eric Snowfdf282d2019-01-11 14:26:55 -0700600 return res;
601}
602
Eric Snow842a2f02019-03-15 15:47:51 -0600603void
Victor Stinner2b1df452020-01-13 18:46:59 +0100604_Py_FinishPendingCalls(PyThreadState *tstate)
Eric Snow842a2f02019-03-15 15:47:51 -0600605{
Eric Snow842a2f02019-03-15 15:47:51 -0600606 assert(PyGILState_Check());
607
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100608 struct _pending_calls *pending = &tstate->interp->runtime->ceval.pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200609
Eric Snow842a2f02019-03-15 15:47:51 -0600610 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
611 pending->finishing = 1;
612 PyThread_release_lock(pending->lock);
613
614 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
615 return;
616 }
617
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100618 if (make_pending_calls(tstate) < 0) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200619 PyObject *exc, *val, *tb;
620 _PyErr_Fetch(tstate, &exc, &val, &tb);
621 PyErr_BadInternalCall();
622 _PyErr_ChainExceptions(exc, val, tb);
623 _PyErr_Print(tstate);
Eric Snow842a2f02019-03-15 15:47:51 -0600624 }
625}
626
Eric Snowfdf282d2019-01-11 14:26:55 -0700627/* Py_MakePendingCalls() is a simple wrapper for the sake
628 of backward-compatibility. */
629int
630Py_MakePendingCalls(void)
631{
632 assert(PyGILState_Check());
633
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100634 PyThreadState *tstate = _PyThreadState_GET();
635
Eric Snowfdf282d2019-01-11 14:26:55 -0700636 /* Python signal handler doesn't really queue a callback: it only signals
637 that a signal was received, see _PyEval_SignalReceived(). */
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100638 int res = handle_signals(tstate);
Eric Snowfdf282d2019-01-11 14:26:55 -0700639 if (res != 0) {
640 return res;
641 }
642
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100643 res = make_pending_calls(tstate);
Eric Snowb75b1a352019-04-12 10:20:10 -0600644 if (res != 0) {
645 return res;
646 }
647
648 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000649}
650
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000651/* The interpreter's recursion limit */
652
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000653#ifndef Py_DEFAULT_RECURSION_LIMIT
654#define Py_DEFAULT_RECURSION_LIMIT 1000
655#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600656
Eric Snow05351c12017-09-05 21:43:08 -0700657int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000658
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600659void
Victor Stinnerdab84232020-03-17 18:56:44 +0100660_PyEval_InitRuntimeState(struct _ceval_runtime_state *ceval)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600661{
Victor Stinnerdab84232020-03-17 18:56:44 +0100662 ceval->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600663 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Victor Stinnerdab84232020-03-17 18:56:44 +0100664 _gil_initialize(&ceval->gil);
665}
666
667void
668_PyEval_InitState(struct _ceval_state *ceval)
669{
670 /* PyInterpreterState_New() initializes ceval to zero */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600671}
672
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000673int
674Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000675{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100676 struct _ceval_runtime_state *ceval = &_PyRuntime.ceval;
677 return ceval->recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000678}
679
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000680void
681Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000682{
Victor Stinnere225beb2019-06-03 18:14:24 +0200683 struct _ceval_runtime_state *ceval = &_PyRuntime.ceval;
684 ceval->recursion_limit = new_limit;
Victor Stinnerdab84232020-03-17 18:56:44 +0100685 _Py_CheckRecursionLimit = new_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000686}
687
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100688/* The function _Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
Armin Rigo2b3eb402003-10-28 12:05:48 +0000689 if the recursion_depth reaches _Py_CheckRecursionLimit.
690 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
691 to guarantee that _Py_CheckRecursiveCall() is regularly called.
692 Without USE_STACKCHECK, there is no need for this. */
693int
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100694_Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000695{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100696 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200697 int recursion_limit = runtime->ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000698
699#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700700 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 if (PyOS_CheckStack()) {
702 --tstate->recursion_depth;
Victor Stinner438a12d2019-05-24 17:01:38 +0200703 _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 return -1;
705 }
pdox18967932017-10-25 23:03:01 -0700706 /* Needed for ABI backwards-compatibility (see bpo-31857) */
Eric Snow05351c12017-09-05 21:43:08 -0700707 _Py_CheckRecursionLimit = recursion_limit;
pdox18967932017-10-25 23:03:01 -0700708#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 if (tstate->recursion_critical)
710 /* Somebody asked that we don't check for recursion. */
711 return 0;
712 if (tstate->overflowed) {
713 if (tstate->recursion_depth > recursion_limit + 50) {
714 /* Overflowing while handling an overflow. Give up. */
715 Py_FatalError("Cannot recover from stack overflow.");
716 }
717 return 0;
718 }
719 if (tstate->recursion_depth > recursion_limit) {
720 --tstate->recursion_depth;
721 tstate->overflowed = 1;
Victor Stinner438a12d2019-05-24 17:01:38 +0200722 _PyErr_Format(tstate, PyExc_RecursionError,
723 "maximum recursion depth exceeded%s",
724 where);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 return -1;
726 }
727 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000728}
729
Victor Stinner09532fe2019-05-10 23:39:09 +0200730static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
Victor Stinner438a12d2019-05-24 17:01:38 +0200731static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000732
Victor Stinnere225beb2019-06-03 18:14:24 +0200733#define _Py_TracingPossible(ceval) ((ceval)->tracing_possible)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000734
Guido van Rossum374a9221991-04-04 10:40:29 +0000735
Guido van Rossumb209a111997-04-29 18:18:01 +0000736PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000737PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000738{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 return PyEval_EvalCodeEx(co,
740 globals, locals,
741 (PyObject **)NULL, 0,
742 (PyObject **)NULL, 0,
743 (PyObject **)NULL, 0,
744 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000745}
746
747
748/* Interpreter main loop */
749
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000750PyObject *
Victor Stinnerb9e68122019-11-14 12:20:46 +0100751PyEval_EvalFrame(PyFrameObject *f)
752{
Victor Stinner0b72b232020-03-12 23:18:39 +0100753 /* Function kept for backward compatibility */
Victor Stinnerb9e68122019-11-14 12:20:46 +0100754 PyThreadState *tstate = _PyThreadState_GET();
755 return _PyEval_EvalFrame(tstate, f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000756}
757
758PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000759PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000760{
Victor Stinnerb9e68122019-11-14 12:20:46 +0100761 PyThreadState *tstate = _PyThreadState_GET();
762 return _PyEval_EvalFrame(tstate, f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700763}
764
Victor Stinnerc6944e72016-11-11 02:13:35 +0100765PyObject* _Py_HOT_FUNCTION
Victor Stinner0b72b232020-03-12 23:18:39 +0100766_PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
Brett Cannon3cebf932016-09-05 15:33:46 -0700767{
Victor Stinner0b72b232020-03-12 23:18:39 +0100768 ensure_tstate_not_null(__func__, tstate);
769
Guido van Rossum950361c1997-01-24 13:49:28 +0000770#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000772#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200773 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300774 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200775 int opcode; /* Current opcode */
776 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200777 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 PyObject *retval = NULL; /* Return value */
Victor Stinner09532fe2019-05-10 23:39:09 +0200779 _PyRuntimeState * const runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200780 struct _ceval_runtime_state * const ceval = &runtime->ceval;
Victor Stinnerdab84232020-03-17 18:56:44 +0100781 struct _ceval_state * const ceval2 = &tstate->interp->ceval;
Victor Stinnere225beb2019-06-03 18:14:24 +0200782 _Py_atomic_int * const eval_breaker = &ceval->eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000784
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000786
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000788
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 is true when the line being executed has changed. The
790 initial values are such as to make this false the first
791 time it is tested. */
792 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000793
Serhiy Storchakaab874002016-09-11 13:48:15 +0300794 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 PyObject *names;
796 PyObject *consts;
Inada Naoki91234a12019-06-03 21:30:58 +0900797 _PyOpcache *co_opcache;
Guido van Rossum374a9221991-04-04 10:40:29 +0000798
Brett Cannon368b4b72012-04-02 12:17:59 -0400799#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200800 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400801#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200802
Antoine Pitroub52ec782009-01-25 16:34:23 +0000803/* Computed GOTOs, or
804 the-optimization-commonly-but-improperly-known-as-"threaded code"
805 using gcc's labels-as-values extension
806 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
807
808 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000809 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000810 combined with a lookup table of jump addresses. However, since the
811 indirect jump instruction is shared by all opcodes, the CPU will have a
812 hard time making the right prediction for where to jump next (actually,
813 it will be always wrong except in the uncommon case of a sequence of
814 several identical opcodes).
815
816 "Threaded code" in contrast, uses an explicit jump table and an explicit
817 indirect jump instruction at the end of each opcode. Since the jump
818 instruction is at a different address for each opcode, the CPU will make a
819 separate prediction for each of these instructions, which is equivalent to
820 predicting the second opcode of each opcode pair. These predictions have
821 a much better chance to turn out valid, especially in small bytecode loops.
822
823 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000825 and potentially many more instructions (depending on the pipeline width).
826 A correctly predicted branch, however, is nearly free.
827
828 At the time of this writing, the "threaded code" version is up to 15-20%
829 faster than the normal "switch" version, depending on the compiler and the
830 CPU architecture.
831
832 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
833 because it would render the measurements invalid.
834
835
836 NOTE: care must be taken that the compiler doesn't try to "optimize" the
837 indirect jumps by sharing them between all opcodes. Such optimizations
838 can be disabled on gcc by using the -fno-gcse flag (or possibly
839 -fno-crossjumping).
840*/
841
Antoine Pitrou042b1282010-08-13 21:15:58 +0000842#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000843#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000844#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000845#endif
846
Antoine Pitrou042b1282010-08-13 21:15:58 +0000847#ifdef HAVE_COMPUTED_GOTOS
848 #ifndef USE_COMPUTED_GOTOS
849 #define USE_COMPUTED_GOTOS 1
850 #endif
851#else
852 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
853 #error "Computed gotos are not supported on this compiler."
854 #endif
855 #undef USE_COMPUTED_GOTOS
856 #define USE_COMPUTED_GOTOS 0
857#endif
858
859#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000860/* Import the static jump table */
861#include "opcode_targets.h"
862
Antoine Pitroub52ec782009-01-25 16:34:23 +0000863#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -0700864 op: \
865 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000866
Antoine Pitroub52ec782009-01-25 16:34:23 +0000867#ifdef LLTRACE
868#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 { \
Victor Stinnerdab84232020-03-17 18:56:44 +0100870 if (!lltrace && !_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300872 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300873 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 } \
875 goto fast_next_opcode; \
876 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000877#else
878#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 { \
Victor Stinnerdab84232020-03-17 18:56:44 +0100880 if (!_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300882 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300883 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 } \
885 goto fast_next_opcode; \
886 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000887#endif
888
Victor Stinner09532fe2019-05-10 23:39:09 +0200889#define DISPATCH() \
890 { \
891 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
892 FAST_DISPATCH(); \
893 } \
894 continue; \
895 }
896
Antoine Pitroub52ec782009-01-25 16:34:23 +0000897#else
Benjamin Petersonddd19492018-09-16 22:38:02 -0700898#define TARGET(op) op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000899#define FAST_DISPATCH() goto fast_next_opcode
Victor Stinner09532fe2019-05-10 23:39:09 +0200900#define DISPATCH() continue
Antoine Pitroub52ec782009-01-25 16:34:23 +0000901#endif
902
903
Neal Norwitza81d2202002-07-14 00:27:26 +0000904/* Tuple access macros */
905
906#ifndef Py_DEBUG
907#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
908#else
909#define GETITEM(v, i) PyTuple_GetItem((v), (i))
910#endif
911
Guido van Rossum374a9221991-04-04 10:40:29 +0000912/* Code access macros */
913
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300914/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600915#define INSTR_OFFSET() \
916 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300917#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300918 _Py_CODEUNIT word = *next_instr; \
919 opcode = _Py_OPCODE(word); \
920 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300921 next_instr++; \
922 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300923#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
924#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000925
Raymond Hettingerf606f872003-03-16 03:11:04 +0000926/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 Some opcodes tend to come in pairs thus making it possible to
928 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300929 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000930
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 Verifying the prediction costs a single high-speed test of a register
932 variable against a constant. If the pairing was good, then the
933 processor's own internal branch predication has a high likelihood of
934 success, resulting in a nearly zero-overhead transition to the
935 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300936 including its unpredictable switch-case branch. Combined with the
937 processor's internal branch prediction, a successful PREDICT has the
938 effect of making the two opcodes run as if they were a single new opcode
939 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000940
Georg Brandl86b2fb92008-07-16 03:43:04 +0000941 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 predictions turned-on and interpret the results as if some opcodes
943 had been combined or turn-off predictions so that the opcode frequency
944 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000945
946 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 the CPU to record separate branch prediction information for each
948 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000949
Raymond Hettingerf606f872003-03-16 03:11:04 +0000950*/
951
Denis Chernikovbaf29b22020-02-21 12:17:50 +0300952#define PREDICT_ID(op) PRED_##op
953
Antoine Pitrou042b1282010-08-13 21:15:58 +0000954#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Denis Chernikovbaf29b22020-02-21 12:17:50 +0300955#define PREDICT(op) if (0) goto PREDICT_ID(op)
Raymond Hettingera7216982004-02-08 19:59:27 +0000956#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300957#define PREDICT(op) \
Denis Chernikovbaf29b22020-02-21 12:17:50 +0300958 do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300959 _Py_CODEUNIT word = *next_instr; \
960 opcode = _Py_OPCODE(word); \
Denis Chernikovbaf29b22020-02-21 12:17:50 +0300961 if (opcode == op) { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300962 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300963 next_instr++; \
Denis Chernikovbaf29b22020-02-21 12:17:50 +0300964 goto PREDICT_ID(op); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300965 } \
966 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000967#endif
Denis Chernikovbaf29b22020-02-21 12:17:50 +0300968#define PREDICTED(op) PREDICT_ID(op):
Antoine Pitroub52ec782009-01-25 16:34:23 +0000969
Raymond Hettingerf606f872003-03-16 03:11:04 +0000970
Guido van Rossum374a9221991-04-04 10:40:29 +0000971/* Stack manipulation macros */
972
Martin v. Löwis18e16552006-02-15 17:27:45 +0000973/* The stack can grow at most MAXINT deep, as co_nlocals and
974 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000975#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
976#define EMPTY() (STACK_LEVEL() == 0)
977#define TOP() (stack_pointer[-1])
978#define SECOND() (stack_pointer[-2])
979#define THIRD() (stack_pointer[-3])
980#define FOURTH() (stack_pointer[-4])
981#define PEEK(n) (stack_pointer[-(n)])
982#define SET_TOP(v) (stack_pointer[-1] = (v))
983#define SET_SECOND(v) (stack_pointer[-2] = (v))
984#define SET_THIRD(v) (stack_pointer[-3] = (v))
985#define SET_FOURTH(v) (stack_pointer[-4] = (v))
986#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
987#define BASIC_STACKADJ(n) (stack_pointer += n)
988#define BASIC_PUSH(v) (*stack_pointer++ = (v))
989#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000990
Guido van Rossum96a42c81992-01-12 02:29:51 +0000991#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +0200993 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000994 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +0200995#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000996 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +0000997#define STACK_GROW(n) do { \
998 assert(n >= 0); \
999 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001000 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001001 assert(STACK_LEVEL() <= co->co_stacksize); \
1002 } while (0)
1003#define STACK_SHRINK(n) do { \
1004 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +02001005 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001006 (void)(BASIC_STACKADJ(-n)); \
1007 assert(STACK_LEVEL() <= co->co_stacksize); \
1008 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +00001009#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +02001010 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001011 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001012#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001013#define PUSH(v) BASIC_PUSH(v)
1014#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +00001015#define STACK_GROW(n) BASIC_STACKADJ(n)
1016#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001017#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001018#endif
1019
Guido van Rossum681d79a1995-07-18 14:51:37 +00001020/* Local variable macros */
1021
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001023
1024/* The SETLOCAL() macro must not DECREF the local variable in-place and
1025 then store the new value; it must copy the old value to a temporary
1026 value, then store the new value, and then DECREF the temporary value.
1027 This is because it is possible that during the DECREF the frame is
1028 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1029 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001031 GETLOCAL(i) = value; \
1032 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001033
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001034
1035#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 while (STACK_LEVEL() > (b)->b_level) { \
1037 PyObject *v = POP(); \
1038 Py_XDECREF(v); \
1039 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001040
1041#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001042 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +01001044 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1046 while (STACK_LEVEL() > (b)->b_level + 3) { \
1047 value = POP(); \
1048 Py_XDECREF(value); \
1049 } \
Mark Shannonae3087c2017-10-22 22:41:51 +01001050 exc_info = tstate->exc_info; \
1051 type = exc_info->exc_type; \
1052 value = exc_info->exc_value; \
1053 traceback = exc_info->exc_traceback; \
1054 exc_info->exc_type = POP(); \
1055 exc_info->exc_value = POP(); \
1056 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 Py_XDECREF(type); \
1058 Py_XDECREF(value); \
1059 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001060 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001061
Inada Naoki91234a12019-06-03 21:30:58 +09001062 /* macros for opcode cache */
1063#define OPCACHE_CHECK() \
1064 do { \
1065 co_opcache = NULL; \
1066 if (co->co_opcache != NULL) { \
1067 unsigned char co_opt_offset = \
1068 co->co_opcache_map[next_instr - first_instr]; \
1069 if (co_opt_offset > 0) { \
1070 assert(co_opt_offset <= co->co_opcache_size); \
1071 co_opcache = &co->co_opcache[co_opt_offset - 1]; \
1072 assert(co_opcache != NULL); \
Inada Naoki91234a12019-06-03 21:30:58 +09001073 } \
1074 } \
1075 } while (0)
1076
1077#if OPCACHE_STATS
1078
1079#define OPCACHE_STAT_GLOBAL_HIT() \
1080 do { \
1081 if (co->co_opcache != NULL) opcache_global_hits++; \
1082 } while (0)
1083
1084#define OPCACHE_STAT_GLOBAL_MISS() \
1085 do { \
1086 if (co->co_opcache != NULL) opcache_global_misses++; \
1087 } while (0)
1088
1089#define OPCACHE_STAT_GLOBAL_OPT() \
1090 do { \
1091 if (co->co_opcache != NULL) opcache_global_opts++; \
1092 } while (0)
1093
1094#else /* OPCACHE_STATS */
1095
1096#define OPCACHE_STAT_GLOBAL_HIT()
1097#define OPCACHE_STAT_GLOBAL_MISS()
1098#define OPCACHE_STAT_GLOBAL_OPT()
1099
1100#endif
1101
Guido van Rossuma027efa1997-05-05 20:56:21 +00001102/* Start of code */
1103
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 /* push frame */
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001105 if (_Py_EnterRecursiveCall(tstate, "")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001107 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001108
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001110
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 if (tstate->use_tracing) {
1112 if (tstate->c_tracefunc != NULL) {
1113 /* tstate->c_tracefunc, if defined, is a
1114 function that will be called on *every* entry
1115 to a code block. Its return value, if not
1116 None, is a function that will be called at
1117 the start of each executed line of code.
1118 (Actually, the function must return itself
1119 in order to continue tracing.) The trace
1120 functions are called with three arguments:
1121 a pointer to the current frame, a string
1122 indicating why the function is called, and
1123 an argument which depends on the situation.
1124 The global trace function is also called
1125 whenever an exception is detected. */
1126 if (call_trace_protected(tstate->c_tracefunc,
1127 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001128 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 /* Trace function raised an error */
1130 goto exit_eval_frame;
1131 }
1132 }
1133 if (tstate->c_profilefunc != NULL) {
1134 /* Similar for c_profilefunc, except it needn't
1135 return itself and isn't called for "line" events */
1136 if (call_trace_protected(tstate->c_profilefunc,
1137 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001138 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 /* Profile function raised an error */
1140 goto exit_eval_frame;
1141 }
1142 }
1143 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001144
Łukasz Langaa785c872016-09-09 17:37:37 -07001145 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1146 dtrace_function_entry(f);
1147
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 co = f->f_code;
1149 names = co->co_names;
1150 consts = co->co_consts;
1151 fastlocals = f->f_localsplus;
1152 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001153 assert(PyBytes_Check(co->co_code));
1154 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001155 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1156 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1157 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001158 /*
1159 f->f_lasti refers to the index of the last instruction,
1160 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001161
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001162 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001163 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001165 When the PREDICT() macros are enabled, some opcode pairs follow in
1166 direct succession without updating f->f_lasti. A successful
1167 prediction effectively links the two codes together as if they
1168 were a single new opcode; accordingly,f->f_lasti will point to
1169 the first code in the pair (for instance, GET_ITER followed by
1170 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001171 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001173 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001174 next_instr = first_instr;
1175 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001176 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1177 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001178 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 stack_pointer = f->f_stacktop;
1180 assert(stack_pointer != NULL);
1181 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001182 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001183
Inada Naoki91234a12019-06-03 21:30:58 +09001184 if (co->co_opcache_flag < OPCACHE_MIN_RUNS) {
1185 co->co_opcache_flag++;
1186 if (co->co_opcache_flag == OPCACHE_MIN_RUNS) {
1187 if (_PyCode_InitOpcache(co) < 0) {
1188 return NULL;
1189 }
1190#if OPCACHE_STATS
1191 opcache_code_objects_extra_mem +=
1192 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1193 sizeof(_PyOpcache) * co->co_opcache_size;
1194 opcache_code_objects++;
1195#endif
1196 }
1197 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001198
Tim Peters5ca576e2001-06-18 22:08:13 +00001199#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001200 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001201#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001202
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001203 if (throwflag) /* support for generator.throw() */
1204 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001205
Victor Stinnerace47d72013-07-18 01:41:08 +02001206#ifdef Py_DEBUG
Victor Stinner0b72b232020-03-12 23:18:39 +01001207 /* _PyEval_EvalFrameDefault() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001208 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001209 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001210 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001211#endif
1212
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001213main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1216 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001217 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 /* Do periodic things. Doing this every time through
1220 the loop would add too much overhead, so we do it
1221 only every Nth instruction. We also do it if
1222 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1223 event needs attention (e.g. a signal handler or
1224 async I/O handler); see Py_AddPendingCall() and
1225 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001226
Eric Snow7bda9de2019-03-08 17:25:54 -07001227 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001228 opcode = _Py_OPCODE(*next_instr);
1229 if (opcode == SETUP_FINALLY ||
1230 opcode == SETUP_WITH ||
1231 opcode == BEFORE_ASYNC_WITH ||
1232 opcode == YIELD_FROM) {
1233 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001234 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001235 - If we're about to enter the 'with:'. It will prevent
1236 emitting a resource warning in the common idiom
1237 'with open(path) as file:'.
1238 - If we're about to enter the 'async with:'.
1239 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001240 *very* useful, but might help in some cases and it's
1241 traditional)
1242 - If we're resuming a chain of nested 'yield from' or
1243 'await' calls, then each frame is parked with YIELD_FROM
1244 as its next opcode. If the user hit control-C we want to
1245 wait until we've reached the innermost frame before
1246 running the signal handler and raising KeyboardInterrupt
1247 (see bpo-30039).
1248 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 goto fast_next_opcode;
1250 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001251
Victor Stinnere225beb2019-06-03 18:14:24 +02001252 if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
Victor Stinnerd7fabc12020-03-18 01:56:21 +01001253 if (handle_signals(tstate) != 0) {
Eric Snowfdf282d2019-01-11 14:26:55 -07001254 goto error;
1255 }
1256 }
Victor Stinnere225beb2019-06-03 18:14:24 +02001257 if (_Py_atomic_load_relaxed(&ceval->pending.calls_to_do)) {
Victor Stinnerd7fabc12020-03-18 01:56:21 +01001258 if (make_pending_calls(tstate) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001259 goto error;
Eric Snowfdf282d2019-01-11 14:26:55 -07001260 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001262
Victor Stinnere225beb2019-06-03 18:14:24 +02001263 if (_Py_atomic_load_relaxed(&ceval->gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 /* Give another thread a chance */
Victor Stinner09532fe2019-05-10 23:39:09 +02001265 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +01001266 Py_FatalError("tstate mix-up");
Victor Stinner09532fe2019-05-10 23:39:09 +02001267 }
Victor Stinnere225beb2019-06-03 18:14:24 +02001268 drop_gil(ceval, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269
1270 /* Other threads may run now */
1271
Victor Stinner85f5a692020-03-09 22:12:04 +01001272 take_gil(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +01001273
Victor Stinner09532fe2019-05-10 23:39:09 +02001274 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +01001275 Py_FatalError("orphan tstate");
Victor Stinner09532fe2019-05-10 23:39:09 +02001276 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 }
1278 /* Check for asynchronous exceptions. */
1279 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001280 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 tstate->async_exc = NULL;
Victor Stinnere225beb2019-06-03 18:14:24 +02001282 UNSIGNAL_ASYNC_EXC(ceval);
Victor Stinner438a12d2019-05-24 17:01:38 +02001283 _PyErr_SetNone(tstate, exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001284 Py_DECREF(exc);
1285 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 }
1287 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 fast_next_opcode:
1290 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001291
Łukasz Langaa785c872016-09-09 17:37:37 -07001292 if (PyDTrace_LINE_ENABLED())
1293 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001295 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001296
Victor Stinnerdab84232020-03-17 18:56:44 +01001297 if (_Py_TracingPossible(ceval2) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001298 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001299 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 /* see maybe_call_line_trace
1301 for expository comments */
1302 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 err = maybe_call_line_trace(tstate->c_tracefunc,
1305 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001306 tstate, f,
1307 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 /* Reload possibly changed frame fields */
1309 JUMPTO(f->f_lasti);
1310 if (f->f_stacktop != NULL) {
1311 stack_pointer = f->f_stacktop;
1312 f->f_stacktop = NULL;
1313 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001314 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001316 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001320
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001321 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001322 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001323#ifdef DYNAMIC_EXECUTION_PROFILE
1324#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 dxpairs[lastopcode][opcode]++;
1326 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001327#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001329#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001330
Guido van Rossum96a42c81992-01-12 02:29:51 +00001331#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 if (lltrace) {
1335 if (HAS_ARG(opcode)) {
1336 printf("%d: %d, %d\n",
1337 f->f_lasti, opcode, oparg);
1338 }
1339 else {
1340 printf("%d: %d\n",
1341 f->f_lasti, opcode);
1342 }
1343 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001344#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001348 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001349 It is essential that any operation that fails must goto error
1350 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001351
Benjamin Petersonddd19492018-09-16 22:38:02 -07001352 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001354 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001355
Benjamin Petersonddd19492018-09-16 22:38:02 -07001356 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001357 PyObject *value = GETLOCAL(oparg);
1358 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001359 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001360 UNBOUNDLOCAL_ERROR_MSG,
1361 PyTuple_GetItem(co->co_varnames, oparg));
1362 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001364 Py_INCREF(value);
1365 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001367 }
1368
Benjamin Petersonddd19492018-09-16 22:38:02 -07001369 case TARGET(LOAD_CONST): {
1370 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001371 PyObject *value = GETITEM(consts, oparg);
1372 Py_INCREF(value);
1373 PUSH(value);
1374 FAST_DISPATCH();
1375 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001376
Benjamin Petersonddd19492018-09-16 22:38:02 -07001377 case TARGET(STORE_FAST): {
1378 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001379 PyObject *value = POP();
1380 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001382 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001383
Benjamin Petersonddd19492018-09-16 22:38:02 -07001384 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001385 PyObject *value = POP();
1386 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001388 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001389
Benjamin Petersonddd19492018-09-16 22:38:02 -07001390 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001391 PyObject *top = TOP();
1392 PyObject *second = SECOND();
1393 SET_TOP(second);
1394 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001396 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001397
Benjamin Petersonddd19492018-09-16 22:38:02 -07001398 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001399 PyObject *top = TOP();
1400 PyObject *second = SECOND();
1401 PyObject *third = THIRD();
1402 SET_TOP(second);
1403 SET_SECOND(third);
1404 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001406 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001407
Benjamin Petersonddd19492018-09-16 22:38:02 -07001408 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001409 PyObject *top = TOP();
1410 PyObject *second = SECOND();
1411 PyObject *third = THIRD();
1412 PyObject *fourth = FOURTH();
1413 SET_TOP(second);
1414 SET_SECOND(third);
1415 SET_THIRD(fourth);
1416 SET_FOURTH(top);
1417 FAST_DISPATCH();
1418 }
1419
Benjamin Petersonddd19492018-09-16 22:38:02 -07001420 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001421 PyObject *top = TOP();
1422 Py_INCREF(top);
1423 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001425 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001426
Benjamin Petersonddd19492018-09-16 22:38:02 -07001427 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001428 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001429 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001430 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001431 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001432 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001433 SET_TOP(top);
1434 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001435 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001436 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001437
Benjamin Petersonddd19492018-09-16 22:38:02 -07001438 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001439 PyObject *value = TOP();
1440 PyObject *res = PyNumber_Positive(value);
1441 Py_DECREF(value);
1442 SET_TOP(res);
1443 if (res == NULL)
1444 goto error;
1445 DISPATCH();
1446 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001447
Benjamin Petersonddd19492018-09-16 22:38:02 -07001448 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001449 PyObject *value = TOP();
1450 PyObject *res = PyNumber_Negative(value);
1451 Py_DECREF(value);
1452 SET_TOP(res);
1453 if (res == NULL)
1454 goto error;
1455 DISPATCH();
1456 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001457
Benjamin Petersonddd19492018-09-16 22:38:02 -07001458 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001459 PyObject *value = TOP();
1460 int err = PyObject_IsTrue(value);
1461 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001462 if (err == 0) {
1463 Py_INCREF(Py_True);
1464 SET_TOP(Py_True);
1465 DISPATCH();
1466 }
1467 else if (err > 0) {
1468 Py_INCREF(Py_False);
1469 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001470 DISPATCH();
1471 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001472 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001473 goto error;
1474 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001475
Benjamin Petersonddd19492018-09-16 22:38:02 -07001476 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001477 PyObject *value = TOP();
1478 PyObject *res = PyNumber_Invert(value);
1479 Py_DECREF(value);
1480 SET_TOP(res);
1481 if (res == NULL)
1482 goto error;
1483 DISPATCH();
1484 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001485
Benjamin Petersonddd19492018-09-16 22:38:02 -07001486 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001487 PyObject *exp = POP();
1488 PyObject *base = TOP();
1489 PyObject *res = PyNumber_Power(base, exp, Py_None);
1490 Py_DECREF(base);
1491 Py_DECREF(exp);
1492 SET_TOP(res);
1493 if (res == NULL)
1494 goto error;
1495 DISPATCH();
1496 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001497
Benjamin Petersonddd19492018-09-16 22:38:02 -07001498 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001499 PyObject *right = POP();
1500 PyObject *left = TOP();
1501 PyObject *res = PyNumber_Multiply(left, right);
1502 Py_DECREF(left);
1503 Py_DECREF(right);
1504 SET_TOP(res);
1505 if (res == NULL)
1506 goto error;
1507 DISPATCH();
1508 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001509
Benjamin Petersonddd19492018-09-16 22:38:02 -07001510 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001511 PyObject *right = POP();
1512 PyObject *left = TOP();
1513 PyObject *res = PyNumber_MatrixMultiply(left, right);
1514 Py_DECREF(left);
1515 Py_DECREF(right);
1516 SET_TOP(res);
1517 if (res == NULL)
1518 goto error;
1519 DISPATCH();
1520 }
1521
Benjamin Petersonddd19492018-09-16 22:38:02 -07001522 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001523 PyObject *divisor = POP();
1524 PyObject *dividend = TOP();
1525 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1526 Py_DECREF(dividend);
1527 Py_DECREF(divisor);
1528 SET_TOP(quotient);
1529 if (quotient == NULL)
1530 goto error;
1531 DISPATCH();
1532 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001533
Benjamin Petersonddd19492018-09-16 22:38:02 -07001534 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001535 PyObject *divisor = POP();
1536 PyObject *dividend = TOP();
1537 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1538 Py_DECREF(dividend);
1539 Py_DECREF(divisor);
1540 SET_TOP(quotient);
1541 if (quotient == NULL)
1542 goto error;
1543 DISPATCH();
1544 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001545
Benjamin Petersonddd19492018-09-16 22:38:02 -07001546 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001547 PyObject *divisor = POP();
1548 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001549 PyObject *res;
1550 if (PyUnicode_CheckExact(dividend) && (
1551 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1552 // fast path; string formatting, but not if the RHS is a str subclass
1553 // (see issue28598)
1554 res = PyUnicode_Format(dividend, divisor);
1555 } else {
1556 res = PyNumber_Remainder(dividend, divisor);
1557 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001558 Py_DECREF(divisor);
1559 Py_DECREF(dividend);
1560 SET_TOP(res);
1561 if (res == NULL)
1562 goto error;
1563 DISPATCH();
1564 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001565
Benjamin Petersonddd19492018-09-16 22:38:02 -07001566 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001567 PyObject *right = POP();
1568 PyObject *left = TOP();
1569 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001570 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1571 CPython using bytecode, it is simply worthless.
1572 See http://bugs.python.org/issue21955 and
1573 http://bugs.python.org/issue10044 for the discussion. In short,
1574 no patch shown any impact on a realistic benchmark, only a minor
1575 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001576 if (PyUnicode_CheckExact(left) &&
1577 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001578 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001579 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001580 }
1581 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001582 sum = PyNumber_Add(left, right);
1583 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001584 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001585 Py_DECREF(right);
1586 SET_TOP(sum);
1587 if (sum == NULL)
1588 goto error;
1589 DISPATCH();
1590 }
1591
Benjamin Petersonddd19492018-09-16 22:38:02 -07001592 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001593 PyObject *right = POP();
1594 PyObject *left = TOP();
1595 PyObject *diff = PyNumber_Subtract(left, right);
1596 Py_DECREF(right);
1597 Py_DECREF(left);
1598 SET_TOP(diff);
1599 if (diff == NULL)
1600 goto error;
1601 DISPATCH();
1602 }
1603
Benjamin Petersonddd19492018-09-16 22:38:02 -07001604 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001605 PyObject *sub = POP();
1606 PyObject *container = TOP();
1607 PyObject *res = PyObject_GetItem(container, sub);
1608 Py_DECREF(container);
1609 Py_DECREF(sub);
1610 SET_TOP(res);
1611 if (res == NULL)
1612 goto error;
1613 DISPATCH();
1614 }
1615
Benjamin Petersonddd19492018-09-16 22:38:02 -07001616 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001617 PyObject *right = POP();
1618 PyObject *left = TOP();
1619 PyObject *res = PyNumber_Lshift(left, right);
1620 Py_DECREF(left);
1621 Py_DECREF(right);
1622 SET_TOP(res);
1623 if (res == NULL)
1624 goto error;
1625 DISPATCH();
1626 }
1627
Benjamin Petersonddd19492018-09-16 22:38:02 -07001628 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001629 PyObject *right = POP();
1630 PyObject *left = TOP();
1631 PyObject *res = PyNumber_Rshift(left, right);
1632 Py_DECREF(left);
1633 Py_DECREF(right);
1634 SET_TOP(res);
1635 if (res == NULL)
1636 goto error;
1637 DISPATCH();
1638 }
1639
Benjamin Petersonddd19492018-09-16 22:38:02 -07001640 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001641 PyObject *right = POP();
1642 PyObject *left = TOP();
1643 PyObject *res = PyNumber_And(left, right);
1644 Py_DECREF(left);
1645 Py_DECREF(right);
1646 SET_TOP(res);
1647 if (res == NULL)
1648 goto error;
1649 DISPATCH();
1650 }
1651
Benjamin Petersonddd19492018-09-16 22:38:02 -07001652 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001653 PyObject *right = POP();
1654 PyObject *left = TOP();
1655 PyObject *res = PyNumber_Xor(left, right);
1656 Py_DECREF(left);
1657 Py_DECREF(right);
1658 SET_TOP(res);
1659 if (res == NULL)
1660 goto error;
1661 DISPATCH();
1662 }
1663
Benjamin Petersonddd19492018-09-16 22:38:02 -07001664 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001665 PyObject *right = POP();
1666 PyObject *left = TOP();
1667 PyObject *res = PyNumber_Or(left, right);
1668 Py_DECREF(left);
1669 Py_DECREF(right);
1670 SET_TOP(res);
1671 if (res == NULL)
1672 goto error;
1673 DISPATCH();
1674 }
1675
Benjamin Petersonddd19492018-09-16 22:38:02 -07001676 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001677 PyObject *v = POP();
1678 PyObject *list = PEEK(oparg);
1679 int err;
1680 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001681 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001682 if (err != 0)
1683 goto error;
1684 PREDICT(JUMP_ABSOLUTE);
1685 DISPATCH();
1686 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001687
Benjamin Petersonddd19492018-09-16 22:38:02 -07001688 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001689 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001690 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001691 int err;
1692 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001693 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001694 if (err != 0)
1695 goto error;
1696 PREDICT(JUMP_ABSOLUTE);
1697 DISPATCH();
1698 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001699
Benjamin Petersonddd19492018-09-16 22:38:02 -07001700 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001701 PyObject *exp = POP();
1702 PyObject *base = TOP();
1703 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1704 Py_DECREF(base);
1705 Py_DECREF(exp);
1706 SET_TOP(res);
1707 if (res == NULL)
1708 goto error;
1709 DISPATCH();
1710 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001711
Benjamin Petersonddd19492018-09-16 22:38:02 -07001712 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001713 PyObject *right = POP();
1714 PyObject *left = TOP();
1715 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1716 Py_DECREF(left);
1717 Py_DECREF(right);
1718 SET_TOP(res);
1719 if (res == NULL)
1720 goto error;
1721 DISPATCH();
1722 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001723
Benjamin Petersonddd19492018-09-16 22:38:02 -07001724 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001725 PyObject *right = POP();
1726 PyObject *left = TOP();
1727 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1728 Py_DECREF(left);
1729 Py_DECREF(right);
1730 SET_TOP(res);
1731 if (res == NULL)
1732 goto error;
1733 DISPATCH();
1734 }
1735
Benjamin Petersonddd19492018-09-16 22:38:02 -07001736 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001737 PyObject *divisor = POP();
1738 PyObject *dividend = TOP();
1739 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1740 Py_DECREF(dividend);
1741 Py_DECREF(divisor);
1742 SET_TOP(quotient);
1743 if (quotient == NULL)
1744 goto error;
1745 DISPATCH();
1746 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001747
Benjamin Petersonddd19492018-09-16 22:38:02 -07001748 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001749 PyObject *divisor = POP();
1750 PyObject *dividend = TOP();
1751 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1752 Py_DECREF(dividend);
1753 Py_DECREF(divisor);
1754 SET_TOP(quotient);
1755 if (quotient == NULL)
1756 goto error;
1757 DISPATCH();
1758 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001759
Benjamin Petersonddd19492018-09-16 22:38:02 -07001760 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001761 PyObject *right = POP();
1762 PyObject *left = TOP();
1763 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1764 Py_DECREF(left);
1765 Py_DECREF(right);
1766 SET_TOP(mod);
1767 if (mod == NULL)
1768 goto error;
1769 DISPATCH();
1770 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001771
Benjamin Petersonddd19492018-09-16 22:38:02 -07001772 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001773 PyObject *right = POP();
1774 PyObject *left = TOP();
1775 PyObject *sum;
1776 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001777 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001778 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001779 }
1780 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001781 sum = PyNumber_InPlaceAdd(left, right);
1782 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001783 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001784 Py_DECREF(right);
1785 SET_TOP(sum);
1786 if (sum == NULL)
1787 goto error;
1788 DISPATCH();
1789 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001790
Benjamin Petersonddd19492018-09-16 22:38:02 -07001791 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001792 PyObject *right = POP();
1793 PyObject *left = TOP();
1794 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1795 Py_DECREF(left);
1796 Py_DECREF(right);
1797 SET_TOP(diff);
1798 if (diff == NULL)
1799 goto error;
1800 DISPATCH();
1801 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001802
Benjamin Petersonddd19492018-09-16 22:38:02 -07001803 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001804 PyObject *right = POP();
1805 PyObject *left = TOP();
1806 PyObject *res = PyNumber_InPlaceLshift(left, right);
1807 Py_DECREF(left);
1808 Py_DECREF(right);
1809 SET_TOP(res);
1810 if (res == NULL)
1811 goto error;
1812 DISPATCH();
1813 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001814
Benjamin Petersonddd19492018-09-16 22:38:02 -07001815 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001816 PyObject *right = POP();
1817 PyObject *left = TOP();
1818 PyObject *res = PyNumber_InPlaceRshift(left, right);
1819 Py_DECREF(left);
1820 Py_DECREF(right);
1821 SET_TOP(res);
1822 if (res == NULL)
1823 goto error;
1824 DISPATCH();
1825 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001826
Benjamin Petersonddd19492018-09-16 22:38:02 -07001827 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001828 PyObject *right = POP();
1829 PyObject *left = TOP();
1830 PyObject *res = PyNumber_InPlaceAnd(left, right);
1831 Py_DECREF(left);
1832 Py_DECREF(right);
1833 SET_TOP(res);
1834 if (res == NULL)
1835 goto error;
1836 DISPATCH();
1837 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001838
Benjamin Petersonddd19492018-09-16 22:38:02 -07001839 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001840 PyObject *right = POP();
1841 PyObject *left = TOP();
1842 PyObject *res = PyNumber_InPlaceXor(left, right);
1843 Py_DECREF(left);
1844 Py_DECREF(right);
1845 SET_TOP(res);
1846 if (res == NULL)
1847 goto error;
1848 DISPATCH();
1849 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001850
Benjamin Petersonddd19492018-09-16 22:38:02 -07001851 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001852 PyObject *right = POP();
1853 PyObject *left = TOP();
1854 PyObject *res = PyNumber_InPlaceOr(left, right);
1855 Py_DECREF(left);
1856 Py_DECREF(right);
1857 SET_TOP(res);
1858 if (res == NULL)
1859 goto error;
1860 DISPATCH();
1861 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001862
Benjamin Petersonddd19492018-09-16 22:38:02 -07001863 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001864 PyObject *sub = TOP();
1865 PyObject *container = SECOND();
1866 PyObject *v = THIRD();
1867 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001868 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001869 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001870 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001871 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001872 Py_DECREF(container);
1873 Py_DECREF(sub);
1874 if (err != 0)
1875 goto error;
1876 DISPATCH();
1877 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001878
Benjamin Petersonddd19492018-09-16 22:38:02 -07001879 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001880 PyObject *sub = TOP();
1881 PyObject *container = SECOND();
1882 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001883 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00001884 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001885 err = PyObject_DelItem(container, sub);
1886 Py_DECREF(container);
1887 Py_DECREF(sub);
1888 if (err != 0)
1889 goto error;
1890 DISPATCH();
1891 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001892
Benjamin Petersonddd19492018-09-16 22:38:02 -07001893 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01001894 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001895 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001896 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001897 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001898 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001899 _PyErr_SetString(tstate, PyExc_RuntimeError,
1900 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001901 Py_DECREF(value);
1902 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903 }
Petr Viktorinffd97532020-02-11 17:46:57 +01001904 res = PyObject_CallOneArg(hook, value);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001905 Py_DECREF(value);
1906 if (res == NULL)
1907 goto error;
1908 Py_DECREF(res);
1909 DISPATCH();
1910 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001911
Benjamin Petersonddd19492018-09-16 22:38:02 -07001912 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001913 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001914 switch (oparg) {
1915 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001916 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001917 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001918 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001919 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001920 /* fall through */
1921 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02001922 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001923 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001924 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001925 break;
1926 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02001927 _PyErr_SetString(tstate, PyExc_SystemError,
1928 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001929 break;
1930 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001931 goto error;
1932 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001933
Benjamin Petersonddd19492018-09-16 22:38:02 -07001934 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001935 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001936 assert(f->f_iblock == 0);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00001937 assert(EMPTY());
1938 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001939 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001940
Benjamin Petersonddd19492018-09-16 22:38:02 -07001941 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001942 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001943 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001944 PyObject *obj = TOP();
1945 PyTypeObject *type = Py_TYPE(obj);
1946
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001947 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001948 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001949 }
Yury Selivanov75445082015-05-11 22:57:16 -04001950
1951 if (getter != NULL) {
1952 iter = (*getter)(obj);
1953 Py_DECREF(obj);
1954 if (iter == NULL) {
1955 SET_TOP(NULL);
1956 goto error;
1957 }
1958 }
1959 else {
1960 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02001961 _PyErr_Format(tstate, PyExc_TypeError,
1962 "'async for' requires an object with "
1963 "__aiter__ method, got %.100s",
1964 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001965 Py_DECREF(obj);
1966 goto error;
1967 }
1968
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001969 if (Py_TYPE(iter)->tp_as_async == NULL ||
1970 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001971
Yury Selivanov398ff912017-03-02 22:20:00 -05001972 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02001973 _PyErr_Format(tstate, PyExc_TypeError,
1974 "'async for' received an object from __aiter__ "
1975 "that does not implement __anext__: %.100s",
1976 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001977 Py_DECREF(iter);
1978 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001979 }
1980
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001981 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001982 DISPATCH();
1983 }
1984
Benjamin Petersonddd19492018-09-16 22:38:02 -07001985 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001986 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001987 PyObject *next_iter = NULL;
1988 PyObject *awaitable = NULL;
1989 PyObject *aiter = TOP();
1990 PyTypeObject *type = Py_TYPE(aiter);
1991
Yury Selivanoveb636452016-09-08 22:01:51 -07001992 if (PyAsyncGen_CheckExact(aiter)) {
1993 awaitable = type->tp_as_async->am_anext(aiter);
1994 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001995 goto error;
1996 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001997 } else {
1998 if (type->tp_as_async != NULL){
1999 getter = type->tp_as_async->am_anext;
2000 }
Yury Selivanov75445082015-05-11 22:57:16 -04002001
Yury Selivanoveb636452016-09-08 22:01:51 -07002002 if (getter != NULL) {
2003 next_iter = (*getter)(aiter);
2004 if (next_iter == NULL) {
2005 goto error;
2006 }
2007 }
2008 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02002009 _PyErr_Format(tstate, PyExc_TypeError,
2010 "'async for' requires an iterator with "
2011 "__anext__ method, got %.100s",
2012 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07002013 goto error;
2014 }
Yury Selivanov75445082015-05-11 22:57:16 -04002015
Yury Selivanoveb636452016-09-08 22:01:51 -07002016 awaitable = _PyCoro_GetAwaitableIter(next_iter);
2017 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05002018 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07002019 PyExc_TypeError,
2020 "'async for' received an invalid object "
2021 "from __anext__: %.100s",
2022 Py_TYPE(next_iter)->tp_name);
2023
2024 Py_DECREF(next_iter);
2025 goto error;
2026 } else {
2027 Py_DECREF(next_iter);
2028 }
2029 }
Yury Selivanov75445082015-05-11 22:57:16 -04002030
2031 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002032 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002033 DISPATCH();
2034 }
2035
Benjamin Petersonddd19492018-09-16 22:38:02 -07002036 case TARGET(GET_AWAITABLE): {
2037 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002038 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002039 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002040
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002041 if (iter == NULL) {
Mark Shannonfee55262019-11-21 09:11:43 +00002042 int opcode_at_minus_3 = 0;
2043 if ((next_instr - first_instr) > 2) {
2044 opcode_at_minus_3 = _Py_OPCODE(next_instr[-3]);
2045 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002046 format_awaitable_error(tstate, Py_TYPE(iterable),
Mark Shannonfee55262019-11-21 09:11:43 +00002047 opcode_at_minus_3,
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002048 _Py_OPCODE(next_instr[-2]));
2049 }
2050
Yury Selivanov75445082015-05-11 22:57:16 -04002051 Py_DECREF(iterable);
2052
Yury Selivanovc724bae2016-03-02 11:30:46 -05002053 if (iter != NULL && PyCoro_CheckExact(iter)) {
2054 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2055 if (yf != NULL) {
2056 /* `iter` is a coroutine object that is being
2057 awaited, `yf` is a pointer to the current awaitable
2058 being awaited on. */
2059 Py_DECREF(yf);
2060 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002061 _PyErr_SetString(tstate, PyExc_RuntimeError,
2062 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002063 /* The code below jumps to `error` if `iter` is NULL. */
2064 }
2065 }
2066
Yury Selivanov75445082015-05-11 22:57:16 -04002067 SET_TOP(iter); /* Even if it's NULL */
2068
2069 if (iter == NULL) {
2070 goto error;
2071 }
2072
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002073 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002074 DISPATCH();
2075 }
2076
Benjamin Petersonddd19492018-09-16 22:38:02 -07002077 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002078 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002079 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002080 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002081 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
2082 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002083 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002084 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002085 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002086 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002087 else
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02002088 retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002089 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002090 Py_DECREF(v);
2091 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002092 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002093 if (tstate->c_tracefunc != NULL
Victor Stinner438a12d2019-05-24 17:01:38 +02002094 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002095 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002096 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002097 if (err < 0)
2098 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002099 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002100 SET_TOP(val);
2101 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002102 }
Martin Panter95f53c12016-07-18 08:23:26 +00002103 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002104 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002105 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002106 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002107 f->f_lasti -= sizeof(_Py_CODEUNIT);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002108 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002109 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002110
Benjamin Petersonddd19492018-09-16 22:38:02 -07002111 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002112 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002113
2114 if (co->co_flags & CO_ASYNC_GENERATOR) {
2115 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2116 Py_DECREF(retval);
2117 if (w == NULL) {
2118 retval = NULL;
2119 goto error;
2120 }
2121 retval = w;
2122 }
2123
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002124 f->f_stacktop = stack_pointer;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002125 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002126 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002127
Benjamin Petersonddd19492018-09-16 22:38:02 -07002128 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002129 PyObject *type, *value, *traceback;
2130 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002131 PyTryBlock *b = PyFrame_BlockPop(f);
2132 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002133 _PyErr_SetString(tstate, PyExc_SystemError,
2134 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002135 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002136 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002137 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2138 STACK_LEVEL() <= (b)->b_level + 4);
2139 exc_info = tstate->exc_info;
2140 type = exc_info->exc_type;
2141 value = exc_info->exc_value;
2142 traceback = exc_info->exc_traceback;
2143 exc_info->exc_type = POP();
2144 exc_info->exc_value = POP();
2145 exc_info->exc_traceback = POP();
2146 Py_XDECREF(type);
2147 Py_XDECREF(value);
2148 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002149 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002150 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002151
Benjamin Petersonddd19492018-09-16 22:38:02 -07002152 case TARGET(POP_BLOCK): {
2153 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002154 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002155 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002156 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002157
Mark Shannonfee55262019-11-21 09:11:43 +00002158 case TARGET(RERAISE): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002159 PyObject *exc = POP();
Mark Shannonfee55262019-11-21 09:11:43 +00002160 PyObject *val = POP();
2161 PyObject *tb = POP();
2162 assert(PyExceptionClass_Check(exc));
Victor Stinner61f4db82020-01-28 03:37:45 +01002163 _PyErr_Restore(tstate, exc, val, tb);
Mark Shannonfee55262019-11-21 09:11:43 +00002164 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002165 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002166
Benjamin Petersonddd19492018-09-16 22:38:02 -07002167 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002168 PyObject *exc = POP();
2169 assert(PyExceptionClass_Check(exc));
2170 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2171 PyTryBlock *b = PyFrame_BlockPop(f);
2172 assert(b->b_type == EXCEPT_HANDLER);
2173 Py_DECREF(exc);
2174 UNWIND_EXCEPT_HANDLER(b);
2175 Py_DECREF(POP());
2176 JUMPBY(oparg);
2177 FAST_DISPATCH();
2178 }
2179 else {
2180 PyObject *val = POP();
2181 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002182 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002183 goto exception_unwind;
2184 }
2185 }
2186
Zackery Spytzce6a0702019-08-25 03:44:09 -06002187 case TARGET(LOAD_ASSERTION_ERROR): {
2188 PyObject *value = PyExc_AssertionError;
2189 Py_INCREF(value);
2190 PUSH(value);
2191 FAST_DISPATCH();
2192 }
2193
Benjamin Petersonddd19492018-09-16 22:38:02 -07002194 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002195 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002196
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002197 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002198 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002199 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002200 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002201 if (!_PyErr_Occurred(tstate)) {
2202 _PyErr_SetString(tstate, PyExc_NameError,
2203 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002204 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002205 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002206 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002207 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002208 }
2209 else {
2210 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2211 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002212 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002213 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2214 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002215 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2216 _PyErr_SetString(tstate, PyExc_NameError,
2217 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002218 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002219 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002220 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002221 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002222 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002223 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002224
Benjamin Petersonddd19492018-09-16 22:38:02 -07002225 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002226 PyObject *name = GETITEM(names, oparg);
2227 PyObject *v = POP();
2228 PyObject *ns = f->f_locals;
2229 int err;
2230 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002231 _PyErr_Format(tstate, PyExc_SystemError,
2232 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002233 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002234 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002235 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002236 if (PyDict_CheckExact(ns))
2237 err = PyDict_SetItem(ns, name, v);
2238 else
2239 err = PyObject_SetItem(ns, name, v);
2240 Py_DECREF(v);
2241 if (err != 0)
2242 goto error;
2243 DISPATCH();
2244 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002245
Benjamin Petersonddd19492018-09-16 22:38:02 -07002246 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002247 PyObject *name = GETITEM(names, oparg);
2248 PyObject *ns = f->f_locals;
2249 int err;
2250 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002251 _PyErr_Format(tstate, PyExc_SystemError,
2252 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002253 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002254 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002255 err = PyObject_DelItem(ns, name);
2256 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002257 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002258 NAME_ERROR_MSG,
2259 name);
2260 goto error;
2261 }
2262 DISPATCH();
2263 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002264
Benjamin Petersonddd19492018-09-16 22:38:02 -07002265 case TARGET(UNPACK_SEQUENCE): {
2266 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002267 PyObject *seq = POP(), *item, **items;
2268 if (PyTuple_CheckExact(seq) &&
2269 PyTuple_GET_SIZE(seq) == oparg) {
2270 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002271 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002272 item = items[oparg];
2273 Py_INCREF(item);
2274 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002275 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002276 } else if (PyList_CheckExact(seq) &&
2277 PyList_GET_SIZE(seq) == oparg) {
2278 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002279 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002280 item = items[oparg];
2281 Py_INCREF(item);
2282 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002283 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002284 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002285 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002286 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002287 } else {
2288 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002289 Py_DECREF(seq);
2290 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002291 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002292 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002293 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002294 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002295
Benjamin Petersonddd19492018-09-16 22:38:02 -07002296 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002297 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2298 PyObject *seq = POP();
2299
Victor Stinner438a12d2019-05-24 17:01:38 +02002300 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002301 stack_pointer + totalargs)) {
2302 stack_pointer += totalargs;
2303 } else {
2304 Py_DECREF(seq);
2305 goto error;
2306 }
2307 Py_DECREF(seq);
2308 DISPATCH();
2309 }
2310
Benjamin Petersonddd19492018-09-16 22:38:02 -07002311 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002312 PyObject *name = GETITEM(names, oparg);
2313 PyObject *owner = TOP();
2314 PyObject *v = SECOND();
2315 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002316 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002317 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002318 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002319 Py_DECREF(owner);
2320 if (err != 0)
2321 goto error;
2322 DISPATCH();
2323 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002324
Benjamin Petersonddd19492018-09-16 22:38:02 -07002325 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002326 PyObject *name = GETITEM(names, oparg);
2327 PyObject *owner = POP();
2328 int err;
2329 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2330 Py_DECREF(owner);
2331 if (err != 0)
2332 goto error;
2333 DISPATCH();
2334 }
2335
Benjamin Petersonddd19492018-09-16 22:38:02 -07002336 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002337 PyObject *name = GETITEM(names, oparg);
2338 PyObject *v = POP();
2339 int err;
2340 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002341 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002342 if (err != 0)
2343 goto error;
2344 DISPATCH();
2345 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002346
Benjamin Petersonddd19492018-09-16 22:38:02 -07002347 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002348 PyObject *name = GETITEM(names, oparg);
2349 int err;
2350 err = PyDict_DelItem(f->f_globals, name);
2351 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002352 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2353 format_exc_check_arg(tstate, PyExc_NameError,
2354 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002355 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002356 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002357 }
2358 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002359 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002360
Benjamin Petersonddd19492018-09-16 22:38:02 -07002361 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002362 PyObject *name = GETITEM(names, oparg);
2363 PyObject *locals = f->f_locals;
2364 PyObject *v;
2365 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002366 _PyErr_Format(tstate, PyExc_SystemError,
2367 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002368 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002369 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002370 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002371 v = PyDict_GetItemWithError(locals, name);
2372 if (v != NULL) {
2373 Py_INCREF(v);
2374 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002375 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002376 goto error;
2377 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002378 }
2379 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002380 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002381 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002382 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002383 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002384 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002385 }
2386 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002387 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002388 v = PyDict_GetItemWithError(f->f_globals, name);
2389 if (v != NULL) {
2390 Py_INCREF(v);
2391 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002392 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002393 goto error;
2394 }
2395 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002396 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002397 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002398 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002399 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002400 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002401 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002402 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002403 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002404 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002405 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002406 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002407 }
2408 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002409 v = PyObject_GetItem(f->f_builtins, name);
2410 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002411 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002412 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002413 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002414 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002415 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002416 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002417 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002418 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002419 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002420 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002421 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002422 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002423 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002424
Benjamin Petersonddd19492018-09-16 22:38:02 -07002425 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002426 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002427 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002428 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002429 && PyDict_CheckExact(f->f_builtins))
2430 {
Inada Naoki91234a12019-06-03 21:30:58 +09002431 OPCACHE_CHECK();
2432 if (co_opcache != NULL && co_opcache->optimized > 0) {
2433 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2434
2435 if (lg->globals_ver ==
2436 ((PyDictObject *)f->f_globals)->ma_version_tag
2437 && lg->builtins_ver ==
2438 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2439 {
2440 PyObject *ptr = lg->ptr;
2441 OPCACHE_STAT_GLOBAL_HIT();
2442 assert(ptr != NULL);
2443 Py_INCREF(ptr);
2444 PUSH(ptr);
2445 DISPATCH();
2446 }
2447 }
2448
2449 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002450 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002451 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002452 name);
2453 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002454 if (!_PyErr_OCCURRED()) {
2455 /* _PyDict_LoadGlobal() returns NULL without raising
2456 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002457 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002458 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002459 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002460 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002461 }
Inada Naoki91234a12019-06-03 21:30:58 +09002462
2463 if (co_opcache != NULL) {
2464 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2465
2466 if (co_opcache->optimized == 0) {
2467 /* Wasn't optimized before. */
2468 OPCACHE_STAT_GLOBAL_OPT();
2469 } else {
2470 OPCACHE_STAT_GLOBAL_MISS();
2471 }
2472
2473 co_opcache->optimized = 1;
2474 lg->globals_ver =
2475 ((PyDictObject *)f->f_globals)->ma_version_tag;
2476 lg->builtins_ver =
2477 ((PyDictObject *)f->f_builtins)->ma_version_tag;
2478 lg->ptr = v; /* borrowed */
2479 }
2480
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002481 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002482 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002483 else {
2484 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002485
2486 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09002487 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002488 v = PyObject_GetItem(f->f_globals, name);
2489 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002490 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002491 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002492 }
2493 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002494
Victor Stinnerb4efc962015-11-20 09:24:02 +01002495 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002496 v = PyObject_GetItem(f->f_builtins, name);
2497 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002498 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002499 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002500 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002501 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002502 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002503 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002504 }
2505 }
2506 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002507 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002508 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002509 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002510
Benjamin Petersonddd19492018-09-16 22:38:02 -07002511 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002512 PyObject *v = GETLOCAL(oparg);
2513 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002514 SETLOCAL(oparg, NULL);
2515 DISPATCH();
2516 }
2517 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002518 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002519 UNBOUNDLOCAL_ERROR_MSG,
2520 PyTuple_GetItem(co->co_varnames, oparg)
2521 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002522 goto error;
2523 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002524
Benjamin Petersonddd19492018-09-16 22:38:02 -07002525 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002526 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002527 PyObject *oldobj = PyCell_GET(cell);
2528 if (oldobj != NULL) {
2529 PyCell_SET(cell, NULL);
2530 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002531 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002532 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002533 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002534 goto error;
2535 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002536
Benjamin Petersonddd19492018-09-16 22:38:02 -07002537 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002538 PyObject *cell = freevars[oparg];
2539 Py_INCREF(cell);
2540 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002541 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002542 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002543
Benjamin Petersonddd19492018-09-16 22:38:02 -07002544 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002545 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002546 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002547 assert(locals);
2548 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2549 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2550 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2551 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2552 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002553 value = PyDict_GetItemWithError(locals, name);
2554 if (value != NULL) {
2555 Py_INCREF(value);
2556 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002557 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002558 goto error;
2559 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002560 }
2561 else {
2562 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002563 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002564 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002565 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002566 }
2567 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002568 }
2569 }
2570 if (!value) {
2571 PyObject *cell = freevars[oparg];
2572 value = PyCell_GET(cell);
2573 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002574 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002575 goto error;
2576 }
2577 Py_INCREF(value);
2578 }
2579 PUSH(value);
2580 DISPATCH();
2581 }
2582
Benjamin Petersonddd19492018-09-16 22:38:02 -07002583 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002584 PyObject *cell = freevars[oparg];
2585 PyObject *value = PyCell_GET(cell);
2586 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002587 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002588 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002589 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002590 Py_INCREF(value);
2591 PUSH(value);
2592 DISPATCH();
2593 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002594
Benjamin Petersonddd19492018-09-16 22:38:02 -07002595 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002596 PyObject *v = POP();
2597 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002598 PyObject *oldobj = PyCell_GET(cell);
2599 PyCell_SET(cell, v);
2600 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002601 DISPATCH();
2602 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002603
Benjamin Petersonddd19492018-09-16 22:38:02 -07002604 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002605 PyObject *str;
2606 PyObject *empty = PyUnicode_New(0, 0);
2607 if (empty == NULL) {
2608 goto error;
2609 }
2610 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2611 Py_DECREF(empty);
2612 if (str == NULL)
2613 goto error;
2614 while (--oparg >= 0) {
2615 PyObject *item = POP();
2616 Py_DECREF(item);
2617 }
2618 PUSH(str);
2619 DISPATCH();
2620 }
2621
Benjamin Petersonddd19492018-09-16 22:38:02 -07002622 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002623 PyObject *tup = PyTuple_New(oparg);
2624 if (tup == NULL)
2625 goto error;
2626 while (--oparg >= 0) {
2627 PyObject *item = POP();
2628 PyTuple_SET_ITEM(tup, oparg, item);
2629 }
2630 PUSH(tup);
2631 DISPATCH();
2632 }
2633
Benjamin Petersonddd19492018-09-16 22:38:02 -07002634 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002635 PyObject *list = PyList_New(oparg);
2636 if (list == NULL)
2637 goto error;
2638 while (--oparg >= 0) {
2639 PyObject *item = POP();
2640 PyList_SET_ITEM(list, oparg, item);
2641 }
2642 PUSH(list);
2643 DISPATCH();
2644 }
2645
Mark Shannon13bc1392020-01-23 09:25:17 +00002646 case TARGET(LIST_TO_TUPLE): {
2647 PyObject *list = POP();
2648 PyObject *tuple = PyList_AsTuple(list);
2649 Py_DECREF(list);
2650 if (tuple == NULL) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002651 goto error;
Mark Shannon13bc1392020-01-23 09:25:17 +00002652 }
2653 PUSH(tuple);
2654 DISPATCH();
2655 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002656
Mark Shannon13bc1392020-01-23 09:25:17 +00002657 case TARGET(LIST_EXTEND): {
2658 PyObject *iterable = POP();
2659 PyObject *list = PEEK(oparg);
2660 PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
2661 if (none_val == NULL) {
2662 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01002663 (Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable)))
Mark Shannon13bc1392020-01-23 09:25:17 +00002664 {
Victor Stinner61f4db82020-01-28 03:37:45 +01002665 _PyErr_Clear(tstate);
Mark Shannon13bc1392020-01-23 09:25:17 +00002666 _PyErr_Format(tstate, PyExc_TypeError,
2667 "Value after * must be an iterable, not %.200s",
2668 Py_TYPE(iterable)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002669 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002670 Py_DECREF(iterable);
2671 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002672 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002673 Py_DECREF(none_val);
2674 Py_DECREF(iterable);
2675 DISPATCH();
2676 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002677
Mark Shannon13bc1392020-01-23 09:25:17 +00002678 case TARGET(SET_UPDATE): {
2679 PyObject *iterable = POP();
2680 PyObject *set = PEEK(oparg);
2681 int err = _PySet_Update(set, iterable);
2682 Py_DECREF(iterable);
2683 if (err < 0) {
2684 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002685 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002686 DISPATCH();
2687 }
2688
Benjamin Petersonddd19492018-09-16 22:38:02 -07002689 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002690 PyObject *set = PySet_New(NULL);
2691 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002692 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002693 if (set == NULL)
2694 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002695 for (i = oparg; i > 0; i--) {
2696 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002697 if (err == 0)
2698 err = PySet_Add(set, item);
2699 Py_DECREF(item);
2700 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002701 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002702 if (err != 0) {
2703 Py_DECREF(set);
2704 goto error;
2705 }
2706 PUSH(set);
2707 DISPATCH();
2708 }
2709
Benjamin Petersonddd19492018-09-16 22:38:02 -07002710 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002711 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002712 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2713 if (map == NULL)
2714 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002715 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002716 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002717 PyObject *key = PEEK(2*i);
2718 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002719 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002720 if (err != 0) {
2721 Py_DECREF(map);
2722 goto error;
2723 }
2724 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002725
2726 while (oparg--) {
2727 Py_DECREF(POP());
2728 Py_DECREF(POP());
2729 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002730 PUSH(map);
2731 DISPATCH();
2732 }
2733
Benjamin Petersonddd19492018-09-16 22:38:02 -07002734 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002735 _Py_IDENTIFIER(__annotations__);
2736 int err;
2737 PyObject *ann_dict;
2738 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002739 _PyErr_Format(tstate, PyExc_SystemError,
2740 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002741 goto error;
2742 }
2743 /* check if __annotations__ in locals()... */
2744 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002745 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002746 &PyId___annotations__);
2747 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002748 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002749 goto error;
2750 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002751 /* ...if not, create a new one */
2752 ann_dict = PyDict_New();
2753 if (ann_dict == NULL) {
2754 goto error;
2755 }
2756 err = _PyDict_SetItemId(f->f_locals,
2757 &PyId___annotations__, ann_dict);
2758 Py_DECREF(ann_dict);
2759 if (err != 0) {
2760 goto error;
2761 }
2762 }
2763 }
2764 else {
2765 /* do the same if locals() is not a dict */
2766 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2767 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002768 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002769 }
2770 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2771 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002772 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002773 goto error;
2774 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002775 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002776 ann_dict = PyDict_New();
2777 if (ann_dict == NULL) {
2778 goto error;
2779 }
2780 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2781 Py_DECREF(ann_dict);
2782 if (err != 0) {
2783 goto error;
2784 }
2785 }
2786 else {
2787 Py_DECREF(ann_dict);
2788 }
2789 }
2790 DISPATCH();
2791 }
2792
Benjamin Petersonddd19492018-09-16 22:38:02 -07002793 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002794 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002795 PyObject *map;
2796 PyObject *keys = TOP();
2797 if (!PyTuple_CheckExact(keys) ||
2798 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002799 _PyErr_SetString(tstate, PyExc_SystemError,
2800 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002801 goto error;
2802 }
2803 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2804 if (map == NULL) {
2805 goto error;
2806 }
2807 for (i = oparg; i > 0; i--) {
2808 int err;
2809 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2810 PyObject *value = PEEK(i + 1);
2811 err = PyDict_SetItem(map, key, value);
2812 if (err != 0) {
2813 Py_DECREF(map);
2814 goto error;
2815 }
2816 }
2817
2818 Py_DECREF(POP());
2819 while (oparg--) {
2820 Py_DECREF(POP());
2821 }
2822 PUSH(map);
2823 DISPATCH();
2824 }
2825
Mark Shannon8a4cd702020-01-27 09:57:45 +00002826 case TARGET(DICT_UPDATE): {
2827 PyObject *update = POP();
2828 PyObject *dict = PEEK(oparg);
2829 if (PyDict_Update(dict, update) < 0) {
2830 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
2831 _PyErr_Format(tstate, PyExc_TypeError,
2832 "'%.200s' object is not a mapping",
Victor Stinnera102ed72020-02-07 02:24:48 +01002833 Py_TYPE(update)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002834 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002835 Py_DECREF(update);
2836 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002837 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002838 Py_DECREF(update);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002839 DISPATCH();
2840 }
2841
Mark Shannon8a4cd702020-01-27 09:57:45 +00002842 case TARGET(DICT_MERGE): {
2843 PyObject *update = POP();
2844 PyObject *dict = PEEK(oparg);
2845
2846 if (_PyDict_MergeEx(dict, update, 2) < 0) {
2847 format_kwargs_error(tstate, PEEK(2 + oparg), update);
2848 Py_DECREF(update);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002849 goto error;
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002850 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002851 Py_DECREF(update);
Brandt Bucherf185a732019-09-28 17:12:49 -07002852 PREDICT(CALL_FUNCTION_EX);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002853 DISPATCH();
2854 }
2855
Benjamin Petersonddd19492018-09-16 22:38:02 -07002856 case TARGET(MAP_ADD): {
Jörn Heisslerc8a35412019-06-22 16:40:55 +02002857 PyObject *value = TOP();
2858 PyObject *key = SECOND();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002859 PyObject *map;
2860 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002861 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002862 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002863 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002864 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002865 Py_DECREF(value);
2866 Py_DECREF(key);
2867 if (err != 0)
2868 goto error;
2869 PREDICT(JUMP_ABSOLUTE);
2870 DISPATCH();
2871 }
2872
Benjamin Petersonddd19492018-09-16 22:38:02 -07002873 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002874 PyObject *name = GETITEM(names, oparg);
2875 PyObject *owner = TOP();
2876 PyObject *res = PyObject_GetAttr(owner, name);
2877 Py_DECREF(owner);
2878 SET_TOP(res);
2879 if (res == NULL)
2880 goto error;
2881 DISPATCH();
2882 }
2883
Benjamin Petersonddd19492018-09-16 22:38:02 -07002884 case TARGET(COMPARE_OP): {
Mark Shannon9af0e472020-01-14 10:12:45 +00002885 assert(oparg <= Py_GE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002886 PyObject *right = POP();
2887 PyObject *left = TOP();
Mark Shannon9af0e472020-01-14 10:12:45 +00002888 PyObject *res = PyObject_RichCompare(left, right, oparg);
2889 SET_TOP(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002890 Py_DECREF(left);
2891 Py_DECREF(right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002892 if (res == NULL)
2893 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002894 PREDICT(POP_JUMP_IF_FALSE);
2895 PREDICT(POP_JUMP_IF_TRUE);
2896 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002897 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002898
Mark Shannon9af0e472020-01-14 10:12:45 +00002899 case TARGET(IS_OP): {
2900 PyObject *right = POP();
2901 PyObject *left = TOP();
2902 int res = (left == right)^oparg;
2903 PyObject *b = res ? Py_True : Py_False;
2904 Py_INCREF(b);
2905 SET_TOP(b);
2906 Py_DECREF(left);
2907 Py_DECREF(right);
2908 PREDICT(POP_JUMP_IF_FALSE);
2909 PREDICT(POP_JUMP_IF_TRUE);
2910 FAST_DISPATCH();
2911 }
2912
2913 case TARGET(CONTAINS_OP): {
2914 PyObject *right = POP();
2915 PyObject *left = POP();
2916 int res = PySequence_Contains(right, left);
2917 Py_DECREF(left);
2918 Py_DECREF(right);
2919 if (res < 0) {
2920 goto error;
2921 }
2922 PyObject *b = (res^oparg) ? Py_True : Py_False;
2923 Py_INCREF(b);
2924 PUSH(b);
2925 PREDICT(POP_JUMP_IF_FALSE);
2926 PREDICT(POP_JUMP_IF_TRUE);
2927 FAST_DISPATCH();
2928 }
2929
2930#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
2931 "BaseException is not allowed"
2932
2933 case TARGET(JUMP_IF_NOT_EXC_MATCH): {
2934 PyObject *right = POP();
2935 PyObject *left = POP();
2936 if (PyTuple_Check(right)) {
2937 Py_ssize_t i, length;
2938 length = PyTuple_GET_SIZE(right);
2939 for (i = 0; i < length; i++) {
2940 PyObject *exc = PyTuple_GET_ITEM(right, i);
2941 if (!PyExceptionClass_Check(exc)) {
2942 _PyErr_SetString(tstate, PyExc_TypeError,
2943 CANNOT_CATCH_MSG);
2944 Py_DECREF(left);
2945 Py_DECREF(right);
2946 goto error;
2947 }
2948 }
2949 }
2950 else {
2951 if (!PyExceptionClass_Check(right)) {
2952 _PyErr_SetString(tstate, PyExc_TypeError,
2953 CANNOT_CATCH_MSG);
2954 Py_DECREF(left);
2955 Py_DECREF(right);
2956 goto error;
2957 }
2958 }
2959 int res = PyErr_GivenExceptionMatches(left, right);
2960 Py_DECREF(left);
2961 Py_DECREF(right);
2962 if (res > 0) {
2963 /* Exception matches -- Do nothing */;
2964 }
2965 else if (res == 0) {
2966 JUMPTO(oparg);
2967 }
2968 else {
2969 goto error;
2970 }
2971 DISPATCH();
2972 }
2973
Benjamin Petersonddd19492018-09-16 22:38:02 -07002974 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002975 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002976 PyObject *fromlist = POP();
2977 PyObject *level = TOP();
2978 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02002979 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002980 Py_DECREF(level);
2981 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002982 SET_TOP(res);
2983 if (res == NULL)
2984 goto error;
2985 DISPATCH();
2986 }
2987
Benjamin Petersonddd19492018-09-16 22:38:02 -07002988 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002989 PyObject *from = POP(), *locals;
2990 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002991 if (PyFrame_FastToLocalsWithError(f) < 0) {
2992 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002993 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002994 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002995
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002996 locals = f->f_locals;
2997 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002998 _PyErr_SetString(tstate, PyExc_SystemError,
2999 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003000 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003001 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003002 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003003 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003004 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003005 Py_DECREF(from);
3006 if (err != 0)
3007 goto error;
3008 DISPATCH();
3009 }
Guido van Rossum25831651993-05-19 14:50:45 +00003010
Benjamin Petersonddd19492018-09-16 22:38:02 -07003011 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003012 PyObject *name = GETITEM(names, oparg);
3013 PyObject *from = TOP();
3014 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003015 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003016 PUSH(res);
3017 if (res == NULL)
3018 goto error;
3019 DISPATCH();
3020 }
Thomas Wouters52152252000-08-17 22:55:00 +00003021
Benjamin Petersonddd19492018-09-16 22:38:02 -07003022 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003023 JUMPBY(oparg);
3024 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003025 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003026
Benjamin Petersonddd19492018-09-16 22:38:02 -07003027 case TARGET(POP_JUMP_IF_FALSE): {
3028 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003029 PyObject *cond = POP();
3030 int err;
3031 if (cond == Py_True) {
3032 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003033 FAST_DISPATCH();
3034 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003035 if (cond == Py_False) {
3036 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003037 JUMPTO(oparg);
3038 FAST_DISPATCH();
3039 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003040 err = PyObject_IsTrue(cond);
3041 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003042 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003043 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003044 else if (err == 0)
3045 JUMPTO(oparg);
3046 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003047 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003048 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003049 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003050
Benjamin Petersonddd19492018-09-16 22:38:02 -07003051 case TARGET(POP_JUMP_IF_TRUE): {
3052 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003053 PyObject *cond = POP();
3054 int err;
3055 if (cond == Py_False) {
3056 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003057 FAST_DISPATCH();
3058 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003059 if (cond == Py_True) {
3060 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003061 JUMPTO(oparg);
3062 FAST_DISPATCH();
3063 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003064 err = PyObject_IsTrue(cond);
3065 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003066 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003067 JUMPTO(oparg);
3068 }
3069 else if (err == 0)
3070 ;
3071 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003072 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003073 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003074 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003075
Benjamin Petersonddd19492018-09-16 22:38:02 -07003076 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003077 PyObject *cond = TOP();
3078 int err;
3079 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003080 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003081 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003082 FAST_DISPATCH();
3083 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003084 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003085 JUMPTO(oparg);
3086 FAST_DISPATCH();
3087 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003088 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003089 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003090 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003091 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003092 }
3093 else if (err == 0)
3094 JUMPTO(oparg);
3095 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003096 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003097 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003098 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003099
Benjamin Petersonddd19492018-09-16 22:38:02 -07003100 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003101 PyObject *cond = TOP();
3102 int err;
3103 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003104 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003105 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003106 FAST_DISPATCH();
3107 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003108 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003109 JUMPTO(oparg);
3110 FAST_DISPATCH();
3111 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003112 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003113 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003114 JUMPTO(oparg);
3115 }
3116 else 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
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003121 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003122 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003123 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003124
Benjamin Petersonddd19492018-09-16 22:38:02 -07003125 case TARGET(JUMP_ABSOLUTE): {
3126 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003127 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003128#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003129 /* Enabling this path speeds-up all while and for-loops by bypassing
3130 the per-loop checks for signals. By default, this should be turned-off
3131 because it prevents detection of a control-break in tight loops like
3132 "while 1: pass". Compile with this option turned-on when you need
3133 the speed-up and do not need break checking inside tight loops (ones
3134 that contain only instructions ending with FAST_DISPATCH).
3135 */
3136 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003137#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003138 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003139#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003140 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003141
Benjamin Petersonddd19492018-09-16 22:38:02 -07003142 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003143 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003144 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003145 PyObject *iter = PyObject_GetIter(iterable);
3146 Py_DECREF(iterable);
3147 SET_TOP(iter);
3148 if (iter == NULL)
3149 goto error;
3150 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003151 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003152 DISPATCH();
3153 }
3154
Benjamin Petersonddd19492018-09-16 22:38:02 -07003155 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04003156 /* before: [obj]; after [getiter(obj)] */
3157 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003158 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003159 if (PyCoro_CheckExact(iterable)) {
3160 /* `iterable` is a coroutine */
3161 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3162 /* and it is used in a 'yield from' expression of a
3163 regular generator. */
3164 Py_DECREF(iterable);
3165 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003166 _PyErr_SetString(tstate, PyExc_TypeError,
3167 "cannot 'yield from' a coroutine object "
3168 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04003169 goto error;
3170 }
3171 }
3172 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003173 /* `iterable` is not a generator. */
3174 iter = PyObject_GetIter(iterable);
3175 Py_DECREF(iterable);
3176 SET_TOP(iter);
3177 if (iter == NULL)
3178 goto error;
3179 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003180 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003181 DISPATCH();
3182 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003183
Benjamin Petersonddd19492018-09-16 22:38:02 -07003184 case TARGET(FOR_ITER): {
3185 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003186 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003187 PyObject *iter = TOP();
Victor Stinnera102ed72020-02-07 02:24:48 +01003188 PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003189 if (next != NULL) {
3190 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003191 PREDICT(STORE_FAST);
3192 PREDICT(UNPACK_SEQUENCE);
3193 DISPATCH();
3194 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003195 if (_PyErr_Occurred(tstate)) {
3196 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003197 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003198 }
3199 else if (tstate->c_tracefunc != NULL) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003200 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Victor Stinner438a12d2019-05-24 17:01:38 +02003201 }
3202 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003203 }
3204 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003205 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003206 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003207 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003208 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003209 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003210 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003211
Benjamin Petersonddd19492018-09-16 22:38:02 -07003212 case TARGET(SETUP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003213 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003214 STACK_LEVEL());
3215 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003216 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003217
Benjamin Petersonddd19492018-09-16 22:38:02 -07003218 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003219 _Py_IDENTIFIER(__aenter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003220 _Py_IDENTIFIER(__aexit__);
Yury Selivanov75445082015-05-11 22:57:16 -04003221 PyObject *mgr = TOP();
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003222 PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04003223 PyObject *res;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003224 if (enter == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003225 goto error;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003226 }
3227 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__);
3228 if (exit == NULL) {
3229 Py_DECREF(enter);
3230 goto error;
3231 }
Yury Selivanov75445082015-05-11 22:57:16 -04003232 SET_TOP(exit);
Yury Selivanov75445082015-05-11 22:57:16 -04003233 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003234 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003235 Py_DECREF(enter);
3236 if (res == NULL)
3237 goto error;
3238 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003239 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003240 DISPATCH();
3241 }
3242
Benjamin Petersonddd19492018-09-16 22:38:02 -07003243 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003244 PyObject *res = POP();
3245 /* Setup the finally block before pushing the result
3246 of __aenter__ on the stack. */
3247 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3248 STACK_LEVEL());
3249 PUSH(res);
3250 DISPATCH();
3251 }
3252
Benjamin Petersonddd19492018-09-16 22:38:02 -07003253 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003254 _Py_IDENTIFIER(__enter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003255 _Py_IDENTIFIER(__exit__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003256 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003257 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003258 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003259 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08003260 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003261 }
3262 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003263 if (exit == NULL) {
3264 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003265 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003266 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003267 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003268 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003269 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003270 Py_DECREF(enter);
3271 if (res == NULL)
3272 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003273 /* Setup the finally block before pushing the result
3274 of __enter__ on the stack. */
3275 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3276 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003277
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003278 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003279 DISPATCH();
3280 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003281
Mark Shannonfee55262019-11-21 09:11:43 +00003282 case TARGET(WITH_EXCEPT_START): {
3283 /* At the top of the stack are 7 values:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003284 - (TOP, SECOND, THIRD) = exc_info()
Mark Shannonfee55262019-11-21 09:11:43 +00003285 - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER
3286 - SEVENTH: the context.__exit__ bound method
3287 We call SEVENTH(TOP, SECOND, THIRD).
3288 Then we push again the TOP exception and the __exit__
3289 return value.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003290 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003291 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003292 PyObject *exc, *val, *tb, *res;
3293
Victor Stinner842cfff2016-12-01 14:45:31 +01003294 exc = TOP();
Mark Shannonfee55262019-11-21 09:11:43 +00003295 val = SECOND();
3296 tb = THIRD();
3297 assert(exc != Py_None);
3298 assert(!PyLong_Check(exc));
3299 exit_func = PEEK(7);
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003300 PyObject *stack[4] = {NULL, exc, val, tb};
Petr Viktorinffd97532020-02-11 17:46:57 +01003301 res = PyObject_Vectorcall(exit_func, stack + 1,
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003302 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003303 if (res == NULL)
3304 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003305
Yury Selivanov75445082015-05-11 22:57:16 -04003306 PUSH(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003307 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003308 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003309
Benjamin Petersonddd19492018-09-16 22:38:02 -07003310 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10003311 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003312 PyObject *name = GETITEM(names, oparg);
3313 PyObject *obj = TOP();
3314 PyObject *meth = NULL;
3315
3316 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3317
Yury Selivanovf2392132016-12-13 19:03:51 -05003318 if (meth == NULL) {
3319 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003320 goto error;
3321 }
3322
3323 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003324 /* We can bypass temporary bound method object.
3325 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003326
INADA Naoki015bce62017-01-16 17:23:30 +09003327 meth | self | arg1 | ... | argN
3328 */
3329 SET_TOP(meth);
3330 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003331 }
3332 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003333 /* meth is not an unbound method (but a regular attr, or
3334 something was returned by a descriptor protocol). Set
3335 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003336 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003337
3338 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003339 */
INADA Naoki015bce62017-01-16 17:23:30 +09003340 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003341 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003342 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003343 }
3344 DISPATCH();
3345 }
3346
Benjamin Petersonddd19492018-09-16 22:38:02 -07003347 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003348 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003349 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003350
3351 sp = stack_pointer;
3352
INADA Naoki015bce62017-01-16 17:23:30 +09003353 meth = PEEK(oparg + 2);
3354 if (meth == NULL) {
3355 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3356 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003357
3358 Stack layout:
3359
INADA Naoki015bce62017-01-16 17:23:30 +09003360 ... | NULL | callable | arg1 | ... | argN
3361 ^- TOP()
3362 ^- (-oparg)
3363 ^- (-oparg-1)
3364 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003365
Ville Skyttä49b27342017-08-03 09:00:59 +03003366 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003367 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003368 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003369 res = call_function(tstate, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003370 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003371 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003372 }
3373 else {
3374 /* This is a method call. Stack layout:
3375
INADA Naoki015bce62017-01-16 17:23:30 +09003376 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003377 ^- TOP()
3378 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003379 ^- (-oparg-1)
3380 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003381
INADA Naoki015bce62017-01-16 17:23:30 +09003382 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003383 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003384 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003385 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003386 res = call_function(tstate, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003387 stack_pointer = sp;
3388 }
3389
3390 PUSH(res);
3391 if (res == NULL)
3392 goto error;
3393 DISPATCH();
3394 }
3395
Benjamin Petersonddd19492018-09-16 22:38:02 -07003396 case TARGET(CALL_FUNCTION): {
3397 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003398 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003399 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003400 res = call_function(tstate, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003401 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003402 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003403 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003404 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003405 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003406 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003407 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003408
Benjamin Petersonddd19492018-09-16 22:38:02 -07003409 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003410 PyObject **sp, *res, *names;
3411
3412 names = POP();
Jeroen Demeyer05677862019-08-16 12:41:27 +02003413 assert(PyTuple_Check(names));
3414 assert(PyTuple_GET_SIZE(names) <= oparg);
3415 /* We assume without checking that names contains only strings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003416 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003417 res = call_function(tstate, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003418 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003419 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003420 Py_DECREF(names);
3421
3422 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003423 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003424 }
3425 DISPATCH();
3426 }
3427
Benjamin Petersonddd19492018-09-16 22:38:02 -07003428 case TARGET(CALL_FUNCTION_EX): {
Brandt Bucherf185a732019-09-28 17:12:49 -07003429 PREDICTED(CALL_FUNCTION_EX);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003430 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003431 if (oparg & 0x01) {
3432 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003433 if (!PyDict_CheckExact(kwargs)) {
3434 PyObject *d = PyDict_New();
3435 if (d == NULL)
3436 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003437 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003438 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02003439 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003440 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003441 goto error;
3442 }
3443 Py_DECREF(kwargs);
3444 kwargs = d;
3445 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003446 assert(PyDict_CheckExact(kwargs));
3447 }
3448 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003449 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003450 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003451 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003452 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003453 goto error;
3454 }
3455 Py_SETREF(callargs, PySequence_Tuple(callargs));
3456 if (callargs == NULL) {
3457 goto error;
3458 }
3459 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003460 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003461
Victor Stinner09532fe2019-05-10 23:39:09 +02003462 result = do_call_core(tstate, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003463 Py_DECREF(func);
3464 Py_DECREF(callargs);
3465 Py_XDECREF(kwargs);
3466
3467 SET_TOP(result);
3468 if (result == NULL) {
3469 goto error;
3470 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003471 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003472 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003473
Benjamin Petersonddd19492018-09-16 22:38:02 -07003474 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003475 PyObject *qualname = POP();
3476 PyObject *codeobj = POP();
3477 PyFunctionObject *func = (PyFunctionObject *)
3478 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003479
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003480 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003481 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003482 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003483 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003484 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003485
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003486 if (oparg & 0x08) {
3487 assert(PyTuple_CheckExact(TOP()));
3488 func ->func_closure = POP();
3489 }
3490 if (oparg & 0x04) {
3491 assert(PyDict_CheckExact(TOP()));
3492 func->func_annotations = POP();
3493 }
3494 if (oparg & 0x02) {
3495 assert(PyDict_CheckExact(TOP()));
3496 func->func_kwdefaults = POP();
3497 }
3498 if (oparg & 0x01) {
3499 assert(PyTuple_CheckExact(TOP()));
3500 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003501 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003502
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003503 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003504 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003505 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003506
Benjamin Petersonddd19492018-09-16 22:38:02 -07003507 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003508 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003509 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003510 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003511 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003512 step = NULL;
3513 stop = POP();
3514 start = TOP();
3515 slice = PySlice_New(start, stop, step);
3516 Py_DECREF(start);
3517 Py_DECREF(stop);
3518 Py_XDECREF(step);
3519 SET_TOP(slice);
3520 if (slice == NULL)
3521 goto error;
3522 DISPATCH();
3523 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003524
Benjamin Petersonddd19492018-09-16 22:38:02 -07003525 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003526 /* Handles f-string value formatting. */
3527 PyObject *result;
3528 PyObject *fmt_spec;
3529 PyObject *value;
3530 PyObject *(*conv_fn)(PyObject *);
3531 int which_conversion = oparg & FVC_MASK;
3532 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3533
3534 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003535 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003536
3537 /* See if any conversion is specified. */
3538 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003539 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003540 case FVC_STR: conv_fn = PyObject_Str; break;
3541 case FVC_REPR: conv_fn = PyObject_Repr; break;
3542 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003543 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02003544 _PyErr_Format(tstate, PyExc_SystemError,
3545 "unexpected conversion flag %d",
3546 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003547 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003548 }
3549
3550 /* If there's a conversion function, call it and replace
3551 value with that result. Otherwise, just use value,
3552 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003553 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003554 result = conv_fn(value);
3555 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003556 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003557 Py_XDECREF(fmt_spec);
3558 goto error;
3559 }
3560 value = result;
3561 }
3562
3563 /* If value is a unicode object, and there's no fmt_spec,
3564 then we know the result of format(value) is value
3565 itself. In that case, skip calling format(). I plan to
3566 move this optimization in to PyObject_Format()
3567 itself. */
3568 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3569 /* Do nothing, just transfer ownership to result. */
3570 result = value;
3571 } else {
3572 /* Actually call format(). */
3573 result = PyObject_Format(value, fmt_spec);
3574 Py_DECREF(value);
3575 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003576 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003577 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003578 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003579 }
3580
Eric V. Smith135d5f42016-02-05 18:23:08 -05003581 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003582 DISPATCH();
3583 }
3584
Benjamin Petersonddd19492018-09-16 22:38:02 -07003585 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003586 int oldoparg = oparg;
3587 NEXTOPARG();
3588 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003589 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003590 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003591
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003592
Antoine Pitrou042b1282010-08-13 21:15:58 +00003593#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003594 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003595#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003596 default:
3597 fprintf(stderr,
3598 "XXX lineno: %d, opcode: %d\n",
3599 PyFrame_GetLineNumber(f),
3600 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02003601 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003602 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003604 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003605
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003606 /* This should never be reached. Every opcode should end with DISPATCH()
3607 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003608 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003609
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003610error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003611 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003612#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02003613 if (!_PyErr_Occurred(tstate)) {
3614 _PyErr_SetString(tstate, PyExc_SystemError,
3615 "error return without exception set");
3616 }
Victor Stinner365b6932013-07-12 00:11:58 +02003617#else
Victor Stinner438a12d2019-05-24 17:01:38 +02003618 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02003619#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003620
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003621 /* Log traceback info. */
3622 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003623
Benjamin Peterson51f46162013-01-23 08:38:47 -05003624 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003625 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3626 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003627
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003628exception_unwind:
3629 /* Unwind stacks if an exception occurred */
3630 while (f->f_iblock > 0) {
3631 /* Pop the current block. */
3632 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003634 if (b->b_type == EXCEPT_HANDLER) {
3635 UNWIND_EXCEPT_HANDLER(b);
3636 continue;
3637 }
3638 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003639 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003640 PyObject *exc, *val, *tb;
3641 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003642 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003643 /* Beware, this invalidates all b->b_* fields */
3644 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003645 PUSH(exc_info->exc_traceback);
3646 PUSH(exc_info->exc_value);
3647 if (exc_info->exc_type != NULL) {
3648 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003649 }
3650 else {
3651 Py_INCREF(Py_None);
3652 PUSH(Py_None);
3653 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003654 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003655 /* Make the raw exception data
3656 available to the handler,
3657 so a program can emulate the
3658 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02003659 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003660 if (tb != NULL)
3661 PyException_SetTraceback(val, tb);
3662 else
3663 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003664 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003665 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003666 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003667 exc_info->exc_value = val;
3668 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003669 if (tb == NULL)
3670 tb = Py_None;
3671 Py_INCREF(tb);
3672 PUSH(tb);
3673 PUSH(val);
3674 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003675 JUMPTO(handler);
Victor Stinnerdab84232020-03-17 18:56:44 +01003676 if (_Py_TracingPossible(ceval2)) {
Pablo Galindo4c53e632020-01-10 09:24:22 +00003677 int needs_new_execution_window = (f->f_lasti < instr_lb || f->f_lasti >= instr_ub);
3678 int needs_line_update = (f->f_lasti == instr_lb || f->f_lasti < instr_prev);
3679 /* Make sure that we trace line after exception if we are in a new execution
3680 * window or we don't need a line update and we are not in the first instruction
3681 * of the line. */
3682 if (needs_new_execution_window || (!needs_line_update && instr_lb > 0)) {
3683 instr_prev = INT_MAX;
3684 }
Mark Shannonfee55262019-11-21 09:11:43 +00003685 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003686 /* Resume normal execution */
3687 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003688 }
3689 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003690
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003691 /* End the loop as we still have an error */
3692 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003693 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003694
Pablo Galindof00828a2019-05-09 16:52:02 +01003695 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003696 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01003697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003698 /* Pop remaining stack entries. */
3699 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003700 PyObject *o = POP();
3701 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003702 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003703
Mark Shannone7c9f4a2020-01-13 12:51:26 +00003704exiting:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003705 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003706 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003707 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3708 tstate, f, PyTrace_RETURN, retval)) {
3709 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003710 }
3711 }
3712 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003713 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3714 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003715 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003716 }
3717 }
3718 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003719
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003720 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003721exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003722 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3723 dtrace_function_return(f);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01003724 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrou58720d62013-08-05 23:26:40 +02003725 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003726 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003727
Victor Stinner0b72b232020-03-12 23:18:39 +01003728 return _Py_CheckFunctionResult(tstate, NULL, retval, __func__);
Guido van Rossum374a9221991-04-04 10:40:29 +00003729}
3730
Benjamin Petersonb204a422011-06-05 22:04:07 -05003731static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003732format_missing(PyThreadState *tstate, const char *kind,
3733 PyCodeObject *co, PyObject *names)
Benjamin Petersone109c702011-06-24 09:37:26 -05003734{
3735 int err;
3736 Py_ssize_t len = PyList_GET_SIZE(names);
3737 PyObject *name_str, *comma, *tail, *tmp;
3738
3739 assert(PyList_CheckExact(names));
3740 assert(len >= 1);
3741 /* Deal with the joys of natural language. */
3742 switch (len) {
3743 case 1:
3744 name_str = PyList_GET_ITEM(names, 0);
3745 Py_INCREF(name_str);
3746 break;
3747 case 2:
3748 name_str = PyUnicode_FromFormat("%U and %U",
3749 PyList_GET_ITEM(names, len - 2),
3750 PyList_GET_ITEM(names, len - 1));
3751 break;
3752 default:
3753 tail = PyUnicode_FromFormat(", %U, and %U",
3754 PyList_GET_ITEM(names, len - 2),
3755 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003756 if (tail == NULL)
3757 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003758 /* Chop off the last two objects in the list. This shouldn't actually
3759 fail, but we can't be too careful. */
3760 err = PyList_SetSlice(names, len - 2, len, NULL);
3761 if (err == -1) {
3762 Py_DECREF(tail);
3763 return;
3764 }
3765 /* Stitch everything up into a nice comma-separated list. */
3766 comma = PyUnicode_FromString(", ");
3767 if (comma == NULL) {
3768 Py_DECREF(tail);
3769 return;
3770 }
3771 tmp = PyUnicode_Join(comma, names);
3772 Py_DECREF(comma);
3773 if (tmp == NULL) {
3774 Py_DECREF(tail);
3775 return;
3776 }
3777 name_str = PyUnicode_Concat(tmp, tail);
3778 Py_DECREF(tmp);
3779 Py_DECREF(tail);
3780 break;
3781 }
3782 if (name_str == NULL)
3783 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02003784 _PyErr_Format(tstate, PyExc_TypeError,
3785 "%U() missing %i required %s argument%s: %U",
3786 co->co_name,
3787 len,
3788 kind,
3789 len == 1 ? "" : "s",
3790 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05003791 Py_DECREF(name_str);
3792}
3793
3794static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003795missing_arguments(PyThreadState *tstate, PyCodeObject *co,
3796 Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003797 PyObject **fastlocals)
3798{
Victor Stinner74319ae2016-08-25 00:04:09 +02003799 Py_ssize_t i, j = 0;
3800 Py_ssize_t start, end;
3801 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003802 const char *kind = positional ? "positional" : "keyword-only";
3803 PyObject *missing_names;
3804
3805 /* Compute the names of the arguments that are missing. */
3806 missing_names = PyList_New(missing);
3807 if (missing_names == NULL)
3808 return;
3809 if (positional) {
3810 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01003811 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003812 }
3813 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003814 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003815 end = start + co->co_kwonlyargcount;
3816 }
3817 for (i = start; i < end; i++) {
3818 if (GETLOCAL(i) == NULL) {
3819 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3820 PyObject *name = PyObject_Repr(raw);
3821 if (name == NULL) {
3822 Py_DECREF(missing_names);
3823 return;
3824 }
3825 PyList_SET_ITEM(missing_names, j++, name);
3826 }
3827 }
3828 assert(j == missing);
Victor Stinner438a12d2019-05-24 17:01:38 +02003829 format_missing(tstate, kind, co, missing_names);
Benjamin Petersone109c702011-06-24 09:37:26 -05003830 Py_DECREF(missing_names);
3831}
3832
3833static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003834too_many_positional(PyThreadState *tstate, PyCodeObject *co,
3835 Py_ssize_t given, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003836 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003837{
3838 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003839 Py_ssize_t kwonly_given = 0;
3840 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003841 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003842 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003843
Benjamin Petersone109c702011-06-24 09:37:26 -05003844 assert((co->co_flags & CO_VARARGS) == 0);
3845 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01003846 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003847 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003848 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003849 }
3850 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003851 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01003852 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003853 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01003854 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003855 }
3856 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003857 plural = (co_argcount != 1);
3858 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003859 }
3860 if (sig == NULL)
3861 return;
3862 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003863 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3864 kwonly_sig = PyUnicode_FromFormat(format,
3865 given != 1 ? "s" : "",
3866 kwonly_given,
3867 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003868 if (kwonly_sig == NULL) {
3869 Py_DECREF(sig);
3870 return;
3871 }
3872 }
3873 else {
3874 /* This will not fail. */
3875 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003876 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003877 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003878 _PyErr_Format(tstate, PyExc_TypeError,
3879 "%U() takes %U positional argument%s but %zd%U %s given",
3880 co->co_name,
3881 sig,
3882 plural ? "s" : "",
3883 given,
3884 kwonly_sig,
3885 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003886 Py_DECREF(sig);
3887 Py_DECREF(kwonly_sig);
3888}
3889
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003890static int
Victor Stinner438a12d2019-05-24 17:01:38 +02003891positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
3892 Py_ssize_t kwcount, PyObject* const* kwnames)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003893{
3894 int posonly_conflicts = 0;
3895 PyObject* posonly_names = PyList_New(0);
3896
3897 for(int k=0; k < co->co_posonlyargcount; k++){
3898 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
3899
3900 for (int k2=0; k2<kwcount; k2++){
3901 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
3902 PyObject* kwname = kwnames[k2];
3903 if (kwname == posonly_name){
3904 if(PyList_Append(posonly_names, kwname) != 0) {
3905 goto fail;
3906 }
3907 posonly_conflicts++;
3908 continue;
3909 }
3910
3911 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
3912
3913 if ( cmp > 0) {
3914 if(PyList_Append(posonly_names, kwname) != 0) {
3915 goto fail;
3916 }
3917 posonly_conflicts++;
3918 } else if (cmp < 0) {
3919 goto fail;
3920 }
3921
3922 }
3923 }
3924 if (posonly_conflicts) {
3925 PyObject* comma = PyUnicode_FromString(", ");
3926 if (comma == NULL) {
3927 goto fail;
3928 }
3929 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
3930 Py_DECREF(comma);
3931 if (error_names == NULL) {
3932 goto fail;
3933 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003934 _PyErr_Format(tstate, PyExc_TypeError,
3935 "%U() got some positional-only arguments passed"
3936 " as keyword arguments: '%U'",
3937 co->co_name, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003938 Py_DECREF(error_names);
3939 goto fail;
3940 }
3941
3942 Py_DECREF(posonly_names);
3943 return 0;
3944
3945fail:
3946 Py_XDECREF(posonly_names);
3947 return 1;
3948
3949}
3950
Guido van Rossumc2e20742006-02-27 22:32:47 +00003951/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02003952 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003953 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003954
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003955PyObject *
Victor Stinnerb5e170f2019-11-16 01:03:22 +01003956_PyEval_EvalCode(PyThreadState *tstate,
3957 PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003958 PyObject *const *args, Py_ssize_t argcount,
3959 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003960 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003961 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003962 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003963 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003964{
Victor Stinnerb5e170f2019-11-16 01:03:22 +01003965 assert(tstate != NULL);
3966
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003967 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003968 PyFrameObject *f;
3969 PyObject *retval = NULL;
3970 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003971 PyObject *x, *u;
Pablo Galindocd74e662019-06-01 18:08:04 +01003972 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003973 Py_ssize_t i, j, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003974 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003975
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003976 if (globals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003977 _PyErr_SetString(tstate, PyExc_SystemError,
3978 "PyEval_EvalCodeEx: NULL globals");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003979 return NULL;
3980 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003981
Victor Stinnerc7020012016-08-16 23:40:29 +02003982 /* Create the frame */
INADA Naoki5a625d02016-12-24 20:19:08 +09003983 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003984 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003985 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003986 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003987 fastlocals = f->f_localsplus;
3988 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003989
Victor Stinnerc7020012016-08-16 23:40:29 +02003990 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003991 if (co->co_flags & CO_VARKEYWORDS) {
3992 kwdict = PyDict_New();
3993 if (kwdict == NULL)
3994 goto fail;
3995 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003996 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003997 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003998 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003999 SETLOCAL(i, kwdict);
4000 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004001 else {
4002 kwdict = NULL;
4003 }
4004
Pablo Galindocd74e662019-06-01 18:08:04 +01004005 /* Copy all positional arguments into local variables */
4006 if (argcount > co->co_argcount) {
4007 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02004008 }
4009 else {
4010 n = argcount;
4011 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004012 for (j = 0; j < n; j++) {
4013 x = args[j];
4014 Py_INCREF(x);
4015 SETLOCAL(j, x);
4016 }
4017
Victor Stinnerc7020012016-08-16 23:40:29 +02004018 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004019 if (co->co_flags & CO_VARARGS) {
Sergey Fedoseev234531b2019-02-25 21:59:12 +05004020 u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02004021 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004022 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004023 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004024 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004025 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004026
Serhiy Storchakab7281052016-09-12 00:52:40 +03004027 /* Handle keyword arguments passed as two strided arrays */
4028 kwcount *= kwstep;
4029 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004030 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03004031 PyObject *keyword = kwnames[i];
4032 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02004033 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004034
Benjamin Petersonb204a422011-06-05 22:04:07 -05004035 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004036 _PyErr_Format(tstate, PyExc_TypeError,
4037 "%U() keywords must be strings",
4038 co->co_name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004039 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004040 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004041
Benjamin Petersonb204a422011-06-05 22:04:07 -05004042 /* Speed hack: do raw pointer compares. As names are
4043 normally interned this should almost always hit. */
4044 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004045 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004046 PyObject *name = co_varnames[j];
4047 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004048 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004049 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004050 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004051
Benjamin Petersonb204a422011-06-05 22:04:07 -05004052 /* Slow fallback, just in case */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004053 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004054 PyObject *name = co_varnames[j];
4055 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
4056 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004057 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004058 }
4059 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004060 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004061 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004062 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004063
Victor Stinner231d1f32017-01-11 02:12:06 +01004064 assert(j >= total_args);
4065 if (kwdict == NULL) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004066
Victor Stinner438a12d2019-05-24 17:01:38 +02004067 if (co->co_posonlyargcount
4068 && positional_only_passed_as_keyword(tstate, co,
4069 kwcount, kwnames))
4070 {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004071 goto fail;
4072 }
4073
Victor Stinner438a12d2019-05-24 17:01:38 +02004074 _PyErr_Format(tstate, PyExc_TypeError,
4075 "%U() got an unexpected keyword argument '%S'",
4076 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004077 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004078 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004079
Christian Heimes0bd447f2013-07-20 14:48:10 +02004080 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4081 goto fail;
4082 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004083 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004084
Benjamin Petersonb204a422011-06-05 22:04:07 -05004085 kw_found:
4086 if (GETLOCAL(j) != NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004087 _PyErr_Format(tstate, PyExc_TypeError,
4088 "%U() got multiple values for argument '%S'",
4089 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004090 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004091 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004092 Py_INCREF(value);
4093 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004094 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004095
4096 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004097 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004098 too_many_positional(tstate, co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004099 goto fail;
4100 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004101
4102 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004103 if (argcount < co->co_argcount) {
4104 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004105 Py_ssize_t missing = 0;
4106 for (i = argcount; i < m; i++) {
4107 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004108 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004109 }
4110 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004111 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004112 missing_arguments(tstate, co, missing, defcount, fastlocals);
Benjamin Petersone109c702011-06-24 09:37:26 -05004113 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004114 }
4115 if (n > m)
4116 i = n - m;
4117 else
4118 i = 0;
4119 for (; i < defcount; i++) {
4120 if (GETLOCAL(m+i) == NULL) {
4121 PyObject *def = defs[i];
4122 Py_INCREF(def);
4123 SETLOCAL(m+i, def);
4124 }
4125 }
4126 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004127
4128 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004129 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004130 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004131 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004132 PyObject *name;
4133 if (GETLOCAL(i) != NULL)
4134 continue;
4135 name = PyTuple_GET_ITEM(co->co_varnames, i);
4136 if (kwdefs != NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004137 PyObject *def = PyDict_GetItemWithError(kwdefs, name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004138 if (def) {
4139 Py_INCREF(def);
4140 SETLOCAL(i, def);
4141 continue;
4142 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004143 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004144 goto fail;
4145 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004146 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004147 missing++;
4148 }
4149 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004150 missing_arguments(tstate, co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004151 goto fail;
4152 }
4153 }
4154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004155 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004156 vars into frame. */
4157 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004158 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004159 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004160 /* Possibly account for the cell variable being an argument. */
4161 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004162 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004163 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004164 /* Clear the local copy. */
4165 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004166 }
4167 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004168 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004169 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004170 if (c == NULL)
4171 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004172 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004173 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004174
4175 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004176 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4177 PyObject *o = PyTuple_GET_ITEM(closure, i);
4178 Py_INCREF(o);
4179 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004180 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004181
Yury Selivanoveb636452016-09-08 22:01:51 -07004182 /* Handle generator/coroutine/asynchronous generator */
4183 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004184 PyObject *gen;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004185 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004186
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004187 /* Don't need to keep the reference to f_back, it will be set
4188 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004189 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004191 /* Create a new generator that owns the ready to run frame
4192 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004193 if (is_coro) {
4194 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004195 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4196 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004197 } else {
4198 gen = PyGen_NewWithQualName(f, name, qualname);
4199 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004200 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004201 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004202 }
INADA Naoki9c157762016-12-26 18:52:46 +09004203
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004204 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004205
Yury Selivanov75445082015-05-11 22:57:16 -04004206 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004207 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004208
Victor Stinnerb9e68122019-11-14 12:20:46 +01004209 retval = _PyEval_EvalFrame(tstate, f, 0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004210
Thomas Woutersce272b62007-09-19 21:19:28 +00004211fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004212
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004213 /* decref'ing the frame can cause __del__ methods to get invoked,
4214 which can call back into Python. While we're done with the
4215 current Python frame (f), the associated C stack is still in use,
4216 so recursion_depth must be boosted for the duration.
4217 */
INADA Naoki5a625d02016-12-24 20:19:08 +09004218 if (Py_REFCNT(f) > 1) {
4219 Py_DECREF(f);
4220 _PyObject_GC_TRACK(f);
4221 }
4222 else {
4223 ++tstate->recursion_depth;
4224 Py_DECREF(f);
4225 --tstate->recursion_depth;
4226 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004227 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004228}
4229
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004230
4231PyObject *
4232_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
4233 PyObject *const *args, Py_ssize_t argcount,
4234 PyObject *const *kwnames, PyObject *const *kwargs,
4235 Py_ssize_t kwcount, int kwstep,
4236 PyObject *const *defs, Py_ssize_t defcount,
4237 PyObject *kwdefs, PyObject *closure,
4238 PyObject *name, PyObject *qualname)
4239{
4240 PyThreadState *tstate = _PyThreadState_GET();
4241 return _PyEval_EvalCode(tstate, _co, globals, locals,
4242 args, argcount,
4243 kwnames, kwargs,
4244 kwcount, kwstep,
4245 defs, defcount,
4246 kwdefs, closure,
4247 name, qualname);
4248}
4249
Victor Stinner40ee3012014-06-16 15:59:28 +02004250PyObject *
4251PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004252 PyObject *const *args, int argcount,
4253 PyObject *const *kws, int kwcount,
4254 PyObject *const *defs, int defcount,
4255 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004256{
4257 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004258 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004259 kws, kws != NULL ? kws + 1 : NULL,
4260 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004261 defs, defcount,
4262 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004263 NULL, NULL);
4264}
Tim Peters5ca576e2001-06-18 22:08:13 +00004265
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004266static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004267special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004268{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004269 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004270 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02004271 if (res == NULL && !_PyErr_Occurred(tstate)) {
4272 _PyErr_SetObject(tstate, PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004273 return NULL;
4274 }
4275 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004276}
4277
4278
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004279/* Logic for the raise statement (too complicated for inlining).
4280 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004281static int
Victor Stinner09532fe2019-05-10 23:39:09 +02004282do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004283{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004284 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004285
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004286 if (exc == NULL) {
4287 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01004288 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004289 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004290 type = exc_info->exc_type;
4291 value = exc_info->exc_value;
4292 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004293 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004294 _PyErr_SetString(tstate, PyExc_RuntimeError,
4295 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004296 return 0;
4297 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004298 Py_XINCREF(type);
4299 Py_XINCREF(value);
4300 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02004301 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004302 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004303 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004305 /* We support the following forms of raise:
4306 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004307 raise <instance>
4308 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004310 if (PyExceptionClass_Check(exc)) {
4311 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004312 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004313 if (value == NULL)
4314 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004315 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004316 _PyErr_Format(tstate, PyExc_TypeError,
4317 "calling %R should have returned an instance of "
4318 "BaseException, not %R",
4319 type, Py_TYPE(value));
4320 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004321 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004322 }
4323 else if (PyExceptionInstance_Check(exc)) {
4324 value = exc;
4325 type = PyExceptionInstance_Class(exc);
4326 Py_INCREF(type);
4327 }
4328 else {
4329 /* Not something you can raise. You get an exception
4330 anyway, just not what you specified :-) */
4331 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02004332 _PyErr_SetString(tstate, PyExc_TypeError,
4333 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004334 goto raise_error;
4335 }
Collin Winter828f04a2007-08-31 00:04:24 +00004336
Serhiy Storchakac0191582016-09-27 11:37:10 +03004337 assert(type != NULL);
4338 assert(value != NULL);
4339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004340 if (cause) {
4341 PyObject *fixed_cause;
4342 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004343 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004344 if (fixed_cause == NULL)
4345 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004346 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004347 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004348 else if (PyExceptionInstance_Check(cause)) {
4349 fixed_cause = cause;
4350 }
4351 else if (cause == Py_None) {
4352 Py_DECREF(cause);
4353 fixed_cause = NULL;
4354 }
4355 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004356 _PyErr_SetString(tstate, PyExc_TypeError,
4357 "exception causes must derive from "
4358 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004359 goto raise_error;
4360 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004361 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004362 }
Collin Winter828f04a2007-08-31 00:04:24 +00004363
Victor Stinner438a12d2019-05-24 17:01:38 +02004364 _PyErr_SetObject(tstate, type, value);
Victor Stinner61f4db82020-01-28 03:37:45 +01004365 /* _PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004366 Py_DECREF(value);
4367 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004368 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004369
4370raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004371 Py_XDECREF(value);
4372 Py_XDECREF(type);
4373 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004374 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004375}
4376
Tim Petersd6d010b2001-06-21 02:49:55 +00004377/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004378 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004379
Guido van Rossum0368b722007-05-11 16:50:42 +00004380 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4381 with a variable target.
4382*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004383
Barry Warsawe42b18f1997-08-25 22:13:04 +00004384static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004385unpack_iterable(PyThreadState *tstate, PyObject *v,
4386 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004387{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004388 int i = 0, j = 0;
4389 Py_ssize_t ll = 0;
4390 PyObject *it; /* iter(v) */
4391 PyObject *w;
4392 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004394 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004396 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004397 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004398 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01004399 Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v))
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004400 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004401 _PyErr_Format(tstate, PyExc_TypeError,
4402 "cannot unpack non-iterable %.200s object",
Victor Stinnera102ed72020-02-07 02:24:48 +01004403 Py_TYPE(v)->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004404 }
4405 return 0;
4406 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004408 for (; i < argcnt; i++) {
4409 w = PyIter_Next(it);
4410 if (w == NULL) {
4411 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004412 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04004413 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004414 _PyErr_Format(tstate, PyExc_ValueError,
4415 "not enough values to unpack "
4416 "(expected %d, got %d)",
4417 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004418 }
4419 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004420 _PyErr_Format(tstate, PyExc_ValueError,
4421 "not enough values to unpack "
4422 "(expected at least %d, got %d)",
4423 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004424 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004425 }
4426 goto Error;
4427 }
4428 *--sp = w;
4429 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004431 if (argcntafter == -1) {
4432 /* We better have exhausted the iterator now. */
4433 w = PyIter_Next(it);
4434 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004435 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004436 goto Error;
4437 Py_DECREF(it);
4438 return 1;
4439 }
4440 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02004441 _PyErr_Format(tstate, PyExc_ValueError,
4442 "too many values to unpack (expected %d)",
4443 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004444 goto Error;
4445 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004447 l = PySequence_List(it);
4448 if (l == NULL)
4449 goto Error;
4450 *--sp = l;
4451 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004453 ll = PyList_GET_SIZE(l);
4454 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004455 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04004456 "not enough values to unpack (expected at least %d, got %zd)",
4457 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004458 goto Error;
4459 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004461 /* Pop the "after-variable" args off the list. */
4462 for (j = argcntafter; j > 0; j--, i++) {
4463 *--sp = PyList_GET_ITEM(l, ll - j);
4464 }
4465 /* Resize the list. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +01004466 Py_SET_SIZE(l, ll - argcntafter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004467 Py_DECREF(it);
4468 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004469
Tim Petersd6d010b2001-06-21 02:49:55 +00004470Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004471 for (; i > 0; i--, sp++)
4472 Py_DECREF(*sp);
4473 Py_XDECREF(it);
4474 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004475}
4476
4477
Guido van Rossum96a42c81992-01-12 02:29:51 +00004478#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004479static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004480prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004481{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004482 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02004483 if (PyObject_Print(v, stdout, 0) != 0) {
4484 /* Don't know what else to do */
4485 _PyErr_Clear(tstate);
4486 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004487 printf("\n");
4488 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004489}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004490#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004491
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004492static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004493call_exc_trace(Py_tracefunc func, PyObject *self,
4494 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004495{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004496 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004497 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004498 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004499 if (value == NULL) {
4500 value = Py_None;
4501 Py_INCREF(value);
4502 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004503 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01004504 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004505 arg = PyTuple_Pack(3, type, value, traceback);
4506 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004507 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004508 return;
4509 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004510 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004511 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02004512 if (err == 0) {
4513 _PyErr_Restore(tstate, type, value, orig_traceback);
4514 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004515 else {
4516 Py_XDECREF(type);
4517 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004518 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004519 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004520}
4521
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004522static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004523call_trace_protected(Py_tracefunc func, PyObject *obj,
4524 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004525 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004526{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004527 PyObject *type, *value, *traceback;
4528 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004529 _PyErr_Fetch(tstate, &type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004530 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004531 if (err == 0)
4532 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004533 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004534 return 0;
4535 }
4536 else {
4537 Py_XDECREF(type);
4538 Py_XDECREF(value);
4539 Py_XDECREF(traceback);
4540 return -1;
4541 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004542}
4543
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004544static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004545call_trace(Py_tracefunc func, PyObject *obj,
4546 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004547 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004548{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004549 int result;
4550 if (tstate->tracing)
4551 return 0;
4552 tstate->tracing++;
4553 tstate->use_tracing = 0;
4554 result = func(obj, frame, what, arg);
4555 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4556 || (tstate->c_profilefunc != NULL));
4557 tstate->tracing--;
4558 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004559}
4560
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004561PyObject *
4562_PyEval_CallTracing(PyObject *func, PyObject *args)
4563{
Victor Stinner50b48572018-11-01 01:51:40 +01004564 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004565 int save_tracing = tstate->tracing;
4566 int save_use_tracing = tstate->use_tracing;
4567 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004569 tstate->tracing = 0;
4570 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4571 || (tstate->c_profilefunc != NULL));
4572 result = PyObject_Call(func, args, NULL);
4573 tstate->tracing = save_tracing;
4574 tstate->use_tracing = save_use_tracing;
4575 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004576}
4577
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004578/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004579static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004580maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004581 PyThreadState *tstate, PyFrameObject *frame,
4582 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004583{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004584 int result = 0;
4585 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004587 /* If the last instruction executed isn't in the current
4588 instruction window, reset the window.
4589 */
4590 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4591 PyAddrPair bounds;
4592 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4593 &bounds);
4594 *instr_lb = bounds.ap_lower;
4595 *instr_ub = bounds.ap_upper;
4596 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004597 /* If the last instruction falls at the start of a line or if it
4598 represents a jump backwards, update the frame's line number and
4599 then call the trace function if we're tracing source lines.
4600 */
4601 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004602 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004603 if (frame->f_trace_lines) {
4604 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4605 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004606 }
George King20faa682017-10-18 17:44:22 -07004607 /* Always emit an opcode event if we're tracing all opcodes. */
4608 if (frame->f_trace_opcodes) {
4609 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4610 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004611 *instr_prev = frame->f_lasti;
4612 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004613}
4614
Victor Stinner309d7cc2020-03-13 16:39:12 +01004615int
4616_PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
4617{
4618 assert(tstate != NULL);
4619 /* The caller must hold the GIL */
4620 assert(PyGILState_Check());
4621
4622 /* Call PySys_Audit() in the context of the current thread state,
4623 even if tstate is not the current thread state. */
4624 if (PySys_Audit("sys.setprofile", NULL) < 0) {
4625 return -1;
4626 }
4627
4628 PyObject *profileobj = tstate->c_profileobj;
4629
4630 tstate->c_profilefunc = NULL;
4631 tstate->c_profileobj = NULL;
4632 /* Must make sure that tracing is not ignored if 'profileobj' is freed */
4633 tstate->use_tracing = tstate->c_tracefunc != NULL;
4634 Py_XDECREF(profileobj);
4635
4636 Py_XINCREF(arg);
4637 tstate->c_profileobj = arg;
4638 tstate->c_profilefunc = func;
4639
4640 /* Flag that tracing or profiling is turned on */
4641 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
4642 return 0;
4643}
4644
Fred Drake5755ce62001-06-27 19:19:46 +00004645void
4646PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004647{
Victor Stinner309d7cc2020-03-13 16:39:12 +01004648 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01004649 if (_PyEval_SetProfile(tstate, func, arg) < 0) {
4650 /* Log PySys_Audit() error */
4651 _PyErr_WriteUnraisableMsg("in PyEval_SetProfile", NULL);
4652 }
Victor Stinner309d7cc2020-03-13 16:39:12 +01004653}
4654
4655int
4656_PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
4657{
4658 assert(tstate != NULL);
4659 /* The caller must hold the GIL */
4660 assert(PyGILState_Check());
4661
4662 /* Call PySys_Audit() in the context of the current thread state,
4663 even if tstate is not the current thread state. */
4664 if (PySys_Audit("sys.settrace", NULL) < 0) {
4665 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004666 }
4667
Victor Stinnerdab84232020-03-17 18:56:44 +01004668 struct _ceval_state *ceval = &tstate->interp->ceval;
Victor Stinner309d7cc2020-03-13 16:39:12 +01004669 PyObject *traceobj = tstate->c_traceobj;
4670 ceval->tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
4671
4672 tstate->c_tracefunc = NULL;
4673 tstate->c_traceobj = NULL;
4674 /* Must make sure that profiling is not ignored if 'traceobj' is freed */
4675 tstate->use_tracing = (tstate->c_profilefunc != NULL);
4676 Py_XDECREF(traceobj);
4677
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004678 Py_XINCREF(arg);
Victor Stinner309d7cc2020-03-13 16:39:12 +01004679 tstate->c_traceobj = arg;
4680 tstate->c_tracefunc = func;
4681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004682 /* Flag that tracing or profiling is turned on */
Victor Stinner309d7cc2020-03-13 16:39:12 +01004683 tstate->use_tracing = ((func != NULL)
4684 || (tstate->c_profilefunc != NULL));
4685
4686 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +00004687}
4688
4689void
4690PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4691{
Victor Stinner309d7cc2020-03-13 16:39:12 +01004692 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01004693 if (_PyEval_SetTrace(tstate, func, arg) < 0) {
4694 /* Log PySys_Audit() error */
4695 _PyErr_WriteUnraisableMsg("in PyEval_SetTrace", NULL);
4696 }
Fred Draked0838392001-06-16 21:02:31 +00004697}
4698
Victor Stinner309d7cc2020-03-13 16:39:12 +01004699
Yury Selivanov75445082015-05-11 22:57:16 -04004700void
Victor Stinner838f2642019-06-13 22:41:23 +02004701_PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004702{
4703 assert(new_depth >= 0);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004704 tstate->coroutine_origin_tracking_depth = new_depth;
4705}
4706
4707int
4708_PyEval_GetCoroutineOriginTrackingDepth(void)
4709{
Victor Stinner50b48572018-11-01 01:51:40 +01004710 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004711 return tstate->coroutine_origin_tracking_depth;
4712}
4713
4714void
Yury Selivanoveb636452016-09-08 22:01:51 -07004715_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4716{
Victor Stinner50b48572018-11-01 01:51:40 +01004717 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004718
4719 if (PySys_Audit("sys.set_asyncgen_hook_firstiter", NULL) < 0) {
4720 return;
4721 }
4722
Yury Selivanoveb636452016-09-08 22:01:51 -07004723 Py_XINCREF(firstiter);
4724 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4725}
4726
4727PyObject *
4728_PyEval_GetAsyncGenFirstiter(void)
4729{
Victor Stinner50b48572018-11-01 01:51:40 +01004730 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004731 return tstate->async_gen_firstiter;
4732}
4733
4734void
4735_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4736{
Victor Stinner50b48572018-11-01 01:51:40 +01004737 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004738
4739 if (PySys_Audit("sys.set_asyncgen_hook_finalizer", NULL) < 0) {
4740 return;
4741 }
4742
Yury Selivanoveb636452016-09-08 22:01:51 -07004743 Py_XINCREF(finalizer);
4744 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4745}
4746
4747PyObject *
4748_PyEval_GetAsyncGenFinalizer(void)
4749{
Victor Stinner50b48572018-11-01 01:51:40 +01004750 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004751 return tstate->async_gen_finalizer;
4752}
4753
Victor Stinner438a12d2019-05-24 17:01:38 +02004754static PyFrameObject *
4755_PyEval_GetFrame(PyThreadState *tstate)
4756{
Victor Stinner01b1cc12019-11-20 02:27:56 +01004757 _PyRuntimeState *runtime = tstate->interp->runtime;
4758 return runtime->gilstate.getframe(tstate);
Victor Stinner438a12d2019-05-24 17:01:38 +02004759}
4760
4761PyFrameObject *
4762PyEval_GetFrame(void)
4763{
4764 PyThreadState *tstate = _PyThreadState_GET();
4765 return _PyEval_GetFrame(tstate);
4766}
4767
Guido van Rossumb209a111997-04-29 18:18:01 +00004768PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004769PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004770{
Victor Stinner438a12d2019-05-24 17:01:38 +02004771 PyThreadState *tstate = _PyThreadState_GET();
4772 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004773 if (current_frame == NULL)
Victor Stinner438a12d2019-05-24 17:01:38 +02004774 return tstate->interp->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004775 else
4776 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004777}
4778
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004779/* Convenience function to get a builtin from its name */
4780PyObject *
4781_PyEval_GetBuiltinId(_Py_Identifier *name)
4782{
Victor Stinner438a12d2019-05-24 17:01:38 +02004783 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004784 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4785 if (attr) {
4786 Py_INCREF(attr);
4787 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004788 else if (!_PyErr_Occurred(tstate)) {
4789 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004790 }
4791 return attr;
4792}
4793
Guido van Rossumb209a111997-04-29 18:18:01 +00004794PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004795PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004796{
Victor Stinner438a12d2019-05-24 17:01:38 +02004797 PyThreadState *tstate = _PyThreadState_GET();
4798 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Victor Stinner41bb43a2013-10-29 01:19:37 +01004799 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004800 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004801 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004802 }
4803
Victor Stinner438a12d2019-05-24 17:01:38 +02004804 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01004805 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004806 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004807
4808 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004809 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004810}
4811
Guido van Rossumb209a111997-04-29 18:18:01 +00004812PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004813PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004814{
Victor Stinner438a12d2019-05-24 17:01:38 +02004815 PyThreadState *tstate = _PyThreadState_GET();
4816 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
4817 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004818 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004819 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004820
4821 assert(current_frame->f_globals != NULL);
4822 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004823}
4824
Guido van Rossum6135a871995-01-09 17:53:26 +00004825int
Tim Peters5ba58662001-07-16 02:29:45 +00004826PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004827{
Victor Stinner438a12d2019-05-24 17:01:38 +02004828 PyThreadState *tstate = _PyThreadState_GET();
4829 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004830 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004831
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004832 if (current_frame != NULL) {
4833 const int codeflags = current_frame->f_code->co_flags;
4834 const int compilerflags = codeflags & PyCF_MASK;
4835 if (compilerflags) {
4836 result = 1;
4837 cf->cf_flags |= compilerflags;
4838 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004839#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004840 if (codeflags & CO_GENERATOR_ALLOWED) {
4841 result = 1;
4842 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4843 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004844#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004845 }
4846 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004847}
4848
Guido van Rossum3f5da241990-12-20 15:06:42 +00004849
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004850const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004851PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004852{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004853 if (PyMethod_Check(func))
4854 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4855 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004856 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004857 else if (PyCFunction_Check(func))
4858 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4859 else
Victor Stinnera102ed72020-02-07 02:24:48 +01004860 return Py_TYPE(func)->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004861}
4862
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004863const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004864PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004865{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004866 if (PyMethod_Check(func))
4867 return "()";
4868 else if (PyFunction_Check(func))
4869 return "()";
4870 else if (PyCFunction_Check(func))
4871 return "()";
4872 else
4873 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004874}
4875
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004876#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004877if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004878 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4879 tstate, tstate->frame, \
4880 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004881 x = NULL; \
4882 } \
4883 else { \
4884 x = call; \
4885 if (tstate->c_profilefunc != NULL) { \
4886 if (x == NULL) { \
4887 call_trace_protected(tstate->c_profilefunc, \
4888 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004889 tstate, tstate->frame, \
4890 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004891 /* XXX should pass (type, value, tb) */ \
4892 } else { \
4893 if (call_trace(tstate->c_profilefunc, \
4894 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004895 tstate, tstate->frame, \
4896 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004897 Py_DECREF(x); \
4898 x = NULL; \
4899 } \
4900 } \
4901 } \
4902 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004903} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004904 x = call; \
4905 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004906
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004907
4908static PyObject *
4909trace_call_function(PyThreadState *tstate,
4910 PyObject *func,
4911 PyObject **args, Py_ssize_t nargs,
4912 PyObject *kwnames)
4913{
4914 PyObject *x;
4915 if (PyCFunction_Check(func)) {
Petr Viktorinffd97532020-02-11 17:46:57 +01004916 C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004917 return x;
4918 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06004919 else if (Py_IS_TYPE(func, &PyMethodDescr_Type) && nargs > 0) {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004920 /* We need to create a temporary bound method as argument
4921 for profiling.
4922
4923 If nargs == 0, then this cannot work because we have no
4924 "self". In any case, the call itself would raise
4925 TypeError (foo needs an argument), so we just skip
4926 profiling. */
4927 PyObject *self = args[0];
4928 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4929 if (func == NULL) {
4930 return NULL;
4931 }
Petr Viktorinffd97532020-02-11 17:46:57 +01004932 C_TRACE(x, PyObject_Vectorcall(func,
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02004933 args+1, nargs-1,
4934 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004935 Py_DECREF(func);
4936 return x;
4937 }
Petr Viktorinffd97532020-02-11 17:46:57 +01004938 return PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004939}
4940
Victor Stinner415c5102017-01-11 00:54:57 +01004941/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4942 to reduce the stack consumption. */
4943Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Victor Stinner09532fe2019-05-10 23:39:09 +02004944call_function(PyThreadState *tstate, PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004945{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004946 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004947 PyObject *func = *pfunc;
4948 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004949 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4950 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004951 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004952
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004953 if (tstate->use_tracing) {
4954 x = trace_call_function(tstate, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09004955 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004956 else {
Petr Viktorinffd97532020-02-11 17:46:57 +01004957 x = PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004958 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004959
Victor Stinner438a12d2019-05-24 17:01:38 +02004960 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004961
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004962 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004963 while ((*pp_stack) > pfunc) {
4964 w = EXT_POP(*pp_stack);
4965 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004966 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004967
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004968 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004969}
4970
Jeremy Hylton52820442001-01-03 23:52:36 +00004971static PyObject *
Victor Stinner09532fe2019-05-10 23:39:09 +02004972do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004973{
jdemeyere89de732018-09-19 12:06:20 +02004974 PyObject *result;
4975
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004976 if (PyCFunction_Check(func)) {
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +02004977 C_TRACE(result, PyObject_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004978 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004979 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06004980 else if (Py_IS_TYPE(func, &PyMethodDescr_Type)) {
jdemeyere89de732018-09-19 12:06:20 +02004981 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
4982 if (nargs > 0 && tstate->use_tracing) {
4983 /* We need to create a temporary bound method as argument
4984 for profiling.
4985
4986 If nargs == 0, then this cannot work because we have no
4987 "self". In any case, the call itself would raise
4988 TypeError (foo needs an argument), so we just skip
4989 profiling. */
4990 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
4991 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4992 if (func == NULL) {
4993 return NULL;
4994 }
4995
Victor Stinner4d231bc2019-11-14 13:36:21 +01004996 C_TRACE(result, _PyObject_FastCallDictTstate(
4997 tstate, func,
4998 &_PyTuple_ITEMS(callargs)[1],
4999 nargs - 1,
5000 kwdict));
jdemeyere89de732018-09-19 12:06:20 +02005001 Py_DECREF(func);
5002 return result;
5003 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005004 }
jdemeyere89de732018-09-19 12:06:20 +02005005 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00005006}
5007
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005008/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005009 nb_index slot defined, and store in *pi.
5010 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08005011 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00005012 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005013*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005014int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005015_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005016{
Victor Stinner438a12d2019-05-24 17:01:38 +02005017 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005018 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005019 Py_ssize_t x;
5020 if (PyIndex_Check(v)) {
5021 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005022 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005023 return 0;
5024 }
5025 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005026 _PyErr_SetString(tstate, PyExc_TypeError,
5027 "slice indices must be integers or "
5028 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005029 return 0;
5030 }
5031 *pi = x;
5032 }
5033 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005034}
5035
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005036int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005037_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005038{
Victor Stinner438a12d2019-05-24 17:01:38 +02005039 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005040 Py_ssize_t x;
5041 if (PyIndex_Check(v)) {
5042 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005043 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005044 return 0;
5045 }
5046 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005047 _PyErr_SetString(tstate, PyExc_TypeError,
5048 "slice indices must be integers or "
5049 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005050 return 0;
5051 }
5052 *pi = x;
5053 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005054}
5055
Thomas Wouters52152252000-08-17 22:55:00 +00005056static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005057import_name(PyThreadState *tstate, PyFrameObject *f,
5058 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005059{
5060 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005061 PyObject *import_func, *res;
5062 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005063
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005064 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005065 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005066 if (!_PyErr_Occurred(tstate)) {
5067 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005068 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005069 return NULL;
5070 }
5071
5072 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005073 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005074 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02005075 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005076 return NULL;
5077 }
5078 res = PyImport_ImportModuleLevelObject(
5079 name,
5080 f->f_globals,
5081 f->f_locals == NULL ? Py_None : f->f_locals,
5082 fromlist,
5083 ilevel);
5084 return res;
5085 }
5086
5087 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005088
5089 stack[0] = name;
5090 stack[1] = f->f_globals;
5091 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5092 stack[3] = fromlist;
5093 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005094 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005095 Py_DECREF(import_func);
5096 return res;
5097}
5098
5099static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005100import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005101{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005102 PyObject *x;
Xiang Zhang4830f582017-03-21 11:13:42 +08005103 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005104
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005105 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005106 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005107 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005108 /* Issue #17636: in case this failed because of a circular relative
5109 import, try to fallback on reading the module directly from
5110 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02005111 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005112 if (pkgname == NULL) {
5113 goto error;
5114 }
Oren Milman6db70332017-09-19 14:23:01 +03005115 if (!PyUnicode_Check(pkgname)) {
5116 Py_CLEAR(pkgname);
5117 goto error;
5118 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005119 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005120 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005121 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005122 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005123 }
Eric Snow3f9eee62017-09-15 16:35:20 -06005124 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005125 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02005126 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07005127 goto error;
5128 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005129 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005130 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005131 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005132 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005133 if (pkgname == NULL) {
5134 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5135 if (pkgname_or_unknown == NULL) {
5136 Py_XDECREF(pkgpath);
5137 return NULL;
5138 }
5139 } else {
5140 pkgname_or_unknown = pkgname;
5141 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005142
5143 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005144 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08005145 errmsg = PyUnicode_FromFormat(
5146 "cannot import name %R from %R (unknown location)",
5147 name, pkgname_or_unknown
5148 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005149 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005150 PyErr_SetImportError(errmsg, pkgname, NULL);
5151 }
5152 else {
Anthony Sottile65366bc2019-09-09 08:17:50 -07005153 _Py_IDENTIFIER(__spec__);
5154 PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
Anthony Sottile65366bc2019-09-09 08:17:50 -07005155 const char *fmt =
5156 _PyModuleSpec_IsInitializing(spec) ?
5157 "cannot import name %R from partially initialized module %R "
5158 "(most likely due to a circular import) (%S)" :
5159 "cannot import name %R from %R (%S)";
5160 Py_XDECREF(spec);
5161
5162 errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
Stefan Krah027b09c2019-03-25 21:50:58 +01005163 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005164 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005165 }
5166
Xiang Zhang4830f582017-03-21 11:13:42 +08005167 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005168 Py_XDECREF(pkgname_or_unknown);
5169 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005170 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005171}
Guido van Rossumac7be682001-01-17 15:42:30 +00005172
Thomas Wouters52152252000-08-17 22:55:00 +00005173static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005174import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00005175{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005176 _Py_IDENTIFIER(__all__);
5177 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005178 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005179 int skip_leading_underscores = 0;
5180 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005181
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005182 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5183 return -1; /* Unexpected error */
5184 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005185 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005186 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5187 return -1;
5188 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005189 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005190 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005191 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005192 return -1;
5193 }
5194 all = PyMapping_Keys(dict);
5195 Py_DECREF(dict);
5196 if (all == NULL)
5197 return -1;
5198 skip_leading_underscores = 1;
5199 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005201 for (pos = 0, err = 0; ; pos++) {
5202 name = PySequence_GetItem(all, pos);
5203 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005204 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005205 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02005206 }
5207 else {
5208 _PyErr_Clear(tstate);
5209 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005210 break;
5211 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005212 if (!PyUnicode_Check(name)) {
5213 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5214 if (modname == NULL) {
5215 Py_DECREF(name);
5216 err = -1;
5217 break;
5218 }
5219 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005220 _PyErr_Format(tstate, PyExc_TypeError,
5221 "module __name__ must be a string, not %.100s",
5222 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005223 }
5224 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005225 _PyErr_Format(tstate, PyExc_TypeError,
5226 "%s in %U.%s must be str, not %.100s",
5227 skip_leading_underscores ? "Key" : "Item",
5228 modname,
5229 skip_leading_underscores ? "__dict__" : "__all__",
5230 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005231 }
5232 Py_DECREF(modname);
5233 Py_DECREF(name);
5234 err = -1;
5235 break;
5236 }
5237 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005238 if (PyUnicode_READY(name) == -1) {
5239 Py_DECREF(name);
5240 err = -1;
5241 break;
5242 }
5243 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5244 Py_DECREF(name);
5245 continue;
5246 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005247 }
5248 value = PyObject_GetAttr(v, name);
5249 if (value == NULL)
5250 err = -1;
5251 else if (PyDict_CheckExact(locals))
5252 err = PyDict_SetItem(locals, name, value);
5253 else
5254 err = PyObject_SetItem(locals, name, value);
5255 Py_DECREF(name);
5256 Py_XDECREF(value);
5257 if (err != 0)
5258 break;
5259 }
5260 Py_DECREF(all);
5261 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005262}
5263
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005264static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005265check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005266{
Victor Stinnera102ed72020-02-07 02:24:48 +01005267 if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005268 /* check_args_iterable() may be called with a live exception:
5269 * clear it to prevent calling _PyObject_FunctionStr() with an
5270 * exception set. */
Victor Stinner61f4db82020-01-28 03:37:45 +01005271 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005272 PyObject *funcstr = _PyObject_FunctionStr(func);
5273 if (funcstr != NULL) {
5274 _PyErr_Format(tstate, PyExc_TypeError,
5275 "%U argument after * must be an iterable, not %.200s",
5276 funcstr, Py_TYPE(args)->tp_name);
5277 Py_DECREF(funcstr);
5278 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005279 return -1;
5280 }
5281 return 0;
5282}
5283
5284static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005285format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005286{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005287 /* _PyDict_MergeEx raises attribute
5288 * error (percolated from an attempt
5289 * to get 'keys' attribute) instead of
5290 * a type error if its second argument
5291 * is not a mapping.
5292 */
Victor Stinner438a12d2019-05-24 17:01:38 +02005293 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005294 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005295 PyObject *funcstr = _PyObject_FunctionStr(func);
5296 if (funcstr != NULL) {
5297 _PyErr_Format(
5298 tstate, PyExc_TypeError,
5299 "%U argument after ** must be a mapping, not %.200s",
5300 funcstr, Py_TYPE(kwargs)->tp_name);
5301 Py_DECREF(funcstr);
5302 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005303 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005304 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005305 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02005306 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005307 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005308 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005309 PyObject *funcstr = _PyObject_FunctionStr(func);
5310 if (funcstr != NULL) {
5311 PyObject *key = PyTuple_GET_ITEM(val, 0);
5312 _PyErr_Format(
5313 tstate, PyExc_TypeError,
5314 "%U got multiple values for keyword argument '%S'",
5315 funcstr, key);
5316 Py_DECREF(funcstr);
5317 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005318 Py_XDECREF(exc);
5319 Py_XDECREF(val);
5320 Py_XDECREF(tb);
5321 }
5322 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005323 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005324 }
5325 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005326}
5327
Guido van Rossumac7be682001-01-17 15:42:30 +00005328static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005329format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
5330 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005331{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005332 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005334 if (!obj)
5335 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005336
Serhiy Storchaka06515832016-11-20 09:13:07 +02005337 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005338 if (!obj_str)
5339 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005340
Victor Stinner438a12d2019-05-24 17:01:38 +02005341 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005342}
Guido van Rossum950361c1997-01-24 13:49:28 +00005343
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005344static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005345format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005346{
5347 PyObject *name;
5348 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02005349 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005350 return;
5351 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5352 name = PyTuple_GET_ITEM(co->co_cellvars,
5353 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005354 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005355 PyExc_UnboundLocalError,
5356 UNBOUNDLOCAL_ERROR_MSG,
5357 name);
5358 } else {
5359 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5360 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02005361 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005362 UNBOUNDFREE_ERROR_MSG, name);
5363 }
5364}
5365
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005366static void
Mark Shannonfee55262019-11-21 09:11:43 +00005367format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevprevopcode, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005368{
5369 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5370 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005371 _PyErr_Format(tstate, PyExc_TypeError,
5372 "'async with' received an object from __aenter__ "
5373 "that does not implement __await__: %.100s",
5374 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005375 }
Mark Shannonfee55262019-11-21 09:11:43 +00005376 else if (prevopcode == WITH_EXCEPT_START || (prevopcode == CALL_FUNCTION && prevprevopcode == DUP_TOP)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005377 _PyErr_Format(tstate, PyExc_TypeError,
5378 "'async with' received an object from __aexit__ "
5379 "that does not implement __await__: %.100s",
5380 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005381 }
5382 }
5383}
5384
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005385static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005386unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005387 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005388{
5389 PyObject *res;
5390 if (Py_REFCNT(v) == 2) {
5391 /* In the common case, there are 2 references to the value
5392 * stored in 'variable' when the += is performed: one on the
5393 * value stack (in 'v') and one still stored in the
5394 * 'variable'. We try to delete the variable now to reduce
5395 * the refcnt to 1.
5396 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005397 int opcode, oparg;
5398 NEXTOPARG();
5399 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005400 case STORE_FAST:
5401 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005402 PyObject **fastlocals = f->f_localsplus;
5403 if (GETLOCAL(oparg) == v)
5404 SETLOCAL(oparg, NULL);
5405 break;
5406 }
5407 case STORE_DEREF:
5408 {
5409 PyObject **freevars = (f->f_localsplus +
5410 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005411 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005412 if (PyCell_GET(c) == v) {
5413 PyCell_SET(c, NULL);
5414 Py_DECREF(v);
5415 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005416 break;
5417 }
5418 case STORE_NAME:
5419 {
5420 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005421 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005422 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005423 if (locals && PyDict_CheckExact(locals)) {
5424 PyObject *w = PyDict_GetItemWithError(locals, name);
5425 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02005426 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005427 {
5428 Py_DECREF(v);
5429 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005430 }
5431 }
5432 break;
5433 }
5434 }
5435 }
5436 res = v;
5437 PyUnicode_Append(&res, w);
5438 return res;
5439}
5440
Guido van Rossum950361c1997-01-24 13:49:28 +00005441#ifdef DYNAMIC_EXECUTION_PROFILE
5442
Skip Montanarof118cb12001-10-15 20:51:38 +00005443static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005444getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005445{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005446 int i;
5447 PyObject *l = PyList_New(256);
5448 if (l == NULL) return NULL;
5449 for (i = 0; i < 256; i++) {
5450 PyObject *x = PyLong_FromLong(a[i]);
5451 if (x == NULL) {
5452 Py_DECREF(l);
5453 return NULL;
5454 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005455 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005456 }
5457 for (i = 0; i < 256; i++)
5458 a[i] = 0;
5459 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005460}
5461
5462PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005463_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005464{
5465#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005466 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005467#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005468 int i;
5469 PyObject *l = PyList_New(257);
5470 if (l == NULL) return NULL;
5471 for (i = 0; i < 257; i++) {
5472 PyObject *x = getarray(dxpairs[i]);
5473 if (x == NULL) {
5474 Py_DECREF(l);
5475 return NULL;
5476 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005477 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005478 }
5479 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005480#endif
5481}
5482
5483#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005484
5485Py_ssize_t
5486_PyEval_RequestCodeExtraIndex(freefunc free)
5487{
Victor Stinnercaba55b2018-08-03 15:33:52 +02005488 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -07005489 Py_ssize_t new_index;
5490
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005491 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005492 return -1;
5493 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005494 new_index = interp->co_extra_user_count++;
5495 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005496 return new_index;
5497}
Łukasz Langaa785c872016-09-09 17:37:37 -07005498
5499static void
5500dtrace_function_entry(PyFrameObject *f)
5501{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005502 const char *filename;
5503 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005504 int lineno;
5505
5506 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5507 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5508 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5509
Andy Lestere6be9b52020-02-11 20:28:35 -06005510 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005511}
5512
5513static void
5514dtrace_function_return(PyFrameObject *f)
5515{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005516 const char *filename;
5517 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005518 int lineno;
5519
5520 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5521 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5522 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5523
Andy Lestere6be9b52020-02-11 20:28:35 -06005524 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005525}
5526
5527/* DTrace equivalent of maybe_call_line_trace. */
5528static void
5529maybe_dtrace_line(PyFrameObject *frame,
5530 int *instr_lb, int *instr_ub, int *instr_prev)
5531{
5532 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005533 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005534
5535 /* If the last instruction executed isn't in the current
5536 instruction window, reset the window.
5537 */
5538 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5539 PyAddrPair bounds;
5540 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5541 &bounds);
5542 *instr_lb = bounds.ap_lower;
5543 *instr_ub = bounds.ap_upper;
5544 }
5545 /* If the last instruction falls at the start of a line or if
5546 it represents a jump backwards, update the frame's line
5547 number and call the trace function. */
5548 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5549 frame->f_lineno = line;
5550 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5551 if (!co_filename)
5552 co_filename = "?";
5553 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5554 if (!co_name)
5555 co_name = "?";
Andy Lestere6be9b52020-02-11 20:28:35 -06005556 PyDTrace_LINE(co_filename, co_name, line);
Łukasz Langaa785c872016-09-09 17:37:37 -07005557 }
5558 *instr_prev = frame->f_lasti;
5559}
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005560
5561
5562/* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions
5563 for the limited API. */
5564
5565#undef Py_EnterRecursiveCall
5566
5567int Py_EnterRecursiveCall(const char *where)
5568{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005569 return _Py_EnterRecursiveCall_inline(where);
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005570}
5571
5572#undef Py_LeaveRecursiveCall
5573
5574void Py_LeaveRecursiveCall(void)
5575{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005576 _Py_LeaveRecursiveCall_inline();
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005577}