blob: d9a71e942153ee533168c9f52310718ca3c6bcb9 [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 Galindocd74e662019-06-01 18:08:04 +01003760 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003761 }
3762 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003763 start = 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;
Victor Stinner74319ae2016-08-25 00:04:09 +02003791 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003792
Benjamin Petersone109c702011-06-24 09:37:26 -05003793 assert((co->co_flags & CO_VARARGS) == 0);
3794 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01003795 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003796 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003797 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003798 }
3799 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003800 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01003801 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003802 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01003803 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003804 }
3805 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01003806 plural = (co_argcount != 1);
3807 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003808 }
3809 if (sig == NULL)
3810 return;
3811 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003812 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3813 kwonly_sig = PyUnicode_FromFormat(format,
3814 given != 1 ? "s" : "",
3815 kwonly_given,
3816 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003817 if (kwonly_sig == NULL) {
3818 Py_DECREF(sig);
3819 return;
3820 }
3821 }
3822 else {
3823 /* This will not fail. */
3824 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003825 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003826 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003827 _PyErr_Format(tstate, PyExc_TypeError,
3828 "%U() takes %U positional argument%s but %zd%U %s given",
3829 co->co_name,
3830 sig,
3831 plural ? "s" : "",
3832 given,
3833 kwonly_sig,
3834 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003835 Py_DECREF(sig);
3836 Py_DECREF(kwonly_sig);
3837}
3838
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003839static int
Victor Stinner438a12d2019-05-24 17:01:38 +02003840positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
3841 Py_ssize_t kwcount, PyObject* const* kwnames)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003842{
3843 int posonly_conflicts = 0;
3844 PyObject* posonly_names = PyList_New(0);
3845
3846 for(int k=0; k < co->co_posonlyargcount; k++){
3847 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
3848
3849 for (int k2=0; k2<kwcount; k2++){
3850 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
3851 PyObject* kwname = kwnames[k2];
3852 if (kwname == posonly_name){
3853 if(PyList_Append(posonly_names, kwname) != 0) {
3854 goto fail;
3855 }
3856 posonly_conflicts++;
3857 continue;
3858 }
3859
3860 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
3861
3862 if ( cmp > 0) {
3863 if(PyList_Append(posonly_names, kwname) != 0) {
3864 goto fail;
3865 }
3866 posonly_conflicts++;
3867 } else if (cmp < 0) {
3868 goto fail;
3869 }
3870
3871 }
3872 }
3873 if (posonly_conflicts) {
3874 PyObject* comma = PyUnicode_FromString(", ");
3875 if (comma == NULL) {
3876 goto fail;
3877 }
3878 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
3879 Py_DECREF(comma);
3880 if (error_names == NULL) {
3881 goto fail;
3882 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003883 _PyErr_Format(tstate, PyExc_TypeError,
3884 "%U() got some positional-only arguments passed"
3885 " as keyword arguments: '%U'",
3886 co->co_name, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003887 Py_DECREF(error_names);
3888 goto fail;
3889 }
3890
3891 Py_DECREF(posonly_names);
3892 return 0;
3893
3894fail:
3895 Py_XDECREF(posonly_names);
3896 return 1;
3897
3898}
3899
Guido van Rossumc2e20742006-02-27 22:32:47 +00003900/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02003901 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003902 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003903
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003904PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +02003905_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003906 PyObject *const *args, Py_ssize_t argcount,
3907 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003908 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003909 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003910 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003911 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003912{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003913 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003914 PyFrameObject *f;
3915 PyObject *retval = NULL;
3916 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003917 PyObject *x, *u;
Pablo Galindocd74e662019-06-01 18:08:04 +01003918 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003919 Py_ssize_t i, j, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003920 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003921
Victor Stinner438a12d2019-05-24 17:01:38 +02003922 PyThreadState *tstate = _PyThreadState_GET();
3923 assert(tstate != NULL);
3924
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003925 if (globals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003926 _PyErr_SetString(tstate, PyExc_SystemError,
3927 "PyEval_EvalCodeEx: NULL globals");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003928 return NULL;
3929 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003930
Victor Stinnerc7020012016-08-16 23:40:29 +02003931 /* Create the frame */
INADA Naoki5a625d02016-12-24 20:19:08 +09003932 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003933 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003934 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003935 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003936 fastlocals = f->f_localsplus;
3937 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003938
Victor Stinnerc7020012016-08-16 23:40:29 +02003939 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003940 if (co->co_flags & CO_VARKEYWORDS) {
3941 kwdict = PyDict_New();
3942 if (kwdict == NULL)
3943 goto fail;
3944 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003945 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003946 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003947 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003948 SETLOCAL(i, kwdict);
3949 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003950 else {
3951 kwdict = NULL;
3952 }
3953
Pablo Galindocd74e662019-06-01 18:08:04 +01003954 /* Copy all positional arguments into local variables */
3955 if (argcount > co->co_argcount) {
3956 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003957 }
3958 else {
3959 n = argcount;
3960 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003961 for (j = 0; j < n; j++) {
3962 x = args[j];
3963 Py_INCREF(x);
3964 SETLOCAL(j, x);
3965 }
3966
Victor Stinnerc7020012016-08-16 23:40:29 +02003967 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003968 if (co->co_flags & CO_VARARGS) {
Sergey Fedoseev234531b2019-02-25 21:59:12 +05003969 u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003970 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003971 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003972 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003973 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003974 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003975
Serhiy Storchakab7281052016-09-12 00:52:40 +03003976 /* Handle keyword arguments passed as two strided arrays */
3977 kwcount *= kwstep;
3978 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003979 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003980 PyObject *keyword = kwnames[i];
3981 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003982 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003983
Benjamin Petersonb204a422011-06-05 22:04:07 -05003984 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003985 _PyErr_Format(tstate, PyExc_TypeError,
3986 "%U() keywords must be strings",
3987 co->co_name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003988 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003989 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003990
Benjamin Petersonb204a422011-06-05 22:04:07 -05003991 /* Speed hack: do raw pointer compares. As names are
3992 normally interned this should almost always hit. */
3993 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003994 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003995 PyObject *name = co_varnames[j];
3996 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003997 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003998 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003999 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004000
Benjamin Petersonb204a422011-06-05 22:04:07 -05004001 /* Slow fallback, just in case */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004002 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004003 PyObject *name = co_varnames[j];
4004 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
4005 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004006 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004007 }
4008 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004009 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004010 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004011 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004012
Victor Stinner231d1f32017-01-11 02:12:06 +01004013 assert(j >= total_args);
4014 if (kwdict == NULL) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004015
Victor Stinner438a12d2019-05-24 17:01:38 +02004016 if (co->co_posonlyargcount
4017 && positional_only_passed_as_keyword(tstate, co,
4018 kwcount, kwnames))
4019 {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004020 goto fail;
4021 }
4022
Victor Stinner438a12d2019-05-24 17:01:38 +02004023 _PyErr_Format(tstate, PyExc_TypeError,
4024 "%U() got an unexpected keyword argument '%S'",
4025 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004026 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004027 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004028
Christian Heimes0bd447f2013-07-20 14:48:10 +02004029 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4030 goto fail;
4031 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004032 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004033
Benjamin Petersonb204a422011-06-05 22:04:07 -05004034 kw_found:
4035 if (GETLOCAL(j) != NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004036 _PyErr_Format(tstate, PyExc_TypeError,
4037 "%U() got multiple values for argument '%S'",
4038 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004039 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004040 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004041 Py_INCREF(value);
4042 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004043 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004044
4045 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004046 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004047 too_many_positional(tstate, co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004048 goto fail;
4049 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004050
4051 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004052 if (argcount < co->co_argcount) {
4053 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004054 Py_ssize_t missing = 0;
4055 for (i = argcount; i < m; i++) {
4056 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004057 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004058 }
4059 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004060 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004061 missing_arguments(tstate, co, missing, defcount, fastlocals);
Benjamin Petersone109c702011-06-24 09:37:26 -05004062 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004063 }
4064 if (n > m)
4065 i = n - m;
4066 else
4067 i = 0;
4068 for (; i < defcount; i++) {
4069 if (GETLOCAL(m+i) == NULL) {
4070 PyObject *def = defs[i];
4071 Py_INCREF(def);
4072 SETLOCAL(m+i, def);
4073 }
4074 }
4075 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004076
4077 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004078 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004079 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004080 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004081 PyObject *name;
4082 if (GETLOCAL(i) != NULL)
4083 continue;
4084 name = PyTuple_GET_ITEM(co->co_varnames, i);
4085 if (kwdefs != NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004086 PyObject *def = PyDict_GetItemWithError(kwdefs, name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004087 if (def) {
4088 Py_INCREF(def);
4089 SETLOCAL(i, def);
4090 continue;
4091 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004092 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004093 goto fail;
4094 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004095 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004096 missing++;
4097 }
4098 if (missing) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004099 missing_arguments(tstate, co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004100 goto fail;
4101 }
4102 }
4103
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004104 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004105 vars into frame. */
4106 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004107 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004108 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004109 /* Possibly account for the cell variable being an argument. */
4110 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004111 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004112 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004113 /* Clear the local copy. */
4114 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004115 }
4116 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004117 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004118 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004119 if (c == NULL)
4120 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004121 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004122 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004123
4124 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004125 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4126 PyObject *o = PyTuple_GET_ITEM(closure, i);
4127 Py_INCREF(o);
4128 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004129 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004130
Yury Selivanoveb636452016-09-08 22:01:51 -07004131 /* Handle generator/coroutine/asynchronous generator */
4132 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004133 PyObject *gen;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004134 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004135
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004136 /* Don't need to keep the reference to f_back, it will be set
4137 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004138 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004140 /* Create a new generator that owns the ready to run frame
4141 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004142 if (is_coro) {
4143 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004144 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4145 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004146 } else {
4147 gen = PyGen_NewWithQualName(f, name, qualname);
4148 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004149 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004150 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004151 }
INADA Naoki9c157762016-12-26 18:52:46 +09004152
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004153 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004154
Yury Selivanov75445082015-05-11 22:57:16 -04004155 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004156 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004157
Victor Stinner59a73272016-12-09 18:51:13 +01004158 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004159
Thomas Woutersce272b62007-09-19 21:19:28 +00004160fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004162 /* decref'ing the frame can cause __del__ methods to get invoked,
4163 which can call back into Python. While we're done with the
4164 current Python frame (f), the associated C stack is still in use,
4165 so recursion_depth must be boosted for the duration.
4166 */
4167 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09004168 if (Py_REFCNT(f) > 1) {
4169 Py_DECREF(f);
4170 _PyObject_GC_TRACK(f);
4171 }
4172 else {
4173 ++tstate->recursion_depth;
4174 Py_DECREF(f);
4175 --tstate->recursion_depth;
4176 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004177 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004178}
4179
Victor Stinner40ee3012014-06-16 15:59:28 +02004180PyObject *
4181PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004182 PyObject *const *args, int argcount,
4183 PyObject *const *kws, int kwcount,
4184 PyObject *const *defs, int defcount,
4185 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004186{
4187 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004188 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004189 kws, kws != NULL ? kws + 1 : NULL,
4190 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004191 defs, defcount,
4192 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004193 NULL, NULL);
4194}
Tim Peters5ca576e2001-06-18 22:08:13 +00004195
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004196static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004197special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004198{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004199 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004200 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02004201 if (res == NULL && !_PyErr_Occurred(tstate)) {
4202 _PyErr_SetObject(tstate, PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004203 return NULL;
4204 }
4205 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004206}
4207
4208
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004209/* Logic for the raise statement (too complicated for inlining).
4210 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004211static int
Victor Stinner09532fe2019-05-10 23:39:09 +02004212do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004214 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004216 if (exc == NULL) {
4217 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01004218 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004219 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004220 type = exc_info->exc_type;
4221 value = exc_info->exc_value;
4222 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004223 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004224 _PyErr_SetString(tstate, PyExc_RuntimeError,
4225 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004226 return 0;
4227 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004228 Py_XINCREF(type);
4229 Py_XINCREF(value);
4230 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02004231 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004232 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004233 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004234
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004235 /* We support the following forms of raise:
4236 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004237 raise <instance>
4238 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004240 if (PyExceptionClass_Check(exc)) {
4241 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004242 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004243 if (value == NULL)
4244 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004245 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004246 _PyErr_Format(tstate, PyExc_TypeError,
4247 "calling %R should have returned an instance of "
4248 "BaseException, not %R",
4249 type, Py_TYPE(value));
4250 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004251 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004252 }
4253 else if (PyExceptionInstance_Check(exc)) {
4254 value = exc;
4255 type = PyExceptionInstance_Class(exc);
4256 Py_INCREF(type);
4257 }
4258 else {
4259 /* Not something you can raise. You get an exception
4260 anyway, just not what you specified :-) */
4261 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02004262 _PyErr_SetString(tstate, PyExc_TypeError,
4263 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004264 goto raise_error;
4265 }
Collin Winter828f04a2007-08-31 00:04:24 +00004266
Serhiy Storchakac0191582016-09-27 11:37:10 +03004267 assert(type != NULL);
4268 assert(value != NULL);
4269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004270 if (cause) {
4271 PyObject *fixed_cause;
4272 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004273 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004274 if (fixed_cause == NULL)
4275 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004276 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004277 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004278 else if (PyExceptionInstance_Check(cause)) {
4279 fixed_cause = cause;
4280 }
4281 else if (cause == Py_None) {
4282 Py_DECREF(cause);
4283 fixed_cause = NULL;
4284 }
4285 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004286 _PyErr_SetString(tstate, PyExc_TypeError,
4287 "exception causes must derive from "
4288 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004289 goto raise_error;
4290 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004291 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004292 }
Collin Winter828f04a2007-08-31 00:04:24 +00004293
Victor Stinner438a12d2019-05-24 17:01:38 +02004294 _PyErr_SetObject(tstate, type, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004295 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004296 Py_DECREF(value);
4297 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004298 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004299
4300raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004301 Py_XDECREF(value);
4302 Py_XDECREF(type);
4303 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004304 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004305}
4306
Tim Petersd6d010b2001-06-21 02:49:55 +00004307/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004308 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004309
Guido van Rossum0368b722007-05-11 16:50:42 +00004310 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4311 with a variable target.
4312*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004313
Barry Warsawe42b18f1997-08-25 22:13:04 +00004314static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004315unpack_iterable(PyThreadState *tstate, PyObject *v,
4316 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004317{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004318 int i = 0, j = 0;
4319 Py_ssize_t ll = 0;
4320 PyObject *it; /* iter(v) */
4321 PyObject *w;
4322 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004324 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004326 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004327 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004328 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004329 v->ob_type->tp_iter == NULL && !PySequence_Check(v))
4330 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004331 _PyErr_Format(tstate, PyExc_TypeError,
4332 "cannot unpack non-iterable %.200s object",
4333 v->ob_type->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004334 }
4335 return 0;
4336 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004338 for (; i < argcnt; i++) {
4339 w = PyIter_Next(it);
4340 if (w == NULL) {
4341 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004342 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04004343 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004344 _PyErr_Format(tstate, PyExc_ValueError,
4345 "not enough values to unpack "
4346 "(expected %d, got %d)",
4347 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004348 }
4349 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004350 _PyErr_Format(tstate, PyExc_ValueError,
4351 "not enough values to unpack "
4352 "(expected at least %d, got %d)",
4353 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004354 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004355 }
4356 goto Error;
4357 }
4358 *--sp = w;
4359 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004361 if (argcntafter == -1) {
4362 /* We better have exhausted the iterator now. */
4363 w = PyIter_Next(it);
4364 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004365 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004366 goto Error;
4367 Py_DECREF(it);
4368 return 1;
4369 }
4370 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02004371 _PyErr_Format(tstate, PyExc_ValueError,
4372 "too many values to unpack (expected %d)",
4373 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004374 goto Error;
4375 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004377 l = PySequence_List(it);
4378 if (l == NULL)
4379 goto Error;
4380 *--sp = l;
4381 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004383 ll = PyList_GET_SIZE(l);
4384 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004385 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04004386 "not enough values to unpack (expected at least %d, got %zd)",
4387 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004388 goto Error;
4389 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004391 /* Pop the "after-variable" args off the list. */
4392 for (j = argcntafter; j > 0; j--, i++) {
4393 *--sp = PyList_GET_ITEM(l, ll - j);
4394 }
4395 /* Resize the list. */
4396 Py_SIZE(l) = ll - argcntafter;
4397 Py_DECREF(it);
4398 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004399
Tim Petersd6d010b2001-06-21 02:49:55 +00004400Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004401 for (; i > 0; i--, sp++)
4402 Py_DECREF(*sp);
4403 Py_XDECREF(it);
4404 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004405}
4406
4407
Guido van Rossum96a42c81992-01-12 02:29:51 +00004408#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004409static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004410prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004411{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004412 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02004413 if (PyObject_Print(v, stdout, 0) != 0) {
4414 /* Don't know what else to do */
4415 _PyErr_Clear(tstate);
4416 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004417 printf("\n");
4418 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004419}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004420#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004421
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004422static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004423call_exc_trace(Py_tracefunc func, PyObject *self,
4424 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004425{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004426 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004427 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004428 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004429 if (value == NULL) {
4430 value = Py_None;
4431 Py_INCREF(value);
4432 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004433 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01004434 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004435 arg = PyTuple_Pack(3, type, value, traceback);
4436 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004437 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004438 return;
4439 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004440 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004441 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02004442 if (err == 0) {
4443 _PyErr_Restore(tstate, type, value, orig_traceback);
4444 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004445 else {
4446 Py_XDECREF(type);
4447 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004448 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004449 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004450}
4451
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004452static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004453call_trace_protected(Py_tracefunc func, PyObject *obj,
4454 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004455 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004456{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004457 PyObject *type, *value, *traceback;
4458 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004459 _PyErr_Fetch(tstate, &type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004460 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004461 if (err == 0)
4462 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004463 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004464 return 0;
4465 }
4466 else {
4467 Py_XDECREF(type);
4468 Py_XDECREF(value);
4469 Py_XDECREF(traceback);
4470 return -1;
4471 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004472}
4473
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004474static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004475call_trace(Py_tracefunc func, PyObject *obj,
4476 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004477 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004478{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004479 int result;
4480 if (tstate->tracing)
4481 return 0;
4482 tstate->tracing++;
4483 tstate->use_tracing = 0;
4484 result = func(obj, frame, what, arg);
4485 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4486 || (tstate->c_profilefunc != NULL));
4487 tstate->tracing--;
4488 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004489}
4490
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004491PyObject *
4492_PyEval_CallTracing(PyObject *func, PyObject *args)
4493{
Victor Stinner50b48572018-11-01 01:51:40 +01004494 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004495 int save_tracing = tstate->tracing;
4496 int save_use_tracing = tstate->use_tracing;
4497 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004499 tstate->tracing = 0;
4500 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4501 || (tstate->c_profilefunc != NULL));
4502 result = PyObject_Call(func, args, NULL);
4503 tstate->tracing = save_tracing;
4504 tstate->use_tracing = save_use_tracing;
4505 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004506}
4507
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004508/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004509static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004510maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004511 PyThreadState *tstate, PyFrameObject *frame,
4512 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004513{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004514 int result = 0;
4515 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004516
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004517 /* If the last instruction executed isn't in the current
4518 instruction window, reset the window.
4519 */
4520 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4521 PyAddrPair bounds;
4522 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4523 &bounds);
4524 *instr_lb = bounds.ap_lower;
4525 *instr_ub = bounds.ap_upper;
4526 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004527 /* If the last instruction falls at the start of a line or if it
4528 represents a jump backwards, update the frame's line number and
4529 then call the trace function if we're tracing source lines.
4530 */
4531 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004532 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004533 if (frame->f_trace_lines) {
4534 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4535 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004536 }
George King20faa682017-10-18 17:44:22 -07004537 /* Always emit an opcode event if we're tracing all opcodes. */
4538 if (frame->f_trace_opcodes) {
4539 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4540 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004541 *instr_prev = frame->f_lasti;
4542 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004543}
4544
Fred Drake5755ce62001-06-27 19:19:46 +00004545void
4546PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004547{
Steve Dowerb82e17e2019-05-23 08:45:22 -07004548 if (PySys_Audit("sys.setprofile", NULL) < 0) {
4549 return;
4550 }
4551
Victor Stinner50b48572018-11-01 01:51:40 +01004552 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004553 PyObject *temp = tstate->c_profileobj;
4554 Py_XINCREF(arg);
4555 tstate->c_profilefunc = NULL;
4556 tstate->c_profileobj = NULL;
4557 /* Must make sure that tracing is not ignored if 'temp' is freed */
4558 tstate->use_tracing = tstate->c_tracefunc != NULL;
4559 Py_XDECREF(temp);
4560 tstate->c_profilefunc = func;
4561 tstate->c_profileobj = arg;
4562 /* Flag that tracing or profiling is turned on */
4563 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004564}
4565
4566void
4567PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4568{
Steve Dowerb82e17e2019-05-23 08:45:22 -07004569 if (PySys_Audit("sys.settrace", NULL) < 0) {
4570 return;
4571 }
4572
Victor Stinner09532fe2019-05-10 23:39:09 +02004573 _PyRuntimeState *runtime = &_PyRuntime;
4574 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004575 PyObject *temp = tstate->c_traceobj;
Victor Stinner09532fe2019-05-10 23:39:09 +02004576 runtime->ceval.tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004577 Py_XINCREF(arg);
4578 tstate->c_tracefunc = NULL;
4579 tstate->c_traceobj = NULL;
4580 /* Must make sure that profiling is not ignored if 'temp' is freed */
4581 tstate->use_tracing = tstate->c_profilefunc != NULL;
4582 Py_XDECREF(temp);
4583 tstate->c_tracefunc = func;
4584 tstate->c_traceobj = arg;
4585 /* Flag that tracing or profiling is turned on */
4586 tstate->use_tracing = ((func != NULL)
4587 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004588}
4589
Yury Selivanov75445082015-05-11 22:57:16 -04004590void
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004591_PyEval_SetCoroutineOriginTrackingDepth(int new_depth)
4592{
4593 assert(new_depth >= 0);
Victor Stinner50b48572018-11-01 01:51:40 +01004594 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004595 tstate->coroutine_origin_tracking_depth = new_depth;
4596}
4597
4598int
4599_PyEval_GetCoroutineOriginTrackingDepth(void)
4600{
Victor Stinner50b48572018-11-01 01:51:40 +01004601 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004602 return tstate->coroutine_origin_tracking_depth;
4603}
4604
4605void
Yury Selivanoveb636452016-09-08 22:01:51 -07004606_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4607{
Victor Stinner50b48572018-11-01 01:51:40 +01004608 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004609
4610 if (PySys_Audit("sys.set_asyncgen_hook_firstiter", NULL) < 0) {
4611 return;
4612 }
4613
Yury Selivanoveb636452016-09-08 22:01:51 -07004614 Py_XINCREF(firstiter);
4615 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4616}
4617
4618PyObject *
4619_PyEval_GetAsyncGenFirstiter(void)
4620{
Victor Stinner50b48572018-11-01 01:51:40 +01004621 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004622 return tstate->async_gen_firstiter;
4623}
4624
4625void
4626_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4627{
Victor Stinner50b48572018-11-01 01:51:40 +01004628 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07004629
4630 if (PySys_Audit("sys.set_asyncgen_hook_finalizer", NULL) < 0) {
4631 return;
4632 }
4633
Yury Selivanoveb636452016-09-08 22:01:51 -07004634 Py_XINCREF(finalizer);
4635 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4636}
4637
4638PyObject *
4639_PyEval_GetAsyncGenFinalizer(void)
4640{
Victor Stinner50b48572018-11-01 01:51:40 +01004641 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004642 return tstate->async_gen_finalizer;
4643}
4644
Victor Stinner438a12d2019-05-24 17:01:38 +02004645static PyFrameObject *
4646_PyEval_GetFrame(PyThreadState *tstate)
4647{
4648 return _PyRuntime.gilstate.getframe(tstate);
4649}
4650
4651PyFrameObject *
4652PyEval_GetFrame(void)
4653{
4654 PyThreadState *tstate = _PyThreadState_GET();
4655 return _PyEval_GetFrame(tstate);
4656}
4657
Guido van Rossumb209a111997-04-29 18:18:01 +00004658PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004659PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004660{
Victor Stinner438a12d2019-05-24 17:01:38 +02004661 PyThreadState *tstate = _PyThreadState_GET();
4662 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004663 if (current_frame == NULL)
Victor Stinner438a12d2019-05-24 17:01:38 +02004664 return tstate->interp->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004665 else
4666 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004667}
4668
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004669/* Convenience function to get a builtin from its name */
4670PyObject *
4671_PyEval_GetBuiltinId(_Py_Identifier *name)
4672{
Victor Stinner438a12d2019-05-24 17:01:38 +02004673 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004674 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4675 if (attr) {
4676 Py_INCREF(attr);
4677 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004678 else if (!_PyErr_Occurred(tstate)) {
4679 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004680 }
4681 return attr;
4682}
4683
Guido van Rossumb209a111997-04-29 18:18:01 +00004684PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004685PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004686{
Victor Stinner438a12d2019-05-24 17:01:38 +02004687 PyThreadState *tstate = _PyThreadState_GET();
4688 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Victor Stinner41bb43a2013-10-29 01:19:37 +01004689 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004690 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004691 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004692 }
4693
Victor Stinner438a12d2019-05-24 17:01:38 +02004694 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01004695 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004696 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004697
4698 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004699 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004700}
4701
Guido van Rossumb209a111997-04-29 18:18:01 +00004702PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004703PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004704{
Victor Stinner438a12d2019-05-24 17:01:38 +02004705 PyThreadState *tstate = _PyThreadState_GET();
4706 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
4707 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004708 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02004709 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01004710
4711 assert(current_frame->f_globals != NULL);
4712 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004713}
4714
Guido van Rossum6135a871995-01-09 17:53:26 +00004715int
Tim Peters5ba58662001-07-16 02:29:45 +00004716PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004717{
Victor Stinner438a12d2019-05-24 17:01:38 +02004718 PyThreadState *tstate = _PyThreadState_GET();
4719 PyFrameObject *current_frame = _PyEval_GetFrame(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004720 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004721
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004722 if (current_frame != NULL) {
4723 const int codeflags = current_frame->f_code->co_flags;
4724 const int compilerflags = codeflags & PyCF_MASK;
4725 if (compilerflags) {
4726 result = 1;
4727 cf->cf_flags |= compilerflags;
4728 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004729#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004730 if (codeflags & CO_GENERATOR_ALLOWED) {
4731 result = 1;
4732 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4733 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004734#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004735 }
4736 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004737}
4738
Guido van Rossum3f5da241990-12-20 15:06:42 +00004739
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004740const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004741PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004742{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004743 if (PyMethod_Check(func))
4744 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4745 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004746 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004747 else if (PyCFunction_Check(func))
4748 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4749 else
4750 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004751}
4752
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004753const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004754PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004755{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004756 if (PyMethod_Check(func))
4757 return "()";
4758 else if (PyFunction_Check(func))
4759 return "()";
4760 else if (PyCFunction_Check(func))
4761 return "()";
4762 else
4763 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004764}
4765
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004766#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004767if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004768 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4769 tstate, tstate->frame, \
4770 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004771 x = NULL; \
4772 } \
4773 else { \
4774 x = call; \
4775 if (tstate->c_profilefunc != NULL) { \
4776 if (x == NULL) { \
4777 call_trace_protected(tstate->c_profilefunc, \
4778 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004779 tstate, tstate->frame, \
4780 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004781 /* XXX should pass (type, value, tb) */ \
4782 } else { \
4783 if (call_trace(tstate->c_profilefunc, \
4784 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004785 tstate, tstate->frame, \
4786 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004787 Py_DECREF(x); \
4788 x = NULL; \
4789 } \
4790 } \
4791 } \
4792 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004793} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004794 x = call; \
4795 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004796
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004797
4798static PyObject *
4799trace_call_function(PyThreadState *tstate,
4800 PyObject *func,
4801 PyObject **args, Py_ssize_t nargs,
4802 PyObject *kwnames)
4803{
4804 PyObject *x;
4805 if (PyCFunction_Check(func)) {
Jeroen Demeyer37788bc2019-05-30 15:11:22 +02004806 C_TRACE(x, _PyCFunction_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004807 return x;
4808 }
4809 else if (Py_TYPE(func) == &PyMethodDescr_Type && nargs > 0) {
4810 /* We need to create a temporary bound method as argument
4811 for profiling.
4812
4813 If nargs == 0, then this cannot work because we have no
4814 "self". In any case, the call itself would raise
4815 TypeError (foo needs an argument), so we just skip
4816 profiling. */
4817 PyObject *self = args[0];
4818 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4819 if (func == NULL) {
4820 return NULL;
4821 }
Jeroen Demeyer37788bc2019-05-30 15:11:22 +02004822 C_TRACE(x, _PyCFunction_Vectorcall(func,
4823 args+1, nargs-1,
4824 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004825 Py_DECREF(func);
4826 return x;
4827 }
4828 return _PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
4829}
4830
Victor Stinner415c5102017-01-11 00:54:57 +01004831/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4832 to reduce the stack consumption. */
4833Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Victor Stinner09532fe2019-05-10 23:39:09 +02004834call_function(PyThreadState *tstate, PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004835{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004836 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004837 PyObject *func = *pfunc;
4838 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004839 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4840 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004841 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004842
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004843 if (tstate->use_tracing) {
4844 x = trace_call_function(tstate, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09004845 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004846 else {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02004847 x = _PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004848 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004849
Victor Stinner438a12d2019-05-24 17:01:38 +02004850 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004851
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004852 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004853 while ((*pp_stack) > pfunc) {
4854 w = EXT_POP(*pp_stack);
4855 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004856 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004857
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004858 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004859}
4860
Jeremy Hylton52820442001-01-03 23:52:36 +00004861static PyObject *
Victor Stinner09532fe2019-05-10 23:39:09 +02004862do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004863{
jdemeyere89de732018-09-19 12:06:20 +02004864 PyObject *result;
4865
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004866 if (PyCFunction_Check(func)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004867 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004868 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004869 }
jdemeyere89de732018-09-19 12:06:20 +02004870 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
jdemeyere89de732018-09-19 12:06:20 +02004871 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
4872 if (nargs > 0 && tstate->use_tracing) {
4873 /* We need to create a temporary bound method as argument
4874 for profiling.
4875
4876 If nargs == 0, then this cannot work because we have no
4877 "self". In any case, the call itself would raise
4878 TypeError (foo needs an argument), so we just skip
4879 profiling. */
4880 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
4881 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4882 if (func == NULL) {
4883 return NULL;
4884 }
4885
4886 C_TRACE(result, _PyCFunction_FastCallDict(func,
Victor Stinnerd17a6932018-11-09 16:56:48 +01004887 &_PyTuple_ITEMS(callargs)[1],
jdemeyere89de732018-09-19 12:06:20 +02004888 nargs - 1,
4889 kwdict));
4890 Py_DECREF(func);
4891 return result;
4892 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004893 }
jdemeyere89de732018-09-19 12:06:20 +02004894 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00004895}
4896
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004897/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004898 nb_index slot defined, and store in *pi.
4899 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004900 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004901 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004902*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004903int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004904_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004905{
Victor Stinner438a12d2019-05-24 17:01:38 +02004906 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004907 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004908 Py_ssize_t x;
4909 if (PyIndex_Check(v)) {
4910 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02004911 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004912 return 0;
4913 }
4914 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004915 _PyErr_SetString(tstate, PyExc_TypeError,
4916 "slice indices must be integers or "
4917 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004918 return 0;
4919 }
4920 *pi = x;
4921 }
4922 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004923}
4924
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004925int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004926_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004927{
Victor Stinner438a12d2019-05-24 17:01:38 +02004928 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004929 Py_ssize_t x;
4930 if (PyIndex_Check(v)) {
4931 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02004932 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004933 return 0;
4934 }
4935 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004936 _PyErr_SetString(tstate, PyExc_TypeError,
4937 "slice indices must be integers or "
4938 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004939 return 0;
4940 }
4941 *pi = x;
4942 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004943}
4944
4945
Guido van Rossum486364b2007-06-30 05:01:58 +00004946#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004947 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004948
Guido van Rossumb209a111997-04-29 18:18:01 +00004949static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004950cmp_outcome(PyThreadState *tstate, int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004951{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004952 int res = 0;
4953 switch (op) {
4954 case PyCmp_IS:
4955 res = (v == w);
4956 break;
4957 case PyCmp_IS_NOT:
4958 res = (v != w);
4959 break;
4960 case PyCmp_IN:
4961 res = PySequence_Contains(w, v);
4962 if (res < 0)
4963 return NULL;
4964 break;
4965 case PyCmp_NOT_IN:
4966 res = PySequence_Contains(w, v);
4967 if (res < 0)
4968 return NULL;
4969 res = !res;
4970 break;
4971 case PyCmp_EXC_MATCH:
4972 if (PyTuple_Check(w)) {
4973 Py_ssize_t i, length;
4974 length = PyTuple_Size(w);
4975 for (i = 0; i < length; i += 1) {
4976 PyObject *exc = PyTuple_GET_ITEM(w, i);
4977 if (!PyExceptionClass_Check(exc)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004978 _PyErr_SetString(tstate, PyExc_TypeError,
4979 CANNOT_CATCH_MSG);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004980 return NULL;
4981 }
4982 }
4983 }
4984 else {
4985 if (!PyExceptionClass_Check(w)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004986 _PyErr_SetString(tstate, PyExc_TypeError,
4987 CANNOT_CATCH_MSG);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004988 return NULL;
4989 }
4990 }
4991 res = PyErr_GivenExceptionMatches(v, w);
4992 break;
4993 default:
4994 return PyObject_RichCompare(v, w, op);
4995 }
4996 v = res ? Py_True : Py_False;
4997 Py_INCREF(v);
4998 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004999}
5000
Thomas Wouters52152252000-08-17 22:55:00 +00005001static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005002import_name(PyThreadState *tstate, PyFrameObject *f,
5003 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005004{
5005 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005006 PyObject *import_func, *res;
5007 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005008
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005009 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005010 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005011 if (!_PyErr_Occurred(tstate)) {
5012 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005013 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005014 return NULL;
5015 }
5016
5017 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005018 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005019 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02005020 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005021 return NULL;
5022 }
5023 res = PyImport_ImportModuleLevelObject(
5024 name,
5025 f->f_globals,
5026 f->f_locals == NULL ? Py_None : f->f_locals,
5027 fromlist,
5028 ilevel);
5029 return res;
5030 }
5031
5032 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005033
5034 stack[0] = name;
5035 stack[1] = f->f_globals;
5036 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5037 stack[3] = fromlist;
5038 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005039 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005040 Py_DECREF(import_func);
5041 return res;
5042}
5043
5044static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005045import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005046{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005047 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02005048 _Py_IDENTIFIER(__name__);
Xiang Zhang4830f582017-03-21 11:13:42 +08005049 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005050
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005051 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005052 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005053 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005054 /* Issue #17636: in case this failed because of a circular relative
5055 import, try to fallback on reading the module directly from
5056 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02005057 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005058 if (pkgname == NULL) {
5059 goto error;
5060 }
Oren Milman6db70332017-09-19 14:23:01 +03005061 if (!PyUnicode_Check(pkgname)) {
5062 Py_CLEAR(pkgname);
5063 goto error;
5064 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005065 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005066 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005067 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005068 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005069 }
Eric Snow3f9eee62017-09-15 16:35:20 -06005070 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005071 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02005072 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07005073 goto error;
5074 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005075 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005076 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005077 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005078 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005079 if (pkgname == NULL) {
5080 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5081 if (pkgname_or_unknown == NULL) {
5082 Py_XDECREF(pkgpath);
5083 return NULL;
5084 }
5085 } else {
5086 pkgname_or_unknown = pkgname;
5087 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005088
5089 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005090 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08005091 errmsg = PyUnicode_FromFormat(
5092 "cannot import name %R from %R (unknown location)",
5093 name, pkgname_or_unknown
5094 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005095 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005096 PyErr_SetImportError(errmsg, pkgname, NULL);
5097 }
5098 else {
5099 errmsg = PyUnicode_FromFormat(
5100 "cannot import name %R from %R (%S)",
5101 name, pkgname_or_unknown, pkgpath
5102 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005103 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005104 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005105 }
5106
Xiang Zhang4830f582017-03-21 11:13:42 +08005107 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005108 Py_XDECREF(pkgname_or_unknown);
5109 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005110 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005111}
Guido van Rossumac7be682001-01-17 15:42:30 +00005112
Thomas Wouters52152252000-08-17 22:55:00 +00005113static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005114import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00005115{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005116 _Py_IDENTIFIER(__all__);
5117 _Py_IDENTIFIER(__dict__);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005118 _Py_IDENTIFIER(__name__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005119 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005120 int skip_leading_underscores = 0;
5121 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005122
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005123 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5124 return -1; /* Unexpected error */
5125 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005126 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005127 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5128 return -1;
5129 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005130 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005131 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005132 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005133 return -1;
5134 }
5135 all = PyMapping_Keys(dict);
5136 Py_DECREF(dict);
5137 if (all == NULL)
5138 return -1;
5139 skip_leading_underscores = 1;
5140 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005142 for (pos = 0, err = 0; ; pos++) {
5143 name = PySequence_GetItem(all, pos);
5144 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005145 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005146 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02005147 }
5148 else {
5149 _PyErr_Clear(tstate);
5150 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005151 break;
5152 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005153 if (!PyUnicode_Check(name)) {
5154 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5155 if (modname == NULL) {
5156 Py_DECREF(name);
5157 err = -1;
5158 break;
5159 }
5160 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005161 _PyErr_Format(tstate, PyExc_TypeError,
5162 "module __name__ must be a string, not %.100s",
5163 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005164 }
5165 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005166 _PyErr_Format(tstate, PyExc_TypeError,
5167 "%s in %U.%s must be str, not %.100s",
5168 skip_leading_underscores ? "Key" : "Item",
5169 modname,
5170 skip_leading_underscores ? "__dict__" : "__all__",
5171 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005172 }
5173 Py_DECREF(modname);
5174 Py_DECREF(name);
5175 err = -1;
5176 break;
5177 }
5178 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005179 if (PyUnicode_READY(name) == -1) {
5180 Py_DECREF(name);
5181 err = -1;
5182 break;
5183 }
5184 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5185 Py_DECREF(name);
5186 continue;
5187 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005188 }
5189 value = PyObject_GetAttr(v, name);
5190 if (value == NULL)
5191 err = -1;
5192 else if (PyDict_CheckExact(locals))
5193 err = PyDict_SetItem(locals, name, value);
5194 else
5195 err = PyObject_SetItem(locals, name, value);
5196 Py_DECREF(name);
5197 Py_XDECREF(value);
5198 if (err != 0)
5199 break;
5200 }
5201 Py_DECREF(all);
5202 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005203}
5204
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005205static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005206check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005207{
5208 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005209 _PyErr_Format(tstate, PyExc_TypeError,
5210 "%.200s%.200s argument after * "
5211 "must be an iterable, not %.200s",
5212 PyEval_GetFuncName(func),
5213 PyEval_GetFuncDesc(func),
5214 args->ob_type->tp_name);
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005215 return -1;
5216 }
5217 return 0;
5218}
5219
5220static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005221format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005222{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005223 /* _PyDict_MergeEx raises attribute
5224 * error (percolated from an attempt
5225 * to get 'keys' attribute) instead of
5226 * a type error if its second argument
5227 * is not a mapping.
5228 */
Victor Stinner438a12d2019-05-24 17:01:38 +02005229 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
5230 _PyErr_Format(tstate, PyExc_TypeError,
5231 "%.200s%.200s argument after ** "
5232 "must be a mapping, not %.200s",
5233 PyEval_GetFuncName(func),
5234 PyEval_GetFuncDesc(func),
5235 kwargs->ob_type->tp_name);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005236 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005237 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005238 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02005239 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005240 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
5241 PyObject *key = PyTuple_GET_ITEM(val, 0);
5242 if (!PyUnicode_Check(key)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005243 _PyErr_Format(tstate, PyExc_TypeError,
5244 "%.200s%.200s keywords must be strings",
5245 PyEval_GetFuncName(func),
5246 PyEval_GetFuncDesc(func));
5247 }
5248 else {
5249 _PyErr_Format(tstate, PyExc_TypeError,
5250 "%.200s%.200s got multiple "
5251 "values for keyword argument '%U'",
5252 PyEval_GetFuncName(func),
5253 PyEval_GetFuncDesc(func),
5254 key);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005255 }
5256 Py_XDECREF(exc);
5257 Py_XDECREF(val);
5258 Py_XDECREF(tb);
5259 }
5260 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005261 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005262 }
5263 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005264}
5265
Guido van Rossumac7be682001-01-17 15:42:30 +00005266static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005267format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
5268 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005269{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005270 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005272 if (!obj)
5273 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005274
Serhiy Storchaka06515832016-11-20 09:13:07 +02005275 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005276 if (!obj_str)
5277 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005278
Victor Stinner438a12d2019-05-24 17:01:38 +02005279 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005280}
Guido van Rossum950361c1997-01-24 13:49:28 +00005281
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005282static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005283format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005284{
5285 PyObject *name;
5286 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02005287 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005288 return;
5289 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5290 name = PyTuple_GET_ITEM(co->co_cellvars,
5291 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005292 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005293 PyExc_UnboundLocalError,
5294 UNBOUNDLOCAL_ERROR_MSG,
5295 name);
5296 } else {
5297 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5298 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02005299 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005300 UNBOUNDFREE_ERROR_MSG, name);
5301 }
5302}
5303
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005304static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005305format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005306{
5307 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5308 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005309 _PyErr_Format(tstate, PyExc_TypeError,
5310 "'async with' received an object from __aenter__ "
5311 "that does not implement __await__: %.100s",
5312 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005313 }
5314 else if (prevopcode == WITH_CLEANUP_START) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005315 _PyErr_Format(tstate, PyExc_TypeError,
5316 "'async with' received an object from __aexit__ "
5317 "that does not implement __await__: %.100s",
5318 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005319 }
5320 }
5321}
5322
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005323static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005324unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005325 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005326{
5327 PyObject *res;
5328 if (Py_REFCNT(v) == 2) {
5329 /* In the common case, there are 2 references to the value
5330 * stored in 'variable' when the += is performed: one on the
5331 * value stack (in 'v') and one still stored in the
5332 * 'variable'. We try to delete the variable now to reduce
5333 * the refcnt to 1.
5334 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005335 int opcode, oparg;
5336 NEXTOPARG();
5337 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005338 case STORE_FAST:
5339 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005340 PyObject **fastlocals = f->f_localsplus;
5341 if (GETLOCAL(oparg) == v)
5342 SETLOCAL(oparg, NULL);
5343 break;
5344 }
5345 case STORE_DEREF:
5346 {
5347 PyObject **freevars = (f->f_localsplus +
5348 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005349 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005350 if (PyCell_GET(c) == v) {
5351 PyCell_SET(c, NULL);
5352 Py_DECREF(v);
5353 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005354 break;
5355 }
5356 case STORE_NAME:
5357 {
5358 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005359 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005360 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005361 if (locals && PyDict_CheckExact(locals)) {
5362 PyObject *w = PyDict_GetItemWithError(locals, name);
5363 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02005364 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005365 {
5366 Py_DECREF(v);
5367 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005368 }
5369 }
5370 break;
5371 }
5372 }
5373 }
5374 res = v;
5375 PyUnicode_Append(&res, w);
5376 return res;
5377}
5378
Guido van Rossum950361c1997-01-24 13:49:28 +00005379#ifdef DYNAMIC_EXECUTION_PROFILE
5380
Skip Montanarof118cb12001-10-15 20:51:38 +00005381static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005382getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005383{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005384 int i;
5385 PyObject *l = PyList_New(256);
5386 if (l == NULL) return NULL;
5387 for (i = 0; i < 256; i++) {
5388 PyObject *x = PyLong_FromLong(a[i]);
5389 if (x == NULL) {
5390 Py_DECREF(l);
5391 return NULL;
5392 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005393 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005394 }
5395 for (i = 0; i < 256; i++)
5396 a[i] = 0;
5397 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005398}
5399
5400PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005401_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005402{
5403#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005404 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005405#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005406 int i;
5407 PyObject *l = PyList_New(257);
5408 if (l == NULL) return NULL;
5409 for (i = 0; i < 257; i++) {
5410 PyObject *x = getarray(dxpairs[i]);
5411 if (x == NULL) {
5412 Py_DECREF(l);
5413 return NULL;
5414 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005415 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005416 }
5417 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005418#endif
5419}
5420
5421#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005422
5423Py_ssize_t
5424_PyEval_RequestCodeExtraIndex(freefunc free)
5425{
Victor Stinnercaba55b2018-08-03 15:33:52 +02005426 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -07005427 Py_ssize_t new_index;
5428
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005429 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005430 return -1;
5431 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005432 new_index = interp->co_extra_user_count++;
5433 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005434 return new_index;
5435}
Łukasz Langaa785c872016-09-09 17:37:37 -07005436
5437static void
5438dtrace_function_entry(PyFrameObject *f)
5439{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005440 const char *filename;
5441 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005442 int lineno;
5443
5444 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5445 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5446 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5447
5448 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5449}
5450
5451static void
5452dtrace_function_return(PyFrameObject *f)
5453{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005454 const char *filename;
5455 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005456 int lineno;
5457
5458 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5459 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5460 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5461
5462 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5463}
5464
5465/* DTrace equivalent of maybe_call_line_trace. */
5466static void
5467maybe_dtrace_line(PyFrameObject *frame,
5468 int *instr_lb, int *instr_ub, int *instr_prev)
5469{
5470 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005471 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005472
5473 /* If the last instruction executed isn't in the current
5474 instruction window, reset the window.
5475 */
5476 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5477 PyAddrPair bounds;
5478 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5479 &bounds);
5480 *instr_lb = bounds.ap_lower;
5481 *instr_ub = bounds.ap_upper;
5482 }
5483 /* If the last instruction falls at the start of a line or if
5484 it represents a jump backwards, update the frame's line
5485 number and call the trace function. */
5486 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5487 frame->f_lineno = line;
5488 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5489 if (!co_filename)
5490 co_filename = "?";
5491 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5492 if (!co_name)
5493 co_name = "?";
5494 PyDTrace_LINE(co_filename, co_name, line);
5495 }
5496 *instr_prev = frame->f_lasti;
5497}