blob: d79a239704f0a5b7b158d72a8b22bd9d1b6980d6 [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 Stinner1c1e68c2020-03-27 15:11:45 +010021#include "pycore_sysmodule.h"
Victor Stinnerec13b932018-11-25 23:56:17 +010022#include "pycore_tupleobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000023
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000024#include "code.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040025#include "dictobject.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000026#include "frameobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000027#include "opcode.h"
Łukasz Langaa785c872016-09-09 17:37:37 -070028#include "pydtrace.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040029#include "setobject.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000030#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000031
Guido van Rossumc6004111993-11-05 10:22:19 +000032#include <ctype.h>
33
Guido van Rossum408027e1996-12-30 16:17:54 +000034#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000035/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000036#define LLTRACE 1 /* Low-level trace feature */
37#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000038#endif
39
Victor Stinner5c75f372019-04-17 23:02:26 +020040#if !defined(Py_BUILD_CORE)
41# error "ceval.c must be build with Py_BUILD_CORE define for best performance"
42#endif
43
Hai Shi46874c22020-01-30 17:20:25 -060044_Py_IDENTIFIER(__name__);
Guido van Rossum5b722181993-03-30 17:46:03 +000045
Guido van Rossum374a9221991-04-04 10:40:29 +000046/* Forward declarations */
Victor Stinner09532fe2019-05-10 23:39:09 +020047Py_LOCAL_INLINE(PyObject *) call_function(
48 PyThreadState *tstate, PyObject ***pp_stack,
49 Py_ssize_t oparg, PyObject *kwnames);
50static PyObject * do_call_core(
51 PyThreadState *tstate, PyObject *func,
52 PyObject *callargs, PyObject *kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +000053
Guido van Rossum0a066c01992-03-27 17:29:15 +000054#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000055static int lltrace;
Victor Stinner438a12d2019-05-24 17:01:38 +020056static int prtrace(PyThreadState *, PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000057#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010058static int call_trace(Py_tracefunc, PyObject *,
59 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000061static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010062 PyThreadState *, PyFrameObject *,
63 int, PyObject *);
64static void call_exc_trace(Py_tracefunc, PyObject *,
65 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000066static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Eric Snow2ebc5ce2017-09-07 23:51:28 -060067 PyThreadState *, PyFrameObject *,
68 int *, int *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070069static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *);
70static void dtrace_function_entry(PyFrameObject *);
71static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000072
Victor Stinner438a12d2019-05-24 17:01:38 +020073static PyObject * import_name(PyThreadState *, PyFrameObject *,
74 PyObject *, PyObject *, PyObject *);
75static PyObject * import_from(PyThreadState *, PyObject *, PyObject *);
76static int import_all_from(PyThreadState *, PyObject *, PyObject *);
77static void format_exc_check_arg(PyThreadState *, PyObject *, const char *, PyObject *);
78static void format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg);
79static PyObject * unicode_concatenate(PyThreadState *, PyObject *, PyObject *,
Serhiy Storchakaab874002016-09-11 13:48:15 +030080 PyFrameObject *, const _Py_CODEUNIT *);
Victor Stinner438a12d2019-05-24 17:01:38 +020081static PyObject * special_lookup(PyThreadState *, PyObject *, _Py_Identifier *);
82static int check_args_iterable(PyThreadState *, PyObject *func, PyObject *vararg);
83static void format_kwargs_error(PyThreadState *, PyObject *func, PyObject *kwargs);
Mark Shannonfee55262019-11-21 09:11:43 +000084static void format_awaitable_error(PyThreadState *, PyTypeObject *, int, int);
Guido van Rossum374a9221991-04-04 10:40:29 +000085
Paul Prescode68140d2000-08-30 20:25:01 +000086#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000088#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000090#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 "free variable '%.200s' referenced before assignment" \
92 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000093
Guido van Rossum950361c1997-01-24 13:49:28 +000094/* Dynamic execution profile */
95#ifdef DYNAMIC_EXECUTION_PROFILE
96#ifdef DXPAIRS
97static long dxpairs[257][256];
98#define dxp dxpairs[256]
99#else
100static long dxp[256];
101#endif
102#endif
103
Inada Naoki91234a12019-06-03 21:30:58 +0900104/* per opcode cache */
Inada Naokieddef862019-06-04 07:38:10 +0900105#ifdef Py_DEBUG
106// --with-pydebug is used to find memory leak. opcache makes it harder.
107// So we disable opcache when Py_DEBUG is defined.
108// See bpo-37146
109#define OPCACHE_MIN_RUNS 0 /* disable opcache */
110#else
Inada Naoki91234a12019-06-03 21:30:58 +0900111#define OPCACHE_MIN_RUNS 1024 /* create opcache when code executed this time */
Inada Naokieddef862019-06-04 07:38:10 +0900112#endif
Inada Naoki91234a12019-06-03 21:30:58 +0900113#define OPCACHE_STATS 0 /* Enable stats */
114
115#if OPCACHE_STATS
116static size_t opcache_code_objects = 0;
117static size_t opcache_code_objects_extra_mem = 0;
118
119static size_t opcache_global_opts = 0;
120static size_t opcache_global_hits = 0;
121static size_t opcache_global_misses = 0;
122#endif
123
Victor Stinner5a3a71d2020-03-19 17:40:12 +0100124
Victor Stinnerda2914d2020-03-20 09:29:08 +0100125#ifndef NDEBUG
126/* Ensure that tstate is valid: sanity check for PyEval_AcquireThread() and
127 PyEval_RestoreThread(). Detect if tstate memory was freed. It can happen
128 when a thread continues to run after Python finalization, especially
129 daemon threads. */
130static int
131is_tstate_valid(PyThreadState *tstate)
132{
133 assert(!_PyMem_IsPtrFreed(tstate));
134 assert(!_PyMem_IsPtrFreed(tstate->interp));
135 return 1;
136}
137#endif
138
139
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000140/* This can set eval_breaker to 0 even though gil_drop_request became
141 1. We believe this is all right because the eval loop will release
142 the GIL eventually anyway. */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100143static inline void
144COMPUTE_EVAL_BREAKER(PyThreadState *tstate,
145 struct _ceval_runtime_state *ceval,
146 struct _ceval_state *ceval2)
147{
148 assert(is_tstate_valid(tstate));
149 _Py_atomic_store_relaxed(&ceval2->eval_breaker,
150 _Py_atomic_load_relaxed(&ceval->gil_drop_request)
151 | (_Py_atomic_load_relaxed(&ceval->signals_pending)
Victor Stinnerd2a8e5b2020-03-20 13:38:58 +0100152 && _Py_ThreadCanHandleSignals(tstate))
Victor Stinnerd8316882020-03-20 14:50:35 +0100153 | (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)
154 && _Py_ThreadCanHandlePendingCalls())
Victor Stinnerda2914d2020-03-20 09:29:08 +0100155 | ceval2->pending.async_exc);
156}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000157
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000158
Victor Stinnerda2914d2020-03-20 09:29:08 +0100159static inline void
160SET_GIL_DROP_REQUEST(PyThreadState *tstate)
161{
162 assert(is_tstate_valid(tstate));
163 struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
164 struct _ceval_state *ceval2 = &tstate->interp->ceval;
165 _Py_atomic_store_relaxed(&ceval->gil_drop_request, 1);
166 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
167}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000168
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000169
Victor Stinnerda2914d2020-03-20 09:29:08 +0100170static inline void
171RESET_GIL_DROP_REQUEST(PyThreadState *tstate)
172{
173 assert(is_tstate_valid(tstate));
174 struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
175 struct _ceval_state *ceval2 = &tstate->interp->ceval;
176 _Py_atomic_store_relaxed(&ceval->gil_drop_request, 0);
177 COMPUTE_EVAL_BREAKER(tstate, ceval, ceval2);
178}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000179
Eric Snowfdf282d2019-01-11 14:26:55 -0700180
Victor Stinnerda2914d2020-03-20 09:29:08 +0100181static inline void
182SIGNAL_PENDING_CALLS(PyThreadState *tstate)
183{
184 assert(is_tstate_valid(tstate));
Victor Stinnerd8316882020-03-20 14:50:35 +0100185 struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100186 struct _ceval_state *ceval2 = &tstate->interp->ceval;
187 _Py_atomic_store_relaxed(&ceval2->pending.calls_to_do, 1);
Victor Stinnerd8316882020-03-20 14:50:35 +0100188 COMPUTE_EVAL_BREAKER(tstate, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100189}
Eric Snowfdf282d2019-01-11 14:26:55 -0700190
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000191
Victor Stinnerda2914d2020-03-20 09:29:08 +0100192static inline void
193UNSIGNAL_PENDING_CALLS(PyThreadState *tstate)
194{
195 assert(is_tstate_valid(tstate));
196 struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
197 struct _ceval_state *ceval2 = &tstate->interp->ceval;
198 _Py_atomic_store_relaxed(&ceval2->pending.calls_to_do, 0);
199 COMPUTE_EVAL_BREAKER(tstate, ceval, ceval2);
200}
201
202
203static inline void
204SIGNAL_PENDING_SIGNALS(PyThreadState *tstate)
205{
206 assert(is_tstate_valid(tstate));
207 struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
208 struct _ceval_state *ceval2 = &tstate->interp->ceval;
209 _Py_atomic_store_relaxed(&ceval->signals_pending, 1);
210 /* eval_breaker is not set to 1 if thread_can_handle_signals() is false */
211 COMPUTE_EVAL_BREAKER(tstate, ceval, ceval2);
212}
213
214
215static inline void
216UNSIGNAL_PENDING_SIGNALS(PyThreadState *tstate)
217{
218 assert(is_tstate_valid(tstate));
219 struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
220 struct _ceval_state *ceval2 = &tstate->interp->ceval;
221 _Py_atomic_store_relaxed(&ceval->signals_pending, 0);
222 COMPUTE_EVAL_BREAKER(tstate, ceval, ceval2);
223}
224
225
226static inline void
227SIGNAL_ASYNC_EXC(PyThreadState *tstate)
228{
229 assert(is_tstate_valid(tstate));
230 struct _ceval_state *ceval2 = &tstate->interp->ceval;
231 ceval2->pending.async_exc = 1;
232 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
233}
234
235
236static inline void
237UNSIGNAL_ASYNC_EXC(PyThreadState *tstate)
238{
239 assert(is_tstate_valid(tstate));
240 struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
241 struct _ceval_state *ceval2 = &tstate->interp->ceval;
242 ceval2->pending.async_exc = 0;
243 COMPUTE_EVAL_BREAKER(tstate, ceval, ceval2);
244}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000245
246
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000247#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000248#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000249#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000250#include "pythread.h"
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000251#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000252
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100253static void
254ensure_tstate_not_null(const char *func, PyThreadState *tstate)
255{
256 if (tstate == NULL) {
Victor Stinner23ef89d2020-03-18 02:26:04 +0100257 _Py_FatalErrorFunc(func,
258 "current thread state is NULL (released GIL?)");
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100259 }
260}
261
262
Tim Peters7f468f22004-10-11 02:40:51 +0000263int
Victor Stinner175a7042020-03-10 00:37:48 +0100264_PyEval_ThreadsInitialized(_PyRuntimeState *runtime)
265{
266 return gil_created(&runtime->ceval.gil);
267}
268
269int
Tim Peters7f468f22004-10-11 02:40:51 +0000270PyEval_ThreadsInitialized(void)
271{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100272 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner175a7042020-03-10 00:37:48 +0100273 return _PyEval_ThreadsInitialized(runtime);
Tim Peters7f468f22004-10-11 02:40:51 +0000274}
275
Victor Stinner111e4ee2020-03-09 21:24:14 +0100276PyStatus
277_PyEval_InitThreads(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000278{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100279 assert(is_tstate_valid(tstate));
280
Victor Stinner50e6e992020-03-19 02:41:21 +0100281 if (_Py_IsMainInterpreter(tstate)) {
282 struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil;
283 if (gil_created(gil)) {
284 return _PyStatus_OK();
285 }
286
287 PyThread_init_thread();
288 create_gil(gil);
289
290 take_gil(tstate);
Victor Stinner111e4ee2020-03-09 21:24:14 +0100291 }
292
Victor Stinner50e6e992020-03-19 02:41:21 +0100293 struct _pending_calls *pending = &tstate->interp->ceval.pending;
294 assert(pending->lock == NULL);
Victor Stinnere225beb2019-06-03 18:14:24 +0200295 pending->lock = PyThread_allocate_lock();
296 if (pending->lock == NULL) {
Victor Stinner111e4ee2020-03-09 21:24:14 +0100297 return _PyStatus_NO_MEMORY();
298 }
Victor Stinner85f5a692020-03-09 22:12:04 +0100299
Victor Stinner111e4ee2020-03-09 21:24:14 +0100300 return _PyStatus_OK();
301}
302
303void
304PyEval_InitThreads(void)
305{
Victor Stinnerb4698ec2020-03-10 01:28:54 +0100306 /* Do nothing: kept for backward compatibility */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000307}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000308
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000309void
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100310_PyEval_FiniThreads(PyThreadState *tstate)
Antoine Pitrou1df15362010-09-13 14:16:46 +0000311{
Victor Stinner50e6e992020-03-19 02:41:21 +0100312 struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil;
Victor Stinner09532fe2019-05-10 23:39:09 +0200313 if (!gil_created(gil)) {
Antoine Pitrou1df15362010-09-13 14:16:46 +0000314 return;
Victor Stinnera7126792019-03-19 14:19:38 +0100315 }
316
Victor Stinner09532fe2019-05-10 23:39:09 +0200317 destroy_gil(gil);
318 assert(!gil_created(gil));
Victor Stinner99fcc612019-04-29 13:04:07 +0200319
Victor Stinner50e6e992020-03-19 02:41:21 +0100320 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Victor Stinnere225beb2019-06-03 18:14:24 +0200321 if (pending->lock != NULL) {
322 PyThread_free_lock(pending->lock);
323 pending->lock = NULL;
324 }
Antoine Pitrou1df15362010-09-13 14:16:46 +0000325}
326
327void
Inada Naoki91234a12019-06-03 21:30:58 +0900328_PyEval_Fini(void)
329{
330#if OPCACHE_STATS
331 fprintf(stderr, "-- Opcode cache number of objects = %zd\n",
332 opcache_code_objects);
333
334 fprintf(stderr, "-- Opcode cache total extra mem = %zd\n",
335 opcache_code_objects_extra_mem);
336
337 fprintf(stderr, "\n");
338
339 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL hits = %zd (%d%%)\n",
340 opcache_global_hits,
341 (int) (100.0 * opcache_global_hits /
342 (opcache_global_hits + opcache_global_misses)));
343
344 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL misses = %zd (%d%%)\n",
345 opcache_global_misses,
346 (int) (100.0 * opcache_global_misses /
347 (opcache_global_hits + opcache_global_misses)));
348
349 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL opts = %zd\n",
350 opcache_global_opts);
351
352 fprintf(stderr, "\n");
353#endif
354}
355
356void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000357PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000358{
Victor Stinner09532fe2019-05-10 23:39:09 +0200359 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200360 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100361 ensure_tstate_not_null(__func__, tstate);
362
Victor Stinner85f5a692020-03-09 22:12:04 +0100363 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000364}
365
366void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000367PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000368{
Victor Stinner09532fe2019-05-10 23:39:09 +0200369 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200370 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100372 We therefore avoid PyThreadState_Get() which dumps a fatal error
Victor Stinnerda2914d2020-03-20 09:29:08 +0100373 in debug mode. */
374 drop_gil(&runtime->ceval, tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000375}
376
377void
Victor Stinner23ef89d2020-03-18 02:26:04 +0100378_PyEval_ReleaseLock(PyThreadState *tstate)
379{
380 struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100381 drop_gil(ceval, tstate);
Victor Stinner23ef89d2020-03-18 02:26:04 +0100382}
383
384void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000385PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000386{
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100387 ensure_tstate_not_null(__func__, tstate);
388
Victor Stinner85f5a692020-03-09 22:12:04 +0100389 take_gil(tstate);
Victor Stinnere225beb2019-06-03 18:14:24 +0200390
Victor Stinner85f5a692020-03-09 22:12:04 +0100391 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
392 if (_PyThreadState_Swap(gilstate, tstate) != NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100393 Py_FatalError("non-NULL old thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200394 }
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000395}
396
397void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000398PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000399{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100400 assert(is_tstate_valid(tstate));
Victor Stinner09532fe2019-05-10 23:39:09 +0200401
Victor Stinner01b1cc12019-11-20 02:27:56 +0100402 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200403 PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
404 if (new_tstate != tstate) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100405 Py_FatalError("wrong thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200406 }
Victor Stinnerda2914d2020-03-20 09:29:08 +0100407 drop_gil(&runtime->ceval, tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000408}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000409
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200410/* This function is called from PyOS_AfterFork_Child to destroy all threads
411 * which are not running in the child process, and clear internal locks
412 * which might be held by those threads.
413 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000414
415void
Victor Stinnerd5d9e812019-05-13 12:35:37 +0200416_PyEval_ReInitThreads(_PyRuntimeState *runtime)
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000417{
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100418 struct _gil_runtime_state *gil = &runtime->ceval.gil;
419 if (!gil_created(gil)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 return;
Victor Stinner09532fe2019-05-10 23:39:09 +0200421 }
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100422 recreate_gil(gil);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100423 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
424 ensure_tstate_not_null(__func__, tstate);
Victor Stinner85f5a692020-03-09 22:12:04 +0100425
426 take_gil(tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700427
Victor Stinner50e6e992020-03-19 02:41:21 +0100428 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200429 pending->lock = PyThread_allocate_lock();
430 if (pending->lock == NULL) {
Eric Snow8479a342019-03-08 23:44:33 -0700431 Py_FatalError("Can't initialize threads for pending calls");
432 }
Jesse Nollera8513972008-07-17 16:49:17 +0000433
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200434 /* Destroy all threads except the current one */
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100435 _PyThreadState_DeleteExcept(runtime, tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000436}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000437
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000438/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600439 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000440
441void
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100442_PyEval_SignalAsyncExc(PyThreadState *tstate)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000443{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100444 SIGNAL_ASYNC_EXC(tstate);
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000445}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000446
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000447PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000448PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000449{
Victor Stinner09532fe2019-05-10 23:39:09 +0200450 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200451 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100452
Victor Stinner09532fe2019-05-10 23:39:09 +0200453 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100454 ensure_tstate_not_null(__func__, tstate);
455
Victor Stinnere225beb2019-06-03 18:14:24 +0200456 assert(gil_created(&ceval->gil));
Victor Stinnerda2914d2020-03-20 09:29:08 +0100457 drop_gil(ceval, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000459}
460
461void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000462PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000463{
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100464 ensure_tstate_not_null(__func__, tstate);
465
Victor Stinner85f5a692020-03-09 22:12:04 +0100466 take_gil(tstate);
Victor Stinner17c68b82020-01-30 12:20:48 +0100467
Victor Stinner85f5a692020-03-09 22:12:04 +0100468 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
469 _PyThreadState_Swap(gilstate, tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000470}
471
472
Guido van Rossuma9672091994-09-14 13:31:22 +0000473/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
474 signal handlers or Mac I/O completion routines) can schedule calls
475 to a function to be called synchronously.
476 The synchronous function is called with one void* argument.
477 It should return 0 for success or -1 for failure -- failure should
478 be accompanied by an exception.
479
480 If registry succeeds, the registry function returns 0; if it fails
481 (e.g. due to too many pending calls) it returns -1 (without setting
482 an exception condition).
483
484 Note that because registry may occur from within signal handlers,
485 or other asynchronous events, calling malloc() is unsafe!
486
Guido van Rossuma9672091994-09-14 13:31:22 +0000487 Any thread can schedule pending calls, but only the main thread
488 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000489 There is no facility to schedule calls to a particular thread, but
490 that should be easy to change, should that ever be required. In
491 that case, the static variables here should go into the python
492 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000493*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000494
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200495void
Victor Stinner8849e592020-03-18 19:28:53 +0100496_PyEval_SignalReceived(PyThreadState *tstate)
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200497{
498 /* bpo-30703: Function called when the C signal handler of Python gets a
Victor Stinner50e6e992020-03-19 02:41:21 +0100499 signal. We cannot queue a callback using _PyEval_AddPendingCall() since
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200500 that function is not async-signal-safe. */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100501 SIGNAL_PENDING_SIGNALS(tstate);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200502}
503
Eric Snow5be45a62019-03-08 22:47:07 -0700504/* Push one item onto the queue while holding the lock. */
505static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200506_push_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600507 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700508{
Eric Snow842a2f02019-03-15 15:47:51 -0600509 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700510 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600511 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700512 return -1; /* Queue full */
513 }
Eric Snow842a2f02019-03-15 15:47:51 -0600514 pending->calls[i].func = func;
515 pending->calls[i].arg = arg;
516 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700517 return 0;
518}
519
520/* Pop one item off the queue while holding the lock. */
521static void
Victor Stinnere225beb2019-06-03 18:14:24 +0200522_pop_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600523 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700524{
Eric Snow842a2f02019-03-15 15:47:51 -0600525 int i = pending->first;
526 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700527 return; /* Queue empty */
528 }
529
Eric Snow842a2f02019-03-15 15:47:51 -0600530 *func = pending->calls[i].func;
531 *arg = pending->calls[i].arg;
532 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700533}
534
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200535/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000536 scheduling to be made from any thread, and even from an executing
537 callback.
538 */
539
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000540int
Victor Stinner438a12d2019-05-24 17:01:38 +0200541_PyEval_AddPendingCall(PyThreadState *tstate,
Victor Stinner09532fe2019-05-10 23:39:09 +0200542 int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000543{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100544 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600545
546 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
547 if (pending->finishing) {
548 PyThread_release_lock(pending->lock);
549
550 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +0200551 _PyErr_Fetch(tstate, &exc, &val, &tb);
552 _PyErr_SetString(tstate, PyExc_SystemError,
Victor Stinner50e6e992020-03-19 02:41:21 +0100553 "Py_AddPendingCall: cannot add pending calls "
554 "(Python shutting down)");
Victor Stinner438a12d2019-05-24 17:01:38 +0200555 _PyErr_Print(tstate);
556 _PyErr_Restore(tstate, exc, val, tb);
Eric Snow842a2f02019-03-15 15:47:51 -0600557 return -1;
558 }
Victor Stinnere225beb2019-06-03 18:14:24 +0200559 int result = _push_pending_call(pending, func, arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600560 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700561
Victor Stinnere225beb2019-06-03 18:14:24 +0200562 /* signal main loop */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100563 SIGNAL_PENDING_CALLS(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000565}
566
Victor Stinner09532fe2019-05-10 23:39:09 +0200567int
568Py_AddPendingCall(int (*func)(void *), void *arg)
569{
Victor Stinner50e6e992020-03-19 02:41:21 +0100570 /* Best-effort to support subinterpreters and calls with the GIL released.
571
572 First attempt _PyThreadState_GET() since it supports subinterpreters.
573
574 If the GIL is released, _PyThreadState_GET() returns NULL . In this
575 case, use PyGILState_GetThisThreadState() which works even if the GIL
576 is released.
577
578 Sadly, PyGILState_GetThisThreadState() doesn't support subinterpreters:
579 see bpo-10915 and bpo-15751.
580
Victor Stinner8849e592020-03-18 19:28:53 +0100581 Py_AddPendingCall() doesn't require the caller to hold the GIL. */
Victor Stinner50e6e992020-03-19 02:41:21 +0100582 PyThreadState *tstate = _PyThreadState_GET();
583 if (tstate == NULL) {
584 tstate = PyGILState_GetThisThreadState();
585 }
586 /* tstate can be NULL if Py_AddPendingCall() is called in a thread
587 which is no Python thread state. Fail with a fatal error in this
588 case. */
589 ensure_tstate_not_null(__func__, tstate);
Victor Stinner8849e592020-03-18 19:28:53 +0100590 return _PyEval_AddPendingCall(tstate, func, arg);
Victor Stinner09532fe2019-05-10 23:39:09 +0200591}
592
Eric Snowfdf282d2019-01-11 14:26:55 -0700593static int
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100594handle_signals(PyThreadState *tstate)
Eric Snowfdf282d2019-01-11 14:26:55 -0700595{
Victor Stinnerd2a8e5b2020-03-20 13:38:58 +0100596 if (!_Py_ThreadCanHandleSignals(tstate)) {
Eric Snow64d6cc82019-02-23 15:40:43 -0700597 return 0;
598 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700599
Victor Stinnerda2914d2020-03-20 09:29:08 +0100600 UNSIGNAL_PENDING_SIGNALS(tstate);
Victor Stinner72818982020-03-26 22:28:11 +0100601 if (_PyErr_CheckSignalsTstate(tstate) < 0) {
602 /* On failure, re-schedule a call to handle_signals(). */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100603 SIGNAL_PENDING_SIGNALS(tstate);
Eric Snowfdf282d2019-01-11 14:26:55 -0700604 return -1;
605 }
606 return 0;
607}
608
609static int
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100610make_pending_calls(PyThreadState *tstate)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000611{
Victor Stinnerd8316882020-03-20 14:50:35 +0100612 /* only execute pending calls on main thread */
613 if (!_Py_ThreadCanHandlePendingCalls()) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200614 return 0;
615 }
616
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000617 /* don't perform recursive pending calls */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100618 static int busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700619 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700621 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200622 busy = 1;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100623
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200624 /* unsignal before starting to call callbacks, so that any callback
625 added in-between re-signals */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100626 UNSIGNAL_PENDING_CALLS(tstate);
Eric Snowfdf282d2019-01-11 14:26:55 -0700627 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 /* perform a bounded number of calls, in case of recursion */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100630 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Eric Snowfdf282d2019-01-11 14:26:55 -0700631 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700632 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 void *arg = NULL;
634
635 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600636 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200637 _pop_pending_call(pending, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600638 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700639
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100640 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700641 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100642 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700643 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700644 res = func(arg);
645 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200646 goto error;
647 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000648 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200649
Charles-François Natalif23339a2011-07-23 18:15:43 +0200650 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700651 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200652
653error:
654 busy = 0;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100655 SIGNAL_PENDING_CALLS(tstate);
Eric Snowfdf282d2019-01-11 14:26:55 -0700656 return res;
657}
658
Eric Snow842a2f02019-03-15 15:47:51 -0600659void
Victor Stinner2b1df452020-01-13 18:46:59 +0100660_Py_FinishPendingCalls(PyThreadState *tstate)
Eric Snow842a2f02019-03-15 15:47:51 -0600661{
Eric Snow842a2f02019-03-15 15:47:51 -0600662 assert(PyGILState_Check());
663
Victor Stinner50e6e992020-03-19 02:41:21 +0100664 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200665
Eric Snow842a2f02019-03-15 15:47:51 -0600666 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
667 pending->finishing = 1;
668 PyThread_release_lock(pending->lock);
669
670 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
671 return;
672 }
673
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100674 if (make_pending_calls(tstate) < 0) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200675 PyObject *exc, *val, *tb;
676 _PyErr_Fetch(tstate, &exc, &val, &tb);
677 PyErr_BadInternalCall();
678 _PyErr_ChainExceptions(exc, val, tb);
679 _PyErr_Print(tstate);
Eric Snow842a2f02019-03-15 15:47:51 -0600680 }
681}
682
Eric Snowfdf282d2019-01-11 14:26:55 -0700683/* Py_MakePendingCalls() is a simple wrapper for the sake
684 of backward-compatibility. */
685int
686Py_MakePendingCalls(void)
687{
688 assert(PyGILState_Check());
689
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100690 PyThreadState *tstate = _PyThreadState_GET();
691
Eric Snowfdf282d2019-01-11 14:26:55 -0700692 /* Python signal handler doesn't really queue a callback: it only signals
693 that a signal was received, see _PyEval_SignalReceived(). */
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100694 int res = handle_signals(tstate);
Eric Snowfdf282d2019-01-11 14:26:55 -0700695 if (res != 0) {
696 return res;
697 }
698
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100699 res = make_pending_calls(tstate);
Eric Snowb75b1a352019-04-12 10:20:10 -0600700 if (res != 0) {
701 return res;
702 }
703
704 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000705}
706
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000707/* The interpreter's recursion limit */
708
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000709#ifndef Py_DEFAULT_RECURSION_LIMIT
710#define Py_DEFAULT_RECURSION_LIMIT 1000
711#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600712
Eric Snow05351c12017-09-05 21:43:08 -0700713int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000714
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600715void
Victor Stinnerdab84232020-03-17 18:56:44 +0100716_PyEval_InitRuntimeState(struct _ceval_runtime_state *ceval)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600717{
Victor Stinnerdab84232020-03-17 18:56:44 +0100718 ceval->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600719 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Victor Stinnerdab84232020-03-17 18:56:44 +0100720 _gil_initialize(&ceval->gil);
721}
722
723void
724_PyEval_InitState(struct _ceval_state *ceval)
725{
726 /* PyInterpreterState_New() initializes ceval to zero */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600727}
728
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000729int
730Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000731{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100732 struct _ceval_runtime_state *ceval = &_PyRuntime.ceval;
733 return ceval->recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000734}
735
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000736void
737Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000738{
Victor Stinnere225beb2019-06-03 18:14:24 +0200739 struct _ceval_runtime_state *ceval = &_PyRuntime.ceval;
740 ceval->recursion_limit = new_limit;
Victor Stinnerdab84232020-03-17 18:56:44 +0100741 _Py_CheckRecursionLimit = new_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000742}
743
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100744/* The function _Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
Armin Rigo2b3eb402003-10-28 12:05:48 +0000745 if the recursion_depth reaches _Py_CheckRecursionLimit.
746 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
747 to guarantee that _Py_CheckRecursiveCall() is regularly called.
748 Without USE_STACKCHECK, there is no need for this. */
749int
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100750_Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000751{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100752 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200753 int recursion_limit = runtime->ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000754
755#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700756 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 if (PyOS_CheckStack()) {
758 --tstate->recursion_depth;
Victor Stinner438a12d2019-05-24 17:01:38 +0200759 _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 return -1;
761 }
pdox18967932017-10-25 23:03:01 -0700762 /* Needed for ABI backwards-compatibility (see bpo-31857) */
Eric Snow05351c12017-09-05 21:43:08 -0700763 _Py_CheckRecursionLimit = recursion_limit;
pdox18967932017-10-25 23:03:01 -0700764#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 if (tstate->recursion_critical)
766 /* Somebody asked that we don't check for recursion. */
767 return 0;
768 if (tstate->overflowed) {
769 if (tstate->recursion_depth > recursion_limit + 50) {
770 /* Overflowing while handling an overflow. Give up. */
771 Py_FatalError("Cannot recover from stack overflow.");
772 }
773 return 0;
774 }
775 if (tstate->recursion_depth > recursion_limit) {
776 --tstate->recursion_depth;
777 tstate->overflowed = 1;
Victor Stinner438a12d2019-05-24 17:01:38 +0200778 _PyErr_Format(tstate, PyExc_RecursionError,
779 "maximum recursion depth exceeded%s",
780 where);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 return -1;
782 }
783 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000784}
785
Victor Stinner09532fe2019-05-10 23:39:09 +0200786static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
Victor Stinner438a12d2019-05-24 17:01:38 +0200787static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000788
Victor Stinnere225beb2019-06-03 18:14:24 +0200789#define _Py_TracingPossible(ceval) ((ceval)->tracing_possible)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000790
Guido van Rossum374a9221991-04-04 10:40:29 +0000791
Guido van Rossumb209a111997-04-29 18:18:01 +0000792PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000793PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000794{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 return PyEval_EvalCodeEx(co,
796 globals, locals,
797 (PyObject **)NULL, 0,
798 (PyObject **)NULL, 0,
799 (PyObject **)NULL, 0,
800 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000801}
802
803
804/* Interpreter main loop */
805
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000806PyObject *
Victor Stinnerb9e68122019-11-14 12:20:46 +0100807PyEval_EvalFrame(PyFrameObject *f)
808{
Victor Stinner0b72b232020-03-12 23:18:39 +0100809 /* Function kept for backward compatibility */
Victor Stinnerb9e68122019-11-14 12:20:46 +0100810 PyThreadState *tstate = _PyThreadState_GET();
811 return _PyEval_EvalFrame(tstate, f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000812}
813
814PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000815PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000816{
Victor Stinnerb9e68122019-11-14 12:20:46 +0100817 PyThreadState *tstate = _PyThreadState_GET();
818 return _PyEval_EvalFrame(tstate, f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700819}
820
Victor Stinnerda2914d2020-03-20 09:29:08 +0100821
822/* Handle signals, pending calls, GIL drop request
823 and asynchronous exception */
824static int
825eval_frame_handle_pending(PyThreadState *tstate)
826{
827 /* Pending signals */
828 _PyRuntimeState * const runtime = &_PyRuntime;
829 struct _ceval_runtime_state *ceval = &runtime->ceval;
830 if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
831 if (handle_signals(tstate) != 0) {
832 return -1;
833 }
834 }
835
836 /* Pending calls */
837 struct _ceval_state *ceval2 = &tstate->interp->ceval;
838 if (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)) {
839 if (make_pending_calls(tstate) != 0) {
840 return -1;
841 }
842 }
843
844 /* GIL drop request */
845 if (_Py_atomic_load_relaxed(&ceval->gil_drop_request)) {
846 /* Give another thread a chance */
847 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
848 Py_FatalError("tstate mix-up");
849 }
850 drop_gil(ceval, tstate);
851
852 /* Other threads may run now */
853
854 take_gil(tstate);
855
856 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
857 Py_FatalError("orphan tstate");
858 }
859 }
860
861 /* Check for asynchronous exception. */
862 if (tstate->async_exc != NULL) {
863 PyObject *exc = tstate->async_exc;
864 tstate->async_exc = NULL;
865 UNSIGNAL_ASYNC_EXC(tstate);
866 _PyErr_SetNone(tstate, exc);
867 Py_DECREF(exc);
868 return -1;
869 }
870
871 return 0;
872}
873
Victor Stinnerc6944e72016-11-11 02:13:35 +0100874PyObject* _Py_HOT_FUNCTION
Victor Stinner0b72b232020-03-12 23:18:39 +0100875_PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
Brett Cannon3cebf932016-09-05 15:33:46 -0700876{
Victor Stinner0b72b232020-03-12 23:18:39 +0100877 ensure_tstate_not_null(__func__, tstate);
878
Guido van Rossum950361c1997-01-24 13:49:28 +0000879#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000881#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200882 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300883 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200884 int opcode; /* Current opcode */
885 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200886 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 PyObject *retval = NULL; /* Return value */
Victor Stinnerdab84232020-03-17 18:56:44 +0100888 struct _ceval_state * const ceval2 = &tstate->interp->ceval;
Victor Stinner50e6e992020-03-19 02:41:21 +0100889 _Py_atomic_int * const eval_breaker = &ceval2->eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000893
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000895
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 is true when the line being executed has changed. The
897 initial values are such as to make this false the first
898 time it is tested. */
899 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000900
Serhiy Storchakaab874002016-09-11 13:48:15 +0300901 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 PyObject *names;
903 PyObject *consts;
Inada Naoki91234a12019-06-03 21:30:58 +0900904 _PyOpcache *co_opcache;
Guido van Rossum374a9221991-04-04 10:40:29 +0000905
Brett Cannon368b4b72012-04-02 12:17:59 -0400906#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200907 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400908#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200909
Antoine Pitroub52ec782009-01-25 16:34:23 +0000910/* Computed GOTOs, or
911 the-optimization-commonly-but-improperly-known-as-"threaded code"
912 using gcc's labels-as-values extension
913 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
914
915 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000917 combined with a lookup table of jump addresses. However, since the
918 indirect jump instruction is shared by all opcodes, the CPU will have a
919 hard time making the right prediction for where to jump next (actually,
920 it will be always wrong except in the uncommon case of a sequence of
921 several identical opcodes).
922
923 "Threaded code" in contrast, uses an explicit jump table and an explicit
924 indirect jump instruction at the end of each opcode. Since the jump
925 instruction is at a different address for each opcode, the CPU will make a
926 separate prediction for each of these instructions, which is equivalent to
927 predicting the second opcode of each opcode pair. These predictions have
928 a much better chance to turn out valid, especially in small bytecode loops.
929
930 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000932 and potentially many more instructions (depending on the pipeline width).
933 A correctly predicted branch, however, is nearly free.
934
935 At the time of this writing, the "threaded code" version is up to 15-20%
936 faster than the normal "switch" version, depending on the compiler and the
937 CPU architecture.
938
939 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
940 because it would render the measurements invalid.
941
942
943 NOTE: care must be taken that the compiler doesn't try to "optimize" the
944 indirect jumps by sharing them between all opcodes. Such optimizations
945 can be disabled on gcc by using the -fno-gcse flag (or possibly
946 -fno-crossjumping).
947*/
948
Antoine Pitrou042b1282010-08-13 21:15:58 +0000949#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000950#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000951#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000952#endif
953
Antoine Pitrou042b1282010-08-13 21:15:58 +0000954#ifdef HAVE_COMPUTED_GOTOS
955 #ifndef USE_COMPUTED_GOTOS
956 #define USE_COMPUTED_GOTOS 1
957 #endif
958#else
959 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
960 #error "Computed gotos are not supported on this compiler."
961 #endif
962 #undef USE_COMPUTED_GOTOS
963 #define USE_COMPUTED_GOTOS 0
964#endif
965
966#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000967/* Import the static jump table */
968#include "opcode_targets.h"
969
Antoine Pitroub52ec782009-01-25 16:34:23 +0000970#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -0700971 op: \
972 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000973
Antoine Pitroub52ec782009-01-25 16:34:23 +0000974#ifdef LLTRACE
975#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 { \
Victor Stinnerdab84232020-03-17 18:56:44 +0100977 if (!lltrace && !_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300979 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300980 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 } \
982 goto fast_next_opcode; \
983 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000984#else
985#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 { \
Victor Stinnerdab84232020-03-17 18:56:44 +0100987 if (!_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300989 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300990 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 } \
992 goto fast_next_opcode; \
993 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000994#endif
995
Victor Stinner09532fe2019-05-10 23:39:09 +0200996#define DISPATCH() \
997 { \
998 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
999 FAST_DISPATCH(); \
1000 } \
1001 continue; \
1002 }
1003
Antoine Pitroub52ec782009-01-25 16:34:23 +00001004#else
Benjamin Petersonddd19492018-09-16 22:38:02 -07001005#define TARGET(op) op
Antoine Pitroub52ec782009-01-25 16:34:23 +00001006#define FAST_DISPATCH() goto fast_next_opcode
Victor Stinner09532fe2019-05-10 23:39:09 +02001007#define DISPATCH() continue
Antoine Pitroub52ec782009-01-25 16:34:23 +00001008#endif
1009
1010
Neal Norwitza81d2202002-07-14 00:27:26 +00001011/* Tuple access macros */
1012
1013#ifndef Py_DEBUG
1014#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
1015#else
1016#define GETITEM(v, i) PyTuple_GetItem((v), (i))
1017#endif
1018
Guido van Rossum374a9221991-04-04 10:40:29 +00001019/* Code access macros */
1020
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001021/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001022#define INSTR_OFFSET() \
1023 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001024#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001025 _Py_CODEUNIT word = *next_instr; \
1026 opcode = _Py_OPCODE(word); \
1027 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001028 next_instr++; \
1029 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +03001030#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
1031#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +00001032
Raymond Hettingerf606f872003-03-16 03:11:04 +00001033/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 Some opcodes tend to come in pairs thus making it possible to
1035 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001036 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 Verifying the prediction costs a single high-speed test of a register
1039 variable against a constant. If the pairing was good, then the
1040 processor's own internal branch predication has a high likelihood of
1041 success, resulting in a nearly zero-overhead transition to the
1042 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001043 including its unpredictable switch-case branch. Combined with the
1044 processor's internal branch prediction, a successful PREDICT has the
1045 effect of making the two opcodes run as if they were a single new opcode
1046 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001047
Georg Brandl86b2fb92008-07-16 03:43:04 +00001048 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 predictions turned-on and interpret the results as if some opcodes
1050 had been combined or turn-off predictions so that the opcode frequency
1051 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001052
1053 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 the CPU to record separate branch prediction information for each
1055 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001056
Raymond Hettingerf606f872003-03-16 03:11:04 +00001057*/
1058
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001059#define PREDICT_ID(op) PRED_##op
1060
Antoine Pitrou042b1282010-08-13 21:15:58 +00001061#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001062#define PREDICT(op) if (0) goto PREDICT_ID(op)
Raymond Hettingera7216982004-02-08 19:59:27 +00001063#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001064#define PREDICT(op) \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001065 do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001066 _Py_CODEUNIT word = *next_instr; \
1067 opcode = _Py_OPCODE(word); \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001068 if (opcode == op) { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001069 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001070 next_instr++; \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001071 goto PREDICT_ID(op); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001072 } \
1073 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +00001074#endif
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001075#define PREDICTED(op) PREDICT_ID(op):
Antoine Pitroub52ec782009-01-25 16:34:23 +00001076
Raymond Hettingerf606f872003-03-16 03:11:04 +00001077
Guido van Rossum374a9221991-04-04 10:40:29 +00001078/* Stack manipulation macros */
1079
Martin v. Löwis18e16552006-02-15 17:27:45 +00001080/* The stack can grow at most MAXINT deep, as co_nlocals and
1081 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001082#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1083#define EMPTY() (STACK_LEVEL() == 0)
1084#define TOP() (stack_pointer[-1])
1085#define SECOND() (stack_pointer[-2])
1086#define THIRD() (stack_pointer[-3])
1087#define FOURTH() (stack_pointer[-4])
1088#define PEEK(n) (stack_pointer[-(n)])
1089#define SET_TOP(v) (stack_pointer[-1] = (v))
1090#define SET_SECOND(v) (stack_pointer[-2] = (v))
1091#define SET_THIRD(v) (stack_pointer[-3] = (v))
1092#define SET_FOURTH(v) (stack_pointer[-4] = (v))
1093#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
1094#define BASIC_STACKADJ(n) (stack_pointer += n)
1095#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1096#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001097
Guido van Rossum96a42c81992-01-12 02:29:51 +00001098#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001100 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001101 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +02001102#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001103 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +00001104#define STACK_GROW(n) do { \
1105 assert(n >= 0); \
1106 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001107 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001108 assert(STACK_LEVEL() <= co->co_stacksize); \
1109 } while (0)
1110#define STACK_SHRINK(n) do { \
1111 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +02001112 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001113 (void)(BASIC_STACKADJ(-n)); \
1114 assert(STACK_LEVEL() <= co->co_stacksize); \
1115 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +00001116#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +02001117 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001118 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001119#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001120#define PUSH(v) BASIC_PUSH(v)
1121#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +00001122#define STACK_GROW(n) BASIC_STACKADJ(n)
1123#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001124#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001125#endif
1126
Guido van Rossum681d79a1995-07-18 14:51:37 +00001127/* Local variable macros */
1128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001130
1131/* The SETLOCAL() macro must not DECREF the local variable in-place and
1132 then store the new value; it must copy the old value to a temporary
1133 value, then store the new value, and then DECREF the temporary value.
1134 This is because it is possible that during the DECREF the frame is
1135 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1136 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001138 GETLOCAL(i) = value; \
1139 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001140
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001141
1142#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 while (STACK_LEVEL() > (b)->b_level) { \
1144 PyObject *v = POP(); \
1145 Py_XDECREF(v); \
1146 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001147
1148#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001149 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +01001151 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1153 while (STACK_LEVEL() > (b)->b_level + 3) { \
1154 value = POP(); \
1155 Py_XDECREF(value); \
1156 } \
Mark Shannonae3087c2017-10-22 22:41:51 +01001157 exc_info = tstate->exc_info; \
1158 type = exc_info->exc_type; \
1159 value = exc_info->exc_value; \
1160 traceback = exc_info->exc_traceback; \
1161 exc_info->exc_type = POP(); \
1162 exc_info->exc_value = POP(); \
1163 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 Py_XDECREF(type); \
1165 Py_XDECREF(value); \
1166 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001167 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001168
Inada Naoki91234a12019-06-03 21:30:58 +09001169 /* macros for opcode cache */
1170#define OPCACHE_CHECK() \
1171 do { \
1172 co_opcache = NULL; \
1173 if (co->co_opcache != NULL) { \
1174 unsigned char co_opt_offset = \
1175 co->co_opcache_map[next_instr - first_instr]; \
1176 if (co_opt_offset > 0) { \
1177 assert(co_opt_offset <= co->co_opcache_size); \
1178 co_opcache = &co->co_opcache[co_opt_offset - 1]; \
1179 assert(co_opcache != NULL); \
Inada Naoki91234a12019-06-03 21:30:58 +09001180 } \
1181 } \
1182 } while (0)
1183
1184#if OPCACHE_STATS
1185
1186#define OPCACHE_STAT_GLOBAL_HIT() \
1187 do { \
1188 if (co->co_opcache != NULL) opcache_global_hits++; \
1189 } while (0)
1190
1191#define OPCACHE_STAT_GLOBAL_MISS() \
1192 do { \
1193 if (co->co_opcache != NULL) opcache_global_misses++; \
1194 } while (0)
1195
1196#define OPCACHE_STAT_GLOBAL_OPT() \
1197 do { \
1198 if (co->co_opcache != NULL) opcache_global_opts++; \
1199 } while (0)
1200
1201#else /* OPCACHE_STATS */
1202
1203#define OPCACHE_STAT_GLOBAL_HIT()
1204#define OPCACHE_STAT_GLOBAL_MISS()
1205#define OPCACHE_STAT_GLOBAL_OPT()
1206
1207#endif
1208
Guido van Rossuma027efa1997-05-05 20:56:21 +00001209/* Start of code */
1210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 /* push frame */
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001212 if (_Py_EnterRecursiveCall(tstate, "")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001214 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 if (tstate->use_tracing) {
1219 if (tstate->c_tracefunc != NULL) {
1220 /* tstate->c_tracefunc, if defined, is a
1221 function that will be called on *every* entry
1222 to a code block. Its return value, if not
1223 None, is a function that will be called at
1224 the start of each executed line of code.
1225 (Actually, the function must return itself
1226 in order to continue tracing.) The trace
1227 functions are called with three arguments:
1228 a pointer to the current frame, a string
1229 indicating why the function is called, and
1230 an argument which depends on the situation.
1231 The global trace function is also called
1232 whenever an exception is detected. */
1233 if (call_trace_protected(tstate->c_tracefunc,
1234 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001235 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 /* Trace function raised an error */
1237 goto exit_eval_frame;
1238 }
1239 }
1240 if (tstate->c_profilefunc != NULL) {
1241 /* Similar for c_profilefunc, except it needn't
1242 return itself and isn't called for "line" events */
1243 if (call_trace_protected(tstate->c_profilefunc,
1244 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001245 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001246 /* Profile function raised an error */
1247 goto exit_eval_frame;
1248 }
1249 }
1250 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001251
Łukasz Langaa785c872016-09-09 17:37:37 -07001252 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1253 dtrace_function_entry(f);
1254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 co = f->f_code;
1256 names = co->co_names;
1257 consts = co->co_consts;
1258 fastlocals = f->f_localsplus;
1259 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001260 assert(PyBytes_Check(co->co_code));
1261 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001262 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1263 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1264 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001265 /*
1266 f->f_lasti refers to the index of the last instruction,
1267 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001268
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001269 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001270 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001272 When the PREDICT() macros are enabled, some opcode pairs follow in
1273 direct succession without updating f->f_lasti. A successful
1274 prediction effectively links the two codes together as if they
1275 were a single new opcode; accordingly,f->f_lasti will point to
1276 the first code in the pair (for instance, GET_ITER followed by
1277 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001278 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001280 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001281 next_instr = first_instr;
1282 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001283 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1284 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001285 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 stack_pointer = f->f_stacktop;
1287 assert(stack_pointer != NULL);
1288 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001289 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001290
Inada Naoki91234a12019-06-03 21:30:58 +09001291 if (co->co_opcache_flag < OPCACHE_MIN_RUNS) {
1292 co->co_opcache_flag++;
1293 if (co->co_opcache_flag == OPCACHE_MIN_RUNS) {
1294 if (_PyCode_InitOpcache(co) < 0) {
1295 return NULL;
1296 }
1297#if OPCACHE_STATS
1298 opcache_code_objects_extra_mem +=
1299 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1300 sizeof(_PyOpcache) * co->co_opcache_size;
1301 opcache_code_objects++;
1302#endif
1303 }
1304 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001305
Tim Peters5ca576e2001-06-18 22:08:13 +00001306#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001307 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001308#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001309
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001310 if (throwflag) /* support for generator.throw() */
1311 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001312
Victor Stinnerace47d72013-07-18 01:41:08 +02001313#ifdef Py_DEBUG
Victor Stinner0b72b232020-03-12 23:18:39 +01001314 /* _PyEval_EvalFrameDefault() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001315 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001316 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001317 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001318#endif
1319
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001320main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1323 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001324 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 /* Do periodic things. Doing this every time through
1327 the loop would add too much overhead, so we do it
1328 only every Nth instruction. We also do it if
1329 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1330 event needs attention (e.g. a signal handler or
1331 async I/O handler); see Py_AddPendingCall() and
1332 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001333
Eric Snow7bda9de2019-03-08 17:25:54 -07001334 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001335 opcode = _Py_OPCODE(*next_instr);
1336 if (opcode == SETUP_FINALLY ||
1337 opcode == SETUP_WITH ||
1338 opcode == BEFORE_ASYNC_WITH ||
1339 opcode == YIELD_FROM) {
1340 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001341 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001342 - If we're about to enter the 'with:'. It will prevent
1343 emitting a resource warning in the common idiom
1344 'with open(path) as file:'.
1345 - If we're about to enter the 'async with:'.
1346 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001347 *very* useful, but might help in some cases and it's
1348 traditional)
1349 - If we're resuming a chain of nested 'yield from' or
1350 'await' calls, then each frame is parked with YIELD_FROM
1351 as its next opcode. If the user hit control-C we want to
1352 wait until we've reached the innermost frame before
1353 running the signal handler and raising KeyboardInterrupt
1354 (see bpo-30039).
1355 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 goto fast_next_opcode;
1357 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001358
Victor Stinnerda2914d2020-03-20 09:29:08 +01001359 if (eval_frame_handle_pending(tstate) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001360 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001361 }
1362 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 fast_next_opcode:
1365 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001366
Łukasz Langaa785c872016-09-09 17:37:37 -07001367 if (PyDTrace_LINE_ENABLED())
1368 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001371
Victor Stinnerdab84232020-03-17 18:56:44 +01001372 if (_Py_TracingPossible(ceval2) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001373 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001374 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 /* see maybe_call_line_trace
1376 for expository comments */
1377 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001379 err = maybe_call_line_trace(tstate->c_tracefunc,
1380 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001381 tstate, f,
1382 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 /* Reload possibly changed frame fields */
1384 JUMPTO(f->f_lasti);
1385 if (f->f_stacktop != NULL) {
1386 stack_pointer = f->f_stacktop;
1387 f->f_stacktop = NULL;
1388 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001389 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001391 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001394 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001395
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001396 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001397 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001398#ifdef DYNAMIC_EXECUTION_PROFILE
1399#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 dxpairs[lastopcode][opcode]++;
1401 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001402#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001404#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001405
Guido van Rossum96a42c81992-01-12 02:29:51 +00001406#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001409 if (lltrace) {
1410 if (HAS_ARG(opcode)) {
1411 printf("%d: %d, %d\n",
1412 f->f_lasti, opcode, oparg);
1413 }
1414 else {
1415 printf("%d: %d\n",
1416 f->f_lasti, opcode);
1417 }
1418 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001419#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001421 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001424 It is essential that any operation that fails must goto error
1425 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001426
Benjamin Petersonddd19492018-09-16 22:38:02 -07001427 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001429 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001430
Benjamin Petersonddd19492018-09-16 22:38:02 -07001431 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001432 PyObject *value = GETLOCAL(oparg);
1433 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001434 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001435 UNBOUNDLOCAL_ERROR_MSG,
1436 PyTuple_GetItem(co->co_varnames, oparg));
1437 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001439 Py_INCREF(value);
1440 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001441 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001442 }
1443
Benjamin Petersonddd19492018-09-16 22:38:02 -07001444 case TARGET(LOAD_CONST): {
1445 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001446 PyObject *value = GETITEM(consts, oparg);
1447 Py_INCREF(value);
1448 PUSH(value);
1449 FAST_DISPATCH();
1450 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001451
Benjamin Petersonddd19492018-09-16 22:38:02 -07001452 case TARGET(STORE_FAST): {
1453 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001454 PyObject *value = POP();
1455 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001456 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001457 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001458
Benjamin Petersonddd19492018-09-16 22:38:02 -07001459 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001460 PyObject *value = POP();
1461 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001462 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001463 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001464
Benjamin Petersonddd19492018-09-16 22:38:02 -07001465 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001466 PyObject *top = TOP();
1467 PyObject *second = SECOND();
1468 SET_TOP(second);
1469 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001470 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001471 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001472
Benjamin Petersonddd19492018-09-16 22:38:02 -07001473 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001474 PyObject *top = TOP();
1475 PyObject *second = SECOND();
1476 PyObject *third = THIRD();
1477 SET_TOP(second);
1478 SET_SECOND(third);
1479 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001480 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001481 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001482
Benjamin Petersonddd19492018-09-16 22:38:02 -07001483 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001484 PyObject *top = TOP();
1485 PyObject *second = SECOND();
1486 PyObject *third = THIRD();
1487 PyObject *fourth = FOURTH();
1488 SET_TOP(second);
1489 SET_SECOND(third);
1490 SET_THIRD(fourth);
1491 SET_FOURTH(top);
1492 FAST_DISPATCH();
1493 }
1494
Benjamin Petersonddd19492018-09-16 22:38:02 -07001495 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001496 PyObject *top = TOP();
1497 Py_INCREF(top);
1498 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001499 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001500 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001501
Benjamin Petersonddd19492018-09-16 22:38:02 -07001502 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001503 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001504 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001505 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001506 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001507 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001508 SET_TOP(top);
1509 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001510 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001511 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001512
Benjamin Petersonddd19492018-09-16 22:38:02 -07001513 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001514 PyObject *value = TOP();
1515 PyObject *res = PyNumber_Positive(value);
1516 Py_DECREF(value);
1517 SET_TOP(res);
1518 if (res == NULL)
1519 goto error;
1520 DISPATCH();
1521 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001522
Benjamin Petersonddd19492018-09-16 22:38:02 -07001523 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001524 PyObject *value = TOP();
1525 PyObject *res = PyNumber_Negative(value);
1526 Py_DECREF(value);
1527 SET_TOP(res);
1528 if (res == NULL)
1529 goto error;
1530 DISPATCH();
1531 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001532
Benjamin Petersonddd19492018-09-16 22:38:02 -07001533 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001534 PyObject *value = TOP();
1535 int err = PyObject_IsTrue(value);
1536 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001537 if (err == 0) {
1538 Py_INCREF(Py_True);
1539 SET_TOP(Py_True);
1540 DISPATCH();
1541 }
1542 else if (err > 0) {
1543 Py_INCREF(Py_False);
1544 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001545 DISPATCH();
1546 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001547 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001548 goto error;
1549 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001550
Benjamin Petersonddd19492018-09-16 22:38:02 -07001551 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001552 PyObject *value = TOP();
1553 PyObject *res = PyNumber_Invert(value);
1554 Py_DECREF(value);
1555 SET_TOP(res);
1556 if (res == NULL)
1557 goto error;
1558 DISPATCH();
1559 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001560
Benjamin Petersonddd19492018-09-16 22:38:02 -07001561 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001562 PyObject *exp = POP();
1563 PyObject *base = TOP();
1564 PyObject *res = PyNumber_Power(base, exp, Py_None);
1565 Py_DECREF(base);
1566 Py_DECREF(exp);
1567 SET_TOP(res);
1568 if (res == NULL)
1569 goto error;
1570 DISPATCH();
1571 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001572
Benjamin Petersonddd19492018-09-16 22:38:02 -07001573 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001574 PyObject *right = POP();
1575 PyObject *left = TOP();
1576 PyObject *res = PyNumber_Multiply(left, right);
1577 Py_DECREF(left);
1578 Py_DECREF(right);
1579 SET_TOP(res);
1580 if (res == NULL)
1581 goto error;
1582 DISPATCH();
1583 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001584
Benjamin Petersonddd19492018-09-16 22:38:02 -07001585 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001586 PyObject *right = POP();
1587 PyObject *left = TOP();
1588 PyObject *res = PyNumber_MatrixMultiply(left, right);
1589 Py_DECREF(left);
1590 Py_DECREF(right);
1591 SET_TOP(res);
1592 if (res == NULL)
1593 goto error;
1594 DISPATCH();
1595 }
1596
Benjamin Petersonddd19492018-09-16 22:38:02 -07001597 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001598 PyObject *divisor = POP();
1599 PyObject *dividend = TOP();
1600 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1601 Py_DECREF(dividend);
1602 Py_DECREF(divisor);
1603 SET_TOP(quotient);
1604 if (quotient == NULL)
1605 goto error;
1606 DISPATCH();
1607 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001608
Benjamin Petersonddd19492018-09-16 22:38:02 -07001609 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001610 PyObject *divisor = POP();
1611 PyObject *dividend = TOP();
1612 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1613 Py_DECREF(dividend);
1614 Py_DECREF(divisor);
1615 SET_TOP(quotient);
1616 if (quotient == NULL)
1617 goto error;
1618 DISPATCH();
1619 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001620
Benjamin Petersonddd19492018-09-16 22:38:02 -07001621 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001622 PyObject *divisor = POP();
1623 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001624 PyObject *res;
1625 if (PyUnicode_CheckExact(dividend) && (
1626 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1627 // fast path; string formatting, but not if the RHS is a str subclass
1628 // (see issue28598)
1629 res = PyUnicode_Format(dividend, divisor);
1630 } else {
1631 res = PyNumber_Remainder(dividend, divisor);
1632 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001633 Py_DECREF(divisor);
1634 Py_DECREF(dividend);
1635 SET_TOP(res);
1636 if (res == NULL)
1637 goto error;
1638 DISPATCH();
1639 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001640
Benjamin Petersonddd19492018-09-16 22:38:02 -07001641 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001642 PyObject *right = POP();
1643 PyObject *left = TOP();
1644 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001645 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1646 CPython using bytecode, it is simply worthless.
1647 See http://bugs.python.org/issue21955 and
1648 http://bugs.python.org/issue10044 for the discussion. In short,
1649 no patch shown any impact on a realistic benchmark, only a minor
1650 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001651 if (PyUnicode_CheckExact(left) &&
1652 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001653 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001654 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001655 }
1656 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001657 sum = PyNumber_Add(left, right);
1658 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001659 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001660 Py_DECREF(right);
1661 SET_TOP(sum);
1662 if (sum == NULL)
1663 goto error;
1664 DISPATCH();
1665 }
1666
Benjamin Petersonddd19492018-09-16 22:38:02 -07001667 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001668 PyObject *right = POP();
1669 PyObject *left = TOP();
1670 PyObject *diff = PyNumber_Subtract(left, right);
1671 Py_DECREF(right);
1672 Py_DECREF(left);
1673 SET_TOP(diff);
1674 if (diff == NULL)
1675 goto error;
1676 DISPATCH();
1677 }
1678
Benjamin Petersonddd19492018-09-16 22:38:02 -07001679 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001680 PyObject *sub = POP();
1681 PyObject *container = TOP();
1682 PyObject *res = PyObject_GetItem(container, sub);
1683 Py_DECREF(container);
1684 Py_DECREF(sub);
1685 SET_TOP(res);
1686 if (res == NULL)
1687 goto error;
1688 DISPATCH();
1689 }
1690
Benjamin Petersonddd19492018-09-16 22:38:02 -07001691 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001692 PyObject *right = POP();
1693 PyObject *left = TOP();
1694 PyObject *res = PyNumber_Lshift(left, right);
1695 Py_DECREF(left);
1696 Py_DECREF(right);
1697 SET_TOP(res);
1698 if (res == NULL)
1699 goto error;
1700 DISPATCH();
1701 }
1702
Benjamin Petersonddd19492018-09-16 22:38:02 -07001703 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001704 PyObject *right = POP();
1705 PyObject *left = TOP();
1706 PyObject *res = PyNumber_Rshift(left, right);
1707 Py_DECREF(left);
1708 Py_DECREF(right);
1709 SET_TOP(res);
1710 if (res == NULL)
1711 goto error;
1712 DISPATCH();
1713 }
1714
Benjamin Petersonddd19492018-09-16 22:38:02 -07001715 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001716 PyObject *right = POP();
1717 PyObject *left = TOP();
1718 PyObject *res = PyNumber_And(left, right);
1719 Py_DECREF(left);
1720 Py_DECREF(right);
1721 SET_TOP(res);
1722 if (res == NULL)
1723 goto error;
1724 DISPATCH();
1725 }
1726
Benjamin Petersonddd19492018-09-16 22:38:02 -07001727 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001728 PyObject *right = POP();
1729 PyObject *left = TOP();
1730 PyObject *res = PyNumber_Xor(left, right);
1731 Py_DECREF(left);
1732 Py_DECREF(right);
1733 SET_TOP(res);
1734 if (res == NULL)
1735 goto error;
1736 DISPATCH();
1737 }
1738
Benjamin Petersonddd19492018-09-16 22:38:02 -07001739 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001740 PyObject *right = POP();
1741 PyObject *left = TOP();
1742 PyObject *res = PyNumber_Or(left, right);
1743 Py_DECREF(left);
1744 Py_DECREF(right);
1745 SET_TOP(res);
1746 if (res == NULL)
1747 goto error;
1748 DISPATCH();
1749 }
1750
Benjamin Petersonddd19492018-09-16 22:38:02 -07001751 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001752 PyObject *v = POP();
1753 PyObject *list = PEEK(oparg);
1754 int err;
1755 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001756 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001757 if (err != 0)
1758 goto error;
1759 PREDICT(JUMP_ABSOLUTE);
1760 DISPATCH();
1761 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001762
Benjamin Petersonddd19492018-09-16 22:38:02 -07001763 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001764 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001765 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001766 int err;
1767 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001768 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001769 if (err != 0)
1770 goto error;
1771 PREDICT(JUMP_ABSOLUTE);
1772 DISPATCH();
1773 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001774
Benjamin Petersonddd19492018-09-16 22:38:02 -07001775 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001776 PyObject *exp = POP();
1777 PyObject *base = TOP();
1778 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1779 Py_DECREF(base);
1780 Py_DECREF(exp);
1781 SET_TOP(res);
1782 if (res == NULL)
1783 goto error;
1784 DISPATCH();
1785 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001786
Benjamin Petersonddd19492018-09-16 22:38:02 -07001787 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001788 PyObject *right = POP();
1789 PyObject *left = TOP();
1790 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1791 Py_DECREF(left);
1792 Py_DECREF(right);
1793 SET_TOP(res);
1794 if (res == NULL)
1795 goto error;
1796 DISPATCH();
1797 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001798
Benjamin Petersonddd19492018-09-16 22:38:02 -07001799 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001800 PyObject *right = POP();
1801 PyObject *left = TOP();
1802 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1803 Py_DECREF(left);
1804 Py_DECREF(right);
1805 SET_TOP(res);
1806 if (res == NULL)
1807 goto error;
1808 DISPATCH();
1809 }
1810
Benjamin Petersonddd19492018-09-16 22:38:02 -07001811 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001812 PyObject *divisor = POP();
1813 PyObject *dividend = TOP();
1814 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1815 Py_DECREF(dividend);
1816 Py_DECREF(divisor);
1817 SET_TOP(quotient);
1818 if (quotient == NULL)
1819 goto error;
1820 DISPATCH();
1821 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001822
Benjamin Petersonddd19492018-09-16 22:38:02 -07001823 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001824 PyObject *divisor = POP();
1825 PyObject *dividend = TOP();
1826 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1827 Py_DECREF(dividend);
1828 Py_DECREF(divisor);
1829 SET_TOP(quotient);
1830 if (quotient == NULL)
1831 goto error;
1832 DISPATCH();
1833 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001834
Benjamin Petersonddd19492018-09-16 22:38:02 -07001835 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001836 PyObject *right = POP();
1837 PyObject *left = TOP();
1838 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1839 Py_DECREF(left);
1840 Py_DECREF(right);
1841 SET_TOP(mod);
1842 if (mod == NULL)
1843 goto error;
1844 DISPATCH();
1845 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001846
Benjamin Petersonddd19492018-09-16 22:38:02 -07001847 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001848 PyObject *right = POP();
1849 PyObject *left = TOP();
1850 PyObject *sum;
1851 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001852 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001853 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001854 }
1855 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001856 sum = PyNumber_InPlaceAdd(left, right);
1857 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001858 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001859 Py_DECREF(right);
1860 SET_TOP(sum);
1861 if (sum == NULL)
1862 goto error;
1863 DISPATCH();
1864 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001865
Benjamin Petersonddd19492018-09-16 22:38:02 -07001866 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001867 PyObject *right = POP();
1868 PyObject *left = TOP();
1869 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1870 Py_DECREF(left);
1871 Py_DECREF(right);
1872 SET_TOP(diff);
1873 if (diff == NULL)
1874 goto error;
1875 DISPATCH();
1876 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001877
Benjamin Petersonddd19492018-09-16 22:38:02 -07001878 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001879 PyObject *right = POP();
1880 PyObject *left = TOP();
1881 PyObject *res = PyNumber_InPlaceLshift(left, right);
1882 Py_DECREF(left);
1883 Py_DECREF(right);
1884 SET_TOP(res);
1885 if (res == NULL)
1886 goto error;
1887 DISPATCH();
1888 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001889
Benjamin Petersonddd19492018-09-16 22:38:02 -07001890 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001891 PyObject *right = POP();
1892 PyObject *left = TOP();
1893 PyObject *res = PyNumber_InPlaceRshift(left, right);
1894 Py_DECREF(left);
1895 Py_DECREF(right);
1896 SET_TOP(res);
1897 if (res == NULL)
1898 goto error;
1899 DISPATCH();
1900 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001901
Benjamin Petersonddd19492018-09-16 22:38:02 -07001902 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001903 PyObject *right = POP();
1904 PyObject *left = TOP();
1905 PyObject *res = PyNumber_InPlaceAnd(left, right);
1906 Py_DECREF(left);
1907 Py_DECREF(right);
1908 SET_TOP(res);
1909 if (res == NULL)
1910 goto error;
1911 DISPATCH();
1912 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001913
Benjamin Petersonddd19492018-09-16 22:38:02 -07001914 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001915 PyObject *right = POP();
1916 PyObject *left = TOP();
1917 PyObject *res = PyNumber_InPlaceXor(left, right);
1918 Py_DECREF(left);
1919 Py_DECREF(right);
1920 SET_TOP(res);
1921 if (res == NULL)
1922 goto error;
1923 DISPATCH();
1924 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001925
Benjamin Petersonddd19492018-09-16 22:38:02 -07001926 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001927 PyObject *right = POP();
1928 PyObject *left = TOP();
1929 PyObject *res = PyNumber_InPlaceOr(left, right);
1930 Py_DECREF(left);
1931 Py_DECREF(right);
1932 SET_TOP(res);
1933 if (res == NULL)
1934 goto error;
1935 DISPATCH();
1936 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001937
Benjamin Petersonddd19492018-09-16 22:38:02 -07001938 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001939 PyObject *sub = TOP();
1940 PyObject *container = SECOND();
1941 PyObject *v = THIRD();
1942 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001943 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001944 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001945 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001946 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001947 Py_DECREF(container);
1948 Py_DECREF(sub);
1949 if (err != 0)
1950 goto error;
1951 DISPATCH();
1952 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001953
Benjamin Petersonddd19492018-09-16 22:38:02 -07001954 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001955 PyObject *sub = TOP();
1956 PyObject *container = SECOND();
1957 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001958 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00001959 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001960 err = PyObject_DelItem(container, sub);
1961 Py_DECREF(container);
1962 Py_DECREF(sub);
1963 if (err != 0)
1964 goto error;
1965 DISPATCH();
1966 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001967
Benjamin Petersonddd19492018-09-16 22:38:02 -07001968 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01001969 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001970 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001971 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001972 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001973 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001974 _PyErr_SetString(tstate, PyExc_RuntimeError,
1975 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001976 Py_DECREF(value);
1977 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001978 }
Petr Viktorinffd97532020-02-11 17:46:57 +01001979 res = PyObject_CallOneArg(hook, value);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001980 Py_DECREF(value);
1981 if (res == NULL)
1982 goto error;
1983 Py_DECREF(res);
1984 DISPATCH();
1985 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001986
Benjamin Petersonddd19492018-09-16 22:38:02 -07001987 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001988 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001989 switch (oparg) {
1990 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001991 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001992 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001993 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001994 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001995 /* fall through */
1996 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02001997 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001998 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001999 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002000 break;
2001 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02002002 _PyErr_SetString(tstate, PyExc_SystemError,
2003 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004 break;
2005 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002006 goto error;
2007 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002008
Benjamin Petersonddd19492018-09-16 22:38:02 -07002009 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002010 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002011 assert(f->f_iblock == 0);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002012 assert(EMPTY());
2013 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002014 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002015
Benjamin Petersonddd19492018-09-16 22:38:02 -07002016 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002017 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002018 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002019 PyObject *obj = TOP();
2020 PyTypeObject *type = Py_TYPE(obj);
2021
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002022 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002023 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002024 }
Yury Selivanov75445082015-05-11 22:57:16 -04002025
2026 if (getter != NULL) {
2027 iter = (*getter)(obj);
2028 Py_DECREF(obj);
2029 if (iter == NULL) {
2030 SET_TOP(NULL);
2031 goto error;
2032 }
2033 }
2034 else {
2035 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002036 _PyErr_Format(tstate, PyExc_TypeError,
2037 "'async for' requires an object with "
2038 "__aiter__ method, got %.100s",
2039 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002040 Py_DECREF(obj);
2041 goto error;
2042 }
2043
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002044 if (Py_TYPE(iter)->tp_as_async == NULL ||
2045 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002046
Yury Selivanov398ff912017-03-02 22:20:00 -05002047 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002048 _PyErr_Format(tstate, PyExc_TypeError,
2049 "'async for' received an object from __aiter__ "
2050 "that does not implement __anext__: %.100s",
2051 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002052 Py_DECREF(iter);
2053 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002054 }
2055
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002056 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04002057 DISPATCH();
2058 }
2059
Benjamin Petersonddd19492018-09-16 22:38:02 -07002060 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002061 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002062 PyObject *next_iter = NULL;
2063 PyObject *awaitable = NULL;
2064 PyObject *aiter = TOP();
2065 PyTypeObject *type = Py_TYPE(aiter);
2066
Yury Selivanoveb636452016-09-08 22:01:51 -07002067 if (PyAsyncGen_CheckExact(aiter)) {
2068 awaitable = type->tp_as_async->am_anext(aiter);
2069 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002070 goto error;
2071 }
Yury Selivanoveb636452016-09-08 22:01:51 -07002072 } else {
2073 if (type->tp_as_async != NULL){
2074 getter = type->tp_as_async->am_anext;
2075 }
Yury Selivanov75445082015-05-11 22:57:16 -04002076
Yury Selivanoveb636452016-09-08 22:01:51 -07002077 if (getter != NULL) {
2078 next_iter = (*getter)(aiter);
2079 if (next_iter == NULL) {
2080 goto error;
2081 }
2082 }
2083 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02002084 _PyErr_Format(tstate, PyExc_TypeError,
2085 "'async for' requires an iterator with "
2086 "__anext__ method, got %.100s",
2087 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07002088 goto error;
2089 }
Yury Selivanov75445082015-05-11 22:57:16 -04002090
Yury Selivanoveb636452016-09-08 22:01:51 -07002091 awaitable = _PyCoro_GetAwaitableIter(next_iter);
2092 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05002093 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07002094 PyExc_TypeError,
2095 "'async for' received an invalid object "
2096 "from __anext__: %.100s",
2097 Py_TYPE(next_iter)->tp_name);
2098
2099 Py_DECREF(next_iter);
2100 goto error;
2101 } else {
2102 Py_DECREF(next_iter);
2103 }
2104 }
Yury Selivanov75445082015-05-11 22:57:16 -04002105
2106 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002107 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002108 DISPATCH();
2109 }
2110
Benjamin Petersonddd19492018-09-16 22:38:02 -07002111 case TARGET(GET_AWAITABLE): {
2112 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002113 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002114 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002115
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002116 if (iter == NULL) {
Mark Shannonfee55262019-11-21 09:11:43 +00002117 int opcode_at_minus_3 = 0;
2118 if ((next_instr - first_instr) > 2) {
2119 opcode_at_minus_3 = _Py_OPCODE(next_instr[-3]);
2120 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002121 format_awaitable_error(tstate, Py_TYPE(iterable),
Mark Shannonfee55262019-11-21 09:11:43 +00002122 opcode_at_minus_3,
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002123 _Py_OPCODE(next_instr[-2]));
2124 }
2125
Yury Selivanov75445082015-05-11 22:57:16 -04002126 Py_DECREF(iterable);
2127
Yury Selivanovc724bae2016-03-02 11:30:46 -05002128 if (iter != NULL && PyCoro_CheckExact(iter)) {
2129 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2130 if (yf != NULL) {
2131 /* `iter` is a coroutine object that is being
2132 awaited, `yf` is a pointer to the current awaitable
2133 being awaited on. */
2134 Py_DECREF(yf);
2135 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002136 _PyErr_SetString(tstate, PyExc_RuntimeError,
2137 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002138 /* The code below jumps to `error` if `iter` is NULL. */
2139 }
2140 }
2141
Yury Selivanov75445082015-05-11 22:57:16 -04002142 SET_TOP(iter); /* Even if it's NULL */
2143
2144 if (iter == NULL) {
2145 goto error;
2146 }
2147
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002148 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002149 DISPATCH();
2150 }
2151
Benjamin Petersonddd19492018-09-16 22:38:02 -07002152 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002153 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002154 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002155 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002156 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
2157 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002158 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002159 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002160 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002161 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002162 else
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02002163 retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002164 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002165 Py_DECREF(v);
2166 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002167 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002168 if (tstate->c_tracefunc != NULL
Victor Stinner438a12d2019-05-24 17:01:38 +02002169 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002170 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002171 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002172 if (err < 0)
2173 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002174 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002175 SET_TOP(val);
2176 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002177 }
Martin Panter95f53c12016-07-18 08:23:26 +00002178 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002179 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002180 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002181 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002182 f->f_lasti -= sizeof(_Py_CODEUNIT);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002183 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002184 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002185
Benjamin Petersonddd19492018-09-16 22:38:02 -07002186 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002187 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002188
2189 if (co->co_flags & CO_ASYNC_GENERATOR) {
2190 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2191 Py_DECREF(retval);
2192 if (w == NULL) {
2193 retval = NULL;
2194 goto error;
2195 }
2196 retval = w;
2197 }
2198
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002199 f->f_stacktop = stack_pointer;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002200 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002201 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002202
Benjamin Petersonddd19492018-09-16 22:38:02 -07002203 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002204 PyObject *type, *value, *traceback;
2205 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002206 PyTryBlock *b = PyFrame_BlockPop(f);
2207 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002208 _PyErr_SetString(tstate, PyExc_SystemError,
2209 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002210 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002211 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002212 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2213 STACK_LEVEL() <= (b)->b_level + 4);
2214 exc_info = tstate->exc_info;
2215 type = exc_info->exc_type;
2216 value = exc_info->exc_value;
2217 traceback = exc_info->exc_traceback;
2218 exc_info->exc_type = POP();
2219 exc_info->exc_value = POP();
2220 exc_info->exc_traceback = POP();
2221 Py_XDECREF(type);
2222 Py_XDECREF(value);
2223 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002224 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002225 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002226
Benjamin Petersonddd19492018-09-16 22:38:02 -07002227 case TARGET(POP_BLOCK): {
2228 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002229 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002230 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002231 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002232
Mark Shannonfee55262019-11-21 09:11:43 +00002233 case TARGET(RERAISE): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002234 PyObject *exc = POP();
Mark Shannonfee55262019-11-21 09:11:43 +00002235 PyObject *val = POP();
2236 PyObject *tb = POP();
2237 assert(PyExceptionClass_Check(exc));
Victor Stinner61f4db82020-01-28 03:37:45 +01002238 _PyErr_Restore(tstate, exc, val, tb);
Mark Shannonfee55262019-11-21 09:11:43 +00002239 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002240 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002241
Benjamin Petersonddd19492018-09-16 22:38:02 -07002242 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002243 PyObject *exc = POP();
2244 assert(PyExceptionClass_Check(exc));
2245 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2246 PyTryBlock *b = PyFrame_BlockPop(f);
2247 assert(b->b_type == EXCEPT_HANDLER);
2248 Py_DECREF(exc);
2249 UNWIND_EXCEPT_HANDLER(b);
2250 Py_DECREF(POP());
2251 JUMPBY(oparg);
2252 FAST_DISPATCH();
2253 }
2254 else {
2255 PyObject *val = POP();
2256 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002257 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002258 goto exception_unwind;
2259 }
2260 }
2261
Zackery Spytzce6a0702019-08-25 03:44:09 -06002262 case TARGET(LOAD_ASSERTION_ERROR): {
2263 PyObject *value = PyExc_AssertionError;
2264 Py_INCREF(value);
2265 PUSH(value);
2266 FAST_DISPATCH();
2267 }
2268
Benjamin Petersonddd19492018-09-16 22:38:02 -07002269 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002270 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002271
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002272 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002273 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002274 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002275 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002276 if (!_PyErr_Occurred(tstate)) {
2277 _PyErr_SetString(tstate, PyExc_NameError,
2278 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002279 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002280 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002281 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002282 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002283 }
2284 else {
2285 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2286 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002287 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002288 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2289 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002290 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2291 _PyErr_SetString(tstate, PyExc_NameError,
2292 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002293 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002294 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002295 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002296 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002297 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002298 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002299
Benjamin Petersonddd19492018-09-16 22:38:02 -07002300 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002301 PyObject *name = GETITEM(names, oparg);
2302 PyObject *v = POP();
2303 PyObject *ns = f->f_locals;
2304 int err;
2305 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002306 _PyErr_Format(tstate, PyExc_SystemError,
2307 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002308 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002309 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002310 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002311 if (PyDict_CheckExact(ns))
2312 err = PyDict_SetItem(ns, name, v);
2313 else
2314 err = PyObject_SetItem(ns, name, v);
2315 Py_DECREF(v);
2316 if (err != 0)
2317 goto error;
2318 DISPATCH();
2319 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002320
Benjamin Petersonddd19492018-09-16 22:38:02 -07002321 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002322 PyObject *name = GETITEM(names, oparg);
2323 PyObject *ns = f->f_locals;
2324 int err;
2325 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002326 _PyErr_Format(tstate, PyExc_SystemError,
2327 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002328 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002329 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002330 err = PyObject_DelItem(ns, name);
2331 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002332 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002333 NAME_ERROR_MSG,
2334 name);
2335 goto error;
2336 }
2337 DISPATCH();
2338 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002339
Benjamin Petersonddd19492018-09-16 22:38:02 -07002340 case TARGET(UNPACK_SEQUENCE): {
2341 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002342 PyObject *seq = POP(), *item, **items;
2343 if (PyTuple_CheckExact(seq) &&
2344 PyTuple_GET_SIZE(seq) == oparg) {
2345 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002346 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002347 item = items[oparg];
2348 Py_INCREF(item);
2349 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002350 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002351 } else if (PyList_CheckExact(seq) &&
2352 PyList_GET_SIZE(seq) == oparg) {
2353 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002354 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002355 item = items[oparg];
2356 Py_INCREF(item);
2357 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002358 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002359 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002360 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002361 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002362 } else {
2363 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002364 Py_DECREF(seq);
2365 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002367 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002368 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002369 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002370
Benjamin Petersonddd19492018-09-16 22:38:02 -07002371 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002372 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2373 PyObject *seq = POP();
2374
Victor Stinner438a12d2019-05-24 17:01:38 +02002375 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002376 stack_pointer + totalargs)) {
2377 stack_pointer += totalargs;
2378 } else {
2379 Py_DECREF(seq);
2380 goto error;
2381 }
2382 Py_DECREF(seq);
2383 DISPATCH();
2384 }
2385
Benjamin Petersonddd19492018-09-16 22:38:02 -07002386 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002387 PyObject *name = GETITEM(names, oparg);
2388 PyObject *owner = TOP();
2389 PyObject *v = SECOND();
2390 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002391 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002392 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002393 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002394 Py_DECREF(owner);
2395 if (err != 0)
2396 goto error;
2397 DISPATCH();
2398 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002399
Benjamin Petersonddd19492018-09-16 22:38:02 -07002400 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002401 PyObject *name = GETITEM(names, oparg);
2402 PyObject *owner = POP();
2403 int err;
2404 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2405 Py_DECREF(owner);
2406 if (err != 0)
2407 goto error;
2408 DISPATCH();
2409 }
2410
Benjamin Petersonddd19492018-09-16 22:38:02 -07002411 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002412 PyObject *name = GETITEM(names, oparg);
2413 PyObject *v = POP();
2414 int err;
2415 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002416 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002417 if (err != 0)
2418 goto error;
2419 DISPATCH();
2420 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002421
Benjamin Petersonddd19492018-09-16 22:38:02 -07002422 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002423 PyObject *name = GETITEM(names, oparg);
2424 int err;
2425 err = PyDict_DelItem(f->f_globals, name);
2426 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002427 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2428 format_exc_check_arg(tstate, PyExc_NameError,
2429 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002430 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002431 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002432 }
2433 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002434 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002435
Benjamin Petersonddd19492018-09-16 22:38:02 -07002436 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002437 PyObject *name = GETITEM(names, oparg);
2438 PyObject *locals = f->f_locals;
2439 PyObject *v;
2440 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002441 _PyErr_Format(tstate, PyExc_SystemError,
2442 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002443 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002444 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002445 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002446 v = PyDict_GetItemWithError(locals, name);
2447 if (v != NULL) {
2448 Py_INCREF(v);
2449 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002450 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002451 goto error;
2452 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002453 }
2454 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002455 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002456 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002457 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002458 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002459 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002460 }
2461 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002462 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002463 v = PyDict_GetItemWithError(f->f_globals, name);
2464 if (v != NULL) {
2465 Py_INCREF(v);
2466 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002467 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002468 goto error;
2469 }
2470 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002471 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002472 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002473 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002474 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002475 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002476 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002477 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002478 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002479 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002480 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002481 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002482 }
2483 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002484 v = PyObject_GetItem(f->f_builtins, name);
2485 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002486 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002487 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002488 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002489 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002490 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002491 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002492 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002493 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002494 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002495 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002496 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002497 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002498 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002499
Benjamin Petersonddd19492018-09-16 22:38:02 -07002500 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002501 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002502 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002503 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002504 && PyDict_CheckExact(f->f_builtins))
2505 {
Inada Naoki91234a12019-06-03 21:30:58 +09002506 OPCACHE_CHECK();
2507 if (co_opcache != NULL && co_opcache->optimized > 0) {
2508 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2509
2510 if (lg->globals_ver ==
2511 ((PyDictObject *)f->f_globals)->ma_version_tag
2512 && lg->builtins_ver ==
2513 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2514 {
2515 PyObject *ptr = lg->ptr;
2516 OPCACHE_STAT_GLOBAL_HIT();
2517 assert(ptr != NULL);
2518 Py_INCREF(ptr);
2519 PUSH(ptr);
2520 DISPATCH();
2521 }
2522 }
2523
2524 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002525 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002526 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002527 name);
2528 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002529 if (!_PyErr_OCCURRED()) {
2530 /* _PyDict_LoadGlobal() returns NULL without raising
2531 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002532 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002533 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002534 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002535 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002536 }
Inada Naoki91234a12019-06-03 21:30:58 +09002537
2538 if (co_opcache != NULL) {
2539 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2540
2541 if (co_opcache->optimized == 0) {
2542 /* Wasn't optimized before. */
2543 OPCACHE_STAT_GLOBAL_OPT();
2544 } else {
2545 OPCACHE_STAT_GLOBAL_MISS();
2546 }
2547
2548 co_opcache->optimized = 1;
2549 lg->globals_ver =
2550 ((PyDictObject *)f->f_globals)->ma_version_tag;
2551 lg->builtins_ver =
2552 ((PyDictObject *)f->f_builtins)->ma_version_tag;
2553 lg->ptr = v; /* borrowed */
2554 }
2555
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002556 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002557 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002558 else {
2559 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002560
2561 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09002562 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002563 v = PyObject_GetItem(f->f_globals, name);
2564 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002565 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002566 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002567 }
2568 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002569
Victor Stinnerb4efc962015-11-20 09:24:02 +01002570 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002571 v = PyObject_GetItem(f->f_builtins, name);
2572 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002573 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002574 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002575 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002576 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002577 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002578 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002579 }
2580 }
2581 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002582 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002583 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002584 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002585
Benjamin Petersonddd19492018-09-16 22:38:02 -07002586 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002587 PyObject *v = GETLOCAL(oparg);
2588 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002589 SETLOCAL(oparg, NULL);
2590 DISPATCH();
2591 }
2592 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002593 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002594 UNBOUNDLOCAL_ERROR_MSG,
2595 PyTuple_GetItem(co->co_varnames, oparg)
2596 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002597 goto error;
2598 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002599
Benjamin Petersonddd19492018-09-16 22:38:02 -07002600 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002601 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002602 PyObject *oldobj = PyCell_GET(cell);
2603 if (oldobj != NULL) {
2604 PyCell_SET(cell, NULL);
2605 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002606 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002607 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002608 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002609 goto error;
2610 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002611
Benjamin Petersonddd19492018-09-16 22:38:02 -07002612 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002613 PyObject *cell = freevars[oparg];
2614 Py_INCREF(cell);
2615 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002616 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002617 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002618
Benjamin Petersonddd19492018-09-16 22:38:02 -07002619 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002620 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002621 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002622 assert(locals);
2623 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2624 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2625 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2626 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2627 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002628 value = PyDict_GetItemWithError(locals, name);
2629 if (value != NULL) {
2630 Py_INCREF(value);
2631 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002632 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002633 goto error;
2634 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002635 }
2636 else {
2637 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002638 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002639 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002640 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002641 }
2642 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002643 }
2644 }
2645 if (!value) {
2646 PyObject *cell = freevars[oparg];
2647 value = PyCell_GET(cell);
2648 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002649 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002650 goto error;
2651 }
2652 Py_INCREF(value);
2653 }
2654 PUSH(value);
2655 DISPATCH();
2656 }
2657
Benjamin Petersonddd19492018-09-16 22:38:02 -07002658 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002659 PyObject *cell = freevars[oparg];
2660 PyObject *value = PyCell_GET(cell);
2661 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002662 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002663 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002664 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002665 Py_INCREF(value);
2666 PUSH(value);
2667 DISPATCH();
2668 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002669
Benjamin Petersonddd19492018-09-16 22:38:02 -07002670 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002671 PyObject *v = POP();
2672 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002673 PyObject *oldobj = PyCell_GET(cell);
2674 PyCell_SET(cell, v);
2675 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002676 DISPATCH();
2677 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002678
Benjamin Petersonddd19492018-09-16 22:38:02 -07002679 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002680 PyObject *str;
2681 PyObject *empty = PyUnicode_New(0, 0);
2682 if (empty == NULL) {
2683 goto error;
2684 }
2685 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2686 Py_DECREF(empty);
2687 if (str == NULL)
2688 goto error;
2689 while (--oparg >= 0) {
2690 PyObject *item = POP();
2691 Py_DECREF(item);
2692 }
2693 PUSH(str);
2694 DISPATCH();
2695 }
2696
Benjamin Petersonddd19492018-09-16 22:38:02 -07002697 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002698 PyObject *tup = PyTuple_New(oparg);
2699 if (tup == NULL)
2700 goto error;
2701 while (--oparg >= 0) {
2702 PyObject *item = POP();
2703 PyTuple_SET_ITEM(tup, oparg, item);
2704 }
2705 PUSH(tup);
2706 DISPATCH();
2707 }
2708
Benjamin Petersonddd19492018-09-16 22:38:02 -07002709 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002710 PyObject *list = PyList_New(oparg);
2711 if (list == NULL)
2712 goto error;
2713 while (--oparg >= 0) {
2714 PyObject *item = POP();
2715 PyList_SET_ITEM(list, oparg, item);
2716 }
2717 PUSH(list);
2718 DISPATCH();
2719 }
2720
Mark Shannon13bc1392020-01-23 09:25:17 +00002721 case TARGET(LIST_TO_TUPLE): {
2722 PyObject *list = POP();
2723 PyObject *tuple = PyList_AsTuple(list);
2724 Py_DECREF(list);
2725 if (tuple == NULL) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002726 goto error;
Mark Shannon13bc1392020-01-23 09:25:17 +00002727 }
2728 PUSH(tuple);
2729 DISPATCH();
2730 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002731
Mark Shannon13bc1392020-01-23 09:25:17 +00002732 case TARGET(LIST_EXTEND): {
2733 PyObject *iterable = POP();
2734 PyObject *list = PEEK(oparg);
2735 PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
2736 if (none_val == NULL) {
2737 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01002738 (Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable)))
Mark Shannon13bc1392020-01-23 09:25:17 +00002739 {
Victor Stinner61f4db82020-01-28 03:37:45 +01002740 _PyErr_Clear(tstate);
Mark Shannon13bc1392020-01-23 09:25:17 +00002741 _PyErr_Format(tstate, PyExc_TypeError,
2742 "Value after * must be an iterable, not %.200s",
2743 Py_TYPE(iterable)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002744 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002745 Py_DECREF(iterable);
2746 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002747 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002748 Py_DECREF(none_val);
2749 Py_DECREF(iterable);
2750 DISPATCH();
2751 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002752
Mark Shannon13bc1392020-01-23 09:25:17 +00002753 case TARGET(SET_UPDATE): {
2754 PyObject *iterable = POP();
2755 PyObject *set = PEEK(oparg);
2756 int err = _PySet_Update(set, iterable);
2757 Py_DECREF(iterable);
2758 if (err < 0) {
2759 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002760 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002761 DISPATCH();
2762 }
2763
Benjamin Petersonddd19492018-09-16 22:38:02 -07002764 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002765 PyObject *set = PySet_New(NULL);
2766 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002767 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002768 if (set == NULL)
2769 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002770 for (i = oparg; i > 0; i--) {
2771 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002772 if (err == 0)
2773 err = PySet_Add(set, item);
2774 Py_DECREF(item);
2775 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002776 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002777 if (err != 0) {
2778 Py_DECREF(set);
2779 goto error;
2780 }
2781 PUSH(set);
2782 DISPATCH();
2783 }
2784
Benjamin Petersonddd19492018-09-16 22:38:02 -07002785 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002786 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002787 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2788 if (map == NULL)
2789 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002790 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002791 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002792 PyObject *key = PEEK(2*i);
2793 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002794 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002795 if (err != 0) {
2796 Py_DECREF(map);
2797 goto error;
2798 }
2799 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002800
2801 while (oparg--) {
2802 Py_DECREF(POP());
2803 Py_DECREF(POP());
2804 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002805 PUSH(map);
2806 DISPATCH();
2807 }
2808
Benjamin Petersonddd19492018-09-16 22:38:02 -07002809 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002810 _Py_IDENTIFIER(__annotations__);
2811 int err;
2812 PyObject *ann_dict;
2813 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002814 _PyErr_Format(tstate, PyExc_SystemError,
2815 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002816 goto error;
2817 }
2818 /* check if __annotations__ in locals()... */
2819 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002820 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002821 &PyId___annotations__);
2822 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002823 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002824 goto error;
2825 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002826 /* ...if not, create a new one */
2827 ann_dict = PyDict_New();
2828 if (ann_dict == NULL) {
2829 goto error;
2830 }
2831 err = _PyDict_SetItemId(f->f_locals,
2832 &PyId___annotations__, ann_dict);
2833 Py_DECREF(ann_dict);
2834 if (err != 0) {
2835 goto error;
2836 }
2837 }
2838 }
2839 else {
2840 /* do the same if locals() is not a dict */
2841 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2842 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002843 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002844 }
2845 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2846 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002847 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002848 goto error;
2849 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002850 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002851 ann_dict = PyDict_New();
2852 if (ann_dict == NULL) {
2853 goto error;
2854 }
2855 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2856 Py_DECREF(ann_dict);
2857 if (err != 0) {
2858 goto error;
2859 }
2860 }
2861 else {
2862 Py_DECREF(ann_dict);
2863 }
2864 }
2865 DISPATCH();
2866 }
2867
Benjamin Petersonddd19492018-09-16 22:38:02 -07002868 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002869 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002870 PyObject *map;
2871 PyObject *keys = TOP();
2872 if (!PyTuple_CheckExact(keys) ||
2873 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002874 _PyErr_SetString(tstate, PyExc_SystemError,
2875 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002876 goto error;
2877 }
2878 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2879 if (map == NULL) {
2880 goto error;
2881 }
2882 for (i = oparg; i > 0; i--) {
2883 int err;
2884 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2885 PyObject *value = PEEK(i + 1);
2886 err = PyDict_SetItem(map, key, value);
2887 if (err != 0) {
2888 Py_DECREF(map);
2889 goto error;
2890 }
2891 }
2892
2893 Py_DECREF(POP());
2894 while (oparg--) {
2895 Py_DECREF(POP());
2896 }
2897 PUSH(map);
2898 DISPATCH();
2899 }
2900
Mark Shannon8a4cd702020-01-27 09:57:45 +00002901 case TARGET(DICT_UPDATE): {
2902 PyObject *update = POP();
2903 PyObject *dict = PEEK(oparg);
2904 if (PyDict_Update(dict, update) < 0) {
2905 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
2906 _PyErr_Format(tstate, PyExc_TypeError,
2907 "'%.200s' object is not a mapping",
Victor Stinnera102ed72020-02-07 02:24:48 +01002908 Py_TYPE(update)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002909 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002910 Py_DECREF(update);
2911 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002912 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002913 Py_DECREF(update);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002914 DISPATCH();
2915 }
2916
Mark Shannon8a4cd702020-01-27 09:57:45 +00002917 case TARGET(DICT_MERGE): {
2918 PyObject *update = POP();
2919 PyObject *dict = PEEK(oparg);
2920
2921 if (_PyDict_MergeEx(dict, update, 2) < 0) {
2922 format_kwargs_error(tstate, PEEK(2 + oparg), update);
2923 Py_DECREF(update);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002924 goto error;
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002925 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00002926 Py_DECREF(update);
Brandt Bucherf185a732019-09-28 17:12:49 -07002927 PREDICT(CALL_FUNCTION_EX);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002928 DISPATCH();
2929 }
2930
Benjamin Petersonddd19492018-09-16 22:38:02 -07002931 case TARGET(MAP_ADD): {
Jörn Heisslerc8a35412019-06-22 16:40:55 +02002932 PyObject *value = TOP();
2933 PyObject *key = SECOND();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002934 PyObject *map;
2935 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002936 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002937 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002938 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002939 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002940 Py_DECREF(value);
2941 Py_DECREF(key);
2942 if (err != 0)
2943 goto error;
2944 PREDICT(JUMP_ABSOLUTE);
2945 DISPATCH();
2946 }
2947
Benjamin Petersonddd19492018-09-16 22:38:02 -07002948 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002949 PyObject *name = GETITEM(names, oparg);
2950 PyObject *owner = TOP();
2951 PyObject *res = PyObject_GetAttr(owner, name);
2952 Py_DECREF(owner);
2953 SET_TOP(res);
2954 if (res == NULL)
2955 goto error;
2956 DISPATCH();
2957 }
2958
Benjamin Petersonddd19492018-09-16 22:38:02 -07002959 case TARGET(COMPARE_OP): {
Mark Shannon9af0e472020-01-14 10:12:45 +00002960 assert(oparg <= Py_GE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002961 PyObject *right = POP();
2962 PyObject *left = TOP();
Mark Shannon9af0e472020-01-14 10:12:45 +00002963 PyObject *res = PyObject_RichCompare(left, right, oparg);
2964 SET_TOP(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002965 Py_DECREF(left);
2966 Py_DECREF(right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002967 if (res == NULL)
2968 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002969 PREDICT(POP_JUMP_IF_FALSE);
2970 PREDICT(POP_JUMP_IF_TRUE);
2971 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002972 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002973
Mark Shannon9af0e472020-01-14 10:12:45 +00002974 case TARGET(IS_OP): {
2975 PyObject *right = POP();
2976 PyObject *left = TOP();
2977 int res = (left == right)^oparg;
2978 PyObject *b = res ? Py_True : Py_False;
2979 Py_INCREF(b);
2980 SET_TOP(b);
2981 Py_DECREF(left);
2982 Py_DECREF(right);
2983 PREDICT(POP_JUMP_IF_FALSE);
2984 PREDICT(POP_JUMP_IF_TRUE);
2985 FAST_DISPATCH();
2986 }
2987
2988 case TARGET(CONTAINS_OP): {
2989 PyObject *right = POP();
2990 PyObject *left = POP();
2991 int res = PySequence_Contains(right, left);
2992 Py_DECREF(left);
2993 Py_DECREF(right);
2994 if (res < 0) {
2995 goto error;
2996 }
2997 PyObject *b = (res^oparg) ? Py_True : Py_False;
2998 Py_INCREF(b);
2999 PUSH(b);
3000 PREDICT(POP_JUMP_IF_FALSE);
3001 PREDICT(POP_JUMP_IF_TRUE);
3002 FAST_DISPATCH();
3003 }
3004
3005#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
3006 "BaseException is not allowed"
3007
3008 case TARGET(JUMP_IF_NOT_EXC_MATCH): {
3009 PyObject *right = POP();
3010 PyObject *left = POP();
3011 if (PyTuple_Check(right)) {
3012 Py_ssize_t i, length;
3013 length = PyTuple_GET_SIZE(right);
3014 for (i = 0; i < length; i++) {
3015 PyObject *exc = PyTuple_GET_ITEM(right, i);
3016 if (!PyExceptionClass_Check(exc)) {
3017 _PyErr_SetString(tstate, PyExc_TypeError,
3018 CANNOT_CATCH_MSG);
3019 Py_DECREF(left);
3020 Py_DECREF(right);
3021 goto error;
3022 }
3023 }
3024 }
3025 else {
3026 if (!PyExceptionClass_Check(right)) {
3027 _PyErr_SetString(tstate, PyExc_TypeError,
3028 CANNOT_CATCH_MSG);
3029 Py_DECREF(left);
3030 Py_DECREF(right);
3031 goto error;
3032 }
3033 }
3034 int res = PyErr_GivenExceptionMatches(left, right);
3035 Py_DECREF(left);
3036 Py_DECREF(right);
3037 if (res > 0) {
3038 /* Exception matches -- Do nothing */;
3039 }
3040 else if (res == 0) {
3041 JUMPTO(oparg);
3042 }
3043 else {
3044 goto error;
3045 }
3046 DISPATCH();
3047 }
3048
Benjamin Petersonddd19492018-09-16 22:38:02 -07003049 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003050 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003051 PyObject *fromlist = POP();
3052 PyObject *level = TOP();
3053 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003054 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003055 Py_DECREF(level);
3056 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003057 SET_TOP(res);
3058 if (res == NULL)
3059 goto error;
3060 DISPATCH();
3061 }
3062
Benjamin Petersonddd19492018-09-16 22:38:02 -07003063 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003064 PyObject *from = POP(), *locals;
3065 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003066 if (PyFrame_FastToLocalsWithError(f) < 0) {
3067 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01003068 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003069 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01003070
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003071 locals = f->f_locals;
3072 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003073 _PyErr_SetString(tstate, PyExc_SystemError,
3074 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003075 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003076 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003077 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003078 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003079 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003080 Py_DECREF(from);
3081 if (err != 0)
3082 goto error;
3083 DISPATCH();
3084 }
Guido van Rossum25831651993-05-19 14:50:45 +00003085
Benjamin Petersonddd19492018-09-16 22:38:02 -07003086 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003087 PyObject *name = GETITEM(names, oparg);
3088 PyObject *from = TOP();
3089 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003090 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003091 PUSH(res);
3092 if (res == NULL)
3093 goto error;
3094 DISPATCH();
3095 }
Thomas Wouters52152252000-08-17 22:55:00 +00003096
Benjamin Petersonddd19492018-09-16 22:38:02 -07003097 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003098 JUMPBY(oparg);
3099 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003100 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003101
Benjamin Petersonddd19492018-09-16 22:38:02 -07003102 case TARGET(POP_JUMP_IF_FALSE): {
3103 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003104 PyObject *cond = POP();
3105 int err;
3106 if (cond == Py_True) {
3107 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003108 FAST_DISPATCH();
3109 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003110 if (cond == Py_False) {
3111 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003112 JUMPTO(oparg);
3113 FAST_DISPATCH();
3114 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003115 err = PyObject_IsTrue(cond);
3116 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003117 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003118 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003119 else if (err == 0)
3120 JUMPTO(oparg);
3121 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003122 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003123 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003124 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003125
Benjamin Petersonddd19492018-09-16 22:38:02 -07003126 case TARGET(POP_JUMP_IF_TRUE): {
3127 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003128 PyObject *cond = POP();
3129 int err;
3130 if (cond == Py_False) {
3131 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003132 FAST_DISPATCH();
3133 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003134 if (cond == Py_True) {
3135 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003136 JUMPTO(oparg);
3137 FAST_DISPATCH();
3138 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003139 err = PyObject_IsTrue(cond);
3140 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003141 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003142 JUMPTO(oparg);
3143 }
3144 else if (err == 0)
3145 ;
3146 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003147 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003148 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003149 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003150
Benjamin Petersonddd19492018-09-16 22:38:02 -07003151 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003152 PyObject *cond = TOP();
3153 int err;
3154 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003155 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003156 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003157 FAST_DISPATCH();
3158 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003159 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003160 JUMPTO(oparg);
3161 FAST_DISPATCH();
3162 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003163 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003164 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003165 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003166 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003167 }
3168 else if (err == 0)
3169 JUMPTO(oparg);
3170 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003171 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003172 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003173 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003174
Benjamin Petersonddd19492018-09-16 22:38:02 -07003175 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003176 PyObject *cond = TOP();
3177 int err;
3178 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003179 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003180 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003181 FAST_DISPATCH();
3182 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003183 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003184 JUMPTO(oparg);
3185 FAST_DISPATCH();
3186 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003187 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003188 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003189 JUMPTO(oparg);
3190 }
3191 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003192 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003193 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003194 }
3195 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003196 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003197 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003198 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003199
Benjamin Petersonddd19492018-09-16 22:38:02 -07003200 case TARGET(JUMP_ABSOLUTE): {
3201 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003202 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003203#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003204 /* Enabling this path speeds-up all while and for-loops by bypassing
3205 the per-loop checks for signals. By default, this should be turned-off
3206 because it prevents detection of a control-break in tight loops like
3207 "while 1: pass". Compile with this option turned-on when you need
3208 the speed-up and do not need break checking inside tight loops (ones
3209 that contain only instructions ending with FAST_DISPATCH).
3210 */
3211 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003212#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003213 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003214#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003215 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003216
Benjamin Petersonddd19492018-09-16 22:38:02 -07003217 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003218 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003219 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003220 PyObject *iter = PyObject_GetIter(iterable);
3221 Py_DECREF(iterable);
3222 SET_TOP(iter);
3223 if (iter == NULL)
3224 goto error;
3225 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003226 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003227 DISPATCH();
3228 }
3229
Benjamin Petersonddd19492018-09-16 22:38:02 -07003230 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04003231 /* before: [obj]; after [getiter(obj)] */
3232 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003233 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003234 if (PyCoro_CheckExact(iterable)) {
3235 /* `iterable` is a coroutine */
3236 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3237 /* and it is used in a 'yield from' expression of a
3238 regular generator. */
3239 Py_DECREF(iterable);
3240 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003241 _PyErr_SetString(tstate, PyExc_TypeError,
3242 "cannot 'yield from' a coroutine object "
3243 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04003244 goto error;
3245 }
3246 }
3247 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003248 /* `iterable` is not a generator. */
3249 iter = PyObject_GetIter(iterable);
3250 Py_DECREF(iterable);
3251 SET_TOP(iter);
3252 if (iter == NULL)
3253 goto error;
3254 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003255 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003256 DISPATCH();
3257 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003258
Benjamin Petersonddd19492018-09-16 22:38:02 -07003259 case TARGET(FOR_ITER): {
3260 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003261 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003262 PyObject *iter = TOP();
Victor Stinnera102ed72020-02-07 02:24:48 +01003263 PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003264 if (next != NULL) {
3265 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003266 PREDICT(STORE_FAST);
3267 PREDICT(UNPACK_SEQUENCE);
3268 DISPATCH();
3269 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003270 if (_PyErr_Occurred(tstate)) {
3271 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003272 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003273 }
3274 else if (tstate->c_tracefunc != NULL) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003275 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Victor Stinner438a12d2019-05-24 17:01:38 +02003276 }
3277 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003278 }
3279 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003280 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003281 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003282 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003283 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003284 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003285 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003286
Benjamin Petersonddd19492018-09-16 22:38:02 -07003287 case TARGET(SETUP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003288 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003289 STACK_LEVEL());
3290 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003291 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003292
Benjamin Petersonddd19492018-09-16 22:38:02 -07003293 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003294 _Py_IDENTIFIER(__aenter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003295 _Py_IDENTIFIER(__aexit__);
Yury Selivanov75445082015-05-11 22:57:16 -04003296 PyObject *mgr = TOP();
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003297 PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04003298 PyObject *res;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003299 if (enter == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003300 goto error;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003301 }
3302 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__);
3303 if (exit == NULL) {
3304 Py_DECREF(enter);
3305 goto error;
3306 }
Yury Selivanov75445082015-05-11 22:57:16 -04003307 SET_TOP(exit);
Yury Selivanov75445082015-05-11 22:57:16 -04003308 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003309 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003310 Py_DECREF(enter);
3311 if (res == NULL)
3312 goto error;
3313 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003314 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003315 DISPATCH();
3316 }
3317
Benjamin Petersonddd19492018-09-16 22:38:02 -07003318 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003319 PyObject *res = POP();
3320 /* Setup the finally block before pushing the result
3321 of __aenter__ on the stack. */
3322 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3323 STACK_LEVEL());
3324 PUSH(res);
3325 DISPATCH();
3326 }
3327
Benjamin Petersonddd19492018-09-16 22:38:02 -07003328 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003329 _Py_IDENTIFIER(__enter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003330 _Py_IDENTIFIER(__exit__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003331 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003332 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003333 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003334 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08003335 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003336 }
3337 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003338 if (exit == NULL) {
3339 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003340 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003341 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003342 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003343 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003344 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003345 Py_DECREF(enter);
3346 if (res == NULL)
3347 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003348 /* Setup the finally block before pushing the result
3349 of __enter__ on the stack. */
3350 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3351 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003352
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003353 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003354 DISPATCH();
3355 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003356
Mark Shannonfee55262019-11-21 09:11:43 +00003357 case TARGET(WITH_EXCEPT_START): {
3358 /* At the top of the stack are 7 values:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003359 - (TOP, SECOND, THIRD) = exc_info()
Mark Shannonfee55262019-11-21 09:11:43 +00003360 - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER
3361 - SEVENTH: the context.__exit__ bound method
3362 We call SEVENTH(TOP, SECOND, THIRD).
3363 Then we push again the TOP exception and the __exit__
3364 return value.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003365 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003366 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003367 PyObject *exc, *val, *tb, *res;
3368
Victor Stinner842cfff2016-12-01 14:45:31 +01003369 exc = TOP();
Mark Shannonfee55262019-11-21 09:11:43 +00003370 val = SECOND();
3371 tb = THIRD();
3372 assert(exc != Py_None);
3373 assert(!PyLong_Check(exc));
3374 exit_func = PEEK(7);
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003375 PyObject *stack[4] = {NULL, exc, val, tb};
Petr Viktorinffd97532020-02-11 17:46:57 +01003376 res = PyObject_Vectorcall(exit_func, stack + 1,
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003377 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003378 if (res == NULL)
3379 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003380
Yury Selivanov75445082015-05-11 22:57:16 -04003381 PUSH(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003382 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003383 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003384
Benjamin Petersonddd19492018-09-16 22:38:02 -07003385 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10003386 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003387 PyObject *name = GETITEM(names, oparg);
3388 PyObject *obj = TOP();
3389 PyObject *meth = NULL;
3390
3391 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3392
Yury Selivanovf2392132016-12-13 19:03:51 -05003393 if (meth == NULL) {
3394 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003395 goto error;
3396 }
3397
3398 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003399 /* We can bypass temporary bound method object.
3400 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003401
INADA Naoki015bce62017-01-16 17:23:30 +09003402 meth | self | arg1 | ... | argN
3403 */
3404 SET_TOP(meth);
3405 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003406 }
3407 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003408 /* meth is not an unbound method (but a regular attr, or
3409 something was returned by a descriptor protocol). Set
3410 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003411 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003412
3413 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003414 */
INADA Naoki015bce62017-01-16 17:23:30 +09003415 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003416 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003417 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003418 }
3419 DISPATCH();
3420 }
3421
Benjamin Petersonddd19492018-09-16 22:38:02 -07003422 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003423 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003424 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003425
3426 sp = stack_pointer;
3427
INADA Naoki015bce62017-01-16 17:23:30 +09003428 meth = PEEK(oparg + 2);
3429 if (meth == NULL) {
3430 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3431 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003432
3433 Stack layout:
3434
INADA Naoki015bce62017-01-16 17:23:30 +09003435 ... | NULL | callable | arg1 | ... | argN
3436 ^- TOP()
3437 ^- (-oparg)
3438 ^- (-oparg-1)
3439 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003440
Ville Skyttä49b27342017-08-03 09:00:59 +03003441 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003442 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003443 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003444 res = call_function(tstate, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003445 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003446 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003447 }
3448 else {
3449 /* This is a method call. Stack layout:
3450
INADA Naoki015bce62017-01-16 17:23:30 +09003451 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003452 ^- TOP()
3453 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003454 ^- (-oparg-1)
3455 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003456
INADA Naoki015bce62017-01-16 17:23:30 +09003457 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003458 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003459 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003460 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003461 res = call_function(tstate, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003462 stack_pointer = sp;
3463 }
3464
3465 PUSH(res);
3466 if (res == NULL)
3467 goto error;
3468 DISPATCH();
3469 }
3470
Benjamin Petersonddd19492018-09-16 22:38:02 -07003471 case TARGET(CALL_FUNCTION): {
3472 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003473 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003474 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003475 res = call_function(tstate, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003476 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003477 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003478 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003479 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003480 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003481 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003482 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003483
Benjamin Petersonddd19492018-09-16 22:38:02 -07003484 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003485 PyObject **sp, *res, *names;
3486
3487 names = POP();
Jeroen Demeyer05677862019-08-16 12:41:27 +02003488 assert(PyTuple_Check(names));
3489 assert(PyTuple_GET_SIZE(names) <= oparg);
3490 /* We assume without checking that names contains only strings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003491 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003492 res = call_function(tstate, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003493 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003494 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003495 Py_DECREF(names);
3496
3497 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003498 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003499 }
3500 DISPATCH();
3501 }
3502
Benjamin Petersonddd19492018-09-16 22:38:02 -07003503 case TARGET(CALL_FUNCTION_EX): {
Brandt Bucherf185a732019-09-28 17:12:49 -07003504 PREDICTED(CALL_FUNCTION_EX);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003505 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003506 if (oparg & 0x01) {
3507 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003508 if (!PyDict_CheckExact(kwargs)) {
3509 PyObject *d = PyDict_New();
3510 if (d == NULL)
3511 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003512 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003513 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02003514 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003515 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003516 goto error;
3517 }
3518 Py_DECREF(kwargs);
3519 kwargs = d;
3520 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003521 assert(PyDict_CheckExact(kwargs));
3522 }
3523 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003524 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003525 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003526 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003527 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003528 goto error;
3529 }
3530 Py_SETREF(callargs, PySequence_Tuple(callargs));
3531 if (callargs == NULL) {
3532 goto error;
3533 }
3534 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003535 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003536
Victor Stinner09532fe2019-05-10 23:39:09 +02003537 result = do_call_core(tstate, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003538 Py_DECREF(func);
3539 Py_DECREF(callargs);
3540 Py_XDECREF(kwargs);
3541
3542 SET_TOP(result);
3543 if (result == NULL) {
3544 goto error;
3545 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003546 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003547 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003548
Benjamin Petersonddd19492018-09-16 22:38:02 -07003549 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003550 PyObject *qualname = POP();
3551 PyObject *codeobj = POP();
3552 PyFunctionObject *func = (PyFunctionObject *)
3553 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003554
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003555 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003556 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003557 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003558 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003559 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003560
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003561 if (oparg & 0x08) {
3562 assert(PyTuple_CheckExact(TOP()));
3563 func ->func_closure = POP();
3564 }
3565 if (oparg & 0x04) {
3566 assert(PyDict_CheckExact(TOP()));
3567 func->func_annotations = POP();
3568 }
3569 if (oparg & 0x02) {
3570 assert(PyDict_CheckExact(TOP()));
3571 func->func_kwdefaults = POP();
3572 }
3573 if (oparg & 0x01) {
3574 assert(PyTuple_CheckExact(TOP()));
3575 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003576 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003577
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003578 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003579 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003580 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003581
Benjamin Petersonddd19492018-09-16 22:38:02 -07003582 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003583 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003584 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003585 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003586 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003587 step = NULL;
3588 stop = POP();
3589 start = TOP();
3590 slice = PySlice_New(start, stop, step);
3591 Py_DECREF(start);
3592 Py_DECREF(stop);
3593 Py_XDECREF(step);
3594 SET_TOP(slice);
3595 if (slice == NULL)
3596 goto error;
3597 DISPATCH();
3598 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003599
Benjamin Petersonddd19492018-09-16 22:38:02 -07003600 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003601 /* Handles f-string value formatting. */
3602 PyObject *result;
3603 PyObject *fmt_spec;
3604 PyObject *value;
3605 PyObject *(*conv_fn)(PyObject *);
3606 int which_conversion = oparg & FVC_MASK;
3607 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3608
3609 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003610 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003611
3612 /* See if any conversion is specified. */
3613 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003614 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003615 case FVC_STR: conv_fn = PyObject_Str; break;
3616 case FVC_REPR: conv_fn = PyObject_Repr; break;
3617 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003618 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02003619 _PyErr_Format(tstate, PyExc_SystemError,
3620 "unexpected conversion flag %d",
3621 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003622 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003623 }
3624
3625 /* If there's a conversion function, call it and replace
3626 value with that result. Otherwise, just use value,
3627 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003628 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003629 result = conv_fn(value);
3630 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003631 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003632 Py_XDECREF(fmt_spec);
3633 goto error;
3634 }
3635 value = result;
3636 }
3637
3638 /* If value is a unicode object, and there's no fmt_spec,
3639 then we know the result of format(value) is value
3640 itself. In that case, skip calling format(). I plan to
3641 move this optimization in to PyObject_Format()
3642 itself. */
3643 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3644 /* Do nothing, just transfer ownership to result. */
3645 result = value;
3646 } else {
3647 /* Actually call format(). */
3648 result = PyObject_Format(value, fmt_spec);
3649 Py_DECREF(value);
3650 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003651 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003652 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003653 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003654 }
3655
Eric V. Smith135d5f42016-02-05 18:23:08 -05003656 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003657 DISPATCH();
3658 }
3659
Benjamin Petersonddd19492018-09-16 22:38:02 -07003660 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003661 int oldoparg = oparg;
3662 NEXTOPARG();
3663 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003664 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003665 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003666
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003667
Antoine Pitrou042b1282010-08-13 21:15:58 +00003668#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003669 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003670#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003671 default:
3672 fprintf(stderr,
3673 "XXX lineno: %d, opcode: %d\n",
3674 PyFrame_GetLineNumber(f),
3675 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02003676 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003677 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003679 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003680
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003681 /* This should never be reached. Every opcode should end with DISPATCH()
3682 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003683 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003684
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003685error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003686 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003687#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02003688 if (!_PyErr_Occurred(tstate)) {
3689 _PyErr_SetString(tstate, PyExc_SystemError,
3690 "error return without exception set");
3691 }
Victor Stinner365b6932013-07-12 00:11:58 +02003692#else
Victor Stinner438a12d2019-05-24 17:01:38 +02003693 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02003694#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003695
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003696 /* Log traceback info. */
3697 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003698
Benjamin Peterson51f46162013-01-23 08:38:47 -05003699 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003700 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3701 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003702
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003703exception_unwind:
3704 /* Unwind stacks if an exception occurred */
3705 while (f->f_iblock > 0) {
3706 /* Pop the current block. */
3707 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003709 if (b->b_type == EXCEPT_HANDLER) {
3710 UNWIND_EXCEPT_HANDLER(b);
3711 continue;
3712 }
3713 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003714 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003715 PyObject *exc, *val, *tb;
3716 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003717 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003718 /* Beware, this invalidates all b->b_* fields */
3719 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003720 PUSH(exc_info->exc_traceback);
3721 PUSH(exc_info->exc_value);
3722 if (exc_info->exc_type != NULL) {
3723 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003724 }
3725 else {
3726 Py_INCREF(Py_None);
3727 PUSH(Py_None);
3728 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003729 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003730 /* Make the raw exception data
3731 available to the handler,
3732 so a program can emulate the
3733 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02003734 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003735 if (tb != NULL)
3736 PyException_SetTraceback(val, tb);
3737 else
3738 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003739 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003740 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003741 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003742 exc_info->exc_value = val;
3743 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003744 if (tb == NULL)
3745 tb = Py_None;
3746 Py_INCREF(tb);
3747 PUSH(tb);
3748 PUSH(val);
3749 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003750 JUMPTO(handler);
Victor Stinnerdab84232020-03-17 18:56:44 +01003751 if (_Py_TracingPossible(ceval2)) {
Pablo Galindo4c53e632020-01-10 09:24:22 +00003752 int needs_new_execution_window = (f->f_lasti < instr_lb || f->f_lasti >= instr_ub);
3753 int needs_line_update = (f->f_lasti == instr_lb || f->f_lasti < instr_prev);
3754 /* Make sure that we trace line after exception if we are in a new execution
3755 * window or we don't need a line update and we are not in the first instruction
3756 * of the line. */
3757 if (needs_new_execution_window || (!needs_line_update && instr_lb > 0)) {
3758 instr_prev = INT_MAX;
3759 }
Mark Shannonfee55262019-11-21 09:11:43 +00003760 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003761 /* Resume normal execution */
3762 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003763 }
3764 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003765
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003766 /* End the loop as we still have an error */
3767 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003768 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003769
Pablo Galindof00828a2019-05-09 16:52:02 +01003770 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003771 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01003772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003773 /* Pop remaining stack entries. */
3774 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003775 PyObject *o = POP();
3776 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003777 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003778
Mark Shannone7c9f4a2020-01-13 12:51:26 +00003779exiting:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003780 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003781 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003782 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3783 tstate, f, PyTrace_RETURN, retval)) {
3784 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003785 }
3786 }
3787 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003788 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3789 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003790 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003791 }
3792 }
3793 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003794
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003795 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003796exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003797 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3798 dtrace_function_return(f);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01003799 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrou58720d62013-08-05 23:26:40 +02003800 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003801 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003802
Victor Stinner0b72b232020-03-12 23:18:39 +01003803 return _Py_CheckFunctionResult(tstate, NULL, retval, __func__);
Guido van Rossum374a9221991-04-04 10:40:29 +00003804}
3805
Benjamin Petersonb204a422011-06-05 22:04:07 -05003806static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003807format_missing(PyThreadState *tstate, const char *kind,
3808 PyCodeObject *co, PyObject *names)
Benjamin Petersone109c702011-06-24 09:37:26 -05003809{
3810 int err;
3811 Py_ssize_t len = PyList_GET_SIZE(names);
3812 PyObject *name_str, *comma, *tail, *tmp;
3813
3814 assert(PyList_CheckExact(names));
3815 assert(len >= 1);
3816 /* Deal with the joys of natural language. */
3817 switch (len) {
3818 case 1:
3819 name_str = PyList_GET_ITEM(names, 0);
3820 Py_INCREF(name_str);
3821 break;
3822 case 2:
3823 name_str = PyUnicode_FromFormat("%U and %U",
3824 PyList_GET_ITEM(names, len - 2),
3825 PyList_GET_ITEM(names, len - 1));
3826 break;
3827 default:
3828 tail = PyUnicode_FromFormat(", %U, and %U",
3829 PyList_GET_ITEM(names, len - 2),
3830 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003831 if (tail == NULL)
3832 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003833 /* Chop off the last two objects in the list. This shouldn't actually
3834 fail, but we can't be too careful. */
3835 err = PyList_SetSlice(names, len - 2, len, NULL);
3836 if (err == -1) {
3837 Py_DECREF(tail);
3838 return;
3839 }
3840 /* Stitch everything up into a nice comma-separated list. */
3841 comma = PyUnicode_FromString(", ");
3842 if (comma == NULL) {
3843 Py_DECREF(tail);
3844 return;
3845 }
3846 tmp = PyUnicode_Join(comma, names);
3847 Py_DECREF(comma);
3848 if (tmp == NULL) {
3849 Py_DECREF(tail);
3850 return;
3851 }
3852 name_str = PyUnicode_Concat(tmp, tail);
3853 Py_DECREF(tmp);
3854 Py_DECREF(tail);
3855 break;
3856 }
3857 if (name_str == NULL)
3858 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02003859 _PyErr_Format(tstate, PyExc_TypeError,
3860 "%U() missing %i required %s argument%s: %U",
3861 co->co_name,
3862 len,
3863 kind,
3864 len == 1 ? "" : "s",
3865 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05003866 Py_DECREF(name_str);
3867}
3868
3869static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003870missing_arguments(PyThreadState *tstate, PyCodeObject *co,
3871 Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003872 PyObject **fastlocals)
3873{
Victor Stinner74319ae2016-08-25 00:04:09 +02003874 Py_ssize_t i, j = 0;
3875 Py_ssize_t start, end;
3876 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003877 const char *kind = positional ? "positional" : "keyword-only";
3878 PyObject *missing_names;
3879
3880 /* Compute the names of the arguments that are missing. */
3881 missing_names = PyList_New(missing);
3882 if (missing_names == NULL)
3883 return;
3884 if (positional) {
3885 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01003886 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003887 }
3888 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003889 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003890 end = start + co->co_kwonlyargcount;
3891 }
3892 for (i = start; i < end; i++) {
3893 if (GETLOCAL(i) == NULL) {
3894 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3895 PyObject *name = PyObject_Repr(raw);
3896 if (name == NULL) {
3897 Py_DECREF(missing_names);
3898 return;
3899 }
3900 PyList_SET_ITEM(missing_names, j++, name);
3901 }
3902 }
3903 assert(j == missing);
Victor Stinner438a12d2019-05-24 17:01:38 +02003904 format_missing(tstate, kind, co, missing_names);
Benjamin Petersone109c702011-06-24 09:37:26 -05003905 Py_DECREF(missing_names);
3906}
3907
3908static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003909too_many_positional(PyThreadState *tstate, PyCodeObject *co,
3910 Py_ssize_t given, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003911 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003912{
3913 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003914 Py_ssize_t kwonly_given = 0;
3915 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003916 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003917 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003918
Benjamin Petersone109c702011-06-24 09:37:26 -05003919 assert((co->co_flags & CO_VARARGS) == 0);
3920 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01003921 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003922 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003923 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003924 }
3925 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003926 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01003927 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003928 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01003929 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003930 }
3931 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003932 plural = (co_argcount != 1);
3933 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003934 }
3935 if (sig == NULL)
3936 return;
3937 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003938 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3939 kwonly_sig = PyUnicode_FromFormat(format,
3940 given != 1 ? "s" : "",
3941 kwonly_given,
3942 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003943 if (kwonly_sig == NULL) {
3944 Py_DECREF(sig);
3945 return;
3946 }
3947 }
3948 else {
3949 /* This will not fail. */
3950 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003951 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003952 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003953 _PyErr_Format(tstate, PyExc_TypeError,
3954 "%U() takes %U positional argument%s but %zd%U %s given",
3955 co->co_name,
3956 sig,
3957 plural ? "s" : "",
3958 given,
3959 kwonly_sig,
3960 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003961 Py_DECREF(sig);
3962 Py_DECREF(kwonly_sig);
3963}
3964
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003965static int
Victor Stinner438a12d2019-05-24 17:01:38 +02003966positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
3967 Py_ssize_t kwcount, PyObject* const* kwnames)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003968{
3969 int posonly_conflicts = 0;
3970 PyObject* posonly_names = PyList_New(0);
3971
3972 for(int k=0; k < co->co_posonlyargcount; k++){
3973 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
3974
3975 for (int k2=0; k2<kwcount; k2++){
3976 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
3977 PyObject* kwname = kwnames[k2];
3978 if (kwname == posonly_name){
3979 if(PyList_Append(posonly_names, kwname) != 0) {
3980 goto fail;
3981 }
3982 posonly_conflicts++;
3983 continue;
3984 }
3985
3986 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
3987
3988 if ( cmp > 0) {
3989 if(PyList_Append(posonly_names, kwname) != 0) {
3990 goto fail;
3991 }
3992 posonly_conflicts++;
3993 } else if (cmp < 0) {
3994 goto fail;
3995 }
3996
3997 }
3998 }
3999 if (posonly_conflicts) {
4000 PyObject* comma = PyUnicode_FromString(", ");
4001 if (comma == NULL) {
4002 goto fail;
4003 }
4004 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
4005 Py_DECREF(comma);
4006 if (error_names == NULL) {
4007 goto fail;
4008 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004009 _PyErr_Format(tstate, PyExc_TypeError,
4010 "%U() got some positional-only arguments passed"
4011 " as keyword arguments: '%U'",
4012 co->co_name, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004013 Py_DECREF(error_names);
4014 goto fail;
4015 }
4016
4017 Py_DECREF(posonly_names);
4018 return 0;
4019
4020fail:
4021 Py_XDECREF(posonly_names);
4022 return 1;
4023
4024}
4025
Guido van Rossumc2e20742006-02-27 22:32:47 +00004026/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02004027 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00004028 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00004029
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004030PyObject *
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004031_PyEval_EvalCode(PyThreadState *tstate,
4032 PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004033 PyObject *const *args, Py_ssize_t argcount,
4034 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03004035 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004036 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02004037 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004038 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00004039{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004040 assert(is_tstate_valid(tstate));
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004041
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00004042 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004043 PyFrameObject *f;
4044 PyObject *retval = NULL;
4045 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004046 PyObject *x, *u;
Pablo Galindocd74e662019-06-01 18:08:04 +01004047 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004048 Py_ssize_t i, j, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02004049 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00004050
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004051 if (globals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004052 _PyErr_SetString(tstate, PyExc_SystemError,
4053 "PyEval_EvalCodeEx: NULL globals");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004054 return NULL;
4055 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004056
Victor Stinnerc7020012016-08-16 23:40:29 +02004057 /* Create the frame */
INADA Naoki5a625d02016-12-24 20:19:08 +09004058 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02004059 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004060 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02004061 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004062 fastlocals = f->f_localsplus;
4063 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00004064
Victor Stinnerc7020012016-08-16 23:40:29 +02004065 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004066 if (co->co_flags & CO_VARKEYWORDS) {
4067 kwdict = PyDict_New();
4068 if (kwdict == NULL)
4069 goto fail;
4070 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02004071 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004072 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02004073 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004074 SETLOCAL(i, kwdict);
4075 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004076 else {
4077 kwdict = NULL;
4078 }
4079
Pablo Galindocd74e662019-06-01 18:08:04 +01004080 /* Copy all positional arguments into local variables */
4081 if (argcount > co->co_argcount) {
4082 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02004083 }
4084 else {
4085 n = argcount;
4086 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004087 for (j = 0; j < n; j++) {
4088 x = args[j];
4089 Py_INCREF(x);
4090 SETLOCAL(j, x);
4091 }
4092
Victor Stinnerc7020012016-08-16 23:40:29 +02004093 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004094 if (co->co_flags & CO_VARARGS) {
Sergey Fedoseev234531b2019-02-25 21:59:12 +05004095 u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02004096 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004097 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004098 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004099 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004100 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004101
Serhiy Storchakab7281052016-09-12 00:52:40 +03004102 /* Handle keyword arguments passed as two strided arrays */
4103 kwcount *= kwstep;
4104 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004105 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03004106 PyObject *keyword = kwnames[i];
4107 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02004108 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004109
Benjamin Petersonb204a422011-06-05 22:04:07 -05004110 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004111 _PyErr_Format(tstate, PyExc_TypeError,
4112 "%U() keywords must be strings",
4113 co->co_name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004114 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004115 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004116
Benjamin Petersonb204a422011-06-05 22:04:07 -05004117 /* Speed hack: do raw pointer compares. As names are
4118 normally interned this should almost always hit. */
4119 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004120 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004121 PyObject *name = co_varnames[j];
4122 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004123 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004124 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004125 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004126
Benjamin Petersonb204a422011-06-05 22:04:07 -05004127 /* Slow fallback, just in case */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004128 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004129 PyObject *name = co_varnames[j];
4130 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
4131 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004132 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004133 }
4134 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004135 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004136 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004137 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004138
Victor Stinner231d1f32017-01-11 02:12:06 +01004139 assert(j >= total_args);
4140 if (kwdict == NULL) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004141
Victor Stinner438a12d2019-05-24 17:01:38 +02004142 if (co->co_posonlyargcount
4143 && positional_only_passed_as_keyword(tstate, co,
4144 kwcount, kwnames))
4145 {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004146 goto fail;
4147 }
4148
Victor Stinner438a12d2019-05-24 17:01:38 +02004149 _PyErr_Format(tstate, PyExc_TypeError,
4150 "%U() got an unexpected keyword argument '%S'",
4151 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004152 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004153 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004154
Christian Heimes0bd447f2013-07-20 14:48:10 +02004155 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4156 goto fail;
4157 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004158 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004159
Benjamin Petersonb204a422011-06-05 22:04:07 -05004160 kw_found:
4161 if (GETLOCAL(j) != NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004162 _PyErr_Format(tstate, PyExc_TypeError,
4163 "%U() got multiple values for argument '%S'",
4164 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004165 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004166 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004167 Py_INCREF(value);
4168 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004169 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004170
4171 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004172 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004173 too_many_positional(tstate, co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004174 goto fail;
4175 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004176
4177 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004178 if (argcount < co->co_argcount) {
4179 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004180 Py_ssize_t missing = 0;
4181 for (i = argcount; i < m; i++) {
4182 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004183 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004184 }
4185 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004186 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004187 missing_arguments(tstate, co, missing, defcount, fastlocals);
Benjamin Petersone109c702011-06-24 09:37:26 -05004188 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004189 }
4190 if (n > m)
4191 i = n - m;
4192 else
4193 i = 0;
4194 for (; i < defcount; i++) {
4195 if (GETLOCAL(m+i) == NULL) {
4196 PyObject *def = defs[i];
4197 Py_INCREF(def);
4198 SETLOCAL(m+i, def);
4199 }
4200 }
4201 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004202
4203 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004204 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004205 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004206 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004207 PyObject *name;
4208 if (GETLOCAL(i) != NULL)
4209 continue;
4210 name = PyTuple_GET_ITEM(co->co_varnames, i);
4211 if (kwdefs != NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004212 PyObject *def = PyDict_GetItemWithError(kwdefs, name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004213 if (def) {
4214 Py_INCREF(def);
4215 SETLOCAL(i, def);
4216 continue;
4217 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004218 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004219 goto fail;
4220 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004221 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004222 missing++;
4223 }
4224 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004225 missing_arguments(tstate, co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004226 goto fail;
4227 }
4228 }
4229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004230 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004231 vars into frame. */
4232 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004233 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004234 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004235 /* Possibly account for the cell variable being an argument. */
4236 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004237 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004238 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004239 /* Clear the local copy. */
4240 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004241 }
4242 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004243 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004244 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004245 if (c == NULL)
4246 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004247 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004248 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004249
4250 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004251 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4252 PyObject *o = PyTuple_GET_ITEM(closure, i);
4253 Py_INCREF(o);
4254 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004255 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004256
Yury Selivanoveb636452016-09-08 22:01:51 -07004257 /* Handle generator/coroutine/asynchronous generator */
4258 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004259 PyObject *gen;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004260 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004261
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004262 /* Don't need to keep the reference to f_back, it will be set
4263 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004264 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004266 /* Create a new generator that owns the ready to run frame
4267 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004268 if (is_coro) {
4269 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004270 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4271 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004272 } else {
4273 gen = PyGen_NewWithQualName(f, name, qualname);
4274 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004275 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004276 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004277 }
INADA Naoki9c157762016-12-26 18:52:46 +09004278
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004279 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004280
Yury Selivanov75445082015-05-11 22:57:16 -04004281 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004282 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004283
Victor Stinnerb9e68122019-11-14 12:20:46 +01004284 retval = _PyEval_EvalFrame(tstate, f, 0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004285
Thomas Woutersce272b62007-09-19 21:19:28 +00004286fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004288 /* decref'ing the frame can cause __del__ methods to get invoked,
4289 which can call back into Python. While we're done with the
4290 current Python frame (f), the associated C stack is still in use,
4291 so recursion_depth must be boosted for the duration.
4292 */
INADA Naoki5a625d02016-12-24 20:19:08 +09004293 if (Py_REFCNT(f) > 1) {
4294 Py_DECREF(f);
4295 _PyObject_GC_TRACK(f);
4296 }
4297 else {
4298 ++tstate->recursion_depth;
4299 Py_DECREF(f);
4300 --tstate->recursion_depth;
4301 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004302 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004303}
4304
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004305
4306PyObject *
4307_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
4308 PyObject *const *args, Py_ssize_t argcount,
4309 PyObject *const *kwnames, PyObject *const *kwargs,
4310 Py_ssize_t kwcount, int kwstep,
4311 PyObject *const *defs, Py_ssize_t defcount,
4312 PyObject *kwdefs, PyObject *closure,
4313 PyObject *name, PyObject *qualname)
4314{
4315 PyThreadState *tstate = _PyThreadState_GET();
4316 return _PyEval_EvalCode(tstate, _co, globals, locals,
4317 args, argcount,
4318 kwnames, kwargs,
4319 kwcount, kwstep,
4320 defs, defcount,
4321 kwdefs, closure,
4322 name, qualname);
4323}
4324
Victor Stinner40ee3012014-06-16 15:59:28 +02004325PyObject *
4326PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004327 PyObject *const *args, int argcount,
4328 PyObject *const *kws, int kwcount,
4329 PyObject *const *defs, int defcount,
4330 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004331{
4332 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004333 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004334 kws, kws != NULL ? kws + 1 : NULL,
4335 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004336 defs, defcount,
4337 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004338 NULL, NULL);
4339}
Tim Peters5ca576e2001-06-18 22:08:13 +00004340
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004341static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004342special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004343{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004344 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004345 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02004346 if (res == NULL && !_PyErr_Occurred(tstate)) {
4347 _PyErr_SetObject(tstate, PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004348 return NULL;
4349 }
4350 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004351}
4352
4353
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004354/* Logic for the raise statement (too complicated for inlining).
4355 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004356static int
Victor Stinner09532fe2019-05-10 23:39:09 +02004357do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004358{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004359 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004361 if (exc == NULL) {
4362 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01004363 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004364 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004365 type = exc_info->exc_type;
4366 value = exc_info->exc_value;
4367 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004368 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004369 _PyErr_SetString(tstate, PyExc_RuntimeError,
4370 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004371 return 0;
4372 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004373 Py_XINCREF(type);
4374 Py_XINCREF(value);
4375 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02004376 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004377 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004378 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004380 /* We support the following forms of raise:
4381 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004382 raise <instance>
4383 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004385 if (PyExceptionClass_Check(exc)) {
4386 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004387 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004388 if (value == NULL)
4389 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004390 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004391 _PyErr_Format(tstate, PyExc_TypeError,
4392 "calling %R should have returned an instance of "
4393 "BaseException, not %R",
4394 type, Py_TYPE(value));
4395 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004396 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004397 }
4398 else if (PyExceptionInstance_Check(exc)) {
4399 value = exc;
4400 type = PyExceptionInstance_Class(exc);
4401 Py_INCREF(type);
4402 }
4403 else {
4404 /* Not something you can raise. You get an exception
4405 anyway, just not what you specified :-) */
4406 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02004407 _PyErr_SetString(tstate, PyExc_TypeError,
4408 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004409 goto raise_error;
4410 }
Collin Winter828f04a2007-08-31 00:04:24 +00004411
Serhiy Storchakac0191582016-09-27 11:37:10 +03004412 assert(type != NULL);
4413 assert(value != NULL);
4414
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004415 if (cause) {
4416 PyObject *fixed_cause;
4417 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004418 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004419 if (fixed_cause == NULL)
4420 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004421 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004422 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004423 else if (PyExceptionInstance_Check(cause)) {
4424 fixed_cause = cause;
4425 }
4426 else if (cause == Py_None) {
4427 Py_DECREF(cause);
4428 fixed_cause = NULL;
4429 }
4430 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004431 _PyErr_SetString(tstate, PyExc_TypeError,
4432 "exception causes must derive from "
4433 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004434 goto raise_error;
4435 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004436 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004437 }
Collin Winter828f04a2007-08-31 00:04:24 +00004438
Victor Stinner438a12d2019-05-24 17:01:38 +02004439 _PyErr_SetObject(tstate, type, value);
Victor Stinner61f4db82020-01-28 03:37:45 +01004440 /* _PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004441 Py_DECREF(value);
4442 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004443 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004444
4445raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004446 Py_XDECREF(value);
4447 Py_XDECREF(type);
4448 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004449 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004450}
4451
Tim Petersd6d010b2001-06-21 02:49:55 +00004452/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004453 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004454
Guido van Rossum0368b722007-05-11 16:50:42 +00004455 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4456 with a variable target.
4457*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004458
Barry Warsawe42b18f1997-08-25 22:13:04 +00004459static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004460unpack_iterable(PyThreadState *tstate, PyObject *v,
4461 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004462{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004463 int i = 0, j = 0;
4464 Py_ssize_t ll = 0;
4465 PyObject *it; /* iter(v) */
4466 PyObject *w;
4467 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004469 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004471 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004472 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004473 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01004474 Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v))
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004475 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004476 _PyErr_Format(tstate, PyExc_TypeError,
4477 "cannot unpack non-iterable %.200s object",
Victor Stinnera102ed72020-02-07 02:24:48 +01004478 Py_TYPE(v)->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004479 }
4480 return 0;
4481 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004483 for (; i < argcnt; i++) {
4484 w = PyIter_Next(it);
4485 if (w == NULL) {
4486 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004487 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04004488 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004489 _PyErr_Format(tstate, PyExc_ValueError,
4490 "not enough values to unpack "
4491 "(expected %d, got %d)",
4492 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004493 }
4494 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004495 _PyErr_Format(tstate, PyExc_ValueError,
4496 "not enough values to unpack "
4497 "(expected at least %d, got %d)",
4498 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004499 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004500 }
4501 goto Error;
4502 }
4503 *--sp = w;
4504 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004506 if (argcntafter == -1) {
4507 /* We better have exhausted the iterator now. */
4508 w = PyIter_Next(it);
4509 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004510 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004511 goto Error;
4512 Py_DECREF(it);
4513 return 1;
4514 }
4515 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02004516 _PyErr_Format(tstate, PyExc_ValueError,
4517 "too many values to unpack (expected %d)",
4518 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004519 goto Error;
4520 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004522 l = PySequence_List(it);
4523 if (l == NULL)
4524 goto Error;
4525 *--sp = l;
4526 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004527
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004528 ll = PyList_GET_SIZE(l);
4529 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004530 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04004531 "not enough values to unpack (expected at least %d, got %zd)",
4532 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004533 goto Error;
4534 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004536 /* Pop the "after-variable" args off the list. */
4537 for (j = argcntafter; j > 0; j--, i++) {
4538 *--sp = PyList_GET_ITEM(l, ll - j);
4539 }
4540 /* Resize the list. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +01004541 Py_SET_SIZE(l, ll - argcntafter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004542 Py_DECREF(it);
4543 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004544
Tim Petersd6d010b2001-06-21 02:49:55 +00004545Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004546 for (; i > 0; i--, sp++)
4547 Py_DECREF(*sp);
4548 Py_XDECREF(it);
4549 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004550}
4551
4552
Guido van Rossum96a42c81992-01-12 02:29:51 +00004553#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004554static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004555prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004556{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004557 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02004558 if (PyObject_Print(v, stdout, 0) != 0) {
4559 /* Don't know what else to do */
4560 _PyErr_Clear(tstate);
4561 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004562 printf("\n");
4563 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004564}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004565#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004566
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004567static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004568call_exc_trace(Py_tracefunc func, PyObject *self,
4569 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004570{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004571 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004572 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004573 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004574 if (value == NULL) {
4575 value = Py_None;
4576 Py_INCREF(value);
4577 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004578 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01004579 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004580 arg = PyTuple_Pack(3, type, value, traceback);
4581 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004582 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004583 return;
4584 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004585 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004586 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02004587 if (err == 0) {
4588 _PyErr_Restore(tstate, type, value, orig_traceback);
4589 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004590 else {
4591 Py_XDECREF(type);
4592 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004593 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004594 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004595}
4596
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004597static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004598call_trace_protected(Py_tracefunc func, PyObject *obj,
4599 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004600 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004601{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004602 PyObject *type, *value, *traceback;
4603 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004604 _PyErr_Fetch(tstate, &type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004605 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004606 if (err == 0)
4607 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004608 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004609 return 0;
4610 }
4611 else {
4612 Py_XDECREF(type);
4613 Py_XDECREF(value);
4614 Py_XDECREF(traceback);
4615 return -1;
4616 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004617}
4618
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004619static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004620call_trace(Py_tracefunc func, PyObject *obj,
4621 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004622 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004623{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004624 int result;
4625 if (tstate->tracing)
4626 return 0;
4627 tstate->tracing++;
4628 tstate->use_tracing = 0;
4629 result = func(obj, frame, what, arg);
4630 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4631 || (tstate->c_profilefunc != NULL));
4632 tstate->tracing--;
4633 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004634}
4635
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004636PyObject *
4637_PyEval_CallTracing(PyObject *func, PyObject *args)
4638{
Victor Stinner50b48572018-11-01 01:51:40 +01004639 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004640 int save_tracing = tstate->tracing;
4641 int save_use_tracing = tstate->use_tracing;
4642 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004644 tstate->tracing = 0;
4645 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4646 || (tstate->c_profilefunc != NULL));
4647 result = PyObject_Call(func, args, NULL);
4648 tstate->tracing = save_tracing;
4649 tstate->use_tracing = save_use_tracing;
4650 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004651}
4652
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004653/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004654static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004655maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004656 PyThreadState *tstate, PyFrameObject *frame,
4657 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004658{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004659 int result = 0;
4660 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004661
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004662 /* If the last instruction executed isn't in the current
4663 instruction window, reset the window.
4664 */
4665 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4666 PyAddrPair bounds;
4667 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4668 &bounds);
4669 *instr_lb = bounds.ap_lower;
4670 *instr_ub = bounds.ap_upper;
4671 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004672 /* If the last instruction falls at the start of a line or if it
4673 represents a jump backwards, update the frame's line number and
4674 then call the trace function if we're tracing source lines.
4675 */
4676 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004677 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004678 if (frame->f_trace_lines) {
4679 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4680 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004681 }
George King20faa682017-10-18 17:44:22 -07004682 /* Always emit an opcode event if we're tracing all opcodes. */
4683 if (frame->f_trace_opcodes) {
4684 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4685 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004686 *instr_prev = frame->f_lasti;
4687 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004688}
4689
Victor Stinner309d7cc2020-03-13 16:39:12 +01004690int
4691_PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
4692{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004693 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01004694 /* The caller must hold the GIL */
4695 assert(PyGILState_Check());
4696
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004697 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01004698 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004699 PyThreadState *current_tstate = _PyThreadState_GET();
4700 if (_PySys_Audit(current_tstate, "sys.setprofile", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01004701 return -1;
4702 }
4703
4704 PyObject *profileobj = tstate->c_profileobj;
4705
4706 tstate->c_profilefunc = NULL;
4707 tstate->c_profileobj = NULL;
4708 /* Must make sure that tracing is not ignored if 'profileobj' is freed */
4709 tstate->use_tracing = tstate->c_tracefunc != NULL;
4710 Py_XDECREF(profileobj);
4711
4712 Py_XINCREF(arg);
4713 tstate->c_profileobj = arg;
4714 tstate->c_profilefunc = func;
4715
4716 /* Flag that tracing or profiling is turned on */
4717 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
4718 return 0;
4719}
4720
Fred Drake5755ce62001-06-27 19:19:46 +00004721void
4722PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004723{
Victor Stinner309d7cc2020-03-13 16:39:12 +01004724 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01004725 if (_PyEval_SetProfile(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004726 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01004727 _PyErr_WriteUnraisableMsg("in PyEval_SetProfile", NULL);
4728 }
Victor Stinner309d7cc2020-03-13 16:39:12 +01004729}
4730
4731int
4732_PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
4733{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004734 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01004735 /* The caller must hold the GIL */
4736 assert(PyGILState_Check());
4737
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004738 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01004739 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004740 PyThreadState *current_tstate = _PyThreadState_GET();
4741 if (_PySys_Audit(current_tstate, "sys.settrace", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01004742 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004743 }
4744
Victor Stinnerda2914d2020-03-20 09:29:08 +01004745 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinner309d7cc2020-03-13 16:39:12 +01004746 PyObject *traceobj = tstate->c_traceobj;
Victor Stinnerda2914d2020-03-20 09:29:08 +01004747 ceval2->tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
Victor Stinner309d7cc2020-03-13 16:39:12 +01004748
4749 tstate->c_tracefunc = NULL;
4750 tstate->c_traceobj = NULL;
4751 /* Must make sure that profiling is not ignored if 'traceobj' is freed */
4752 tstate->use_tracing = (tstate->c_profilefunc != NULL);
4753 Py_XDECREF(traceobj);
4754
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004755 Py_XINCREF(arg);
Victor Stinner309d7cc2020-03-13 16:39:12 +01004756 tstate->c_traceobj = arg;
4757 tstate->c_tracefunc = func;
4758
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004759 /* Flag that tracing or profiling is turned on */
Victor Stinner309d7cc2020-03-13 16:39:12 +01004760 tstate->use_tracing = ((func != NULL)
4761 || (tstate->c_profilefunc != NULL));
4762
4763 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +00004764}
4765
4766void
4767PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4768{
Victor Stinner309d7cc2020-03-13 16:39:12 +01004769 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01004770 if (_PyEval_SetTrace(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004771 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01004772 _PyErr_WriteUnraisableMsg("in PyEval_SetTrace", NULL);
4773 }
Fred Draked0838392001-06-16 21:02:31 +00004774}
4775
Victor Stinner309d7cc2020-03-13 16:39:12 +01004776
Yury Selivanov75445082015-05-11 22:57:16 -04004777void
Victor Stinner838f2642019-06-13 22:41:23 +02004778_PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004779{
4780 assert(new_depth >= 0);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004781 tstate->coroutine_origin_tracking_depth = new_depth;
4782}
4783
4784int
4785_PyEval_GetCoroutineOriginTrackingDepth(void)
4786{
Victor Stinner50b48572018-11-01 01:51:40 +01004787 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004788 return tstate->coroutine_origin_tracking_depth;
4789}
4790
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004791int
Yury Selivanoveb636452016-09-08 22:01:51 -07004792_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4793{
Victor Stinner50b48572018-11-01 01:51:40 +01004794 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004795
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004796 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_firstiter", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004797 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004798 }
4799
Yury Selivanoveb636452016-09-08 22:01:51 -07004800 Py_XINCREF(firstiter);
4801 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004802 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07004803}
4804
4805PyObject *
4806_PyEval_GetAsyncGenFirstiter(void)
4807{
Victor Stinner50b48572018-11-01 01:51:40 +01004808 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004809 return tstate->async_gen_firstiter;
4810}
4811
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004812int
Yury Selivanoveb636452016-09-08 22:01:51 -07004813_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4814{
Victor Stinner50b48572018-11-01 01:51:40 +01004815 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004816
Victor Stinner1c1e68c2020-03-27 15:11:45 +01004817 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_finalizer", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004818 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004819 }
4820
Yury Selivanoveb636452016-09-08 22:01:51 -07004821 Py_XINCREF(finalizer);
4822 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06004823 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07004824}
4825
4826PyObject *
4827_PyEval_GetAsyncGenFinalizer(void)
4828{
Victor Stinner50b48572018-11-01 01:51:40 +01004829 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004830 return tstate->async_gen_finalizer;
4831}
4832
Victor Stinner438a12d2019-05-24 17:01:38 +02004833PyFrameObject *
4834PyEval_GetFrame(void)
4835{
4836 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004837 return tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02004838}
4839
Guido van Rossumb209a111997-04-29 18:18:01 +00004840PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004841PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004842{
Victor Stinner438a12d2019-05-24 17:01:38 +02004843 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004844 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004845 if (current_frame == NULL)
Victor Stinner438a12d2019-05-24 17:01:38 +02004846 return tstate->interp->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004847 else
4848 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004849}
4850
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004851/* Convenience function to get a builtin from its name */
4852PyObject *
4853_PyEval_GetBuiltinId(_Py_Identifier *name)
4854{
Victor Stinner438a12d2019-05-24 17:01:38 +02004855 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004856 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4857 if (attr) {
4858 Py_INCREF(attr);
4859 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004860 else if (!_PyErr_Occurred(tstate)) {
4861 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004862 }
4863 return attr;
4864}
4865
Guido van Rossumb209a111997-04-29 18:18:01 +00004866PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004867PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004868{
Victor Stinner438a12d2019-05-24 17:01:38 +02004869 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004870 PyFrameObject *current_frame = tstate->frame;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004871 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004872 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004873 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004874 }
4875
Victor Stinner438a12d2019-05-24 17:01:38 +02004876 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01004877 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004878 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004879
4880 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004881 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004882}
4883
Guido van Rossumb209a111997-04-29 18:18:01 +00004884PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004885PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004886{
Victor Stinner438a12d2019-05-24 17:01:38 +02004887 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004888 PyFrameObject *current_frame = tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02004889 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004890 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004891 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004892
4893 assert(current_frame->f_globals != NULL);
4894 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004895}
4896
Guido van Rossum6135a871995-01-09 17:53:26 +00004897int
Tim Peters5ba58662001-07-16 02:29:45 +00004898PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004899{
Victor Stinner438a12d2019-05-24 17:01:38 +02004900 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01004901 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004902 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004903
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004904 if (current_frame != NULL) {
4905 const int codeflags = current_frame->f_code->co_flags;
4906 const int compilerflags = codeflags & PyCF_MASK;
4907 if (compilerflags) {
4908 result = 1;
4909 cf->cf_flags |= compilerflags;
4910 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004911#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004912 if (codeflags & CO_GENERATOR_ALLOWED) {
4913 result = 1;
4914 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4915 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004916#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004917 }
4918 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004919}
4920
Guido van Rossum3f5da241990-12-20 15:06:42 +00004921
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004922const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004923PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004924{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004925 if (PyMethod_Check(func))
4926 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4927 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004928 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004929 else if (PyCFunction_Check(func))
4930 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4931 else
Victor Stinnera102ed72020-02-07 02:24:48 +01004932 return Py_TYPE(func)->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004933}
4934
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004935const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004936PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004937{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004938 if (PyMethod_Check(func))
4939 return "()";
4940 else if (PyFunction_Check(func))
4941 return "()";
4942 else if (PyCFunction_Check(func))
4943 return "()";
4944 else
4945 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004946}
4947
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004948#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004949if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004950 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4951 tstate, tstate->frame, \
4952 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004953 x = NULL; \
4954 } \
4955 else { \
4956 x = call; \
4957 if (tstate->c_profilefunc != NULL) { \
4958 if (x == NULL) { \
4959 call_trace_protected(tstate->c_profilefunc, \
4960 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004961 tstate, tstate->frame, \
4962 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004963 /* XXX should pass (type, value, tb) */ \
4964 } else { \
4965 if (call_trace(tstate->c_profilefunc, \
4966 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004967 tstate, tstate->frame, \
4968 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004969 Py_DECREF(x); \
4970 x = NULL; \
4971 } \
4972 } \
4973 } \
4974 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004975} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004976 x = call; \
4977 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004978
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004979
4980static PyObject *
4981trace_call_function(PyThreadState *tstate,
4982 PyObject *func,
4983 PyObject **args, Py_ssize_t nargs,
4984 PyObject *kwnames)
4985{
4986 PyObject *x;
4987 if (PyCFunction_Check(func)) {
Petr Viktorinffd97532020-02-11 17:46:57 +01004988 C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004989 return x;
4990 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06004991 else if (Py_IS_TYPE(func, &PyMethodDescr_Type) && nargs > 0) {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004992 /* We need to create a temporary bound method as argument
4993 for profiling.
4994
4995 If nargs == 0, then this cannot work because we have no
4996 "self". In any case, the call itself would raise
4997 TypeError (foo needs an argument), so we just skip
4998 profiling. */
4999 PyObject *self = args[0];
5000 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5001 if (func == NULL) {
5002 return NULL;
5003 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005004 C_TRACE(x, PyObject_Vectorcall(func,
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02005005 args+1, nargs-1,
5006 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005007 Py_DECREF(func);
5008 return x;
5009 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005010 return PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005011}
5012
Victor Stinner415c5102017-01-11 00:54:57 +01005013/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
5014 to reduce the stack consumption. */
5015Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Victor Stinner09532fe2019-05-10 23:39:09 +02005016call_function(PyThreadState *tstate, PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005017{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005018 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005019 PyObject *func = *pfunc;
5020 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07005021 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
5022 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09005023 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005024
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005025 if (tstate->use_tracing) {
5026 x = trace_call_function(tstate, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09005027 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01005028 else {
Petr Viktorinffd97532020-02-11 17:46:57 +01005029 x = PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005030 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00005031
Victor Stinner438a12d2019-05-24 17:01:38 +02005032 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005033
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01005034 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005035 while ((*pp_stack) > pfunc) {
5036 w = EXT_POP(*pp_stack);
5037 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005038 }
Victor Stinnerace47d72013-07-18 01:41:08 +02005039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005040 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005041}
5042
Jeremy Hylton52820442001-01-03 23:52:36 +00005043static PyObject *
Victor Stinner09532fe2019-05-10 23:39:09 +02005044do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005045{
jdemeyere89de732018-09-19 12:06:20 +02005046 PyObject *result;
5047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005048 if (PyCFunction_Check(func)) {
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +02005049 C_TRACE(result, PyObject_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005050 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005051 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005052 else if (Py_IS_TYPE(func, &PyMethodDescr_Type)) {
jdemeyere89de732018-09-19 12:06:20 +02005053 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
5054 if (nargs > 0 && tstate->use_tracing) {
5055 /* We need to create a temporary bound method as argument
5056 for profiling.
5057
5058 If nargs == 0, then this cannot work because we have no
5059 "self". In any case, the call itself would raise
5060 TypeError (foo needs an argument), so we just skip
5061 profiling. */
5062 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
5063 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5064 if (func == NULL) {
5065 return NULL;
5066 }
5067
Victor Stinner4d231bc2019-11-14 13:36:21 +01005068 C_TRACE(result, _PyObject_FastCallDictTstate(
5069 tstate, func,
5070 &_PyTuple_ITEMS(callargs)[1],
5071 nargs - 1,
5072 kwdict));
jdemeyere89de732018-09-19 12:06:20 +02005073 Py_DECREF(func);
5074 return result;
5075 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005076 }
jdemeyere89de732018-09-19 12:06:20 +02005077 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00005078}
5079
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005080/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005081 nb_index slot defined, and store in *pi.
5082 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08005083 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00005084 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005085*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005086int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005087_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005088{
Victor Stinner438a12d2019-05-24 17:01:38 +02005089 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005090 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005091 Py_ssize_t x;
5092 if (PyIndex_Check(v)) {
5093 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005094 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005095 return 0;
5096 }
5097 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005098 _PyErr_SetString(tstate, PyExc_TypeError,
5099 "slice indices must be integers or "
5100 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005101 return 0;
5102 }
5103 *pi = x;
5104 }
5105 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005106}
5107
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005108int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005109_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005110{
Victor Stinner438a12d2019-05-24 17:01:38 +02005111 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005112 Py_ssize_t x;
5113 if (PyIndex_Check(v)) {
5114 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005115 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005116 return 0;
5117 }
5118 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005119 _PyErr_SetString(tstate, PyExc_TypeError,
5120 "slice indices must be integers or "
5121 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005122 return 0;
5123 }
5124 *pi = x;
5125 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005126}
5127
Thomas Wouters52152252000-08-17 22:55:00 +00005128static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005129import_name(PyThreadState *tstate, PyFrameObject *f,
5130 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005131{
5132 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005133 PyObject *import_func, *res;
5134 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005135
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005136 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005137 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005138 if (!_PyErr_Occurred(tstate)) {
5139 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005140 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005141 return NULL;
5142 }
5143
5144 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005145 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005146 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02005147 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005148 return NULL;
5149 }
5150 res = PyImport_ImportModuleLevelObject(
5151 name,
5152 f->f_globals,
5153 f->f_locals == NULL ? Py_None : f->f_locals,
5154 fromlist,
5155 ilevel);
5156 return res;
5157 }
5158
5159 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005160
5161 stack[0] = name;
5162 stack[1] = f->f_globals;
5163 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5164 stack[3] = fromlist;
5165 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005166 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005167 Py_DECREF(import_func);
5168 return res;
5169}
5170
5171static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005172import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005173{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005174 PyObject *x;
Xiang Zhang4830f582017-03-21 11:13:42 +08005175 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005176
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005177 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005178 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005179 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005180 /* Issue #17636: in case this failed because of a circular relative
5181 import, try to fallback on reading the module directly from
5182 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02005183 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005184 if (pkgname == NULL) {
5185 goto error;
5186 }
Oren Milman6db70332017-09-19 14:23:01 +03005187 if (!PyUnicode_Check(pkgname)) {
5188 Py_CLEAR(pkgname);
5189 goto error;
5190 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005191 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005192 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005193 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005194 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005195 }
Eric Snow3f9eee62017-09-15 16:35:20 -06005196 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005197 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02005198 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07005199 goto error;
5200 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005201 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005202 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005203 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005204 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005205 if (pkgname == NULL) {
5206 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5207 if (pkgname_or_unknown == NULL) {
5208 Py_XDECREF(pkgpath);
5209 return NULL;
5210 }
5211 } else {
5212 pkgname_or_unknown = pkgname;
5213 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005214
5215 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005216 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08005217 errmsg = PyUnicode_FromFormat(
5218 "cannot import name %R from %R (unknown location)",
5219 name, pkgname_or_unknown
5220 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005221 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005222 PyErr_SetImportError(errmsg, pkgname, NULL);
5223 }
5224 else {
Anthony Sottile65366bc2019-09-09 08:17:50 -07005225 _Py_IDENTIFIER(__spec__);
5226 PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
Anthony Sottile65366bc2019-09-09 08:17:50 -07005227 const char *fmt =
5228 _PyModuleSpec_IsInitializing(spec) ?
5229 "cannot import name %R from partially initialized module %R "
5230 "(most likely due to a circular import) (%S)" :
5231 "cannot import name %R from %R (%S)";
5232 Py_XDECREF(spec);
5233
5234 errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
Stefan Krah027b09c2019-03-25 21:50:58 +01005235 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005236 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005237 }
5238
Xiang Zhang4830f582017-03-21 11:13:42 +08005239 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005240 Py_XDECREF(pkgname_or_unknown);
5241 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005242 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005243}
Guido van Rossumac7be682001-01-17 15:42:30 +00005244
Thomas Wouters52152252000-08-17 22:55:00 +00005245static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005246import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00005247{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005248 _Py_IDENTIFIER(__all__);
5249 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005250 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005251 int skip_leading_underscores = 0;
5252 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005253
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005254 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5255 return -1; /* Unexpected error */
5256 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005257 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005258 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5259 return -1;
5260 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005261 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005262 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005263 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005264 return -1;
5265 }
5266 all = PyMapping_Keys(dict);
5267 Py_DECREF(dict);
5268 if (all == NULL)
5269 return -1;
5270 skip_leading_underscores = 1;
5271 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005273 for (pos = 0, err = 0; ; pos++) {
5274 name = PySequence_GetItem(all, pos);
5275 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005276 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005277 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02005278 }
5279 else {
5280 _PyErr_Clear(tstate);
5281 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005282 break;
5283 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005284 if (!PyUnicode_Check(name)) {
5285 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5286 if (modname == NULL) {
5287 Py_DECREF(name);
5288 err = -1;
5289 break;
5290 }
5291 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005292 _PyErr_Format(tstate, PyExc_TypeError,
5293 "module __name__ must be a string, not %.100s",
5294 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005295 }
5296 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005297 _PyErr_Format(tstate, PyExc_TypeError,
5298 "%s in %U.%s must be str, not %.100s",
5299 skip_leading_underscores ? "Key" : "Item",
5300 modname,
5301 skip_leading_underscores ? "__dict__" : "__all__",
5302 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005303 }
5304 Py_DECREF(modname);
5305 Py_DECREF(name);
5306 err = -1;
5307 break;
5308 }
5309 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005310 if (PyUnicode_READY(name) == -1) {
5311 Py_DECREF(name);
5312 err = -1;
5313 break;
5314 }
5315 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5316 Py_DECREF(name);
5317 continue;
5318 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005319 }
5320 value = PyObject_GetAttr(v, name);
5321 if (value == NULL)
5322 err = -1;
5323 else if (PyDict_CheckExact(locals))
5324 err = PyDict_SetItem(locals, name, value);
5325 else
5326 err = PyObject_SetItem(locals, name, value);
5327 Py_DECREF(name);
5328 Py_XDECREF(value);
5329 if (err != 0)
5330 break;
5331 }
5332 Py_DECREF(all);
5333 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005334}
5335
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005336static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005337check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005338{
Victor Stinnera102ed72020-02-07 02:24:48 +01005339 if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005340 /* check_args_iterable() may be called with a live exception:
5341 * clear it to prevent calling _PyObject_FunctionStr() with an
5342 * exception set. */
Victor Stinner61f4db82020-01-28 03:37:45 +01005343 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005344 PyObject *funcstr = _PyObject_FunctionStr(func);
5345 if (funcstr != NULL) {
5346 _PyErr_Format(tstate, PyExc_TypeError,
5347 "%U argument after * must be an iterable, not %.200s",
5348 funcstr, Py_TYPE(args)->tp_name);
5349 Py_DECREF(funcstr);
5350 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005351 return -1;
5352 }
5353 return 0;
5354}
5355
5356static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005357format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005358{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005359 /* _PyDict_MergeEx raises attribute
5360 * error (percolated from an attempt
5361 * to get 'keys' attribute) instead of
5362 * a type error if its second argument
5363 * is not a mapping.
5364 */
Victor Stinner438a12d2019-05-24 17:01:38 +02005365 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005366 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005367 PyObject *funcstr = _PyObject_FunctionStr(func);
5368 if (funcstr != NULL) {
5369 _PyErr_Format(
5370 tstate, PyExc_TypeError,
5371 "%U argument after ** must be a mapping, not %.200s",
5372 funcstr, Py_TYPE(kwargs)->tp_name);
5373 Py_DECREF(funcstr);
5374 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005375 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005376 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005377 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02005378 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005379 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005380 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005381 PyObject *funcstr = _PyObject_FunctionStr(func);
5382 if (funcstr != NULL) {
5383 PyObject *key = PyTuple_GET_ITEM(val, 0);
5384 _PyErr_Format(
5385 tstate, PyExc_TypeError,
5386 "%U got multiple values for keyword argument '%S'",
5387 funcstr, key);
5388 Py_DECREF(funcstr);
5389 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005390 Py_XDECREF(exc);
5391 Py_XDECREF(val);
5392 Py_XDECREF(tb);
5393 }
5394 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005395 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005396 }
5397 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005398}
5399
Guido van Rossumac7be682001-01-17 15:42:30 +00005400static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005401format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
5402 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005403{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005404 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005406 if (!obj)
5407 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005408
Serhiy Storchaka06515832016-11-20 09:13:07 +02005409 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005410 if (!obj_str)
5411 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005412
Victor Stinner438a12d2019-05-24 17:01:38 +02005413 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005414}
Guido van Rossum950361c1997-01-24 13:49:28 +00005415
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005416static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005417format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005418{
5419 PyObject *name;
5420 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02005421 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005422 return;
5423 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5424 name = PyTuple_GET_ITEM(co->co_cellvars,
5425 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005426 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005427 PyExc_UnboundLocalError,
5428 UNBOUNDLOCAL_ERROR_MSG,
5429 name);
5430 } else {
5431 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5432 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02005433 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005434 UNBOUNDFREE_ERROR_MSG, name);
5435 }
5436}
5437
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005438static void
Mark Shannonfee55262019-11-21 09:11:43 +00005439format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevprevopcode, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005440{
5441 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5442 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005443 _PyErr_Format(tstate, PyExc_TypeError,
5444 "'async with' received an object from __aenter__ "
5445 "that does not implement __await__: %.100s",
5446 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005447 }
Mark Shannonfee55262019-11-21 09:11:43 +00005448 else if (prevopcode == WITH_EXCEPT_START || (prevopcode == CALL_FUNCTION && prevprevopcode == DUP_TOP)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005449 _PyErr_Format(tstate, PyExc_TypeError,
5450 "'async with' received an object from __aexit__ "
5451 "that does not implement __await__: %.100s",
5452 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005453 }
5454 }
5455}
5456
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005457static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005458unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005459 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005460{
5461 PyObject *res;
5462 if (Py_REFCNT(v) == 2) {
5463 /* In the common case, there are 2 references to the value
5464 * stored in 'variable' when the += is performed: one on the
5465 * value stack (in 'v') and one still stored in the
5466 * 'variable'. We try to delete the variable now to reduce
5467 * the refcnt to 1.
5468 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005469 int opcode, oparg;
5470 NEXTOPARG();
5471 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005472 case STORE_FAST:
5473 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005474 PyObject **fastlocals = f->f_localsplus;
5475 if (GETLOCAL(oparg) == v)
5476 SETLOCAL(oparg, NULL);
5477 break;
5478 }
5479 case STORE_DEREF:
5480 {
5481 PyObject **freevars = (f->f_localsplus +
5482 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005483 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005484 if (PyCell_GET(c) == v) {
5485 PyCell_SET(c, NULL);
5486 Py_DECREF(v);
5487 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005488 break;
5489 }
5490 case STORE_NAME:
5491 {
5492 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005493 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005494 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005495 if (locals && PyDict_CheckExact(locals)) {
5496 PyObject *w = PyDict_GetItemWithError(locals, name);
5497 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02005498 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005499 {
5500 Py_DECREF(v);
5501 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005502 }
5503 }
5504 break;
5505 }
5506 }
5507 }
5508 res = v;
5509 PyUnicode_Append(&res, w);
5510 return res;
5511}
5512
Guido van Rossum950361c1997-01-24 13:49:28 +00005513#ifdef DYNAMIC_EXECUTION_PROFILE
5514
Skip Montanarof118cb12001-10-15 20:51:38 +00005515static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005516getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005517{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005518 int i;
5519 PyObject *l = PyList_New(256);
5520 if (l == NULL) return NULL;
5521 for (i = 0; i < 256; i++) {
5522 PyObject *x = PyLong_FromLong(a[i]);
5523 if (x == NULL) {
5524 Py_DECREF(l);
5525 return NULL;
5526 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005527 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005528 }
5529 for (i = 0; i < 256; i++)
5530 a[i] = 0;
5531 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005532}
5533
5534PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005535_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005536{
5537#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005538 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005539#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005540 int i;
5541 PyObject *l = PyList_New(257);
5542 if (l == NULL) return NULL;
5543 for (i = 0; i < 257; i++) {
5544 PyObject *x = getarray(dxpairs[i]);
5545 if (x == NULL) {
5546 Py_DECREF(l);
5547 return NULL;
5548 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005549 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005550 }
5551 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005552#endif
5553}
5554
5555#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005556
5557Py_ssize_t
5558_PyEval_RequestCodeExtraIndex(freefunc free)
5559{
Victor Stinnercaba55b2018-08-03 15:33:52 +02005560 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -07005561 Py_ssize_t new_index;
5562
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005563 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005564 return -1;
5565 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005566 new_index = interp->co_extra_user_count++;
5567 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005568 return new_index;
5569}
Łukasz Langaa785c872016-09-09 17:37:37 -07005570
5571static void
5572dtrace_function_entry(PyFrameObject *f)
5573{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005574 const char *filename;
5575 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005576 int lineno;
5577
5578 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5579 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5580 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5581
Andy Lestere6be9b52020-02-11 20:28:35 -06005582 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005583}
5584
5585static void
5586dtrace_function_return(PyFrameObject *f)
5587{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005588 const char *filename;
5589 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005590 int lineno;
5591
5592 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5593 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5594 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5595
Andy Lestere6be9b52020-02-11 20:28:35 -06005596 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005597}
5598
5599/* DTrace equivalent of maybe_call_line_trace. */
5600static void
5601maybe_dtrace_line(PyFrameObject *frame,
5602 int *instr_lb, int *instr_ub, int *instr_prev)
5603{
5604 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005605 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005606
5607 /* If the last instruction executed isn't in the current
5608 instruction window, reset the window.
5609 */
5610 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5611 PyAddrPair bounds;
5612 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5613 &bounds);
5614 *instr_lb = bounds.ap_lower;
5615 *instr_ub = bounds.ap_upper;
5616 }
5617 /* If the last instruction falls at the start of a line or if
5618 it represents a jump backwards, update the frame's line
5619 number and call the trace function. */
5620 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5621 frame->f_lineno = line;
5622 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5623 if (!co_filename)
5624 co_filename = "?";
5625 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5626 if (!co_name)
5627 co_name = "?";
Andy Lestere6be9b52020-02-11 20:28:35 -06005628 PyDTrace_LINE(co_filename, co_name, line);
Łukasz Langaa785c872016-09-09 17:37:37 -07005629 }
5630 *instr_prev = frame->f_lasti;
5631}
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005632
5633
5634/* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions
5635 for the limited API. */
5636
5637#undef Py_EnterRecursiveCall
5638
5639int Py_EnterRecursiveCall(const char *where)
5640{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005641 return _Py_EnterRecursiveCall_inline(where);
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005642}
5643
5644#undef Py_LeaveRecursiveCall
5645
5646void Py_LeaveRecursiveCall(void)
5647{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005648 _Py_LeaveRecursiveCall_inline();
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005649}