blob: 68c1617c78f2842dd71a69ba8f9484a1e35c843b [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 Stinnerbcda8f12018-11-21 22:27:47 +010013#include "pycore_object.h"
Victor Stinner621cebe2018-11-12 16:53:38 +010014#include "pycore_pystate.h"
Victor Stinnerec13b932018-11-25 23:56:17 +010015#include "pycore_tupleobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000016
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000017#include "code.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040018#include "dictobject.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000019#include "frameobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000020#include "opcode.h"
Łukasz Langaa785c872016-09-09 17:37:37 -070021#include "pydtrace.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040022#include "setobject.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000023#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000024
Guido van Rossumc6004111993-11-05 10:22:19 +000025#include <ctype.h>
26
Guido van Rossum408027e1996-12-30 16:17:54 +000027#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000028/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000029#define LLTRACE 1 /* Low-level trace feature */
30#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000031#endif
32
Yury Selivanovf2392132016-12-13 19:03:51 -050033/* Private API for the LOAD_METHOD opcode. */
34extern int _PyObject_GetMethod(PyObject *, PyObject *, PyObject **);
35
Jeremy Hylton52820442001-01-03 23:52:36 +000036typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000037
Guido van Rossum374a9221991-04-04 10:40:29 +000038/* Forward declarations */
Eric Snow2ebc5ce2017-09-07 23:51:28 -060039Py_LOCAL_INLINE(PyObject *) call_function(PyObject ***, Py_ssize_t,
40 PyObject *);
Victor Stinnerf9b760f2016-09-09 10:17:08 -070041static PyObject * do_call_core(PyObject *, PyObject *, PyObject *);
Jeremy Hylton52820442001-01-03 23:52:36 +000042
Guido van Rossum0a066c01992-03-27 17:29:15 +000043#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000044static int lltrace;
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020045static int prtrace(PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000046#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010047static int call_trace(Py_tracefunc, PyObject *,
48 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000050static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010051 PyThreadState *, PyFrameObject *,
52 int, PyObject *);
53static void call_exc_trace(Py_tracefunc, PyObject *,
54 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000055static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Eric Snow2ebc5ce2017-09-07 23:51:28 -060056 PyThreadState *, PyFrameObject *,
57 int *, int *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070058static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *);
59static void dtrace_function_entry(PyFrameObject *);
60static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000061
Thomas Wouters477c8d52006-05-27 19:21:47 +000062static PyObject * cmp_outcome(int, PyObject *, PyObject *);
Eric Snow2ebc5ce2017-09-07 23:51:28 -060063static PyObject * import_name(PyFrameObject *, PyObject *, PyObject *,
64 PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +000065static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +000066static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +000067static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +000068static void format_exc_unbound(PyCodeObject *co, int oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +020069static PyObject * unicode_concatenate(PyObject *, PyObject *,
Serhiy Storchakaab874002016-09-11 13:48:15 +030070 PyFrameObject *, const _Py_CODEUNIT *);
Benjamin Petersonce798522012-01-22 11:24:29 -050071static PyObject * special_lookup(PyObject *, _Py_Identifier *);
Serhiy Storchaka25e4f772017-08-03 11:37:15 +030072static int check_args_iterable(PyObject *func, PyObject *vararg);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +020073static void format_kwargs_error(PyObject *func, PyObject *kwargs);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +030074static void format_awaitable_error(PyTypeObject *, int);
Guido van Rossum374a9221991-04-04 10:40:29 +000075
Paul Prescode68140d2000-08-30 20:25:01 +000076#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000077 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000078#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000080#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 "free variable '%.200s' referenced before assignment" \
82 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000083
Guido van Rossum950361c1997-01-24 13:49:28 +000084/* Dynamic execution profile */
85#ifdef DYNAMIC_EXECUTION_PROFILE
86#ifdef DXPAIRS
87static long dxpairs[257][256];
88#define dxp dxpairs[256]
89#else
90static long dxp[256];
91#endif
92#endif
93
Eric Snow2ebc5ce2017-09-07 23:51:28 -060094#define GIL_REQUEST _Py_atomic_load_relaxed(&_PyRuntime.ceval.gil_drop_request)
Benjamin Petersond2be5b42010-09-10 22:47:02 +000095
Jeffrey Yasskin39370832010-05-03 19:29:34 +000096/* This can set eval_breaker to 0 even though gil_drop_request became
97 1. We believe this is all right because the eval loop will release
98 the GIL eventually anyway. */
Eric Snowef4ac962019-02-24 15:40:47 -080099#define COMPUTE_EVAL_BREAKER(interp) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 _Py_atomic_store_relaxed( \
Eric Snowef4ac962019-02-24 15:40:47 -0800101 &interp->ceval.eval_breaker, \
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000102 GIL_REQUEST | \
Eric Snowfdf282d2019-01-11 14:26:55 -0700103 _Py_atomic_load_relaxed(&_PyRuntime.ceval.signals_pending) | \
Eric Snowef4ac962019-02-24 15:40:47 -0800104 _Py_atomic_load_relaxed(&interp->ceval.pending.calls_to_do) | \
105 interp->ceval.pending.async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000106
Eric Snowef4ac962019-02-24 15:40:47 -0800107#define SET_GIL_DROP_REQUEST(interp) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 do { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600109 _Py_atomic_store_relaxed(&_PyRuntime.ceval.gil_drop_request, 1); \
Eric Snowef4ac962019-02-24 15:40:47 -0800110 _Py_atomic_store_relaxed(&interp->ceval.eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000112
Eric Snowef4ac962019-02-24 15:40:47 -0800113#define RESET_GIL_DROP_REQUEST(interp) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 do { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600115 _Py_atomic_store_relaxed(&_PyRuntime.ceval.gil_drop_request, 0); \
Eric Snowef4ac962019-02-24 15:40:47 -0800116 COMPUTE_EVAL_BREAKER(interp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000118
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000119/* Pending calls are only modified under pending_lock */
Eric Snowef4ac962019-02-24 15:40:47 -0800120#define SIGNAL_PENDING_CALLS(interp) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 do { \
Eric Snowef4ac962019-02-24 15:40:47 -0800122 _Py_atomic_store_relaxed(&interp->ceval.pending.calls_to_do, 1); \
123 _Py_atomic_store_relaxed(&interp->ceval.eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000125
Eric Snowef4ac962019-02-24 15:40:47 -0800126#define UNSIGNAL_PENDING_CALLS(interp) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 do { \
Eric Snowef4ac962019-02-24 15:40:47 -0800128 _Py_atomic_store_relaxed(&interp->ceval.pending.calls_to_do, 0); \
129 COMPUTE_EVAL_BREAKER(interp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000130 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000131
Eric Snowfdf282d2019-01-11 14:26:55 -0700132#define SIGNAL_PENDING_SIGNALS() \
133 do { \
134 _Py_atomic_store_relaxed(&_PyRuntime.ceval.signals_pending, 1); \
Eric Snowef4ac962019-02-24 15:40:47 -0800135 _Py_atomic_store_relaxed(&_PyRuntime.interpreters.main->ceval.eval_breaker, 1); \
Eric Snowfdf282d2019-01-11 14:26:55 -0700136 } while (0)
137
138#define UNSIGNAL_PENDING_SIGNALS() \
139 do { \
140 _Py_atomic_store_relaxed(&_PyRuntime.ceval.signals_pending, 0); \
Eric Snowef4ac962019-02-24 15:40:47 -0800141 COMPUTE_EVAL_BREAKER(_PyRuntime.interpreters.main); \
Eric Snowfdf282d2019-01-11 14:26:55 -0700142 } while (0)
143
Eric Snowef4ac962019-02-24 15:40:47 -0800144#define SIGNAL_ASYNC_EXC(interp) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 do { \
Eric Snowef4ac962019-02-24 15:40:47 -0800146 interp->ceval.pending.async_exc = 1; \
147 _Py_atomic_store_relaxed(&interp->ceval.eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000149
Eric Snowef4ac962019-02-24 15:40:47 -0800150#define UNSIGNAL_ASYNC_EXC(interp) \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600151 do { \
Eric Snowef4ac962019-02-24 15:40:47 -0800152 interp->ceval.pending.async_exc = 0; \
153 COMPUTE_EVAL_BREAKER(interp); \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600154 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000155
156
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000157#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000158#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000159#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000160#include "pythread.h"
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000161#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000162
Tim Peters7f468f22004-10-11 02:40:51 +0000163int
164PyEval_ThreadsInitialized(void)
165{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000167}
168
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000169void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000170PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000171{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 if (gil_created())
173 return;
Inada Naoki001fee12019-02-20 10:00:09 +0900174 PyThread_init_thread();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 create_gil();
Victor Stinner50b48572018-11-01 01:51:40 +0100176 take_gil(_PyThreadState_GET());
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000177}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000178
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000179void
Antoine Pitrou1df15362010-09-13 14:16:46 +0000180_PyEval_FiniThreads(void)
181{
182 if (!gil_created())
183 return;
184 destroy_gil();
185 assert(!gil_created());
186}
187
188void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000189PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000190{
Victor Stinner50b48572018-11-01 01:51:40 +0100191 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 if (tstate == NULL)
193 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
194 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000195}
196
197void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000198PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000199{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100201 We therefore avoid PyThreadState_Get() which dumps a fatal error
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 in debug mode.
203 */
Victor Stinner50b48572018-11-01 01:51:40 +0100204 drop_gil(_PyThreadState_GET());
Guido van Rossum25ce5661997-08-02 03:10:38 +0000205}
206
207void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000208PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000209{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 if (tstate == NULL)
211 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
212 /* Check someone has called PyEval_InitThreads() to create the lock */
213 assert(gil_created());
214 take_gil(tstate);
215 if (PyThreadState_Swap(tstate) != NULL)
216 Py_FatalError(
217 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000218}
219
220void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000221PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000222{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 if (tstate == NULL)
224 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
225 if (PyThreadState_Swap(NULL) != tstate)
226 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
227 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000228}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000229
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200230/* This function is called from PyOS_AfterFork_Child to destroy all threads
231 * which are not running in the child process, and clear internal locks
232 * which might be held by those threads.
233 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000234
235void
236PyEval_ReInitThreads(void)
237{
Victor Stinner50b48572018-11-01 01:51:40 +0100238 PyThreadState *current_tstate = _PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 if (!gil_created())
241 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 recreate_gil();
Eric Snowef4ac962019-02-24 15:40:47 -0800243 // This will be reset in make_pending_calls() below.
244 current_tstate->interp->ceval.pending.lock = NULL;
245
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200246 take_gil(current_tstate);
Eric Snowef4ac962019-02-24 15:40:47 -0800247 _PyRuntime.main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000248
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200249 /* Destroy all threads except the current one */
250 _PyThreadState_DeleteExcept(current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000251}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000252
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000253/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600254 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000255
256void
Eric Snowef4ac962019-02-24 15:40:47 -0800257_PyEval_SignalAsyncExc(PyInterpreterState *interp)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000258{
Eric Snowef4ac962019-02-24 15:40:47 -0800259 SIGNAL_ASYNC_EXC(interp);
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000260}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000261
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000262PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000263PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000264{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 PyThreadState *tstate = PyThreadState_Swap(NULL);
266 if (tstate == NULL)
267 Py_FatalError("PyEval_SaveThread: NULL tstate");
Victor Stinner2914bb32018-01-29 11:57:45 +0100268 assert(gil_created());
269 drop_gil(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000271}
272
273void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000274PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000275{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 if (tstate == NULL)
277 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Victor Stinner2914bb32018-01-29 11:57:45 +0100278 assert(gil_created());
279
280 int err = errno;
281 take_gil(tstate);
282 /* _Py_Finalizing is protected by the GIL */
283 if (_Py_IsFinalizing() && !_Py_CURRENTLY_FINALIZING(tstate)) {
284 drop_gil(tstate);
285 PyThread_exit_thread();
286 Py_UNREACHABLE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 }
Victor Stinner2914bb32018-01-29 11:57:45 +0100288 errno = err;
289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000291}
292
293
Guido van Rossuma9672091994-09-14 13:31:22 +0000294/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
295 signal handlers or Mac I/O completion routines) can schedule calls
296 to a function to be called synchronously.
297 The synchronous function is called with one void* argument.
298 It should return 0 for success or -1 for failure -- failure should
299 be accompanied by an exception.
300
301 If registry succeeds, the registry function returns 0; if it fails
302 (e.g. due to too many pending calls) it returns -1 (without setting
303 an exception condition).
304
305 Note that because registry may occur from within signal handlers,
306 or other asynchronous events, calling malloc() is unsafe!
307
Guido van Rossuma9672091994-09-14 13:31:22 +0000308 Any thread can schedule pending calls, but only the main thread
309 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000310 There is no facility to schedule calls to a particular thread, but
311 that should be easy to change, should that ever be required. In
312 that case, the static variables here should go into the python
313 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000314*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000315
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200316void
317_PyEval_SignalReceived(void)
318{
319 /* bpo-30703: Function called when the C signal handler of Python gets a
320 signal. We cannot queue a callback using Py_AddPendingCall() since
321 that function is not async-signal-safe. */
Eric Snowfdf282d2019-01-11 14:26:55 -0700322 SIGNAL_PENDING_SIGNALS();
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200323}
324
Eric Snowef4ac962019-02-24 15:40:47 -0800325static int
326_add_pending_call(PyInterpreterState *interp, unsigned long thread_id, int (*func)(void *), void *arg)
327{
328 int i = interp->ceval.pending.last;
329 int j = (i + 1) % NPENDINGCALLS;
330 if (j == interp->ceval.pending.first) {
331 return -1; /* Queue full */
332 }
333 interp->ceval.pending.calls[i].thread_id = thread_id;
334 interp->ceval.pending.calls[i].func = func;
335 interp->ceval.pending.calls[i].arg = arg;
336 interp->ceval.pending.last = j;
337 return 0;
338}
339
340/* pop one item off the queue while holding the lock */
341static void
342_pop_pending_call(PyInterpreterState *interp, int (**func)(void *), void **arg)
343{
344 int i = interp->ceval.pending.first;
345 if (i == interp->ceval.pending.last) {
346 return; /* Queue empty */
347 }
348
349 *func = interp->ceval.pending.calls[i].func;
350 *arg = interp->ceval.pending.calls[i].arg;
351 interp->ceval.pending.first = (i + 1) % NPENDINGCALLS;
352
353 unsigned long thread_id = interp->ceval.pending.calls[i].thread_id;
354 if (thread_id && PyThread_get_thread_ident() != thread_id) {
355 // Thread mismatch, so move it to the end of the list
356 // and start over.
357 _Py_AddPendingCall(interp, thread_id, *func, *arg);
358 return;
359 }
360}
361
362int
363Py_AddPendingCall(int (*func)(void *), void *arg)
364{
365 PyInterpreterState *interp = _PyRuntime.interpreters.main;
366 return _Py_AddPendingCall(interp, _PyRuntime.main_thread, func, arg);
367}
368
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200369/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000370 scheduling to be made from any thread, and even from an executing
371 callback.
372 */
373
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000374int
Eric Snowef4ac962019-02-24 15:40:47 -0800375_Py_AddPendingCall(PyInterpreterState *interp, unsigned long thread_id, int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000376{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 /* try a few times for the lock. Since this mechanism is used
378 * for signal handling (on the main thread), there is a (slim)
379 * chance that a signal is delivered on the same thread while we
380 * hold the lock during the Py_MakePendingCalls() function.
381 * This avoids a deadlock in that case.
382 * Note that signals can be delivered on any thread. In particular,
383 * on Windows, a SIGINT is delivered on a system-created worker
384 * thread.
385 * We also check for lock being NULL, in the unlikely case that
386 * this function is called before any bytecode evaluation takes place.
387 */
Eric Snowef4ac962019-02-24 15:40:47 -0800388 PyThread_type_lock lock = interp->ceval.pending.lock;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 if (lock != NULL) {
Eric Snowef4ac962019-02-24 15:40:47 -0800390 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 for (i = 0; i<100; i++) {
392 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
393 break;
394 }
395 if (i == 100)
396 return -1;
397 }
398
Eric Snowef4ac962019-02-24 15:40:47 -0800399 int result = -1;
400 if (interp->finalizing) {
401 PyObject *exc, *val, *tb;
402 PyErr_Fetch(&exc, &val, &tb);
403 PyErr_SetString(PyExc_SystemError, "Py_AddPendingCall: cannot add pending calls (interpreter shutting down)");
404 PyErr_Print();
405 PyErr_Restore(exc, val, tb);
406 goto done;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 }
Eric Snowef4ac962019-02-24 15:40:47 -0800408
409 result = _add_pending_call(interp, thread_id, func, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 /* signal main loop */
Eric Snowef4ac962019-02-24 15:40:47 -0800411 SIGNAL_PENDING_CALLS(interp);
412
413done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 if (lock != NULL)
415 PyThread_release_lock(lock);
416 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000417}
418
Eric Snowfdf282d2019-01-11 14:26:55 -0700419static int
420handle_signals(void)
421{
422 /* Only handle signals on main thread. */
Eric Snowef4ac962019-02-24 15:40:47 -0800423 if (PyThread_get_thread_ident() != _PyRuntime.main_thread) {
Eric Snowfdf282d2019-01-11 14:26:55 -0700424 return 0;
425 }
Eric Snow64d6cc82019-02-23 15:40:43 -0700426 /*
427 * Ensure that the thread isn't currently running some other
428 * interpreter.
429 */
430 if (_PyInterpreterState_GET_UNSAFE() != _PyRuntime.interpreters.main) {
431 return 0;
432 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700433
434 UNSIGNAL_PENDING_SIGNALS();
Eric Snow64d6cc82019-02-23 15:40:43 -0700435 if (_PyErr_CheckSignals() < 0) {
Eric Snowfdf282d2019-01-11 14:26:55 -0700436 SIGNAL_PENDING_SIGNALS(); /* We're not done yet */
437 return -1;
438 }
439 return 0;
440}
441
442static int
Eric Snowef4ac962019-02-24 15:40:47 -0800443make_pending_calls(PyInterpreterState *interp)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000444{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200445 static int busy = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 /* don't perform recursive pending calls */
Eric Snowfdf282d2019-01-11 14:26:55 -0700448 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700450 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200451 busy = 1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200452 /* unsignal before starting to call callbacks, so that any callback
453 added in-between re-signals */
Eric Snowef4ac962019-02-24 15:40:47 -0800454 UNSIGNAL_PENDING_CALLS(interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700455 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200456
Eric Snowef4ac962019-02-24 15:40:47 -0800457 if (!interp->ceval.pending.lock) {
Eric Snowfdf282d2019-01-11 14:26:55 -0700458 /* initial allocation of the lock */
Eric Snowef4ac962019-02-24 15:40:47 -0800459 interp->ceval.pending.lock = PyThread_allocate_lock();
460 if (interp->ceval.pending.lock == NULL) {
Eric Snowfdf282d2019-01-11 14:26:55 -0700461 res = -1;
462 goto error;
463 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200464 }
465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 /* perform a bounded number of calls, in case of recursion */
Eric Snowfdf282d2019-01-11 14:26:55 -0700467 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snowef4ac962019-02-24 15:40:47 -0800468 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 void *arg = NULL;
470
471 /* pop one item off the queue while holding the lock */
Eric Snowef4ac962019-02-24 15:40:47 -0800472 PyThread_acquire_lock(interp->ceval.pending.lock, WAIT_LOCK);
473 _pop_pending_call(interp, &func, &arg);
474 PyThread_release_lock(interp->ceval.pending.lock);
475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 /* having released the lock, perform the callback */
Eric Snowef4ac962019-02-24 15:40:47 -0800477 if (func == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 break;
Eric Snowef4ac962019-02-24 15:40:47 -0800479 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700480 res = func(arg);
481 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200482 goto error;
483 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200485
Charles-François Natalif23339a2011-07-23 18:15:43 +0200486 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700487 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200488
489error:
490 busy = 0;
Eric Snowef4ac962019-02-24 15:40:47 -0800491 SIGNAL_PENDING_CALLS(interp); /* We're not done yet */
Eric Snowfdf282d2019-01-11 14:26:55 -0700492 return res;
493}
494
Eric Snowef4ac962019-02-24 15:40:47 -0800495int
496_Py_MakePendingCalls(PyInterpreterState *interp)
497{
498 assert(PyGILState_Check());
499
500 return make_pending_calls(interp);
501}
502
Eric Snowfdf282d2019-01-11 14:26:55 -0700503/* Py_MakePendingCalls() is a simple wrapper for the sake
504 of backward-compatibility. */
505int
506Py_MakePendingCalls(void)
507{
508 assert(PyGILState_Check());
509
510 /* Python signal handler doesn't really queue a callback: it only signals
511 that a signal was received, see _PyEval_SignalReceived(). */
512 int res = handle_signals();
513 if (res != 0) {
514 return res;
515 }
516
Eric Snowef4ac962019-02-24 15:40:47 -0800517 PyInterpreterState *interp = _PyRuntime.interpreters.main;
518 return make_pending_calls(interp);
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000519}
520
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000521/* The interpreter's recursion limit */
522
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000523#ifndef Py_DEFAULT_RECURSION_LIMIT
524#define Py_DEFAULT_RECURSION_LIMIT 1000
525#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600526
Eric Snow05351c12017-09-05 21:43:08 -0700527int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000528
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600529void
530_PyEval_Initialize(struct _ceval_runtime_state *state)
531{
532 state->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
533 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
534 _gil_initialize(&state->gil);
535}
536
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000537int
538Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000539{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600540 return _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000541}
542
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000543void
544Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000545{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600546 _PyRuntime.ceval.recursion_limit = new_limit;
547 _Py_CheckRecursionLimit = _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000548}
549
Armin Rigo2b3eb402003-10-28 12:05:48 +0000550/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
551 if the recursion_depth reaches _Py_CheckRecursionLimit.
552 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
553 to guarantee that _Py_CheckRecursiveCall() is regularly called.
554 Without USE_STACKCHECK, there is no need for this. */
555int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300556_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000557{
Victor Stinner50b48572018-11-01 01:51:40 +0100558 PyThreadState *tstate = _PyThreadState_GET();
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600559 int recursion_limit = _PyRuntime.ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000560
561#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700562 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 if (PyOS_CheckStack()) {
564 --tstate->recursion_depth;
565 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
566 return -1;
567 }
pdox18967932017-10-25 23:03:01 -0700568 /* Needed for ABI backwards-compatibility (see bpo-31857) */
Eric Snow05351c12017-09-05 21:43:08 -0700569 _Py_CheckRecursionLimit = recursion_limit;
pdox18967932017-10-25 23:03:01 -0700570#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 if (tstate->recursion_critical)
572 /* Somebody asked that we don't check for recursion. */
573 return 0;
574 if (tstate->overflowed) {
575 if (tstate->recursion_depth > recursion_limit + 50) {
576 /* Overflowing while handling an overflow. Give up. */
577 Py_FatalError("Cannot recover from stack overflow.");
578 }
579 return 0;
580 }
581 if (tstate->recursion_depth > recursion_limit) {
582 --tstate->recursion_depth;
583 tstate->overflowed = 1;
Yury Selivanovf488fb42015-07-03 01:04:23 -0400584 PyErr_Format(PyExc_RecursionError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 "maximum recursion depth exceeded%s",
586 where);
587 return -1;
588 }
589 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000590}
591
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400592static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000593static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000594
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600595#define _Py_TracingPossible _PyRuntime.ceval.tracing_possible
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000596
Guido van Rossum374a9221991-04-04 10:40:29 +0000597
Guido van Rossumb209a111997-04-29 18:18:01 +0000598PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000599PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000600{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 return PyEval_EvalCodeEx(co,
602 globals, locals,
603 (PyObject **)NULL, 0,
604 (PyObject **)NULL, 0,
605 (PyObject **)NULL, 0,
606 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000607}
608
609
610/* Interpreter main loop */
611
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000612PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000613PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 /* This is for backward compatibility with extension modules that
615 used this API; core interpreter code should call
616 PyEval_EvalFrameEx() */
617 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000618}
619
620PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000621PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000622{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200623 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
624 return interp->eval_frame(f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700625}
626
Victor Stinnerc6944e72016-11-11 02:13:35 +0100627PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700628_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
629{
Guido van Rossum950361c1997-01-24 13:49:28 +0000630#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000632#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200633 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300634 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200635 int opcode; /* Current opcode */
636 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200637 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 PyObject *retval = NULL; /* Return value */
Victor Stinner50b48572018-11-01 01:51:40 +0100639 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 is true when the line being executed has changed. The
647 initial values are such as to make this false the first
648 time it is tested. */
649 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000650
Serhiy Storchakaab874002016-09-11 13:48:15 +0300651 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 PyObject *names;
653 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000654
Brett Cannon368b4b72012-04-02 12:17:59 -0400655#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200656 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400657#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200658
Antoine Pitroub52ec782009-01-25 16:34:23 +0000659/* Computed GOTOs, or
660 the-optimization-commonly-but-improperly-known-as-"threaded code"
661 using gcc's labels-as-values extension
662 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
663
664 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000666 combined with a lookup table of jump addresses. However, since the
667 indirect jump instruction is shared by all opcodes, the CPU will have a
668 hard time making the right prediction for where to jump next (actually,
669 it will be always wrong except in the uncommon case of a sequence of
670 several identical opcodes).
671
672 "Threaded code" in contrast, uses an explicit jump table and an explicit
673 indirect jump instruction at the end of each opcode. Since the jump
674 instruction is at a different address for each opcode, the CPU will make a
675 separate prediction for each of these instructions, which is equivalent to
676 predicting the second opcode of each opcode pair. These predictions have
677 a much better chance to turn out valid, especially in small bytecode loops.
678
679 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000681 and potentially many more instructions (depending on the pipeline width).
682 A correctly predicted branch, however, is nearly free.
683
684 At the time of this writing, the "threaded code" version is up to 15-20%
685 faster than the normal "switch" version, depending on the compiler and the
686 CPU architecture.
687
688 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
689 because it would render the measurements invalid.
690
691
692 NOTE: care must be taken that the compiler doesn't try to "optimize" the
693 indirect jumps by sharing them between all opcodes. Such optimizations
694 can be disabled on gcc by using the -fno-gcse flag (or possibly
695 -fno-crossjumping).
696*/
697
Antoine Pitrou042b1282010-08-13 21:15:58 +0000698#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000699#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000700#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000701#endif
702
Antoine Pitrou042b1282010-08-13 21:15:58 +0000703#ifdef HAVE_COMPUTED_GOTOS
704 #ifndef USE_COMPUTED_GOTOS
705 #define USE_COMPUTED_GOTOS 1
706 #endif
707#else
708 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
709 #error "Computed gotos are not supported on this compiler."
710 #endif
711 #undef USE_COMPUTED_GOTOS
712 #define USE_COMPUTED_GOTOS 0
713#endif
714
715#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000716/* Import the static jump table */
717#include "opcode_targets.h"
718
Antoine Pitroub52ec782009-01-25 16:34:23 +0000719#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -0700720 op: \
721 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000722
Antoine Pitroub52ec782009-01-25 16:34:23 +0000723#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 { \
Eric Snowef4ac962019-02-24 15:40:47 -0800725 if (!_Py_atomic_load_relaxed(&tstate->interp->ceval.eval_breaker)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 FAST_DISPATCH(); \
727 } \
728 continue; \
729 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000730
731#ifdef LLTRACE
732#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700734 if (!lltrace && !_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300736 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300737 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000738 } \
739 goto fast_next_opcode; \
740 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000741#else
742#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700744 if (!_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300746 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300747 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000748 } \
749 goto fast_next_opcode; \
750 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000751#endif
752
753#else
Benjamin Petersonddd19492018-09-16 22:38:02 -0700754#define TARGET(op) op
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300755
Antoine Pitroub52ec782009-01-25 16:34:23 +0000756#define DISPATCH() continue
757#define FAST_DISPATCH() goto fast_next_opcode
758#endif
759
760
Neal Norwitza81d2202002-07-14 00:27:26 +0000761/* Tuple access macros */
762
763#ifndef Py_DEBUG
764#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
765#else
766#define GETITEM(v, i) PyTuple_GetItem((v), (i))
767#endif
768
Guido van Rossum374a9221991-04-04 10:40:29 +0000769/* Code access macros */
770
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300771/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600772#define INSTR_OFFSET() \
773 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300774#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300775 _Py_CODEUNIT word = *next_instr; \
776 opcode = _Py_OPCODE(word); \
777 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300778 next_instr++; \
779 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300780#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
781#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000782
Raymond Hettingerf606f872003-03-16 03:11:04 +0000783/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 Some opcodes tend to come in pairs thus making it possible to
785 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300786 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000787
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 Verifying the prediction costs a single high-speed test of a register
789 variable against a constant. If the pairing was good, then the
790 processor's own internal branch predication has a high likelihood of
791 success, resulting in a nearly zero-overhead transition to the
792 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300793 including its unpredictable switch-case branch. Combined with the
794 processor's internal branch prediction, a successful PREDICT has the
795 effect of making the two opcodes run as if they were a single new opcode
796 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000797
Georg Brandl86b2fb92008-07-16 03:43:04 +0000798 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 predictions turned-on and interpret the results as if some opcodes
800 had been combined or turn-off predictions so that the opcode frequency
801 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000802
803 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 the CPU to record separate branch prediction information for each
805 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000806
Raymond Hettingerf606f872003-03-16 03:11:04 +0000807*/
808
Antoine Pitrou042b1282010-08-13 21:15:58 +0000809#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000811#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300812#define PREDICT(op) \
813 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300814 _Py_CODEUNIT word = *next_instr; \
815 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300816 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300817 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300818 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300819 goto PRED_##op; \
820 } \
821 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000822#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300823#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000824
Raymond Hettingerf606f872003-03-16 03:11:04 +0000825
Guido van Rossum374a9221991-04-04 10:40:29 +0000826/* Stack manipulation macros */
827
Martin v. Löwis18e16552006-02-15 17:27:45 +0000828/* The stack can grow at most MAXINT deep, as co_nlocals and
829 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000830#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
831#define EMPTY() (STACK_LEVEL() == 0)
832#define TOP() (stack_pointer[-1])
833#define SECOND() (stack_pointer[-2])
834#define THIRD() (stack_pointer[-3])
835#define FOURTH() (stack_pointer[-4])
836#define PEEK(n) (stack_pointer[-(n)])
837#define SET_TOP(v) (stack_pointer[-1] = (v))
838#define SET_SECOND(v) (stack_pointer[-2] = (v))
839#define SET_THIRD(v) (stack_pointer[-3] = (v))
840#define SET_FOURTH(v) (stack_pointer[-4] = (v))
841#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
842#define BASIC_STACKADJ(n) (stack_pointer += n)
843#define BASIC_PUSH(v) (*stack_pointer++ = (v))
844#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000845
Guido van Rossum96a42c81992-01-12 02:29:51 +0000846#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000848 lltrace && prtrace(TOP(), "push")); \
849 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000851 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +0000852#define STACK_GROW(n) do { \
853 assert(n >= 0); \
854 (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000855 lltrace && prtrace(TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +0000856 assert(STACK_LEVEL() <= co->co_stacksize); \
857 } while (0)
858#define STACK_SHRINK(n) do { \
859 assert(n >= 0); \
860 (void)(lltrace && prtrace(TOP(), "stackadj")); \
861 (void)(BASIC_STACKADJ(-n)); \
862 assert(STACK_LEVEL() <= co->co_stacksize); \
863 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +0000864#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +0000865 prtrace((STACK_POINTER)[-1], "ext_pop")), \
866 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000867#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000868#define PUSH(v) BASIC_PUSH(v)
869#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +0000870#define STACK_GROW(n) BASIC_STACKADJ(n)
871#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000872#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000873#endif
874
Guido van Rossum681d79a1995-07-18 14:51:37 +0000875/* Local variable macros */
876
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000878
879/* The SETLOCAL() macro must not DECREF the local variable in-place and
880 then store the new value; it must copy the old value to a temporary
881 value, then store the new value, and then DECREF the temporary value.
882 This is because it is possible that during the DECREF the frame is
883 accessed by other code (e.g. a __del__ method or gc.collect()) and the
884 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000886 GETLOCAL(i) = value; \
887 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000888
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000889
890#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 while (STACK_LEVEL() > (b)->b_level) { \
892 PyObject *v = POP(); \
893 Py_XDECREF(v); \
894 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000895
896#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300897 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +0100899 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 assert(STACK_LEVEL() >= (b)->b_level + 3); \
901 while (STACK_LEVEL() > (b)->b_level + 3) { \
902 value = POP(); \
903 Py_XDECREF(value); \
904 } \
Mark Shannonae3087c2017-10-22 22:41:51 +0100905 exc_info = tstate->exc_info; \
906 type = exc_info->exc_type; \
907 value = exc_info->exc_value; \
908 traceback = exc_info->exc_traceback; \
909 exc_info->exc_type = POP(); \
910 exc_info->exc_value = POP(); \
911 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 Py_XDECREF(type); \
913 Py_XDECREF(value); \
914 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300915 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000916
Guido van Rossuma027efa1997-05-05 20:56:21 +0000917/* Start of code */
918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 /* push frame */
920 if (Py_EnterRecursiveCall(""))
921 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000924
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 if (tstate->use_tracing) {
926 if (tstate->c_tracefunc != NULL) {
927 /* tstate->c_tracefunc, if defined, is a
928 function that will be called on *every* entry
929 to a code block. Its return value, if not
930 None, is a function that will be called at
931 the start of each executed line of code.
932 (Actually, the function must return itself
933 in order to continue tracing.) The trace
934 functions are called with three arguments:
935 a pointer to the current frame, a string
936 indicating why the function is called, and
937 an argument which depends on the situation.
938 The global trace function is also called
939 whenever an exception is detected. */
940 if (call_trace_protected(tstate->c_tracefunc,
941 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100942 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 /* Trace function raised an error */
944 goto exit_eval_frame;
945 }
946 }
947 if (tstate->c_profilefunc != NULL) {
948 /* Similar for c_profilefunc, except it needn't
949 return itself and isn't called for "line" events */
950 if (call_trace_protected(tstate->c_profilefunc,
951 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100952 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 /* Profile function raised an error */
954 goto exit_eval_frame;
955 }
956 }
957 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000958
Łukasz Langaa785c872016-09-09 17:37:37 -0700959 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
960 dtrace_function_entry(f);
961
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 co = f->f_code;
963 names = co->co_names;
964 consts = co->co_consts;
965 fastlocals = f->f_localsplus;
966 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300967 assert(PyBytes_Check(co->co_code));
968 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +0300969 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
970 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
971 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300972 /*
973 f->f_lasti refers to the index of the last instruction,
974 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000975
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300976 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500977 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000978
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 When the PREDICT() macros are enabled, some opcode pairs follow in
980 direct succession without updating f->f_lasti. A successful
981 prediction effectively links the two codes together as if they
982 were a single new opcode; accordingly,f->f_lasti will point to
983 the first code in the pair (for instance, GET_ITER followed by
984 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300985 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300987 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300988 next_instr = first_instr;
989 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +0300990 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
991 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300992 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 stack_pointer = f->f_stacktop;
994 assert(stack_pointer != NULL);
995 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +0200996 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000997
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000998
Tim Peters5ca576e2001-06-18 22:08:13 +0000999#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001000 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001001#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001002
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001003 if (throwflag) /* support for generator.throw() */
1004 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001005
Victor Stinnerace47d72013-07-18 01:41:08 +02001006#ifdef Py_DEBUG
1007 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001008 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001009 caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +02001010 assert(!PyErr_Occurred());
1011#endif
1012
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001013main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1016 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +02001017 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001018
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 /* Do periodic things. Doing this every time through
1020 the loop would add too much overhead, so we do it
1021 only every Nth instruction. We also do it if
1022 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1023 event needs attention (e.g. a signal handler or
1024 async I/O handler); see Py_AddPendingCall() and
1025 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001026
Eric Snowef4ac962019-02-24 15:40:47 -08001027 if (_Py_atomic_load_relaxed(&(tstate->interp->ceval.eval_breaker))) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001028 opcode = _Py_OPCODE(*next_instr);
1029 if (opcode == SETUP_FINALLY ||
1030 opcode == SETUP_WITH ||
1031 opcode == BEFORE_ASYNC_WITH ||
1032 opcode == YIELD_FROM) {
1033 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001034 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001035 - If we're about to enter the 'with:'. It will prevent
1036 emitting a resource warning in the common idiom
1037 'with open(path) as file:'.
1038 - If we're about to enter the 'async with:'.
1039 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001040 *very* useful, but might help in some cases and it's
1041 traditional)
1042 - If we're resuming a chain of nested 'yield from' or
1043 'await' calls, then each frame is parked with YIELD_FROM
1044 as its next opcode. If the user hit control-C we want to
1045 wait until we've reached the innermost frame before
1046 running the signal handler and raising KeyboardInterrupt
1047 (see bpo-30039).
1048 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 goto fast_next_opcode;
1050 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001051
1052 if (_Py_atomic_load_relaxed(
1053 &_PyRuntime.ceval.signals_pending))
1054 {
1055 if (handle_signals() != 0) {
1056 goto error;
1057 }
1058 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001059 if (_Py_atomic_load_relaxed(
Eric Snowef4ac962019-02-24 15:40:47 -08001060 &(tstate->interp->ceval.pending.calls_to_do)))
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001061 {
Eric Snowef4ac962019-02-24 15:40:47 -08001062 if (_Py_MakePendingCalls(tstate->interp) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001063 goto error;
Eric Snowfdf282d2019-01-11 14:26:55 -07001064 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001066
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001067 if (_Py_atomic_load_relaxed(
1068 &_PyRuntime.ceval.gil_drop_request))
1069 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 /* Give another thread a chance */
1071 if (PyThreadState_Swap(NULL) != tstate)
1072 Py_FatalError("ceval: tstate mix-up");
1073 drop_gil(tstate);
1074
1075 /* Other threads may run now */
1076
1077 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001078
1079 /* Check if we should make a quick exit. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001080 if (_Py_IsFinalizing() &&
1081 !_Py_CURRENTLY_FINALIZING(tstate))
1082 {
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001083 drop_gil(tstate);
1084 PyThread_exit_thread();
1085 }
1086
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 if (PyThreadState_Swap(tstate) != NULL)
1088 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 }
1090 /* Check for asynchronous exceptions. */
1091 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001092 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 tstate->async_exc = NULL;
Eric Snowef4ac962019-02-24 15:40:47 -08001094 UNSIGNAL_ASYNC_EXC(tstate->interp);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001095 PyErr_SetNone(exc);
1096 Py_DECREF(exc);
1097 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 }
1099 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001100
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 fast_next_opcode:
1102 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001103
Łukasz Langaa785c872016-09-09 17:37:37 -07001104 if (PyDTrace_LINE_ENABLED())
1105 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1106
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001108
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001110 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001111 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001112 /* see maybe_call_line_trace
1113 for expository comments */
1114 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 err = maybe_call_line_trace(tstate->c_tracefunc,
1117 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001118 tstate, f,
1119 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 /* Reload possibly changed frame fields */
1121 JUMPTO(f->f_lasti);
1122 if (f->f_stacktop != NULL) {
1123 stack_pointer = f->f_stacktop;
1124 f->f_stacktop = NULL;
1125 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001126 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001128 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001130
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001132
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001133 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001134 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001135#ifdef DYNAMIC_EXECUTION_PROFILE
1136#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 dxpairs[lastopcode][opcode]++;
1138 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001139#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001141#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001142
Guido van Rossum96a42c81992-01-12 02:29:51 +00001143#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 if (lltrace) {
1147 if (HAS_ARG(opcode)) {
1148 printf("%d: %d, %d\n",
1149 f->f_lasti, opcode, oparg);
1150 }
1151 else {
1152 printf("%d: %d\n",
1153 f->f_lasti, opcode);
1154 }
1155 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001156#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001157
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001159
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001160 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001161 It is essential that any operation that fails must goto error
1162 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001163
Benjamin Petersonddd19492018-09-16 22:38:02 -07001164 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001165 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001166 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001167
Benjamin Petersonddd19492018-09-16 22:38:02 -07001168 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001169 PyObject *value = GETLOCAL(oparg);
1170 if (value == NULL) {
1171 format_exc_check_arg(PyExc_UnboundLocalError,
1172 UNBOUNDLOCAL_ERROR_MSG,
1173 PyTuple_GetItem(co->co_varnames, oparg));
1174 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001176 Py_INCREF(value);
1177 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001178 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001179 }
1180
Benjamin Petersonddd19492018-09-16 22:38:02 -07001181 case TARGET(LOAD_CONST): {
1182 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001183 PyObject *value = GETITEM(consts, oparg);
1184 Py_INCREF(value);
1185 PUSH(value);
1186 FAST_DISPATCH();
1187 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001188
Benjamin Petersonddd19492018-09-16 22:38:02 -07001189 case TARGET(STORE_FAST): {
1190 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001191 PyObject *value = POP();
1192 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001194 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001195
Benjamin Petersonddd19492018-09-16 22:38:02 -07001196 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001197 PyObject *value = POP();
1198 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001200 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001201
Benjamin Petersonddd19492018-09-16 22:38:02 -07001202 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001203 PyObject *top = TOP();
1204 PyObject *second = SECOND();
1205 SET_TOP(second);
1206 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001208 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001209
Benjamin Petersonddd19492018-09-16 22:38:02 -07001210 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001211 PyObject *top = TOP();
1212 PyObject *second = SECOND();
1213 PyObject *third = THIRD();
1214 SET_TOP(second);
1215 SET_SECOND(third);
1216 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001218 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001219
Benjamin Petersonddd19492018-09-16 22:38:02 -07001220 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001221 PyObject *top = TOP();
1222 PyObject *second = SECOND();
1223 PyObject *third = THIRD();
1224 PyObject *fourth = FOURTH();
1225 SET_TOP(second);
1226 SET_SECOND(third);
1227 SET_THIRD(fourth);
1228 SET_FOURTH(top);
1229 FAST_DISPATCH();
1230 }
1231
Benjamin Petersonddd19492018-09-16 22:38:02 -07001232 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001233 PyObject *top = TOP();
1234 Py_INCREF(top);
1235 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001237 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001238
Benjamin Petersonddd19492018-09-16 22:38:02 -07001239 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001240 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001241 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001242 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001243 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001244 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001245 SET_TOP(top);
1246 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001247 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001248 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001249
Benjamin Petersonddd19492018-09-16 22:38:02 -07001250 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001251 PyObject *value = TOP();
1252 PyObject *res = PyNumber_Positive(value);
1253 Py_DECREF(value);
1254 SET_TOP(res);
1255 if (res == NULL)
1256 goto error;
1257 DISPATCH();
1258 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001259
Benjamin Petersonddd19492018-09-16 22:38:02 -07001260 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001261 PyObject *value = TOP();
1262 PyObject *res = PyNumber_Negative(value);
1263 Py_DECREF(value);
1264 SET_TOP(res);
1265 if (res == NULL)
1266 goto error;
1267 DISPATCH();
1268 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001269
Benjamin Petersonddd19492018-09-16 22:38:02 -07001270 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001271 PyObject *value = TOP();
1272 int err = PyObject_IsTrue(value);
1273 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 if (err == 0) {
1275 Py_INCREF(Py_True);
1276 SET_TOP(Py_True);
1277 DISPATCH();
1278 }
1279 else if (err > 0) {
1280 Py_INCREF(Py_False);
1281 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 DISPATCH();
1283 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001284 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001285 goto error;
1286 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001287
Benjamin Petersonddd19492018-09-16 22:38:02 -07001288 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001289 PyObject *value = TOP();
1290 PyObject *res = PyNumber_Invert(value);
1291 Py_DECREF(value);
1292 SET_TOP(res);
1293 if (res == NULL)
1294 goto error;
1295 DISPATCH();
1296 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001297
Benjamin Petersonddd19492018-09-16 22:38:02 -07001298 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001299 PyObject *exp = POP();
1300 PyObject *base = TOP();
1301 PyObject *res = PyNumber_Power(base, exp, Py_None);
1302 Py_DECREF(base);
1303 Py_DECREF(exp);
1304 SET_TOP(res);
1305 if (res == NULL)
1306 goto error;
1307 DISPATCH();
1308 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001309
Benjamin Petersonddd19492018-09-16 22:38:02 -07001310 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001311 PyObject *right = POP();
1312 PyObject *left = TOP();
1313 PyObject *res = PyNumber_Multiply(left, right);
1314 Py_DECREF(left);
1315 Py_DECREF(right);
1316 SET_TOP(res);
1317 if (res == NULL)
1318 goto error;
1319 DISPATCH();
1320 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001321
Benjamin Petersonddd19492018-09-16 22:38:02 -07001322 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001323 PyObject *right = POP();
1324 PyObject *left = TOP();
1325 PyObject *res = PyNumber_MatrixMultiply(left, right);
1326 Py_DECREF(left);
1327 Py_DECREF(right);
1328 SET_TOP(res);
1329 if (res == NULL)
1330 goto error;
1331 DISPATCH();
1332 }
1333
Benjamin Petersonddd19492018-09-16 22:38:02 -07001334 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001335 PyObject *divisor = POP();
1336 PyObject *dividend = TOP();
1337 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1338 Py_DECREF(dividend);
1339 Py_DECREF(divisor);
1340 SET_TOP(quotient);
1341 if (quotient == NULL)
1342 goto error;
1343 DISPATCH();
1344 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001345
Benjamin Petersonddd19492018-09-16 22:38:02 -07001346 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001347 PyObject *divisor = POP();
1348 PyObject *dividend = TOP();
1349 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1350 Py_DECREF(dividend);
1351 Py_DECREF(divisor);
1352 SET_TOP(quotient);
1353 if (quotient == NULL)
1354 goto error;
1355 DISPATCH();
1356 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001357
Benjamin Petersonddd19492018-09-16 22:38:02 -07001358 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001359 PyObject *divisor = POP();
1360 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001361 PyObject *res;
1362 if (PyUnicode_CheckExact(dividend) && (
1363 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1364 // fast path; string formatting, but not if the RHS is a str subclass
1365 // (see issue28598)
1366 res = PyUnicode_Format(dividend, divisor);
1367 } else {
1368 res = PyNumber_Remainder(dividend, divisor);
1369 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001370 Py_DECREF(divisor);
1371 Py_DECREF(dividend);
1372 SET_TOP(res);
1373 if (res == NULL)
1374 goto error;
1375 DISPATCH();
1376 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001377
Benjamin Petersonddd19492018-09-16 22:38:02 -07001378 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001379 PyObject *right = POP();
1380 PyObject *left = TOP();
1381 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001382 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1383 CPython using bytecode, it is simply worthless.
1384 See http://bugs.python.org/issue21955 and
1385 http://bugs.python.org/issue10044 for the discussion. In short,
1386 no patch shown any impact on a realistic benchmark, only a minor
1387 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001388 if (PyUnicode_CheckExact(left) &&
1389 PyUnicode_CheckExact(right)) {
1390 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001391 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001392 }
1393 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001394 sum = PyNumber_Add(left, right);
1395 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001396 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001397 Py_DECREF(right);
1398 SET_TOP(sum);
1399 if (sum == NULL)
1400 goto error;
1401 DISPATCH();
1402 }
1403
Benjamin Petersonddd19492018-09-16 22:38:02 -07001404 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001405 PyObject *right = POP();
1406 PyObject *left = TOP();
1407 PyObject *diff = PyNumber_Subtract(left, right);
1408 Py_DECREF(right);
1409 Py_DECREF(left);
1410 SET_TOP(diff);
1411 if (diff == NULL)
1412 goto error;
1413 DISPATCH();
1414 }
1415
Benjamin Petersonddd19492018-09-16 22:38:02 -07001416 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001417 PyObject *sub = POP();
1418 PyObject *container = TOP();
1419 PyObject *res = PyObject_GetItem(container, sub);
1420 Py_DECREF(container);
1421 Py_DECREF(sub);
1422 SET_TOP(res);
1423 if (res == NULL)
1424 goto error;
1425 DISPATCH();
1426 }
1427
Benjamin Petersonddd19492018-09-16 22:38:02 -07001428 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001429 PyObject *right = POP();
1430 PyObject *left = TOP();
1431 PyObject *res = PyNumber_Lshift(left, right);
1432 Py_DECREF(left);
1433 Py_DECREF(right);
1434 SET_TOP(res);
1435 if (res == NULL)
1436 goto error;
1437 DISPATCH();
1438 }
1439
Benjamin Petersonddd19492018-09-16 22:38:02 -07001440 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001441 PyObject *right = POP();
1442 PyObject *left = TOP();
1443 PyObject *res = PyNumber_Rshift(left, right);
1444 Py_DECREF(left);
1445 Py_DECREF(right);
1446 SET_TOP(res);
1447 if (res == NULL)
1448 goto error;
1449 DISPATCH();
1450 }
1451
Benjamin Petersonddd19492018-09-16 22:38:02 -07001452 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001453 PyObject *right = POP();
1454 PyObject *left = TOP();
1455 PyObject *res = PyNumber_And(left, right);
1456 Py_DECREF(left);
1457 Py_DECREF(right);
1458 SET_TOP(res);
1459 if (res == NULL)
1460 goto error;
1461 DISPATCH();
1462 }
1463
Benjamin Petersonddd19492018-09-16 22:38:02 -07001464 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001465 PyObject *right = POP();
1466 PyObject *left = TOP();
1467 PyObject *res = PyNumber_Xor(left, right);
1468 Py_DECREF(left);
1469 Py_DECREF(right);
1470 SET_TOP(res);
1471 if (res == NULL)
1472 goto error;
1473 DISPATCH();
1474 }
1475
Benjamin Petersonddd19492018-09-16 22:38:02 -07001476 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001477 PyObject *right = POP();
1478 PyObject *left = TOP();
1479 PyObject *res = PyNumber_Or(left, right);
1480 Py_DECREF(left);
1481 Py_DECREF(right);
1482 SET_TOP(res);
1483 if (res == NULL)
1484 goto error;
1485 DISPATCH();
1486 }
1487
Benjamin Petersonddd19492018-09-16 22:38:02 -07001488 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001489 PyObject *v = POP();
1490 PyObject *list = PEEK(oparg);
1491 int err;
1492 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001493 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001494 if (err != 0)
1495 goto error;
1496 PREDICT(JUMP_ABSOLUTE);
1497 DISPATCH();
1498 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001499
Benjamin Petersonddd19492018-09-16 22:38:02 -07001500 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001501 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001502 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001503 int err;
1504 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001505 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001506 if (err != 0)
1507 goto error;
1508 PREDICT(JUMP_ABSOLUTE);
1509 DISPATCH();
1510 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001511
Benjamin Petersonddd19492018-09-16 22:38:02 -07001512 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001513 PyObject *exp = POP();
1514 PyObject *base = TOP();
1515 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1516 Py_DECREF(base);
1517 Py_DECREF(exp);
1518 SET_TOP(res);
1519 if (res == NULL)
1520 goto error;
1521 DISPATCH();
1522 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001523
Benjamin Petersonddd19492018-09-16 22:38:02 -07001524 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001525 PyObject *right = POP();
1526 PyObject *left = TOP();
1527 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1528 Py_DECREF(left);
1529 Py_DECREF(right);
1530 SET_TOP(res);
1531 if (res == NULL)
1532 goto error;
1533 DISPATCH();
1534 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001535
Benjamin Petersonddd19492018-09-16 22:38:02 -07001536 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001537 PyObject *right = POP();
1538 PyObject *left = TOP();
1539 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1540 Py_DECREF(left);
1541 Py_DECREF(right);
1542 SET_TOP(res);
1543 if (res == NULL)
1544 goto error;
1545 DISPATCH();
1546 }
1547
Benjamin Petersonddd19492018-09-16 22:38:02 -07001548 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001549 PyObject *divisor = POP();
1550 PyObject *dividend = TOP();
1551 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1552 Py_DECREF(dividend);
1553 Py_DECREF(divisor);
1554 SET_TOP(quotient);
1555 if (quotient == NULL)
1556 goto error;
1557 DISPATCH();
1558 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001559
Benjamin Petersonddd19492018-09-16 22:38:02 -07001560 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001561 PyObject *divisor = POP();
1562 PyObject *dividend = TOP();
1563 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1564 Py_DECREF(dividend);
1565 Py_DECREF(divisor);
1566 SET_TOP(quotient);
1567 if (quotient == NULL)
1568 goto error;
1569 DISPATCH();
1570 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001571
Benjamin Petersonddd19492018-09-16 22:38:02 -07001572 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001573 PyObject *right = POP();
1574 PyObject *left = TOP();
1575 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1576 Py_DECREF(left);
1577 Py_DECREF(right);
1578 SET_TOP(mod);
1579 if (mod == NULL)
1580 goto error;
1581 DISPATCH();
1582 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001583
Benjamin Petersonddd19492018-09-16 22:38:02 -07001584 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001585 PyObject *right = POP();
1586 PyObject *left = TOP();
1587 PyObject *sum;
1588 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1589 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001590 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001591 }
1592 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001593 sum = PyNumber_InPlaceAdd(left, right);
1594 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001595 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001596 Py_DECREF(right);
1597 SET_TOP(sum);
1598 if (sum == NULL)
1599 goto error;
1600 DISPATCH();
1601 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001602
Benjamin Petersonddd19492018-09-16 22:38:02 -07001603 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001604 PyObject *right = POP();
1605 PyObject *left = TOP();
1606 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1607 Py_DECREF(left);
1608 Py_DECREF(right);
1609 SET_TOP(diff);
1610 if (diff == NULL)
1611 goto error;
1612 DISPATCH();
1613 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001614
Benjamin Petersonddd19492018-09-16 22:38:02 -07001615 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001616 PyObject *right = POP();
1617 PyObject *left = TOP();
1618 PyObject *res = PyNumber_InPlaceLshift(left, right);
1619 Py_DECREF(left);
1620 Py_DECREF(right);
1621 SET_TOP(res);
1622 if (res == NULL)
1623 goto error;
1624 DISPATCH();
1625 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001626
Benjamin Petersonddd19492018-09-16 22:38:02 -07001627 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001628 PyObject *right = POP();
1629 PyObject *left = TOP();
1630 PyObject *res = PyNumber_InPlaceRshift(left, right);
1631 Py_DECREF(left);
1632 Py_DECREF(right);
1633 SET_TOP(res);
1634 if (res == NULL)
1635 goto error;
1636 DISPATCH();
1637 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001638
Benjamin Petersonddd19492018-09-16 22:38:02 -07001639 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001640 PyObject *right = POP();
1641 PyObject *left = TOP();
1642 PyObject *res = PyNumber_InPlaceAnd(left, right);
1643 Py_DECREF(left);
1644 Py_DECREF(right);
1645 SET_TOP(res);
1646 if (res == NULL)
1647 goto error;
1648 DISPATCH();
1649 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001650
Benjamin Petersonddd19492018-09-16 22:38:02 -07001651 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001652 PyObject *right = POP();
1653 PyObject *left = TOP();
1654 PyObject *res = PyNumber_InPlaceXor(left, right);
1655 Py_DECREF(left);
1656 Py_DECREF(right);
1657 SET_TOP(res);
1658 if (res == NULL)
1659 goto error;
1660 DISPATCH();
1661 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001662
Benjamin Petersonddd19492018-09-16 22:38:02 -07001663 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001664 PyObject *right = POP();
1665 PyObject *left = TOP();
1666 PyObject *res = PyNumber_InPlaceOr(left, right);
1667 Py_DECREF(left);
1668 Py_DECREF(right);
1669 SET_TOP(res);
1670 if (res == NULL)
1671 goto error;
1672 DISPATCH();
1673 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001674
Benjamin Petersonddd19492018-09-16 22:38:02 -07001675 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001676 PyObject *sub = TOP();
1677 PyObject *container = SECOND();
1678 PyObject *v = THIRD();
1679 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001680 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001681 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001682 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001684 Py_DECREF(container);
1685 Py_DECREF(sub);
1686 if (err != 0)
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(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001692 PyObject *sub = TOP();
1693 PyObject *container = SECOND();
1694 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001695 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00001696 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001697 err = PyObject_DelItem(container, sub);
1698 Py_DECREF(container);
1699 Py_DECREF(sub);
1700 if (err != 0)
1701 goto error;
1702 DISPATCH();
1703 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001704
Benjamin Petersonddd19492018-09-16 22:38:02 -07001705 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01001706 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001707 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001708 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001709 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001710 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001711 PyErr_SetString(PyExc_RuntimeError,
1712 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001713 Py_DECREF(value);
1714 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001715 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001716 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001717 Py_DECREF(value);
1718 if (res == NULL)
1719 goto error;
1720 Py_DECREF(res);
1721 DISPATCH();
1722 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001723
Benjamin Petersonddd19492018-09-16 22:38:02 -07001724 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001725 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001726 switch (oparg) {
1727 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001728 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001729 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001730 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001731 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001732 /* fall through */
1733 case 0:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001734 if (do_raise(exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001735 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001736 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 break;
1738 default:
1739 PyErr_SetString(PyExc_SystemError,
1740 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001741 break;
1742 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001743 goto error;
1744 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001745
Benjamin Petersonddd19492018-09-16 22:38:02 -07001746 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001747 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001748 assert(f->f_iblock == 0);
1749 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001750 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001751
Benjamin Petersonddd19492018-09-16 22:38:02 -07001752 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001753 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001754 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001755 PyObject *obj = TOP();
1756 PyTypeObject *type = Py_TYPE(obj);
1757
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001758 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001759 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001760 }
Yury Selivanov75445082015-05-11 22:57:16 -04001761
1762 if (getter != NULL) {
1763 iter = (*getter)(obj);
1764 Py_DECREF(obj);
1765 if (iter == NULL) {
1766 SET_TOP(NULL);
1767 goto error;
1768 }
1769 }
1770 else {
1771 SET_TOP(NULL);
1772 PyErr_Format(
1773 PyExc_TypeError,
1774 "'async for' requires an object with "
1775 "__aiter__ method, got %.100s",
1776 type->tp_name);
1777 Py_DECREF(obj);
1778 goto error;
1779 }
1780
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001781 if (Py_TYPE(iter)->tp_as_async == NULL ||
1782 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001783
Yury Selivanov398ff912017-03-02 22:20:00 -05001784 SET_TOP(NULL);
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001785 PyErr_Format(
1786 PyExc_TypeError,
1787 "'async for' received an object from __aiter__ "
1788 "that does not implement __anext__: %.100s",
1789 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001790 Py_DECREF(iter);
1791 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001792 }
1793
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001794 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001795 DISPATCH();
1796 }
1797
Benjamin Petersonddd19492018-09-16 22:38:02 -07001798 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001799 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001800 PyObject *next_iter = NULL;
1801 PyObject *awaitable = NULL;
1802 PyObject *aiter = TOP();
1803 PyTypeObject *type = Py_TYPE(aiter);
1804
Yury Selivanoveb636452016-09-08 22:01:51 -07001805 if (PyAsyncGen_CheckExact(aiter)) {
1806 awaitable = type->tp_as_async->am_anext(aiter);
1807 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001808 goto error;
1809 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001810 } else {
1811 if (type->tp_as_async != NULL){
1812 getter = type->tp_as_async->am_anext;
1813 }
Yury Selivanov75445082015-05-11 22:57:16 -04001814
Yury Selivanoveb636452016-09-08 22:01:51 -07001815 if (getter != NULL) {
1816 next_iter = (*getter)(aiter);
1817 if (next_iter == NULL) {
1818 goto error;
1819 }
1820 }
1821 else {
1822 PyErr_Format(
1823 PyExc_TypeError,
1824 "'async for' requires an iterator with "
1825 "__anext__ method, got %.100s",
1826 type->tp_name);
1827 goto error;
1828 }
Yury Selivanov75445082015-05-11 22:57:16 -04001829
Yury Selivanoveb636452016-09-08 22:01:51 -07001830 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1831 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001832 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001833 PyExc_TypeError,
1834 "'async for' received an invalid object "
1835 "from __anext__: %.100s",
1836 Py_TYPE(next_iter)->tp_name);
1837
1838 Py_DECREF(next_iter);
1839 goto error;
1840 } else {
1841 Py_DECREF(next_iter);
1842 }
1843 }
Yury Selivanov75445082015-05-11 22:57:16 -04001844
1845 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001846 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001847 DISPATCH();
1848 }
1849
Benjamin Petersonddd19492018-09-16 22:38:02 -07001850 case TARGET(GET_AWAITABLE): {
1851 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04001852 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04001853 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04001854
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03001855 if (iter == NULL) {
1856 format_awaitable_error(Py_TYPE(iterable),
1857 _Py_OPCODE(next_instr[-2]));
1858 }
1859
Yury Selivanov75445082015-05-11 22:57:16 -04001860 Py_DECREF(iterable);
1861
Yury Selivanovc724bae2016-03-02 11:30:46 -05001862 if (iter != NULL && PyCoro_CheckExact(iter)) {
1863 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
1864 if (yf != NULL) {
1865 /* `iter` is a coroutine object that is being
1866 awaited, `yf` is a pointer to the current awaitable
1867 being awaited on. */
1868 Py_DECREF(yf);
1869 Py_CLEAR(iter);
1870 PyErr_SetString(
1871 PyExc_RuntimeError,
1872 "coroutine is being awaited already");
1873 /* The code below jumps to `error` if `iter` is NULL. */
1874 }
1875 }
1876
Yury Selivanov75445082015-05-11 22:57:16 -04001877 SET_TOP(iter); /* Even if it's NULL */
1878
1879 if (iter == NULL) {
1880 goto error;
1881 }
1882
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001883 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001884 DISPATCH();
1885 }
1886
Benjamin Petersonddd19492018-09-16 22:38:02 -07001887 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001888 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001889 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001890 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001891 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
1892 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001893 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001894 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001895 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001896 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001897 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001898 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001899 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001900 Py_DECREF(v);
1901 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001902 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08001903 if (tstate->c_tracefunc != NULL
1904 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001905 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10001906 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001907 if (err < 0)
1908 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001909 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001910 SET_TOP(val);
1911 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001912 }
Martin Panter95f53c12016-07-18 08:23:26 +00001913 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001914 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001915 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01001916 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03001917 f->f_lasti -= sizeof(_Py_CODEUNIT);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001918 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001919 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001920
Benjamin Petersonddd19492018-09-16 22:38:02 -07001921 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001922 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07001923
1924 if (co->co_flags & CO_ASYNC_GENERATOR) {
1925 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
1926 Py_DECREF(retval);
1927 if (w == NULL) {
1928 retval = NULL;
1929 goto error;
1930 }
1931 retval = w;
1932 }
1933
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001934 f->f_stacktop = stack_pointer;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001935 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001936 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001937
Benjamin Petersonddd19492018-09-16 22:38:02 -07001938 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001939 PyObject *type, *value, *traceback;
1940 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001941 PyTryBlock *b = PyFrame_BlockPop(f);
1942 if (b->b_type != EXCEPT_HANDLER) {
1943 PyErr_SetString(PyExc_SystemError,
1944 "popped block is not an except handler");
1945 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001946 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001947 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
1948 STACK_LEVEL() <= (b)->b_level + 4);
1949 exc_info = tstate->exc_info;
1950 type = exc_info->exc_type;
1951 value = exc_info->exc_value;
1952 traceback = exc_info->exc_traceback;
1953 exc_info->exc_type = POP();
1954 exc_info->exc_value = POP();
1955 exc_info->exc_traceback = POP();
1956 Py_XDECREF(type);
1957 Py_XDECREF(value);
1958 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001959 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001960 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001961
Benjamin Petersonddd19492018-09-16 22:38:02 -07001962 case TARGET(POP_BLOCK): {
1963 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001964 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001965 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001966 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001967
Benjamin Petersonddd19492018-09-16 22:38:02 -07001968 case TARGET(POP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001969 /* If oparg is 0 at the top of the stack are 1 or 6 values:
1970 Either:
1971 - TOP = NULL or an integer
1972 or:
1973 - (TOP, SECOND, THIRD) = exc_info()
1974 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
1975
1976 If oparg is 1 the value for 'return' was additionally pushed
1977 at the top of the stack.
1978 */
1979 PyObject *res = NULL;
1980 if (oparg) {
1981 res = POP();
1982 }
1983 PyObject *exc = POP();
1984 if (exc == NULL || PyLong_CheckExact(exc)) {
1985 Py_XDECREF(exc);
1986 }
1987 else {
1988 Py_DECREF(exc);
1989 Py_DECREF(POP());
1990 Py_DECREF(POP());
1991
1992 PyObject *type, *value, *traceback;
1993 _PyErr_StackItem *exc_info;
1994 PyTryBlock *b = PyFrame_BlockPop(f);
1995 if (b->b_type != EXCEPT_HANDLER) {
1996 PyErr_SetString(PyExc_SystemError,
1997 "popped block is not an except handler");
1998 Py_XDECREF(res);
1999 goto error;
2000 }
2001 assert(STACK_LEVEL() == (b)->b_level + 3);
2002 exc_info = tstate->exc_info;
2003 type = exc_info->exc_type;
2004 value = exc_info->exc_value;
2005 traceback = exc_info->exc_traceback;
2006 exc_info->exc_type = POP();
2007 exc_info->exc_value = POP();
2008 exc_info->exc_traceback = POP();
2009 Py_XDECREF(type);
2010 Py_XDECREF(value);
2011 Py_XDECREF(traceback);
2012 }
2013 if (oparg) {
2014 PUSH(res);
2015 }
2016 DISPATCH();
2017 }
2018
Benjamin Petersonddd19492018-09-16 22:38:02 -07002019 case TARGET(CALL_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002020 PyObject *ret = PyLong_FromLong(INSTR_OFFSET());
2021 if (ret == NULL) {
2022 goto error;
2023 }
2024 PUSH(ret);
2025 JUMPBY(oparg);
2026 FAST_DISPATCH();
2027 }
2028
Benjamin Petersonddd19492018-09-16 22:38:02 -07002029 case TARGET(BEGIN_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002030 /* Push NULL onto the stack for using it in END_FINALLY,
2031 POP_FINALLY, WITH_CLEANUP_START and WITH_CLEANUP_FINISH.
2032 */
2033 PUSH(NULL);
2034 FAST_DISPATCH();
2035 }
2036
Benjamin Petersonddd19492018-09-16 22:38:02 -07002037 case TARGET(END_FINALLY): {
2038 PREDICTED(END_FINALLY);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002039 /* At the top of the stack are 1 or 6 values:
2040 Either:
2041 - TOP = NULL or an integer
2042 or:
2043 - (TOP, SECOND, THIRD) = exc_info()
2044 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2045 */
2046 PyObject *exc = POP();
2047 if (exc == NULL) {
2048 FAST_DISPATCH();
2049 }
2050 else if (PyLong_CheckExact(exc)) {
2051 int ret = _PyLong_AsInt(exc);
2052 Py_DECREF(exc);
2053 if (ret == -1 && PyErr_Occurred()) {
2054 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002055 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002056 JUMPTO(ret);
2057 FAST_DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002058 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002059 else {
2060 assert(PyExceptionClass_Check(exc));
2061 PyObject *val = POP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002062 PyObject *tb = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002063 PyErr_Restore(exc, val, tb);
2064 goto exception_unwind;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002065 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002066 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002067
Benjamin Petersonddd19492018-09-16 22:38:02 -07002068 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002069 PyObject *exc = POP();
2070 assert(PyExceptionClass_Check(exc));
2071 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2072 PyTryBlock *b = PyFrame_BlockPop(f);
2073 assert(b->b_type == EXCEPT_HANDLER);
2074 Py_DECREF(exc);
2075 UNWIND_EXCEPT_HANDLER(b);
2076 Py_DECREF(POP());
2077 JUMPBY(oparg);
2078 FAST_DISPATCH();
2079 }
2080 else {
2081 PyObject *val = POP();
2082 PyObject *tb = POP();
2083 PyErr_Restore(exc, val, tb);
2084 goto exception_unwind;
2085 }
2086 }
2087
Benjamin Petersonddd19492018-09-16 22:38:02 -07002088 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002089 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002090
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002091 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002092 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002093 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002094 if (bc == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002095 if (!PyErr_Occurred()) {
2096 PyErr_SetString(PyExc_NameError,
2097 "__build_class__ not found");
2098 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002099 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002100 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002101 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002102 }
2103 else {
2104 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2105 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002106 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002107 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2108 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002109 if (PyErr_ExceptionMatches(PyExc_KeyError))
2110 PyErr_SetString(PyExc_NameError,
2111 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002112 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002113 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002114 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002115 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002116 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002117 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002118
Benjamin Petersonddd19492018-09-16 22:38:02 -07002119 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002120 PyObject *name = GETITEM(names, oparg);
2121 PyObject *v = POP();
2122 PyObject *ns = f->f_locals;
2123 int err;
2124 if (ns == NULL) {
2125 PyErr_Format(PyExc_SystemError,
2126 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002127 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002128 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002129 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002130 if (PyDict_CheckExact(ns))
2131 err = PyDict_SetItem(ns, name, v);
2132 else
2133 err = PyObject_SetItem(ns, name, v);
2134 Py_DECREF(v);
2135 if (err != 0)
2136 goto error;
2137 DISPATCH();
2138 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002139
Benjamin Petersonddd19492018-09-16 22:38:02 -07002140 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002141 PyObject *name = GETITEM(names, oparg);
2142 PyObject *ns = f->f_locals;
2143 int err;
2144 if (ns == NULL) {
2145 PyErr_Format(PyExc_SystemError,
2146 "no locals when deleting %R", name);
2147 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002148 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002149 err = PyObject_DelItem(ns, name);
2150 if (err != 0) {
2151 format_exc_check_arg(PyExc_NameError,
2152 NAME_ERROR_MSG,
2153 name);
2154 goto error;
2155 }
2156 DISPATCH();
2157 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002158
Benjamin Petersonddd19492018-09-16 22:38:02 -07002159 case TARGET(UNPACK_SEQUENCE): {
2160 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002161 PyObject *seq = POP(), *item, **items;
2162 if (PyTuple_CheckExact(seq) &&
2163 PyTuple_GET_SIZE(seq) == oparg) {
2164 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002165 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002166 item = items[oparg];
2167 Py_INCREF(item);
2168 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002169 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002170 } else if (PyList_CheckExact(seq) &&
2171 PyList_GET_SIZE(seq) == oparg) {
2172 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002173 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002174 item = items[oparg];
2175 Py_INCREF(item);
2176 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002177 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002178 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002179 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002180 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002181 } else {
2182 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002183 Py_DECREF(seq);
2184 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002185 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002186 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002187 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002188 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002189
Benjamin Petersonddd19492018-09-16 22:38:02 -07002190 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002191 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2192 PyObject *seq = POP();
2193
2194 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2195 stack_pointer + totalargs)) {
2196 stack_pointer += totalargs;
2197 } else {
2198 Py_DECREF(seq);
2199 goto error;
2200 }
2201 Py_DECREF(seq);
2202 DISPATCH();
2203 }
2204
Benjamin Petersonddd19492018-09-16 22:38:02 -07002205 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002206 PyObject *name = GETITEM(names, oparg);
2207 PyObject *owner = TOP();
2208 PyObject *v = SECOND();
2209 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002210 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002211 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002212 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002213 Py_DECREF(owner);
2214 if (err != 0)
2215 goto error;
2216 DISPATCH();
2217 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002218
Benjamin Petersonddd19492018-09-16 22:38:02 -07002219 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002220 PyObject *name = GETITEM(names, oparg);
2221 PyObject *owner = POP();
2222 int err;
2223 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2224 Py_DECREF(owner);
2225 if (err != 0)
2226 goto error;
2227 DISPATCH();
2228 }
2229
Benjamin Petersonddd19492018-09-16 22:38:02 -07002230 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002231 PyObject *name = GETITEM(names, oparg);
2232 PyObject *v = POP();
2233 int err;
2234 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002235 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002236 if (err != 0)
2237 goto error;
2238 DISPATCH();
2239 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002240
Benjamin Petersonddd19492018-09-16 22:38:02 -07002241 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002242 PyObject *name = GETITEM(names, oparg);
2243 int err;
2244 err = PyDict_DelItem(f->f_globals, name);
2245 if (err != 0) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002246 if (PyErr_ExceptionMatches(PyExc_KeyError)) {
2247 format_exc_check_arg(
2248 PyExc_NameError, NAME_ERROR_MSG, name);
2249 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002250 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002251 }
2252 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002253 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002254
Benjamin Petersonddd19492018-09-16 22:38:02 -07002255 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002256 PyObject *name = GETITEM(names, oparg);
2257 PyObject *locals = f->f_locals;
2258 PyObject *v;
2259 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002260 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002261 "no locals when loading %R", name);
2262 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002263 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002264 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002265 v = PyDict_GetItemWithError(locals, name);
2266 if (v != NULL) {
2267 Py_INCREF(v);
2268 }
2269 else if (PyErr_Occurred()) {
2270 goto error;
2271 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002272 }
2273 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002274 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002275 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002276 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2277 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 PyErr_Clear();
2279 }
2280 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002281 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002282 v = PyDict_GetItemWithError(f->f_globals, name);
2283 if (v != NULL) {
2284 Py_INCREF(v);
2285 }
2286 else if (PyErr_Occurred()) {
2287 goto error;
2288 }
2289 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002290 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002291 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002292 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002293 if (!PyErr_Occurred()) {
2294 format_exc_check_arg(
Victor Stinnerb0b22422012-04-19 00:57:45 +02002295 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002296 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002297 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002298 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002299 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002300 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002301 }
2302 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002303 v = PyObject_GetItem(f->f_builtins, name);
2304 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002305 if (PyErr_ExceptionMatches(PyExc_KeyError))
2306 format_exc_check_arg(
2307 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002308 NAME_ERROR_MSG, name);
2309 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002310 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002311 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002312 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002313 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002314 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002315 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002316 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002317
Benjamin Petersonddd19492018-09-16 22:38:02 -07002318 case TARGET(LOAD_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002319 PyObject *name = GETITEM(names, oparg);
2320 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002321 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002322 && PyDict_CheckExact(f->f_builtins))
2323 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002324 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002325 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002326 name);
2327 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002328 if (!_PyErr_OCCURRED()) {
2329 /* _PyDict_LoadGlobal() returns NULL without raising
2330 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002331 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002332 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002333 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002334 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002335 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002336 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002337 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002338 else {
2339 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002340
2341 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002342 v = PyObject_GetItem(f->f_globals, name);
2343 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002344 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2345 goto error;
2346 PyErr_Clear();
2347
Victor Stinnerb4efc962015-11-20 09:24:02 +01002348 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002349 v = PyObject_GetItem(f->f_builtins, name);
2350 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002351 if (PyErr_ExceptionMatches(PyExc_KeyError))
2352 format_exc_check_arg(
2353 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002354 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002355 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002356 }
2357 }
2358 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002359 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002360 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002361 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002362
Benjamin Petersonddd19492018-09-16 22:38:02 -07002363 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002364 PyObject *v = GETLOCAL(oparg);
2365 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366 SETLOCAL(oparg, NULL);
2367 DISPATCH();
2368 }
2369 format_exc_check_arg(
2370 PyExc_UnboundLocalError,
2371 UNBOUNDLOCAL_ERROR_MSG,
2372 PyTuple_GetItem(co->co_varnames, oparg)
2373 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002374 goto error;
2375 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002376
Benjamin Petersonddd19492018-09-16 22:38:02 -07002377 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002378 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002379 PyObject *oldobj = PyCell_GET(cell);
2380 if (oldobj != NULL) {
2381 PyCell_SET(cell, NULL);
2382 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002383 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002384 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002385 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002386 goto error;
2387 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002388
Benjamin Petersonddd19492018-09-16 22:38:02 -07002389 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002390 PyObject *cell = freevars[oparg];
2391 Py_INCREF(cell);
2392 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002393 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002394 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002395
Benjamin Petersonddd19492018-09-16 22:38:02 -07002396 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002397 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002398 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002399 assert(locals);
2400 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2401 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2402 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2403 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2404 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002405 value = PyDict_GetItemWithError(locals, name);
2406 if (value != NULL) {
2407 Py_INCREF(value);
2408 }
2409 else if (PyErr_Occurred()) {
2410 goto error;
2411 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002412 }
2413 else {
2414 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002415 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002416 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2417 goto error;
2418 PyErr_Clear();
2419 }
2420 }
2421 if (!value) {
2422 PyObject *cell = freevars[oparg];
2423 value = PyCell_GET(cell);
2424 if (value == NULL) {
2425 format_exc_unbound(co, oparg);
2426 goto error;
2427 }
2428 Py_INCREF(value);
2429 }
2430 PUSH(value);
2431 DISPATCH();
2432 }
2433
Benjamin Petersonddd19492018-09-16 22:38:02 -07002434 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002435 PyObject *cell = freevars[oparg];
2436 PyObject *value = PyCell_GET(cell);
2437 if (value == NULL) {
2438 format_exc_unbound(co, oparg);
2439 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002440 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002441 Py_INCREF(value);
2442 PUSH(value);
2443 DISPATCH();
2444 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002445
Benjamin Petersonddd19492018-09-16 22:38:02 -07002446 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002447 PyObject *v = POP();
2448 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002449 PyObject *oldobj = PyCell_GET(cell);
2450 PyCell_SET(cell, v);
2451 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002452 DISPATCH();
2453 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002454
Benjamin Petersonddd19492018-09-16 22:38:02 -07002455 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002456 PyObject *str;
2457 PyObject *empty = PyUnicode_New(0, 0);
2458 if (empty == NULL) {
2459 goto error;
2460 }
2461 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2462 Py_DECREF(empty);
2463 if (str == NULL)
2464 goto error;
2465 while (--oparg >= 0) {
2466 PyObject *item = POP();
2467 Py_DECREF(item);
2468 }
2469 PUSH(str);
2470 DISPATCH();
2471 }
2472
Benjamin Petersonddd19492018-09-16 22:38:02 -07002473 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002474 PyObject *tup = PyTuple_New(oparg);
2475 if (tup == NULL)
2476 goto error;
2477 while (--oparg >= 0) {
2478 PyObject *item = POP();
2479 PyTuple_SET_ITEM(tup, oparg, item);
2480 }
2481 PUSH(tup);
2482 DISPATCH();
2483 }
2484
Benjamin Petersonddd19492018-09-16 22:38:02 -07002485 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002486 PyObject *list = PyList_New(oparg);
2487 if (list == NULL)
2488 goto error;
2489 while (--oparg >= 0) {
2490 PyObject *item = POP();
2491 PyList_SET_ITEM(list, oparg, item);
2492 }
2493 PUSH(list);
2494 DISPATCH();
2495 }
2496
Benjamin Petersonddd19492018-09-16 22:38:02 -07002497 case TARGET(BUILD_TUPLE_UNPACK_WITH_CALL):
2498 case TARGET(BUILD_TUPLE_UNPACK):
2499 case TARGET(BUILD_LIST_UNPACK): {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002500 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002501 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002502 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002503 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002504
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002505 if (sum == NULL)
2506 goto error;
2507
2508 for (i = oparg; i > 0; i--) {
2509 PyObject *none_val;
2510
2511 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2512 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002513 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002514 PyErr_ExceptionMatches(PyExc_TypeError))
2515 {
2516 check_args_iterable(PEEK(1 + oparg), PEEK(i));
Serhiy Storchaka73442852016-10-02 10:33:46 +03002517 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002518 Py_DECREF(sum);
2519 goto error;
2520 }
2521 Py_DECREF(none_val);
2522 }
2523
2524 if (convert_to_tuple) {
2525 return_value = PyList_AsTuple(sum);
2526 Py_DECREF(sum);
2527 if (return_value == NULL)
2528 goto error;
2529 }
2530 else {
2531 return_value = sum;
2532 }
2533
2534 while (oparg--)
2535 Py_DECREF(POP());
2536 PUSH(return_value);
2537 DISPATCH();
2538 }
2539
Benjamin Petersonddd19492018-09-16 22:38:02 -07002540 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002541 PyObject *set = PySet_New(NULL);
2542 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002543 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002544 if (set == NULL)
2545 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002546 for (i = oparg; i > 0; i--) {
2547 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002548 if (err == 0)
2549 err = PySet_Add(set, item);
2550 Py_DECREF(item);
2551 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002552 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002553 if (err != 0) {
2554 Py_DECREF(set);
2555 goto error;
2556 }
2557 PUSH(set);
2558 DISPATCH();
2559 }
2560
Benjamin Petersonddd19492018-09-16 22:38:02 -07002561 case TARGET(BUILD_SET_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002562 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002563 PyObject *sum = PySet_New(NULL);
2564 if (sum == NULL)
2565 goto error;
2566
2567 for (i = oparg; i > 0; i--) {
2568 if (_PySet_Update(sum, PEEK(i)) < 0) {
2569 Py_DECREF(sum);
2570 goto error;
2571 }
2572 }
2573
2574 while (oparg--)
2575 Py_DECREF(POP());
2576 PUSH(sum);
2577 DISPATCH();
2578 }
2579
Benjamin Petersonddd19492018-09-16 22:38:02 -07002580 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002581 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002582 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2583 if (map == NULL)
2584 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002585 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002586 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002587 PyObject *key = PEEK(2*i);
2588 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002589 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002590 if (err != 0) {
2591 Py_DECREF(map);
2592 goto error;
2593 }
2594 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002595
2596 while (oparg--) {
2597 Py_DECREF(POP());
2598 Py_DECREF(POP());
2599 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002600 PUSH(map);
2601 DISPATCH();
2602 }
2603
Benjamin Petersonddd19492018-09-16 22:38:02 -07002604 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002605 _Py_IDENTIFIER(__annotations__);
2606 int err;
2607 PyObject *ann_dict;
2608 if (f->f_locals == NULL) {
2609 PyErr_Format(PyExc_SystemError,
2610 "no locals found when setting up annotations");
2611 goto error;
2612 }
2613 /* check if __annotations__ in locals()... */
2614 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002615 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002616 &PyId___annotations__);
2617 if (ann_dict == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002618 if (PyErr_Occurred()) {
2619 goto error;
2620 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002621 /* ...if not, create a new one */
2622 ann_dict = PyDict_New();
2623 if (ann_dict == NULL) {
2624 goto error;
2625 }
2626 err = _PyDict_SetItemId(f->f_locals,
2627 &PyId___annotations__, ann_dict);
2628 Py_DECREF(ann_dict);
2629 if (err != 0) {
2630 goto error;
2631 }
2632 }
2633 }
2634 else {
2635 /* do the same if locals() is not a dict */
2636 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2637 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002638 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002639 }
2640 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2641 if (ann_dict == NULL) {
2642 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2643 goto error;
2644 }
2645 PyErr_Clear();
2646 ann_dict = PyDict_New();
2647 if (ann_dict == NULL) {
2648 goto error;
2649 }
2650 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2651 Py_DECREF(ann_dict);
2652 if (err != 0) {
2653 goto error;
2654 }
2655 }
2656 else {
2657 Py_DECREF(ann_dict);
2658 }
2659 }
2660 DISPATCH();
2661 }
2662
Benjamin Petersonddd19492018-09-16 22:38:02 -07002663 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002664 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002665 PyObject *map;
2666 PyObject *keys = TOP();
2667 if (!PyTuple_CheckExact(keys) ||
2668 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2669 PyErr_SetString(PyExc_SystemError,
2670 "bad BUILD_CONST_KEY_MAP keys argument");
2671 goto error;
2672 }
2673 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2674 if (map == NULL) {
2675 goto error;
2676 }
2677 for (i = oparg; i > 0; i--) {
2678 int err;
2679 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2680 PyObject *value = PEEK(i + 1);
2681 err = PyDict_SetItem(map, key, value);
2682 if (err != 0) {
2683 Py_DECREF(map);
2684 goto error;
2685 }
2686 }
2687
2688 Py_DECREF(POP());
2689 while (oparg--) {
2690 Py_DECREF(POP());
2691 }
2692 PUSH(map);
2693 DISPATCH();
2694 }
2695
Benjamin Petersonddd19492018-09-16 22:38:02 -07002696 case TARGET(BUILD_MAP_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002697 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002698 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002699 if (sum == NULL)
2700 goto error;
2701
2702 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002703 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002704 if (PyDict_Update(sum, arg) < 0) {
2705 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2706 PyErr_Format(PyExc_TypeError,
Berker Peksag8e9045d2016-10-02 13:08:25 +03002707 "'%.200s' object is not a mapping",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002708 arg->ob_type->tp_name);
2709 }
2710 Py_DECREF(sum);
2711 goto error;
2712 }
2713 }
2714
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002715 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002716 Py_DECREF(POP());
2717 PUSH(sum);
2718 DISPATCH();
2719 }
2720
Benjamin Petersonddd19492018-09-16 22:38:02 -07002721 case TARGET(BUILD_MAP_UNPACK_WITH_CALL): {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002722 Py_ssize_t i;
2723 PyObject *sum = PyDict_New();
2724 if (sum == NULL)
2725 goto error;
2726
2727 for (i = oparg; i > 0; i--) {
2728 PyObject *arg = PEEK(i);
2729 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002730 Py_DECREF(sum);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02002731 format_kwargs_error(PEEK(2 + oparg), arg);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002732 goto error;
2733 }
2734 }
2735
2736 while (oparg--)
2737 Py_DECREF(POP());
2738 PUSH(sum);
2739 DISPATCH();
2740 }
2741
Benjamin Petersonddd19492018-09-16 22:38:02 -07002742 case TARGET(MAP_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002743 PyObject *key = TOP();
2744 PyObject *value = SECOND();
2745 PyObject *map;
2746 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002747 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002748 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002749 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002750 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002751 Py_DECREF(value);
2752 Py_DECREF(key);
2753 if (err != 0)
2754 goto error;
2755 PREDICT(JUMP_ABSOLUTE);
2756 DISPATCH();
2757 }
2758
Benjamin Petersonddd19492018-09-16 22:38:02 -07002759 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002760 PyObject *name = GETITEM(names, oparg);
2761 PyObject *owner = TOP();
2762 PyObject *res = PyObject_GetAttr(owner, name);
2763 Py_DECREF(owner);
2764 SET_TOP(res);
2765 if (res == NULL)
2766 goto error;
2767 DISPATCH();
2768 }
2769
Benjamin Petersonddd19492018-09-16 22:38:02 -07002770 case TARGET(COMPARE_OP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002771 PyObject *right = POP();
2772 PyObject *left = TOP();
2773 PyObject *res = cmp_outcome(oparg, left, right);
2774 Py_DECREF(left);
2775 Py_DECREF(right);
2776 SET_TOP(res);
2777 if (res == NULL)
2778 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002779 PREDICT(POP_JUMP_IF_FALSE);
2780 PREDICT(POP_JUMP_IF_TRUE);
2781 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002782 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002783
Benjamin Petersonddd19492018-09-16 22:38:02 -07002784 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002785 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002786 PyObject *fromlist = POP();
2787 PyObject *level = TOP();
2788 PyObject *res;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002789 res = import_name(f, name, fromlist, level);
2790 Py_DECREF(level);
2791 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002792 SET_TOP(res);
2793 if (res == NULL)
2794 goto error;
2795 DISPATCH();
2796 }
2797
Benjamin Petersonddd19492018-09-16 22:38:02 -07002798 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002799 PyObject *from = POP(), *locals;
2800 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002801 if (PyFrame_FastToLocalsWithError(f) < 0) {
2802 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002803 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002804 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002805
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002806 locals = f->f_locals;
2807 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002808 PyErr_SetString(PyExc_SystemError,
2809 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002810 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002811 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002812 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002813 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002814 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002815 Py_DECREF(from);
2816 if (err != 0)
2817 goto error;
2818 DISPATCH();
2819 }
Guido van Rossum25831651993-05-19 14:50:45 +00002820
Benjamin Petersonddd19492018-09-16 22:38:02 -07002821 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002822 PyObject *name = GETITEM(names, oparg);
2823 PyObject *from = TOP();
2824 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002825 res = import_from(from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002826 PUSH(res);
2827 if (res == NULL)
2828 goto error;
2829 DISPATCH();
2830 }
Thomas Wouters52152252000-08-17 22:55:00 +00002831
Benjamin Petersonddd19492018-09-16 22:38:02 -07002832 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002833 JUMPBY(oparg);
2834 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002835 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002836
Benjamin Petersonddd19492018-09-16 22:38:02 -07002837 case TARGET(POP_JUMP_IF_FALSE): {
2838 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002839 PyObject *cond = POP();
2840 int err;
2841 if (cond == Py_True) {
2842 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002843 FAST_DISPATCH();
2844 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002845 if (cond == Py_False) {
2846 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002847 JUMPTO(oparg);
2848 FAST_DISPATCH();
2849 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002850 err = PyObject_IsTrue(cond);
2851 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002852 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07002853 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002854 else if (err == 0)
2855 JUMPTO(oparg);
2856 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002857 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002858 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002859 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002860
Benjamin Petersonddd19492018-09-16 22:38:02 -07002861 case TARGET(POP_JUMP_IF_TRUE): {
2862 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002863 PyObject *cond = POP();
2864 int err;
2865 if (cond == Py_False) {
2866 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002867 FAST_DISPATCH();
2868 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002869 if (cond == Py_True) {
2870 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002871 JUMPTO(oparg);
2872 FAST_DISPATCH();
2873 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002874 err = PyObject_IsTrue(cond);
2875 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002876 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002877 JUMPTO(oparg);
2878 }
2879 else if (err == 0)
2880 ;
2881 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002882 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002883 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002884 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002885
Benjamin Petersonddd19492018-09-16 22:38:02 -07002886 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002887 PyObject *cond = TOP();
2888 int err;
2889 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002890 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002891 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002892 FAST_DISPATCH();
2893 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002894 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002895 JUMPTO(oparg);
2896 FAST_DISPATCH();
2897 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002898 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002899 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002900 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002901 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002902 }
2903 else if (err == 0)
2904 JUMPTO(oparg);
2905 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002906 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002907 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002908 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002909
Benjamin Petersonddd19492018-09-16 22:38:02 -07002910 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002911 PyObject *cond = TOP();
2912 int err;
2913 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002914 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002915 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002916 FAST_DISPATCH();
2917 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002918 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002919 JUMPTO(oparg);
2920 FAST_DISPATCH();
2921 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002922 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002923 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002924 JUMPTO(oparg);
2925 }
2926 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002927 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002928 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002929 }
2930 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002931 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002932 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002933 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002934
Benjamin Petersonddd19492018-09-16 22:38:02 -07002935 case TARGET(JUMP_ABSOLUTE): {
2936 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002937 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002938#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002939 /* Enabling this path speeds-up all while and for-loops by bypassing
2940 the per-loop checks for signals. By default, this should be turned-off
2941 because it prevents detection of a control-break in tight loops like
2942 "while 1: pass". Compile with this option turned-on when you need
2943 the speed-up and do not need break checking inside tight loops (ones
2944 that contain only instructions ending with FAST_DISPATCH).
2945 */
2946 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002947#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002948 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002949#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002950 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002951
Benjamin Petersonddd19492018-09-16 22:38:02 -07002952 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002953 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002954 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002955 PyObject *iter = PyObject_GetIter(iterable);
2956 Py_DECREF(iterable);
2957 SET_TOP(iter);
2958 if (iter == NULL)
2959 goto error;
2960 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002961 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04002962 DISPATCH();
2963 }
2964
Benjamin Petersonddd19492018-09-16 22:38:02 -07002965 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04002966 /* before: [obj]; after [getiter(obj)] */
2967 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04002968 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04002969 if (PyCoro_CheckExact(iterable)) {
2970 /* `iterable` is a coroutine */
2971 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
2972 /* and it is used in a 'yield from' expression of a
2973 regular generator. */
2974 Py_DECREF(iterable);
2975 SET_TOP(NULL);
2976 PyErr_SetString(PyExc_TypeError,
2977 "cannot 'yield from' a coroutine object "
2978 "in a non-coroutine generator");
2979 goto error;
2980 }
2981 }
2982 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04002983 /* `iterable` is not a generator. */
2984 iter = PyObject_GetIter(iterable);
2985 Py_DECREF(iterable);
2986 SET_TOP(iter);
2987 if (iter == NULL)
2988 goto error;
2989 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002990 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002991 DISPATCH();
2992 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002993
Benjamin Petersonddd19492018-09-16 22:38:02 -07002994 case TARGET(FOR_ITER): {
2995 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002996 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002997 PyObject *iter = TOP();
2998 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
2999 if (next != NULL) {
3000 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003001 PREDICT(STORE_FAST);
3002 PREDICT(UNPACK_SEQUENCE);
3003 DISPATCH();
3004 }
3005 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003006 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
3007 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08003008 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003009 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003010 PyErr_Clear();
3011 }
3012 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003013 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003014 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003015 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003016 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003017 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003018 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003019
Benjamin Petersonddd19492018-09-16 22:38:02 -07003020 case TARGET(SETUP_FINALLY): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003021 /* NOTE: If you add any new block-setup opcodes that
3022 are not try/except/finally handlers, you may need
3023 to update the PyGen_NeedsFinalizing() function.
3024 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003025
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003026 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003027 STACK_LEVEL());
3028 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003029 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003030
Benjamin Petersonddd19492018-09-16 22:38:02 -07003031 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003032 _Py_IDENTIFIER(__aexit__);
3033 _Py_IDENTIFIER(__aenter__);
3034
3035 PyObject *mgr = TOP();
3036 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
3037 *enter;
3038 PyObject *res;
3039 if (exit == NULL)
3040 goto error;
3041 SET_TOP(exit);
3042 enter = special_lookup(mgr, &PyId___aenter__);
3043 Py_DECREF(mgr);
3044 if (enter == NULL)
3045 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003046 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003047 Py_DECREF(enter);
3048 if (res == NULL)
3049 goto error;
3050 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003051 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003052 DISPATCH();
3053 }
3054
Benjamin Petersonddd19492018-09-16 22:38:02 -07003055 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003056 PyObject *res = POP();
3057 /* Setup the finally block before pushing the result
3058 of __aenter__ on the stack. */
3059 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3060 STACK_LEVEL());
3061 PUSH(res);
3062 DISPATCH();
3063 }
3064
Benjamin Petersonddd19492018-09-16 22:38:02 -07003065 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003066 _Py_IDENTIFIER(__exit__);
3067 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003068 PyObject *mgr = TOP();
Raymond Hettingera3fec152016-11-21 17:24:23 -08003069 PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003070 PyObject *res;
Raymond Hettingera3fec152016-11-21 17:24:23 -08003071 if (enter == NULL)
3072 goto error;
3073 exit = special_lookup(mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003074 if (exit == NULL) {
3075 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003076 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003077 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003078 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003079 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003080 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003081 Py_DECREF(enter);
3082 if (res == NULL)
3083 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003084 /* Setup the finally block before pushing the result
3085 of __enter__ on the stack. */
3086 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3087 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003088
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003089 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003090 DISPATCH();
3091 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003092
Benjamin Petersonddd19492018-09-16 22:38:02 -07003093 case TARGET(WITH_CLEANUP_START): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003094 /* At the top of the stack are 1 or 6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003095 how/why we entered the finally clause:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003096 - TOP = NULL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003097 - (TOP, SECOND, THIRD) = exc_info()
3098 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003099 Below them is EXIT, the context.__exit__ or context.__aexit__
3100 bound method.
3101 In the first case, we must call
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003102 EXIT(None, None, None)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003103 otherwise we must call
3104 EXIT(TOP, SECOND, THIRD)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003105
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003106 In the first case, we remove EXIT from the
3107 stack, leaving TOP, and push TOP on the stack.
3108 Otherwise we shift the bottom 3 values of the
3109 stack down, replace the empty spot with NULL, and push
3110 None on the stack.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003111
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003112 Finally we push the result of the call.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003113 */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003114 PyObject *stack[3];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003115 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003116 PyObject *exc, *val, *tb, *res;
3117
3118 val = tb = Py_None;
3119 exc = TOP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003120 if (exc == NULL) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003121 STACK_SHRINK(1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003122 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003123 SET_TOP(exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003124 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003125 }
3126 else {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003127 assert(PyExceptionClass_Check(exc));
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003128 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003129 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003130 val = SECOND();
3131 tb = THIRD();
3132 tp2 = FOURTH();
3133 exc2 = PEEK(5);
3134 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003135 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003136 SET_VALUE(7, tb2);
3137 SET_VALUE(6, exc2);
3138 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003139 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3140 SET_FOURTH(NULL);
3141 /* We just shifted the stack down, so we have
3142 to tell the except handler block that the
3143 values are lower than it expects. */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003144 assert(f->f_iblock > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003145 block = &f->f_blockstack[f->f_iblock - 1];
3146 assert(block->b_type == EXCEPT_HANDLER);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003147 assert(block->b_level > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003148 block->b_level--;
3149 }
Victor Stinner842cfff2016-12-01 14:45:31 +01003150
3151 stack[0] = exc;
3152 stack[1] = val;
3153 stack[2] = tb;
3154 res = _PyObject_FastCall(exit_func, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003155 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003156 if (res == NULL)
3157 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003158
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003159 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003160 PUSH(exc);
3161 PUSH(res);
3162 PREDICT(WITH_CLEANUP_FINISH);
3163 DISPATCH();
3164 }
3165
Benjamin Petersonddd19492018-09-16 22:38:02 -07003166 case TARGET(WITH_CLEANUP_FINISH): {
3167 PREDICTED(WITH_CLEANUP_FINISH);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003168 /* TOP = the result of calling the context.__exit__ bound method
3169 SECOND = either None or exception type
3170
3171 If SECOND is None below is NULL or the return address,
3172 otherwise below are 7 values representing an exception.
3173 */
Yury Selivanov75445082015-05-11 22:57:16 -04003174 PyObject *res = POP();
3175 PyObject *exc = POP();
3176 int err;
3177
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003178 if (exc != Py_None)
3179 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003180 else
3181 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003182
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003183 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003184 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003186 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003187 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003188 else if (err > 0) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003189 /* There was an exception and a True return.
3190 * We must manually unwind the EXCEPT_HANDLER block
3191 * which was created when the exception was caught,
Quan Tian3bd0d622018-10-20 05:30:03 +08003192 * otherwise the stack will be in an inconsistent state.
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003193 */
3194 PyTryBlock *b = PyFrame_BlockPop(f);
3195 assert(b->b_type == EXCEPT_HANDLER);
3196 UNWIND_EXCEPT_HANDLER(b);
3197 PUSH(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003198 }
3199 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003200 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003201 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003202
Benjamin Petersonddd19492018-09-16 22:38:02 -07003203 case TARGET(LOAD_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003204 /* Designed to work in tamdem with CALL_METHOD. */
3205 PyObject *name = GETITEM(names, oparg);
3206 PyObject *obj = TOP();
3207 PyObject *meth = NULL;
3208
3209 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3210
Yury Selivanovf2392132016-12-13 19:03:51 -05003211 if (meth == NULL) {
3212 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003213 goto error;
3214 }
3215
3216 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003217 /* We can bypass temporary bound method object.
3218 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003219
INADA Naoki015bce62017-01-16 17:23:30 +09003220 meth | self | arg1 | ... | argN
3221 */
3222 SET_TOP(meth);
3223 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003224 }
3225 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003226 /* meth is not an unbound method (but a regular attr, or
3227 something was returned by a descriptor protocol). Set
3228 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003229 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003230
3231 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003232 */
INADA Naoki015bce62017-01-16 17:23:30 +09003233 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003234 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003235 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003236 }
3237 DISPATCH();
3238 }
3239
Benjamin Petersonddd19492018-09-16 22:38:02 -07003240 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003241 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003242 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003243
3244 sp = stack_pointer;
3245
INADA Naoki015bce62017-01-16 17:23:30 +09003246 meth = PEEK(oparg + 2);
3247 if (meth == NULL) {
3248 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3249 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003250
3251 Stack layout:
3252
INADA Naoki015bce62017-01-16 17:23:30 +09003253 ... | NULL | callable | arg1 | ... | argN
3254 ^- TOP()
3255 ^- (-oparg)
3256 ^- (-oparg-1)
3257 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003258
Ville Skyttä49b27342017-08-03 09:00:59 +03003259 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003260 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003261 */
Yury Selivanovf2392132016-12-13 19:03:51 -05003262 res = call_function(&sp, oparg, NULL);
3263 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003264 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003265 }
3266 else {
3267 /* This is a method call. Stack layout:
3268
INADA Naoki015bce62017-01-16 17:23:30 +09003269 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003270 ^- TOP()
3271 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003272 ^- (-oparg-1)
3273 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003274
INADA Naoki015bce62017-01-16 17:23:30 +09003275 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003276 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003277 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003278 */
3279 res = call_function(&sp, oparg + 1, NULL);
3280 stack_pointer = sp;
3281 }
3282
3283 PUSH(res);
3284 if (res == NULL)
3285 goto error;
3286 DISPATCH();
3287 }
3288
Benjamin Petersonddd19492018-09-16 22:38:02 -07003289 case TARGET(CALL_FUNCTION): {
3290 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003291 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003292 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003293 res = call_function(&sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003294 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003295 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003296 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003297 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003298 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003299 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003300 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003301
Benjamin Petersonddd19492018-09-16 22:38:02 -07003302 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003303 PyObject **sp, *res, *names;
3304
3305 names = POP();
3306 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003307 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003308 res = call_function(&sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003309 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003310 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003311 Py_DECREF(names);
3312
3313 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003314 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003315 }
3316 DISPATCH();
3317 }
3318
Benjamin Petersonddd19492018-09-16 22:38:02 -07003319 case TARGET(CALL_FUNCTION_EX): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003320 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003321 if (oparg & 0x01) {
3322 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003323 if (!PyDict_CheckExact(kwargs)) {
3324 PyObject *d = PyDict_New();
3325 if (d == NULL)
3326 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003327 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003328 Py_DECREF(d);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003329 format_kwargs_error(SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003330 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003331 goto error;
3332 }
3333 Py_DECREF(kwargs);
3334 kwargs = d;
3335 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003336 assert(PyDict_CheckExact(kwargs));
3337 }
3338 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003339 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003340 if (!PyTuple_CheckExact(callargs)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003341 if (check_args_iterable(func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003342 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003343 goto error;
3344 }
3345 Py_SETREF(callargs, PySequence_Tuple(callargs));
3346 if (callargs == NULL) {
3347 goto error;
3348 }
3349 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003350 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003351
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003352 result = do_call_core(func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003353 Py_DECREF(func);
3354 Py_DECREF(callargs);
3355 Py_XDECREF(kwargs);
3356
3357 SET_TOP(result);
3358 if (result == NULL) {
3359 goto error;
3360 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003361 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003362 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003363
Benjamin Petersonddd19492018-09-16 22:38:02 -07003364 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003365 PyObject *qualname = POP();
3366 PyObject *codeobj = POP();
3367 PyFunctionObject *func = (PyFunctionObject *)
3368 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003369
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003370 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003371 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003372 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003373 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003374 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003375
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003376 if (oparg & 0x08) {
3377 assert(PyTuple_CheckExact(TOP()));
3378 func ->func_closure = POP();
3379 }
3380 if (oparg & 0x04) {
3381 assert(PyDict_CheckExact(TOP()));
3382 func->func_annotations = POP();
3383 }
3384 if (oparg & 0x02) {
3385 assert(PyDict_CheckExact(TOP()));
3386 func->func_kwdefaults = POP();
3387 }
3388 if (oparg & 0x01) {
3389 assert(PyTuple_CheckExact(TOP()));
3390 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003391 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003392
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003393 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003394 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003395 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003396
Benjamin Petersonddd19492018-09-16 22:38:02 -07003397 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003398 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003399 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003400 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003401 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003402 step = NULL;
3403 stop = POP();
3404 start = TOP();
3405 slice = PySlice_New(start, stop, step);
3406 Py_DECREF(start);
3407 Py_DECREF(stop);
3408 Py_XDECREF(step);
3409 SET_TOP(slice);
3410 if (slice == NULL)
3411 goto error;
3412 DISPATCH();
3413 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003414
Benjamin Petersonddd19492018-09-16 22:38:02 -07003415 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003416 /* Handles f-string value formatting. */
3417 PyObject *result;
3418 PyObject *fmt_spec;
3419 PyObject *value;
3420 PyObject *(*conv_fn)(PyObject *);
3421 int which_conversion = oparg & FVC_MASK;
3422 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3423
3424 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003425 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003426
3427 /* See if any conversion is specified. */
3428 switch (which_conversion) {
3429 case FVC_STR: conv_fn = PyObject_Str; break;
3430 case FVC_REPR: conv_fn = PyObject_Repr; break;
3431 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3432
3433 /* Must be 0 (meaning no conversion), since only four
3434 values are allowed by (oparg & FVC_MASK). */
3435 default: conv_fn = NULL; break;
3436 }
3437
3438 /* If there's a conversion function, call it and replace
3439 value with that result. Otherwise, just use value,
3440 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003441 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003442 result = conv_fn(value);
3443 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003444 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003445 Py_XDECREF(fmt_spec);
3446 goto error;
3447 }
3448 value = result;
3449 }
3450
3451 /* If value is a unicode object, and there's no fmt_spec,
3452 then we know the result of format(value) is value
3453 itself. In that case, skip calling format(). I plan to
3454 move this optimization in to PyObject_Format()
3455 itself. */
3456 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3457 /* Do nothing, just transfer ownership to result. */
3458 result = value;
3459 } else {
3460 /* Actually call format(). */
3461 result = PyObject_Format(value, fmt_spec);
3462 Py_DECREF(value);
3463 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003464 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003465 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003466 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003467 }
3468
Eric V. Smith135d5f42016-02-05 18:23:08 -05003469 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003470 DISPATCH();
3471 }
3472
Benjamin Petersonddd19492018-09-16 22:38:02 -07003473 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003474 int oldoparg = oparg;
3475 NEXTOPARG();
3476 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003477 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003478 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003479
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003480
Antoine Pitrou042b1282010-08-13 21:15:58 +00003481#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003482 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003483#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003484 default:
3485 fprintf(stderr,
3486 "XXX lineno: %d, opcode: %d\n",
3487 PyFrame_GetLineNumber(f),
3488 opcode);
3489 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003490 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003491
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003492 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003493
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003494 /* This should never be reached. Every opcode should end with DISPATCH()
3495 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003496 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003497
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003498error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003499 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003500#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003501 if (!PyErr_Occurred())
3502 PyErr_SetString(PyExc_SystemError,
3503 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003504#else
3505 assert(PyErr_Occurred());
3506#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003507
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003508 /* Log traceback info. */
3509 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003510
Benjamin Peterson51f46162013-01-23 08:38:47 -05003511 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003512 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3513 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003514
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003515exception_unwind:
3516 /* Unwind stacks if an exception occurred */
3517 while (f->f_iblock > 0) {
3518 /* Pop the current block. */
3519 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003521 if (b->b_type == EXCEPT_HANDLER) {
3522 UNWIND_EXCEPT_HANDLER(b);
3523 continue;
3524 }
3525 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003526 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003527 PyObject *exc, *val, *tb;
3528 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003529 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003530 /* Beware, this invalidates all b->b_* fields */
3531 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003532 PUSH(exc_info->exc_traceback);
3533 PUSH(exc_info->exc_value);
3534 if (exc_info->exc_type != NULL) {
3535 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003536 }
3537 else {
3538 Py_INCREF(Py_None);
3539 PUSH(Py_None);
3540 }
3541 PyErr_Fetch(&exc, &val, &tb);
3542 /* Make the raw exception data
3543 available to the handler,
3544 so a program can emulate the
3545 Python main loop. */
3546 PyErr_NormalizeException(
3547 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003548 if (tb != NULL)
3549 PyException_SetTraceback(val, tb);
3550 else
3551 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003552 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003553 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003554 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003555 exc_info->exc_value = val;
3556 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003557 if (tb == NULL)
3558 tb = Py_None;
3559 Py_INCREF(tb);
3560 PUSH(tb);
3561 PUSH(val);
3562 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003563 JUMPTO(handler);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003564 /* Resume normal execution */
3565 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003566 }
3567 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003568
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003569 /* End the loop as we still have an error */
3570 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003571 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003573 /* Pop remaining stack entries. */
3574 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003575 PyObject *o = POP();
3576 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003577 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003578
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003579 assert(retval == NULL);
3580 assert(PyErr_Occurred());
Guido van Rossumac7be682001-01-17 15:42:30 +00003581
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003582return_or_yield:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003583 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003584 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003585 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3586 tstate, f, PyTrace_RETURN, retval)) {
3587 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003588 }
3589 }
3590 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003591 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3592 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003593 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003594 }
3595 }
3596 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003598 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003599exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003600 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3601 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003602 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003603 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003604 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003605
Victor Stinnerefde1462015-03-21 15:04:43 +01003606 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003607}
3608
Benjamin Petersonb204a422011-06-05 22:04:07 -05003609static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003610format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3611{
3612 int err;
3613 Py_ssize_t len = PyList_GET_SIZE(names);
3614 PyObject *name_str, *comma, *tail, *tmp;
3615
3616 assert(PyList_CheckExact(names));
3617 assert(len >= 1);
3618 /* Deal with the joys of natural language. */
3619 switch (len) {
3620 case 1:
3621 name_str = PyList_GET_ITEM(names, 0);
3622 Py_INCREF(name_str);
3623 break;
3624 case 2:
3625 name_str = PyUnicode_FromFormat("%U and %U",
3626 PyList_GET_ITEM(names, len - 2),
3627 PyList_GET_ITEM(names, len - 1));
3628 break;
3629 default:
3630 tail = PyUnicode_FromFormat(", %U, and %U",
3631 PyList_GET_ITEM(names, len - 2),
3632 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003633 if (tail == NULL)
3634 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003635 /* Chop off the last two objects in the list. This shouldn't actually
3636 fail, but we can't be too careful. */
3637 err = PyList_SetSlice(names, len - 2, len, NULL);
3638 if (err == -1) {
3639 Py_DECREF(tail);
3640 return;
3641 }
3642 /* Stitch everything up into a nice comma-separated list. */
3643 comma = PyUnicode_FromString(", ");
3644 if (comma == NULL) {
3645 Py_DECREF(tail);
3646 return;
3647 }
3648 tmp = PyUnicode_Join(comma, names);
3649 Py_DECREF(comma);
3650 if (tmp == NULL) {
3651 Py_DECREF(tail);
3652 return;
3653 }
3654 name_str = PyUnicode_Concat(tmp, tail);
3655 Py_DECREF(tmp);
3656 Py_DECREF(tail);
3657 break;
3658 }
3659 if (name_str == NULL)
3660 return;
3661 PyErr_Format(PyExc_TypeError,
3662 "%U() missing %i required %s argument%s: %U",
3663 co->co_name,
3664 len,
3665 kind,
3666 len == 1 ? "" : "s",
3667 name_str);
3668 Py_DECREF(name_str);
3669}
3670
3671static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003672missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003673 PyObject **fastlocals)
3674{
Victor Stinner74319ae2016-08-25 00:04:09 +02003675 Py_ssize_t i, j = 0;
3676 Py_ssize_t start, end;
3677 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003678 const char *kind = positional ? "positional" : "keyword-only";
3679 PyObject *missing_names;
3680
3681 /* Compute the names of the arguments that are missing. */
3682 missing_names = PyList_New(missing);
3683 if (missing_names == NULL)
3684 return;
3685 if (positional) {
3686 start = 0;
3687 end = co->co_argcount - defcount;
3688 }
3689 else {
3690 start = co->co_argcount;
3691 end = start + co->co_kwonlyargcount;
3692 }
3693 for (i = start; i < end; i++) {
3694 if (GETLOCAL(i) == NULL) {
3695 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3696 PyObject *name = PyObject_Repr(raw);
3697 if (name == NULL) {
3698 Py_DECREF(missing_names);
3699 return;
3700 }
3701 PyList_SET_ITEM(missing_names, j++, name);
3702 }
3703 }
3704 assert(j == missing);
3705 format_missing(kind, co, missing_names);
3706 Py_DECREF(missing_names);
3707}
3708
3709static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003710too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3711 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003712{
3713 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003714 Py_ssize_t kwonly_given = 0;
3715 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003716 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003717 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003718
Benjamin Petersone109c702011-06-24 09:37:26 -05003719 assert((co->co_flags & CO_VARARGS) == 0);
3720 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003721 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3722 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003723 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003724 }
3725 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003726 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003727 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003728 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003729 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003730 }
3731 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003732 plural = (co_argcount != 1);
3733 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003734 }
3735 if (sig == NULL)
3736 return;
3737 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003738 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3739 kwonly_sig = PyUnicode_FromFormat(format,
3740 given != 1 ? "s" : "",
3741 kwonly_given,
3742 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003743 if (kwonly_sig == NULL) {
3744 Py_DECREF(sig);
3745 return;
3746 }
3747 }
3748 else {
3749 /* This will not fail. */
3750 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003751 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003752 }
3753 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003754 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003755 co->co_name,
3756 sig,
3757 plural ? "s" : "",
3758 given,
3759 kwonly_sig,
3760 given == 1 && !kwonly_given ? "was" : "were");
3761 Py_DECREF(sig);
3762 Py_DECREF(kwonly_sig);
3763}
3764
Guido van Rossumc2e20742006-02-27 22:32:47 +00003765/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02003766 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003767 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003768
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003769PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +02003770_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003771 PyObject *const *args, Py_ssize_t argcount,
3772 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003773 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003774 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003775 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003776 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003777{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003778 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003779 PyFrameObject *f;
3780 PyObject *retval = NULL;
3781 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003782 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003783 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003784 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3785 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003786 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003787
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003788 if (globals == NULL) {
3789 PyErr_SetString(PyExc_SystemError,
3790 "PyEval_EvalCodeEx: NULL globals");
3791 return NULL;
3792 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003793
Victor Stinnerc7020012016-08-16 23:40:29 +02003794 /* Create the frame */
Victor Stinner50b48572018-11-01 01:51:40 +01003795 tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003796 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003797 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003798 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003799 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003800 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003801 fastlocals = f->f_localsplus;
3802 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003803
Victor Stinnerc7020012016-08-16 23:40:29 +02003804 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003805 if (co->co_flags & CO_VARKEYWORDS) {
3806 kwdict = PyDict_New();
3807 if (kwdict == NULL)
3808 goto fail;
3809 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003810 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003811 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003812 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003813 SETLOCAL(i, kwdict);
3814 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003815 else {
3816 kwdict = NULL;
3817 }
3818
3819 /* Copy positional arguments into local variables */
3820 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003821 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003822 }
3823 else {
3824 n = argcount;
3825 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003826 for (i = 0; i < n; i++) {
3827 x = args[i];
3828 Py_INCREF(x);
3829 SETLOCAL(i, x);
3830 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003831
3832 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003833 if (co->co_flags & CO_VARARGS) {
Sergey Fedoseev234531b2019-02-25 21:59:12 +05003834 u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003835 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003836 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003837 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003838 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003839 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003840
Serhiy Storchakab7281052016-09-12 00:52:40 +03003841 /* Handle keyword arguments passed as two strided arrays */
3842 kwcount *= kwstep;
3843 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003844 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003845 PyObject *keyword = kwnames[i];
3846 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003847 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003848
Benjamin Petersonb204a422011-06-05 22:04:07 -05003849 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3850 PyErr_Format(PyExc_TypeError,
3851 "%U() keywords must be strings",
3852 co->co_name);
3853 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003854 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003855
Benjamin Petersonb204a422011-06-05 22:04:07 -05003856 /* Speed hack: do raw pointer compares. As names are
3857 normally interned this should almost always hit. */
3858 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3859 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003860 PyObject *name = co_varnames[j];
3861 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003862 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003863 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003864 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003865
Benjamin Petersonb204a422011-06-05 22:04:07 -05003866 /* Slow fallback, just in case */
3867 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003868 PyObject *name = co_varnames[j];
3869 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
3870 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003871 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003872 }
3873 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003874 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003875 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003876 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003877
Victor Stinner231d1f32017-01-11 02:12:06 +01003878 assert(j >= total_args);
3879 if (kwdict == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003880 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003881 "%U() got an unexpected keyword argument '%S'",
3882 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003883 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003884 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003885
Christian Heimes0bd447f2013-07-20 14:48:10 +02003886 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3887 goto fail;
3888 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003889 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02003890
Benjamin Petersonb204a422011-06-05 22:04:07 -05003891 kw_found:
3892 if (GETLOCAL(j) != NULL) {
3893 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003894 "%U() got multiple values for argument '%S'",
3895 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003896 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003897 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003898 Py_INCREF(value);
3899 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003900 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003901
3902 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003903 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003904 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003905 goto fail;
3906 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003907
3908 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003909 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02003910 Py_ssize_t m = co->co_argcount - defcount;
3911 Py_ssize_t missing = 0;
3912 for (i = argcount; i < m; i++) {
3913 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003914 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02003915 }
3916 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003917 if (missing) {
3918 missing_arguments(co, missing, defcount, fastlocals);
3919 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003920 }
3921 if (n > m)
3922 i = n - m;
3923 else
3924 i = 0;
3925 for (; i < defcount; i++) {
3926 if (GETLOCAL(m+i) == NULL) {
3927 PyObject *def = defs[i];
3928 Py_INCREF(def);
3929 SETLOCAL(m+i, def);
3930 }
3931 }
3932 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003933
3934 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003935 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02003936 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003937 for (i = co->co_argcount; i < total_args; i++) {
3938 PyObject *name;
3939 if (GETLOCAL(i) != NULL)
3940 continue;
3941 name = PyTuple_GET_ITEM(co->co_varnames, i);
3942 if (kwdefs != NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003943 PyObject *def = PyDict_GetItemWithError(kwdefs, name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003944 if (def) {
3945 Py_INCREF(def);
3946 SETLOCAL(i, def);
3947 continue;
3948 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003949 else if (PyErr_Occurred()) {
3950 goto fail;
3951 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003952 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003953 missing++;
3954 }
3955 if (missing) {
3956 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003957 goto fail;
3958 }
3959 }
3960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003961 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003962 vars into frame. */
3963 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003964 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02003965 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05003966 /* Possibly account for the cell variable being an argument. */
3967 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07003968 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05003969 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05003970 /* Clear the local copy. */
3971 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003972 }
3973 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05003974 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003975 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05003976 if (c == NULL)
3977 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05003978 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003979 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003980
3981 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05003982 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3983 PyObject *o = PyTuple_GET_ITEM(closure, i);
3984 Py_INCREF(o);
3985 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003986 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003987
Yury Selivanoveb636452016-09-08 22:01:51 -07003988 /* Handle generator/coroutine/asynchronous generator */
3989 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003990 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04003991 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003992 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04003993
3994 if (is_coro && tstate->in_coroutine_wrapper) {
3995 assert(coro_wrapper != NULL);
3996 PyErr_Format(PyExc_RuntimeError,
3997 "coroutine wrapper %.200R attempted "
3998 "to recursively wrap %.200R",
3999 coro_wrapper,
4000 co);
4001 goto fail;
4002 }
Yury Selivanov75445082015-05-11 22:57:16 -04004003
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004004 /* Don't need to keep the reference to f_back, it will be set
4005 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004006 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004007
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004008 /* Create a new generator that owns the ready to run frame
4009 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004010 if (is_coro) {
4011 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004012 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4013 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004014 } else {
4015 gen = PyGen_NewWithQualName(f, name, qualname);
4016 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004017 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004018 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004019 }
INADA Naoki9c157762016-12-26 18:52:46 +09004020
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004021 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004022
Yury Selivanov94c22632015-06-04 10:16:51 -04004023 if (is_coro && coro_wrapper != NULL) {
4024 PyObject *wrapped;
4025 tstate->in_coroutine_wrapper = 1;
4026 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
4027 tstate->in_coroutine_wrapper = 0;
4028 return wrapped;
4029 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04004030
Yury Selivanov75445082015-05-11 22:57:16 -04004031 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004032 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004033
Victor Stinner59a73272016-12-09 18:51:13 +01004034 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004035
Thomas Woutersce272b62007-09-19 21:19:28 +00004036fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004038 /* decref'ing the frame can cause __del__ methods to get invoked,
4039 which can call back into Python. While we're done with the
4040 current Python frame (f), the associated C stack is still in use,
4041 so recursion_depth must be boosted for the duration.
4042 */
4043 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09004044 if (Py_REFCNT(f) > 1) {
4045 Py_DECREF(f);
4046 _PyObject_GC_TRACK(f);
4047 }
4048 else {
4049 ++tstate->recursion_depth;
4050 Py_DECREF(f);
4051 --tstate->recursion_depth;
4052 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004053 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004054}
4055
Victor Stinner40ee3012014-06-16 15:59:28 +02004056PyObject *
4057PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004058 PyObject *const *args, int argcount,
4059 PyObject *const *kws, int kwcount,
4060 PyObject *const *defs, int defcount,
4061 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004062{
4063 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004064 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004065 kws, kws != NULL ? kws + 1 : NULL,
4066 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004067 defs, defcount,
4068 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004069 NULL, NULL);
4070}
Tim Peters5ca576e2001-06-18 22:08:13 +00004071
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004072static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05004073special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004074{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004075 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004076 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004077 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05004078 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004079 return NULL;
4080 }
4081 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004082}
4083
4084
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004085/* Logic for the raise statement (too complicated for inlining).
4086 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004087static int
Collin Winter828f04a2007-08-31 00:04:24 +00004088do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004089{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004090 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004091
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004092 if (exc == NULL) {
4093 /* Reraise */
Victor Stinner50b48572018-11-01 01:51:40 +01004094 PyThreadState *tstate = _PyThreadState_GET();
Mark Shannonae3087c2017-10-22 22:41:51 +01004095 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004096 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004097 type = exc_info->exc_type;
4098 value = exc_info->exc_value;
4099 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004100 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004101 PyErr_SetString(PyExc_RuntimeError,
4102 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004103 return 0;
4104 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004105 Py_XINCREF(type);
4106 Py_XINCREF(value);
4107 Py_XINCREF(tb);
4108 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004109 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004110 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004111
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004112 /* We support the following forms of raise:
4113 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004114 raise <instance>
4115 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004116
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004117 if (PyExceptionClass_Check(exc)) {
4118 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004119 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004120 if (value == NULL)
4121 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004122 if (!PyExceptionInstance_Check(value)) {
4123 PyErr_Format(PyExc_TypeError,
4124 "calling %R should have returned an instance of "
4125 "BaseException, not %R",
4126 type, Py_TYPE(value));
4127 goto raise_error;
4128 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004129 }
4130 else if (PyExceptionInstance_Check(exc)) {
4131 value = exc;
4132 type = PyExceptionInstance_Class(exc);
4133 Py_INCREF(type);
4134 }
4135 else {
4136 /* Not something you can raise. You get an exception
4137 anyway, just not what you specified :-) */
4138 Py_DECREF(exc);
4139 PyErr_SetString(PyExc_TypeError,
4140 "exceptions must derive from BaseException");
4141 goto raise_error;
4142 }
Collin Winter828f04a2007-08-31 00:04:24 +00004143
Serhiy Storchakac0191582016-09-27 11:37:10 +03004144 assert(type != NULL);
4145 assert(value != NULL);
4146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004147 if (cause) {
4148 PyObject *fixed_cause;
4149 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004150 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004151 if (fixed_cause == NULL)
4152 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004153 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004154 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004155 else if (PyExceptionInstance_Check(cause)) {
4156 fixed_cause = cause;
4157 }
4158 else if (cause == Py_None) {
4159 Py_DECREF(cause);
4160 fixed_cause = NULL;
4161 }
4162 else {
4163 PyErr_SetString(PyExc_TypeError,
4164 "exception causes must derive from "
4165 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004166 goto raise_error;
4167 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004168 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004169 }
Collin Winter828f04a2007-08-31 00:04:24 +00004170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004171 PyErr_SetObject(type, value);
4172 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004173 Py_DECREF(value);
4174 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004175 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004176
4177raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004178 Py_XDECREF(value);
4179 Py_XDECREF(type);
4180 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004181 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004182}
4183
Tim Petersd6d010b2001-06-21 02:49:55 +00004184/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004185 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004186
Guido van Rossum0368b722007-05-11 16:50:42 +00004187 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4188 with a variable target.
4189*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004190
Barry Warsawe42b18f1997-08-25 22:13:04 +00004191static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004192unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004193{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004194 int i = 0, j = 0;
4195 Py_ssize_t ll = 0;
4196 PyObject *it; /* iter(v) */
4197 PyObject *w;
4198 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004199
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004200 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004201
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004202 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004203 if (it == NULL) {
4204 if (PyErr_ExceptionMatches(PyExc_TypeError) &&
4205 v->ob_type->tp_iter == NULL && !PySequence_Check(v))
4206 {
4207 PyErr_Format(PyExc_TypeError,
4208 "cannot unpack non-iterable %.200s object",
4209 v->ob_type->tp_name);
4210 }
4211 return 0;
4212 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004214 for (; i < argcnt; i++) {
4215 w = PyIter_Next(it);
4216 if (w == NULL) {
4217 /* Iterator done, via error or exhaustion. */
4218 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004219 if (argcntafter == -1) {
4220 PyErr_Format(PyExc_ValueError,
4221 "not enough values to unpack (expected %d, got %d)",
4222 argcnt, i);
4223 }
4224 else {
4225 PyErr_Format(PyExc_ValueError,
4226 "not enough values to unpack "
4227 "(expected at least %d, got %d)",
4228 argcnt + argcntafter, i);
4229 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004230 }
4231 goto Error;
4232 }
4233 *--sp = w;
4234 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004235
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004236 if (argcntafter == -1) {
4237 /* We better have exhausted the iterator now. */
4238 w = PyIter_Next(it);
4239 if (w == NULL) {
4240 if (PyErr_Occurred())
4241 goto Error;
4242 Py_DECREF(it);
4243 return 1;
4244 }
4245 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004246 PyErr_Format(PyExc_ValueError,
4247 "too many values to unpack (expected %d)",
4248 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004249 goto Error;
4250 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004252 l = PySequence_List(it);
4253 if (l == NULL)
4254 goto Error;
4255 *--sp = l;
4256 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004257
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004258 ll = PyList_GET_SIZE(l);
4259 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004260 PyErr_Format(PyExc_ValueError,
4261 "not enough values to unpack (expected at least %d, got %zd)",
4262 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004263 goto Error;
4264 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004266 /* Pop the "after-variable" args off the list. */
4267 for (j = argcntafter; j > 0; j--, i++) {
4268 *--sp = PyList_GET_ITEM(l, ll - j);
4269 }
4270 /* Resize the list. */
4271 Py_SIZE(l) = ll - argcntafter;
4272 Py_DECREF(it);
4273 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004274
Tim Petersd6d010b2001-06-21 02:49:55 +00004275Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004276 for (; i > 0; i--, sp++)
4277 Py_DECREF(*sp);
4278 Py_XDECREF(it);
4279 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004280}
4281
4282
Guido van Rossum96a42c81992-01-12 02:29:51 +00004283#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004284static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004285prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004286{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004287 printf("%s ", str);
4288 if (PyObject_Print(v, stdout, 0) != 0)
4289 PyErr_Clear(); /* Don't know what else to do */
4290 printf("\n");
4291 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004292}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004293#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004294
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004295static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004296call_exc_trace(Py_tracefunc func, PyObject *self,
4297 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004298{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004299 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004300 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004301 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004302 if (value == NULL) {
4303 value = Py_None;
4304 Py_INCREF(value);
4305 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004306 PyErr_NormalizeException(&type, &value, &orig_traceback);
4307 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004308 arg = PyTuple_Pack(3, type, value, traceback);
4309 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004310 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004311 return;
4312 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004313 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004314 Py_DECREF(arg);
4315 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004316 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004317 else {
4318 Py_XDECREF(type);
4319 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004320 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004321 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004322}
4323
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004324static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004325call_trace_protected(Py_tracefunc func, PyObject *obj,
4326 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004327 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004328{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004329 PyObject *type, *value, *traceback;
4330 int err;
4331 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004332 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004333 if (err == 0)
4334 {
4335 PyErr_Restore(type, value, traceback);
4336 return 0;
4337 }
4338 else {
4339 Py_XDECREF(type);
4340 Py_XDECREF(value);
4341 Py_XDECREF(traceback);
4342 return -1;
4343 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004344}
4345
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004346static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004347call_trace(Py_tracefunc func, PyObject *obj,
4348 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004349 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004350{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004351 int result;
4352 if (tstate->tracing)
4353 return 0;
4354 tstate->tracing++;
4355 tstate->use_tracing = 0;
4356 result = func(obj, frame, what, arg);
4357 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4358 || (tstate->c_profilefunc != NULL));
4359 tstate->tracing--;
4360 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004361}
4362
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004363PyObject *
4364_PyEval_CallTracing(PyObject *func, PyObject *args)
4365{
Victor Stinner50b48572018-11-01 01:51:40 +01004366 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004367 int save_tracing = tstate->tracing;
4368 int save_use_tracing = tstate->use_tracing;
4369 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004371 tstate->tracing = 0;
4372 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4373 || (tstate->c_profilefunc != NULL));
4374 result = PyObject_Call(func, args, NULL);
4375 tstate->tracing = save_tracing;
4376 tstate->use_tracing = save_use_tracing;
4377 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004378}
4379
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004380/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004381static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004382maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004383 PyThreadState *tstate, PyFrameObject *frame,
4384 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004385{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004386 int result = 0;
4387 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004389 /* If the last instruction executed isn't in the current
4390 instruction window, reset the window.
4391 */
4392 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4393 PyAddrPair bounds;
4394 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4395 &bounds);
4396 *instr_lb = bounds.ap_lower;
4397 *instr_ub = bounds.ap_upper;
4398 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004399 /* If the last instruction falls at the start of a line or if it
4400 represents a jump backwards, update the frame's line number and
4401 then call the trace function if we're tracing source lines.
4402 */
4403 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004404 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004405 if (frame->f_trace_lines) {
4406 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4407 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004408 }
George King20faa682017-10-18 17:44:22 -07004409 /* Always emit an opcode event if we're tracing all opcodes. */
4410 if (frame->f_trace_opcodes) {
4411 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4412 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004413 *instr_prev = frame->f_lasti;
4414 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004415}
4416
Fred Drake5755ce62001-06-27 19:19:46 +00004417void
4418PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004419{
Victor Stinner50b48572018-11-01 01:51:40 +01004420 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004421 PyObject *temp = tstate->c_profileobj;
4422 Py_XINCREF(arg);
4423 tstate->c_profilefunc = NULL;
4424 tstate->c_profileobj = NULL;
4425 /* Must make sure that tracing is not ignored if 'temp' is freed */
4426 tstate->use_tracing = tstate->c_tracefunc != NULL;
4427 Py_XDECREF(temp);
4428 tstate->c_profilefunc = func;
4429 tstate->c_profileobj = arg;
4430 /* Flag that tracing or profiling is turned on */
4431 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004432}
4433
4434void
4435PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4436{
Victor Stinner50b48572018-11-01 01:51:40 +01004437 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004438 PyObject *temp = tstate->c_traceobj;
4439 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4440 Py_XINCREF(arg);
4441 tstate->c_tracefunc = NULL;
4442 tstate->c_traceobj = NULL;
4443 /* Must make sure that profiling is not ignored if 'temp' is freed */
4444 tstate->use_tracing = tstate->c_profilefunc != NULL;
4445 Py_XDECREF(temp);
4446 tstate->c_tracefunc = func;
4447 tstate->c_traceobj = arg;
4448 /* Flag that tracing or profiling is turned on */
4449 tstate->use_tracing = ((func != NULL)
4450 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004451}
4452
Yury Selivanov75445082015-05-11 22:57:16 -04004453void
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004454_PyEval_SetCoroutineOriginTrackingDepth(int new_depth)
4455{
4456 assert(new_depth >= 0);
Victor Stinner50b48572018-11-01 01:51:40 +01004457 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004458 tstate->coroutine_origin_tracking_depth = new_depth;
4459}
4460
4461int
4462_PyEval_GetCoroutineOriginTrackingDepth(void)
4463{
Victor Stinner50b48572018-11-01 01:51:40 +01004464 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004465 return tstate->coroutine_origin_tracking_depth;
4466}
4467
4468void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004469_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004470{
Victor Stinner50b48572018-11-01 01:51:40 +01004471 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanov75445082015-05-11 22:57:16 -04004472
Yury Selivanov75445082015-05-11 22:57:16 -04004473 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004474 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004475}
4476
4477PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004478_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004479{
Victor Stinner50b48572018-11-01 01:51:40 +01004480 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanov75445082015-05-11 22:57:16 -04004481 return tstate->coroutine_wrapper;
4482}
4483
Yury Selivanoveb636452016-09-08 22:01:51 -07004484void
4485_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4486{
Victor Stinner50b48572018-11-01 01:51:40 +01004487 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004488
4489 Py_XINCREF(firstiter);
4490 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4491}
4492
4493PyObject *
4494_PyEval_GetAsyncGenFirstiter(void)
4495{
Victor Stinner50b48572018-11-01 01:51:40 +01004496 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004497 return tstate->async_gen_firstiter;
4498}
4499
4500void
4501_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4502{
Victor Stinner50b48572018-11-01 01:51:40 +01004503 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004504
4505 Py_XINCREF(finalizer);
4506 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4507}
4508
4509PyObject *
4510_PyEval_GetAsyncGenFinalizer(void)
4511{
Victor Stinner50b48572018-11-01 01:51:40 +01004512 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004513 return tstate->async_gen_finalizer;
4514}
4515
Guido van Rossumb209a111997-04-29 18:18:01 +00004516PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004517PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004518{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004519 PyFrameObject *current_frame = PyEval_GetFrame();
4520 if (current_frame == NULL)
Victor Stinnercaba55b2018-08-03 15:33:52 +02004521 return _PyInterpreterState_GET_UNSAFE()->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004522 else
4523 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004524}
4525
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004526/* Convenience function to get a builtin from its name */
4527PyObject *
4528_PyEval_GetBuiltinId(_Py_Identifier *name)
4529{
4530 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4531 if (attr) {
4532 Py_INCREF(attr);
4533 }
4534 else if (!PyErr_Occurred()) {
4535 PyErr_SetObject(PyExc_AttributeError, _PyUnicode_FromId(name));
4536 }
4537 return attr;
4538}
4539
Guido van Rossumb209a111997-04-29 18:18:01 +00004540PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004541PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004542{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004543 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004544 if (current_frame == NULL) {
4545 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004546 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004547 }
4548
4549 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4550 return NULL;
4551
4552 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004553 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004554}
4555
Guido van Rossumb209a111997-04-29 18:18:01 +00004556PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004557PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004558{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004559 PyFrameObject *current_frame = PyEval_GetFrame();
4560 if (current_frame == NULL)
4561 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004562
4563 assert(current_frame->f_globals != NULL);
4564 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004565}
4566
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004567PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004568PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004569{
Victor Stinner50b48572018-11-01 01:51:40 +01004570 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004571 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004572}
4573
Guido van Rossum6135a871995-01-09 17:53:26 +00004574int
Tim Peters5ba58662001-07-16 02:29:45 +00004575PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004576{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004577 PyFrameObject *current_frame = PyEval_GetFrame();
4578 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004580 if (current_frame != NULL) {
4581 const int codeflags = current_frame->f_code->co_flags;
4582 const int compilerflags = codeflags & PyCF_MASK;
4583 if (compilerflags) {
4584 result = 1;
4585 cf->cf_flags |= compilerflags;
4586 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004587#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004588 if (codeflags & CO_GENERATOR_ALLOWED) {
4589 result = 1;
4590 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4591 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004592#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004593 }
4594 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004595}
4596
Guido van Rossum3f5da241990-12-20 15:06:42 +00004597
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004598const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004599PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004600{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004601 if (PyMethod_Check(func))
4602 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4603 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004604 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004605 else if (PyCFunction_Check(func))
4606 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4607 else
4608 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004609}
4610
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004611const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004612PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004613{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004614 if (PyMethod_Check(func))
4615 return "()";
4616 else if (PyFunction_Check(func))
4617 return "()";
4618 else if (PyCFunction_Check(func))
4619 return "()";
4620 else
4621 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004622}
4623
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004624#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004625if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004626 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4627 tstate, tstate->frame, \
4628 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004629 x = NULL; \
4630 } \
4631 else { \
4632 x = call; \
4633 if (tstate->c_profilefunc != NULL) { \
4634 if (x == NULL) { \
4635 call_trace_protected(tstate->c_profilefunc, \
4636 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004637 tstate, tstate->frame, \
4638 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004639 /* XXX should pass (type, value, tb) */ \
4640 } else { \
4641 if (call_trace(tstate->c_profilefunc, \
4642 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004643 tstate, tstate->frame, \
4644 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004645 Py_DECREF(x); \
4646 x = NULL; \
4647 } \
4648 } \
4649 } \
4650 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004651} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004652 x = call; \
4653 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004654
Victor Stinner415c5102017-01-11 00:54:57 +01004655/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4656 to reduce the stack consumption. */
4657Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Benjamin Peterson4fd64b92016-09-09 14:57:58 -07004658call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004659{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004660 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004661 PyObject *func = *pfunc;
4662 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004663 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4664 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004665 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004666
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004667 /* Always dispatch PyCFunction first, because these are
4668 presumed to be the most frequent callable object.
4669 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004670 if (PyCFunction_Check(func)) {
Victor Stinner50b48572018-11-01 01:51:40 +01004671 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004672 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004673 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004674 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
Victor Stinner50b48572018-11-01 01:51:40 +01004675 PyThreadState *tstate = _PyThreadState_GET();
jdemeyer56868f92018-07-21 10:30:59 +02004676 if (nargs > 0 && tstate->use_tracing) {
4677 /* We need to create a temporary bound method as argument
4678 for profiling.
4679
4680 If nargs == 0, then this cannot work because we have no
4681 "self". In any case, the call itself would raise
4682 TypeError (foo needs an argument), so we just skip
4683 profiling. */
4684 PyObject *self = stack[0];
4685 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
jdemeyer147d9552018-07-23 18:41:20 +02004686 if (func != NULL) {
4687 C_TRACE(x, _PyCFunction_FastCallKeywords(func,
4688 stack+1, nargs-1,
4689 kwnames));
4690 Py_DECREF(func);
INADA Naoki93fac8d2017-03-07 14:24:37 +09004691 }
jdemeyer147d9552018-07-23 18:41:20 +02004692 else {
4693 x = NULL;
4694 }
INADA Naoki93fac8d2017-03-07 14:24:37 +09004695 }
4696 else {
4697 x = _PyMethodDescr_FastCallKeywords(func, stack, nargs, kwnames);
4698 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004699 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004700 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004701 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
Victor Stinnerb69ee8c2016-11-28 18:32:31 +01004702 /* Optimize access to bound methods. Reuse the Python stack
4703 to pass 'self' as the first argument, replace 'func'
4704 with 'self'. It avoids the creation of a new temporary tuple
4705 for arguments (to replace func with self) when the method uses
4706 FASTCALL. */
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004707 PyObject *self = PyMethod_GET_SELF(func);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004708 Py_INCREF(self);
4709 func = PyMethod_GET_FUNCTION(func);
4710 Py_INCREF(func);
4711 Py_SETREF(*pfunc, self);
4712 nargs++;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004713 stack--;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004714 }
4715 else {
4716 Py_INCREF(func);
4717 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004718
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004719 if (PyFunction_Check(func)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004720 x = _PyFunction_FastCallKeywords(func, stack, nargs, kwnames);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004721 }
4722 else {
4723 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4724 }
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004725 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004726 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004727
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004728 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4729
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004730 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004731 while ((*pp_stack) > pfunc) {
4732 w = EXT_POP(*pp_stack);
4733 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004734 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004735
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004736 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004737}
4738
Jeremy Hylton52820442001-01-03 23:52:36 +00004739static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004740do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004741{
jdemeyere89de732018-09-19 12:06:20 +02004742 PyObject *result;
4743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004744 if (PyCFunction_Check(func)) {
Victor Stinner50b48572018-11-01 01:51:40 +01004745 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004746 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004747 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004748 }
jdemeyere89de732018-09-19 12:06:20 +02004749 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
Victor Stinner50b48572018-11-01 01:51:40 +01004750 PyThreadState *tstate = _PyThreadState_GET();
jdemeyere89de732018-09-19 12:06:20 +02004751 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
4752 if (nargs > 0 && tstate->use_tracing) {
4753 /* We need to create a temporary bound method as argument
4754 for profiling.
4755
4756 If nargs == 0, then this cannot work because we have no
4757 "self". In any case, the call itself would raise
4758 TypeError (foo needs an argument), so we just skip
4759 profiling. */
4760 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
4761 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4762 if (func == NULL) {
4763 return NULL;
4764 }
4765
4766 C_TRACE(result, _PyCFunction_FastCallDict(func,
Victor Stinnerd17a6932018-11-09 16:56:48 +01004767 &_PyTuple_ITEMS(callargs)[1],
jdemeyere89de732018-09-19 12:06:20 +02004768 nargs - 1,
4769 kwdict));
4770 Py_DECREF(func);
4771 return result;
4772 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004773 }
jdemeyere89de732018-09-19 12:06:20 +02004774 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00004775}
4776
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004777/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004778 nb_index slot defined, and store in *pi.
4779 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004780 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004781 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004782*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004783int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004784_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004785{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004786 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004787 Py_ssize_t x;
4788 if (PyIndex_Check(v)) {
4789 x = PyNumber_AsSsize_t(v, NULL);
4790 if (x == -1 && PyErr_Occurred())
4791 return 0;
4792 }
4793 else {
4794 PyErr_SetString(PyExc_TypeError,
4795 "slice indices must be integers or "
4796 "None or have an __index__ method");
4797 return 0;
4798 }
4799 *pi = x;
4800 }
4801 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004802}
4803
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004804int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004805_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004806{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004807 Py_ssize_t x;
4808 if (PyIndex_Check(v)) {
4809 x = PyNumber_AsSsize_t(v, NULL);
4810 if (x == -1 && PyErr_Occurred())
4811 return 0;
4812 }
4813 else {
4814 PyErr_SetString(PyExc_TypeError,
4815 "slice indices must be integers or "
4816 "have an __index__ method");
4817 return 0;
4818 }
4819 *pi = x;
4820 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004821}
4822
4823
Guido van Rossum486364b2007-06-30 05:01:58 +00004824#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004825 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004826
Guido van Rossumb209a111997-04-29 18:18:01 +00004827static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004828cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004829{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004830 int res = 0;
4831 switch (op) {
4832 case PyCmp_IS:
4833 res = (v == w);
4834 break;
4835 case PyCmp_IS_NOT:
4836 res = (v != w);
4837 break;
4838 case PyCmp_IN:
4839 res = PySequence_Contains(w, v);
4840 if (res < 0)
4841 return NULL;
4842 break;
4843 case PyCmp_NOT_IN:
4844 res = PySequence_Contains(w, v);
4845 if (res < 0)
4846 return NULL;
4847 res = !res;
4848 break;
4849 case PyCmp_EXC_MATCH:
4850 if (PyTuple_Check(w)) {
4851 Py_ssize_t i, length;
4852 length = PyTuple_Size(w);
4853 for (i = 0; i < length; i += 1) {
4854 PyObject *exc = PyTuple_GET_ITEM(w, i);
4855 if (!PyExceptionClass_Check(exc)) {
4856 PyErr_SetString(PyExc_TypeError,
4857 CANNOT_CATCH_MSG);
4858 return NULL;
4859 }
4860 }
4861 }
4862 else {
4863 if (!PyExceptionClass_Check(w)) {
4864 PyErr_SetString(PyExc_TypeError,
4865 CANNOT_CATCH_MSG);
4866 return NULL;
4867 }
4868 }
4869 res = PyErr_GivenExceptionMatches(v, w);
4870 break;
4871 default:
4872 return PyObject_RichCompare(v, w, op);
4873 }
4874 v = res ? Py_True : Py_False;
4875 Py_INCREF(v);
4876 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004877}
4878
Thomas Wouters52152252000-08-17 22:55:00 +00004879static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004880import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
4881{
4882 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004883 PyObject *import_func, *res;
4884 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004885
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004886 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004887 if (import_func == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004888 if (!PyErr_Occurred()) {
4889 PyErr_SetString(PyExc_ImportError, "__import__ not found");
4890 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004891 return NULL;
4892 }
4893
4894 /* Fast path for not overloaded __import__. */
Victor Stinnercaba55b2018-08-03 15:33:52 +02004895 if (import_func == _PyInterpreterState_GET_UNSAFE()->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004896 int ilevel = _PyLong_AsInt(level);
4897 if (ilevel == -1 && PyErr_Occurred()) {
4898 return NULL;
4899 }
4900 res = PyImport_ImportModuleLevelObject(
4901 name,
4902 f->f_globals,
4903 f->f_locals == NULL ? Py_None : f->f_locals,
4904 fromlist,
4905 ilevel);
4906 return res;
4907 }
4908
4909 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004910
4911 stack[0] = name;
4912 stack[1] = f->f_globals;
4913 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
4914 stack[3] = fromlist;
4915 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02004916 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004917 Py_DECREF(import_func);
4918 return res;
4919}
4920
4921static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004922import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004923{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004924 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02004925 _Py_IDENTIFIER(__name__);
Xiang Zhang4830f582017-03-21 11:13:42 +08004926 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004927
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004928 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02004929 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004930 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004931 /* Issue #17636: in case this failed because of a circular relative
4932 import, try to fallback on reading the module directly from
4933 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02004934 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07004935 if (pkgname == NULL) {
4936 goto error;
4937 }
Oren Milman6db70332017-09-19 14:23:01 +03004938 if (!PyUnicode_Check(pkgname)) {
4939 Py_CLEAR(pkgname);
4940 goto error;
4941 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004942 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07004943 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08004944 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004945 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07004946 }
Eric Snow3f9eee62017-09-15 16:35:20 -06004947 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004948 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07004949 if (x == NULL) {
4950 goto error;
4951 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004952 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004953 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07004954 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004955 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004956 if (pkgname == NULL) {
4957 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
4958 if (pkgname_or_unknown == NULL) {
4959 Py_XDECREF(pkgpath);
4960 return NULL;
4961 }
4962 } else {
4963 pkgname_or_unknown = pkgname;
4964 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004965
4966 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
4967 PyErr_Clear();
Xiang Zhang4830f582017-03-21 11:13:42 +08004968 errmsg = PyUnicode_FromFormat(
4969 "cannot import name %R from %R (unknown location)",
4970 name, pkgname_or_unknown
4971 );
4972 /* NULL check for errmsg done by PyErr_SetImportError. */
4973 PyErr_SetImportError(errmsg, pkgname, NULL);
4974 }
4975 else {
4976 errmsg = PyUnicode_FromFormat(
4977 "cannot import name %R from %R (%S)",
4978 name, pkgname_or_unknown, pkgpath
4979 );
4980 /* NULL check for errmsg done by PyErr_SetImportError. */
4981 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004982 }
4983
Xiang Zhang4830f582017-03-21 11:13:42 +08004984 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004985 Py_XDECREF(pkgname_or_unknown);
4986 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07004987 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00004988}
Guido van Rossumac7be682001-01-17 15:42:30 +00004989
Thomas Wouters52152252000-08-17 22:55:00 +00004990static int
4991import_all_from(PyObject *locals, PyObject *v)
4992{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004993 _Py_IDENTIFIER(__all__);
4994 _Py_IDENTIFIER(__dict__);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08004995 _Py_IDENTIFIER(__name__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004996 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004997 int skip_leading_underscores = 0;
4998 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004999
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005000 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5001 return -1; /* Unexpected error */
5002 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005003 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005004 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5005 return -1;
5006 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005007 if (dict == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005008 PyErr_SetString(PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005009 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005010 return -1;
5011 }
5012 all = PyMapping_Keys(dict);
5013 Py_DECREF(dict);
5014 if (all == NULL)
5015 return -1;
5016 skip_leading_underscores = 1;
5017 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005018
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005019 for (pos = 0, err = 0; ; pos++) {
5020 name = PySequence_GetItem(all, pos);
5021 if (name == NULL) {
5022 if (!PyErr_ExceptionMatches(PyExc_IndexError))
5023 err = -1;
5024 else
5025 PyErr_Clear();
5026 break;
5027 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005028 if (!PyUnicode_Check(name)) {
5029 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5030 if (modname == NULL) {
5031 Py_DECREF(name);
5032 err = -1;
5033 break;
5034 }
5035 if (!PyUnicode_Check(modname)) {
5036 PyErr_Format(PyExc_TypeError,
5037 "module __name__ must be a string, not %.100s",
5038 Py_TYPE(modname)->tp_name);
5039 }
5040 else {
5041 PyErr_Format(PyExc_TypeError,
5042 "%s in %U.%s must be str, not %.100s",
5043 skip_leading_underscores ? "Key" : "Item",
5044 modname,
5045 skip_leading_underscores ? "__dict__" : "__all__",
5046 Py_TYPE(name)->tp_name);
5047 }
5048 Py_DECREF(modname);
5049 Py_DECREF(name);
5050 err = -1;
5051 break;
5052 }
5053 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005054 if (PyUnicode_READY(name) == -1) {
5055 Py_DECREF(name);
5056 err = -1;
5057 break;
5058 }
5059 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5060 Py_DECREF(name);
5061 continue;
5062 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005063 }
5064 value = PyObject_GetAttr(v, name);
5065 if (value == NULL)
5066 err = -1;
5067 else if (PyDict_CheckExact(locals))
5068 err = PyDict_SetItem(locals, name, value);
5069 else
5070 err = PyObject_SetItem(locals, name, value);
5071 Py_DECREF(name);
5072 Py_XDECREF(value);
5073 if (err != 0)
5074 break;
5075 }
5076 Py_DECREF(all);
5077 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005078}
5079
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005080static int
5081check_args_iterable(PyObject *func, PyObject *args)
5082{
5083 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
5084 PyErr_Format(PyExc_TypeError,
5085 "%.200s%.200s argument after * "
5086 "must be an iterable, not %.200s",
5087 PyEval_GetFuncName(func),
5088 PyEval_GetFuncDesc(func),
5089 args->ob_type->tp_name);
5090 return -1;
5091 }
5092 return 0;
5093}
5094
5095static void
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005096format_kwargs_error(PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005097{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005098 /* _PyDict_MergeEx raises attribute
5099 * error (percolated from an attempt
5100 * to get 'keys' attribute) instead of
5101 * a type error if its second argument
5102 * is not a mapping.
5103 */
5104 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
5105 PyErr_Format(PyExc_TypeError,
5106 "%.200s%.200s argument after ** "
5107 "must be a mapping, not %.200s",
5108 PyEval_GetFuncName(func),
5109 PyEval_GetFuncDesc(func),
5110 kwargs->ob_type->tp_name);
5111 }
5112 else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
5113 PyObject *exc, *val, *tb;
5114 PyErr_Fetch(&exc, &val, &tb);
5115 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
5116 PyObject *key = PyTuple_GET_ITEM(val, 0);
5117 if (!PyUnicode_Check(key)) {
5118 PyErr_Format(PyExc_TypeError,
5119 "%.200s%.200s keywords must be strings",
5120 PyEval_GetFuncName(func),
5121 PyEval_GetFuncDesc(func));
5122 } else {
5123 PyErr_Format(PyExc_TypeError,
5124 "%.200s%.200s got multiple "
5125 "values for keyword argument '%U'",
5126 PyEval_GetFuncName(func),
5127 PyEval_GetFuncDesc(func),
5128 key);
5129 }
5130 Py_XDECREF(exc);
5131 Py_XDECREF(val);
5132 Py_XDECREF(tb);
5133 }
5134 else {
5135 PyErr_Restore(exc, val, tb);
5136 }
5137 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005138}
5139
Guido van Rossumac7be682001-01-17 15:42:30 +00005140static void
Neal Norwitzda059e32007-08-26 05:33:45 +00005141format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005142{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005143 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005144
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005145 if (!obj)
5146 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005147
Serhiy Storchaka06515832016-11-20 09:13:07 +02005148 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005149 if (!obj_str)
5150 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005152 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005153}
Guido van Rossum950361c1997-01-24 13:49:28 +00005154
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005155static void
5156format_exc_unbound(PyCodeObject *co, int oparg)
5157{
5158 PyObject *name;
5159 /* Don't stomp existing exception */
5160 if (PyErr_Occurred())
5161 return;
5162 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5163 name = PyTuple_GET_ITEM(co->co_cellvars,
5164 oparg);
5165 format_exc_check_arg(
5166 PyExc_UnboundLocalError,
5167 UNBOUNDLOCAL_ERROR_MSG,
5168 name);
5169 } else {
5170 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5171 PyTuple_GET_SIZE(co->co_cellvars));
5172 format_exc_check_arg(PyExc_NameError,
5173 UNBOUNDFREE_ERROR_MSG, name);
5174 }
5175}
5176
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005177static void
5178format_awaitable_error(PyTypeObject *type, int prevopcode)
5179{
5180 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5181 if (prevopcode == BEFORE_ASYNC_WITH) {
5182 PyErr_Format(PyExc_TypeError,
5183 "'async with' received an object from __aenter__ "
5184 "that does not implement __await__: %.100s",
5185 type->tp_name);
5186 }
5187 else if (prevopcode == WITH_CLEANUP_START) {
5188 PyErr_Format(PyExc_TypeError,
5189 "'async with' received an object from __aexit__ "
5190 "that does not implement __await__: %.100s",
5191 type->tp_name);
5192 }
5193 }
5194}
5195
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005196static PyObject *
5197unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005198 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005199{
5200 PyObject *res;
5201 if (Py_REFCNT(v) == 2) {
5202 /* In the common case, there are 2 references to the value
5203 * stored in 'variable' when the += is performed: one on the
5204 * value stack (in 'v') and one still stored in the
5205 * 'variable'. We try to delete the variable now to reduce
5206 * the refcnt to 1.
5207 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005208 int opcode, oparg;
5209 NEXTOPARG();
5210 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005211 case STORE_FAST:
5212 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005213 PyObject **fastlocals = f->f_localsplus;
5214 if (GETLOCAL(oparg) == v)
5215 SETLOCAL(oparg, NULL);
5216 break;
5217 }
5218 case STORE_DEREF:
5219 {
5220 PyObject **freevars = (f->f_localsplus +
5221 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005222 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005223 if (PyCell_GET(c) == v) {
5224 PyCell_SET(c, NULL);
5225 Py_DECREF(v);
5226 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005227 break;
5228 }
5229 case STORE_NAME:
5230 {
5231 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005232 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005233 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005234 if (locals && PyDict_CheckExact(locals)) {
5235 PyObject *w = PyDict_GetItemWithError(locals, name);
5236 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
5237 (w == NULL && PyErr_Occurred()))
5238 {
5239 Py_DECREF(v);
5240 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005241 }
5242 }
5243 break;
5244 }
5245 }
5246 }
5247 res = v;
5248 PyUnicode_Append(&res, w);
5249 return res;
5250}
5251
Guido van Rossum950361c1997-01-24 13:49:28 +00005252#ifdef DYNAMIC_EXECUTION_PROFILE
5253
Skip Montanarof118cb12001-10-15 20:51:38 +00005254static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005255getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005256{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005257 int i;
5258 PyObject *l = PyList_New(256);
5259 if (l == NULL) return NULL;
5260 for (i = 0; i < 256; i++) {
5261 PyObject *x = PyLong_FromLong(a[i]);
5262 if (x == NULL) {
5263 Py_DECREF(l);
5264 return NULL;
5265 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005266 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005267 }
5268 for (i = 0; i < 256; i++)
5269 a[i] = 0;
5270 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005271}
5272
5273PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005274_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005275{
5276#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005277 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005278#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005279 int i;
5280 PyObject *l = PyList_New(257);
5281 if (l == NULL) return NULL;
5282 for (i = 0; i < 257; i++) {
5283 PyObject *x = getarray(dxpairs[i]);
5284 if (x == NULL) {
5285 Py_DECREF(l);
5286 return NULL;
5287 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005288 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005289 }
5290 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005291#endif
5292}
5293
5294#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005295
5296Py_ssize_t
5297_PyEval_RequestCodeExtraIndex(freefunc free)
5298{
Victor Stinnercaba55b2018-08-03 15:33:52 +02005299 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -07005300 Py_ssize_t new_index;
5301
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005302 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005303 return -1;
5304 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005305 new_index = interp->co_extra_user_count++;
5306 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005307 return new_index;
5308}
Łukasz Langaa785c872016-09-09 17:37:37 -07005309
5310static void
5311dtrace_function_entry(PyFrameObject *f)
5312{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005313 const char *filename;
5314 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005315 int lineno;
5316
5317 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5318 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5319 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5320
5321 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5322}
5323
5324static void
5325dtrace_function_return(PyFrameObject *f)
5326{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005327 const char *filename;
5328 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005329 int lineno;
5330
5331 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5332 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5333 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5334
5335 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5336}
5337
5338/* DTrace equivalent of maybe_call_line_trace. */
5339static void
5340maybe_dtrace_line(PyFrameObject *frame,
5341 int *instr_lb, int *instr_ub, int *instr_prev)
5342{
5343 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005344 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005345
5346 /* If the last instruction executed isn't in the current
5347 instruction window, reset the window.
5348 */
5349 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5350 PyAddrPair bounds;
5351 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5352 &bounds);
5353 *instr_lb = bounds.ap_lower;
5354 *instr_ub = bounds.ap_upper;
5355 }
5356 /* If the last instruction falls at the start of a line or if
5357 it represents a jump backwards, update the frame's line
5358 number and call the trace function. */
5359 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5360 frame->f_lineno = line;
5361 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5362 if (!co_filename)
5363 co_filename = "?";
5364 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5365 if (!co_name)
5366 co_name = "?";
5367 PyDTrace_LINE(co_filename, co_name, line);
5368 }
5369 *instr_prev = frame->f_lasti;
5370}