blob: cb5a4beb2a663362321a06dad2b948a6a20655bd [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 Stinner09532fe2019-05-10 23:39:09 +020013#include "pycore_ceval.h"
Victor Stinnerbcda8f12018-11-21 22:27:47 +010014#include "pycore_object.h"
Victor Stinner438a12d2019-05-24 17:01:38 +020015#include "pycore_pyerrors.h"
16#include "pycore_pylifecycle.h"
Victor Stinner621cebe2018-11-12 16:53:38 +010017#include "pycore_pystate.h"
Victor Stinnerec13b932018-11-25 23:56:17 +010018#include "pycore_tupleobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000019
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000020#include "code.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040021#include "dictobject.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000022#include "frameobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000023#include "opcode.h"
Łukasz Langaa785c872016-09-09 17:37:37 -070024#include "pydtrace.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040025#include "setobject.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000026#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000027
Guido van Rossumc6004111993-11-05 10:22:19 +000028#include <ctype.h>
29
Guido van Rossum408027e1996-12-30 16:17:54 +000030#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000031/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000032#define LLTRACE 1 /* Low-level trace feature */
33#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000034#endif
35
Victor Stinner5c75f372019-04-17 23:02:26 +020036#if !defined(Py_BUILD_CORE)
37# error "ceval.c must be build with Py_BUILD_CORE define for best performance"
38#endif
39
Yury Selivanovf2392132016-12-13 19:03:51 -050040/* Private API for the LOAD_METHOD opcode. */
41extern int _PyObject_GetMethod(PyObject *, PyObject *, PyObject **);
42
Jeremy Hylton52820442001-01-03 23:52:36 +000043typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000044
Guido van Rossum374a9221991-04-04 10:40:29 +000045/* Forward declarations */
Victor Stinner09532fe2019-05-10 23:39:09 +020046Py_LOCAL_INLINE(PyObject *) call_function(
47 PyThreadState *tstate, PyObject ***pp_stack,
48 Py_ssize_t oparg, PyObject *kwnames);
49static PyObject * do_call_core(
50 PyThreadState *tstate, PyObject *func,
51 PyObject *callargs, PyObject *kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +000052
Guido van Rossum0a066c01992-03-27 17:29:15 +000053#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000054static int lltrace;
Victor Stinner438a12d2019-05-24 17:01:38 +020055static int prtrace(PyThreadState *, PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000056#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010057static int call_trace(Py_tracefunc, PyObject *,
58 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000060static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010061 PyThreadState *, PyFrameObject *,
62 int, PyObject *);
63static void call_exc_trace(Py_tracefunc, PyObject *,
64 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000065static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Eric Snow2ebc5ce2017-09-07 23:51:28 -060066 PyThreadState *, PyFrameObject *,
67 int *, int *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070068static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *);
69static void dtrace_function_entry(PyFrameObject *);
70static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000071
Victor Stinner438a12d2019-05-24 17:01:38 +020072static PyObject * cmp_outcome(PyThreadState *, int, PyObject *, PyObject *);
73static 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);
84static void format_awaitable_error(PyThreadState *, PyTypeObject *, 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
Victor Stinner09532fe2019-05-10 23:39:09 +0200104#define GIL_REQUEST _Py_atomic_load_relaxed(&ceval->gil_drop_request)
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000105
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000106/* This can set eval_breaker to 0 even though gil_drop_request became
107 1. We believe this is all right because the eval loop will release
108 the GIL eventually anyway. */
Victor Stinner09532fe2019-05-10 23:39:09 +0200109#define COMPUTE_EVAL_BREAKER(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 _Py_atomic_store_relaxed( \
Victor Stinner09532fe2019-05-10 23:39:09 +0200111 &(ceval)->eval_breaker, \
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000112 GIL_REQUEST | \
Victor Stinner09532fe2019-05-10 23:39:09 +0200113 _Py_atomic_load_relaxed(&(ceval)->signals_pending) | \
114 _Py_atomic_load_relaxed(&(ceval)->pending.calls_to_do) | \
115 (ceval)->pending.async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000116
Victor Stinner09532fe2019-05-10 23:39:09 +0200117#define SET_GIL_DROP_REQUEST(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 do { \
Victor Stinner09532fe2019-05-10 23:39:09 +0200119 _Py_atomic_store_relaxed(&(ceval)->gil_drop_request, 1); \
120 _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000122
Victor Stinner09532fe2019-05-10 23:39:09 +0200123#define RESET_GIL_DROP_REQUEST(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 do { \
Victor Stinner09532fe2019-05-10 23:39:09 +0200125 _Py_atomic_store_relaxed(&(ceval)->gil_drop_request, 0); \
126 COMPUTE_EVAL_BREAKER(ceval); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000128
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000129/* Pending calls are only modified under pending_lock */
Victor Stinner09532fe2019-05-10 23:39:09 +0200130#define SIGNAL_PENDING_CALLS(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 do { \
Victor Stinner09532fe2019-05-10 23:39:09 +0200132 _Py_atomic_store_relaxed(&(ceval)->pending.calls_to_do, 1); \
133 _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000134 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000135
Victor Stinner09532fe2019-05-10 23:39:09 +0200136#define UNSIGNAL_PENDING_CALLS(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137 do { \
Victor Stinner09532fe2019-05-10 23:39:09 +0200138 _Py_atomic_store_relaxed(&(ceval)->pending.calls_to_do, 0); \
139 COMPUTE_EVAL_BREAKER(ceval); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000141
Victor Stinner09532fe2019-05-10 23:39:09 +0200142#define SIGNAL_PENDING_SIGNALS(ceval) \
Eric Snowfdf282d2019-01-11 14:26:55 -0700143 do { \
Victor Stinner09532fe2019-05-10 23:39:09 +0200144 _Py_atomic_store_relaxed(&(ceval)->signals_pending, 1); \
145 _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \
Eric Snowfdf282d2019-01-11 14:26:55 -0700146 } while (0)
147
Victor Stinner09532fe2019-05-10 23:39:09 +0200148#define UNSIGNAL_PENDING_SIGNALS(ceval) \
Eric Snowfdf282d2019-01-11 14:26:55 -0700149 do { \
Victor Stinner09532fe2019-05-10 23:39:09 +0200150 _Py_atomic_store_relaxed(&(ceval)->signals_pending, 0); \
151 COMPUTE_EVAL_BREAKER(ceval); \
Eric Snowfdf282d2019-01-11 14:26:55 -0700152 } while (0)
153
Victor Stinner09532fe2019-05-10 23:39:09 +0200154#define SIGNAL_ASYNC_EXC(ceval) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 do { \
Victor Stinner09532fe2019-05-10 23:39:09 +0200156 (ceval)->pending.async_exc = 1; \
157 _Py_atomic_store_relaxed(&(ceval)->eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000159
Victor Stinner09532fe2019-05-10 23:39:09 +0200160#define UNSIGNAL_ASYNC_EXC(ceval) \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600161 do { \
Victor Stinner09532fe2019-05-10 23:39:09 +0200162 (ceval)->pending.async_exc = 0; \
163 COMPUTE_EVAL_BREAKER(ceval); \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600164 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000165
166
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000167#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000168#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000169#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000170#include "pythread.h"
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000171#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000172
Tim Peters7f468f22004-10-11 02:40:51 +0000173int
174PyEval_ThreadsInitialized(void)
175{
Victor Stinner09532fe2019-05-10 23:39:09 +0200176 return gil_created(&_PyRuntime.ceval.gil);
Tim Peters7f468f22004-10-11 02:40:51 +0000177}
178
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000179void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000180PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000181{
Victor Stinner09532fe2019-05-10 23:39:09 +0200182 _PyRuntimeState *runtime = &_PyRuntime;
183 struct _ceval_runtime_state *ceval = &runtime->ceval;
184 struct _gil_runtime_state *gil = &ceval->gil;
185 if (gil_created(gil)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000186 return;
Victor Stinnera7126792019-03-19 14:19:38 +0100187 }
188
Inada Naoki001fee12019-02-20 10:00:09 +0900189 PyThread_init_thread();
Victor Stinner09532fe2019-05-10 23:39:09 +0200190 create_gil(gil);
191 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
192 take_gil(ceval, tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700193
Victor Stinner09532fe2019-05-10 23:39:09 +0200194 struct _pending_calls *pending = &ceval->pending;
195 pending->lock = PyThread_allocate_lock();
196 if (pending->lock == NULL) {
Eric Snowb75b1a352019-04-12 10:20:10 -0600197 Py_FatalError("Can't initialize threads for pending calls");
198 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000199}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000200
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000201void
Victor Stinner09532fe2019-05-10 23:39:09 +0200202_PyEval_FiniThreads(struct _ceval_runtime_state *ceval)
Antoine Pitrou1df15362010-09-13 14:16:46 +0000203{
Victor Stinner09532fe2019-05-10 23:39:09 +0200204 struct _gil_runtime_state *gil = &ceval->gil;
205 if (!gil_created(gil)) {
Antoine Pitrou1df15362010-09-13 14:16:46 +0000206 return;
Victor Stinnera7126792019-03-19 14:19:38 +0100207 }
208
Victor Stinner09532fe2019-05-10 23:39:09 +0200209 destroy_gil(gil);
210 assert(!gil_created(gil));
Victor Stinner99fcc612019-04-29 13:04:07 +0200211
Victor Stinner09532fe2019-05-10 23:39:09 +0200212 struct _pending_calls *pending = &ceval->pending;
213 if (pending->lock != NULL) {
214 PyThread_free_lock(pending->lock);
215 pending->lock = NULL;
Victor Stinner99fcc612019-04-29 13:04:07 +0200216 }
Antoine Pitrou1df15362010-09-13 14:16:46 +0000217}
218
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400219static inline void
Victor Stinner09532fe2019-05-10 23:39:09 +0200220exit_thread_if_finalizing(_PyRuntimeState *runtime, PyThreadState *tstate)
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400221{
222 /* _Py_Finalizing is protected by the GIL */
Victor Stinner09532fe2019-05-10 23:39:09 +0200223 if (runtime->finalizing != NULL && !_Py_CURRENTLY_FINALIZING(runtime, tstate)) {
224 drop_gil(&runtime->ceval, tstate);
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400225 PyThread_exit_thread();
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400226 }
227}
228
Antoine Pitrou1df15362010-09-13 14:16:46 +0000229void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000230PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000231{
Victor Stinner09532fe2019-05-10 23:39:09 +0200232 _PyRuntimeState *runtime = &_PyRuntime;
233 struct _ceval_runtime_state *ceval = &runtime->ceval;
234 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
235 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
Victor Stinner09532fe2019-05-10 23:39:09 +0200237 }
238 take_gil(ceval, tstate);
239 exit_thread_if_finalizing(runtime, tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000240}
241
242void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000243PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000244{
Victor Stinner09532fe2019-05-10 23:39:09 +0200245 _PyRuntimeState *runtime = &_PyRuntime;
246 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100248 We therefore avoid PyThreadState_Get() which dumps a fatal error
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 in debug mode.
250 */
Victor Stinner09532fe2019-05-10 23:39:09 +0200251 drop_gil(&runtime->ceval, tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000252}
253
254void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000255PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000256{
Victor Stinner09532fe2019-05-10 23:39:09 +0200257 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200259 }
260
261 _PyRuntimeState *runtime = &_PyRuntime;
262 struct _ceval_runtime_state *ceval = &runtime->ceval;
263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 /* Check someone has called PyEval_InitThreads() to create the lock */
Victor Stinner09532fe2019-05-10 23:39:09 +0200265 assert(gil_created(&ceval->gil));
266 take_gil(ceval, tstate);
267 exit_thread_if_finalizing(runtime, tstate);
268 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
269 Py_FatalError("PyEval_AcquireThread: non-NULL old thread state");
270 }
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000271}
272
273void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000274PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000275{
Victor Stinner09532fe2019-05-10 23:39:09 +0200276 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200278 }
279
280 _PyRuntimeState *runtime = &_PyRuntime;
281 PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
282 if (new_tstate != tstate) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200284 }
285 drop_gil(&runtime->ceval, tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000286}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000287
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200288/* This function is called from PyOS_AfterFork_Child to destroy all threads
289 * which are not running in the child process, and clear internal locks
290 * which might be held by those threads.
291 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000292
293void
Victor Stinnerd5d9e812019-05-13 12:35:37 +0200294_PyEval_ReInitThreads(_PyRuntimeState *runtime)
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000295{
Victor Stinner09532fe2019-05-10 23:39:09 +0200296 struct _ceval_runtime_state *ceval = &runtime->ceval;
297 if (!gil_created(&ceval->gil)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 return;
Victor Stinner09532fe2019-05-10 23:39:09 +0200299 }
300 recreate_gil(&ceval->gil);
301 PyThreadState *current_tstate = _PyRuntimeState_GetThreadState(runtime);
302 take_gil(ceval, current_tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700303
Victor Stinner09532fe2019-05-10 23:39:09 +0200304 struct _pending_calls *pending = &ceval->pending;
305 pending->lock = PyThread_allocate_lock();
306 if (pending->lock == NULL) {
Eric Snow8479a342019-03-08 23:44:33 -0700307 Py_FatalError("Can't initialize threads for pending calls");
308 }
Jesse Nollera8513972008-07-17 16:49:17 +0000309
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200310 /* Destroy all threads except the current one */
Victor Stinner09532fe2019-05-10 23:39:09 +0200311 _PyThreadState_DeleteExcept(runtime, current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000312}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000313
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000314/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600315 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000316
317void
Victor Stinner09532fe2019-05-10 23:39:09 +0200318_PyEval_SignalAsyncExc(struct _ceval_runtime_state *ceval)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000319{
Victor Stinner09532fe2019-05-10 23:39:09 +0200320 SIGNAL_ASYNC_EXC(ceval);
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000321}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000322
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000323PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000324PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000325{
Victor Stinner09532fe2019-05-10 23:39:09 +0200326 _PyRuntimeState *runtime = &_PyRuntime;
327 struct _ceval_runtime_state *ceval = &runtime->ceval;
328 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
329 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 Py_FatalError("PyEval_SaveThread: NULL tstate");
Victor Stinner09532fe2019-05-10 23:39:09 +0200331 }
332 assert(gil_created(&ceval->gil));
333 drop_gil(ceval, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000335}
336
337void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000338PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000339{
Victor Stinner09532fe2019-05-10 23:39:09 +0200340 _PyRuntimeState *runtime = &_PyRuntime;
341 struct _ceval_runtime_state *ceval = &runtime->ceval;
342
343 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Victor Stinner09532fe2019-05-10 23:39:09 +0200345 }
346 assert(gil_created(&ceval->gil));
Victor Stinner2914bb32018-01-29 11:57:45 +0100347
348 int err = errno;
Victor Stinner09532fe2019-05-10 23:39:09 +0200349 take_gil(ceval, tstate);
350 exit_thread_if_finalizing(runtime, tstate);
Victor Stinner2914bb32018-01-29 11:57:45 +0100351 errno = err;
352
Victor Stinner09532fe2019-05-10 23:39:09 +0200353 _PyThreadState_Swap(&runtime->gilstate, tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000354}
355
356
Guido van Rossuma9672091994-09-14 13:31:22 +0000357/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
358 signal handlers or Mac I/O completion routines) can schedule calls
359 to a function to be called synchronously.
360 The synchronous function is called with one void* argument.
361 It should return 0 for success or -1 for failure -- failure should
362 be accompanied by an exception.
363
364 If registry succeeds, the registry function returns 0; if it fails
365 (e.g. due to too many pending calls) it returns -1 (without setting
366 an exception condition).
367
368 Note that because registry may occur from within signal handlers,
369 or other asynchronous events, calling malloc() is unsafe!
370
Guido van Rossuma9672091994-09-14 13:31:22 +0000371 Any thread can schedule pending calls, but only the main thread
372 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000373 There is no facility to schedule calls to a particular thread, but
374 that should be easy to change, should that ever be required. In
375 that case, the static variables here should go into the python
376 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000377*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000378
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200379void
Victor Stinner09532fe2019-05-10 23:39:09 +0200380_PyEval_SignalReceived(struct _ceval_runtime_state *ceval)
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200381{
382 /* bpo-30703: Function called when the C signal handler of Python gets a
383 signal. We cannot queue a callback using Py_AddPendingCall() since
384 that function is not async-signal-safe. */
Victor Stinner09532fe2019-05-10 23:39:09 +0200385 SIGNAL_PENDING_SIGNALS(ceval);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200386}
387
Eric Snow5be45a62019-03-08 22:47:07 -0700388/* Push one item onto the queue while holding the lock. */
389static int
Eric Snowb75b1a352019-04-12 10:20:10 -0600390_push_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600391 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700392{
Eric Snow842a2f02019-03-15 15:47:51 -0600393 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700394 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600395 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700396 return -1; /* Queue full */
397 }
Eric Snow842a2f02019-03-15 15:47:51 -0600398 pending->calls[i].func = func;
399 pending->calls[i].arg = arg;
400 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700401 return 0;
402}
403
404/* Pop one item off the queue while holding the lock. */
405static void
Eric Snowb75b1a352019-04-12 10:20:10 -0600406_pop_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600407 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700408{
Eric Snow842a2f02019-03-15 15:47:51 -0600409 int i = pending->first;
410 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700411 return; /* Queue empty */
412 }
413
Eric Snow842a2f02019-03-15 15:47:51 -0600414 *func = pending->calls[i].func;
415 *arg = pending->calls[i].arg;
416 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700417}
418
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200419/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000420 scheduling to be made from any thread, and even from an executing
421 callback.
422 */
423
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000424int
Victor Stinner438a12d2019-05-24 17:01:38 +0200425_PyEval_AddPendingCall(PyThreadState *tstate,
426 struct _ceval_runtime_state *ceval,
Victor Stinner09532fe2019-05-10 23:39:09 +0200427 int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000428{
Victor Stinner09532fe2019-05-10 23:39:09 +0200429 struct _pending_calls *pending = &ceval->pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600430
431 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
432 if (pending->finishing) {
433 PyThread_release_lock(pending->lock);
434
435 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +0200436 _PyErr_Fetch(tstate, &exc, &val, &tb);
437 _PyErr_SetString(tstate, PyExc_SystemError,
Eric Snow842a2f02019-03-15 15:47:51 -0600438 "Py_AddPendingCall: cannot add pending calls "
439 "(Python shutting down)");
Victor Stinner438a12d2019-05-24 17:01:38 +0200440 _PyErr_Print(tstate);
441 _PyErr_Restore(tstate, exc, val, tb);
Eric Snow842a2f02019-03-15 15:47:51 -0600442 return -1;
443 }
Eric Snowb75b1a352019-04-12 10:20:10 -0600444 int result = _push_pending_call(pending, func, arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600445 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700446
Eric Snowb75b1a352019-04-12 10:20:10 -0600447 /* signal main loop */
Victor Stinner09532fe2019-05-10 23:39:09 +0200448 SIGNAL_PENDING_CALLS(ceval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000450}
451
Victor Stinner09532fe2019-05-10 23:39:09 +0200452int
453Py_AddPendingCall(int (*func)(void *), void *arg)
454{
Victor Stinner438a12d2019-05-24 17:01:38 +0200455 _PyRuntimeState *runtime = &_PyRuntime;
456 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
457 return _PyEval_AddPendingCall(tstate, &runtime->ceval, func, arg);
Victor Stinner09532fe2019-05-10 23:39:09 +0200458}
459
Eric Snowfdf282d2019-01-11 14:26:55 -0700460static int
Victor Stinner09532fe2019-05-10 23:39:09 +0200461handle_signals(_PyRuntimeState *runtime)
Eric Snowfdf282d2019-01-11 14:26:55 -0700462{
Eric Snow5be45a62019-03-08 22:47:07 -0700463 /* Only handle signals on main thread. PyEval_InitThreads must
464 * have been called already.
465 */
Victor Stinner09532fe2019-05-10 23:39:09 +0200466 if (PyThread_get_thread_ident() != runtime->main_thread) {
Eric Snowfdf282d2019-01-11 14:26:55 -0700467 return 0;
468 }
Eric Snow64d6cc82019-02-23 15:40:43 -0700469 /*
470 * Ensure that the thread isn't currently running some other
471 * interpreter.
472 */
Victor Stinner09532fe2019-05-10 23:39:09 +0200473 PyInterpreterState *interp = _PyRuntimeState_GetThreadState(runtime)->interp;
474 if (interp != runtime->interpreters.main) {
Eric Snow64d6cc82019-02-23 15:40:43 -0700475 return 0;
476 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700477
Victor Stinner09532fe2019-05-10 23:39:09 +0200478 struct _ceval_runtime_state *ceval = &runtime->ceval;
479 UNSIGNAL_PENDING_SIGNALS(ceval);
Eric Snow64d6cc82019-02-23 15:40:43 -0700480 if (_PyErr_CheckSignals() < 0) {
Victor Stinner09532fe2019-05-10 23:39:09 +0200481 SIGNAL_PENDING_SIGNALS(ceval); /* We're not done yet */
Eric Snowfdf282d2019-01-11 14:26:55 -0700482 return -1;
483 }
484 return 0;
485}
486
487static int
Victor Stinner09532fe2019-05-10 23:39:09 +0200488make_pending_calls(_PyRuntimeState *runtime)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000489{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200490 static int busy = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000491
Eric Snowb75b1a352019-04-12 10:20:10 -0600492 /* only service pending calls on main thread */
Victor Stinner09532fe2019-05-10 23:39:09 +0200493 if (PyThread_get_thread_ident() != runtime->main_thread) {
Eric Snowb75b1a352019-04-12 10:20:10 -0600494 return 0;
495 }
496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 /* don't perform recursive pending calls */
Eric Snowfdf282d2019-01-11 14:26:55 -0700498 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700500 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200501 busy = 1;
Victor Stinner09532fe2019-05-10 23:39:09 +0200502 struct _ceval_runtime_state *ceval = &runtime->ceval;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200503 /* unsignal before starting to call callbacks, so that any callback
504 added in-between re-signals */
Victor Stinner09532fe2019-05-10 23:39:09 +0200505 UNSIGNAL_PENDING_CALLS(ceval);
Eric Snowfdf282d2019-01-11 14:26:55 -0700506 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200507
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 /* perform a bounded number of calls, in case of recursion */
Victor Stinner09532fe2019-05-10 23:39:09 +0200509 struct _pending_calls *pending = &ceval->pending;
Eric Snowfdf282d2019-01-11 14:26:55 -0700510 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700511 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 void *arg = NULL;
513
514 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600515 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Eric Snowb75b1a352019-04-12 10:20:10 -0600516 _pop_pending_call(pending, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600517 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700518
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100519 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700520 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100521 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700522 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700523 res = func(arg);
524 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200525 goto error;
526 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200528
Charles-François Natalif23339a2011-07-23 18:15:43 +0200529 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700530 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200531
532error:
533 busy = 0;
Victor Stinner09532fe2019-05-10 23:39:09 +0200534 SIGNAL_PENDING_CALLS(ceval);
Eric Snowfdf282d2019-01-11 14:26:55 -0700535 return res;
536}
537
Eric Snow842a2f02019-03-15 15:47:51 -0600538void
Victor Stinner09532fe2019-05-10 23:39:09 +0200539_Py_FinishPendingCalls(_PyRuntimeState *runtime)
Eric Snow842a2f02019-03-15 15:47:51 -0600540{
Eric Snow842a2f02019-03-15 15:47:51 -0600541 assert(PyGILState_Check());
542
Victor Stinner438a12d2019-05-24 17:01:38 +0200543 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinner09532fe2019-05-10 23:39:09 +0200544 struct _pending_calls *pending = &runtime->ceval.pending;
545
Eric Snow842a2f02019-03-15 15:47:51 -0600546 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
547 pending->finishing = 1;
548 PyThread_release_lock(pending->lock);
549
550 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
551 return;
552 }
553
Victor Stinner09532fe2019-05-10 23:39:09 +0200554 if (make_pending_calls(runtime) < 0) {
Eric Snow842a2f02019-03-15 15:47:51 -0600555 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +0200556 _PyErr_Fetch(tstate, &exc, &val, &tb);
Eric Snow842a2f02019-03-15 15:47:51 -0600557 PyErr_BadInternalCall();
558 _PyErr_ChainExceptions(exc, val, tb);
Victor Stinner438a12d2019-05-24 17:01:38 +0200559 _PyErr_Print(tstate);
Eric Snow842a2f02019-03-15 15:47:51 -0600560 }
561}
562
Eric Snowfdf282d2019-01-11 14:26:55 -0700563/* Py_MakePendingCalls() is a simple wrapper for the sake
564 of backward-compatibility. */
565int
566Py_MakePendingCalls(void)
567{
568 assert(PyGILState_Check());
569
570 /* Python signal handler doesn't really queue a callback: it only signals
571 that a signal was received, see _PyEval_SignalReceived(). */
Victor Stinner09532fe2019-05-10 23:39:09 +0200572 _PyRuntimeState *runtime = &_PyRuntime;
573 int res = handle_signals(runtime);
Eric Snowfdf282d2019-01-11 14:26:55 -0700574 if (res != 0) {
575 return res;
576 }
577
Victor Stinner09532fe2019-05-10 23:39:09 +0200578 res = make_pending_calls(runtime);
Eric Snowb75b1a352019-04-12 10:20:10 -0600579 if (res != 0) {
580 return res;
581 }
582
583 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000584}
585
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000586/* The interpreter's recursion limit */
587
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000588#ifndef Py_DEFAULT_RECURSION_LIMIT
589#define Py_DEFAULT_RECURSION_LIMIT 1000
590#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600591
Eric Snow05351c12017-09-05 21:43:08 -0700592int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000593
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600594void
595_PyEval_Initialize(struct _ceval_runtime_state *state)
596{
597 state->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
598 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
599 _gil_initialize(&state->gil);
600}
601
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000602int
603Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000604{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600605 return _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000606}
607
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000608void
609Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000610{
Victor Stinner09532fe2019-05-10 23:39:09 +0200611 struct _ceval_runtime_state *ceval = &_PyRuntime.ceval;
612 ceval->recursion_limit = new_limit;
613 _Py_CheckRecursionLimit = ceval->recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000614}
615
Armin Rigo2b3eb402003-10-28 12:05:48 +0000616/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
617 if the recursion_depth reaches _Py_CheckRecursionLimit.
618 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
619 to guarantee that _Py_CheckRecursiveCall() is regularly called.
620 Without USE_STACKCHECK, there is no need for this. */
621int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300622_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000623{
Victor Stinner09532fe2019-05-10 23:39:09 +0200624 _PyRuntimeState *runtime = &_PyRuntime;
625 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
626 int recursion_limit = runtime->ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000627
628#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700629 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 if (PyOS_CheckStack()) {
631 --tstate->recursion_depth;
Victor Stinner438a12d2019-05-24 17:01:38 +0200632 _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 return -1;
634 }
pdox18967932017-10-25 23:03:01 -0700635 /* Needed for ABI backwards-compatibility (see bpo-31857) */
Eric Snow05351c12017-09-05 21:43:08 -0700636 _Py_CheckRecursionLimit = recursion_limit;
pdox18967932017-10-25 23:03:01 -0700637#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 if (tstate->recursion_critical)
639 /* Somebody asked that we don't check for recursion. */
640 return 0;
641 if (tstate->overflowed) {
642 if (tstate->recursion_depth > recursion_limit + 50) {
643 /* Overflowing while handling an overflow. Give up. */
644 Py_FatalError("Cannot recover from stack overflow.");
645 }
646 return 0;
647 }
648 if (tstate->recursion_depth > recursion_limit) {
649 --tstate->recursion_depth;
650 tstate->overflowed = 1;
Victor Stinner438a12d2019-05-24 17:01:38 +0200651 _PyErr_Format(tstate, PyExc_RecursionError,
652 "maximum recursion depth exceeded%s",
653 where);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 return -1;
655 }
656 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000657}
658
Victor Stinner09532fe2019-05-10 23:39:09 +0200659static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
Victor Stinner438a12d2019-05-24 17:01:38 +0200660static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000661
Victor Stinner09532fe2019-05-10 23:39:09 +0200662#define _Py_TracingPossible(ceval) ((ceval)->tracing_possible)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000663
Guido van Rossum374a9221991-04-04 10:40:29 +0000664
Guido van Rossumb209a111997-04-29 18:18:01 +0000665PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000666PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000667{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 return PyEval_EvalCodeEx(co,
669 globals, locals,
670 (PyObject **)NULL, 0,
671 (PyObject **)NULL, 0,
672 (PyObject **)NULL, 0,
673 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000674}
675
676
677/* Interpreter main loop */
678
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000679PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000680PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 /* This is for backward compatibility with extension modules that
682 used this API; core interpreter code should call
683 PyEval_EvalFrameEx() */
684 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000685}
686
687PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000688PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000689{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200690 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
691 return interp->eval_frame(f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700692}
693
Victor Stinnerc6944e72016-11-11 02:13:35 +0100694PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700695_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
696{
Guido van Rossum950361c1997-01-24 13:49:28 +0000697#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000699#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200700 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300701 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200702 int opcode; /* Current opcode */
703 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200704 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 PyObject *retval = NULL; /* Return value */
Victor Stinner09532fe2019-05-10 23:39:09 +0200706 _PyRuntimeState * const runtime = &_PyRuntime;
707 PyThreadState * const tstate = _PyRuntimeState_GetThreadState(runtime);
708 struct _ceval_runtime_state * const ceval = &runtime->ceval;
709 _Py_atomic_int * const eval_breaker = &ceval->eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000711
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000715
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 is true when the line being executed has changed. The
717 initial values are such as to make this false the first
718 time it is tested. */
719 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000720
Serhiy Storchakaab874002016-09-11 13:48:15 +0300721 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 PyObject *names;
723 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000724
Brett Cannon368b4b72012-04-02 12:17:59 -0400725#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200726 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400727#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200728
Antoine Pitroub52ec782009-01-25 16:34:23 +0000729/* Computed GOTOs, or
730 the-optimization-commonly-but-improperly-known-as-"threaded code"
731 using gcc's labels-as-values extension
732 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
733
734 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000736 combined with a lookup table of jump addresses. However, since the
737 indirect jump instruction is shared by all opcodes, the CPU will have a
738 hard time making the right prediction for where to jump next (actually,
739 it will be always wrong except in the uncommon case of a sequence of
740 several identical opcodes).
741
742 "Threaded code" in contrast, uses an explicit jump table and an explicit
743 indirect jump instruction at the end of each opcode. Since the jump
744 instruction is at a different address for each opcode, the CPU will make a
745 separate prediction for each of these instructions, which is equivalent to
746 predicting the second opcode of each opcode pair. These predictions have
747 a much better chance to turn out valid, especially in small bytecode loops.
748
749 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000750 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000751 and potentially many more instructions (depending on the pipeline width).
752 A correctly predicted branch, however, is nearly free.
753
754 At the time of this writing, the "threaded code" version is up to 15-20%
755 faster than the normal "switch" version, depending on the compiler and the
756 CPU architecture.
757
758 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
759 because it would render the measurements invalid.
760
761
762 NOTE: care must be taken that the compiler doesn't try to "optimize" the
763 indirect jumps by sharing them between all opcodes. Such optimizations
764 can be disabled on gcc by using the -fno-gcse flag (or possibly
765 -fno-crossjumping).
766*/
767
Antoine Pitrou042b1282010-08-13 21:15:58 +0000768#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000769#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000770#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000771#endif
772
Antoine Pitrou042b1282010-08-13 21:15:58 +0000773#ifdef HAVE_COMPUTED_GOTOS
774 #ifndef USE_COMPUTED_GOTOS
775 #define USE_COMPUTED_GOTOS 1
776 #endif
777#else
778 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
779 #error "Computed gotos are not supported on this compiler."
780 #endif
781 #undef USE_COMPUTED_GOTOS
782 #define USE_COMPUTED_GOTOS 0
783#endif
784
785#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000786/* Import the static jump table */
787#include "opcode_targets.h"
788
Antoine Pitroub52ec782009-01-25 16:34:23 +0000789#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -0700790 op: \
791 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000792
Antoine Pitroub52ec782009-01-25 16:34:23 +0000793#ifdef LLTRACE
794#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 { \
Victor Stinner09532fe2019-05-10 23:39:09 +0200796 if (!lltrace && !_Py_TracingPossible(ceval) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300798 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300799 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 } \
801 goto fast_next_opcode; \
802 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000803#else
804#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 { \
Victor Stinner09532fe2019-05-10 23:39:09 +0200806 if (!_Py_TracingPossible(ceval) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300808 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300809 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 } \
811 goto fast_next_opcode; \
812 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000813#endif
814
Victor Stinner09532fe2019-05-10 23:39:09 +0200815#define DISPATCH() \
816 { \
817 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
818 FAST_DISPATCH(); \
819 } \
820 continue; \
821 }
822
Antoine Pitroub52ec782009-01-25 16:34:23 +0000823#else
Benjamin Petersonddd19492018-09-16 22:38:02 -0700824#define TARGET(op) op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000825#define FAST_DISPATCH() goto fast_next_opcode
Victor Stinner09532fe2019-05-10 23:39:09 +0200826#define DISPATCH() continue
Antoine Pitroub52ec782009-01-25 16:34:23 +0000827#endif
828
829
Neal Norwitza81d2202002-07-14 00:27:26 +0000830/* Tuple access macros */
831
832#ifndef Py_DEBUG
833#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
834#else
835#define GETITEM(v, i) PyTuple_GetItem((v), (i))
836#endif
837
Guido van Rossum374a9221991-04-04 10:40:29 +0000838/* Code access macros */
839
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300840/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600841#define INSTR_OFFSET() \
842 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300843#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300844 _Py_CODEUNIT word = *next_instr; \
845 opcode = _Py_OPCODE(word); \
846 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300847 next_instr++; \
848 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300849#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
850#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000851
Raymond Hettingerf606f872003-03-16 03:11:04 +0000852/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 Some opcodes tend to come in pairs thus making it possible to
854 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300855 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000856
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 Verifying the prediction costs a single high-speed test of a register
858 variable against a constant. If the pairing was good, then the
859 processor's own internal branch predication has a high likelihood of
860 success, resulting in a nearly zero-overhead transition to the
861 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300862 including its unpredictable switch-case branch. Combined with the
863 processor's internal branch prediction, a successful PREDICT has the
864 effect of making the two opcodes run as if they were a single new opcode
865 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000866
Georg Brandl86b2fb92008-07-16 03:43:04 +0000867 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 predictions turned-on and interpret the results as if some opcodes
869 had been combined or turn-off predictions so that the opcode frequency
870 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000871
872 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 the CPU to record separate branch prediction information for each
874 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000875
Raymond Hettingerf606f872003-03-16 03:11:04 +0000876*/
877
Antoine Pitrou042b1282010-08-13 21:15:58 +0000878#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000880#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300881#define PREDICT(op) \
882 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300883 _Py_CODEUNIT word = *next_instr; \
884 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300885 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300886 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300887 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300888 goto PRED_##op; \
889 } \
890 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000891#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300892#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000893
Raymond Hettingerf606f872003-03-16 03:11:04 +0000894
Guido van Rossum374a9221991-04-04 10:40:29 +0000895/* Stack manipulation macros */
896
Martin v. Löwis18e16552006-02-15 17:27:45 +0000897/* The stack can grow at most MAXINT deep, as co_nlocals and
898 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000899#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
900#define EMPTY() (STACK_LEVEL() == 0)
901#define TOP() (stack_pointer[-1])
902#define SECOND() (stack_pointer[-2])
903#define THIRD() (stack_pointer[-3])
904#define FOURTH() (stack_pointer[-4])
905#define PEEK(n) (stack_pointer[-(n)])
906#define SET_TOP(v) (stack_pointer[-1] = (v))
907#define SET_SECOND(v) (stack_pointer[-2] = (v))
908#define SET_THIRD(v) (stack_pointer[-3] = (v))
909#define SET_FOURTH(v) (stack_pointer[-4] = (v))
910#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
911#define BASIC_STACKADJ(n) (stack_pointer += n)
912#define BASIC_PUSH(v) (*stack_pointer++ = (v))
913#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000914
Guido van Rossum96a42c81992-01-12 02:29:51 +0000915#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +0200917 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000918 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +0200919#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000920 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +0000921#define STACK_GROW(n) do { \
922 assert(n >= 0); \
923 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +0200924 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +0000925 assert(STACK_LEVEL() <= co->co_stacksize); \
926 } while (0)
927#define STACK_SHRINK(n) do { \
928 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +0200929 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +0000930 (void)(BASIC_STACKADJ(-n)); \
931 assert(STACK_LEVEL() <= co->co_stacksize); \
932 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +0000933#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +0200934 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000935 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000936#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000937#define PUSH(v) BASIC_PUSH(v)
938#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +0000939#define STACK_GROW(n) BASIC_STACKADJ(n)
940#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000941#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000942#endif
943
Guido van Rossum681d79a1995-07-18 14:51:37 +0000944/* Local variable macros */
945
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000947
948/* The SETLOCAL() macro must not DECREF the local variable in-place and
949 then store the new value; it must copy the old value to a temporary
950 value, then store the new value, and then DECREF the temporary value.
951 This is because it is possible that during the DECREF the frame is
952 accessed by other code (e.g. a __del__ method or gc.collect()) and the
953 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000955 GETLOCAL(i) = value; \
956 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000957
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000958
959#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 while (STACK_LEVEL() > (b)->b_level) { \
961 PyObject *v = POP(); \
962 Py_XDECREF(v); \
963 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000964
965#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300966 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +0100968 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 assert(STACK_LEVEL() >= (b)->b_level + 3); \
970 while (STACK_LEVEL() > (b)->b_level + 3) { \
971 value = POP(); \
972 Py_XDECREF(value); \
973 } \
Mark Shannonae3087c2017-10-22 22:41:51 +0100974 exc_info = tstate->exc_info; \
975 type = exc_info->exc_type; \
976 value = exc_info->exc_value; \
977 traceback = exc_info->exc_traceback; \
978 exc_info->exc_type = POP(); \
979 exc_info->exc_value = POP(); \
980 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 Py_XDECREF(type); \
982 Py_XDECREF(value); \
983 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300984 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000985
Guido van Rossuma027efa1997-05-05 20:56:21 +0000986/* Start of code */
987
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 /* push frame */
989 if (Py_EnterRecursiveCall(""))
990 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000991
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000993
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 if (tstate->use_tracing) {
995 if (tstate->c_tracefunc != NULL) {
996 /* tstate->c_tracefunc, if defined, is a
997 function that will be called on *every* entry
998 to a code block. Its return value, if not
999 None, is a function that will be called at
1000 the start of each executed line of code.
1001 (Actually, the function must return itself
1002 in order to continue tracing.) The trace
1003 functions are called with three arguments:
1004 a pointer to the current frame, a string
1005 indicating why the function is called, and
1006 an argument which depends on the situation.
1007 The global trace function is also called
1008 whenever an exception is detected. */
1009 if (call_trace_protected(tstate->c_tracefunc,
1010 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001011 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 /* Trace function raised an error */
1013 goto exit_eval_frame;
1014 }
1015 }
1016 if (tstate->c_profilefunc != NULL) {
1017 /* Similar for c_profilefunc, except it needn't
1018 return itself and isn't called for "line" events */
1019 if (call_trace_protected(tstate->c_profilefunc,
1020 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001021 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 /* Profile function raised an error */
1023 goto exit_eval_frame;
1024 }
1025 }
1026 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001027
Łukasz Langaa785c872016-09-09 17:37:37 -07001028 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1029 dtrace_function_entry(f);
1030
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 co = f->f_code;
1032 names = co->co_names;
1033 consts = co->co_consts;
1034 fastlocals = f->f_localsplus;
1035 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001036 assert(PyBytes_Check(co->co_code));
1037 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001038 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1039 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1040 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001041 /*
1042 f->f_lasti refers to the index of the last instruction,
1043 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001044
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001045 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001046 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 When the PREDICT() macros are enabled, some opcode pairs follow in
1049 direct succession without updating f->f_lasti. A successful
1050 prediction effectively links the two codes together as if they
1051 were a single new opcode; accordingly,f->f_lasti will point to
1052 the first code in the pair (for instance, GET_ITER followed by
1053 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001054 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001056 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001057 next_instr = first_instr;
1058 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001059 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1060 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001061 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 stack_pointer = f->f_stacktop;
1063 assert(stack_pointer != NULL);
1064 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001065 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001066
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001067
Tim Peters5ca576e2001-06-18 22:08:13 +00001068#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001069 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001070#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001071
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001072 if (throwflag) /* support for generator.throw() */
1073 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001074
Victor Stinnerace47d72013-07-18 01:41:08 +02001075#ifdef Py_DEBUG
1076 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001077 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001078 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001079 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001080#endif
1081
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001082main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1085 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001086 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001087
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 /* Do periodic things. Doing this every time through
1089 the loop would add too much overhead, so we do it
1090 only every Nth instruction. We also do it if
1091 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1092 event needs attention (e.g. a signal handler or
1093 async I/O handler); see Py_AddPendingCall() and
1094 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001095
Eric Snow7bda9de2019-03-08 17:25:54 -07001096 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001097 opcode = _Py_OPCODE(*next_instr);
1098 if (opcode == SETUP_FINALLY ||
1099 opcode == SETUP_WITH ||
1100 opcode == BEFORE_ASYNC_WITH ||
1101 opcode == YIELD_FROM) {
1102 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001103 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001104 - If we're about to enter the 'with:'. It will prevent
1105 emitting a resource warning in the common idiom
1106 'with open(path) as file:'.
1107 - If we're about to enter the 'async with:'.
1108 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001109 *very* useful, but might help in some cases and it's
1110 traditional)
1111 - If we're resuming a chain of nested 'yield from' or
1112 'await' calls, then each frame is parked with YIELD_FROM
1113 as its next opcode. If the user hit control-C we want to
1114 wait until we've reached the innermost frame before
1115 running the signal handler and raising KeyboardInterrupt
1116 (see bpo-30039).
1117 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 goto fast_next_opcode;
1119 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001120
Victor Stinner09532fe2019-05-10 23:39:09 +02001121 if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
1122 if (handle_signals(runtime) != 0) {
Eric Snowfdf282d2019-01-11 14:26:55 -07001123 goto error;
1124 }
1125 }
Victor Stinner09532fe2019-05-10 23:39:09 +02001126 if (_Py_atomic_load_relaxed(&ceval->pending.calls_to_do)) {
1127 if (make_pending_calls(runtime) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001128 goto error;
Eric Snowfdf282d2019-01-11 14:26:55 -07001129 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001131
Victor Stinner09532fe2019-05-10 23:39:09 +02001132 if (_Py_atomic_load_relaxed(&ceval->gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 /* Give another thread a chance */
Victor Stinner09532fe2019-05-10 23:39:09 +02001134 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 Py_FatalError("ceval: tstate mix-up");
Victor Stinner09532fe2019-05-10 23:39:09 +02001136 }
1137 drop_gil(ceval, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138
1139 /* Other threads may run now */
1140
Victor Stinner09532fe2019-05-10 23:39:09 +02001141 take_gil(ceval, tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001142
1143 /* Check if we should make a quick exit. */
Victor Stinner09532fe2019-05-10 23:39:09 +02001144 exit_thread_if_finalizing(runtime, tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001145
Victor Stinner09532fe2019-05-10 23:39:09 +02001146 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 Py_FatalError("ceval: orphan tstate");
Victor Stinner09532fe2019-05-10 23:39:09 +02001148 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 }
1150 /* Check for asynchronous exceptions. */
1151 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001152 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 tstate->async_exc = NULL;
Victor Stinner09532fe2019-05-10 23:39:09 +02001154 UNSIGNAL_ASYNC_EXC(ceval);
Victor Stinner438a12d2019-05-24 17:01:38 +02001155 _PyErr_SetNone(tstate, exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001156 Py_DECREF(exc);
1157 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 }
1159 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001160
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 fast_next_opcode:
1162 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001163
Łukasz Langaa785c872016-09-09 17:37:37 -07001164 if (PyDTrace_LINE_ENABLED())
1165 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001168
Victor Stinner09532fe2019-05-10 23:39:09 +02001169 if (_Py_TracingPossible(ceval) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001170 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001171 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 /* see maybe_call_line_trace
1173 for expository comments */
1174 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001175
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001176 err = maybe_call_line_trace(tstate->c_tracefunc,
1177 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001178 tstate, f,
1179 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 /* Reload possibly changed frame fields */
1181 JUMPTO(f->f_lasti);
1182 if (f->f_stacktop != NULL) {
1183 stack_pointer = f->f_stacktop;
1184 f->f_stacktop = NULL;
1185 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001186 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001188 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001192
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001193 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001194 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001195#ifdef DYNAMIC_EXECUTION_PROFILE
1196#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001197 dxpairs[lastopcode][opcode]++;
1198 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001199#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001201#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001202
Guido van Rossum96a42c81992-01-12 02:29:51 +00001203#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001205
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 if (lltrace) {
1207 if (HAS_ARG(opcode)) {
1208 printf("%d: %d, %d\n",
1209 f->f_lasti, opcode, oparg);
1210 }
1211 else {
1212 printf("%d: %d\n",
1213 f->f_lasti, opcode);
1214 }
1215 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001216#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001219
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001221 It is essential that any operation that fails must goto error
1222 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001223
Benjamin Petersonddd19492018-09-16 22:38:02 -07001224 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001226 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001227
Benjamin Petersonddd19492018-09-16 22:38:02 -07001228 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001229 PyObject *value = GETLOCAL(oparg);
1230 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001231 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001232 UNBOUNDLOCAL_ERROR_MSG,
1233 PyTuple_GetItem(co->co_varnames, oparg));
1234 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001236 Py_INCREF(value);
1237 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001238 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001239 }
1240
Benjamin Petersonddd19492018-09-16 22:38:02 -07001241 case TARGET(LOAD_CONST): {
1242 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001243 PyObject *value = GETITEM(consts, oparg);
1244 Py_INCREF(value);
1245 PUSH(value);
1246 FAST_DISPATCH();
1247 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001248
Benjamin Petersonddd19492018-09-16 22:38:02 -07001249 case TARGET(STORE_FAST): {
1250 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001251 PyObject *value = POP();
1252 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001254 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001255
Benjamin Petersonddd19492018-09-16 22:38:02 -07001256 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001257 PyObject *value = POP();
1258 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001260 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001261
Benjamin Petersonddd19492018-09-16 22:38:02 -07001262 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001263 PyObject *top = TOP();
1264 PyObject *second = SECOND();
1265 SET_TOP(second);
1266 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001268 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001269
Benjamin Petersonddd19492018-09-16 22:38:02 -07001270 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001271 PyObject *top = TOP();
1272 PyObject *second = SECOND();
1273 PyObject *third = THIRD();
1274 SET_TOP(second);
1275 SET_SECOND(third);
1276 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001278 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001279
Benjamin Petersonddd19492018-09-16 22:38:02 -07001280 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001281 PyObject *top = TOP();
1282 PyObject *second = SECOND();
1283 PyObject *third = THIRD();
1284 PyObject *fourth = FOURTH();
1285 SET_TOP(second);
1286 SET_SECOND(third);
1287 SET_THIRD(fourth);
1288 SET_FOURTH(top);
1289 FAST_DISPATCH();
1290 }
1291
Benjamin Petersonddd19492018-09-16 22:38:02 -07001292 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001293 PyObject *top = TOP();
1294 Py_INCREF(top);
1295 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001297 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001298
Benjamin Petersonddd19492018-09-16 22:38:02 -07001299 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001300 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001301 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001302 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001303 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001304 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001305 SET_TOP(top);
1306 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001307 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001308 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001309
Benjamin Petersonddd19492018-09-16 22:38:02 -07001310 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001311 PyObject *value = TOP();
1312 PyObject *res = PyNumber_Positive(value);
1313 Py_DECREF(value);
1314 SET_TOP(res);
1315 if (res == NULL)
1316 goto error;
1317 DISPATCH();
1318 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001319
Benjamin Petersonddd19492018-09-16 22:38:02 -07001320 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001321 PyObject *value = TOP();
1322 PyObject *res = PyNumber_Negative(value);
1323 Py_DECREF(value);
1324 SET_TOP(res);
1325 if (res == NULL)
1326 goto error;
1327 DISPATCH();
1328 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001329
Benjamin Petersonddd19492018-09-16 22:38:02 -07001330 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001331 PyObject *value = TOP();
1332 int err = PyObject_IsTrue(value);
1333 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 if (err == 0) {
1335 Py_INCREF(Py_True);
1336 SET_TOP(Py_True);
1337 DISPATCH();
1338 }
1339 else if (err > 0) {
1340 Py_INCREF(Py_False);
1341 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 DISPATCH();
1343 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001344 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001345 goto error;
1346 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001347
Benjamin Petersonddd19492018-09-16 22:38:02 -07001348 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001349 PyObject *value = TOP();
1350 PyObject *res = PyNumber_Invert(value);
1351 Py_DECREF(value);
1352 SET_TOP(res);
1353 if (res == NULL)
1354 goto error;
1355 DISPATCH();
1356 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001357
Benjamin Petersonddd19492018-09-16 22:38:02 -07001358 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001359 PyObject *exp = POP();
1360 PyObject *base = TOP();
1361 PyObject *res = PyNumber_Power(base, exp, Py_None);
1362 Py_DECREF(base);
1363 Py_DECREF(exp);
1364 SET_TOP(res);
1365 if (res == NULL)
1366 goto error;
1367 DISPATCH();
1368 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001369
Benjamin Petersonddd19492018-09-16 22:38:02 -07001370 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001371 PyObject *right = POP();
1372 PyObject *left = TOP();
1373 PyObject *res = PyNumber_Multiply(left, right);
1374 Py_DECREF(left);
1375 Py_DECREF(right);
1376 SET_TOP(res);
1377 if (res == NULL)
1378 goto error;
1379 DISPATCH();
1380 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001381
Benjamin Petersonddd19492018-09-16 22:38:02 -07001382 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001383 PyObject *right = POP();
1384 PyObject *left = TOP();
1385 PyObject *res = PyNumber_MatrixMultiply(left, right);
1386 Py_DECREF(left);
1387 Py_DECREF(right);
1388 SET_TOP(res);
1389 if (res == NULL)
1390 goto error;
1391 DISPATCH();
1392 }
1393
Benjamin Petersonddd19492018-09-16 22:38:02 -07001394 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001395 PyObject *divisor = POP();
1396 PyObject *dividend = TOP();
1397 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1398 Py_DECREF(dividend);
1399 Py_DECREF(divisor);
1400 SET_TOP(quotient);
1401 if (quotient == NULL)
1402 goto error;
1403 DISPATCH();
1404 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001405
Benjamin Petersonddd19492018-09-16 22:38:02 -07001406 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001407 PyObject *divisor = POP();
1408 PyObject *dividend = TOP();
1409 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1410 Py_DECREF(dividend);
1411 Py_DECREF(divisor);
1412 SET_TOP(quotient);
1413 if (quotient == NULL)
1414 goto error;
1415 DISPATCH();
1416 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001417
Benjamin Petersonddd19492018-09-16 22:38:02 -07001418 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001419 PyObject *divisor = POP();
1420 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001421 PyObject *res;
1422 if (PyUnicode_CheckExact(dividend) && (
1423 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1424 // fast path; string formatting, but not if the RHS is a str subclass
1425 // (see issue28598)
1426 res = PyUnicode_Format(dividend, divisor);
1427 } else {
1428 res = PyNumber_Remainder(dividend, divisor);
1429 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001430 Py_DECREF(divisor);
1431 Py_DECREF(dividend);
1432 SET_TOP(res);
1433 if (res == NULL)
1434 goto error;
1435 DISPATCH();
1436 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001437
Benjamin Petersonddd19492018-09-16 22:38:02 -07001438 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001439 PyObject *right = POP();
1440 PyObject *left = TOP();
1441 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001442 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1443 CPython using bytecode, it is simply worthless.
1444 See http://bugs.python.org/issue21955 and
1445 http://bugs.python.org/issue10044 for the discussion. In short,
1446 no patch shown any impact on a realistic benchmark, only a minor
1447 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001448 if (PyUnicode_CheckExact(left) &&
1449 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001450 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001451 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001452 }
1453 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001454 sum = PyNumber_Add(left, right);
1455 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001456 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001457 Py_DECREF(right);
1458 SET_TOP(sum);
1459 if (sum == NULL)
1460 goto error;
1461 DISPATCH();
1462 }
1463
Benjamin Petersonddd19492018-09-16 22:38:02 -07001464 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001465 PyObject *right = POP();
1466 PyObject *left = TOP();
1467 PyObject *diff = PyNumber_Subtract(left, right);
1468 Py_DECREF(right);
1469 Py_DECREF(left);
1470 SET_TOP(diff);
1471 if (diff == NULL)
1472 goto error;
1473 DISPATCH();
1474 }
1475
Benjamin Petersonddd19492018-09-16 22:38:02 -07001476 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001477 PyObject *sub = POP();
1478 PyObject *container = TOP();
1479 PyObject *res = PyObject_GetItem(container, sub);
1480 Py_DECREF(container);
1481 Py_DECREF(sub);
1482 SET_TOP(res);
1483 if (res == NULL)
1484 goto error;
1485 DISPATCH();
1486 }
1487
Benjamin Petersonddd19492018-09-16 22:38:02 -07001488 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001489 PyObject *right = POP();
1490 PyObject *left = TOP();
1491 PyObject *res = PyNumber_Lshift(left, right);
1492 Py_DECREF(left);
1493 Py_DECREF(right);
1494 SET_TOP(res);
1495 if (res == NULL)
1496 goto error;
1497 DISPATCH();
1498 }
1499
Benjamin Petersonddd19492018-09-16 22:38:02 -07001500 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001501 PyObject *right = POP();
1502 PyObject *left = TOP();
1503 PyObject *res = PyNumber_Rshift(left, right);
1504 Py_DECREF(left);
1505 Py_DECREF(right);
1506 SET_TOP(res);
1507 if (res == NULL)
1508 goto error;
1509 DISPATCH();
1510 }
1511
Benjamin Petersonddd19492018-09-16 22:38:02 -07001512 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001513 PyObject *right = POP();
1514 PyObject *left = TOP();
1515 PyObject *res = PyNumber_And(left, right);
1516 Py_DECREF(left);
1517 Py_DECREF(right);
1518 SET_TOP(res);
1519 if (res == NULL)
1520 goto error;
1521 DISPATCH();
1522 }
1523
Benjamin Petersonddd19492018-09-16 22:38:02 -07001524 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001525 PyObject *right = POP();
1526 PyObject *left = TOP();
1527 PyObject *res = PyNumber_Xor(left, right);
1528 Py_DECREF(left);
1529 Py_DECREF(right);
1530 SET_TOP(res);
1531 if (res == NULL)
1532 goto error;
1533 DISPATCH();
1534 }
1535
Benjamin Petersonddd19492018-09-16 22:38:02 -07001536 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001537 PyObject *right = POP();
1538 PyObject *left = TOP();
1539 PyObject *res = PyNumber_Or(left, right);
1540 Py_DECREF(left);
1541 Py_DECREF(right);
1542 SET_TOP(res);
1543 if (res == NULL)
1544 goto error;
1545 DISPATCH();
1546 }
1547
Benjamin Petersonddd19492018-09-16 22:38:02 -07001548 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001549 PyObject *v = POP();
1550 PyObject *list = PEEK(oparg);
1551 int err;
1552 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001553 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001554 if (err != 0)
1555 goto error;
1556 PREDICT(JUMP_ABSOLUTE);
1557 DISPATCH();
1558 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001559
Benjamin Petersonddd19492018-09-16 22:38:02 -07001560 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001561 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001562 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001563 int err;
1564 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001565 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001566 if (err != 0)
1567 goto error;
1568 PREDICT(JUMP_ABSOLUTE);
1569 DISPATCH();
1570 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001571
Benjamin Petersonddd19492018-09-16 22:38:02 -07001572 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001573 PyObject *exp = POP();
1574 PyObject *base = TOP();
1575 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1576 Py_DECREF(base);
1577 Py_DECREF(exp);
1578 SET_TOP(res);
1579 if (res == NULL)
1580 goto error;
1581 DISPATCH();
1582 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001583
Benjamin Petersonddd19492018-09-16 22:38:02 -07001584 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001585 PyObject *right = POP();
1586 PyObject *left = TOP();
1587 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1588 Py_DECREF(left);
1589 Py_DECREF(right);
1590 SET_TOP(res);
1591 if (res == NULL)
1592 goto error;
1593 DISPATCH();
1594 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001595
Benjamin Petersonddd19492018-09-16 22:38:02 -07001596 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001597 PyObject *right = POP();
1598 PyObject *left = TOP();
1599 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1600 Py_DECREF(left);
1601 Py_DECREF(right);
1602 SET_TOP(res);
1603 if (res == NULL)
1604 goto error;
1605 DISPATCH();
1606 }
1607
Benjamin Petersonddd19492018-09-16 22:38:02 -07001608 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001609 PyObject *divisor = POP();
1610 PyObject *dividend = TOP();
1611 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1612 Py_DECREF(dividend);
1613 Py_DECREF(divisor);
1614 SET_TOP(quotient);
1615 if (quotient == NULL)
1616 goto error;
1617 DISPATCH();
1618 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001619
Benjamin Petersonddd19492018-09-16 22:38:02 -07001620 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001621 PyObject *divisor = POP();
1622 PyObject *dividend = TOP();
1623 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1624 Py_DECREF(dividend);
1625 Py_DECREF(divisor);
1626 SET_TOP(quotient);
1627 if (quotient == NULL)
1628 goto error;
1629 DISPATCH();
1630 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001631
Benjamin Petersonddd19492018-09-16 22:38:02 -07001632 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001633 PyObject *right = POP();
1634 PyObject *left = TOP();
1635 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1636 Py_DECREF(left);
1637 Py_DECREF(right);
1638 SET_TOP(mod);
1639 if (mod == NULL)
1640 goto error;
1641 DISPATCH();
1642 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001643
Benjamin Petersonddd19492018-09-16 22:38:02 -07001644 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001645 PyObject *right = POP();
1646 PyObject *left = TOP();
1647 PyObject *sum;
1648 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001649 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001650 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001651 }
1652 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001653 sum = PyNumber_InPlaceAdd(left, right);
1654 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001655 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001656 Py_DECREF(right);
1657 SET_TOP(sum);
1658 if (sum == NULL)
1659 goto error;
1660 DISPATCH();
1661 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001662
Benjamin Petersonddd19492018-09-16 22:38:02 -07001663 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001664 PyObject *right = POP();
1665 PyObject *left = TOP();
1666 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1667 Py_DECREF(left);
1668 Py_DECREF(right);
1669 SET_TOP(diff);
1670 if (diff == NULL)
1671 goto error;
1672 DISPATCH();
1673 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001674
Benjamin Petersonddd19492018-09-16 22:38:02 -07001675 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001676 PyObject *right = POP();
1677 PyObject *left = TOP();
1678 PyObject *res = PyNumber_InPlaceLshift(left, right);
1679 Py_DECREF(left);
1680 Py_DECREF(right);
1681 SET_TOP(res);
1682 if (res == NULL)
1683 goto error;
1684 DISPATCH();
1685 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001686
Benjamin Petersonddd19492018-09-16 22:38:02 -07001687 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001688 PyObject *right = POP();
1689 PyObject *left = TOP();
1690 PyObject *res = PyNumber_InPlaceRshift(left, right);
1691 Py_DECREF(left);
1692 Py_DECREF(right);
1693 SET_TOP(res);
1694 if (res == NULL)
1695 goto error;
1696 DISPATCH();
1697 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001698
Benjamin Petersonddd19492018-09-16 22:38:02 -07001699 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001700 PyObject *right = POP();
1701 PyObject *left = TOP();
1702 PyObject *res = PyNumber_InPlaceAnd(left, right);
1703 Py_DECREF(left);
1704 Py_DECREF(right);
1705 SET_TOP(res);
1706 if (res == NULL)
1707 goto error;
1708 DISPATCH();
1709 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001710
Benjamin Petersonddd19492018-09-16 22:38:02 -07001711 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001712 PyObject *right = POP();
1713 PyObject *left = TOP();
1714 PyObject *res = PyNumber_InPlaceXor(left, right);
1715 Py_DECREF(left);
1716 Py_DECREF(right);
1717 SET_TOP(res);
1718 if (res == NULL)
1719 goto error;
1720 DISPATCH();
1721 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001722
Benjamin Petersonddd19492018-09-16 22:38:02 -07001723 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001724 PyObject *right = POP();
1725 PyObject *left = TOP();
1726 PyObject *res = PyNumber_InPlaceOr(left, right);
1727 Py_DECREF(left);
1728 Py_DECREF(right);
1729 SET_TOP(res);
1730 if (res == NULL)
1731 goto error;
1732 DISPATCH();
1733 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001734
Benjamin Petersonddd19492018-09-16 22:38:02 -07001735 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001736 PyObject *sub = TOP();
1737 PyObject *container = SECOND();
1738 PyObject *v = THIRD();
1739 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001740 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001741 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001742 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001743 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001744 Py_DECREF(container);
1745 Py_DECREF(sub);
1746 if (err != 0)
1747 goto error;
1748 DISPATCH();
1749 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001750
Benjamin Petersonddd19492018-09-16 22:38:02 -07001751 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001752 PyObject *sub = TOP();
1753 PyObject *container = SECOND();
1754 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001755 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00001756 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001757 err = PyObject_DelItem(container, sub);
1758 Py_DECREF(container);
1759 Py_DECREF(sub);
1760 if (err != 0)
1761 goto error;
1762 DISPATCH();
1763 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001764
Benjamin Petersonddd19492018-09-16 22:38:02 -07001765 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01001766 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001767 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001768 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001769 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001770 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001771 _PyErr_SetString(tstate, PyExc_RuntimeError,
1772 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001773 Py_DECREF(value);
1774 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001775 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001776 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001777 Py_DECREF(value);
1778 if (res == NULL)
1779 goto error;
1780 Py_DECREF(res);
1781 DISPATCH();
1782 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001783
Benjamin Petersonddd19492018-09-16 22:38:02 -07001784 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001785 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001786 switch (oparg) {
1787 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001788 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001789 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001790 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001791 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001792 /* fall through */
1793 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02001794 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001795 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001796 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001797 break;
1798 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02001799 _PyErr_SetString(tstate, PyExc_SystemError,
1800 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001801 break;
1802 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001803 goto error;
1804 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001805
Benjamin Petersonddd19492018-09-16 22:38:02 -07001806 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001807 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001808 assert(f->f_iblock == 0);
Pablo Galindof00828a2019-05-09 16:52:02 +01001809 goto exit_returning;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001810 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001811
Benjamin Petersonddd19492018-09-16 22:38:02 -07001812 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001813 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001814 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001815 PyObject *obj = TOP();
1816 PyTypeObject *type = Py_TYPE(obj);
1817
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001818 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001819 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001820 }
Yury Selivanov75445082015-05-11 22:57:16 -04001821
1822 if (getter != NULL) {
1823 iter = (*getter)(obj);
1824 Py_DECREF(obj);
1825 if (iter == NULL) {
1826 SET_TOP(NULL);
1827 goto error;
1828 }
1829 }
1830 else {
1831 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02001832 _PyErr_Format(tstate, PyExc_TypeError,
1833 "'async for' requires an object with "
1834 "__aiter__ method, got %.100s",
1835 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001836 Py_DECREF(obj);
1837 goto error;
1838 }
1839
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001840 if (Py_TYPE(iter)->tp_as_async == NULL ||
1841 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001842
Yury Selivanov398ff912017-03-02 22:20:00 -05001843 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02001844 _PyErr_Format(tstate, PyExc_TypeError,
1845 "'async for' received an object from __aiter__ "
1846 "that does not implement __anext__: %.100s",
1847 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001848 Py_DECREF(iter);
1849 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001850 }
1851
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001852 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001853 DISPATCH();
1854 }
1855
Benjamin Petersonddd19492018-09-16 22:38:02 -07001856 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001857 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001858 PyObject *next_iter = NULL;
1859 PyObject *awaitable = NULL;
1860 PyObject *aiter = TOP();
1861 PyTypeObject *type = Py_TYPE(aiter);
1862
Yury Selivanoveb636452016-09-08 22:01:51 -07001863 if (PyAsyncGen_CheckExact(aiter)) {
1864 awaitable = type->tp_as_async->am_anext(aiter);
1865 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001866 goto error;
1867 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001868 } else {
1869 if (type->tp_as_async != NULL){
1870 getter = type->tp_as_async->am_anext;
1871 }
Yury Selivanov75445082015-05-11 22:57:16 -04001872
Yury Selivanoveb636452016-09-08 22:01:51 -07001873 if (getter != NULL) {
1874 next_iter = (*getter)(aiter);
1875 if (next_iter == NULL) {
1876 goto error;
1877 }
1878 }
1879 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02001880 _PyErr_Format(tstate, PyExc_TypeError,
1881 "'async for' requires an iterator with "
1882 "__anext__ method, got %.100s",
1883 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07001884 goto error;
1885 }
Yury Selivanov75445082015-05-11 22:57:16 -04001886
Yury Selivanoveb636452016-09-08 22:01:51 -07001887 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1888 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001889 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001890 PyExc_TypeError,
1891 "'async for' received an invalid object "
1892 "from __anext__: %.100s",
1893 Py_TYPE(next_iter)->tp_name);
1894
1895 Py_DECREF(next_iter);
1896 goto error;
1897 } else {
1898 Py_DECREF(next_iter);
1899 }
1900 }
Yury Selivanov75445082015-05-11 22:57:16 -04001901
1902 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001903 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001904 DISPATCH();
1905 }
1906
Benjamin Petersonddd19492018-09-16 22:38:02 -07001907 case TARGET(GET_AWAITABLE): {
1908 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04001909 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04001910 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04001911
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03001912 if (iter == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001913 format_awaitable_error(tstate, Py_TYPE(iterable),
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03001914 _Py_OPCODE(next_instr[-2]));
1915 }
1916
Yury Selivanov75445082015-05-11 22:57:16 -04001917 Py_DECREF(iterable);
1918
Yury Selivanovc724bae2016-03-02 11:30:46 -05001919 if (iter != NULL && PyCoro_CheckExact(iter)) {
1920 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
1921 if (yf != NULL) {
1922 /* `iter` is a coroutine object that is being
1923 awaited, `yf` is a pointer to the current awaitable
1924 being awaited on. */
1925 Py_DECREF(yf);
1926 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02001927 _PyErr_SetString(tstate, PyExc_RuntimeError,
1928 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05001929 /* The code below jumps to `error` if `iter` is NULL. */
1930 }
1931 }
1932
Yury Selivanov75445082015-05-11 22:57:16 -04001933 SET_TOP(iter); /* Even if it's NULL */
1934
1935 if (iter == NULL) {
1936 goto error;
1937 }
1938
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001939 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001940 DISPATCH();
1941 }
1942
Benjamin Petersonddd19492018-09-16 22:38:02 -07001943 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001944 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001945 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001946 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001947 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
1948 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001949 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001950 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001951 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001952 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001953 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001954 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001955 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001956 Py_DECREF(v);
1957 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001958 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08001959 if (tstate->c_tracefunc != NULL
Victor Stinner438a12d2019-05-24 17:01:38 +02001960 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001961 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10001962 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001963 if (err < 0)
1964 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001965 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001966 SET_TOP(val);
1967 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001968 }
Martin Panter95f53c12016-07-18 08:23:26 +00001969 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001970 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001971 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01001972 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03001973 f->f_lasti -= sizeof(_Py_CODEUNIT);
Pablo Galindof00828a2019-05-09 16:52:02 +01001974 goto exit_yielding;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001975 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001976
Benjamin Petersonddd19492018-09-16 22:38:02 -07001977 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001978 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07001979
1980 if (co->co_flags & CO_ASYNC_GENERATOR) {
1981 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
1982 Py_DECREF(retval);
1983 if (w == NULL) {
1984 retval = NULL;
1985 goto error;
1986 }
1987 retval = w;
1988 }
1989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001990 f->f_stacktop = stack_pointer;
Pablo Galindof00828a2019-05-09 16:52:02 +01001991 goto exit_yielding;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001992 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001993
Benjamin Petersonddd19492018-09-16 22:38:02 -07001994 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001995 PyObject *type, *value, *traceback;
1996 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001997 PyTryBlock *b = PyFrame_BlockPop(f);
1998 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001999 _PyErr_SetString(tstate, PyExc_SystemError,
2000 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002001 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002002 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002003 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2004 STACK_LEVEL() <= (b)->b_level + 4);
2005 exc_info = tstate->exc_info;
2006 type = exc_info->exc_type;
2007 value = exc_info->exc_value;
2008 traceback = exc_info->exc_traceback;
2009 exc_info->exc_type = POP();
2010 exc_info->exc_value = POP();
2011 exc_info->exc_traceback = POP();
2012 Py_XDECREF(type);
2013 Py_XDECREF(value);
2014 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002015 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002016 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002017
Benjamin Petersonddd19492018-09-16 22:38:02 -07002018 case TARGET(POP_BLOCK): {
2019 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002020 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002021 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002022 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002023
Benjamin Petersonddd19492018-09-16 22:38:02 -07002024 case TARGET(POP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002025 /* If oparg is 0 at the top of the stack are 1 or 6 values:
2026 Either:
2027 - TOP = NULL or an integer
2028 or:
2029 - (TOP, SECOND, THIRD) = exc_info()
2030 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2031
2032 If oparg is 1 the value for 'return' was additionally pushed
2033 at the top of the stack.
2034 */
2035 PyObject *res = NULL;
2036 if (oparg) {
2037 res = POP();
2038 }
2039 PyObject *exc = POP();
2040 if (exc == NULL || PyLong_CheckExact(exc)) {
2041 Py_XDECREF(exc);
2042 }
2043 else {
2044 Py_DECREF(exc);
2045 Py_DECREF(POP());
2046 Py_DECREF(POP());
2047
2048 PyObject *type, *value, *traceback;
2049 _PyErr_StackItem *exc_info;
2050 PyTryBlock *b = PyFrame_BlockPop(f);
2051 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002052 _PyErr_SetString(tstate, PyExc_SystemError,
2053 "popped block is not an except handler");
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002054 Py_XDECREF(res);
2055 goto error;
2056 }
2057 assert(STACK_LEVEL() == (b)->b_level + 3);
2058 exc_info = tstate->exc_info;
2059 type = exc_info->exc_type;
2060 value = exc_info->exc_value;
2061 traceback = exc_info->exc_traceback;
2062 exc_info->exc_type = POP();
2063 exc_info->exc_value = POP();
2064 exc_info->exc_traceback = POP();
2065 Py_XDECREF(type);
2066 Py_XDECREF(value);
2067 Py_XDECREF(traceback);
2068 }
2069 if (oparg) {
2070 PUSH(res);
2071 }
2072 DISPATCH();
2073 }
2074
Benjamin Petersonddd19492018-09-16 22:38:02 -07002075 case TARGET(CALL_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002076 PyObject *ret = PyLong_FromLong(INSTR_OFFSET());
2077 if (ret == NULL) {
2078 goto error;
2079 }
2080 PUSH(ret);
2081 JUMPBY(oparg);
2082 FAST_DISPATCH();
2083 }
2084
Benjamin Petersonddd19492018-09-16 22:38:02 -07002085 case TARGET(BEGIN_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002086 /* Push NULL onto the stack for using it in END_FINALLY,
2087 POP_FINALLY, WITH_CLEANUP_START and WITH_CLEANUP_FINISH.
2088 */
2089 PUSH(NULL);
2090 FAST_DISPATCH();
2091 }
2092
Benjamin Petersonddd19492018-09-16 22:38:02 -07002093 case TARGET(END_FINALLY): {
2094 PREDICTED(END_FINALLY);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002095 /* At the top of the stack are 1 or 6 values:
2096 Either:
2097 - TOP = NULL or an integer
2098 or:
2099 - (TOP, SECOND, THIRD) = exc_info()
2100 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2101 */
2102 PyObject *exc = POP();
2103 if (exc == NULL) {
2104 FAST_DISPATCH();
2105 }
2106 else if (PyLong_CheckExact(exc)) {
2107 int ret = _PyLong_AsInt(exc);
2108 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02002109 if (ret == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002110 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002111 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002112 JUMPTO(ret);
2113 FAST_DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002114 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002115 else {
2116 assert(PyExceptionClass_Check(exc));
2117 PyObject *val = POP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002118 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002119 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002120 goto exception_unwind;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002121 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002122 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002123
Benjamin Petersonddd19492018-09-16 22:38:02 -07002124 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002125 PyObject *exc = POP();
2126 assert(PyExceptionClass_Check(exc));
2127 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2128 PyTryBlock *b = PyFrame_BlockPop(f);
2129 assert(b->b_type == EXCEPT_HANDLER);
2130 Py_DECREF(exc);
2131 UNWIND_EXCEPT_HANDLER(b);
2132 Py_DECREF(POP());
2133 JUMPBY(oparg);
2134 FAST_DISPATCH();
2135 }
2136 else {
2137 PyObject *val = POP();
2138 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002139 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002140 goto exception_unwind;
2141 }
2142 }
2143
Benjamin Petersonddd19492018-09-16 22:38:02 -07002144 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002145 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002146
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002147 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002148 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002149 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002150 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002151 if (!_PyErr_Occurred(tstate)) {
2152 _PyErr_SetString(tstate, PyExc_NameError,
2153 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002154 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002155 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002156 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002157 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002158 }
2159 else {
2160 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2161 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002162 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002163 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2164 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002165 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2166 _PyErr_SetString(tstate, PyExc_NameError,
2167 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002168 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002169 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002170 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002171 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002172 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002173 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002174
Benjamin Petersonddd19492018-09-16 22:38:02 -07002175 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002176 PyObject *name = GETITEM(names, oparg);
2177 PyObject *v = POP();
2178 PyObject *ns = f->f_locals;
2179 int err;
2180 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002181 _PyErr_Format(tstate, PyExc_SystemError,
2182 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002183 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002184 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002185 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002186 if (PyDict_CheckExact(ns))
2187 err = PyDict_SetItem(ns, name, v);
2188 else
2189 err = PyObject_SetItem(ns, name, v);
2190 Py_DECREF(v);
2191 if (err != 0)
2192 goto error;
2193 DISPATCH();
2194 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002195
Benjamin Petersonddd19492018-09-16 22:38:02 -07002196 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002197 PyObject *name = GETITEM(names, oparg);
2198 PyObject *ns = f->f_locals;
2199 int err;
2200 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002201 _PyErr_Format(tstate, PyExc_SystemError,
2202 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002203 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002204 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002205 err = PyObject_DelItem(ns, name);
2206 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002207 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002208 NAME_ERROR_MSG,
2209 name);
2210 goto error;
2211 }
2212 DISPATCH();
2213 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002214
Benjamin Petersonddd19492018-09-16 22:38:02 -07002215 case TARGET(UNPACK_SEQUENCE): {
2216 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002217 PyObject *seq = POP(), *item, **items;
2218 if (PyTuple_CheckExact(seq) &&
2219 PyTuple_GET_SIZE(seq) == oparg) {
2220 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002221 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002222 item = items[oparg];
2223 Py_INCREF(item);
2224 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002225 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002226 } else if (PyList_CheckExact(seq) &&
2227 PyList_GET_SIZE(seq) == oparg) {
2228 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002229 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002230 item = items[oparg];
2231 Py_INCREF(item);
2232 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002233 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002234 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002235 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002236 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002237 } else {
2238 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002239 Py_DECREF(seq);
2240 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002241 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002242 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002243 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002244 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002245
Benjamin Petersonddd19492018-09-16 22:38:02 -07002246 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002247 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2248 PyObject *seq = POP();
2249
Victor Stinner438a12d2019-05-24 17:01:38 +02002250 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002251 stack_pointer + totalargs)) {
2252 stack_pointer += totalargs;
2253 } else {
2254 Py_DECREF(seq);
2255 goto error;
2256 }
2257 Py_DECREF(seq);
2258 DISPATCH();
2259 }
2260
Benjamin Petersonddd19492018-09-16 22:38:02 -07002261 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002262 PyObject *name = GETITEM(names, oparg);
2263 PyObject *owner = TOP();
2264 PyObject *v = SECOND();
2265 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002266 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002267 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002268 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002269 Py_DECREF(owner);
2270 if (err != 0)
2271 goto error;
2272 DISPATCH();
2273 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002274
Benjamin Petersonddd19492018-09-16 22:38:02 -07002275 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002276 PyObject *name = GETITEM(names, oparg);
2277 PyObject *owner = POP();
2278 int err;
2279 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2280 Py_DECREF(owner);
2281 if (err != 0)
2282 goto error;
2283 DISPATCH();
2284 }
2285
Benjamin Petersonddd19492018-09-16 22:38:02 -07002286 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002287 PyObject *name = GETITEM(names, oparg);
2288 PyObject *v = POP();
2289 int err;
2290 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002291 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002292 if (err != 0)
2293 goto error;
2294 DISPATCH();
2295 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002296
Benjamin Petersonddd19492018-09-16 22:38:02 -07002297 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002298 PyObject *name = GETITEM(names, oparg);
2299 int err;
2300 err = PyDict_DelItem(f->f_globals, name);
2301 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002302 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2303 format_exc_check_arg(tstate, PyExc_NameError,
2304 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002305 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002306 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002307 }
2308 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002309 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002310
Benjamin Petersonddd19492018-09-16 22:38:02 -07002311 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002312 PyObject *name = GETITEM(names, oparg);
2313 PyObject *locals = f->f_locals;
2314 PyObject *v;
2315 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002316 _PyErr_Format(tstate, PyExc_SystemError,
2317 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002318 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002319 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002320 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002321 v = PyDict_GetItemWithError(locals, name);
2322 if (v != NULL) {
2323 Py_INCREF(v);
2324 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002325 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002326 goto error;
2327 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002328 }
2329 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002330 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002331 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002332 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002333 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002334 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002335 }
2336 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002337 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002338 v = PyDict_GetItemWithError(f->f_globals, name);
2339 if (v != NULL) {
2340 Py_INCREF(v);
2341 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002342 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002343 goto error;
2344 }
2345 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002346 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002347 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002348 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002349 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002350 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002351 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002352 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002353 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002354 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002355 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002356 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002357 }
2358 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002359 v = PyObject_GetItem(f->f_builtins, name);
2360 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002361 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002362 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002363 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002364 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002365 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002366 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002367 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002368 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002369 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002370 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002371 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002372 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002373 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002374
Benjamin Petersonddd19492018-09-16 22:38:02 -07002375 case TARGET(LOAD_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002376 PyObject *name = GETITEM(names, oparg);
2377 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002378 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002379 && PyDict_CheckExact(f->f_builtins))
2380 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002381 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002382 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002383 name);
2384 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002385 if (!_PyErr_OCCURRED()) {
2386 /* _PyDict_LoadGlobal() returns NULL without raising
2387 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002388 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002389 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002390 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002391 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002392 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002393 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002394 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002395 else {
2396 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002397
2398 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002399 v = PyObject_GetItem(f->f_globals, name);
2400 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002401 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002402 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002403 }
2404 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002405
Victor Stinnerb4efc962015-11-20 09:24:02 +01002406 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002407 v = PyObject_GetItem(f->f_builtins, name);
2408 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002409 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002410 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002411 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002412 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002413 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002414 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002415 }
2416 }
2417 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002418 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002419 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002420 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002421
Benjamin Petersonddd19492018-09-16 22:38:02 -07002422 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002423 PyObject *v = GETLOCAL(oparg);
2424 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002425 SETLOCAL(oparg, NULL);
2426 DISPATCH();
2427 }
2428 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002429 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002430 UNBOUNDLOCAL_ERROR_MSG,
2431 PyTuple_GetItem(co->co_varnames, oparg)
2432 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002433 goto error;
2434 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002435
Benjamin Petersonddd19492018-09-16 22:38:02 -07002436 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002437 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002438 PyObject *oldobj = PyCell_GET(cell);
2439 if (oldobj != NULL) {
2440 PyCell_SET(cell, NULL);
2441 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002442 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002443 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002444 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002445 goto error;
2446 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002447
Benjamin Petersonddd19492018-09-16 22:38:02 -07002448 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002449 PyObject *cell = freevars[oparg];
2450 Py_INCREF(cell);
2451 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002452 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002453 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002454
Benjamin Petersonddd19492018-09-16 22:38:02 -07002455 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002456 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002457 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002458 assert(locals);
2459 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2460 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2461 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2462 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2463 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002464 value = PyDict_GetItemWithError(locals, name);
2465 if (value != NULL) {
2466 Py_INCREF(value);
2467 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002468 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002469 goto error;
2470 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002471 }
2472 else {
2473 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002474 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002475 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002476 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002477 }
2478 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002479 }
2480 }
2481 if (!value) {
2482 PyObject *cell = freevars[oparg];
2483 value = PyCell_GET(cell);
2484 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002485 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002486 goto error;
2487 }
2488 Py_INCREF(value);
2489 }
2490 PUSH(value);
2491 DISPATCH();
2492 }
2493
Benjamin Petersonddd19492018-09-16 22:38:02 -07002494 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002495 PyObject *cell = freevars[oparg];
2496 PyObject *value = PyCell_GET(cell);
2497 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002498 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002499 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002500 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002501 Py_INCREF(value);
2502 PUSH(value);
2503 DISPATCH();
2504 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002505
Benjamin Petersonddd19492018-09-16 22:38:02 -07002506 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002507 PyObject *v = POP();
2508 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002509 PyObject *oldobj = PyCell_GET(cell);
2510 PyCell_SET(cell, v);
2511 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002512 DISPATCH();
2513 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002514
Benjamin Petersonddd19492018-09-16 22:38:02 -07002515 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002516 PyObject *str;
2517 PyObject *empty = PyUnicode_New(0, 0);
2518 if (empty == NULL) {
2519 goto error;
2520 }
2521 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2522 Py_DECREF(empty);
2523 if (str == NULL)
2524 goto error;
2525 while (--oparg >= 0) {
2526 PyObject *item = POP();
2527 Py_DECREF(item);
2528 }
2529 PUSH(str);
2530 DISPATCH();
2531 }
2532
Benjamin Petersonddd19492018-09-16 22:38:02 -07002533 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002534 PyObject *tup = PyTuple_New(oparg);
2535 if (tup == NULL)
2536 goto error;
2537 while (--oparg >= 0) {
2538 PyObject *item = POP();
2539 PyTuple_SET_ITEM(tup, oparg, item);
2540 }
2541 PUSH(tup);
2542 DISPATCH();
2543 }
2544
Benjamin Petersonddd19492018-09-16 22:38:02 -07002545 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002546 PyObject *list = PyList_New(oparg);
2547 if (list == NULL)
2548 goto error;
2549 while (--oparg >= 0) {
2550 PyObject *item = POP();
2551 PyList_SET_ITEM(list, oparg, item);
2552 }
2553 PUSH(list);
2554 DISPATCH();
2555 }
2556
Benjamin Petersonddd19492018-09-16 22:38:02 -07002557 case TARGET(BUILD_TUPLE_UNPACK_WITH_CALL):
2558 case TARGET(BUILD_TUPLE_UNPACK):
2559 case TARGET(BUILD_LIST_UNPACK): {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002560 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002561 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002562 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002563 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002564
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002565 if (sum == NULL)
2566 goto error;
2567
2568 for (i = oparg; i > 0; i--) {
2569 PyObject *none_val;
2570
2571 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2572 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002573 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
Victor Stinner438a12d2019-05-24 17:01:38 +02002574 _PyErr_ExceptionMatches(tstate, PyExc_TypeError))
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002575 {
Victor Stinner438a12d2019-05-24 17:01:38 +02002576 check_args_iterable(tstate, PEEK(1 + oparg), PEEK(i));
Serhiy Storchaka73442852016-10-02 10:33:46 +03002577 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002578 Py_DECREF(sum);
2579 goto error;
2580 }
2581 Py_DECREF(none_val);
2582 }
2583
2584 if (convert_to_tuple) {
2585 return_value = PyList_AsTuple(sum);
2586 Py_DECREF(sum);
2587 if (return_value == NULL)
2588 goto error;
2589 }
2590 else {
2591 return_value = sum;
2592 }
2593
2594 while (oparg--)
2595 Py_DECREF(POP());
2596 PUSH(return_value);
2597 DISPATCH();
2598 }
2599
Benjamin Petersonddd19492018-09-16 22:38:02 -07002600 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002601 PyObject *set = PySet_New(NULL);
2602 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002603 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002604 if (set == NULL)
2605 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002606 for (i = oparg; i > 0; i--) {
2607 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002608 if (err == 0)
2609 err = PySet_Add(set, item);
2610 Py_DECREF(item);
2611 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002612 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002613 if (err != 0) {
2614 Py_DECREF(set);
2615 goto error;
2616 }
2617 PUSH(set);
2618 DISPATCH();
2619 }
2620
Benjamin Petersonddd19492018-09-16 22:38:02 -07002621 case TARGET(BUILD_SET_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002622 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002623 PyObject *sum = PySet_New(NULL);
2624 if (sum == NULL)
2625 goto error;
2626
2627 for (i = oparg; i > 0; i--) {
2628 if (_PySet_Update(sum, PEEK(i)) < 0) {
2629 Py_DECREF(sum);
2630 goto error;
2631 }
2632 }
2633
2634 while (oparg--)
2635 Py_DECREF(POP());
2636 PUSH(sum);
2637 DISPATCH();
2638 }
2639
Benjamin Petersonddd19492018-09-16 22:38:02 -07002640 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002641 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002642 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2643 if (map == NULL)
2644 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002645 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002646 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002647 PyObject *key = PEEK(2*i);
2648 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002649 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002650 if (err != 0) {
2651 Py_DECREF(map);
2652 goto error;
2653 }
2654 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002655
2656 while (oparg--) {
2657 Py_DECREF(POP());
2658 Py_DECREF(POP());
2659 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002660 PUSH(map);
2661 DISPATCH();
2662 }
2663
Benjamin Petersonddd19492018-09-16 22:38:02 -07002664 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002665 _Py_IDENTIFIER(__annotations__);
2666 int err;
2667 PyObject *ann_dict;
2668 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002669 _PyErr_Format(tstate, PyExc_SystemError,
2670 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002671 goto error;
2672 }
2673 /* check if __annotations__ in locals()... */
2674 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002675 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002676 &PyId___annotations__);
2677 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002678 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002679 goto error;
2680 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002681 /* ...if not, create a new one */
2682 ann_dict = PyDict_New();
2683 if (ann_dict == NULL) {
2684 goto error;
2685 }
2686 err = _PyDict_SetItemId(f->f_locals,
2687 &PyId___annotations__, ann_dict);
2688 Py_DECREF(ann_dict);
2689 if (err != 0) {
2690 goto error;
2691 }
2692 }
2693 }
2694 else {
2695 /* do the same if locals() is not a dict */
2696 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2697 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002698 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002699 }
2700 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2701 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002702 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002703 goto error;
2704 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002705 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002706 ann_dict = PyDict_New();
2707 if (ann_dict == NULL) {
2708 goto error;
2709 }
2710 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2711 Py_DECREF(ann_dict);
2712 if (err != 0) {
2713 goto error;
2714 }
2715 }
2716 else {
2717 Py_DECREF(ann_dict);
2718 }
2719 }
2720 DISPATCH();
2721 }
2722
Benjamin Petersonddd19492018-09-16 22:38:02 -07002723 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002724 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002725 PyObject *map;
2726 PyObject *keys = TOP();
2727 if (!PyTuple_CheckExact(keys) ||
2728 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002729 _PyErr_SetString(tstate, PyExc_SystemError,
2730 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002731 goto error;
2732 }
2733 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2734 if (map == NULL) {
2735 goto error;
2736 }
2737 for (i = oparg; i > 0; i--) {
2738 int err;
2739 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2740 PyObject *value = PEEK(i + 1);
2741 err = PyDict_SetItem(map, key, value);
2742 if (err != 0) {
2743 Py_DECREF(map);
2744 goto error;
2745 }
2746 }
2747
2748 Py_DECREF(POP());
2749 while (oparg--) {
2750 Py_DECREF(POP());
2751 }
2752 PUSH(map);
2753 DISPATCH();
2754 }
2755
Benjamin Petersonddd19492018-09-16 22:38:02 -07002756 case TARGET(BUILD_MAP_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002757 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002758 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002759 if (sum == NULL)
2760 goto error;
2761
2762 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002763 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002764 if (PyDict_Update(sum, arg) < 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002765 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
2766 _PyErr_Format(tstate, PyExc_TypeError,
2767 "'%.200s' object is not a mapping",
2768 arg->ob_type->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002769 }
2770 Py_DECREF(sum);
2771 goto error;
2772 }
2773 }
2774
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002775 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002776 Py_DECREF(POP());
2777 PUSH(sum);
2778 DISPATCH();
2779 }
2780
Benjamin Petersonddd19492018-09-16 22:38:02 -07002781 case TARGET(BUILD_MAP_UNPACK_WITH_CALL): {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002782 Py_ssize_t i;
2783 PyObject *sum = PyDict_New();
2784 if (sum == NULL)
2785 goto error;
2786
2787 for (i = oparg; i > 0; i--) {
2788 PyObject *arg = PEEK(i);
2789 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002790 Py_DECREF(sum);
Victor Stinner438a12d2019-05-24 17:01:38 +02002791 format_kwargs_error(tstate, PEEK(2 + oparg), arg);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002792 goto error;
2793 }
2794 }
2795
2796 while (oparg--)
2797 Py_DECREF(POP());
2798 PUSH(sum);
2799 DISPATCH();
2800 }
2801
Benjamin Petersonddd19492018-09-16 22:38:02 -07002802 case TARGET(MAP_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002803 PyObject *key = TOP();
2804 PyObject *value = SECOND();
2805 PyObject *map;
2806 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002807 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002808 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002809 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002810 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002811 Py_DECREF(value);
2812 Py_DECREF(key);
2813 if (err != 0)
2814 goto error;
2815 PREDICT(JUMP_ABSOLUTE);
2816 DISPATCH();
2817 }
2818
Benjamin Petersonddd19492018-09-16 22:38:02 -07002819 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002820 PyObject *name = GETITEM(names, oparg);
2821 PyObject *owner = TOP();
2822 PyObject *res = PyObject_GetAttr(owner, name);
2823 Py_DECREF(owner);
2824 SET_TOP(res);
2825 if (res == NULL)
2826 goto error;
2827 DISPATCH();
2828 }
2829
Benjamin Petersonddd19492018-09-16 22:38:02 -07002830 case TARGET(COMPARE_OP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002831 PyObject *right = POP();
2832 PyObject *left = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002833 PyObject *res = cmp_outcome(tstate, oparg, left, right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002834 Py_DECREF(left);
2835 Py_DECREF(right);
2836 SET_TOP(res);
2837 if (res == NULL)
2838 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002839 PREDICT(POP_JUMP_IF_FALSE);
2840 PREDICT(POP_JUMP_IF_TRUE);
2841 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002842 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002843
Benjamin Petersonddd19492018-09-16 22:38:02 -07002844 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002845 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002846 PyObject *fromlist = POP();
2847 PyObject *level = TOP();
2848 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02002849 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002850 Py_DECREF(level);
2851 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002852 SET_TOP(res);
2853 if (res == NULL)
2854 goto error;
2855 DISPATCH();
2856 }
2857
Benjamin Petersonddd19492018-09-16 22:38:02 -07002858 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002859 PyObject *from = POP(), *locals;
2860 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002861 if (PyFrame_FastToLocalsWithError(f) < 0) {
2862 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002863 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002864 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002865
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002866 locals = f->f_locals;
2867 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002868 _PyErr_SetString(tstate, PyExc_SystemError,
2869 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002870 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002871 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002872 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002873 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002874 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002875 Py_DECREF(from);
2876 if (err != 0)
2877 goto error;
2878 DISPATCH();
2879 }
Guido van Rossum25831651993-05-19 14:50:45 +00002880
Benjamin Petersonddd19492018-09-16 22:38:02 -07002881 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002882 PyObject *name = GETITEM(names, oparg);
2883 PyObject *from = TOP();
2884 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02002885 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002886 PUSH(res);
2887 if (res == NULL)
2888 goto error;
2889 DISPATCH();
2890 }
Thomas Wouters52152252000-08-17 22:55:00 +00002891
Benjamin Petersonddd19492018-09-16 22:38:02 -07002892 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002893 JUMPBY(oparg);
2894 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002895 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002896
Benjamin Petersonddd19492018-09-16 22:38:02 -07002897 case TARGET(POP_JUMP_IF_FALSE): {
2898 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002899 PyObject *cond = POP();
2900 int err;
2901 if (cond == Py_True) {
2902 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002903 FAST_DISPATCH();
2904 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002905 if (cond == Py_False) {
2906 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002907 JUMPTO(oparg);
2908 FAST_DISPATCH();
2909 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002910 err = PyObject_IsTrue(cond);
2911 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002912 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07002913 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002914 else if (err == 0)
2915 JUMPTO(oparg);
2916 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002917 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002918 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002919 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002920
Benjamin Petersonddd19492018-09-16 22:38:02 -07002921 case TARGET(POP_JUMP_IF_TRUE): {
2922 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002923 PyObject *cond = POP();
2924 int err;
2925 if (cond == Py_False) {
2926 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002927 FAST_DISPATCH();
2928 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002929 if (cond == Py_True) {
2930 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002931 JUMPTO(oparg);
2932 FAST_DISPATCH();
2933 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002934 err = PyObject_IsTrue(cond);
2935 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002936 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002937 JUMPTO(oparg);
2938 }
2939 else if (err == 0)
2940 ;
2941 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002942 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002943 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002944 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002945
Benjamin Petersonddd19492018-09-16 22:38:02 -07002946 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002947 PyObject *cond = TOP();
2948 int err;
2949 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002950 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002951 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002952 FAST_DISPATCH();
2953 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002954 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002955 JUMPTO(oparg);
2956 FAST_DISPATCH();
2957 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002958 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002959 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002960 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002961 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002962 }
2963 else if (err == 0)
2964 JUMPTO(oparg);
2965 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002966 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002967 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002968 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002969
Benjamin Petersonddd19492018-09-16 22:38:02 -07002970 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002971 PyObject *cond = TOP();
2972 int err;
2973 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002974 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002975 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002976 FAST_DISPATCH();
2977 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002978 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002979 JUMPTO(oparg);
2980 FAST_DISPATCH();
2981 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002982 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002983 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002984 JUMPTO(oparg);
2985 }
2986 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002987 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002988 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002989 }
2990 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002991 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002992 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002993 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002994
Benjamin Petersonddd19492018-09-16 22:38:02 -07002995 case TARGET(JUMP_ABSOLUTE): {
2996 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002997 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002998#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002999 /* Enabling this path speeds-up all while and for-loops by bypassing
3000 the per-loop checks for signals. By default, this should be turned-off
3001 because it prevents detection of a control-break in tight loops like
3002 "while 1: pass". Compile with this option turned-on when you need
3003 the speed-up and do not need break checking inside tight loops (ones
3004 that contain only instructions ending with FAST_DISPATCH).
3005 */
3006 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003007#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003008 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003009#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003010 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003011
Benjamin Petersonddd19492018-09-16 22:38:02 -07003012 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003013 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003014 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003015 PyObject *iter = PyObject_GetIter(iterable);
3016 Py_DECREF(iterable);
3017 SET_TOP(iter);
3018 if (iter == NULL)
3019 goto error;
3020 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003021 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003022 DISPATCH();
3023 }
3024
Benjamin Petersonddd19492018-09-16 22:38:02 -07003025 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04003026 /* before: [obj]; after [getiter(obj)] */
3027 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003028 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003029 if (PyCoro_CheckExact(iterable)) {
3030 /* `iterable` is a coroutine */
3031 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3032 /* and it is used in a 'yield from' expression of a
3033 regular generator. */
3034 Py_DECREF(iterable);
3035 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003036 _PyErr_SetString(tstate, PyExc_TypeError,
3037 "cannot 'yield from' a coroutine object "
3038 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04003039 goto error;
3040 }
3041 }
3042 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003043 /* `iterable` is not a generator. */
3044 iter = PyObject_GetIter(iterable);
3045 Py_DECREF(iterable);
3046 SET_TOP(iter);
3047 if (iter == NULL)
3048 goto error;
3049 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003050 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003051 DISPATCH();
3052 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003053
Benjamin Petersonddd19492018-09-16 22:38:02 -07003054 case TARGET(FOR_ITER): {
3055 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003056 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003057 PyObject *iter = TOP();
3058 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
3059 if (next != NULL) {
3060 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003061 PREDICT(STORE_FAST);
3062 PREDICT(UNPACK_SEQUENCE);
3063 DISPATCH();
3064 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003065 if (_PyErr_Occurred(tstate)) {
3066 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003067 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003068 }
3069 else if (tstate->c_tracefunc != NULL) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003070 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Victor Stinner438a12d2019-05-24 17:01:38 +02003071 }
3072 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003073 }
3074 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003075 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003076 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003077 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003078 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003079 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003080 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003081
Benjamin Petersonddd19492018-09-16 22:38:02 -07003082 case TARGET(SETUP_FINALLY): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003083 /* NOTE: If you add any new block-setup opcodes that
3084 are not try/except/finally handlers, you may need
3085 to update the PyGen_NeedsFinalizing() function.
3086 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003087
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003088 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003089 STACK_LEVEL());
3090 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003091 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003092
Benjamin Petersonddd19492018-09-16 22:38:02 -07003093 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003094 _Py_IDENTIFIER(__aexit__);
3095 _Py_IDENTIFIER(__aenter__);
3096
3097 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003098 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__),
Yury Selivanov75445082015-05-11 22:57:16 -04003099 *enter;
3100 PyObject *res;
3101 if (exit == NULL)
3102 goto error;
3103 SET_TOP(exit);
Victor Stinner438a12d2019-05-24 17:01:38 +02003104 enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04003105 Py_DECREF(mgr);
3106 if (enter == NULL)
3107 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003108 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003109 Py_DECREF(enter);
3110 if (res == NULL)
3111 goto error;
3112 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003113 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003114 DISPATCH();
3115 }
3116
Benjamin Petersonddd19492018-09-16 22:38:02 -07003117 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003118 PyObject *res = POP();
3119 /* Setup the finally block before pushing the result
3120 of __aenter__ on the stack. */
3121 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3122 STACK_LEVEL());
3123 PUSH(res);
3124 DISPATCH();
3125 }
3126
Benjamin Petersonddd19492018-09-16 22:38:02 -07003127 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003128 _Py_IDENTIFIER(__exit__);
3129 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003130 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003131 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003132 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003133 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08003134 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003135 }
3136 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003137 if (exit == NULL) {
3138 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003139 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003140 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003141 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003142 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003143 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003144 Py_DECREF(enter);
3145 if (res == NULL)
3146 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003147 /* Setup the finally block before pushing the result
3148 of __enter__ on the stack. */
3149 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3150 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003151
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003152 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003153 DISPATCH();
3154 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003155
Benjamin Petersonddd19492018-09-16 22:38:02 -07003156 case TARGET(WITH_CLEANUP_START): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003157 /* At the top of the stack are 1 or 6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003158 how/why we entered the finally clause:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003159 - TOP = NULL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003160 - (TOP, SECOND, THIRD) = exc_info()
3161 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003162 Below them is EXIT, the context.__exit__ or context.__aexit__
3163 bound method.
3164 In the first case, we must call
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003165 EXIT(None, None, None)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003166 otherwise we must call
3167 EXIT(TOP, SECOND, THIRD)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003168
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003169 In the first case, we remove EXIT from the
3170 stack, leaving TOP, and push TOP on the stack.
3171 Otherwise we shift the bottom 3 values of the
3172 stack down, replace the empty spot with NULL, and push
3173 None on the stack.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003174
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003175 Finally we push the result of the call.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003176 */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003177 PyObject *stack[3];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003178 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003179 PyObject *exc, *val, *tb, *res;
3180
3181 val = tb = Py_None;
3182 exc = TOP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003183 if (exc == NULL) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003184 STACK_SHRINK(1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003185 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003186 SET_TOP(exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003187 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003188 }
3189 else {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003190 assert(PyExceptionClass_Check(exc));
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003191 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003192 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003193 val = SECOND();
3194 tb = THIRD();
3195 tp2 = FOURTH();
3196 exc2 = PEEK(5);
3197 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003198 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003199 SET_VALUE(7, tb2);
3200 SET_VALUE(6, exc2);
3201 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003202 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3203 SET_FOURTH(NULL);
3204 /* We just shifted the stack down, so we have
3205 to tell the except handler block that the
3206 values are lower than it expects. */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003207 assert(f->f_iblock > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003208 block = &f->f_blockstack[f->f_iblock - 1];
3209 assert(block->b_type == EXCEPT_HANDLER);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003210 assert(block->b_level > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003211 block->b_level--;
3212 }
Victor Stinner842cfff2016-12-01 14:45:31 +01003213
3214 stack[0] = exc;
3215 stack[1] = val;
3216 stack[2] = tb;
3217 res = _PyObject_FastCall(exit_func, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003218 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003219 if (res == NULL)
3220 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003221
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003222 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003223 PUSH(exc);
3224 PUSH(res);
3225 PREDICT(WITH_CLEANUP_FINISH);
3226 DISPATCH();
3227 }
3228
Benjamin Petersonddd19492018-09-16 22:38:02 -07003229 case TARGET(WITH_CLEANUP_FINISH): {
3230 PREDICTED(WITH_CLEANUP_FINISH);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003231 /* TOP = the result of calling the context.__exit__ bound method
3232 SECOND = either None or exception type
3233
3234 If SECOND is None below is NULL or the return address,
3235 otherwise below are 7 values representing an exception.
3236 */
Yury Selivanov75445082015-05-11 22:57:16 -04003237 PyObject *res = POP();
3238 PyObject *exc = POP();
3239 int err;
3240
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003241 if (exc != Py_None)
3242 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003243 else
3244 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003245
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003246 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003247 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003249 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003250 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003251 else if (err > 0) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003252 /* There was an exception and a True return.
3253 * We must manually unwind the EXCEPT_HANDLER block
3254 * which was created when the exception was caught,
Quan Tian3bd0d622018-10-20 05:30:03 +08003255 * otherwise the stack will be in an inconsistent state.
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003256 */
3257 PyTryBlock *b = PyFrame_BlockPop(f);
3258 assert(b->b_type == EXCEPT_HANDLER);
3259 UNWIND_EXCEPT_HANDLER(b);
3260 PUSH(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003261 }
3262 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003263 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003264 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003265
Benjamin Petersonddd19492018-09-16 22:38:02 -07003266 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10003267 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003268 PyObject *name = GETITEM(names, oparg);
3269 PyObject *obj = TOP();
3270 PyObject *meth = NULL;
3271
3272 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3273
Yury Selivanovf2392132016-12-13 19:03:51 -05003274 if (meth == NULL) {
3275 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003276 goto error;
3277 }
3278
3279 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003280 /* We can bypass temporary bound method object.
3281 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003282
INADA Naoki015bce62017-01-16 17:23:30 +09003283 meth | self | arg1 | ... | argN
3284 */
3285 SET_TOP(meth);
3286 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003287 }
3288 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003289 /* meth is not an unbound method (but a regular attr, or
3290 something was returned by a descriptor protocol). Set
3291 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003292 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003293
3294 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003295 */
INADA Naoki015bce62017-01-16 17:23:30 +09003296 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003297 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003298 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003299 }
3300 DISPATCH();
3301 }
3302
Benjamin Petersonddd19492018-09-16 22:38:02 -07003303 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003304 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003305 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003306
3307 sp = stack_pointer;
3308
INADA Naoki015bce62017-01-16 17:23:30 +09003309 meth = PEEK(oparg + 2);
3310 if (meth == NULL) {
3311 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3312 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003313
3314 Stack layout:
3315
INADA Naoki015bce62017-01-16 17:23:30 +09003316 ... | NULL | callable | arg1 | ... | argN
3317 ^- TOP()
3318 ^- (-oparg)
3319 ^- (-oparg-1)
3320 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003321
Ville Skyttä49b27342017-08-03 09:00:59 +03003322 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003323 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003324 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003325 res = call_function(tstate, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003326 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003327 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003328 }
3329 else {
3330 /* This is a method call. Stack layout:
3331
INADA Naoki015bce62017-01-16 17:23:30 +09003332 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003333 ^- TOP()
3334 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003335 ^- (-oparg-1)
3336 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003337
INADA Naoki015bce62017-01-16 17:23:30 +09003338 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003339 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003340 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003341 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003342 res = call_function(tstate, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003343 stack_pointer = sp;
3344 }
3345
3346 PUSH(res);
3347 if (res == NULL)
3348 goto error;
3349 DISPATCH();
3350 }
3351
Benjamin Petersonddd19492018-09-16 22:38:02 -07003352 case TARGET(CALL_FUNCTION): {
3353 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003354 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003355 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003356 res = call_function(tstate, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003357 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003358 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003359 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003360 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003361 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003362 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003363 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003364
Benjamin Petersonddd19492018-09-16 22:38:02 -07003365 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003366 PyObject **sp, *res, *names;
3367
3368 names = POP();
3369 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003370 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003371 res = call_function(tstate, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003372 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003373 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003374 Py_DECREF(names);
3375
3376 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003377 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003378 }
3379 DISPATCH();
3380 }
3381
Benjamin Petersonddd19492018-09-16 22:38:02 -07003382 case TARGET(CALL_FUNCTION_EX): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003383 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003384 if (oparg & 0x01) {
3385 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003386 if (!PyDict_CheckExact(kwargs)) {
3387 PyObject *d = PyDict_New();
3388 if (d == NULL)
3389 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003390 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003391 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02003392 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003393 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003394 goto error;
3395 }
3396 Py_DECREF(kwargs);
3397 kwargs = d;
3398 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003399 assert(PyDict_CheckExact(kwargs));
3400 }
3401 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003402 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003403 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003404 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003405 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003406 goto error;
3407 }
3408 Py_SETREF(callargs, PySequence_Tuple(callargs));
3409 if (callargs == NULL) {
3410 goto error;
3411 }
3412 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003413 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003414
Victor Stinner09532fe2019-05-10 23:39:09 +02003415 result = do_call_core(tstate, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003416 Py_DECREF(func);
3417 Py_DECREF(callargs);
3418 Py_XDECREF(kwargs);
3419
3420 SET_TOP(result);
3421 if (result == NULL) {
3422 goto error;
3423 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003424 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003425 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003426
Benjamin Petersonddd19492018-09-16 22:38:02 -07003427 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003428 PyObject *qualname = POP();
3429 PyObject *codeobj = POP();
3430 PyFunctionObject *func = (PyFunctionObject *)
3431 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003432
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003433 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003434 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003435 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003436 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003437 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003438
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003439 if (oparg & 0x08) {
3440 assert(PyTuple_CheckExact(TOP()));
3441 func ->func_closure = POP();
3442 }
3443 if (oparg & 0x04) {
3444 assert(PyDict_CheckExact(TOP()));
3445 func->func_annotations = POP();
3446 }
3447 if (oparg & 0x02) {
3448 assert(PyDict_CheckExact(TOP()));
3449 func->func_kwdefaults = POP();
3450 }
3451 if (oparg & 0x01) {
3452 assert(PyTuple_CheckExact(TOP()));
3453 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003454 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003455
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003456 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003457 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003458 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003459
Benjamin Petersonddd19492018-09-16 22:38:02 -07003460 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003461 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003462 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003463 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003464 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003465 step = NULL;
3466 stop = POP();
3467 start = TOP();
3468 slice = PySlice_New(start, stop, step);
3469 Py_DECREF(start);
3470 Py_DECREF(stop);
3471 Py_XDECREF(step);
3472 SET_TOP(slice);
3473 if (slice == NULL)
3474 goto error;
3475 DISPATCH();
3476 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003477
Benjamin Petersonddd19492018-09-16 22:38:02 -07003478 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003479 /* Handles f-string value formatting. */
3480 PyObject *result;
3481 PyObject *fmt_spec;
3482 PyObject *value;
3483 PyObject *(*conv_fn)(PyObject *);
3484 int which_conversion = oparg & FVC_MASK;
3485 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3486
3487 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003488 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003489
3490 /* See if any conversion is specified. */
3491 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003492 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003493 case FVC_STR: conv_fn = PyObject_Str; break;
3494 case FVC_REPR: conv_fn = PyObject_Repr; break;
3495 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003496 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02003497 _PyErr_Format(tstate, PyExc_SystemError,
3498 "unexpected conversion flag %d",
3499 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003500 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003501 }
3502
3503 /* If there's a conversion function, call it and replace
3504 value with that result. Otherwise, just use value,
3505 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003506 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003507 result = conv_fn(value);
3508 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003509 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003510 Py_XDECREF(fmt_spec);
3511 goto error;
3512 }
3513 value = result;
3514 }
3515
3516 /* If value is a unicode object, and there's no fmt_spec,
3517 then we know the result of format(value) is value
3518 itself. In that case, skip calling format(). I plan to
3519 move this optimization in to PyObject_Format()
3520 itself. */
3521 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3522 /* Do nothing, just transfer ownership to result. */
3523 result = value;
3524 } else {
3525 /* Actually call format(). */
3526 result = PyObject_Format(value, fmt_spec);
3527 Py_DECREF(value);
3528 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003529 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003530 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003531 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003532 }
3533
Eric V. Smith135d5f42016-02-05 18:23:08 -05003534 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003535 DISPATCH();
3536 }
3537
Benjamin Petersonddd19492018-09-16 22:38:02 -07003538 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003539 int oldoparg = oparg;
3540 NEXTOPARG();
3541 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003542 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003543 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003544
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003545
Antoine Pitrou042b1282010-08-13 21:15:58 +00003546#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003547 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003548#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003549 default:
3550 fprintf(stderr,
3551 "XXX lineno: %d, opcode: %d\n",
3552 PyFrame_GetLineNumber(f),
3553 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02003554 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003555 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003557 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003558
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003559 /* This should never be reached. Every opcode should end with DISPATCH()
3560 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003561 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003562
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003563error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003564 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003565#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02003566 if (!_PyErr_Occurred(tstate)) {
3567 _PyErr_SetString(tstate, PyExc_SystemError,
3568 "error return without exception set");
3569 }
Victor Stinner365b6932013-07-12 00:11:58 +02003570#else
Victor Stinner438a12d2019-05-24 17:01:38 +02003571 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02003572#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003573
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003574 /* Log traceback info. */
3575 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003576
Benjamin Peterson51f46162013-01-23 08:38:47 -05003577 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003578 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3579 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003580
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003581exception_unwind:
3582 /* Unwind stacks if an exception occurred */
3583 while (f->f_iblock > 0) {
3584 /* Pop the current block. */
3585 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003587 if (b->b_type == EXCEPT_HANDLER) {
3588 UNWIND_EXCEPT_HANDLER(b);
3589 continue;
3590 }
3591 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003592 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003593 PyObject *exc, *val, *tb;
3594 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003595 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003596 /* Beware, this invalidates all b->b_* fields */
3597 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003598 PUSH(exc_info->exc_traceback);
3599 PUSH(exc_info->exc_value);
3600 if (exc_info->exc_type != NULL) {
3601 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003602 }
3603 else {
3604 Py_INCREF(Py_None);
3605 PUSH(Py_None);
3606 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003607 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003608 /* Make the raw exception data
3609 available to the handler,
3610 so a program can emulate the
3611 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02003612 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003613 if (tb != NULL)
3614 PyException_SetTraceback(val, tb);
3615 else
3616 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003617 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003618 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003619 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003620 exc_info->exc_value = val;
3621 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003622 if (tb == NULL)
3623 tb = Py_None;
3624 Py_INCREF(tb);
3625 PUSH(tb);
3626 PUSH(val);
3627 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003628 JUMPTO(handler);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003629 /* Resume normal execution */
3630 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003631 }
3632 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003633
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003634 /* End the loop as we still have an error */
3635 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003636 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003637
Pablo Galindof00828a2019-05-09 16:52:02 +01003638 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003639 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01003640
3641exit_returning:
3642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003643 /* Pop remaining stack entries. */
3644 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003645 PyObject *o = POP();
3646 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003647 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003648
Pablo Galindof00828a2019-05-09 16:52:02 +01003649exit_yielding:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003650 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003651 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003652 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3653 tstate, f, PyTrace_RETURN, retval)) {
3654 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003655 }
3656 }
3657 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003658 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3659 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003660 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003661 }
3662 }
3663 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003664
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003665 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003666exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003667 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3668 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003669 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003670 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003671 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003672
Victor Stinnerefde1462015-03-21 15:04:43 +01003673 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003674}
3675
Benjamin Petersonb204a422011-06-05 22:04:07 -05003676static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003677format_missing(PyThreadState *tstate, const char *kind,
3678 PyCodeObject *co, PyObject *names)
Benjamin Petersone109c702011-06-24 09:37:26 -05003679{
3680 int err;
3681 Py_ssize_t len = PyList_GET_SIZE(names);
3682 PyObject *name_str, *comma, *tail, *tmp;
3683
3684 assert(PyList_CheckExact(names));
3685 assert(len >= 1);
3686 /* Deal with the joys of natural language. */
3687 switch (len) {
3688 case 1:
3689 name_str = PyList_GET_ITEM(names, 0);
3690 Py_INCREF(name_str);
3691 break;
3692 case 2:
3693 name_str = PyUnicode_FromFormat("%U and %U",
3694 PyList_GET_ITEM(names, len - 2),
3695 PyList_GET_ITEM(names, len - 1));
3696 break;
3697 default:
3698 tail = PyUnicode_FromFormat(", %U, and %U",
3699 PyList_GET_ITEM(names, len - 2),
3700 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003701 if (tail == NULL)
3702 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003703 /* Chop off the last two objects in the list. This shouldn't actually
3704 fail, but we can't be too careful. */
3705 err = PyList_SetSlice(names, len - 2, len, NULL);
3706 if (err == -1) {
3707 Py_DECREF(tail);
3708 return;
3709 }
3710 /* Stitch everything up into a nice comma-separated list. */
3711 comma = PyUnicode_FromString(", ");
3712 if (comma == NULL) {
3713 Py_DECREF(tail);
3714 return;
3715 }
3716 tmp = PyUnicode_Join(comma, names);
3717 Py_DECREF(comma);
3718 if (tmp == NULL) {
3719 Py_DECREF(tail);
3720 return;
3721 }
3722 name_str = PyUnicode_Concat(tmp, tail);
3723 Py_DECREF(tmp);
3724 Py_DECREF(tail);
3725 break;
3726 }
3727 if (name_str == NULL)
3728 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02003729 _PyErr_Format(tstate, PyExc_TypeError,
3730 "%U() missing %i required %s argument%s: %U",
3731 co->co_name,
3732 len,
3733 kind,
3734 len == 1 ? "" : "s",
3735 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05003736 Py_DECREF(name_str);
3737}
3738
3739static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003740missing_arguments(PyThreadState *tstate, PyCodeObject *co,
3741 Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003742 PyObject **fastlocals)
3743{
Victor Stinner74319ae2016-08-25 00:04:09 +02003744 Py_ssize_t i, j = 0;
3745 Py_ssize_t start, end;
3746 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003747 const char *kind = positional ? "positional" : "keyword-only";
3748 PyObject *missing_names;
3749
3750 /* Compute the names of the arguments that are missing. */
3751 missing_names = PyList_New(missing);
3752 if (missing_names == NULL)
3753 return;
3754 if (positional) {
3755 start = 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003756 end = co->co_posonlyargcount + co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003757 }
3758 else {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003759 start = co->co_posonlyargcount + co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003760 end = start + co->co_kwonlyargcount;
3761 }
3762 for (i = start; i < end; i++) {
3763 if (GETLOCAL(i) == NULL) {
3764 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3765 PyObject *name = PyObject_Repr(raw);
3766 if (name == NULL) {
3767 Py_DECREF(missing_names);
3768 return;
3769 }
3770 PyList_SET_ITEM(missing_names, j++, name);
3771 }
3772 }
3773 assert(j == missing);
Victor Stinner438a12d2019-05-24 17:01:38 +02003774 format_missing(tstate, kind, co, missing_names);
Benjamin Petersone109c702011-06-24 09:37:26 -05003775 Py_DECREF(missing_names);
3776}
3777
3778static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003779too_many_positional(PyThreadState *tstate, PyCodeObject *co,
3780 Py_ssize_t given, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003781 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003782{
3783 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003784 Py_ssize_t kwonly_given = 0;
3785 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003786 PyObject *sig, *kwonly_sig;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003787 Py_ssize_t co_posonlyargcount = co->co_posonlyargcount;
Victor Stinner74319ae2016-08-25 00:04:09 +02003788 Py_ssize_t co_argcount = co->co_argcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003789 Py_ssize_t total_positional = co_argcount + co_posonlyargcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003790
Benjamin Petersone109c702011-06-24 09:37:26 -05003791 assert((co->co_flags & CO_VARARGS) == 0);
3792 /* Count missing keyword-only args. */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003793 for (i = total_positional; i < total_positional + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003794 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003795 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003796 }
3797 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003798 if (defcount) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003799 Py_ssize_t atleast = total_positional - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003800 plural = 1;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003801 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, total_positional);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003802 }
3803 else {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003804 plural = (total_positional != 1);
3805 sig = PyUnicode_FromFormat("%zd", total_positional);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003806 }
3807 if (sig == NULL)
3808 return;
3809 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003810 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3811 kwonly_sig = PyUnicode_FromFormat(format,
3812 given != 1 ? "s" : "",
3813 kwonly_given,
3814 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003815 if (kwonly_sig == NULL) {
3816 Py_DECREF(sig);
3817 return;
3818 }
3819 }
3820 else {
3821 /* This will not fail. */
3822 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003823 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003824 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003825 _PyErr_Format(tstate, PyExc_TypeError,
3826 "%U() takes %U positional argument%s but %zd%U %s given",
3827 co->co_name,
3828 sig,
3829 plural ? "s" : "",
3830 given,
3831 kwonly_sig,
3832 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003833 Py_DECREF(sig);
3834 Py_DECREF(kwonly_sig);
3835}
3836
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003837static int
Victor Stinner438a12d2019-05-24 17:01:38 +02003838positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
3839 Py_ssize_t kwcount, PyObject* const* kwnames)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003840{
3841 int posonly_conflicts = 0;
3842 PyObject* posonly_names = PyList_New(0);
3843
3844 for(int k=0; k < co->co_posonlyargcount; k++){
3845 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
3846
3847 for (int k2=0; k2<kwcount; k2++){
3848 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
3849 PyObject* kwname = kwnames[k2];
3850 if (kwname == posonly_name){
3851 if(PyList_Append(posonly_names, kwname) != 0) {
3852 goto fail;
3853 }
3854 posonly_conflicts++;
3855 continue;
3856 }
3857
3858 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
3859
3860 if ( cmp > 0) {
3861 if(PyList_Append(posonly_names, kwname) != 0) {
3862 goto fail;
3863 }
3864 posonly_conflicts++;
3865 } else if (cmp < 0) {
3866 goto fail;
3867 }
3868
3869 }
3870 }
3871 if (posonly_conflicts) {
3872 PyObject* comma = PyUnicode_FromString(", ");
3873 if (comma == NULL) {
3874 goto fail;
3875 }
3876 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
3877 Py_DECREF(comma);
3878 if (error_names == NULL) {
3879 goto fail;
3880 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003881 _PyErr_Format(tstate, PyExc_TypeError,
3882 "%U() got some positional-only arguments passed"
3883 " as keyword arguments: '%U'",
3884 co->co_name, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003885 Py_DECREF(error_names);
3886 goto fail;
3887 }
3888
3889 Py_DECREF(posonly_names);
3890 return 0;
3891
3892fail:
3893 Py_XDECREF(posonly_names);
3894 return 1;
3895
3896}
3897
Guido van Rossumc2e20742006-02-27 22:32:47 +00003898/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02003899 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003900 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003901
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003902PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +02003903_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003904 PyObject *const *args, Py_ssize_t argcount,
3905 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003906 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003907 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003908 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003909 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003910{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003911 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003912 PyFrameObject *f;
3913 PyObject *retval = NULL;
3914 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003915 PyObject *x, *u;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003916 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount + co->co_posonlyargcount;
3917 Py_ssize_t i, j, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003918 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003919
Victor Stinner438a12d2019-05-24 17:01:38 +02003920 PyThreadState *tstate = _PyThreadState_GET();
3921 assert(tstate != NULL);
3922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003923 if (globals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003924 _PyErr_SetString(tstate, PyExc_SystemError,
3925 "PyEval_EvalCodeEx: NULL globals");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003926 return NULL;
3927 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003928
Victor Stinnerc7020012016-08-16 23:40:29 +02003929 /* Create the frame */
INADA Naoki5a625d02016-12-24 20:19:08 +09003930 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003931 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003932 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003933 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003934 fastlocals = f->f_localsplus;
3935 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003936
Victor Stinnerc7020012016-08-16 23:40:29 +02003937 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003938 if (co->co_flags & CO_VARKEYWORDS) {
3939 kwdict = PyDict_New();
3940 if (kwdict == NULL)
3941 goto fail;
3942 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003943 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003944 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003945 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003946 SETLOCAL(i, kwdict);
3947 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003948 else {
3949 kwdict = NULL;
3950 }
3951
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003952 /* Copy positional only arguments into local variables */
3953 if (argcount > co->co_argcount + co->co_posonlyargcount) {
3954 n = co->co_posonlyargcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003955 }
3956 else {
3957 n = argcount;
3958 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003959 for (j = 0; j < n; j++) {
3960 x = args[j];
3961 Py_INCREF(x);
3962 SETLOCAL(j, x);
3963 }
3964
3965
3966 /* Copy positional arguments into local variables */
3967 if (argcount > co->co_argcount + co->co_posonlyargcount) {
3968 n += co->co_argcount;
3969 }
3970 else {
3971 n = argcount;
3972 }
3973 for (i = j; i < n; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003974 x = args[i];
3975 Py_INCREF(x);
3976 SETLOCAL(i, x);
3977 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003978
3979 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003980 if (co->co_flags & CO_VARARGS) {
Sergey Fedoseev234531b2019-02-25 21:59:12 +05003981 u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003982 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003983 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003984 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003985 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003986 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003987
Serhiy Storchakab7281052016-09-12 00:52:40 +03003988 /* Handle keyword arguments passed as two strided arrays */
3989 kwcount *= kwstep;
3990 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003991 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003992 PyObject *keyword = kwnames[i];
3993 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003994 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003995
Benjamin Petersonb204a422011-06-05 22:04:07 -05003996 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003997 _PyErr_Format(tstate, PyExc_TypeError,
3998 "%U() keywords must be strings",
3999 co->co_name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004000 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004001 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004002
Benjamin Petersonb204a422011-06-05 22:04:07 -05004003 /* Speed hack: do raw pointer compares. As names are
4004 normally interned this should almost always hit. */
4005 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004006 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004007 PyObject *name = co_varnames[j];
4008 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004009 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004010 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004011 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004012
Benjamin Petersonb204a422011-06-05 22:04:07 -05004013 /* Slow fallback, just in case */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004014 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004015 PyObject *name = co_varnames[j];
4016 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
4017 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004018 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004019 }
4020 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004021 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004022 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004023 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004024
Victor Stinner231d1f32017-01-11 02:12:06 +01004025 assert(j >= total_args);
4026 if (kwdict == NULL) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004027
Victor Stinner438a12d2019-05-24 17:01:38 +02004028 if (co->co_posonlyargcount
4029 && positional_only_passed_as_keyword(tstate, co,
4030 kwcount, kwnames))
4031 {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004032 goto fail;
4033 }
4034
Victor Stinner438a12d2019-05-24 17:01:38 +02004035 _PyErr_Format(tstate, PyExc_TypeError,
4036 "%U() got an unexpected keyword argument '%S'",
4037 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004038 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004039 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004040
Christian Heimes0bd447f2013-07-20 14:48:10 +02004041 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4042 goto fail;
4043 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004044 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004045
Benjamin Petersonb204a422011-06-05 22:04:07 -05004046 kw_found:
4047 if (GETLOCAL(j) != NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004048 _PyErr_Format(tstate, PyExc_TypeError,
4049 "%U() got multiple values for argument '%S'",
4050 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004051 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004052 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004053 Py_INCREF(value);
4054 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004055 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004056
4057 /* Check the number of positional arguments */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004058 if ((argcount > co->co_argcount + co->co_posonlyargcount) && !(co->co_flags & CO_VARARGS)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004059 too_many_positional(tstate, co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004060 goto fail;
4061 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004062
4063 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004064 if (argcount < co->co_posonlyargcount + co->co_argcount) {
4065 Py_ssize_t m = co->co_posonlyargcount + co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004066 Py_ssize_t missing = 0;
4067 for (i = argcount; i < m; i++) {
4068 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004069 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004070 }
4071 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004072 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004073 missing_arguments(tstate, co, missing, defcount, fastlocals);
Benjamin Petersone109c702011-06-24 09:37:26 -05004074 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004075 }
4076 if (n > m)
4077 i = n - m;
4078 else
4079 i = 0;
4080 for (; i < defcount; i++) {
4081 if (GETLOCAL(m+i) == NULL) {
4082 PyObject *def = defs[i];
4083 Py_INCREF(def);
4084 SETLOCAL(m+i, def);
4085 }
4086 }
4087 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004088
4089 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004090 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004091 Py_ssize_t missing = 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004092 for (i = co->co_posonlyargcount + co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004093 PyObject *name;
4094 if (GETLOCAL(i) != NULL)
4095 continue;
4096 name = PyTuple_GET_ITEM(co->co_varnames, i);
4097 if (kwdefs != NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004098 PyObject *def = PyDict_GetItemWithError(kwdefs, name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004099 if (def) {
4100 Py_INCREF(def);
4101 SETLOCAL(i, def);
4102 continue;
4103 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004104 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004105 goto fail;
4106 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004107 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004108 missing++;
4109 }
4110 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004111 missing_arguments(tstate, co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004112 goto fail;
4113 }
4114 }
4115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004116 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004117 vars into frame. */
4118 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004119 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004120 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004121 /* Possibly account for the cell variable being an argument. */
4122 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004123 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004124 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004125 /* Clear the local copy. */
4126 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004127 }
4128 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004129 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004130 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004131 if (c == NULL)
4132 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004133 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004134 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004135
4136 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004137 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4138 PyObject *o = PyTuple_GET_ITEM(closure, i);
4139 Py_INCREF(o);
4140 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004141 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004142
Yury Selivanoveb636452016-09-08 22:01:51 -07004143 /* Handle generator/coroutine/asynchronous generator */
4144 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004145 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04004146 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004147 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004148
4149 if (is_coro && tstate->in_coroutine_wrapper) {
4150 assert(coro_wrapper != NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02004151 _PyErr_Format(tstate, PyExc_RuntimeError,
4152 "coroutine wrapper %.200R attempted "
4153 "to recursively wrap %.200R",
4154 coro_wrapper,
4155 co);
Yury Selivanov94c22632015-06-04 10:16:51 -04004156 goto fail;
4157 }
Yury Selivanov75445082015-05-11 22:57:16 -04004158
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004159 /* Don't need to keep the reference to f_back, it will be set
4160 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004161 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004162
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004163 /* Create a new generator that owns the ready to run frame
4164 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004165 if (is_coro) {
4166 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004167 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4168 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004169 } else {
4170 gen = PyGen_NewWithQualName(f, name, qualname);
4171 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004172 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004173 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004174 }
INADA Naoki9c157762016-12-26 18:52:46 +09004175
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004176 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004177
Yury Selivanov94c22632015-06-04 10:16:51 -04004178 if (is_coro && coro_wrapper != NULL) {
4179 PyObject *wrapped;
4180 tstate->in_coroutine_wrapper = 1;
4181 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
4182 tstate->in_coroutine_wrapper = 0;
4183 return wrapped;
4184 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04004185
Yury Selivanov75445082015-05-11 22:57:16 -04004186 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004187 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004188
Victor Stinner59a73272016-12-09 18:51:13 +01004189 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004190
Thomas Woutersce272b62007-09-19 21:19:28 +00004191fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004193 /* decref'ing the frame can cause __del__ methods to get invoked,
4194 which can call back into Python. While we're done with the
4195 current Python frame (f), the associated C stack is still in use,
4196 so recursion_depth must be boosted for the duration.
4197 */
4198 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09004199 if (Py_REFCNT(f) > 1) {
4200 Py_DECREF(f);
4201 _PyObject_GC_TRACK(f);
4202 }
4203 else {
4204 ++tstate->recursion_depth;
4205 Py_DECREF(f);
4206 --tstate->recursion_depth;
4207 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004208 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004209}
4210
Victor Stinner40ee3012014-06-16 15:59:28 +02004211PyObject *
4212PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004213 PyObject *const *args, int argcount,
4214 PyObject *const *kws, int kwcount,
4215 PyObject *const *defs, int defcount,
4216 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004217{
4218 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004219 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004220 kws, kws != NULL ? kws + 1 : NULL,
4221 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004222 defs, defcount,
4223 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004224 NULL, NULL);
4225}
Tim Peters5ca576e2001-06-18 22:08:13 +00004226
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004227static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004228special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004229{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004230 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004231 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02004232 if (res == NULL && !_PyErr_Occurred(tstate)) {
4233 _PyErr_SetObject(tstate, PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004234 return NULL;
4235 }
4236 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004237}
4238
4239
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004240/* Logic for the raise statement (too complicated for inlining).
4241 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004242static int
Victor Stinner09532fe2019-05-10 23:39:09 +02004243do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004244{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004245 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004247 if (exc == NULL) {
4248 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01004249 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004250 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004251 type = exc_info->exc_type;
4252 value = exc_info->exc_value;
4253 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004254 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004255 _PyErr_SetString(tstate, PyExc_RuntimeError,
4256 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004257 return 0;
4258 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004259 Py_XINCREF(type);
4260 Py_XINCREF(value);
4261 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02004262 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004263 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004264 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004266 /* We support the following forms of raise:
4267 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004268 raise <instance>
4269 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004271 if (PyExceptionClass_Check(exc)) {
4272 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004273 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004274 if (value == NULL)
4275 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004276 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004277 _PyErr_Format(tstate, PyExc_TypeError,
4278 "calling %R should have returned an instance of "
4279 "BaseException, not %R",
4280 type, Py_TYPE(value));
4281 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004282 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004283 }
4284 else if (PyExceptionInstance_Check(exc)) {
4285 value = exc;
4286 type = PyExceptionInstance_Class(exc);
4287 Py_INCREF(type);
4288 }
4289 else {
4290 /* Not something you can raise. You get an exception
4291 anyway, just not what you specified :-) */
4292 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02004293 _PyErr_SetString(tstate, PyExc_TypeError,
4294 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004295 goto raise_error;
4296 }
Collin Winter828f04a2007-08-31 00:04:24 +00004297
Serhiy Storchakac0191582016-09-27 11:37:10 +03004298 assert(type != NULL);
4299 assert(value != NULL);
4300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004301 if (cause) {
4302 PyObject *fixed_cause;
4303 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004304 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004305 if (fixed_cause == NULL)
4306 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004307 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004308 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004309 else if (PyExceptionInstance_Check(cause)) {
4310 fixed_cause = cause;
4311 }
4312 else if (cause == Py_None) {
4313 Py_DECREF(cause);
4314 fixed_cause = NULL;
4315 }
4316 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004317 _PyErr_SetString(tstate, PyExc_TypeError,
4318 "exception causes must derive from "
4319 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004320 goto raise_error;
4321 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004322 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004323 }
Collin Winter828f04a2007-08-31 00:04:24 +00004324
Victor Stinner438a12d2019-05-24 17:01:38 +02004325 _PyErr_SetObject(tstate, type, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004326 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004327 Py_DECREF(value);
4328 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004329 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004330
4331raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004332 Py_XDECREF(value);
4333 Py_XDECREF(type);
4334 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004335 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004336}
4337
Tim Petersd6d010b2001-06-21 02:49:55 +00004338/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004339 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004340
Guido van Rossum0368b722007-05-11 16:50:42 +00004341 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4342 with a variable target.
4343*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004344
Barry Warsawe42b18f1997-08-25 22:13:04 +00004345static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004346unpack_iterable(PyThreadState *tstate, PyObject *v,
4347 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004348{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004349 int i = 0, j = 0;
4350 Py_ssize_t ll = 0;
4351 PyObject *it; /* iter(v) */
4352 PyObject *w;
4353 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004355 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004357 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004358 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004359 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004360 v->ob_type->tp_iter == NULL && !PySequence_Check(v))
4361 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004362 _PyErr_Format(tstate, PyExc_TypeError,
4363 "cannot unpack non-iterable %.200s object",
4364 v->ob_type->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004365 }
4366 return 0;
4367 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004369 for (; i < argcnt; i++) {
4370 w = PyIter_Next(it);
4371 if (w == NULL) {
4372 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004373 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04004374 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004375 _PyErr_Format(tstate, PyExc_ValueError,
4376 "not enough values to unpack "
4377 "(expected %d, got %d)",
4378 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004379 }
4380 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004381 _PyErr_Format(tstate, PyExc_ValueError,
4382 "not enough values to unpack "
4383 "(expected at least %d, got %d)",
4384 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004385 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004386 }
4387 goto Error;
4388 }
4389 *--sp = w;
4390 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004392 if (argcntafter == -1) {
4393 /* We better have exhausted the iterator now. */
4394 w = PyIter_Next(it);
4395 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004396 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004397 goto Error;
4398 Py_DECREF(it);
4399 return 1;
4400 }
4401 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02004402 _PyErr_Format(tstate, PyExc_ValueError,
4403 "too many values to unpack (expected %d)",
4404 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004405 goto Error;
4406 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004408 l = PySequence_List(it);
4409 if (l == NULL)
4410 goto Error;
4411 *--sp = l;
4412 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004414 ll = PyList_GET_SIZE(l);
4415 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004416 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04004417 "not enough values to unpack (expected at least %d, got %zd)",
4418 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004419 goto Error;
4420 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004422 /* Pop the "after-variable" args off the list. */
4423 for (j = argcntafter; j > 0; j--, i++) {
4424 *--sp = PyList_GET_ITEM(l, ll - j);
4425 }
4426 /* Resize the list. */
4427 Py_SIZE(l) = ll - argcntafter;
4428 Py_DECREF(it);
4429 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004430
Tim Petersd6d010b2001-06-21 02:49:55 +00004431Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004432 for (; i > 0; i--, sp++)
4433 Py_DECREF(*sp);
4434 Py_XDECREF(it);
4435 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004436}
4437
4438
Guido van Rossum96a42c81992-01-12 02:29:51 +00004439#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004440static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004441prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004442{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004443 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02004444 if (PyObject_Print(v, stdout, 0) != 0) {
4445 /* Don't know what else to do */
4446 _PyErr_Clear(tstate);
4447 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004448 printf("\n");
4449 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004450}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004451#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004452
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004453static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004454call_exc_trace(Py_tracefunc func, PyObject *self,
4455 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004456{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004457 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004458 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004459 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004460 if (value == NULL) {
4461 value = Py_None;
4462 Py_INCREF(value);
4463 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004464 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01004465 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004466 arg = PyTuple_Pack(3, type, value, traceback);
4467 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004468 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004469 return;
4470 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004471 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004472 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02004473 if (err == 0) {
4474 _PyErr_Restore(tstate, type, value, orig_traceback);
4475 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004476 else {
4477 Py_XDECREF(type);
4478 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004479 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004480 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004481}
4482
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004483static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004484call_trace_protected(Py_tracefunc func, PyObject *obj,
4485 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004486 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004487{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004488 PyObject *type, *value, *traceback;
4489 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004490 _PyErr_Fetch(tstate, &type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004491 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004492 if (err == 0)
4493 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004494 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004495 return 0;
4496 }
4497 else {
4498 Py_XDECREF(type);
4499 Py_XDECREF(value);
4500 Py_XDECREF(traceback);
4501 return -1;
4502 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004503}
4504
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004505static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004506call_trace(Py_tracefunc func, PyObject *obj,
4507 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004508 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004509{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004510 int result;
4511 if (tstate->tracing)
4512 return 0;
4513 tstate->tracing++;
4514 tstate->use_tracing = 0;
4515 result = func(obj, frame, what, arg);
4516 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4517 || (tstate->c_profilefunc != NULL));
4518 tstate->tracing--;
4519 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004520}
4521
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004522PyObject *
4523_PyEval_CallTracing(PyObject *func, PyObject *args)
4524{
Victor Stinner50b48572018-11-01 01:51:40 +01004525 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004526 int save_tracing = tstate->tracing;
4527 int save_use_tracing = tstate->use_tracing;
4528 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004530 tstate->tracing = 0;
4531 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4532 || (tstate->c_profilefunc != NULL));
4533 result = PyObject_Call(func, args, NULL);
4534 tstate->tracing = save_tracing;
4535 tstate->use_tracing = save_use_tracing;
4536 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004537}
4538
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004539/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004540static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004541maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004542 PyThreadState *tstate, PyFrameObject *frame,
4543 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004544{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004545 int result = 0;
4546 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004548 /* If the last instruction executed isn't in the current
4549 instruction window, reset the window.
4550 */
4551 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4552 PyAddrPair bounds;
4553 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4554 &bounds);
4555 *instr_lb = bounds.ap_lower;
4556 *instr_ub = bounds.ap_upper;
4557 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004558 /* If the last instruction falls at the start of a line or if it
4559 represents a jump backwards, update the frame's line number and
4560 then call the trace function if we're tracing source lines.
4561 */
4562 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004563 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004564 if (frame->f_trace_lines) {
4565 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4566 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004567 }
George King20faa682017-10-18 17:44:22 -07004568 /* Always emit an opcode event if we're tracing all opcodes. */
4569 if (frame->f_trace_opcodes) {
4570 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4571 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004572 *instr_prev = frame->f_lasti;
4573 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004574}
4575
Fred Drake5755ce62001-06-27 19:19:46 +00004576void
4577PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004578{
Steve Dowerb82e17e2019-05-23 08:45:22 -07004579 if (PySys_Audit("sys.setprofile", NULL) < 0) {
4580 return;
4581 }
4582
Victor Stinner50b48572018-11-01 01:51:40 +01004583 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004584 PyObject *temp = tstate->c_profileobj;
4585 Py_XINCREF(arg);
4586 tstate->c_profilefunc = NULL;
4587 tstate->c_profileobj = NULL;
4588 /* Must make sure that tracing is not ignored if 'temp' is freed */
4589 tstate->use_tracing = tstate->c_tracefunc != NULL;
4590 Py_XDECREF(temp);
4591 tstate->c_profilefunc = func;
4592 tstate->c_profileobj = arg;
4593 /* Flag that tracing or profiling is turned on */
4594 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004595}
4596
4597void
4598PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4599{
Steve Dowerb82e17e2019-05-23 08:45:22 -07004600 if (PySys_Audit("sys.settrace", NULL) < 0) {
4601 return;
4602 }
4603
Victor Stinner09532fe2019-05-10 23:39:09 +02004604 _PyRuntimeState *runtime = &_PyRuntime;
4605 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004606 PyObject *temp = tstate->c_traceobj;
Victor Stinner09532fe2019-05-10 23:39:09 +02004607 runtime->ceval.tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004608 Py_XINCREF(arg);
4609 tstate->c_tracefunc = NULL;
4610 tstate->c_traceobj = NULL;
4611 /* Must make sure that profiling is not ignored if 'temp' is freed */
4612 tstate->use_tracing = tstate->c_profilefunc != NULL;
4613 Py_XDECREF(temp);
4614 tstate->c_tracefunc = func;
4615 tstate->c_traceobj = arg;
4616 /* Flag that tracing or profiling is turned on */
4617 tstate->use_tracing = ((func != NULL)
4618 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004619}
4620
Yury Selivanov75445082015-05-11 22:57:16 -04004621void
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004622_PyEval_SetCoroutineOriginTrackingDepth(int new_depth)
4623{
4624 assert(new_depth >= 0);
Victor Stinner50b48572018-11-01 01:51:40 +01004625 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004626 tstate->coroutine_origin_tracking_depth = new_depth;
4627}
4628
4629int
4630_PyEval_GetCoroutineOriginTrackingDepth(void)
4631{
Victor Stinner50b48572018-11-01 01:51:40 +01004632 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004633 return tstate->coroutine_origin_tracking_depth;
4634}
4635
4636void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004637_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004638{
Victor Stinner50b48572018-11-01 01:51:40 +01004639 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004640
4641 if (PySys_Audit("sys.set_coroutine_wrapper", NULL) < 0) {
4642 return;
4643 }
4644
Yury Selivanov75445082015-05-11 22:57:16 -04004645 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004646 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004647}
4648
4649PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004650_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004651{
Victor Stinner50b48572018-11-01 01:51:40 +01004652 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanov75445082015-05-11 22:57:16 -04004653 return tstate->coroutine_wrapper;
4654}
4655
Yury Selivanoveb636452016-09-08 22:01:51 -07004656void
4657_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4658{
Victor Stinner50b48572018-11-01 01:51:40 +01004659 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004660
4661 if (PySys_Audit("sys.set_asyncgen_hook_firstiter", NULL) < 0) {
4662 return;
4663 }
4664
Yury Selivanoveb636452016-09-08 22:01:51 -07004665 Py_XINCREF(firstiter);
4666 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4667}
4668
4669PyObject *
4670_PyEval_GetAsyncGenFirstiter(void)
4671{
Victor Stinner50b48572018-11-01 01:51:40 +01004672 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004673 return tstate->async_gen_firstiter;
4674}
4675
4676void
4677_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4678{
Victor Stinner50b48572018-11-01 01:51:40 +01004679 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004680
4681 if (PySys_Audit("sys.set_asyncgen_hook_finalizer", NULL) < 0) {
4682 return;
4683 }
4684
Yury Selivanoveb636452016-09-08 22:01:51 -07004685 Py_XINCREF(finalizer);
4686 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4687}
4688
4689PyObject *
4690_PyEval_GetAsyncGenFinalizer(void)
4691{
Victor Stinner50b48572018-11-01 01:51:40 +01004692 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004693 return tstate->async_gen_finalizer;
4694}
4695
Victor Stinner438a12d2019-05-24 17:01:38 +02004696static PyFrameObject *
4697_PyEval_GetFrame(PyThreadState *tstate)
4698{
4699 return _PyRuntime.gilstate.getframe(tstate);
4700}
4701
4702PyFrameObject *
4703PyEval_GetFrame(void)
4704{
4705 PyThreadState *tstate = _PyThreadState_GET();
4706 return _PyEval_GetFrame(tstate);
4707}
4708
Guido van Rossumb209a111997-04-29 18:18:01 +00004709PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004710PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004711{
Victor Stinner438a12d2019-05-24 17:01:38 +02004712 PyThreadState *tstate = _PyThreadState_GET();
4713 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004714 if (current_frame == NULL)
Victor Stinner438a12d2019-05-24 17:01:38 +02004715 return tstate->interp->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004716 else
4717 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004718}
4719
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004720/* Convenience function to get a builtin from its name */
4721PyObject *
4722_PyEval_GetBuiltinId(_Py_Identifier *name)
4723{
Victor Stinner438a12d2019-05-24 17:01:38 +02004724 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004725 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4726 if (attr) {
4727 Py_INCREF(attr);
4728 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004729 else if (!_PyErr_Occurred(tstate)) {
4730 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004731 }
4732 return attr;
4733}
4734
Guido van Rossumb209a111997-04-29 18:18:01 +00004735PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004736PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004737{
Victor Stinner438a12d2019-05-24 17:01:38 +02004738 PyThreadState *tstate = _PyThreadState_GET();
4739 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Victor Stinner41bb43a2013-10-29 01:19:37 +01004740 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004741 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004742 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004743 }
4744
Victor Stinner438a12d2019-05-24 17:01:38 +02004745 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01004746 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004747 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004748
4749 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004750 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004751}
4752
Guido van Rossumb209a111997-04-29 18:18:01 +00004753PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004754PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004755{
Victor Stinner438a12d2019-05-24 17:01:38 +02004756 PyThreadState *tstate = _PyThreadState_GET();
4757 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
4758 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004759 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004760 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004761
4762 assert(current_frame->f_globals != NULL);
4763 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004764}
4765
Guido van Rossum6135a871995-01-09 17:53:26 +00004766int
Tim Peters5ba58662001-07-16 02:29:45 +00004767PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004768{
Victor Stinner438a12d2019-05-24 17:01:38 +02004769 PyThreadState *tstate = _PyThreadState_GET();
4770 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004771 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004773 if (current_frame != NULL) {
4774 const int codeflags = current_frame->f_code->co_flags;
4775 const int compilerflags = codeflags & PyCF_MASK;
4776 if (compilerflags) {
4777 result = 1;
4778 cf->cf_flags |= compilerflags;
4779 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004780#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004781 if (codeflags & CO_GENERATOR_ALLOWED) {
4782 result = 1;
4783 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4784 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004785#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004786 }
4787 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004788}
4789
Guido van Rossum3f5da241990-12-20 15:06:42 +00004790
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004791const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004792PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004793{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004794 if (PyMethod_Check(func))
4795 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4796 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004797 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004798 else if (PyCFunction_Check(func))
4799 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4800 else
4801 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004802}
4803
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004804const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004805PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004806{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004807 if (PyMethod_Check(func))
4808 return "()";
4809 else if (PyFunction_Check(func))
4810 return "()";
4811 else if (PyCFunction_Check(func))
4812 return "()";
4813 else
4814 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004815}
4816
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004817#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004818if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004819 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4820 tstate, tstate->frame, \
4821 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004822 x = NULL; \
4823 } \
4824 else { \
4825 x = call; \
4826 if (tstate->c_profilefunc != NULL) { \
4827 if (x == NULL) { \
4828 call_trace_protected(tstate->c_profilefunc, \
4829 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004830 tstate, tstate->frame, \
4831 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004832 /* XXX should pass (type, value, tb) */ \
4833 } else { \
4834 if (call_trace(tstate->c_profilefunc, \
4835 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004836 tstate, tstate->frame, \
4837 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004838 Py_DECREF(x); \
4839 x = NULL; \
4840 } \
4841 } \
4842 } \
4843 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004844} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004845 x = call; \
4846 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004847
Victor Stinner415c5102017-01-11 00:54:57 +01004848/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4849 to reduce the stack consumption. */
4850Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Victor Stinner09532fe2019-05-10 23:39:09 +02004851call_function(PyThreadState *tstate, PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004852{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004853 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004854 PyObject *func = *pfunc;
4855 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004856 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4857 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004858 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004859
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004860 /* Always dispatch PyCFunction first, because these are
4861 presumed to be the most frequent callable object.
4862 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004863 if (PyCFunction_Check(func)) {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004864 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004865 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004866 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
jdemeyer56868f92018-07-21 10:30:59 +02004867 if (nargs > 0 && tstate->use_tracing) {
4868 /* We need to create a temporary bound method as argument
4869 for profiling.
4870
4871 If nargs == 0, then this cannot work because we have no
4872 "self". In any case, the call itself would raise
4873 TypeError (foo needs an argument), so we just skip
4874 profiling. */
4875 PyObject *self = stack[0];
4876 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
jdemeyer147d9552018-07-23 18:41:20 +02004877 if (func != NULL) {
4878 C_TRACE(x, _PyCFunction_FastCallKeywords(func,
4879 stack+1, nargs-1,
4880 kwnames));
4881 Py_DECREF(func);
INADA Naoki93fac8d2017-03-07 14:24:37 +09004882 }
jdemeyer147d9552018-07-23 18:41:20 +02004883 else {
4884 x = NULL;
4885 }
INADA Naoki93fac8d2017-03-07 14:24:37 +09004886 }
4887 else {
4888 x = _PyMethodDescr_FastCallKeywords(func, stack, nargs, kwnames);
4889 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004890 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004891 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004892 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
Victor Stinnerb69ee8c2016-11-28 18:32:31 +01004893 /* Optimize access to bound methods. Reuse the Python stack
4894 to pass 'self' as the first argument, replace 'func'
4895 with 'self'. It avoids the creation of a new temporary tuple
4896 for arguments (to replace func with self) when the method uses
4897 FASTCALL. */
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004898 PyObject *self = PyMethod_GET_SELF(func);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004899 Py_INCREF(self);
4900 func = PyMethod_GET_FUNCTION(func);
4901 Py_INCREF(func);
4902 Py_SETREF(*pfunc, self);
4903 nargs++;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004904 stack--;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004905 }
4906 else {
4907 Py_INCREF(func);
4908 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004909
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004910 if (PyFunction_Check(func)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004911 x = _PyFunction_FastCallKeywords(func, stack, nargs, kwnames);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004912 }
4913 else {
4914 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4915 }
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004916 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004917 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004918
Victor Stinner438a12d2019-05-24 17:01:38 +02004919 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004920
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004921 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004922 while ((*pp_stack) > pfunc) {
4923 w = EXT_POP(*pp_stack);
4924 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004925 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004927 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004928}
4929
Jeremy Hylton52820442001-01-03 23:52:36 +00004930static PyObject *
Victor Stinner09532fe2019-05-10 23:39:09 +02004931do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004932{
jdemeyere89de732018-09-19 12:06:20 +02004933 PyObject *result;
4934
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004935 if (PyCFunction_Check(func)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004936 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004937 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004938 }
jdemeyere89de732018-09-19 12:06:20 +02004939 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
jdemeyere89de732018-09-19 12:06:20 +02004940 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
4941 if (nargs > 0 && tstate->use_tracing) {
4942 /* We need to create a temporary bound method as argument
4943 for profiling.
4944
4945 If nargs == 0, then this cannot work because we have no
4946 "self". In any case, the call itself would raise
4947 TypeError (foo needs an argument), so we just skip
4948 profiling. */
4949 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
4950 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4951 if (func == NULL) {
4952 return NULL;
4953 }
4954
4955 C_TRACE(result, _PyCFunction_FastCallDict(func,
Victor Stinnerd17a6932018-11-09 16:56:48 +01004956 &_PyTuple_ITEMS(callargs)[1],
jdemeyere89de732018-09-19 12:06:20 +02004957 nargs - 1,
4958 kwdict));
4959 Py_DECREF(func);
4960 return result;
4961 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004962 }
jdemeyere89de732018-09-19 12:06:20 +02004963 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00004964}
4965
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004966/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004967 nb_index slot defined, and store in *pi.
4968 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004969 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004970 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004971*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004972int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004973_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004974{
Victor Stinner438a12d2019-05-24 17:01:38 +02004975 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004976 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004977 Py_ssize_t x;
4978 if (PyIndex_Check(v)) {
4979 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02004980 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004981 return 0;
4982 }
4983 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004984 _PyErr_SetString(tstate, PyExc_TypeError,
4985 "slice indices must be integers or "
4986 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004987 return 0;
4988 }
4989 *pi = x;
4990 }
4991 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004992}
4993
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004994int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004995_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004996{
Victor Stinner438a12d2019-05-24 17:01:38 +02004997 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004998 Py_ssize_t x;
4999 if (PyIndex_Check(v)) {
5000 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005001 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005002 return 0;
5003 }
5004 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005005 _PyErr_SetString(tstate, PyExc_TypeError,
5006 "slice indices must be integers or "
5007 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005008 return 0;
5009 }
5010 *pi = x;
5011 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005012}
5013
5014
Guido van Rossum486364b2007-06-30 05:01:58 +00005015#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005016 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00005017
Guido van Rossumb209a111997-04-29 18:18:01 +00005018static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005019cmp_outcome(PyThreadState *tstate, int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005020{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005021 int res = 0;
5022 switch (op) {
5023 case PyCmp_IS:
5024 res = (v == w);
5025 break;
5026 case PyCmp_IS_NOT:
5027 res = (v != w);
5028 break;
5029 case PyCmp_IN:
5030 res = PySequence_Contains(w, v);
5031 if (res < 0)
5032 return NULL;
5033 break;
5034 case PyCmp_NOT_IN:
5035 res = PySequence_Contains(w, v);
5036 if (res < 0)
5037 return NULL;
5038 res = !res;
5039 break;
5040 case PyCmp_EXC_MATCH:
5041 if (PyTuple_Check(w)) {
5042 Py_ssize_t i, length;
5043 length = PyTuple_Size(w);
5044 for (i = 0; i < length; i += 1) {
5045 PyObject *exc = PyTuple_GET_ITEM(w, i);
5046 if (!PyExceptionClass_Check(exc)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005047 _PyErr_SetString(tstate, PyExc_TypeError,
5048 CANNOT_CATCH_MSG);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005049 return NULL;
5050 }
5051 }
5052 }
5053 else {
5054 if (!PyExceptionClass_Check(w)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005055 _PyErr_SetString(tstate, PyExc_TypeError,
5056 CANNOT_CATCH_MSG);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005057 return NULL;
5058 }
5059 }
5060 res = PyErr_GivenExceptionMatches(v, w);
5061 break;
5062 default:
5063 return PyObject_RichCompare(v, w, op);
5064 }
5065 v = res ? Py_True : Py_False;
5066 Py_INCREF(v);
5067 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005068}
5069
Thomas Wouters52152252000-08-17 22:55:00 +00005070static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005071import_name(PyThreadState *tstate, PyFrameObject *f,
5072 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005073{
5074 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005075 PyObject *import_func, *res;
5076 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005077
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005078 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005079 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005080 if (!_PyErr_Occurred(tstate)) {
5081 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005082 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005083 return NULL;
5084 }
5085
5086 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005087 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005088 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02005089 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005090 return NULL;
5091 }
5092 res = PyImport_ImportModuleLevelObject(
5093 name,
5094 f->f_globals,
5095 f->f_locals == NULL ? Py_None : f->f_locals,
5096 fromlist,
5097 ilevel);
5098 return res;
5099 }
5100
5101 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005102
5103 stack[0] = name;
5104 stack[1] = f->f_globals;
5105 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5106 stack[3] = fromlist;
5107 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005108 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005109 Py_DECREF(import_func);
5110 return res;
5111}
5112
5113static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005114import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005115{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005116 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02005117 _Py_IDENTIFIER(__name__);
Xiang Zhang4830f582017-03-21 11:13:42 +08005118 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005119
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005120 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005121 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005122 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005123 /* Issue #17636: in case this failed because of a circular relative
5124 import, try to fallback on reading the module directly from
5125 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02005126 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005127 if (pkgname == NULL) {
5128 goto error;
5129 }
Oren Milman6db70332017-09-19 14:23:01 +03005130 if (!PyUnicode_Check(pkgname)) {
5131 Py_CLEAR(pkgname);
5132 goto error;
5133 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005134 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005135 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005136 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005137 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005138 }
Eric Snow3f9eee62017-09-15 16:35:20 -06005139 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005140 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02005141 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07005142 goto error;
5143 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005144 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005145 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005146 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005147 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005148 if (pkgname == NULL) {
5149 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5150 if (pkgname_or_unknown == NULL) {
5151 Py_XDECREF(pkgpath);
5152 return NULL;
5153 }
5154 } else {
5155 pkgname_or_unknown = pkgname;
5156 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005157
5158 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005159 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08005160 errmsg = PyUnicode_FromFormat(
5161 "cannot import name %R from %R (unknown location)",
5162 name, pkgname_or_unknown
5163 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005164 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005165 PyErr_SetImportError(errmsg, pkgname, NULL);
5166 }
5167 else {
5168 errmsg = PyUnicode_FromFormat(
5169 "cannot import name %R from %R (%S)",
5170 name, pkgname_or_unknown, pkgpath
5171 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005172 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005173 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005174 }
5175
Xiang Zhang4830f582017-03-21 11:13:42 +08005176 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005177 Py_XDECREF(pkgname_or_unknown);
5178 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005179 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005180}
Guido van Rossumac7be682001-01-17 15:42:30 +00005181
Thomas Wouters52152252000-08-17 22:55:00 +00005182static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005183import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00005184{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005185 _Py_IDENTIFIER(__all__);
5186 _Py_IDENTIFIER(__dict__);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005187 _Py_IDENTIFIER(__name__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005188 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005189 int skip_leading_underscores = 0;
5190 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005191
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005192 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5193 return -1; /* Unexpected error */
5194 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005195 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005196 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5197 return -1;
5198 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005199 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005200 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005201 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005202 return -1;
5203 }
5204 all = PyMapping_Keys(dict);
5205 Py_DECREF(dict);
5206 if (all == NULL)
5207 return -1;
5208 skip_leading_underscores = 1;
5209 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005211 for (pos = 0, err = 0; ; pos++) {
5212 name = PySequence_GetItem(all, pos);
5213 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005214 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005215 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02005216 }
5217 else {
5218 _PyErr_Clear(tstate);
5219 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005220 break;
5221 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005222 if (!PyUnicode_Check(name)) {
5223 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5224 if (modname == NULL) {
5225 Py_DECREF(name);
5226 err = -1;
5227 break;
5228 }
5229 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005230 _PyErr_Format(tstate, PyExc_TypeError,
5231 "module __name__ must be a string, not %.100s",
5232 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005233 }
5234 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005235 _PyErr_Format(tstate, PyExc_TypeError,
5236 "%s in %U.%s must be str, not %.100s",
5237 skip_leading_underscores ? "Key" : "Item",
5238 modname,
5239 skip_leading_underscores ? "__dict__" : "__all__",
5240 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005241 }
5242 Py_DECREF(modname);
5243 Py_DECREF(name);
5244 err = -1;
5245 break;
5246 }
5247 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005248 if (PyUnicode_READY(name) == -1) {
5249 Py_DECREF(name);
5250 err = -1;
5251 break;
5252 }
5253 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5254 Py_DECREF(name);
5255 continue;
5256 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005257 }
5258 value = PyObject_GetAttr(v, name);
5259 if (value == NULL)
5260 err = -1;
5261 else if (PyDict_CheckExact(locals))
5262 err = PyDict_SetItem(locals, name, value);
5263 else
5264 err = PyObject_SetItem(locals, name, value);
5265 Py_DECREF(name);
5266 Py_XDECREF(value);
5267 if (err != 0)
5268 break;
5269 }
5270 Py_DECREF(all);
5271 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005272}
5273
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005274static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005275check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005276{
5277 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005278 _PyErr_Format(tstate, PyExc_TypeError,
5279 "%.200s%.200s argument after * "
5280 "must be an iterable, not %.200s",
5281 PyEval_GetFuncName(func),
5282 PyEval_GetFuncDesc(func),
5283 args->ob_type->tp_name);
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005284 return -1;
5285 }
5286 return 0;
5287}
5288
5289static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005290format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005291{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005292 /* _PyDict_MergeEx raises attribute
5293 * error (percolated from an attempt
5294 * to get 'keys' attribute) instead of
5295 * a type error if its second argument
5296 * is not a mapping.
5297 */
Victor Stinner438a12d2019-05-24 17:01:38 +02005298 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
5299 _PyErr_Format(tstate, PyExc_TypeError,
5300 "%.200s%.200s argument after ** "
5301 "must be a mapping, not %.200s",
5302 PyEval_GetFuncName(func),
5303 PyEval_GetFuncDesc(func),
5304 kwargs->ob_type->tp_name);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005305 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005306 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005307 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02005308 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005309 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
5310 PyObject *key = PyTuple_GET_ITEM(val, 0);
5311 if (!PyUnicode_Check(key)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005312 _PyErr_Format(tstate, PyExc_TypeError,
5313 "%.200s%.200s keywords must be strings",
5314 PyEval_GetFuncName(func),
5315 PyEval_GetFuncDesc(func));
5316 }
5317 else {
5318 _PyErr_Format(tstate, PyExc_TypeError,
5319 "%.200s%.200s got multiple "
5320 "values for keyword argument '%U'",
5321 PyEval_GetFuncName(func),
5322 PyEval_GetFuncDesc(func),
5323 key);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005324 }
5325 Py_XDECREF(exc);
5326 Py_XDECREF(val);
5327 Py_XDECREF(tb);
5328 }
5329 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005330 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005331 }
5332 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005333}
5334
Guido van Rossumac7be682001-01-17 15:42:30 +00005335static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005336format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
5337 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005338{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005339 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005340
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005341 if (!obj)
5342 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005343
Serhiy Storchaka06515832016-11-20 09:13:07 +02005344 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005345 if (!obj_str)
5346 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005347
Victor Stinner438a12d2019-05-24 17:01:38 +02005348 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005349}
Guido van Rossum950361c1997-01-24 13:49:28 +00005350
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005351static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005352format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005353{
5354 PyObject *name;
5355 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02005356 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005357 return;
5358 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5359 name = PyTuple_GET_ITEM(co->co_cellvars,
5360 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005361 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005362 PyExc_UnboundLocalError,
5363 UNBOUNDLOCAL_ERROR_MSG,
5364 name);
5365 } else {
5366 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5367 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02005368 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005369 UNBOUNDFREE_ERROR_MSG, name);
5370 }
5371}
5372
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005373static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005374format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005375{
5376 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5377 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005378 _PyErr_Format(tstate, PyExc_TypeError,
5379 "'async with' received an object from __aenter__ "
5380 "that does not implement __await__: %.100s",
5381 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005382 }
5383 else if (prevopcode == WITH_CLEANUP_START) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005384 _PyErr_Format(tstate, PyExc_TypeError,
5385 "'async with' received an object from __aexit__ "
5386 "that does not implement __await__: %.100s",
5387 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005388 }
5389 }
5390}
5391
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005392static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005393unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005394 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005395{
5396 PyObject *res;
5397 if (Py_REFCNT(v) == 2) {
5398 /* In the common case, there are 2 references to the value
5399 * stored in 'variable' when the += is performed: one on the
5400 * value stack (in 'v') and one still stored in the
5401 * 'variable'. We try to delete the variable now to reduce
5402 * the refcnt to 1.
5403 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005404 int opcode, oparg;
5405 NEXTOPARG();
5406 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005407 case STORE_FAST:
5408 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005409 PyObject **fastlocals = f->f_localsplus;
5410 if (GETLOCAL(oparg) == v)
5411 SETLOCAL(oparg, NULL);
5412 break;
5413 }
5414 case STORE_DEREF:
5415 {
5416 PyObject **freevars = (f->f_localsplus +
5417 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005418 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005419 if (PyCell_GET(c) == v) {
5420 PyCell_SET(c, NULL);
5421 Py_DECREF(v);
5422 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005423 break;
5424 }
5425 case STORE_NAME:
5426 {
5427 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005428 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005429 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005430 if (locals && PyDict_CheckExact(locals)) {
5431 PyObject *w = PyDict_GetItemWithError(locals, name);
5432 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02005433 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005434 {
5435 Py_DECREF(v);
5436 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005437 }
5438 }
5439 break;
5440 }
5441 }
5442 }
5443 res = v;
5444 PyUnicode_Append(&res, w);
5445 return res;
5446}
5447
Guido van Rossum950361c1997-01-24 13:49:28 +00005448#ifdef DYNAMIC_EXECUTION_PROFILE
5449
Skip Montanarof118cb12001-10-15 20:51:38 +00005450static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005451getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005452{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005453 int i;
5454 PyObject *l = PyList_New(256);
5455 if (l == NULL) return NULL;
5456 for (i = 0; i < 256; i++) {
5457 PyObject *x = PyLong_FromLong(a[i]);
5458 if (x == NULL) {
5459 Py_DECREF(l);
5460 return NULL;
5461 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005462 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005463 }
5464 for (i = 0; i < 256; i++)
5465 a[i] = 0;
5466 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005467}
5468
5469PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005470_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005471{
5472#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005473 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005474#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005475 int i;
5476 PyObject *l = PyList_New(257);
5477 if (l == NULL) return NULL;
5478 for (i = 0; i < 257; i++) {
5479 PyObject *x = getarray(dxpairs[i]);
5480 if (x == NULL) {
5481 Py_DECREF(l);
5482 return NULL;
5483 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005484 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005485 }
5486 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005487#endif
5488}
5489
5490#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005491
5492Py_ssize_t
5493_PyEval_RequestCodeExtraIndex(freefunc free)
5494{
Victor Stinnercaba55b2018-08-03 15:33:52 +02005495 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -07005496 Py_ssize_t new_index;
5497
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005498 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005499 return -1;
5500 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005501 new_index = interp->co_extra_user_count++;
5502 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005503 return new_index;
5504}
Łukasz Langaa785c872016-09-09 17:37:37 -07005505
5506static void
5507dtrace_function_entry(PyFrameObject *f)
5508{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005509 const char *filename;
5510 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005511 int lineno;
5512
5513 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5514 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5515 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5516
5517 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5518}
5519
5520static void
5521dtrace_function_return(PyFrameObject *f)
5522{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005523 const char *filename;
5524 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005525 int lineno;
5526
5527 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5528 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5529 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5530
5531 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5532}
5533
5534/* DTrace equivalent of maybe_call_line_trace. */
5535static void
5536maybe_dtrace_line(PyFrameObject *frame,
5537 int *instr_lb, int *instr_ub, int *instr_prev)
5538{
5539 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005540 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005541
5542 /* If the last instruction executed isn't in the current
5543 instruction window, reset the window.
5544 */
5545 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5546 PyAddrPair bounds;
5547 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5548 &bounds);
5549 *instr_lb = bounds.ap_lower;
5550 *instr_ub = bounds.ap_upper;
5551 }
5552 /* If the last instruction falls at the start of a line or if
5553 it represents a jump backwards, update the frame's line
5554 number and call the trace function. */
5555 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5556 frame->f_lineno = line;
5557 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5558 if (!co_filename)
5559 co_filename = "?";
5560 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5561 if (!co_name)
5562 co_name = "?";
5563 PyDTrace_LINE(co_filename, co_name, line);
5564 }
5565 *instr_prev = frame->f_lasti;
5566}