blob: f9ff4e09f17e20564b29d7b93c942b6aa6a33e91 [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
Eric Snow396e0a82019-05-31 21:16:47 -0600220exit_thread_if_finalizing(PyThreadState *tstate)
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400221{
Eric Snow396e0a82019-05-31 21:16:47 -0600222 _PyRuntimeState *runtime = tstate->interp->runtime;
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400223 /* _Py_Finalizing is protected by the GIL */
Victor Stinner09532fe2019-05-10 23:39:09 +0200224 if (runtime->finalizing != NULL && !_Py_CURRENTLY_FINALIZING(runtime, tstate)) {
225 drop_gil(&runtime->ceval, tstate);
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400226 PyThread_exit_thread();
Joannah Nanjekyef781d202019-04-29 04:38:45 -0400227 }
228}
229
Antoine Pitrou1df15362010-09-13 14:16:46 +0000230void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000231PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000232{
Victor Stinner09532fe2019-05-10 23:39:09 +0200233 _PyRuntimeState *runtime = &_PyRuntime;
234 struct _ceval_runtime_state *ceval = &runtime->ceval;
235 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
236 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
Victor Stinner09532fe2019-05-10 23:39:09 +0200238 }
239 take_gil(ceval, tstate);
Eric Snow396e0a82019-05-31 21:16:47 -0600240 exit_thread_if_finalizing(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000241}
242
243void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000244PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000245{
Victor Stinner09532fe2019-05-10 23:39:09 +0200246 _PyRuntimeState *runtime = &_PyRuntime;
247 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100249 We therefore avoid PyThreadState_Get() which dumps a fatal error
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 in debug mode.
251 */
Victor Stinner09532fe2019-05-10 23:39:09 +0200252 drop_gil(&runtime->ceval, tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000253}
254
255void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000256PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000257{
Victor Stinner09532fe2019-05-10 23:39:09 +0200258 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200260 }
Eric Snow396e0a82019-05-31 21:16:47 -0600261 assert(tstate->interp != NULL);
Victor Stinner09532fe2019-05-10 23:39:09 +0200262
Eric Snow396e0a82019-05-31 21:16:47 -0600263 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200264 struct _ceval_runtime_state *ceval = &runtime->ceval;
265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 /* Check someone has called PyEval_InitThreads() to create the lock */
Victor Stinner09532fe2019-05-10 23:39:09 +0200267 assert(gil_created(&ceval->gil));
268 take_gil(ceval, tstate);
Eric Snow396e0a82019-05-31 21:16:47 -0600269 exit_thread_if_finalizing(tstate);
Victor Stinner09532fe2019-05-10 23:39:09 +0200270 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
271 Py_FatalError("PyEval_AcquireThread: non-NULL old thread state");
272 }
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000273}
274
275void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000276PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000277{
Victor Stinner09532fe2019-05-10 23:39:09 +0200278 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200280 }
Eric Snow396e0a82019-05-31 21:16:47 -0600281 assert(tstate->interp != NULL);
Victor Stinner09532fe2019-05-10 23:39:09 +0200282
Eric Snow396e0a82019-05-31 21:16:47 -0600283 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200284 PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
285 if (new_tstate != tstate) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200287 }
288 drop_gil(&runtime->ceval, tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000289}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000290
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200291/* This function is called from PyOS_AfterFork_Child to destroy all threads
292 * which are not running in the child process, and clear internal locks
293 * which might be held by those threads.
294 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000295
296void
Victor Stinnerd5d9e812019-05-13 12:35:37 +0200297_PyEval_ReInitThreads(_PyRuntimeState *runtime)
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000298{
Victor Stinner09532fe2019-05-10 23:39:09 +0200299 struct _ceval_runtime_state *ceval = &runtime->ceval;
300 if (!gil_created(&ceval->gil)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 return;
Victor Stinner09532fe2019-05-10 23:39:09 +0200302 }
303 recreate_gil(&ceval->gil);
304 PyThreadState *current_tstate = _PyRuntimeState_GetThreadState(runtime);
305 take_gil(ceval, current_tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700306
Victor Stinner09532fe2019-05-10 23:39:09 +0200307 struct _pending_calls *pending = &ceval->pending;
308 pending->lock = PyThread_allocate_lock();
309 if (pending->lock == NULL) {
Eric Snow8479a342019-03-08 23:44:33 -0700310 Py_FatalError("Can't initialize threads for pending calls");
311 }
Jesse Nollera8513972008-07-17 16:49:17 +0000312
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200313 /* Destroy all threads except the current one */
Eric Snow396e0a82019-05-31 21:16:47 -0600314 _PyThreadState_DeleteExcept(current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000315}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000316
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000317/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600318 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000319
320void
Victor Stinner09532fe2019-05-10 23:39:09 +0200321_PyEval_SignalAsyncExc(struct _ceval_runtime_state *ceval)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000322{
Victor Stinner09532fe2019-05-10 23:39:09 +0200323 SIGNAL_ASYNC_EXC(ceval);
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000324}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000325
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000326PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000327PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000328{
Victor Stinner09532fe2019-05-10 23:39:09 +0200329 _PyRuntimeState *runtime = &_PyRuntime;
330 struct _ceval_runtime_state *ceval = &runtime->ceval;
331 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
332 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 Py_FatalError("PyEval_SaveThread: NULL tstate");
Victor Stinner09532fe2019-05-10 23:39:09 +0200334 }
335 assert(gil_created(&ceval->gil));
336 drop_gil(ceval, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000337 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000338}
339
340void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000341PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000342{
Victor Stinner09532fe2019-05-10 23:39:09 +0200343 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 }
Eric Snow396e0a82019-05-31 21:16:47 -0600346 assert(tstate->interp != NULL);
347
348 _PyRuntimeState *runtime = tstate->interp->runtime;
349 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinner09532fe2019-05-10 23:39:09 +0200350 assert(gil_created(&ceval->gil));
Victor Stinner2914bb32018-01-29 11:57:45 +0100351
352 int err = errno;
Victor Stinner09532fe2019-05-10 23:39:09 +0200353 take_gil(ceval, tstate);
Eric Snow396e0a82019-05-31 21:16:47 -0600354 exit_thread_if_finalizing(tstate);
Victor Stinner2914bb32018-01-29 11:57:45 +0100355 errno = err;
356
Victor Stinner09532fe2019-05-10 23:39:09 +0200357 _PyThreadState_Swap(&runtime->gilstate, tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000358}
359
360
Guido van Rossuma9672091994-09-14 13:31:22 +0000361/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
362 signal handlers or Mac I/O completion routines) can schedule calls
363 to a function to be called synchronously.
364 The synchronous function is called with one void* argument.
365 It should return 0 for success or -1 for failure -- failure should
366 be accompanied by an exception.
367
368 If registry succeeds, the registry function returns 0; if it fails
369 (e.g. due to too many pending calls) it returns -1 (without setting
370 an exception condition).
371
372 Note that because registry may occur from within signal handlers,
373 or other asynchronous events, calling malloc() is unsafe!
374
Guido van Rossuma9672091994-09-14 13:31:22 +0000375 Any thread can schedule pending calls, but only the main thread
376 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000377 There is no facility to schedule calls to a particular thread, but
378 that should be easy to change, should that ever be required. In
379 that case, the static variables here should go into the python
380 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000381*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000382
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200383void
Victor Stinner09532fe2019-05-10 23:39:09 +0200384_PyEval_SignalReceived(struct _ceval_runtime_state *ceval)
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200385{
386 /* bpo-30703: Function called when the C signal handler of Python gets a
387 signal. We cannot queue a callback using Py_AddPendingCall() since
388 that function is not async-signal-safe. */
Victor Stinner09532fe2019-05-10 23:39:09 +0200389 SIGNAL_PENDING_SIGNALS(ceval);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200390}
391
Eric Snow5be45a62019-03-08 22:47:07 -0700392/* Push one item onto the queue while holding the lock. */
393static int
Eric Snowb75b1a352019-04-12 10:20:10 -0600394_push_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600395 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700396{
Eric Snow842a2f02019-03-15 15:47:51 -0600397 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700398 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600399 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700400 return -1; /* Queue full */
401 }
Eric Snow842a2f02019-03-15 15:47:51 -0600402 pending->calls[i].func = func;
403 pending->calls[i].arg = arg;
404 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700405 return 0;
406}
407
408/* Pop one item off the queue while holding the lock. */
409static void
Eric Snowb75b1a352019-04-12 10:20:10 -0600410_pop_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600411 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700412{
Eric Snow842a2f02019-03-15 15:47:51 -0600413 int i = pending->first;
414 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700415 return; /* Queue empty */
416 }
417
Eric Snow842a2f02019-03-15 15:47:51 -0600418 *func = pending->calls[i].func;
419 *arg = pending->calls[i].arg;
420 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700421}
422
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200423/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000424 scheduling to be made from any thread, and even from an executing
425 callback.
426 */
427
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000428int
Victor Stinner438a12d2019-05-24 17:01:38 +0200429_PyEval_AddPendingCall(PyThreadState *tstate,
430 struct _ceval_runtime_state *ceval,
Victor Stinner09532fe2019-05-10 23:39:09 +0200431 int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000432{
Victor Stinner09532fe2019-05-10 23:39:09 +0200433 struct _pending_calls *pending = &ceval->pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600434
435 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
436 if (pending->finishing) {
437 PyThread_release_lock(pending->lock);
438
439 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +0200440 _PyErr_Fetch(tstate, &exc, &val, &tb);
441 _PyErr_SetString(tstate, PyExc_SystemError,
Eric Snow842a2f02019-03-15 15:47:51 -0600442 "Py_AddPendingCall: cannot add pending calls "
443 "(Python shutting down)");
Victor Stinner438a12d2019-05-24 17:01:38 +0200444 _PyErr_Print(tstate);
445 _PyErr_Restore(tstate, exc, val, tb);
Eric Snow842a2f02019-03-15 15:47:51 -0600446 return -1;
447 }
Eric Snowb75b1a352019-04-12 10:20:10 -0600448 int result = _push_pending_call(pending, func, arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600449 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700450
Eric Snowb75b1a352019-04-12 10:20:10 -0600451 /* signal main loop */
Victor Stinner09532fe2019-05-10 23:39:09 +0200452 SIGNAL_PENDING_CALLS(ceval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000454}
455
Victor Stinner09532fe2019-05-10 23:39:09 +0200456int
457Py_AddPendingCall(int (*func)(void *), void *arg)
458{
Victor Stinner438a12d2019-05-24 17:01:38 +0200459 _PyRuntimeState *runtime = &_PyRuntime;
460 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
461 return _PyEval_AddPendingCall(tstate, &runtime->ceval, func, arg);
Victor Stinner09532fe2019-05-10 23:39:09 +0200462}
463
Eric Snowfdf282d2019-01-11 14:26:55 -0700464static int
Victor Stinner09532fe2019-05-10 23:39:09 +0200465handle_signals(_PyRuntimeState *runtime)
Eric Snowfdf282d2019-01-11 14:26:55 -0700466{
Eric Snow5be45a62019-03-08 22:47:07 -0700467 /* Only handle signals on main thread. PyEval_InitThreads must
468 * have been called already.
469 */
Victor Stinner09532fe2019-05-10 23:39:09 +0200470 if (PyThread_get_thread_ident() != runtime->main_thread) {
Eric Snowfdf282d2019-01-11 14:26:55 -0700471 return 0;
472 }
Eric Snow64d6cc82019-02-23 15:40:43 -0700473 /*
474 * Ensure that the thread isn't currently running some other
475 * interpreter.
476 */
Victor Stinner09532fe2019-05-10 23:39:09 +0200477 PyInterpreterState *interp = _PyRuntimeState_GetThreadState(runtime)->interp;
478 if (interp != runtime->interpreters.main) {
Eric Snow64d6cc82019-02-23 15:40:43 -0700479 return 0;
480 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700481
Victor Stinner09532fe2019-05-10 23:39:09 +0200482 struct _ceval_runtime_state *ceval = &runtime->ceval;
483 UNSIGNAL_PENDING_SIGNALS(ceval);
Eric Snow64d6cc82019-02-23 15:40:43 -0700484 if (_PyErr_CheckSignals() < 0) {
Victor Stinner09532fe2019-05-10 23:39:09 +0200485 SIGNAL_PENDING_SIGNALS(ceval); /* We're not done yet */
Eric Snowfdf282d2019-01-11 14:26:55 -0700486 return -1;
487 }
488 return 0;
489}
490
491static int
Victor Stinner09532fe2019-05-10 23:39:09 +0200492make_pending_calls(_PyRuntimeState *runtime)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000493{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200494 static int busy = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000495
Eric Snowb75b1a352019-04-12 10:20:10 -0600496 /* only service pending calls on main thread */
Victor Stinner09532fe2019-05-10 23:39:09 +0200497 if (PyThread_get_thread_ident() != runtime->main_thread) {
Eric Snowb75b1a352019-04-12 10:20:10 -0600498 return 0;
499 }
500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 /* don't perform recursive pending calls */
Eric Snowfdf282d2019-01-11 14:26:55 -0700502 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700504 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200505 busy = 1;
Victor Stinner09532fe2019-05-10 23:39:09 +0200506 struct _ceval_runtime_state *ceval = &runtime->ceval;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200507 /* unsignal before starting to call callbacks, so that any callback
508 added in-between re-signals */
Victor Stinner09532fe2019-05-10 23:39:09 +0200509 UNSIGNAL_PENDING_CALLS(ceval);
Eric Snowfdf282d2019-01-11 14:26:55 -0700510 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 /* perform a bounded number of calls, in case of recursion */
Victor Stinner09532fe2019-05-10 23:39:09 +0200513 struct _pending_calls *pending = &ceval->pending;
Eric Snowfdf282d2019-01-11 14:26:55 -0700514 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700515 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 void *arg = NULL;
517
518 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600519 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Eric Snowb75b1a352019-04-12 10:20:10 -0600520 _pop_pending_call(pending, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600521 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700522
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100523 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700524 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100525 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700526 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700527 res = func(arg);
528 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200529 goto error;
530 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200532
Charles-François Natalif23339a2011-07-23 18:15:43 +0200533 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700534 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200535
536error:
537 busy = 0;
Victor Stinner09532fe2019-05-10 23:39:09 +0200538 SIGNAL_PENDING_CALLS(ceval);
Eric Snowfdf282d2019-01-11 14:26:55 -0700539 return res;
540}
541
Eric Snow842a2f02019-03-15 15:47:51 -0600542void
Victor Stinner09532fe2019-05-10 23:39:09 +0200543_Py_FinishPendingCalls(_PyRuntimeState *runtime)
Eric Snow842a2f02019-03-15 15:47:51 -0600544{
Eric Snow842a2f02019-03-15 15:47:51 -0600545 assert(PyGILState_Check());
546
Victor Stinner438a12d2019-05-24 17:01:38 +0200547 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinner09532fe2019-05-10 23:39:09 +0200548 struct _pending_calls *pending = &runtime->ceval.pending;
549
Eric Snow842a2f02019-03-15 15:47:51 -0600550 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
551 pending->finishing = 1;
552 PyThread_release_lock(pending->lock);
553
554 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
555 return;
556 }
557
Victor Stinner09532fe2019-05-10 23:39:09 +0200558 if (make_pending_calls(runtime) < 0) {
Eric Snow842a2f02019-03-15 15:47:51 -0600559 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +0200560 _PyErr_Fetch(tstate, &exc, &val, &tb);
Eric Snow842a2f02019-03-15 15:47:51 -0600561 PyErr_BadInternalCall();
562 _PyErr_ChainExceptions(exc, val, tb);
Victor Stinner438a12d2019-05-24 17:01:38 +0200563 _PyErr_Print(tstate);
Eric Snow842a2f02019-03-15 15:47:51 -0600564 }
565}
566
Eric Snowfdf282d2019-01-11 14:26:55 -0700567/* Py_MakePendingCalls() is a simple wrapper for the sake
568 of backward-compatibility. */
569int
570Py_MakePendingCalls(void)
571{
572 assert(PyGILState_Check());
573
574 /* Python signal handler doesn't really queue a callback: it only signals
575 that a signal was received, see _PyEval_SignalReceived(). */
Victor Stinner09532fe2019-05-10 23:39:09 +0200576 _PyRuntimeState *runtime = &_PyRuntime;
577 int res = handle_signals(runtime);
Eric Snowfdf282d2019-01-11 14:26:55 -0700578 if (res != 0) {
579 return res;
580 }
581
Victor Stinner09532fe2019-05-10 23:39:09 +0200582 res = make_pending_calls(runtime);
Eric Snowb75b1a352019-04-12 10:20:10 -0600583 if (res != 0) {
584 return res;
585 }
586
587 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000588}
589
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000590/* The interpreter's recursion limit */
591
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000592#ifndef Py_DEFAULT_RECURSION_LIMIT
593#define Py_DEFAULT_RECURSION_LIMIT 1000
594#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600595
Eric Snow05351c12017-09-05 21:43:08 -0700596int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000597
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600598void
599_PyEval_Initialize(struct _ceval_runtime_state *state)
600{
601 state->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
602 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
603 _gil_initialize(&state->gil);
604}
605
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000606int
607Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000608{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600609 return _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000610}
611
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000612void
613Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000614{
Victor Stinner09532fe2019-05-10 23:39:09 +0200615 struct _ceval_runtime_state *ceval = &_PyRuntime.ceval;
616 ceval->recursion_limit = new_limit;
617 _Py_CheckRecursionLimit = ceval->recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000618}
619
Armin Rigo2b3eb402003-10-28 12:05:48 +0000620/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
621 if the recursion_depth reaches _Py_CheckRecursionLimit.
622 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
623 to guarantee that _Py_CheckRecursiveCall() is regularly called.
624 Without USE_STACKCHECK, there is no need for this. */
625int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300626_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000627{
Victor Stinner09532fe2019-05-10 23:39:09 +0200628 _PyRuntimeState *runtime = &_PyRuntime;
629 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
630 int recursion_limit = runtime->ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000631
632#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700633 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 if (PyOS_CheckStack()) {
635 --tstate->recursion_depth;
Victor Stinner438a12d2019-05-24 17:01:38 +0200636 _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 return -1;
638 }
pdox18967932017-10-25 23:03:01 -0700639 /* Needed for ABI backwards-compatibility (see bpo-31857) */
Eric Snow05351c12017-09-05 21:43:08 -0700640 _Py_CheckRecursionLimit = recursion_limit;
pdox18967932017-10-25 23:03:01 -0700641#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 if (tstate->recursion_critical)
643 /* Somebody asked that we don't check for recursion. */
644 return 0;
645 if (tstate->overflowed) {
646 if (tstate->recursion_depth > recursion_limit + 50) {
647 /* Overflowing while handling an overflow. Give up. */
648 Py_FatalError("Cannot recover from stack overflow.");
649 }
650 return 0;
651 }
652 if (tstate->recursion_depth > recursion_limit) {
653 --tstate->recursion_depth;
654 tstate->overflowed = 1;
Victor Stinner438a12d2019-05-24 17:01:38 +0200655 _PyErr_Format(tstate, PyExc_RecursionError,
656 "maximum recursion depth exceeded%s",
657 where);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 return -1;
659 }
660 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000661}
662
Victor Stinner09532fe2019-05-10 23:39:09 +0200663static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
Victor Stinner438a12d2019-05-24 17:01:38 +0200664static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000665
Victor Stinner09532fe2019-05-10 23:39:09 +0200666#define _Py_TracingPossible(ceval) ((ceval)->tracing_possible)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000667
Guido van Rossum374a9221991-04-04 10:40:29 +0000668
Guido van Rossumb209a111997-04-29 18:18:01 +0000669PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000670PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000671{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 return PyEval_EvalCodeEx(co,
673 globals, locals,
674 (PyObject **)NULL, 0,
675 (PyObject **)NULL, 0,
676 (PyObject **)NULL, 0,
677 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000678}
679
680
681/* Interpreter main loop */
682
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000683PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000684PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000685 /* This is for backward compatibility with extension modules that
686 used this API; core interpreter code should call
687 PyEval_EvalFrameEx() */
688 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000689}
690
691PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000692PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000693{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200694 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
695 return interp->eval_frame(f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700696}
697
Victor Stinnerc6944e72016-11-11 02:13:35 +0100698PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700699_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
700{
Guido van Rossum950361c1997-01-24 13:49:28 +0000701#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000703#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200704 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300705 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200706 int opcode; /* Current opcode */
707 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200708 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 PyObject *retval = NULL; /* Return value */
Victor Stinner09532fe2019-05-10 23:39:09 +0200710 _PyRuntimeState * const runtime = &_PyRuntime;
711 PyThreadState * const tstate = _PyRuntimeState_GetThreadState(runtime);
712 struct _ceval_runtime_state * const ceval = &runtime->ceval;
713 _Py_atomic_int * const eval_breaker = &ceval->eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000715
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000717
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000719
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000720 is true when the line being executed has changed. The
721 initial values are such as to make this false the first
722 time it is tested. */
723 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000724
Serhiy Storchakaab874002016-09-11 13:48:15 +0300725 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 PyObject *names;
727 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000728
Brett Cannon368b4b72012-04-02 12:17:59 -0400729#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200730 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400731#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200732
Antoine Pitroub52ec782009-01-25 16:34:23 +0000733/* Computed GOTOs, or
734 the-optimization-commonly-but-improperly-known-as-"threaded code"
735 using gcc's labels-as-values extension
736 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
737
738 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000740 combined with a lookup table of jump addresses. However, since the
741 indirect jump instruction is shared by all opcodes, the CPU will have a
742 hard time making the right prediction for where to jump next (actually,
743 it will be always wrong except in the uncommon case of a sequence of
744 several identical opcodes).
745
746 "Threaded code" in contrast, uses an explicit jump table and an explicit
747 indirect jump instruction at the end of each opcode. Since the jump
748 instruction is at a different address for each opcode, the CPU will make a
749 separate prediction for each of these instructions, which is equivalent to
750 predicting the second opcode of each opcode pair. These predictions have
751 a much better chance to turn out valid, especially in small bytecode loops.
752
753 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000755 and potentially many more instructions (depending on the pipeline width).
756 A correctly predicted branch, however, is nearly free.
757
758 At the time of this writing, the "threaded code" version is up to 15-20%
759 faster than the normal "switch" version, depending on the compiler and the
760 CPU architecture.
761
762 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
763 because it would render the measurements invalid.
764
765
766 NOTE: care must be taken that the compiler doesn't try to "optimize" the
767 indirect jumps by sharing them between all opcodes. Such optimizations
768 can be disabled on gcc by using the -fno-gcse flag (or possibly
769 -fno-crossjumping).
770*/
771
Antoine Pitrou042b1282010-08-13 21:15:58 +0000772#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000773#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000774#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000775#endif
776
Antoine Pitrou042b1282010-08-13 21:15:58 +0000777#ifdef HAVE_COMPUTED_GOTOS
778 #ifndef USE_COMPUTED_GOTOS
779 #define USE_COMPUTED_GOTOS 1
780 #endif
781#else
782 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
783 #error "Computed gotos are not supported on this compiler."
784 #endif
785 #undef USE_COMPUTED_GOTOS
786 #define USE_COMPUTED_GOTOS 0
787#endif
788
789#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000790/* Import the static jump table */
791#include "opcode_targets.h"
792
Antoine Pitroub52ec782009-01-25 16:34:23 +0000793#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -0700794 op: \
795 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000796
Antoine Pitroub52ec782009-01-25 16:34:23 +0000797#ifdef LLTRACE
798#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 { \
Victor Stinner09532fe2019-05-10 23:39:09 +0200800 if (!lltrace && !_Py_TracingPossible(ceval) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300802 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300803 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 } \
805 goto fast_next_opcode; \
806 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000807#else
808#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000809 { \
Victor Stinner09532fe2019-05-10 23:39:09 +0200810 if (!_Py_TracingPossible(ceval) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300812 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300813 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 } \
815 goto fast_next_opcode; \
816 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000817#endif
818
Victor Stinner09532fe2019-05-10 23:39:09 +0200819#define DISPATCH() \
820 { \
821 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
822 FAST_DISPATCH(); \
823 } \
824 continue; \
825 }
826
Antoine Pitroub52ec782009-01-25 16:34:23 +0000827#else
Benjamin Petersonddd19492018-09-16 22:38:02 -0700828#define TARGET(op) op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000829#define FAST_DISPATCH() goto fast_next_opcode
Victor Stinner09532fe2019-05-10 23:39:09 +0200830#define DISPATCH() continue
Antoine Pitroub52ec782009-01-25 16:34:23 +0000831#endif
832
833
Neal Norwitza81d2202002-07-14 00:27:26 +0000834/* Tuple access macros */
835
836#ifndef Py_DEBUG
837#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
838#else
839#define GETITEM(v, i) PyTuple_GetItem((v), (i))
840#endif
841
Guido van Rossum374a9221991-04-04 10:40:29 +0000842/* Code access macros */
843
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300844/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600845#define INSTR_OFFSET() \
846 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300847#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300848 _Py_CODEUNIT word = *next_instr; \
849 opcode = _Py_OPCODE(word); \
850 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300851 next_instr++; \
852 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300853#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
854#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000855
Raymond Hettingerf606f872003-03-16 03:11:04 +0000856/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 Some opcodes tend to come in pairs thus making it possible to
858 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300859 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 Verifying the prediction costs a single high-speed test of a register
862 variable against a constant. If the pairing was good, then the
863 processor's own internal branch predication has a high likelihood of
864 success, resulting in a nearly zero-overhead transition to the
865 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300866 including its unpredictable switch-case branch. Combined with the
867 processor's internal branch prediction, a successful PREDICT has the
868 effect of making the two opcodes run as if they were a single new opcode
869 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000870
Georg Brandl86b2fb92008-07-16 03:43:04 +0000871 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 predictions turned-on and interpret the results as if some opcodes
873 had been combined or turn-off predictions so that the opcode frequency
874 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000875
876 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 the CPU to record separate branch prediction information for each
878 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000879
Raymond Hettingerf606f872003-03-16 03:11:04 +0000880*/
881
Antoine Pitrou042b1282010-08-13 21:15:58 +0000882#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000884#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300885#define PREDICT(op) \
886 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300887 _Py_CODEUNIT word = *next_instr; \
888 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300889 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300890 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300891 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300892 goto PRED_##op; \
893 } \
894 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000895#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300896#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000897
Raymond Hettingerf606f872003-03-16 03:11:04 +0000898
Guido van Rossum374a9221991-04-04 10:40:29 +0000899/* Stack manipulation macros */
900
Martin v. Löwis18e16552006-02-15 17:27:45 +0000901/* The stack can grow at most MAXINT deep, as co_nlocals and
902 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000903#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
904#define EMPTY() (STACK_LEVEL() == 0)
905#define TOP() (stack_pointer[-1])
906#define SECOND() (stack_pointer[-2])
907#define THIRD() (stack_pointer[-3])
908#define FOURTH() (stack_pointer[-4])
909#define PEEK(n) (stack_pointer[-(n)])
910#define SET_TOP(v) (stack_pointer[-1] = (v))
911#define SET_SECOND(v) (stack_pointer[-2] = (v))
912#define SET_THIRD(v) (stack_pointer[-3] = (v))
913#define SET_FOURTH(v) (stack_pointer[-4] = (v))
914#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
915#define BASIC_STACKADJ(n) (stack_pointer += n)
916#define BASIC_PUSH(v) (*stack_pointer++ = (v))
917#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000918
Guido van Rossum96a42c81992-01-12 02:29:51 +0000919#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +0200921 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000922 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +0200923#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000924 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +0000925#define STACK_GROW(n) do { \
926 assert(n >= 0); \
927 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +0200928 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +0000929 assert(STACK_LEVEL() <= co->co_stacksize); \
930 } while (0)
931#define STACK_SHRINK(n) do { \
932 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +0200933 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +0000934 (void)(BASIC_STACKADJ(-n)); \
935 assert(STACK_LEVEL() <= co->co_stacksize); \
936 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +0000937#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +0200938 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000939 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000940#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000941#define PUSH(v) BASIC_PUSH(v)
942#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +0000943#define STACK_GROW(n) BASIC_STACKADJ(n)
944#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000945#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000946#endif
947
Guido van Rossum681d79a1995-07-18 14:51:37 +0000948/* Local variable macros */
949
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000951
952/* The SETLOCAL() macro must not DECREF the local variable in-place and
953 then store the new value; it must copy the old value to a temporary
954 value, then store the new value, and then DECREF the temporary value.
955 This is because it is possible that during the DECREF the frame is
956 accessed by other code (e.g. a __del__ method or gc.collect()) and the
957 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000959 GETLOCAL(i) = value; \
960 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000961
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000962
963#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 while (STACK_LEVEL() > (b)->b_level) { \
965 PyObject *v = POP(); \
966 Py_XDECREF(v); \
967 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000968
969#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300970 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +0100972 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 assert(STACK_LEVEL() >= (b)->b_level + 3); \
974 while (STACK_LEVEL() > (b)->b_level + 3) { \
975 value = POP(); \
976 Py_XDECREF(value); \
977 } \
Mark Shannonae3087c2017-10-22 22:41:51 +0100978 exc_info = tstate->exc_info; \
979 type = exc_info->exc_type; \
980 value = exc_info->exc_value; \
981 traceback = exc_info->exc_traceback; \
982 exc_info->exc_type = POP(); \
983 exc_info->exc_value = POP(); \
984 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 Py_XDECREF(type); \
986 Py_XDECREF(value); \
987 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300988 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000989
Guido van Rossuma027efa1997-05-05 20:56:21 +0000990/* Start of code */
991
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 /* push frame */
993 if (Py_EnterRecursiveCall(""))
994 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000995
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000997
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 if (tstate->use_tracing) {
999 if (tstate->c_tracefunc != NULL) {
1000 /* tstate->c_tracefunc, if defined, is a
1001 function that will be called on *every* entry
1002 to a code block. Its return value, if not
1003 None, is a function that will be called at
1004 the start of each executed line of code.
1005 (Actually, the function must return itself
1006 in order to continue tracing.) The trace
1007 functions are called with three arguments:
1008 a pointer to the current frame, a string
1009 indicating why the function is called, and
1010 an argument which depends on the situation.
1011 The global trace function is also called
1012 whenever an exception is detected. */
1013 if (call_trace_protected(tstate->c_tracefunc,
1014 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001015 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 /* Trace function raised an error */
1017 goto exit_eval_frame;
1018 }
1019 }
1020 if (tstate->c_profilefunc != NULL) {
1021 /* Similar for c_profilefunc, except it needn't
1022 return itself and isn't called for "line" events */
1023 if (call_trace_protected(tstate->c_profilefunc,
1024 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001025 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 /* Profile function raised an error */
1027 goto exit_eval_frame;
1028 }
1029 }
1030 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001031
Łukasz Langaa785c872016-09-09 17:37:37 -07001032 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1033 dtrace_function_entry(f);
1034
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 co = f->f_code;
1036 names = co->co_names;
1037 consts = co->co_consts;
1038 fastlocals = f->f_localsplus;
1039 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001040 assert(PyBytes_Check(co->co_code));
1041 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001042 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1043 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1044 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001045 /*
1046 f->f_lasti refers to the index of the last instruction,
1047 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001048
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001049 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001050 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 When the PREDICT() macros are enabled, some opcode pairs follow in
1053 direct succession without updating f->f_lasti. A successful
1054 prediction effectively links the two codes together as if they
1055 were a single new opcode; accordingly,f->f_lasti will point to
1056 the first code in the pair (for instance, GET_ITER followed by
1057 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001058 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001060 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001061 next_instr = first_instr;
1062 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001063 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1064 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001065 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 stack_pointer = f->f_stacktop;
1067 assert(stack_pointer != NULL);
1068 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001069 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001070
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001071
Tim Peters5ca576e2001-06-18 22:08:13 +00001072#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001073 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001074#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001075
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001076 if (throwflag) /* support for generator.throw() */
1077 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001078
Victor Stinnerace47d72013-07-18 01:41:08 +02001079#ifdef Py_DEBUG
1080 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001081 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001082 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001083 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001084#endif
1085
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001086main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1089 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001090 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001091
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092 /* Do periodic things. Doing this every time through
1093 the loop would add too much overhead, so we do it
1094 only every Nth instruction. We also do it if
1095 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1096 event needs attention (e.g. a signal handler or
1097 async I/O handler); see Py_AddPendingCall() and
1098 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001099
Eric Snow7bda9de2019-03-08 17:25:54 -07001100 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001101 opcode = _Py_OPCODE(*next_instr);
1102 if (opcode == SETUP_FINALLY ||
1103 opcode == SETUP_WITH ||
1104 opcode == BEFORE_ASYNC_WITH ||
1105 opcode == YIELD_FROM) {
1106 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001107 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001108 - If we're about to enter the 'with:'. It will prevent
1109 emitting a resource warning in the common idiom
1110 'with open(path) as file:'.
1111 - If we're about to enter the 'async with:'.
1112 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001113 *very* useful, but might help in some cases and it's
1114 traditional)
1115 - If we're resuming a chain of nested 'yield from' or
1116 'await' calls, then each frame is parked with YIELD_FROM
1117 as its next opcode. If the user hit control-C we want to
1118 wait until we've reached the innermost frame before
1119 running the signal handler and raising KeyboardInterrupt
1120 (see bpo-30039).
1121 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 goto fast_next_opcode;
1123 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001124
Victor Stinner09532fe2019-05-10 23:39:09 +02001125 if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
1126 if (handle_signals(runtime) != 0) {
Eric Snowfdf282d2019-01-11 14:26:55 -07001127 goto error;
1128 }
1129 }
Victor Stinner09532fe2019-05-10 23:39:09 +02001130 if (_Py_atomic_load_relaxed(&ceval->pending.calls_to_do)) {
1131 if (make_pending_calls(runtime) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001132 goto error;
Eric Snowfdf282d2019-01-11 14:26:55 -07001133 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001134 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001135
Victor Stinner09532fe2019-05-10 23:39:09 +02001136 if (_Py_atomic_load_relaxed(&ceval->gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 /* Give another thread a chance */
Victor Stinner09532fe2019-05-10 23:39:09 +02001138 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 Py_FatalError("ceval: tstate mix-up");
Victor Stinner09532fe2019-05-10 23:39:09 +02001140 }
1141 drop_gil(ceval, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142
1143 /* Other threads may run now */
1144
Victor Stinner09532fe2019-05-10 23:39:09 +02001145 take_gil(ceval, tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001146
1147 /* Check if we should make a quick exit. */
Eric Snow396e0a82019-05-31 21:16:47 -06001148 exit_thread_if_finalizing(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001149
Victor Stinner09532fe2019-05-10 23:39:09 +02001150 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 Py_FatalError("ceval: orphan tstate");
Victor Stinner09532fe2019-05-10 23:39:09 +02001152 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 }
1154 /* Check for asynchronous exceptions. */
1155 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001156 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001157 tstate->async_exc = NULL;
Victor Stinner09532fe2019-05-10 23:39:09 +02001158 UNSIGNAL_ASYNC_EXC(ceval);
Victor Stinner438a12d2019-05-24 17:01:38 +02001159 _PyErr_SetNone(tstate, exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001160 Py_DECREF(exc);
1161 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 }
1163 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001165 fast_next_opcode:
1166 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001167
Łukasz Langaa785c872016-09-09 17:37:37 -07001168 if (PyDTrace_LINE_ENABLED())
1169 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001172
Victor Stinner09532fe2019-05-10 23:39:09 +02001173 if (_Py_TracingPossible(ceval) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001174 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001175 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001176 /* see maybe_call_line_trace
1177 for expository comments */
1178 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001179
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 err = maybe_call_line_trace(tstate->c_tracefunc,
1181 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001182 tstate, f,
1183 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 /* Reload possibly changed frame fields */
1185 JUMPTO(f->f_lasti);
1186 if (f->f_stacktop != NULL) {
1187 stack_pointer = f->f_stacktop;
1188 f->f_stacktop = NULL;
1189 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001190 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001192 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001196
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001197 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001198 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001199#ifdef DYNAMIC_EXECUTION_PROFILE
1200#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 dxpairs[lastopcode][opcode]++;
1202 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001203#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001205#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001206
Guido van Rossum96a42c81992-01-12 02:29:51 +00001207#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 if (lltrace) {
1211 if (HAS_ARG(opcode)) {
1212 printf("%d: %d, %d\n",
1213 f->f_lasti, opcode, oparg);
1214 }
1215 else {
1216 printf("%d: %d\n",
1217 f->f_lasti, opcode);
1218 }
1219 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001220#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001221
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001225 It is essential that any operation that fails must goto error
1226 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001227
Benjamin Petersonddd19492018-09-16 22:38:02 -07001228 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001229 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001230 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001231
Benjamin Petersonddd19492018-09-16 22:38:02 -07001232 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001233 PyObject *value = GETLOCAL(oparg);
1234 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001235 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001236 UNBOUNDLOCAL_ERROR_MSG,
1237 PyTuple_GetItem(co->co_varnames, oparg));
1238 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001240 Py_INCREF(value);
1241 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001243 }
1244
Benjamin Petersonddd19492018-09-16 22:38:02 -07001245 case TARGET(LOAD_CONST): {
1246 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001247 PyObject *value = GETITEM(consts, oparg);
1248 Py_INCREF(value);
1249 PUSH(value);
1250 FAST_DISPATCH();
1251 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001252
Benjamin Petersonddd19492018-09-16 22:38:02 -07001253 case TARGET(STORE_FAST): {
1254 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001255 PyObject *value = POP();
1256 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001258 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001259
Benjamin Petersonddd19492018-09-16 22:38:02 -07001260 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001261 PyObject *value = POP();
1262 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001264 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001265
Benjamin Petersonddd19492018-09-16 22:38:02 -07001266 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001267 PyObject *top = TOP();
1268 PyObject *second = SECOND();
1269 SET_TOP(second);
1270 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001272 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001273
Benjamin Petersonddd19492018-09-16 22:38:02 -07001274 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001275 PyObject *top = TOP();
1276 PyObject *second = SECOND();
1277 PyObject *third = THIRD();
1278 SET_TOP(second);
1279 SET_SECOND(third);
1280 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001282 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001283
Benjamin Petersonddd19492018-09-16 22:38:02 -07001284 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001285 PyObject *top = TOP();
1286 PyObject *second = SECOND();
1287 PyObject *third = THIRD();
1288 PyObject *fourth = FOURTH();
1289 SET_TOP(second);
1290 SET_SECOND(third);
1291 SET_THIRD(fourth);
1292 SET_FOURTH(top);
1293 FAST_DISPATCH();
1294 }
1295
Benjamin Petersonddd19492018-09-16 22:38:02 -07001296 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001297 PyObject *top = TOP();
1298 Py_INCREF(top);
1299 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001301 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001302
Benjamin Petersonddd19492018-09-16 22:38:02 -07001303 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001304 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001305 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001306 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001307 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001308 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001309 SET_TOP(top);
1310 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001311 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001312 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001313
Benjamin Petersonddd19492018-09-16 22:38:02 -07001314 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001315 PyObject *value = TOP();
1316 PyObject *res = PyNumber_Positive(value);
1317 Py_DECREF(value);
1318 SET_TOP(res);
1319 if (res == NULL)
1320 goto error;
1321 DISPATCH();
1322 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001323
Benjamin Petersonddd19492018-09-16 22:38:02 -07001324 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001325 PyObject *value = TOP();
1326 PyObject *res = PyNumber_Negative(value);
1327 Py_DECREF(value);
1328 SET_TOP(res);
1329 if (res == NULL)
1330 goto error;
1331 DISPATCH();
1332 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001333
Benjamin Petersonddd19492018-09-16 22:38:02 -07001334 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001335 PyObject *value = TOP();
1336 int err = PyObject_IsTrue(value);
1337 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 if (err == 0) {
1339 Py_INCREF(Py_True);
1340 SET_TOP(Py_True);
1341 DISPATCH();
1342 }
1343 else if (err > 0) {
1344 Py_INCREF(Py_False);
1345 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 DISPATCH();
1347 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001348 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001349 goto error;
1350 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001351
Benjamin Petersonddd19492018-09-16 22:38:02 -07001352 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001353 PyObject *value = TOP();
1354 PyObject *res = PyNumber_Invert(value);
1355 Py_DECREF(value);
1356 SET_TOP(res);
1357 if (res == NULL)
1358 goto error;
1359 DISPATCH();
1360 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001361
Benjamin Petersonddd19492018-09-16 22:38:02 -07001362 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001363 PyObject *exp = POP();
1364 PyObject *base = TOP();
1365 PyObject *res = PyNumber_Power(base, exp, Py_None);
1366 Py_DECREF(base);
1367 Py_DECREF(exp);
1368 SET_TOP(res);
1369 if (res == NULL)
1370 goto error;
1371 DISPATCH();
1372 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001373
Benjamin Petersonddd19492018-09-16 22:38:02 -07001374 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001375 PyObject *right = POP();
1376 PyObject *left = TOP();
1377 PyObject *res = PyNumber_Multiply(left, right);
1378 Py_DECREF(left);
1379 Py_DECREF(right);
1380 SET_TOP(res);
1381 if (res == NULL)
1382 goto error;
1383 DISPATCH();
1384 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001385
Benjamin Petersonddd19492018-09-16 22:38:02 -07001386 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001387 PyObject *right = POP();
1388 PyObject *left = TOP();
1389 PyObject *res = PyNumber_MatrixMultiply(left, right);
1390 Py_DECREF(left);
1391 Py_DECREF(right);
1392 SET_TOP(res);
1393 if (res == NULL)
1394 goto error;
1395 DISPATCH();
1396 }
1397
Benjamin Petersonddd19492018-09-16 22:38:02 -07001398 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001399 PyObject *divisor = POP();
1400 PyObject *dividend = TOP();
1401 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1402 Py_DECREF(dividend);
1403 Py_DECREF(divisor);
1404 SET_TOP(quotient);
1405 if (quotient == NULL)
1406 goto error;
1407 DISPATCH();
1408 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001409
Benjamin Petersonddd19492018-09-16 22:38:02 -07001410 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001411 PyObject *divisor = POP();
1412 PyObject *dividend = TOP();
1413 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1414 Py_DECREF(dividend);
1415 Py_DECREF(divisor);
1416 SET_TOP(quotient);
1417 if (quotient == NULL)
1418 goto error;
1419 DISPATCH();
1420 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001421
Benjamin Petersonddd19492018-09-16 22:38:02 -07001422 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001423 PyObject *divisor = POP();
1424 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001425 PyObject *res;
1426 if (PyUnicode_CheckExact(dividend) && (
1427 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1428 // fast path; string formatting, but not if the RHS is a str subclass
1429 // (see issue28598)
1430 res = PyUnicode_Format(dividend, divisor);
1431 } else {
1432 res = PyNumber_Remainder(dividend, divisor);
1433 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001434 Py_DECREF(divisor);
1435 Py_DECREF(dividend);
1436 SET_TOP(res);
1437 if (res == NULL)
1438 goto error;
1439 DISPATCH();
1440 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001441
Benjamin Petersonddd19492018-09-16 22:38:02 -07001442 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001443 PyObject *right = POP();
1444 PyObject *left = TOP();
1445 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001446 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1447 CPython using bytecode, it is simply worthless.
1448 See http://bugs.python.org/issue21955 and
1449 http://bugs.python.org/issue10044 for the discussion. In short,
1450 no patch shown any impact on a realistic benchmark, only a minor
1451 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001452 if (PyUnicode_CheckExact(left) &&
1453 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001454 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001455 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001456 }
1457 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001458 sum = PyNumber_Add(left, right);
1459 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001460 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001461 Py_DECREF(right);
1462 SET_TOP(sum);
1463 if (sum == NULL)
1464 goto error;
1465 DISPATCH();
1466 }
1467
Benjamin Petersonddd19492018-09-16 22:38:02 -07001468 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001469 PyObject *right = POP();
1470 PyObject *left = TOP();
1471 PyObject *diff = PyNumber_Subtract(left, right);
1472 Py_DECREF(right);
1473 Py_DECREF(left);
1474 SET_TOP(diff);
1475 if (diff == NULL)
1476 goto error;
1477 DISPATCH();
1478 }
1479
Benjamin Petersonddd19492018-09-16 22:38:02 -07001480 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001481 PyObject *sub = POP();
1482 PyObject *container = TOP();
1483 PyObject *res = PyObject_GetItem(container, sub);
1484 Py_DECREF(container);
1485 Py_DECREF(sub);
1486 SET_TOP(res);
1487 if (res == NULL)
1488 goto error;
1489 DISPATCH();
1490 }
1491
Benjamin Petersonddd19492018-09-16 22:38:02 -07001492 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001493 PyObject *right = POP();
1494 PyObject *left = TOP();
1495 PyObject *res = PyNumber_Lshift(left, right);
1496 Py_DECREF(left);
1497 Py_DECREF(right);
1498 SET_TOP(res);
1499 if (res == NULL)
1500 goto error;
1501 DISPATCH();
1502 }
1503
Benjamin Petersonddd19492018-09-16 22:38:02 -07001504 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001505 PyObject *right = POP();
1506 PyObject *left = TOP();
1507 PyObject *res = PyNumber_Rshift(left, right);
1508 Py_DECREF(left);
1509 Py_DECREF(right);
1510 SET_TOP(res);
1511 if (res == NULL)
1512 goto error;
1513 DISPATCH();
1514 }
1515
Benjamin Petersonddd19492018-09-16 22:38:02 -07001516 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001517 PyObject *right = POP();
1518 PyObject *left = TOP();
1519 PyObject *res = PyNumber_And(left, right);
1520 Py_DECREF(left);
1521 Py_DECREF(right);
1522 SET_TOP(res);
1523 if (res == NULL)
1524 goto error;
1525 DISPATCH();
1526 }
1527
Benjamin Petersonddd19492018-09-16 22:38:02 -07001528 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001529 PyObject *right = POP();
1530 PyObject *left = TOP();
1531 PyObject *res = PyNumber_Xor(left, right);
1532 Py_DECREF(left);
1533 Py_DECREF(right);
1534 SET_TOP(res);
1535 if (res == NULL)
1536 goto error;
1537 DISPATCH();
1538 }
1539
Benjamin Petersonddd19492018-09-16 22:38:02 -07001540 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001541 PyObject *right = POP();
1542 PyObject *left = TOP();
1543 PyObject *res = PyNumber_Or(left, right);
1544 Py_DECREF(left);
1545 Py_DECREF(right);
1546 SET_TOP(res);
1547 if (res == NULL)
1548 goto error;
1549 DISPATCH();
1550 }
1551
Benjamin Petersonddd19492018-09-16 22:38:02 -07001552 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001553 PyObject *v = POP();
1554 PyObject *list = PEEK(oparg);
1555 int err;
1556 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001557 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001558 if (err != 0)
1559 goto error;
1560 PREDICT(JUMP_ABSOLUTE);
1561 DISPATCH();
1562 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001563
Benjamin Petersonddd19492018-09-16 22:38:02 -07001564 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001565 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001566 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001567 int err;
1568 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001569 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001570 if (err != 0)
1571 goto error;
1572 PREDICT(JUMP_ABSOLUTE);
1573 DISPATCH();
1574 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001575
Benjamin Petersonddd19492018-09-16 22:38:02 -07001576 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001577 PyObject *exp = POP();
1578 PyObject *base = TOP();
1579 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1580 Py_DECREF(base);
1581 Py_DECREF(exp);
1582 SET_TOP(res);
1583 if (res == NULL)
1584 goto error;
1585 DISPATCH();
1586 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001587
Benjamin Petersonddd19492018-09-16 22:38:02 -07001588 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001589 PyObject *right = POP();
1590 PyObject *left = TOP();
1591 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1592 Py_DECREF(left);
1593 Py_DECREF(right);
1594 SET_TOP(res);
1595 if (res == NULL)
1596 goto error;
1597 DISPATCH();
1598 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001599
Benjamin Petersonddd19492018-09-16 22:38:02 -07001600 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001601 PyObject *right = POP();
1602 PyObject *left = TOP();
1603 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1604 Py_DECREF(left);
1605 Py_DECREF(right);
1606 SET_TOP(res);
1607 if (res == NULL)
1608 goto error;
1609 DISPATCH();
1610 }
1611
Benjamin Petersonddd19492018-09-16 22:38:02 -07001612 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001613 PyObject *divisor = POP();
1614 PyObject *dividend = TOP();
1615 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1616 Py_DECREF(dividend);
1617 Py_DECREF(divisor);
1618 SET_TOP(quotient);
1619 if (quotient == NULL)
1620 goto error;
1621 DISPATCH();
1622 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001623
Benjamin Petersonddd19492018-09-16 22:38:02 -07001624 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001625 PyObject *divisor = POP();
1626 PyObject *dividend = TOP();
1627 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1628 Py_DECREF(dividend);
1629 Py_DECREF(divisor);
1630 SET_TOP(quotient);
1631 if (quotient == NULL)
1632 goto error;
1633 DISPATCH();
1634 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001635
Benjamin Petersonddd19492018-09-16 22:38:02 -07001636 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001637 PyObject *right = POP();
1638 PyObject *left = TOP();
1639 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1640 Py_DECREF(left);
1641 Py_DECREF(right);
1642 SET_TOP(mod);
1643 if (mod == NULL)
1644 goto error;
1645 DISPATCH();
1646 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001647
Benjamin Petersonddd19492018-09-16 22:38:02 -07001648 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001649 PyObject *right = POP();
1650 PyObject *left = TOP();
1651 PyObject *sum;
1652 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001653 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001654 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001655 }
1656 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001657 sum = PyNumber_InPlaceAdd(left, right);
1658 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001659 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001660 Py_DECREF(right);
1661 SET_TOP(sum);
1662 if (sum == NULL)
1663 goto error;
1664 DISPATCH();
1665 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001666
Benjamin Petersonddd19492018-09-16 22:38:02 -07001667 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001668 PyObject *right = POP();
1669 PyObject *left = TOP();
1670 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1671 Py_DECREF(left);
1672 Py_DECREF(right);
1673 SET_TOP(diff);
1674 if (diff == NULL)
1675 goto error;
1676 DISPATCH();
1677 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001678
Benjamin Petersonddd19492018-09-16 22:38:02 -07001679 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001680 PyObject *right = POP();
1681 PyObject *left = TOP();
1682 PyObject *res = PyNumber_InPlaceLshift(left, right);
1683 Py_DECREF(left);
1684 Py_DECREF(right);
1685 SET_TOP(res);
1686 if (res == NULL)
1687 goto error;
1688 DISPATCH();
1689 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001690
Benjamin Petersonddd19492018-09-16 22:38:02 -07001691 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001692 PyObject *right = POP();
1693 PyObject *left = TOP();
1694 PyObject *res = PyNumber_InPlaceRshift(left, right);
1695 Py_DECREF(left);
1696 Py_DECREF(right);
1697 SET_TOP(res);
1698 if (res == NULL)
1699 goto error;
1700 DISPATCH();
1701 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001702
Benjamin Petersonddd19492018-09-16 22:38:02 -07001703 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001704 PyObject *right = POP();
1705 PyObject *left = TOP();
1706 PyObject *res = PyNumber_InPlaceAnd(left, right);
1707 Py_DECREF(left);
1708 Py_DECREF(right);
1709 SET_TOP(res);
1710 if (res == NULL)
1711 goto error;
1712 DISPATCH();
1713 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001714
Benjamin Petersonddd19492018-09-16 22:38:02 -07001715 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001716 PyObject *right = POP();
1717 PyObject *left = TOP();
1718 PyObject *res = PyNumber_InPlaceXor(left, right);
1719 Py_DECREF(left);
1720 Py_DECREF(right);
1721 SET_TOP(res);
1722 if (res == NULL)
1723 goto error;
1724 DISPATCH();
1725 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001726
Benjamin Petersonddd19492018-09-16 22:38:02 -07001727 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001728 PyObject *right = POP();
1729 PyObject *left = TOP();
1730 PyObject *res = PyNumber_InPlaceOr(left, right);
1731 Py_DECREF(left);
1732 Py_DECREF(right);
1733 SET_TOP(res);
1734 if (res == NULL)
1735 goto error;
1736 DISPATCH();
1737 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001738
Benjamin Petersonddd19492018-09-16 22:38:02 -07001739 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001740 PyObject *sub = TOP();
1741 PyObject *container = SECOND();
1742 PyObject *v = THIRD();
1743 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001744 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001745 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001746 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001747 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001748 Py_DECREF(container);
1749 Py_DECREF(sub);
1750 if (err != 0)
1751 goto error;
1752 DISPATCH();
1753 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001754
Benjamin Petersonddd19492018-09-16 22:38:02 -07001755 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001756 PyObject *sub = TOP();
1757 PyObject *container = SECOND();
1758 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001759 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00001760 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001761 err = PyObject_DelItem(container, sub);
1762 Py_DECREF(container);
1763 Py_DECREF(sub);
1764 if (err != 0)
1765 goto error;
1766 DISPATCH();
1767 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001768
Benjamin Petersonddd19492018-09-16 22:38:02 -07001769 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01001770 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001771 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001772 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001773 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001774 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001775 _PyErr_SetString(tstate, PyExc_RuntimeError,
1776 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001777 Py_DECREF(value);
1778 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001779 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001780 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001781 Py_DECREF(value);
1782 if (res == NULL)
1783 goto error;
1784 Py_DECREF(res);
1785 DISPATCH();
1786 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001787
Benjamin Petersonddd19492018-09-16 22:38:02 -07001788 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001789 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001790 switch (oparg) {
1791 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001792 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001793 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001794 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001795 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001796 /* fall through */
1797 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02001798 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001799 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001800 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001801 break;
1802 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02001803 _PyErr_SetString(tstate, PyExc_SystemError,
1804 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001805 break;
1806 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001807 goto error;
1808 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001809
Benjamin Petersonddd19492018-09-16 22:38:02 -07001810 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001811 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001812 assert(f->f_iblock == 0);
Pablo Galindof00828a2019-05-09 16:52:02 +01001813 goto exit_returning;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001814 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001815
Benjamin Petersonddd19492018-09-16 22:38:02 -07001816 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001817 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001818 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001819 PyObject *obj = TOP();
1820 PyTypeObject *type = Py_TYPE(obj);
1821
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001822 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001823 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001824 }
Yury Selivanov75445082015-05-11 22:57:16 -04001825
1826 if (getter != NULL) {
1827 iter = (*getter)(obj);
1828 Py_DECREF(obj);
1829 if (iter == NULL) {
1830 SET_TOP(NULL);
1831 goto error;
1832 }
1833 }
1834 else {
1835 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02001836 _PyErr_Format(tstate, PyExc_TypeError,
1837 "'async for' requires an object with "
1838 "__aiter__ method, got %.100s",
1839 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001840 Py_DECREF(obj);
1841 goto error;
1842 }
1843
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001844 if (Py_TYPE(iter)->tp_as_async == NULL ||
1845 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001846
Yury Selivanov398ff912017-03-02 22:20:00 -05001847 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02001848 _PyErr_Format(tstate, PyExc_TypeError,
1849 "'async for' received an object from __aiter__ "
1850 "that does not implement __anext__: %.100s",
1851 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001852 Py_DECREF(iter);
1853 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001854 }
1855
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001856 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001857 DISPATCH();
1858 }
1859
Benjamin Petersonddd19492018-09-16 22:38:02 -07001860 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001861 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001862 PyObject *next_iter = NULL;
1863 PyObject *awaitable = NULL;
1864 PyObject *aiter = TOP();
1865 PyTypeObject *type = Py_TYPE(aiter);
1866
Yury Selivanoveb636452016-09-08 22:01:51 -07001867 if (PyAsyncGen_CheckExact(aiter)) {
1868 awaitable = type->tp_as_async->am_anext(aiter);
1869 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001870 goto error;
1871 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001872 } else {
1873 if (type->tp_as_async != NULL){
1874 getter = type->tp_as_async->am_anext;
1875 }
Yury Selivanov75445082015-05-11 22:57:16 -04001876
Yury Selivanoveb636452016-09-08 22:01:51 -07001877 if (getter != NULL) {
1878 next_iter = (*getter)(aiter);
1879 if (next_iter == NULL) {
1880 goto error;
1881 }
1882 }
1883 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02001884 _PyErr_Format(tstate, PyExc_TypeError,
1885 "'async for' requires an iterator with "
1886 "__anext__ method, got %.100s",
1887 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07001888 goto error;
1889 }
Yury Selivanov75445082015-05-11 22:57:16 -04001890
Yury Selivanoveb636452016-09-08 22:01:51 -07001891 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1892 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001893 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001894 PyExc_TypeError,
1895 "'async for' received an invalid object "
1896 "from __anext__: %.100s",
1897 Py_TYPE(next_iter)->tp_name);
1898
1899 Py_DECREF(next_iter);
1900 goto error;
1901 } else {
1902 Py_DECREF(next_iter);
1903 }
1904 }
Yury Selivanov75445082015-05-11 22:57:16 -04001905
1906 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001907 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001908 DISPATCH();
1909 }
1910
Benjamin Petersonddd19492018-09-16 22:38:02 -07001911 case TARGET(GET_AWAITABLE): {
1912 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04001913 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04001914 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04001915
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03001916 if (iter == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001917 format_awaitable_error(tstate, Py_TYPE(iterable),
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03001918 _Py_OPCODE(next_instr[-2]));
1919 }
1920
Yury Selivanov75445082015-05-11 22:57:16 -04001921 Py_DECREF(iterable);
1922
Yury Selivanovc724bae2016-03-02 11:30:46 -05001923 if (iter != NULL && PyCoro_CheckExact(iter)) {
1924 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
1925 if (yf != NULL) {
1926 /* `iter` is a coroutine object that is being
1927 awaited, `yf` is a pointer to the current awaitable
1928 being awaited on. */
1929 Py_DECREF(yf);
1930 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02001931 _PyErr_SetString(tstate, PyExc_RuntimeError,
1932 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05001933 /* The code below jumps to `error` if `iter` is NULL. */
1934 }
1935 }
1936
Yury Selivanov75445082015-05-11 22:57:16 -04001937 SET_TOP(iter); /* Even if it's NULL */
1938
1939 if (iter == NULL) {
1940 goto error;
1941 }
1942
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001943 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001944 DISPATCH();
1945 }
1946
Benjamin Petersonddd19492018-09-16 22:38:02 -07001947 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001948 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001949 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001950 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001951 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
1952 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001953 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001954 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001955 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001956 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001957 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001958 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001959 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001960 Py_DECREF(v);
1961 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001962 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08001963 if (tstate->c_tracefunc != NULL
Victor Stinner438a12d2019-05-24 17:01:38 +02001964 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001965 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10001966 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001967 if (err < 0)
1968 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001969 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001970 SET_TOP(val);
1971 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001972 }
Martin Panter95f53c12016-07-18 08:23:26 +00001973 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001974 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001975 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01001976 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03001977 f->f_lasti -= sizeof(_Py_CODEUNIT);
Pablo Galindof00828a2019-05-09 16:52:02 +01001978 goto exit_yielding;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001979 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001980
Benjamin Petersonddd19492018-09-16 22:38:02 -07001981 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001982 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07001983
1984 if (co->co_flags & CO_ASYNC_GENERATOR) {
1985 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
1986 Py_DECREF(retval);
1987 if (w == NULL) {
1988 retval = NULL;
1989 goto error;
1990 }
1991 retval = w;
1992 }
1993
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001994 f->f_stacktop = stack_pointer;
Pablo Galindof00828a2019-05-09 16:52:02 +01001995 goto exit_yielding;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001996 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001997
Benjamin Petersonddd19492018-09-16 22:38:02 -07001998 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001999 PyObject *type, *value, *traceback;
2000 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002001 PyTryBlock *b = PyFrame_BlockPop(f);
2002 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002003 _PyErr_SetString(tstate, PyExc_SystemError,
2004 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002005 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002006 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002007 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2008 STACK_LEVEL() <= (b)->b_level + 4);
2009 exc_info = tstate->exc_info;
2010 type = exc_info->exc_type;
2011 value = exc_info->exc_value;
2012 traceback = exc_info->exc_traceback;
2013 exc_info->exc_type = POP();
2014 exc_info->exc_value = POP();
2015 exc_info->exc_traceback = POP();
2016 Py_XDECREF(type);
2017 Py_XDECREF(value);
2018 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002019 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002020 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002021
Benjamin Petersonddd19492018-09-16 22:38:02 -07002022 case TARGET(POP_BLOCK): {
2023 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002024 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002026 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002027
Benjamin Petersonddd19492018-09-16 22:38:02 -07002028 case TARGET(POP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002029 /* If oparg is 0 at the top of the stack are 1 or 6 values:
2030 Either:
2031 - TOP = NULL or an integer
2032 or:
2033 - (TOP, SECOND, THIRD) = exc_info()
2034 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2035
2036 If oparg is 1 the value for 'return' was additionally pushed
2037 at the top of the stack.
2038 */
2039 PyObject *res = NULL;
2040 if (oparg) {
2041 res = POP();
2042 }
2043 PyObject *exc = POP();
2044 if (exc == NULL || PyLong_CheckExact(exc)) {
2045 Py_XDECREF(exc);
2046 }
2047 else {
2048 Py_DECREF(exc);
2049 Py_DECREF(POP());
2050 Py_DECREF(POP());
2051
2052 PyObject *type, *value, *traceback;
2053 _PyErr_StackItem *exc_info;
2054 PyTryBlock *b = PyFrame_BlockPop(f);
2055 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002056 _PyErr_SetString(tstate, PyExc_SystemError,
2057 "popped block is not an except handler");
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002058 Py_XDECREF(res);
2059 goto error;
2060 }
2061 assert(STACK_LEVEL() == (b)->b_level + 3);
2062 exc_info = tstate->exc_info;
2063 type = exc_info->exc_type;
2064 value = exc_info->exc_value;
2065 traceback = exc_info->exc_traceback;
2066 exc_info->exc_type = POP();
2067 exc_info->exc_value = POP();
2068 exc_info->exc_traceback = POP();
2069 Py_XDECREF(type);
2070 Py_XDECREF(value);
2071 Py_XDECREF(traceback);
2072 }
2073 if (oparg) {
2074 PUSH(res);
2075 }
2076 DISPATCH();
2077 }
2078
Benjamin Petersonddd19492018-09-16 22:38:02 -07002079 case TARGET(CALL_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002080 PyObject *ret = PyLong_FromLong(INSTR_OFFSET());
2081 if (ret == NULL) {
2082 goto error;
2083 }
2084 PUSH(ret);
2085 JUMPBY(oparg);
2086 FAST_DISPATCH();
2087 }
2088
Benjamin Petersonddd19492018-09-16 22:38:02 -07002089 case TARGET(BEGIN_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002090 /* Push NULL onto the stack for using it in END_FINALLY,
2091 POP_FINALLY, WITH_CLEANUP_START and WITH_CLEANUP_FINISH.
2092 */
2093 PUSH(NULL);
2094 FAST_DISPATCH();
2095 }
2096
Benjamin Petersonddd19492018-09-16 22:38:02 -07002097 case TARGET(END_FINALLY): {
2098 PREDICTED(END_FINALLY);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002099 /* At the top of the stack are 1 or 6 values:
2100 Either:
2101 - TOP = NULL or an integer
2102 or:
2103 - (TOP, SECOND, THIRD) = exc_info()
2104 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2105 */
2106 PyObject *exc = POP();
2107 if (exc == NULL) {
2108 FAST_DISPATCH();
2109 }
2110 else if (PyLong_CheckExact(exc)) {
2111 int ret = _PyLong_AsInt(exc);
2112 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02002113 if (ret == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002114 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002115 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002116 JUMPTO(ret);
2117 FAST_DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002118 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002119 else {
2120 assert(PyExceptionClass_Check(exc));
2121 PyObject *val = POP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002122 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002123 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002124 goto exception_unwind;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002125 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002126 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002127
Benjamin Petersonddd19492018-09-16 22:38:02 -07002128 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002129 PyObject *exc = POP();
2130 assert(PyExceptionClass_Check(exc));
2131 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2132 PyTryBlock *b = PyFrame_BlockPop(f);
2133 assert(b->b_type == EXCEPT_HANDLER);
2134 Py_DECREF(exc);
2135 UNWIND_EXCEPT_HANDLER(b);
2136 Py_DECREF(POP());
2137 JUMPBY(oparg);
2138 FAST_DISPATCH();
2139 }
2140 else {
2141 PyObject *val = POP();
2142 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002143 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002144 goto exception_unwind;
2145 }
2146 }
2147
Benjamin Petersonddd19492018-09-16 22:38:02 -07002148 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002149 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002150
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002151 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002152 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002153 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002154 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002155 if (!_PyErr_Occurred(tstate)) {
2156 _PyErr_SetString(tstate, PyExc_NameError,
2157 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002158 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002159 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002160 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002161 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002162 }
2163 else {
2164 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2165 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002166 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002167 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2168 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002169 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2170 _PyErr_SetString(tstate, PyExc_NameError,
2171 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002172 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002173 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002174 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002175 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002176 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002177 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002178
Benjamin Petersonddd19492018-09-16 22:38:02 -07002179 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002180 PyObject *name = GETITEM(names, oparg);
2181 PyObject *v = POP();
2182 PyObject *ns = f->f_locals;
2183 int err;
2184 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002185 _PyErr_Format(tstate, PyExc_SystemError,
2186 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002187 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002188 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002189 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002190 if (PyDict_CheckExact(ns))
2191 err = PyDict_SetItem(ns, name, v);
2192 else
2193 err = PyObject_SetItem(ns, name, v);
2194 Py_DECREF(v);
2195 if (err != 0)
2196 goto error;
2197 DISPATCH();
2198 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002199
Benjamin Petersonddd19492018-09-16 22:38:02 -07002200 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002201 PyObject *name = GETITEM(names, oparg);
2202 PyObject *ns = f->f_locals;
2203 int err;
2204 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002205 _PyErr_Format(tstate, PyExc_SystemError,
2206 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002207 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002208 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002209 err = PyObject_DelItem(ns, name);
2210 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002211 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002212 NAME_ERROR_MSG,
2213 name);
2214 goto error;
2215 }
2216 DISPATCH();
2217 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002218
Benjamin Petersonddd19492018-09-16 22:38:02 -07002219 case TARGET(UNPACK_SEQUENCE): {
2220 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002221 PyObject *seq = POP(), *item, **items;
2222 if (PyTuple_CheckExact(seq) &&
2223 PyTuple_GET_SIZE(seq) == oparg) {
2224 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002225 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002226 item = items[oparg];
2227 Py_INCREF(item);
2228 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002229 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002230 } else if (PyList_CheckExact(seq) &&
2231 PyList_GET_SIZE(seq) == oparg) {
2232 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002233 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002234 item = items[oparg];
2235 Py_INCREF(item);
2236 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002237 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002238 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002239 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002240 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002241 } else {
2242 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002243 Py_DECREF(seq);
2244 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002245 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002246 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002247 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002248 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002249
Benjamin Petersonddd19492018-09-16 22:38:02 -07002250 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002251 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2252 PyObject *seq = POP();
2253
Victor Stinner438a12d2019-05-24 17:01:38 +02002254 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002255 stack_pointer + totalargs)) {
2256 stack_pointer += totalargs;
2257 } else {
2258 Py_DECREF(seq);
2259 goto error;
2260 }
2261 Py_DECREF(seq);
2262 DISPATCH();
2263 }
2264
Benjamin Petersonddd19492018-09-16 22:38:02 -07002265 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002266 PyObject *name = GETITEM(names, oparg);
2267 PyObject *owner = TOP();
2268 PyObject *v = SECOND();
2269 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002270 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002271 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002272 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002273 Py_DECREF(owner);
2274 if (err != 0)
2275 goto error;
2276 DISPATCH();
2277 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002278
Benjamin Petersonddd19492018-09-16 22:38:02 -07002279 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002280 PyObject *name = GETITEM(names, oparg);
2281 PyObject *owner = POP();
2282 int err;
2283 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2284 Py_DECREF(owner);
2285 if (err != 0)
2286 goto error;
2287 DISPATCH();
2288 }
2289
Benjamin Petersonddd19492018-09-16 22:38:02 -07002290 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002291 PyObject *name = GETITEM(names, oparg);
2292 PyObject *v = POP();
2293 int err;
2294 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002295 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002296 if (err != 0)
2297 goto error;
2298 DISPATCH();
2299 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002300
Benjamin Petersonddd19492018-09-16 22:38:02 -07002301 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002302 PyObject *name = GETITEM(names, oparg);
2303 int err;
2304 err = PyDict_DelItem(f->f_globals, name);
2305 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002306 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2307 format_exc_check_arg(tstate, PyExc_NameError,
2308 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002309 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002310 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002311 }
2312 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002313 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002314
Benjamin Petersonddd19492018-09-16 22:38:02 -07002315 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002316 PyObject *name = GETITEM(names, oparg);
2317 PyObject *locals = f->f_locals;
2318 PyObject *v;
2319 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002320 _PyErr_Format(tstate, PyExc_SystemError,
2321 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002322 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002323 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002324 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002325 v = PyDict_GetItemWithError(locals, name);
2326 if (v != NULL) {
2327 Py_INCREF(v);
2328 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002329 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002330 goto error;
2331 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002332 }
2333 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002334 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002335 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002336 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002337 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002338 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002339 }
2340 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002341 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002342 v = PyDict_GetItemWithError(f->f_globals, name);
2343 if (v != NULL) {
2344 Py_INCREF(v);
2345 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002346 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002347 goto error;
2348 }
2349 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002350 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002351 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002352 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002353 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002354 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002355 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002356 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002357 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002358 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002359 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002360 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002361 }
2362 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002363 v = PyObject_GetItem(f->f_builtins, name);
2364 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002365 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002366 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002367 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002368 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002369 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002370 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002371 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002372 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002373 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002374 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002375 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002376 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002377 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002378
Benjamin Petersonddd19492018-09-16 22:38:02 -07002379 case TARGET(LOAD_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002380 PyObject *name = GETITEM(names, oparg);
2381 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002382 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002383 && PyDict_CheckExact(f->f_builtins))
2384 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002385 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002386 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002387 name);
2388 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002389 if (!_PyErr_OCCURRED()) {
2390 /* _PyDict_LoadGlobal() returns NULL without raising
2391 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002392 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002393 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002394 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002395 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002396 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002397 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002398 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002399 else {
2400 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002401
2402 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002403 v = PyObject_GetItem(f->f_globals, name);
2404 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002405 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002406 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002407 }
2408 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002409
Victor Stinnerb4efc962015-11-20 09:24:02 +01002410 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002411 v = PyObject_GetItem(f->f_builtins, name);
2412 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002413 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002414 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002415 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002416 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002417 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002418 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002419 }
2420 }
2421 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002422 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002423 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002424 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002425
Benjamin Petersonddd19492018-09-16 22:38:02 -07002426 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002427 PyObject *v = GETLOCAL(oparg);
2428 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002429 SETLOCAL(oparg, NULL);
2430 DISPATCH();
2431 }
2432 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002433 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002434 UNBOUNDLOCAL_ERROR_MSG,
2435 PyTuple_GetItem(co->co_varnames, oparg)
2436 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002437 goto error;
2438 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002439
Benjamin Petersonddd19492018-09-16 22:38:02 -07002440 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002441 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002442 PyObject *oldobj = PyCell_GET(cell);
2443 if (oldobj != NULL) {
2444 PyCell_SET(cell, NULL);
2445 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002446 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002447 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002448 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002449 goto error;
2450 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002451
Benjamin Petersonddd19492018-09-16 22:38:02 -07002452 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002453 PyObject *cell = freevars[oparg];
2454 Py_INCREF(cell);
2455 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002456 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002457 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002458
Benjamin Petersonddd19492018-09-16 22:38:02 -07002459 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002460 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002461 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002462 assert(locals);
2463 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2464 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2465 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2466 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2467 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002468 value = PyDict_GetItemWithError(locals, name);
2469 if (value != NULL) {
2470 Py_INCREF(value);
2471 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002472 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002473 goto error;
2474 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002475 }
2476 else {
2477 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002478 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002479 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002480 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002481 }
2482 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002483 }
2484 }
2485 if (!value) {
2486 PyObject *cell = freevars[oparg];
2487 value = PyCell_GET(cell);
2488 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002489 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002490 goto error;
2491 }
2492 Py_INCREF(value);
2493 }
2494 PUSH(value);
2495 DISPATCH();
2496 }
2497
Benjamin Petersonddd19492018-09-16 22:38:02 -07002498 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002499 PyObject *cell = freevars[oparg];
2500 PyObject *value = PyCell_GET(cell);
2501 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002502 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002503 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002504 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002505 Py_INCREF(value);
2506 PUSH(value);
2507 DISPATCH();
2508 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002509
Benjamin Petersonddd19492018-09-16 22:38:02 -07002510 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002511 PyObject *v = POP();
2512 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002513 PyObject *oldobj = PyCell_GET(cell);
2514 PyCell_SET(cell, v);
2515 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002516 DISPATCH();
2517 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002518
Benjamin Petersonddd19492018-09-16 22:38:02 -07002519 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002520 PyObject *str;
2521 PyObject *empty = PyUnicode_New(0, 0);
2522 if (empty == NULL) {
2523 goto error;
2524 }
2525 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2526 Py_DECREF(empty);
2527 if (str == NULL)
2528 goto error;
2529 while (--oparg >= 0) {
2530 PyObject *item = POP();
2531 Py_DECREF(item);
2532 }
2533 PUSH(str);
2534 DISPATCH();
2535 }
2536
Benjamin Petersonddd19492018-09-16 22:38:02 -07002537 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002538 PyObject *tup = PyTuple_New(oparg);
2539 if (tup == NULL)
2540 goto error;
2541 while (--oparg >= 0) {
2542 PyObject *item = POP();
2543 PyTuple_SET_ITEM(tup, oparg, item);
2544 }
2545 PUSH(tup);
2546 DISPATCH();
2547 }
2548
Benjamin Petersonddd19492018-09-16 22:38:02 -07002549 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002550 PyObject *list = PyList_New(oparg);
2551 if (list == NULL)
2552 goto error;
2553 while (--oparg >= 0) {
2554 PyObject *item = POP();
2555 PyList_SET_ITEM(list, oparg, item);
2556 }
2557 PUSH(list);
2558 DISPATCH();
2559 }
2560
Benjamin Petersonddd19492018-09-16 22:38:02 -07002561 case TARGET(BUILD_TUPLE_UNPACK_WITH_CALL):
2562 case TARGET(BUILD_TUPLE_UNPACK):
2563 case TARGET(BUILD_LIST_UNPACK): {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002564 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002565 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002566 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002567 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002568
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002569 if (sum == NULL)
2570 goto error;
2571
2572 for (i = oparg; i > 0; i--) {
2573 PyObject *none_val;
2574
2575 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2576 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002577 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
Victor Stinner438a12d2019-05-24 17:01:38 +02002578 _PyErr_ExceptionMatches(tstate, PyExc_TypeError))
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002579 {
Victor Stinner438a12d2019-05-24 17:01:38 +02002580 check_args_iterable(tstate, PEEK(1 + oparg), PEEK(i));
Serhiy Storchaka73442852016-10-02 10:33:46 +03002581 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002582 Py_DECREF(sum);
2583 goto error;
2584 }
2585 Py_DECREF(none_val);
2586 }
2587
2588 if (convert_to_tuple) {
2589 return_value = PyList_AsTuple(sum);
2590 Py_DECREF(sum);
2591 if (return_value == NULL)
2592 goto error;
2593 }
2594 else {
2595 return_value = sum;
2596 }
2597
2598 while (oparg--)
2599 Py_DECREF(POP());
2600 PUSH(return_value);
2601 DISPATCH();
2602 }
2603
Benjamin Petersonddd19492018-09-16 22:38:02 -07002604 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002605 PyObject *set = PySet_New(NULL);
2606 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002607 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002608 if (set == NULL)
2609 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002610 for (i = oparg; i > 0; i--) {
2611 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002612 if (err == 0)
2613 err = PySet_Add(set, item);
2614 Py_DECREF(item);
2615 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002616 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002617 if (err != 0) {
2618 Py_DECREF(set);
2619 goto error;
2620 }
2621 PUSH(set);
2622 DISPATCH();
2623 }
2624
Benjamin Petersonddd19492018-09-16 22:38:02 -07002625 case TARGET(BUILD_SET_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002626 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002627 PyObject *sum = PySet_New(NULL);
2628 if (sum == NULL)
2629 goto error;
2630
2631 for (i = oparg; i > 0; i--) {
2632 if (_PySet_Update(sum, PEEK(i)) < 0) {
2633 Py_DECREF(sum);
2634 goto error;
2635 }
2636 }
2637
2638 while (oparg--)
2639 Py_DECREF(POP());
2640 PUSH(sum);
2641 DISPATCH();
2642 }
2643
Benjamin Petersonddd19492018-09-16 22:38:02 -07002644 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002645 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002646 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2647 if (map == NULL)
2648 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002649 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002650 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002651 PyObject *key = PEEK(2*i);
2652 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002653 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002654 if (err != 0) {
2655 Py_DECREF(map);
2656 goto error;
2657 }
2658 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002659
2660 while (oparg--) {
2661 Py_DECREF(POP());
2662 Py_DECREF(POP());
2663 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002664 PUSH(map);
2665 DISPATCH();
2666 }
2667
Benjamin Petersonddd19492018-09-16 22:38:02 -07002668 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002669 _Py_IDENTIFIER(__annotations__);
2670 int err;
2671 PyObject *ann_dict;
2672 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002673 _PyErr_Format(tstate, PyExc_SystemError,
2674 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002675 goto error;
2676 }
2677 /* check if __annotations__ in locals()... */
2678 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002679 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002680 &PyId___annotations__);
2681 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002682 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002683 goto error;
2684 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002685 /* ...if not, create a new one */
2686 ann_dict = PyDict_New();
2687 if (ann_dict == NULL) {
2688 goto error;
2689 }
2690 err = _PyDict_SetItemId(f->f_locals,
2691 &PyId___annotations__, ann_dict);
2692 Py_DECREF(ann_dict);
2693 if (err != 0) {
2694 goto error;
2695 }
2696 }
2697 }
2698 else {
2699 /* do the same if locals() is not a dict */
2700 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2701 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002702 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002703 }
2704 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2705 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002706 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002707 goto error;
2708 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002709 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002710 ann_dict = PyDict_New();
2711 if (ann_dict == NULL) {
2712 goto error;
2713 }
2714 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2715 Py_DECREF(ann_dict);
2716 if (err != 0) {
2717 goto error;
2718 }
2719 }
2720 else {
2721 Py_DECREF(ann_dict);
2722 }
2723 }
2724 DISPATCH();
2725 }
2726
Benjamin Petersonddd19492018-09-16 22:38:02 -07002727 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002728 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002729 PyObject *map;
2730 PyObject *keys = TOP();
2731 if (!PyTuple_CheckExact(keys) ||
2732 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002733 _PyErr_SetString(tstate, PyExc_SystemError,
2734 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002735 goto error;
2736 }
2737 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2738 if (map == NULL) {
2739 goto error;
2740 }
2741 for (i = oparg; i > 0; i--) {
2742 int err;
2743 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2744 PyObject *value = PEEK(i + 1);
2745 err = PyDict_SetItem(map, key, value);
2746 if (err != 0) {
2747 Py_DECREF(map);
2748 goto error;
2749 }
2750 }
2751
2752 Py_DECREF(POP());
2753 while (oparg--) {
2754 Py_DECREF(POP());
2755 }
2756 PUSH(map);
2757 DISPATCH();
2758 }
2759
Benjamin Petersonddd19492018-09-16 22:38:02 -07002760 case TARGET(BUILD_MAP_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002761 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002762 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002763 if (sum == NULL)
2764 goto error;
2765
2766 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002767 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002768 if (PyDict_Update(sum, arg) < 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002769 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
2770 _PyErr_Format(tstate, PyExc_TypeError,
2771 "'%.200s' object is not a mapping",
2772 arg->ob_type->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002773 }
2774 Py_DECREF(sum);
2775 goto error;
2776 }
2777 }
2778
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002779 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002780 Py_DECREF(POP());
2781 PUSH(sum);
2782 DISPATCH();
2783 }
2784
Benjamin Petersonddd19492018-09-16 22:38:02 -07002785 case TARGET(BUILD_MAP_UNPACK_WITH_CALL): {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002786 Py_ssize_t i;
2787 PyObject *sum = PyDict_New();
2788 if (sum == NULL)
2789 goto error;
2790
2791 for (i = oparg; i > 0; i--) {
2792 PyObject *arg = PEEK(i);
2793 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002794 Py_DECREF(sum);
Victor Stinner438a12d2019-05-24 17:01:38 +02002795 format_kwargs_error(tstate, PEEK(2 + oparg), arg);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002796 goto error;
2797 }
2798 }
2799
2800 while (oparg--)
2801 Py_DECREF(POP());
2802 PUSH(sum);
2803 DISPATCH();
2804 }
2805
Benjamin Petersonddd19492018-09-16 22:38:02 -07002806 case TARGET(MAP_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002807 PyObject *key = TOP();
2808 PyObject *value = SECOND();
2809 PyObject *map;
2810 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002811 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002812 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002813 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002814 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002815 Py_DECREF(value);
2816 Py_DECREF(key);
2817 if (err != 0)
2818 goto error;
2819 PREDICT(JUMP_ABSOLUTE);
2820 DISPATCH();
2821 }
2822
Benjamin Petersonddd19492018-09-16 22:38:02 -07002823 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002824 PyObject *name = GETITEM(names, oparg);
2825 PyObject *owner = TOP();
2826 PyObject *res = PyObject_GetAttr(owner, name);
2827 Py_DECREF(owner);
2828 SET_TOP(res);
2829 if (res == NULL)
2830 goto error;
2831 DISPATCH();
2832 }
2833
Benjamin Petersonddd19492018-09-16 22:38:02 -07002834 case TARGET(COMPARE_OP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002835 PyObject *right = POP();
2836 PyObject *left = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002837 PyObject *res = cmp_outcome(tstate, oparg, left, right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002838 Py_DECREF(left);
2839 Py_DECREF(right);
2840 SET_TOP(res);
2841 if (res == NULL)
2842 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002843 PREDICT(POP_JUMP_IF_FALSE);
2844 PREDICT(POP_JUMP_IF_TRUE);
2845 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002846 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002847
Benjamin Petersonddd19492018-09-16 22:38:02 -07002848 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002849 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002850 PyObject *fromlist = POP();
2851 PyObject *level = TOP();
2852 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02002853 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002854 Py_DECREF(level);
2855 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002856 SET_TOP(res);
2857 if (res == NULL)
2858 goto error;
2859 DISPATCH();
2860 }
2861
Benjamin Petersonddd19492018-09-16 22:38:02 -07002862 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002863 PyObject *from = POP(), *locals;
2864 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002865 if (PyFrame_FastToLocalsWithError(f) < 0) {
2866 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002867 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002868 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002869
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002870 locals = f->f_locals;
2871 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002872 _PyErr_SetString(tstate, PyExc_SystemError,
2873 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002874 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002875 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002876 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002877 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002878 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002879 Py_DECREF(from);
2880 if (err != 0)
2881 goto error;
2882 DISPATCH();
2883 }
Guido van Rossum25831651993-05-19 14:50:45 +00002884
Benjamin Petersonddd19492018-09-16 22:38:02 -07002885 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002886 PyObject *name = GETITEM(names, oparg);
2887 PyObject *from = TOP();
2888 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02002889 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002890 PUSH(res);
2891 if (res == NULL)
2892 goto error;
2893 DISPATCH();
2894 }
Thomas Wouters52152252000-08-17 22:55:00 +00002895
Benjamin Petersonddd19492018-09-16 22:38:02 -07002896 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002897 JUMPBY(oparg);
2898 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002899 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002900
Benjamin Petersonddd19492018-09-16 22:38:02 -07002901 case TARGET(POP_JUMP_IF_FALSE): {
2902 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002903 PyObject *cond = POP();
2904 int err;
2905 if (cond == Py_True) {
2906 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002907 FAST_DISPATCH();
2908 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002909 if (cond == Py_False) {
2910 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002911 JUMPTO(oparg);
2912 FAST_DISPATCH();
2913 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002914 err = PyObject_IsTrue(cond);
2915 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002916 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07002917 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002918 else if (err == 0)
2919 JUMPTO(oparg);
2920 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002921 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002922 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002923 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002924
Benjamin Petersonddd19492018-09-16 22:38:02 -07002925 case TARGET(POP_JUMP_IF_TRUE): {
2926 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002927 PyObject *cond = POP();
2928 int err;
2929 if (cond == Py_False) {
2930 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002931 FAST_DISPATCH();
2932 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002933 if (cond == Py_True) {
2934 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002935 JUMPTO(oparg);
2936 FAST_DISPATCH();
2937 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002938 err = PyObject_IsTrue(cond);
2939 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002940 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002941 JUMPTO(oparg);
2942 }
2943 else if (err == 0)
2944 ;
2945 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002946 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002947 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002948 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002949
Benjamin Petersonddd19492018-09-16 22:38:02 -07002950 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002951 PyObject *cond = TOP();
2952 int err;
2953 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002954 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002955 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002956 FAST_DISPATCH();
2957 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002958 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002959 JUMPTO(oparg);
2960 FAST_DISPATCH();
2961 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002962 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002963 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002964 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002965 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002966 }
2967 else if (err == 0)
2968 JUMPTO(oparg);
2969 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002970 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002971 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002972 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002973
Benjamin Petersonddd19492018-09-16 22:38:02 -07002974 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002975 PyObject *cond = TOP();
2976 int err;
2977 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002978 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002979 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002980 FAST_DISPATCH();
2981 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002982 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002983 JUMPTO(oparg);
2984 FAST_DISPATCH();
2985 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002986 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002987 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002988 JUMPTO(oparg);
2989 }
2990 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002991 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002992 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002993 }
2994 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002995 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002996 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002997 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002998
Benjamin Petersonddd19492018-09-16 22:38:02 -07002999 case TARGET(JUMP_ABSOLUTE): {
3000 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003001 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003002#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003003 /* Enabling this path speeds-up all while and for-loops by bypassing
3004 the per-loop checks for signals. By default, this should be turned-off
3005 because it prevents detection of a control-break in tight loops like
3006 "while 1: pass". Compile with this option turned-on when you need
3007 the speed-up and do not need break checking inside tight loops (ones
3008 that contain only instructions ending with FAST_DISPATCH).
3009 */
3010 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003011#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003012 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003013#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003014 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003015
Benjamin Petersonddd19492018-09-16 22:38:02 -07003016 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003017 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003018 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003019 PyObject *iter = PyObject_GetIter(iterable);
3020 Py_DECREF(iterable);
3021 SET_TOP(iter);
3022 if (iter == NULL)
3023 goto error;
3024 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003025 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003026 DISPATCH();
3027 }
3028
Benjamin Petersonddd19492018-09-16 22:38:02 -07003029 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04003030 /* before: [obj]; after [getiter(obj)] */
3031 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003032 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003033 if (PyCoro_CheckExact(iterable)) {
3034 /* `iterable` is a coroutine */
3035 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3036 /* and it is used in a 'yield from' expression of a
3037 regular generator. */
3038 Py_DECREF(iterable);
3039 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003040 _PyErr_SetString(tstate, PyExc_TypeError,
3041 "cannot 'yield from' a coroutine object "
3042 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04003043 goto error;
3044 }
3045 }
3046 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003047 /* `iterable` is not a generator. */
3048 iter = PyObject_GetIter(iterable);
3049 Py_DECREF(iterable);
3050 SET_TOP(iter);
3051 if (iter == NULL)
3052 goto error;
3053 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003054 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003055 DISPATCH();
3056 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003057
Benjamin Petersonddd19492018-09-16 22:38:02 -07003058 case TARGET(FOR_ITER): {
3059 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003060 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003061 PyObject *iter = TOP();
3062 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
3063 if (next != NULL) {
3064 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003065 PREDICT(STORE_FAST);
3066 PREDICT(UNPACK_SEQUENCE);
3067 DISPATCH();
3068 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003069 if (_PyErr_Occurred(tstate)) {
3070 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003071 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003072 }
3073 else if (tstate->c_tracefunc != NULL) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003074 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Victor Stinner438a12d2019-05-24 17:01:38 +02003075 }
3076 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003077 }
3078 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003079 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003080 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003081 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003082 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003083 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003084 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003085
Benjamin Petersonddd19492018-09-16 22:38:02 -07003086 case TARGET(SETUP_FINALLY): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003087 /* NOTE: If you add any new block-setup opcodes that
3088 are not try/except/finally handlers, you may need
3089 to update the PyGen_NeedsFinalizing() function.
3090 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003091
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003092 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003093 STACK_LEVEL());
3094 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003095 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003096
Benjamin Petersonddd19492018-09-16 22:38:02 -07003097 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003098 _Py_IDENTIFIER(__aexit__);
3099 _Py_IDENTIFIER(__aenter__);
3100
3101 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003102 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__),
Yury Selivanov75445082015-05-11 22:57:16 -04003103 *enter;
3104 PyObject *res;
3105 if (exit == NULL)
3106 goto error;
3107 SET_TOP(exit);
Victor Stinner438a12d2019-05-24 17:01:38 +02003108 enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04003109 Py_DECREF(mgr);
3110 if (enter == NULL)
3111 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003112 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003113 Py_DECREF(enter);
3114 if (res == NULL)
3115 goto error;
3116 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003117 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003118 DISPATCH();
3119 }
3120
Benjamin Petersonddd19492018-09-16 22:38:02 -07003121 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003122 PyObject *res = POP();
3123 /* Setup the finally block before pushing the result
3124 of __aenter__ on the stack. */
3125 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3126 STACK_LEVEL());
3127 PUSH(res);
3128 DISPATCH();
3129 }
3130
Benjamin Petersonddd19492018-09-16 22:38:02 -07003131 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003132 _Py_IDENTIFIER(__exit__);
3133 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003134 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003135 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003136 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003137 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08003138 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003139 }
3140 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003141 if (exit == NULL) {
3142 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003143 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003144 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003145 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003146 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003147 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003148 Py_DECREF(enter);
3149 if (res == NULL)
3150 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003151 /* Setup the finally block before pushing the result
3152 of __enter__ on the stack. */
3153 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3154 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003155
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003156 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003157 DISPATCH();
3158 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003159
Benjamin Petersonddd19492018-09-16 22:38:02 -07003160 case TARGET(WITH_CLEANUP_START): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003161 /* At the top of the stack are 1 or 6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003162 how/why we entered the finally clause:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003163 - TOP = NULL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003164 - (TOP, SECOND, THIRD) = exc_info()
3165 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003166 Below them is EXIT, the context.__exit__ or context.__aexit__
3167 bound method.
3168 In the first case, we must call
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003169 EXIT(None, None, None)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003170 otherwise we must call
3171 EXIT(TOP, SECOND, THIRD)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003172
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003173 In the first case, we remove EXIT from the
3174 stack, leaving TOP, and push TOP on the stack.
3175 Otherwise we shift the bottom 3 values of the
3176 stack down, replace the empty spot with NULL, and push
3177 None on the stack.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003178
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003179 Finally we push the result of the call.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003180 */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003181 PyObject *stack[3];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003182 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003183 PyObject *exc, *val, *tb, *res;
3184
3185 val = tb = Py_None;
3186 exc = TOP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003187 if (exc == NULL) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003188 STACK_SHRINK(1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003189 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003190 SET_TOP(exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003191 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003192 }
3193 else {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003194 assert(PyExceptionClass_Check(exc));
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003195 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003196 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003197 val = SECOND();
3198 tb = THIRD();
3199 tp2 = FOURTH();
3200 exc2 = PEEK(5);
3201 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003202 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003203 SET_VALUE(7, tb2);
3204 SET_VALUE(6, exc2);
3205 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003206 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3207 SET_FOURTH(NULL);
3208 /* We just shifted the stack down, so we have
3209 to tell the except handler block that the
3210 values are lower than it expects. */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003211 assert(f->f_iblock > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003212 block = &f->f_blockstack[f->f_iblock - 1];
3213 assert(block->b_type == EXCEPT_HANDLER);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003214 assert(block->b_level > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003215 block->b_level--;
3216 }
Victor Stinner842cfff2016-12-01 14:45:31 +01003217
3218 stack[0] = exc;
3219 stack[1] = val;
3220 stack[2] = tb;
3221 res = _PyObject_FastCall(exit_func, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003222 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003223 if (res == NULL)
3224 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003225
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003226 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003227 PUSH(exc);
3228 PUSH(res);
3229 PREDICT(WITH_CLEANUP_FINISH);
3230 DISPATCH();
3231 }
3232
Benjamin Petersonddd19492018-09-16 22:38:02 -07003233 case TARGET(WITH_CLEANUP_FINISH): {
3234 PREDICTED(WITH_CLEANUP_FINISH);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003235 /* TOP = the result of calling the context.__exit__ bound method
3236 SECOND = either None or exception type
3237
3238 If SECOND is None below is NULL or the return address,
3239 otherwise below are 7 values representing an exception.
3240 */
Yury Selivanov75445082015-05-11 22:57:16 -04003241 PyObject *res = POP();
3242 PyObject *exc = POP();
3243 int err;
3244
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003245 if (exc != Py_None)
3246 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003247 else
3248 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003249
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003250 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003251 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003253 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003254 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003255 else if (err > 0) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003256 /* There was an exception and a True return.
3257 * We must manually unwind the EXCEPT_HANDLER block
3258 * which was created when the exception was caught,
Quan Tian3bd0d622018-10-20 05:30:03 +08003259 * otherwise the stack will be in an inconsistent state.
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003260 */
3261 PyTryBlock *b = PyFrame_BlockPop(f);
3262 assert(b->b_type == EXCEPT_HANDLER);
3263 UNWIND_EXCEPT_HANDLER(b);
3264 PUSH(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003265 }
3266 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003267 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003268 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003269
Benjamin Petersonddd19492018-09-16 22:38:02 -07003270 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10003271 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003272 PyObject *name = GETITEM(names, oparg);
3273 PyObject *obj = TOP();
3274 PyObject *meth = NULL;
3275
3276 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3277
Yury Selivanovf2392132016-12-13 19:03:51 -05003278 if (meth == NULL) {
3279 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003280 goto error;
3281 }
3282
3283 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003284 /* We can bypass temporary bound method object.
3285 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003286
INADA Naoki015bce62017-01-16 17:23:30 +09003287 meth | self | arg1 | ... | argN
3288 */
3289 SET_TOP(meth);
3290 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003291 }
3292 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003293 /* meth is not an unbound method (but a regular attr, or
3294 something was returned by a descriptor protocol). Set
3295 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003296 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003297
3298 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003299 */
INADA Naoki015bce62017-01-16 17:23:30 +09003300 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003301 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003302 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003303 }
3304 DISPATCH();
3305 }
3306
Benjamin Petersonddd19492018-09-16 22:38:02 -07003307 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003308 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003309 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003310
3311 sp = stack_pointer;
3312
INADA Naoki015bce62017-01-16 17:23:30 +09003313 meth = PEEK(oparg + 2);
3314 if (meth == NULL) {
3315 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3316 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003317
3318 Stack layout:
3319
INADA Naoki015bce62017-01-16 17:23:30 +09003320 ... | NULL | callable | arg1 | ... | argN
3321 ^- TOP()
3322 ^- (-oparg)
3323 ^- (-oparg-1)
3324 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003325
Ville Skyttä49b27342017-08-03 09:00:59 +03003326 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003327 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003328 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003329 res = call_function(tstate, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003330 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003331 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003332 }
3333 else {
3334 /* This is a method call. Stack layout:
3335
INADA Naoki015bce62017-01-16 17:23:30 +09003336 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003337 ^- TOP()
3338 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003339 ^- (-oparg-1)
3340 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003341
INADA Naoki015bce62017-01-16 17:23:30 +09003342 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003343 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003344 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003345 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003346 res = call_function(tstate, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003347 stack_pointer = sp;
3348 }
3349
3350 PUSH(res);
3351 if (res == NULL)
3352 goto error;
3353 DISPATCH();
3354 }
3355
Benjamin Petersonddd19492018-09-16 22:38:02 -07003356 case TARGET(CALL_FUNCTION): {
3357 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003358 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003359 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003360 res = call_function(tstate, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003361 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003362 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003363 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003364 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003365 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003366 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003367 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003368
Benjamin Petersonddd19492018-09-16 22:38:02 -07003369 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003370 PyObject **sp, *res, *names;
3371
3372 names = POP();
3373 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003374 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003375 res = call_function(tstate, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003376 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003377 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003378 Py_DECREF(names);
3379
3380 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003381 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003382 }
3383 DISPATCH();
3384 }
3385
Benjamin Petersonddd19492018-09-16 22:38:02 -07003386 case TARGET(CALL_FUNCTION_EX): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003387 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003388 if (oparg & 0x01) {
3389 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003390 if (!PyDict_CheckExact(kwargs)) {
3391 PyObject *d = PyDict_New();
3392 if (d == NULL)
3393 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003394 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003395 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02003396 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003397 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003398 goto error;
3399 }
3400 Py_DECREF(kwargs);
3401 kwargs = d;
3402 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003403 assert(PyDict_CheckExact(kwargs));
3404 }
3405 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003406 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003407 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003408 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003409 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003410 goto error;
3411 }
3412 Py_SETREF(callargs, PySequence_Tuple(callargs));
3413 if (callargs == NULL) {
3414 goto error;
3415 }
3416 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003417 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003418
Victor Stinner09532fe2019-05-10 23:39:09 +02003419 result = do_call_core(tstate, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003420 Py_DECREF(func);
3421 Py_DECREF(callargs);
3422 Py_XDECREF(kwargs);
3423
3424 SET_TOP(result);
3425 if (result == NULL) {
3426 goto error;
3427 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003428 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003429 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003430
Benjamin Petersonddd19492018-09-16 22:38:02 -07003431 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003432 PyObject *qualname = POP();
3433 PyObject *codeobj = POP();
3434 PyFunctionObject *func = (PyFunctionObject *)
3435 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003436
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003437 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003438 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003439 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003440 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003441 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003442
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003443 if (oparg & 0x08) {
3444 assert(PyTuple_CheckExact(TOP()));
3445 func ->func_closure = POP();
3446 }
3447 if (oparg & 0x04) {
3448 assert(PyDict_CheckExact(TOP()));
3449 func->func_annotations = POP();
3450 }
3451 if (oparg & 0x02) {
3452 assert(PyDict_CheckExact(TOP()));
3453 func->func_kwdefaults = POP();
3454 }
3455 if (oparg & 0x01) {
3456 assert(PyTuple_CheckExact(TOP()));
3457 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003458 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003459
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003460 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003461 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003462 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003463
Benjamin Petersonddd19492018-09-16 22:38:02 -07003464 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003465 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003466 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003467 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003468 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003469 step = NULL;
3470 stop = POP();
3471 start = TOP();
3472 slice = PySlice_New(start, stop, step);
3473 Py_DECREF(start);
3474 Py_DECREF(stop);
3475 Py_XDECREF(step);
3476 SET_TOP(slice);
3477 if (slice == NULL)
3478 goto error;
3479 DISPATCH();
3480 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003481
Benjamin Petersonddd19492018-09-16 22:38:02 -07003482 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003483 /* Handles f-string value formatting. */
3484 PyObject *result;
3485 PyObject *fmt_spec;
3486 PyObject *value;
3487 PyObject *(*conv_fn)(PyObject *);
3488 int which_conversion = oparg & FVC_MASK;
3489 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3490
3491 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003492 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003493
3494 /* See if any conversion is specified. */
3495 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003496 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003497 case FVC_STR: conv_fn = PyObject_Str; break;
3498 case FVC_REPR: conv_fn = PyObject_Repr; break;
3499 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003500 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02003501 _PyErr_Format(tstate, PyExc_SystemError,
3502 "unexpected conversion flag %d",
3503 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003504 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003505 }
3506
3507 /* If there's a conversion function, call it and replace
3508 value with that result. Otherwise, just use value,
3509 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003510 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003511 result = conv_fn(value);
3512 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003513 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003514 Py_XDECREF(fmt_spec);
3515 goto error;
3516 }
3517 value = result;
3518 }
3519
3520 /* If value is a unicode object, and there's no fmt_spec,
3521 then we know the result of format(value) is value
3522 itself. In that case, skip calling format(). I plan to
3523 move this optimization in to PyObject_Format()
3524 itself. */
3525 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3526 /* Do nothing, just transfer ownership to result. */
3527 result = value;
3528 } else {
3529 /* Actually call format(). */
3530 result = PyObject_Format(value, fmt_spec);
3531 Py_DECREF(value);
3532 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003533 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003534 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003535 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003536 }
3537
Eric V. Smith135d5f42016-02-05 18:23:08 -05003538 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003539 DISPATCH();
3540 }
3541
Benjamin Petersonddd19492018-09-16 22:38:02 -07003542 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003543 int oldoparg = oparg;
3544 NEXTOPARG();
3545 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003546 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003547 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003548
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003549
Antoine Pitrou042b1282010-08-13 21:15:58 +00003550#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003551 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003552#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003553 default:
3554 fprintf(stderr,
3555 "XXX lineno: %d, opcode: %d\n",
3556 PyFrame_GetLineNumber(f),
3557 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02003558 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003559 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003561 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003562
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003563 /* This should never be reached. Every opcode should end with DISPATCH()
3564 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003565 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003566
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003567error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003568 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003569#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02003570 if (!_PyErr_Occurred(tstate)) {
3571 _PyErr_SetString(tstate, PyExc_SystemError,
3572 "error return without exception set");
3573 }
Victor Stinner365b6932013-07-12 00:11:58 +02003574#else
Victor Stinner438a12d2019-05-24 17:01:38 +02003575 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02003576#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003577
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003578 /* Log traceback info. */
3579 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003580
Benjamin Peterson51f46162013-01-23 08:38:47 -05003581 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003582 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3583 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003584
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003585exception_unwind:
3586 /* Unwind stacks if an exception occurred */
3587 while (f->f_iblock > 0) {
3588 /* Pop the current block. */
3589 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003591 if (b->b_type == EXCEPT_HANDLER) {
3592 UNWIND_EXCEPT_HANDLER(b);
3593 continue;
3594 }
3595 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003596 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003597 PyObject *exc, *val, *tb;
3598 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003599 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003600 /* Beware, this invalidates all b->b_* fields */
3601 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003602 PUSH(exc_info->exc_traceback);
3603 PUSH(exc_info->exc_value);
3604 if (exc_info->exc_type != NULL) {
3605 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003606 }
3607 else {
3608 Py_INCREF(Py_None);
3609 PUSH(Py_None);
3610 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003611 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003612 /* Make the raw exception data
3613 available to the handler,
3614 so a program can emulate the
3615 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02003616 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003617 if (tb != NULL)
3618 PyException_SetTraceback(val, tb);
3619 else
3620 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003621 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003622 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003623 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003624 exc_info->exc_value = val;
3625 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003626 if (tb == NULL)
3627 tb = Py_None;
3628 Py_INCREF(tb);
3629 PUSH(tb);
3630 PUSH(val);
3631 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003632 JUMPTO(handler);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003633 /* Resume normal execution */
3634 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003635 }
3636 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003637
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003638 /* End the loop as we still have an error */
3639 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003640 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003641
Pablo Galindof00828a2019-05-09 16:52:02 +01003642 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003643 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01003644
3645exit_returning:
3646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003647 /* Pop remaining stack entries. */
3648 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003649 PyObject *o = POP();
3650 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003651 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003652
Pablo Galindof00828a2019-05-09 16:52:02 +01003653exit_yielding:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003654 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003655 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003656 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3657 tstate, f, PyTrace_RETURN, retval)) {
3658 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003659 }
3660 }
3661 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003662 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3663 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003664 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003665 }
3666 }
3667 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003669 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003670exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003671 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3672 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003673 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003674 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003675 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003676
Victor Stinnerefde1462015-03-21 15:04:43 +01003677 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003678}
3679
Benjamin Petersonb204a422011-06-05 22:04:07 -05003680static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003681format_missing(PyThreadState *tstate, const char *kind,
3682 PyCodeObject *co, PyObject *names)
Benjamin Petersone109c702011-06-24 09:37:26 -05003683{
3684 int err;
3685 Py_ssize_t len = PyList_GET_SIZE(names);
3686 PyObject *name_str, *comma, *tail, *tmp;
3687
3688 assert(PyList_CheckExact(names));
3689 assert(len >= 1);
3690 /* Deal with the joys of natural language. */
3691 switch (len) {
3692 case 1:
3693 name_str = PyList_GET_ITEM(names, 0);
3694 Py_INCREF(name_str);
3695 break;
3696 case 2:
3697 name_str = PyUnicode_FromFormat("%U and %U",
3698 PyList_GET_ITEM(names, len - 2),
3699 PyList_GET_ITEM(names, len - 1));
3700 break;
3701 default:
3702 tail = PyUnicode_FromFormat(", %U, and %U",
3703 PyList_GET_ITEM(names, len - 2),
3704 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003705 if (tail == NULL)
3706 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003707 /* Chop off the last two objects in the list. This shouldn't actually
3708 fail, but we can't be too careful. */
3709 err = PyList_SetSlice(names, len - 2, len, NULL);
3710 if (err == -1) {
3711 Py_DECREF(tail);
3712 return;
3713 }
3714 /* Stitch everything up into a nice comma-separated list. */
3715 comma = PyUnicode_FromString(", ");
3716 if (comma == NULL) {
3717 Py_DECREF(tail);
3718 return;
3719 }
3720 tmp = PyUnicode_Join(comma, names);
3721 Py_DECREF(comma);
3722 if (tmp == NULL) {
3723 Py_DECREF(tail);
3724 return;
3725 }
3726 name_str = PyUnicode_Concat(tmp, tail);
3727 Py_DECREF(tmp);
3728 Py_DECREF(tail);
3729 break;
3730 }
3731 if (name_str == NULL)
3732 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02003733 _PyErr_Format(tstate, PyExc_TypeError,
3734 "%U() missing %i required %s argument%s: %U",
3735 co->co_name,
3736 len,
3737 kind,
3738 len == 1 ? "" : "s",
3739 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05003740 Py_DECREF(name_str);
3741}
3742
3743static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003744missing_arguments(PyThreadState *tstate, PyCodeObject *co,
3745 Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003746 PyObject **fastlocals)
3747{
Victor Stinner74319ae2016-08-25 00:04:09 +02003748 Py_ssize_t i, j = 0;
3749 Py_ssize_t start, end;
3750 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003751 const char *kind = positional ? "positional" : "keyword-only";
3752 PyObject *missing_names;
3753
3754 /* Compute the names of the arguments that are missing. */
3755 missing_names = PyList_New(missing);
3756 if (missing_names == NULL)
3757 return;
3758 if (positional) {
3759 start = 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003760 end = co->co_posonlyargcount + co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003761 }
3762 else {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003763 start = co->co_posonlyargcount + co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003764 end = start + co->co_kwonlyargcount;
3765 }
3766 for (i = start; i < end; i++) {
3767 if (GETLOCAL(i) == NULL) {
3768 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3769 PyObject *name = PyObject_Repr(raw);
3770 if (name == NULL) {
3771 Py_DECREF(missing_names);
3772 return;
3773 }
3774 PyList_SET_ITEM(missing_names, j++, name);
3775 }
3776 }
3777 assert(j == missing);
Victor Stinner438a12d2019-05-24 17:01:38 +02003778 format_missing(tstate, kind, co, missing_names);
Benjamin Petersone109c702011-06-24 09:37:26 -05003779 Py_DECREF(missing_names);
3780}
3781
3782static void
Victor Stinner438a12d2019-05-24 17:01:38 +02003783too_many_positional(PyThreadState *tstate, PyCodeObject *co,
3784 Py_ssize_t given, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003785 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003786{
3787 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003788 Py_ssize_t kwonly_given = 0;
3789 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003790 PyObject *sig, *kwonly_sig;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003791 Py_ssize_t co_posonlyargcount = co->co_posonlyargcount;
Victor Stinner74319ae2016-08-25 00:04:09 +02003792 Py_ssize_t co_argcount = co->co_argcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003793 Py_ssize_t total_positional = co_argcount + co_posonlyargcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003794
Benjamin Petersone109c702011-06-24 09:37:26 -05003795 assert((co->co_flags & CO_VARARGS) == 0);
3796 /* Count missing keyword-only args. */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003797 for (i = total_positional; i < total_positional + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003798 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003799 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003800 }
3801 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003802 if (defcount) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003803 Py_ssize_t atleast = total_positional - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003804 plural = 1;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003805 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, total_positional);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003806 }
3807 else {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003808 plural = (total_positional != 1);
3809 sig = PyUnicode_FromFormat("%zd", total_positional);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003810 }
3811 if (sig == NULL)
3812 return;
3813 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003814 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3815 kwonly_sig = PyUnicode_FromFormat(format,
3816 given != 1 ? "s" : "",
3817 kwonly_given,
3818 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003819 if (kwonly_sig == NULL) {
3820 Py_DECREF(sig);
3821 return;
3822 }
3823 }
3824 else {
3825 /* This will not fail. */
3826 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003827 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003828 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003829 _PyErr_Format(tstate, PyExc_TypeError,
3830 "%U() takes %U positional argument%s but %zd%U %s given",
3831 co->co_name,
3832 sig,
3833 plural ? "s" : "",
3834 given,
3835 kwonly_sig,
3836 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003837 Py_DECREF(sig);
3838 Py_DECREF(kwonly_sig);
3839}
3840
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003841static int
Victor Stinner438a12d2019-05-24 17:01:38 +02003842positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
3843 Py_ssize_t kwcount, PyObject* const* kwnames)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003844{
3845 int posonly_conflicts = 0;
3846 PyObject* posonly_names = PyList_New(0);
3847
3848 for(int k=0; k < co->co_posonlyargcount; k++){
3849 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
3850
3851 for (int k2=0; k2<kwcount; k2++){
3852 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
3853 PyObject* kwname = kwnames[k2];
3854 if (kwname == posonly_name){
3855 if(PyList_Append(posonly_names, kwname) != 0) {
3856 goto fail;
3857 }
3858 posonly_conflicts++;
3859 continue;
3860 }
3861
3862 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
3863
3864 if ( cmp > 0) {
3865 if(PyList_Append(posonly_names, kwname) != 0) {
3866 goto fail;
3867 }
3868 posonly_conflicts++;
3869 } else if (cmp < 0) {
3870 goto fail;
3871 }
3872
3873 }
3874 }
3875 if (posonly_conflicts) {
3876 PyObject* comma = PyUnicode_FromString(", ");
3877 if (comma == NULL) {
3878 goto fail;
3879 }
3880 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
3881 Py_DECREF(comma);
3882 if (error_names == NULL) {
3883 goto fail;
3884 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003885 _PyErr_Format(tstate, PyExc_TypeError,
3886 "%U() got some positional-only arguments passed"
3887 " as keyword arguments: '%U'",
3888 co->co_name, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003889 Py_DECREF(error_names);
3890 goto fail;
3891 }
3892
3893 Py_DECREF(posonly_names);
3894 return 0;
3895
3896fail:
3897 Py_XDECREF(posonly_names);
3898 return 1;
3899
3900}
3901
Guido van Rossumc2e20742006-02-27 22:32:47 +00003902/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02003903 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003904 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003905
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003906PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +02003907_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003908 PyObject *const *args, Py_ssize_t argcount,
3909 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003910 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003911 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003912 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003913 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003914{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003915 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003916 PyFrameObject *f;
3917 PyObject *retval = NULL;
3918 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003919 PyObject *x, *u;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003920 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount + co->co_posonlyargcount;
3921 Py_ssize_t i, j, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003922 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003923
Victor Stinner438a12d2019-05-24 17:01:38 +02003924 PyThreadState *tstate = _PyThreadState_GET();
3925 assert(tstate != NULL);
3926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003927 if (globals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003928 _PyErr_SetString(tstate, PyExc_SystemError,
3929 "PyEval_EvalCodeEx: NULL globals");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003930 return NULL;
3931 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003932
Victor Stinnerc7020012016-08-16 23:40:29 +02003933 /* Create the frame */
INADA Naoki5a625d02016-12-24 20:19:08 +09003934 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003935 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003936 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003937 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003938 fastlocals = f->f_localsplus;
3939 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003940
Victor Stinnerc7020012016-08-16 23:40:29 +02003941 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003942 if (co->co_flags & CO_VARKEYWORDS) {
3943 kwdict = PyDict_New();
3944 if (kwdict == NULL)
3945 goto fail;
3946 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003947 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003948 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003949 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003950 SETLOCAL(i, kwdict);
3951 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003952 else {
3953 kwdict = NULL;
3954 }
3955
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003956 /* Copy positional only arguments into local variables */
3957 if (argcount > co->co_argcount + co->co_posonlyargcount) {
3958 n = co->co_posonlyargcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003959 }
3960 else {
3961 n = argcount;
3962 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003963 for (j = 0; j < n; j++) {
3964 x = args[j];
3965 Py_INCREF(x);
3966 SETLOCAL(j, x);
3967 }
3968
3969
3970 /* Copy positional arguments into local variables */
3971 if (argcount > co->co_argcount + co->co_posonlyargcount) {
3972 n += co->co_argcount;
3973 }
3974 else {
3975 n = argcount;
3976 }
3977 for (i = j; i < n; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003978 x = args[i];
3979 Py_INCREF(x);
3980 SETLOCAL(i, x);
3981 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003982
3983 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003984 if (co->co_flags & CO_VARARGS) {
Sergey Fedoseev234531b2019-02-25 21:59:12 +05003985 u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003986 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003987 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003988 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003989 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003990 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003991
Serhiy Storchakab7281052016-09-12 00:52:40 +03003992 /* Handle keyword arguments passed as two strided arrays */
3993 kwcount *= kwstep;
3994 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003995 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003996 PyObject *keyword = kwnames[i];
3997 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003998 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003999
Benjamin Petersonb204a422011-06-05 22:04:07 -05004000 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004001 _PyErr_Format(tstate, PyExc_TypeError,
4002 "%U() keywords must be strings",
4003 co->co_name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004004 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004005 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004006
Benjamin Petersonb204a422011-06-05 22:04:07 -05004007 /* Speed hack: do raw pointer compares. As names are
4008 normally interned this should almost always hit. */
4009 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004010 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004011 PyObject *name = co_varnames[j];
4012 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004013 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004014 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004015 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004016
Benjamin Petersonb204a422011-06-05 22:04:07 -05004017 /* Slow fallback, just in case */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004018 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004019 PyObject *name = co_varnames[j];
4020 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
4021 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004022 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004023 }
4024 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004025 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004026 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004027 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004028
Victor Stinner231d1f32017-01-11 02:12:06 +01004029 assert(j >= total_args);
4030 if (kwdict == NULL) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004031
Victor Stinner438a12d2019-05-24 17:01:38 +02004032 if (co->co_posonlyargcount
4033 && positional_only_passed_as_keyword(tstate, co,
4034 kwcount, kwnames))
4035 {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004036 goto fail;
4037 }
4038
Victor Stinner438a12d2019-05-24 17:01:38 +02004039 _PyErr_Format(tstate, PyExc_TypeError,
4040 "%U() got an unexpected keyword argument '%S'",
4041 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004042 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004043 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004044
Christian Heimes0bd447f2013-07-20 14:48:10 +02004045 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4046 goto fail;
4047 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004048 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004049
Benjamin Petersonb204a422011-06-05 22:04:07 -05004050 kw_found:
4051 if (GETLOCAL(j) != NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004052 _PyErr_Format(tstate, PyExc_TypeError,
4053 "%U() got multiple values for argument '%S'",
4054 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004055 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004056 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004057 Py_INCREF(value);
4058 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004059 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004060
4061 /* Check the number of positional arguments */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004062 if ((argcount > co->co_argcount + co->co_posonlyargcount) && !(co->co_flags & CO_VARARGS)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004063 too_many_positional(tstate, co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004064 goto fail;
4065 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004066
4067 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004068 if (argcount < co->co_posonlyargcount + co->co_argcount) {
4069 Py_ssize_t m = co->co_posonlyargcount + co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004070 Py_ssize_t missing = 0;
4071 for (i = argcount; i < m; i++) {
4072 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004073 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004074 }
4075 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004076 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004077 missing_arguments(tstate, co, missing, defcount, fastlocals);
Benjamin Petersone109c702011-06-24 09:37:26 -05004078 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004079 }
4080 if (n > m)
4081 i = n - m;
4082 else
4083 i = 0;
4084 for (; i < defcount; i++) {
4085 if (GETLOCAL(m+i) == NULL) {
4086 PyObject *def = defs[i];
4087 Py_INCREF(def);
4088 SETLOCAL(m+i, def);
4089 }
4090 }
4091 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004092
4093 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004094 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004095 Py_ssize_t missing = 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004096 for (i = co->co_posonlyargcount + co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004097 PyObject *name;
4098 if (GETLOCAL(i) != NULL)
4099 continue;
4100 name = PyTuple_GET_ITEM(co->co_varnames, i);
4101 if (kwdefs != NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004102 PyObject *def = PyDict_GetItemWithError(kwdefs, name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004103 if (def) {
4104 Py_INCREF(def);
4105 SETLOCAL(i, def);
4106 continue;
4107 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004108 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004109 goto fail;
4110 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004111 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004112 missing++;
4113 }
4114 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004115 missing_arguments(tstate, co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004116 goto fail;
4117 }
4118 }
4119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004120 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004121 vars into frame. */
4122 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004123 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004124 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004125 /* Possibly account for the cell variable being an argument. */
4126 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004127 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004128 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004129 /* Clear the local copy. */
4130 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004131 }
4132 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004133 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004134 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004135 if (c == NULL)
4136 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004137 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004138 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004139
4140 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004141 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4142 PyObject *o = PyTuple_GET_ITEM(closure, i);
4143 Py_INCREF(o);
4144 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004145 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004146
Yury Selivanoveb636452016-09-08 22:01:51 -07004147 /* Handle generator/coroutine/asynchronous generator */
4148 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004149 PyObject *gen;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004150 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004152 /* Don't need to keep the reference to f_back, it will be set
4153 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004154 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004155
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004156 /* Create a new generator that owns the ready to run frame
4157 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004158 if (is_coro) {
4159 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004160 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4161 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004162 } else {
4163 gen = PyGen_NewWithQualName(f, name, qualname);
4164 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004165 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004166 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004167 }
INADA Naoki9c157762016-12-26 18:52:46 +09004168
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004169 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004170
Yury Selivanov75445082015-05-11 22:57:16 -04004171 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004172 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004173
Victor Stinner59a73272016-12-09 18:51:13 +01004174 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004175
Thomas Woutersce272b62007-09-19 21:19:28 +00004176fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004178 /* decref'ing the frame can cause __del__ methods to get invoked,
4179 which can call back into Python. While we're done with the
4180 current Python frame (f), the associated C stack is still in use,
4181 so recursion_depth must be boosted for the duration.
4182 */
4183 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09004184 if (Py_REFCNT(f) > 1) {
4185 Py_DECREF(f);
4186 _PyObject_GC_TRACK(f);
4187 }
4188 else {
4189 ++tstate->recursion_depth;
4190 Py_DECREF(f);
4191 --tstate->recursion_depth;
4192 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004193 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004194}
4195
Victor Stinner40ee3012014-06-16 15:59:28 +02004196PyObject *
4197PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004198 PyObject *const *args, int argcount,
4199 PyObject *const *kws, int kwcount,
4200 PyObject *const *defs, int defcount,
4201 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004202{
4203 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004204 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004205 kws, kws != NULL ? kws + 1 : NULL,
4206 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004207 defs, defcount,
4208 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004209 NULL, NULL);
4210}
Tim Peters5ca576e2001-06-18 22:08:13 +00004211
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004212static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004213special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004214{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004215 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004216 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02004217 if (res == NULL && !_PyErr_Occurred(tstate)) {
4218 _PyErr_SetObject(tstate, PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004219 return NULL;
4220 }
4221 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004222}
4223
4224
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004225/* Logic for the raise statement (too complicated for inlining).
4226 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004227static int
Victor Stinner09532fe2019-05-10 23:39:09 +02004228do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004229{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004230 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004232 if (exc == NULL) {
4233 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01004234 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004235 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004236 type = exc_info->exc_type;
4237 value = exc_info->exc_value;
4238 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004239 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004240 _PyErr_SetString(tstate, PyExc_RuntimeError,
4241 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004242 return 0;
4243 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004244 Py_XINCREF(type);
4245 Py_XINCREF(value);
4246 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02004247 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004248 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004249 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004251 /* We support the following forms of raise:
4252 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004253 raise <instance>
4254 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004256 if (PyExceptionClass_Check(exc)) {
4257 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004258 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004259 if (value == NULL)
4260 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004261 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004262 _PyErr_Format(tstate, PyExc_TypeError,
4263 "calling %R should have returned an instance of "
4264 "BaseException, not %R",
4265 type, Py_TYPE(value));
4266 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004267 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004268 }
4269 else if (PyExceptionInstance_Check(exc)) {
4270 value = exc;
4271 type = PyExceptionInstance_Class(exc);
4272 Py_INCREF(type);
4273 }
4274 else {
4275 /* Not something you can raise. You get an exception
4276 anyway, just not what you specified :-) */
4277 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02004278 _PyErr_SetString(tstate, PyExc_TypeError,
4279 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004280 goto raise_error;
4281 }
Collin Winter828f04a2007-08-31 00:04:24 +00004282
Serhiy Storchakac0191582016-09-27 11:37:10 +03004283 assert(type != NULL);
4284 assert(value != NULL);
4285
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004286 if (cause) {
4287 PyObject *fixed_cause;
4288 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004289 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004290 if (fixed_cause == NULL)
4291 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004292 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004293 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004294 else if (PyExceptionInstance_Check(cause)) {
4295 fixed_cause = cause;
4296 }
4297 else if (cause == Py_None) {
4298 Py_DECREF(cause);
4299 fixed_cause = NULL;
4300 }
4301 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004302 _PyErr_SetString(tstate, PyExc_TypeError,
4303 "exception causes must derive from "
4304 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004305 goto raise_error;
4306 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004307 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004308 }
Collin Winter828f04a2007-08-31 00:04:24 +00004309
Victor Stinner438a12d2019-05-24 17:01:38 +02004310 _PyErr_SetObject(tstate, type, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004311 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004312 Py_DECREF(value);
4313 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004314 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004315
4316raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004317 Py_XDECREF(value);
4318 Py_XDECREF(type);
4319 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004320 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004321}
4322
Tim Petersd6d010b2001-06-21 02:49:55 +00004323/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004324 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004325
Guido van Rossum0368b722007-05-11 16:50:42 +00004326 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4327 with a variable target.
4328*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004329
Barry Warsawe42b18f1997-08-25 22:13:04 +00004330static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004331unpack_iterable(PyThreadState *tstate, PyObject *v,
4332 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004333{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004334 int i = 0, j = 0;
4335 Py_ssize_t ll = 0;
4336 PyObject *it; /* iter(v) */
4337 PyObject *w;
4338 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004340 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004342 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004343 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004344 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004345 v->ob_type->tp_iter == NULL && !PySequence_Check(v))
4346 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004347 _PyErr_Format(tstate, PyExc_TypeError,
4348 "cannot unpack non-iterable %.200s object",
4349 v->ob_type->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004350 }
4351 return 0;
4352 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004354 for (; i < argcnt; i++) {
4355 w = PyIter_Next(it);
4356 if (w == NULL) {
4357 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004358 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04004359 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004360 _PyErr_Format(tstate, PyExc_ValueError,
4361 "not enough values to unpack "
4362 "(expected %d, got %d)",
4363 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004364 }
4365 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004366 _PyErr_Format(tstate, PyExc_ValueError,
4367 "not enough values to unpack "
4368 "(expected at least %d, got %d)",
4369 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004370 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004371 }
4372 goto Error;
4373 }
4374 *--sp = w;
4375 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004377 if (argcntafter == -1) {
4378 /* We better have exhausted the iterator now. */
4379 w = PyIter_Next(it);
4380 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004381 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004382 goto Error;
4383 Py_DECREF(it);
4384 return 1;
4385 }
4386 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02004387 _PyErr_Format(tstate, PyExc_ValueError,
4388 "too many values to unpack (expected %d)",
4389 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004390 goto Error;
4391 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004393 l = PySequence_List(it);
4394 if (l == NULL)
4395 goto Error;
4396 *--sp = l;
4397 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004399 ll = PyList_GET_SIZE(l);
4400 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004401 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04004402 "not enough values to unpack (expected at least %d, got %zd)",
4403 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004404 goto Error;
4405 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004407 /* Pop the "after-variable" args off the list. */
4408 for (j = argcntafter; j > 0; j--, i++) {
4409 *--sp = PyList_GET_ITEM(l, ll - j);
4410 }
4411 /* Resize the list. */
4412 Py_SIZE(l) = ll - argcntafter;
4413 Py_DECREF(it);
4414 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004415
Tim Petersd6d010b2001-06-21 02:49:55 +00004416Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004417 for (; i > 0; i--, sp++)
4418 Py_DECREF(*sp);
4419 Py_XDECREF(it);
4420 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004421}
4422
4423
Guido van Rossum96a42c81992-01-12 02:29:51 +00004424#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004425static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004426prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004427{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004428 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02004429 if (PyObject_Print(v, stdout, 0) != 0) {
4430 /* Don't know what else to do */
4431 _PyErr_Clear(tstate);
4432 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004433 printf("\n");
4434 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004435}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004436#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004437
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004438static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004439call_exc_trace(Py_tracefunc func, PyObject *self,
4440 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004441{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004442 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004443 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004444 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004445 if (value == NULL) {
4446 value = Py_None;
4447 Py_INCREF(value);
4448 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004449 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01004450 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004451 arg = PyTuple_Pack(3, type, value, traceback);
4452 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004453 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004454 return;
4455 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004456 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004457 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02004458 if (err == 0) {
4459 _PyErr_Restore(tstate, type, value, orig_traceback);
4460 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004461 else {
4462 Py_XDECREF(type);
4463 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004464 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004465 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004466}
4467
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004468static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004469call_trace_protected(Py_tracefunc func, PyObject *obj,
4470 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004471 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004472{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004473 PyObject *type, *value, *traceback;
4474 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004475 _PyErr_Fetch(tstate, &type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004476 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004477 if (err == 0)
4478 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004479 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004480 return 0;
4481 }
4482 else {
4483 Py_XDECREF(type);
4484 Py_XDECREF(value);
4485 Py_XDECREF(traceback);
4486 return -1;
4487 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004488}
4489
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004490static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004491call_trace(Py_tracefunc func, PyObject *obj,
4492 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004493 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004494{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004495 int result;
4496 if (tstate->tracing)
4497 return 0;
4498 tstate->tracing++;
4499 tstate->use_tracing = 0;
4500 result = func(obj, frame, what, arg);
4501 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4502 || (tstate->c_profilefunc != NULL));
4503 tstate->tracing--;
4504 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004505}
4506
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004507PyObject *
4508_PyEval_CallTracing(PyObject *func, PyObject *args)
4509{
Victor Stinner50b48572018-11-01 01:51:40 +01004510 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004511 int save_tracing = tstate->tracing;
4512 int save_use_tracing = tstate->use_tracing;
4513 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004515 tstate->tracing = 0;
4516 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4517 || (tstate->c_profilefunc != NULL));
4518 result = PyObject_Call(func, args, NULL);
4519 tstate->tracing = save_tracing;
4520 tstate->use_tracing = save_use_tracing;
4521 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004522}
4523
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004524/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004525static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004526maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004527 PyThreadState *tstate, PyFrameObject *frame,
4528 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004529{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004530 int result = 0;
4531 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004533 /* If the last instruction executed isn't in the current
4534 instruction window, reset the window.
4535 */
4536 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4537 PyAddrPair bounds;
4538 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4539 &bounds);
4540 *instr_lb = bounds.ap_lower;
4541 *instr_ub = bounds.ap_upper;
4542 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004543 /* If the last instruction falls at the start of a line or if it
4544 represents a jump backwards, update the frame's line number and
4545 then call the trace function if we're tracing source lines.
4546 */
4547 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004548 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004549 if (frame->f_trace_lines) {
4550 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4551 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004552 }
George King20faa682017-10-18 17:44:22 -07004553 /* Always emit an opcode event if we're tracing all opcodes. */
4554 if (frame->f_trace_opcodes) {
4555 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4556 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004557 *instr_prev = frame->f_lasti;
4558 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004559}
4560
Fred Drake5755ce62001-06-27 19:19:46 +00004561void
4562PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004563{
Steve Dowerb82e17e2019-05-23 08:45:22 -07004564 if (PySys_Audit("sys.setprofile", NULL) < 0) {
4565 return;
4566 }
4567
Victor Stinner50b48572018-11-01 01:51:40 +01004568 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004569 PyObject *temp = tstate->c_profileobj;
4570 Py_XINCREF(arg);
4571 tstate->c_profilefunc = NULL;
4572 tstate->c_profileobj = NULL;
4573 /* Must make sure that tracing is not ignored if 'temp' is freed */
4574 tstate->use_tracing = tstate->c_tracefunc != NULL;
4575 Py_XDECREF(temp);
4576 tstate->c_profilefunc = func;
4577 tstate->c_profileobj = arg;
4578 /* Flag that tracing or profiling is turned on */
4579 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004580}
4581
4582void
4583PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4584{
Steve Dowerb82e17e2019-05-23 08:45:22 -07004585 if (PySys_Audit("sys.settrace", NULL) < 0) {
4586 return;
4587 }
4588
Victor Stinner09532fe2019-05-10 23:39:09 +02004589 _PyRuntimeState *runtime = &_PyRuntime;
4590 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004591 PyObject *temp = tstate->c_traceobj;
Victor Stinner09532fe2019-05-10 23:39:09 +02004592 runtime->ceval.tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004593 Py_XINCREF(arg);
4594 tstate->c_tracefunc = NULL;
4595 tstate->c_traceobj = NULL;
4596 /* Must make sure that profiling is not ignored if 'temp' is freed */
4597 tstate->use_tracing = tstate->c_profilefunc != NULL;
4598 Py_XDECREF(temp);
4599 tstate->c_tracefunc = func;
4600 tstate->c_traceobj = arg;
4601 /* Flag that tracing or profiling is turned on */
4602 tstate->use_tracing = ((func != NULL)
4603 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004604}
4605
Yury Selivanov75445082015-05-11 22:57:16 -04004606void
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004607_PyEval_SetCoroutineOriginTrackingDepth(int new_depth)
4608{
4609 assert(new_depth >= 0);
Victor Stinner50b48572018-11-01 01:51:40 +01004610 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004611 tstate->coroutine_origin_tracking_depth = new_depth;
4612}
4613
4614int
4615_PyEval_GetCoroutineOriginTrackingDepth(void)
4616{
Victor Stinner50b48572018-11-01 01:51:40 +01004617 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004618 return tstate->coroutine_origin_tracking_depth;
4619}
4620
4621void
Yury Selivanoveb636452016-09-08 22:01:51 -07004622_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4623{
Victor Stinner50b48572018-11-01 01:51:40 +01004624 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004625
4626 if (PySys_Audit("sys.set_asyncgen_hook_firstiter", NULL) < 0) {
4627 return;
4628 }
4629
Yury Selivanoveb636452016-09-08 22:01:51 -07004630 Py_XINCREF(firstiter);
4631 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4632}
4633
4634PyObject *
4635_PyEval_GetAsyncGenFirstiter(void)
4636{
Victor Stinner50b48572018-11-01 01:51:40 +01004637 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004638 return tstate->async_gen_firstiter;
4639}
4640
4641void
4642_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4643{
Victor Stinner50b48572018-11-01 01:51:40 +01004644 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004645
4646 if (PySys_Audit("sys.set_asyncgen_hook_finalizer", NULL) < 0) {
4647 return;
4648 }
4649
Yury Selivanoveb636452016-09-08 22:01:51 -07004650 Py_XINCREF(finalizer);
4651 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4652}
4653
4654PyObject *
4655_PyEval_GetAsyncGenFinalizer(void)
4656{
Victor Stinner50b48572018-11-01 01:51:40 +01004657 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004658 return tstate->async_gen_finalizer;
4659}
4660
Victor Stinner438a12d2019-05-24 17:01:38 +02004661static PyFrameObject *
4662_PyEval_GetFrame(PyThreadState *tstate)
4663{
4664 return _PyRuntime.gilstate.getframe(tstate);
4665}
4666
4667PyFrameObject *
4668PyEval_GetFrame(void)
4669{
4670 PyThreadState *tstate = _PyThreadState_GET();
4671 return _PyEval_GetFrame(tstate);
4672}
4673
Guido van Rossumb209a111997-04-29 18:18:01 +00004674PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004675PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004676{
Victor Stinner438a12d2019-05-24 17:01:38 +02004677 PyThreadState *tstate = _PyThreadState_GET();
4678 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004679 if (current_frame == NULL)
Victor Stinner438a12d2019-05-24 17:01:38 +02004680 return tstate->interp->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004681 else
4682 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004683}
4684
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004685/* Convenience function to get a builtin from its name */
4686PyObject *
4687_PyEval_GetBuiltinId(_Py_Identifier *name)
4688{
Victor Stinner438a12d2019-05-24 17:01:38 +02004689 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004690 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4691 if (attr) {
4692 Py_INCREF(attr);
4693 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004694 else if (!_PyErr_Occurred(tstate)) {
4695 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004696 }
4697 return attr;
4698}
4699
Guido van Rossumb209a111997-04-29 18:18:01 +00004700PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004701PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004702{
Victor Stinner438a12d2019-05-24 17:01:38 +02004703 PyThreadState *tstate = _PyThreadState_GET();
4704 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Victor Stinner41bb43a2013-10-29 01:19:37 +01004705 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004706 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004707 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004708 }
4709
Victor Stinner438a12d2019-05-24 17:01:38 +02004710 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01004711 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004712 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004713
4714 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004715 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004716}
4717
Guido van Rossumb209a111997-04-29 18:18:01 +00004718PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004719PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004720{
Victor Stinner438a12d2019-05-24 17:01:38 +02004721 PyThreadState *tstate = _PyThreadState_GET();
4722 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
4723 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004724 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004725 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004726
4727 assert(current_frame->f_globals != NULL);
4728 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004729}
4730
Guido van Rossum6135a871995-01-09 17:53:26 +00004731int
Tim Peters5ba58662001-07-16 02:29:45 +00004732PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004733{
Victor Stinner438a12d2019-05-24 17:01:38 +02004734 PyThreadState *tstate = _PyThreadState_GET();
4735 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004736 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004737
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004738 if (current_frame != NULL) {
4739 const int codeflags = current_frame->f_code->co_flags;
4740 const int compilerflags = codeflags & PyCF_MASK;
4741 if (compilerflags) {
4742 result = 1;
4743 cf->cf_flags |= compilerflags;
4744 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004745#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004746 if (codeflags & CO_GENERATOR_ALLOWED) {
4747 result = 1;
4748 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4749 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004750#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004751 }
4752 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004753}
4754
Guido van Rossum3f5da241990-12-20 15:06:42 +00004755
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004756const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004757PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004758{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004759 if (PyMethod_Check(func))
4760 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4761 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004762 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004763 else if (PyCFunction_Check(func))
4764 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4765 else
4766 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004767}
4768
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004769const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004770PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004771{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004772 if (PyMethod_Check(func))
4773 return "()";
4774 else if (PyFunction_Check(func))
4775 return "()";
4776 else if (PyCFunction_Check(func))
4777 return "()";
4778 else
4779 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004780}
4781
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004782#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004783if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004784 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4785 tstate, tstate->frame, \
4786 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004787 x = NULL; \
4788 } \
4789 else { \
4790 x = call; \
4791 if (tstate->c_profilefunc != NULL) { \
4792 if (x == NULL) { \
4793 call_trace_protected(tstate->c_profilefunc, \
4794 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004795 tstate, tstate->frame, \
4796 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004797 /* XXX should pass (type, value, tb) */ \
4798 } else { \
4799 if (call_trace(tstate->c_profilefunc, \
4800 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004801 tstate, tstate->frame, \
4802 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004803 Py_DECREF(x); \
4804 x = NULL; \
4805 } \
4806 } \
4807 } \
4808 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004809} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004810 x = call; \
4811 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004812
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004813
4814static PyObject *
4815trace_call_function(PyThreadState *tstate,
4816 PyObject *func,
4817 PyObject **args, Py_ssize_t nargs,
4818 PyObject *kwnames)
4819{
4820 PyObject *x;
4821 if (PyCFunction_Check(func)) {
Jeroen Demeyer37788bc2019-05-30 15:11:22 +02004822 C_TRACE(x, _PyCFunction_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004823 return x;
4824 }
4825 else if (Py_TYPE(func) == &PyMethodDescr_Type && nargs > 0) {
4826 /* We need to create a temporary bound method as argument
4827 for profiling.
4828
4829 If nargs == 0, then this cannot work because we have no
4830 "self". In any case, the call itself would raise
4831 TypeError (foo needs an argument), so we just skip
4832 profiling. */
4833 PyObject *self = args[0];
4834 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4835 if (func == NULL) {
4836 return NULL;
4837 }
Jeroen Demeyer37788bc2019-05-30 15:11:22 +02004838 C_TRACE(x, _PyCFunction_Vectorcall(func,
4839 args+1, nargs-1,
4840 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004841 Py_DECREF(func);
4842 return x;
4843 }
4844 return _PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
4845}
4846
Victor Stinner415c5102017-01-11 00:54:57 +01004847/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4848 to reduce the stack consumption. */
4849Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Victor Stinner09532fe2019-05-10 23:39:09 +02004850call_function(PyThreadState *tstate, PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004851{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004852 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004853 PyObject *func = *pfunc;
4854 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004855 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4856 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004857 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004858
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004859 if (tstate->use_tracing) {
4860 x = trace_call_function(tstate, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09004861 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004862 else {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004863 x = _PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004864 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004865
Victor Stinner438a12d2019-05-24 17:01:38 +02004866 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004867
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004868 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004869 while ((*pp_stack) > pfunc) {
4870 w = EXT_POP(*pp_stack);
4871 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004872 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004873
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004874 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004875}
4876
Jeremy Hylton52820442001-01-03 23:52:36 +00004877static PyObject *
Victor Stinner09532fe2019-05-10 23:39:09 +02004878do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004879{
jdemeyere89de732018-09-19 12:06:20 +02004880 PyObject *result;
4881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004882 if (PyCFunction_Check(func)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004883 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004884 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004885 }
jdemeyere89de732018-09-19 12:06:20 +02004886 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
jdemeyere89de732018-09-19 12:06:20 +02004887 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
4888 if (nargs > 0 && tstate->use_tracing) {
4889 /* We need to create a temporary bound method as argument
4890 for profiling.
4891
4892 If nargs == 0, then this cannot work because we have no
4893 "self". In any case, the call itself would raise
4894 TypeError (foo needs an argument), so we just skip
4895 profiling. */
4896 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
4897 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4898 if (func == NULL) {
4899 return NULL;
4900 }
4901
4902 C_TRACE(result, _PyCFunction_FastCallDict(func,
Victor Stinnerd17a6932018-11-09 16:56:48 +01004903 &_PyTuple_ITEMS(callargs)[1],
jdemeyere89de732018-09-19 12:06:20 +02004904 nargs - 1,
4905 kwdict));
4906 Py_DECREF(func);
4907 return result;
4908 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004909 }
jdemeyere89de732018-09-19 12:06:20 +02004910 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00004911}
4912
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004913/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004914 nb_index slot defined, and store in *pi.
4915 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004916 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004917 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004918*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004919int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004920_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004921{
Victor Stinner438a12d2019-05-24 17:01:38 +02004922 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004923 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004924 Py_ssize_t x;
4925 if (PyIndex_Check(v)) {
4926 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02004927 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004928 return 0;
4929 }
4930 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004931 _PyErr_SetString(tstate, PyExc_TypeError,
4932 "slice indices must be integers or "
4933 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004934 return 0;
4935 }
4936 *pi = x;
4937 }
4938 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004939}
4940
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004941int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004942_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004943{
Victor Stinner438a12d2019-05-24 17:01:38 +02004944 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004945 Py_ssize_t x;
4946 if (PyIndex_Check(v)) {
4947 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02004948 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004949 return 0;
4950 }
4951 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004952 _PyErr_SetString(tstate, PyExc_TypeError,
4953 "slice indices must be integers or "
4954 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004955 return 0;
4956 }
4957 *pi = x;
4958 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004959}
4960
4961
Guido van Rossum486364b2007-06-30 05:01:58 +00004962#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004963 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004964
Guido van Rossumb209a111997-04-29 18:18:01 +00004965static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004966cmp_outcome(PyThreadState *tstate, int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004967{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004968 int res = 0;
4969 switch (op) {
4970 case PyCmp_IS:
4971 res = (v == w);
4972 break;
4973 case PyCmp_IS_NOT:
4974 res = (v != w);
4975 break;
4976 case PyCmp_IN:
4977 res = PySequence_Contains(w, v);
4978 if (res < 0)
4979 return NULL;
4980 break;
4981 case PyCmp_NOT_IN:
4982 res = PySequence_Contains(w, v);
4983 if (res < 0)
4984 return NULL;
4985 res = !res;
4986 break;
4987 case PyCmp_EXC_MATCH:
4988 if (PyTuple_Check(w)) {
4989 Py_ssize_t i, length;
4990 length = PyTuple_Size(w);
4991 for (i = 0; i < length; i += 1) {
4992 PyObject *exc = PyTuple_GET_ITEM(w, i);
4993 if (!PyExceptionClass_Check(exc)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004994 _PyErr_SetString(tstate, PyExc_TypeError,
4995 CANNOT_CATCH_MSG);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004996 return NULL;
4997 }
4998 }
4999 }
5000 else {
5001 if (!PyExceptionClass_Check(w)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005002 _PyErr_SetString(tstate, PyExc_TypeError,
5003 CANNOT_CATCH_MSG);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005004 return NULL;
5005 }
5006 }
5007 res = PyErr_GivenExceptionMatches(v, w);
5008 break;
5009 default:
5010 return PyObject_RichCompare(v, w, op);
5011 }
5012 v = res ? Py_True : Py_False;
5013 Py_INCREF(v);
5014 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005015}
5016
Thomas Wouters52152252000-08-17 22:55:00 +00005017static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005018import_name(PyThreadState *tstate, PyFrameObject *f,
5019 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005020{
5021 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005022 PyObject *import_func, *res;
5023 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005024
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005025 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005026 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005027 if (!_PyErr_Occurred(tstate)) {
5028 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005029 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005030 return NULL;
5031 }
5032
5033 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005034 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005035 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02005036 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005037 return NULL;
5038 }
5039 res = PyImport_ImportModuleLevelObject(
5040 name,
5041 f->f_globals,
5042 f->f_locals == NULL ? Py_None : f->f_locals,
5043 fromlist,
5044 ilevel);
5045 return res;
5046 }
5047
5048 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005049
5050 stack[0] = name;
5051 stack[1] = f->f_globals;
5052 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5053 stack[3] = fromlist;
5054 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005055 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005056 Py_DECREF(import_func);
5057 return res;
5058}
5059
5060static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005061import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005062{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005063 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02005064 _Py_IDENTIFIER(__name__);
Xiang Zhang4830f582017-03-21 11:13:42 +08005065 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005066
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005067 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005068 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005069 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005070 /* Issue #17636: in case this failed because of a circular relative
5071 import, try to fallback on reading the module directly from
5072 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02005073 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005074 if (pkgname == NULL) {
5075 goto error;
5076 }
Oren Milman6db70332017-09-19 14:23:01 +03005077 if (!PyUnicode_Check(pkgname)) {
5078 Py_CLEAR(pkgname);
5079 goto error;
5080 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005081 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005082 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005083 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005084 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005085 }
Eric Snow3f9eee62017-09-15 16:35:20 -06005086 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005087 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02005088 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07005089 goto error;
5090 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005091 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005092 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005093 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005094 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005095 if (pkgname == NULL) {
5096 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5097 if (pkgname_or_unknown == NULL) {
5098 Py_XDECREF(pkgpath);
5099 return NULL;
5100 }
5101 } else {
5102 pkgname_or_unknown = pkgname;
5103 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005104
5105 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005106 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08005107 errmsg = PyUnicode_FromFormat(
5108 "cannot import name %R from %R (unknown location)",
5109 name, pkgname_or_unknown
5110 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005111 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005112 PyErr_SetImportError(errmsg, pkgname, NULL);
5113 }
5114 else {
5115 errmsg = PyUnicode_FromFormat(
5116 "cannot import name %R from %R (%S)",
5117 name, pkgname_or_unknown, pkgpath
5118 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005119 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005120 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005121 }
5122
Xiang Zhang4830f582017-03-21 11:13:42 +08005123 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005124 Py_XDECREF(pkgname_or_unknown);
5125 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005126 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005127}
Guido van Rossumac7be682001-01-17 15:42:30 +00005128
Thomas Wouters52152252000-08-17 22:55:00 +00005129static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005130import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00005131{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005132 _Py_IDENTIFIER(__all__);
5133 _Py_IDENTIFIER(__dict__);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005134 _Py_IDENTIFIER(__name__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005135 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005136 int skip_leading_underscores = 0;
5137 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005138
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005139 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5140 return -1; /* Unexpected error */
5141 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005142 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005143 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5144 return -1;
5145 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005146 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005147 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005148 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005149 return -1;
5150 }
5151 all = PyMapping_Keys(dict);
5152 Py_DECREF(dict);
5153 if (all == NULL)
5154 return -1;
5155 skip_leading_underscores = 1;
5156 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005157
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005158 for (pos = 0, err = 0; ; pos++) {
5159 name = PySequence_GetItem(all, pos);
5160 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005161 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005162 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02005163 }
5164 else {
5165 _PyErr_Clear(tstate);
5166 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005167 break;
5168 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005169 if (!PyUnicode_Check(name)) {
5170 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5171 if (modname == NULL) {
5172 Py_DECREF(name);
5173 err = -1;
5174 break;
5175 }
5176 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005177 _PyErr_Format(tstate, PyExc_TypeError,
5178 "module __name__ must be a string, not %.100s",
5179 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005180 }
5181 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005182 _PyErr_Format(tstate, PyExc_TypeError,
5183 "%s in %U.%s must be str, not %.100s",
5184 skip_leading_underscores ? "Key" : "Item",
5185 modname,
5186 skip_leading_underscores ? "__dict__" : "__all__",
5187 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005188 }
5189 Py_DECREF(modname);
5190 Py_DECREF(name);
5191 err = -1;
5192 break;
5193 }
5194 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005195 if (PyUnicode_READY(name) == -1) {
5196 Py_DECREF(name);
5197 err = -1;
5198 break;
5199 }
5200 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5201 Py_DECREF(name);
5202 continue;
5203 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005204 }
5205 value = PyObject_GetAttr(v, name);
5206 if (value == NULL)
5207 err = -1;
5208 else if (PyDict_CheckExact(locals))
5209 err = PyDict_SetItem(locals, name, value);
5210 else
5211 err = PyObject_SetItem(locals, name, value);
5212 Py_DECREF(name);
5213 Py_XDECREF(value);
5214 if (err != 0)
5215 break;
5216 }
5217 Py_DECREF(all);
5218 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005219}
5220
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005221static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005222check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005223{
5224 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005225 _PyErr_Format(tstate, PyExc_TypeError,
5226 "%.200s%.200s argument after * "
5227 "must be an iterable, not %.200s",
5228 PyEval_GetFuncName(func),
5229 PyEval_GetFuncDesc(func),
5230 args->ob_type->tp_name);
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005231 return -1;
5232 }
5233 return 0;
5234}
5235
5236static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005237format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005238{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005239 /* _PyDict_MergeEx raises attribute
5240 * error (percolated from an attempt
5241 * to get 'keys' attribute) instead of
5242 * a type error if its second argument
5243 * is not a mapping.
5244 */
Victor Stinner438a12d2019-05-24 17:01:38 +02005245 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
5246 _PyErr_Format(tstate, PyExc_TypeError,
5247 "%.200s%.200s argument after ** "
5248 "must be a mapping, not %.200s",
5249 PyEval_GetFuncName(func),
5250 PyEval_GetFuncDesc(func),
5251 kwargs->ob_type->tp_name);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005252 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005253 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005254 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02005255 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005256 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
5257 PyObject *key = PyTuple_GET_ITEM(val, 0);
5258 if (!PyUnicode_Check(key)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005259 _PyErr_Format(tstate, PyExc_TypeError,
5260 "%.200s%.200s keywords must be strings",
5261 PyEval_GetFuncName(func),
5262 PyEval_GetFuncDesc(func));
5263 }
5264 else {
5265 _PyErr_Format(tstate, PyExc_TypeError,
5266 "%.200s%.200s got multiple "
5267 "values for keyword argument '%U'",
5268 PyEval_GetFuncName(func),
5269 PyEval_GetFuncDesc(func),
5270 key);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005271 }
5272 Py_XDECREF(exc);
5273 Py_XDECREF(val);
5274 Py_XDECREF(tb);
5275 }
5276 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005277 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005278 }
5279 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005280}
5281
Guido van Rossumac7be682001-01-17 15:42:30 +00005282static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005283format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
5284 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005285{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005286 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005288 if (!obj)
5289 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005290
Serhiy Storchaka06515832016-11-20 09:13:07 +02005291 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005292 if (!obj_str)
5293 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005294
Victor Stinner438a12d2019-05-24 17:01:38 +02005295 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005296}
Guido van Rossum950361c1997-01-24 13:49:28 +00005297
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005298static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005299format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005300{
5301 PyObject *name;
5302 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02005303 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005304 return;
5305 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5306 name = PyTuple_GET_ITEM(co->co_cellvars,
5307 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005308 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005309 PyExc_UnboundLocalError,
5310 UNBOUNDLOCAL_ERROR_MSG,
5311 name);
5312 } else {
5313 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5314 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02005315 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005316 UNBOUNDFREE_ERROR_MSG, name);
5317 }
5318}
5319
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005320static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005321format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005322{
5323 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5324 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005325 _PyErr_Format(tstate, PyExc_TypeError,
5326 "'async with' received an object from __aenter__ "
5327 "that does not implement __await__: %.100s",
5328 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005329 }
5330 else if (prevopcode == WITH_CLEANUP_START) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005331 _PyErr_Format(tstate, PyExc_TypeError,
5332 "'async with' received an object from __aexit__ "
5333 "that does not implement __await__: %.100s",
5334 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005335 }
5336 }
5337}
5338
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005339static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005340unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005341 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005342{
5343 PyObject *res;
5344 if (Py_REFCNT(v) == 2) {
5345 /* In the common case, there are 2 references to the value
5346 * stored in 'variable' when the += is performed: one on the
5347 * value stack (in 'v') and one still stored in the
5348 * 'variable'. We try to delete the variable now to reduce
5349 * the refcnt to 1.
5350 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005351 int opcode, oparg;
5352 NEXTOPARG();
5353 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005354 case STORE_FAST:
5355 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005356 PyObject **fastlocals = f->f_localsplus;
5357 if (GETLOCAL(oparg) == v)
5358 SETLOCAL(oparg, NULL);
5359 break;
5360 }
5361 case STORE_DEREF:
5362 {
5363 PyObject **freevars = (f->f_localsplus +
5364 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005365 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005366 if (PyCell_GET(c) == v) {
5367 PyCell_SET(c, NULL);
5368 Py_DECREF(v);
5369 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005370 break;
5371 }
5372 case STORE_NAME:
5373 {
5374 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005375 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005376 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005377 if (locals && PyDict_CheckExact(locals)) {
5378 PyObject *w = PyDict_GetItemWithError(locals, name);
5379 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02005380 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005381 {
5382 Py_DECREF(v);
5383 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005384 }
5385 }
5386 break;
5387 }
5388 }
5389 }
5390 res = v;
5391 PyUnicode_Append(&res, w);
5392 return res;
5393}
5394
Guido van Rossum950361c1997-01-24 13:49:28 +00005395#ifdef DYNAMIC_EXECUTION_PROFILE
5396
Skip Montanarof118cb12001-10-15 20:51:38 +00005397static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005398getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005399{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005400 int i;
5401 PyObject *l = PyList_New(256);
5402 if (l == NULL) return NULL;
5403 for (i = 0; i < 256; i++) {
5404 PyObject *x = PyLong_FromLong(a[i]);
5405 if (x == NULL) {
5406 Py_DECREF(l);
5407 return NULL;
5408 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005409 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005410 }
5411 for (i = 0; i < 256; i++)
5412 a[i] = 0;
5413 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005414}
5415
5416PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005417_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005418{
5419#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005420 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005421#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005422 int i;
5423 PyObject *l = PyList_New(257);
5424 if (l == NULL) return NULL;
5425 for (i = 0; i < 257; i++) {
5426 PyObject *x = getarray(dxpairs[i]);
5427 if (x == NULL) {
5428 Py_DECREF(l);
5429 return NULL;
5430 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005431 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005432 }
5433 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005434#endif
5435}
5436
5437#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005438
5439Py_ssize_t
5440_PyEval_RequestCodeExtraIndex(freefunc free)
5441{
Victor Stinnercaba55b2018-08-03 15:33:52 +02005442 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -07005443 Py_ssize_t new_index;
5444
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005445 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005446 return -1;
5447 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005448 new_index = interp->co_extra_user_count++;
5449 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005450 return new_index;
5451}
Łukasz Langaa785c872016-09-09 17:37:37 -07005452
5453static void
5454dtrace_function_entry(PyFrameObject *f)
5455{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005456 const char *filename;
5457 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005458 int lineno;
5459
5460 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5461 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5462 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5463
5464 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5465}
5466
5467static void
5468dtrace_function_return(PyFrameObject *f)
5469{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005470 const char *filename;
5471 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005472 int lineno;
5473
5474 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5475 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5476 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5477
5478 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5479}
5480
5481/* DTrace equivalent of maybe_call_line_trace. */
5482static void
5483maybe_dtrace_line(PyFrameObject *frame,
5484 int *instr_lb, int *instr_ub, int *instr_prev)
5485{
5486 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005487 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005488
5489 /* If the last instruction executed isn't in the current
5490 instruction window, reset the window.
5491 */
5492 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5493 PyAddrPair bounds;
5494 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5495 &bounds);
5496 *instr_lb = bounds.ap_lower;
5497 *instr_ub = bounds.ap_upper;
5498 }
5499 /* If the last instruction falls at the start of a line or if
5500 it represents a jump backwards, update the frame's line
5501 number and call the trace function. */
5502 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5503 frame->f_lineno = line;
5504 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5505 if (!co_filename)
5506 co_filename = "?";
5507 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5508 if (!co_name)
5509 co_name = "?";
5510 PyDTrace_LINE(co_filename, co_name, line);
5511 }
5512 *instr_prev = frame->f_lasti;
5513}