blob: be75ade909d7640dee38b206740638812481c3ea [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();
Eric Snowbda918bf2019-03-01 13:15:45 -0700640 _Py_atomic_int *eval_breaker = &tstate->interp->ceval.eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 is true when the line being executed has changed. The
648 initial values are such as to make this false the first
649 time it is tested. */
650 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000651
Serhiy Storchakaab874002016-09-11 13:48:15 +0300652 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 PyObject *names;
654 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000655
Brett Cannon368b4b72012-04-02 12:17:59 -0400656#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200657 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400658#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200659
Antoine Pitroub52ec782009-01-25 16:34:23 +0000660/* Computed GOTOs, or
661 the-optimization-commonly-but-improperly-known-as-"threaded code"
662 using gcc's labels-as-values extension
663 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
664
665 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000667 combined with a lookup table of jump addresses. However, since the
668 indirect jump instruction is shared by all opcodes, the CPU will have a
669 hard time making the right prediction for where to jump next (actually,
670 it will be always wrong except in the uncommon case of a sequence of
671 several identical opcodes).
672
673 "Threaded code" in contrast, uses an explicit jump table and an explicit
674 indirect jump instruction at the end of each opcode. Since the jump
675 instruction is at a different address for each opcode, the CPU will make a
676 separate prediction for each of these instructions, which is equivalent to
677 predicting the second opcode of each opcode pair. These predictions have
678 a much better chance to turn out valid, especially in small bytecode loops.
679
680 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000682 and potentially many more instructions (depending on the pipeline width).
683 A correctly predicted branch, however, is nearly free.
684
685 At the time of this writing, the "threaded code" version is up to 15-20%
686 faster than the normal "switch" version, depending on the compiler and the
687 CPU architecture.
688
689 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
690 because it would render the measurements invalid.
691
692
693 NOTE: care must be taken that the compiler doesn't try to "optimize" the
694 indirect jumps by sharing them between all opcodes. Such optimizations
695 can be disabled on gcc by using the -fno-gcse flag (or possibly
696 -fno-crossjumping).
697*/
698
Antoine Pitrou042b1282010-08-13 21:15:58 +0000699#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000700#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000701#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000702#endif
703
Antoine Pitrou042b1282010-08-13 21:15:58 +0000704#ifdef HAVE_COMPUTED_GOTOS
705 #ifndef USE_COMPUTED_GOTOS
706 #define USE_COMPUTED_GOTOS 1
707 #endif
708#else
709 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
710 #error "Computed gotos are not supported on this compiler."
711 #endif
712 #undef USE_COMPUTED_GOTOS
713 #define USE_COMPUTED_GOTOS 0
714#endif
715
716#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000717/* Import the static jump table */
718#include "opcode_targets.h"
719
Antoine Pitroub52ec782009-01-25 16:34:23 +0000720#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -0700721 op: \
722 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000723
Antoine Pitroub52ec782009-01-25 16:34:23 +0000724#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 { \
Eric Snowbda918bf2019-03-01 13:15:45 -0700726 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 FAST_DISPATCH(); \
728 } \
729 continue; \
730 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000731
732#ifdef LLTRACE
733#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700735 if (!lltrace && !_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300737 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300738 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 } \
740 goto fast_next_opcode; \
741 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000742#else
743#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700745 if (!_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000746 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300747 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300748 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 } \
750 goto fast_next_opcode; \
751 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000752#endif
753
754#else
Benjamin Petersonddd19492018-09-16 22:38:02 -0700755#define TARGET(op) op
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300756
Antoine Pitroub52ec782009-01-25 16:34:23 +0000757#define DISPATCH() continue
758#define FAST_DISPATCH() goto fast_next_opcode
759#endif
760
761
Neal Norwitza81d2202002-07-14 00:27:26 +0000762/* Tuple access macros */
763
764#ifndef Py_DEBUG
765#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
766#else
767#define GETITEM(v, i) PyTuple_GetItem((v), (i))
768#endif
769
Guido van Rossum374a9221991-04-04 10:40:29 +0000770/* Code access macros */
771
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300772/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600773#define INSTR_OFFSET() \
774 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300775#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300776 _Py_CODEUNIT word = *next_instr; \
777 opcode = _Py_OPCODE(word); \
778 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300779 next_instr++; \
780 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300781#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
782#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000783
Raymond Hettingerf606f872003-03-16 03:11:04 +0000784/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 Some opcodes tend to come in pairs thus making it possible to
786 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300787 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000788
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 Verifying the prediction costs a single high-speed test of a register
790 variable against a constant. If the pairing was good, then the
791 processor's own internal branch predication has a high likelihood of
792 success, resulting in a nearly zero-overhead transition to the
793 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300794 including its unpredictable switch-case branch. Combined with the
795 processor's internal branch prediction, a successful PREDICT has the
796 effect of making the two opcodes run as if they were a single new opcode
797 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000798
Georg Brandl86b2fb92008-07-16 03:43:04 +0000799 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 predictions turned-on and interpret the results as if some opcodes
801 had been combined or turn-off predictions so that the opcode frequency
802 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000803
804 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 the CPU to record separate branch prediction information for each
806 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000807
Raymond Hettingerf606f872003-03-16 03:11:04 +0000808*/
809
Antoine Pitrou042b1282010-08-13 21:15:58 +0000810#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000812#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300813#define PREDICT(op) \
814 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300815 _Py_CODEUNIT word = *next_instr; \
816 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300817 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300818 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300819 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300820 goto PRED_##op; \
821 } \
822 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000823#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300824#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000825
Raymond Hettingerf606f872003-03-16 03:11:04 +0000826
Guido van Rossum374a9221991-04-04 10:40:29 +0000827/* Stack manipulation macros */
828
Martin v. Löwis18e16552006-02-15 17:27:45 +0000829/* The stack can grow at most MAXINT deep, as co_nlocals and
830 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000831#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
832#define EMPTY() (STACK_LEVEL() == 0)
833#define TOP() (stack_pointer[-1])
834#define SECOND() (stack_pointer[-2])
835#define THIRD() (stack_pointer[-3])
836#define FOURTH() (stack_pointer[-4])
837#define PEEK(n) (stack_pointer[-(n)])
838#define SET_TOP(v) (stack_pointer[-1] = (v))
839#define SET_SECOND(v) (stack_pointer[-2] = (v))
840#define SET_THIRD(v) (stack_pointer[-3] = (v))
841#define SET_FOURTH(v) (stack_pointer[-4] = (v))
842#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
843#define BASIC_STACKADJ(n) (stack_pointer += n)
844#define BASIC_PUSH(v) (*stack_pointer++ = (v))
845#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000846
Guido van Rossum96a42c81992-01-12 02:29:51 +0000847#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000849 lltrace && prtrace(TOP(), "push")); \
850 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000852 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +0000853#define STACK_GROW(n) do { \
854 assert(n >= 0); \
855 (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000856 lltrace && prtrace(TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +0000857 assert(STACK_LEVEL() <= co->co_stacksize); \
858 } while (0)
859#define STACK_SHRINK(n) do { \
860 assert(n >= 0); \
861 (void)(lltrace && prtrace(TOP(), "stackadj")); \
862 (void)(BASIC_STACKADJ(-n)); \
863 assert(STACK_LEVEL() <= co->co_stacksize); \
864 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +0000865#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +0000866 prtrace((STACK_POINTER)[-1], "ext_pop")), \
867 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000868#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000869#define PUSH(v) BASIC_PUSH(v)
870#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +0000871#define STACK_GROW(n) BASIC_STACKADJ(n)
872#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000873#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000874#endif
875
Guido van Rossum681d79a1995-07-18 14:51:37 +0000876/* Local variable macros */
877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000879
880/* The SETLOCAL() macro must not DECREF the local variable in-place and
881 then store the new value; it must copy the old value to a temporary
882 value, then store the new value, and then DECREF the temporary value.
883 This is because it is possible that during the DECREF the frame is
884 accessed by other code (e.g. a __del__ method or gc.collect()) and the
885 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000887 GETLOCAL(i) = value; \
888 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000889
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000890
891#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 while (STACK_LEVEL() > (b)->b_level) { \
893 PyObject *v = POP(); \
894 Py_XDECREF(v); \
895 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000896
897#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300898 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +0100900 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 assert(STACK_LEVEL() >= (b)->b_level + 3); \
902 while (STACK_LEVEL() > (b)->b_level + 3) { \
903 value = POP(); \
904 Py_XDECREF(value); \
905 } \
Mark Shannonae3087c2017-10-22 22:41:51 +0100906 exc_info = tstate->exc_info; \
907 type = exc_info->exc_type; \
908 value = exc_info->exc_value; \
909 traceback = exc_info->exc_traceback; \
910 exc_info->exc_type = POP(); \
911 exc_info->exc_value = POP(); \
912 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 Py_XDECREF(type); \
914 Py_XDECREF(value); \
915 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300916 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000917
Guido van Rossuma027efa1997-05-05 20:56:21 +0000918/* Start of code */
919
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 /* push frame */
921 if (Py_EnterRecursiveCall(""))
922 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000925
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 if (tstate->use_tracing) {
927 if (tstate->c_tracefunc != NULL) {
928 /* tstate->c_tracefunc, if defined, is a
929 function that will be called on *every* entry
930 to a code block. Its return value, if not
931 None, is a function that will be called at
932 the start of each executed line of code.
933 (Actually, the function must return itself
934 in order to continue tracing.) The trace
935 functions are called with three arguments:
936 a pointer to the current frame, a string
937 indicating why the function is called, and
938 an argument which depends on the situation.
939 The global trace function is also called
940 whenever an exception is detected. */
941 if (call_trace_protected(tstate->c_tracefunc,
942 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100943 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 /* Trace function raised an error */
945 goto exit_eval_frame;
946 }
947 }
948 if (tstate->c_profilefunc != NULL) {
949 /* Similar for c_profilefunc, except it needn't
950 return itself and isn't called for "line" events */
951 if (call_trace_protected(tstate->c_profilefunc,
952 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100953 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 /* Profile function raised an error */
955 goto exit_eval_frame;
956 }
957 }
958 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000959
Łukasz Langaa785c872016-09-09 17:37:37 -0700960 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
961 dtrace_function_entry(f);
962
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 co = f->f_code;
964 names = co->co_names;
965 consts = co->co_consts;
966 fastlocals = f->f_localsplus;
967 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300968 assert(PyBytes_Check(co->co_code));
969 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +0300970 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
971 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
972 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300973 /*
974 f->f_lasti refers to the index of the last instruction,
975 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000976
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300977 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500978 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 When the PREDICT() macros are enabled, some opcode pairs follow in
981 direct succession without updating f->f_lasti. A successful
982 prediction effectively links the two codes together as if they
983 were a single new opcode; accordingly,f->f_lasti will point to
984 the first code in the pair (for instance, GET_ITER followed by
985 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300986 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300988 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300989 next_instr = first_instr;
990 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +0300991 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
992 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300993 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 stack_pointer = f->f_stacktop;
995 assert(stack_pointer != NULL);
996 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +0200997 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000998
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000999
Tim Peters5ca576e2001-06-18 22:08:13 +00001000#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001001 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001002#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001003
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001004 if (throwflag) /* support for generator.throw() */
1005 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001006
Victor Stinnerace47d72013-07-18 01:41:08 +02001007#ifdef Py_DEBUG
1008 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001009 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001010 caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +02001011 assert(!PyErr_Occurred());
1012#endif
1013
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001014main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1017 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +02001018 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 /* Do periodic things. Doing this every time through
1021 the loop would add too much overhead, so we do it
1022 only every Nth instruction. We also do it if
1023 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1024 event needs attention (e.g. a signal handler or
1025 async I/O handler); see Py_AddPendingCall() and
1026 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001027
Eric Snowbda918bf2019-03-01 13:15:45 -07001028 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001029 opcode = _Py_OPCODE(*next_instr);
1030 if (opcode == SETUP_FINALLY ||
1031 opcode == SETUP_WITH ||
1032 opcode == BEFORE_ASYNC_WITH ||
1033 opcode == YIELD_FROM) {
1034 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001035 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001036 - If we're about to enter the 'with:'. It will prevent
1037 emitting a resource warning in the common idiom
1038 'with open(path) as file:'.
1039 - If we're about to enter the 'async with:'.
1040 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001041 *very* useful, but might help in some cases and it's
1042 traditional)
1043 - If we're resuming a chain of nested 'yield from' or
1044 'await' calls, then each frame is parked with YIELD_FROM
1045 as its next opcode. If the user hit control-C we want to
1046 wait until we've reached the innermost frame before
1047 running the signal handler and raising KeyboardInterrupt
1048 (see bpo-30039).
1049 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 goto fast_next_opcode;
1051 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001052
1053 if (_Py_atomic_load_relaxed(
1054 &_PyRuntime.ceval.signals_pending))
1055 {
1056 if (handle_signals() != 0) {
1057 goto error;
1058 }
1059 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001060 if (_Py_atomic_load_relaxed(
Eric Snowef4ac962019-02-24 15:40:47 -08001061 &(tstate->interp->ceval.pending.calls_to_do)))
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001062 {
Eric Snowef4ac962019-02-24 15:40:47 -08001063 if (_Py_MakePendingCalls(tstate->interp) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001064 goto error;
Eric Snowfdf282d2019-01-11 14:26:55 -07001065 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001067
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001068 if (_Py_atomic_load_relaxed(
1069 &_PyRuntime.ceval.gil_drop_request))
1070 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 /* Give another thread a chance */
1072 if (PyThreadState_Swap(NULL) != tstate)
1073 Py_FatalError("ceval: tstate mix-up");
1074 drop_gil(tstate);
1075
1076 /* Other threads may run now */
1077
1078 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001079
1080 /* Check if we should make a quick exit. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001081 if (_Py_IsFinalizing() &&
1082 !_Py_CURRENTLY_FINALIZING(tstate))
1083 {
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001084 drop_gil(tstate);
1085 PyThread_exit_thread();
1086 }
1087
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 if (PyThreadState_Swap(tstate) != NULL)
1089 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 }
1091 /* Check for asynchronous exceptions. */
1092 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001093 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 tstate->async_exc = NULL;
Eric Snowef4ac962019-02-24 15:40:47 -08001095 UNSIGNAL_ASYNC_EXC(tstate->interp);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001096 PyErr_SetNone(exc);
1097 Py_DECREF(exc);
1098 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 }
1100 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001101
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001102 fast_next_opcode:
1103 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001104
Łukasz Langaa785c872016-09-09 17:37:37 -07001105 if (PyDTrace_LINE_ENABLED())
1106 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1107
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001109
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001110 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001111 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001112 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 /* see maybe_call_line_trace
1114 for expository comments */
1115 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001116
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 err = maybe_call_line_trace(tstate->c_tracefunc,
1118 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001119 tstate, f,
1120 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 /* Reload possibly changed frame fields */
1122 JUMPTO(f->f_lasti);
1123 if (f->f_stacktop != NULL) {
1124 stack_pointer = f->f_stacktop;
1125 f->f_stacktop = NULL;
1126 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001127 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001128 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001129 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001131
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001133
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001134 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001135 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001136#ifdef DYNAMIC_EXECUTION_PROFILE
1137#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 dxpairs[lastopcode][opcode]++;
1139 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001140#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001141 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001142#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001143
Guido van Rossum96a42c81992-01-12 02:29:51 +00001144#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 if (lltrace) {
1148 if (HAS_ARG(opcode)) {
1149 printf("%d: %d, %d\n",
1150 f->f_lasti, opcode, oparg);
1151 }
1152 else {
1153 printf("%d: %d\n",
1154 f->f_lasti, opcode);
1155 }
1156 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001157#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001158
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001160
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001162 It is essential that any operation that fails must goto error
1163 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001164
Benjamin Petersonddd19492018-09-16 22:38:02 -07001165 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001167 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001168
Benjamin Petersonddd19492018-09-16 22:38:02 -07001169 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001170 PyObject *value = GETLOCAL(oparg);
1171 if (value == NULL) {
1172 format_exc_check_arg(PyExc_UnboundLocalError,
1173 UNBOUNDLOCAL_ERROR_MSG,
1174 PyTuple_GetItem(co->co_varnames, oparg));
1175 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001176 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001177 Py_INCREF(value);
1178 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001180 }
1181
Benjamin Petersonddd19492018-09-16 22:38:02 -07001182 case TARGET(LOAD_CONST): {
1183 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001184 PyObject *value = GETITEM(consts, oparg);
1185 Py_INCREF(value);
1186 PUSH(value);
1187 FAST_DISPATCH();
1188 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001189
Benjamin Petersonddd19492018-09-16 22:38:02 -07001190 case TARGET(STORE_FAST): {
1191 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001192 PyObject *value = POP();
1193 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001195 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001196
Benjamin Petersonddd19492018-09-16 22:38:02 -07001197 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001198 PyObject *value = POP();
1199 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001201 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001202
Benjamin Petersonddd19492018-09-16 22:38:02 -07001203 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001204 PyObject *top = TOP();
1205 PyObject *second = SECOND();
1206 SET_TOP(second);
1207 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001209 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001210
Benjamin Petersonddd19492018-09-16 22:38:02 -07001211 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001212 PyObject *top = TOP();
1213 PyObject *second = SECOND();
1214 PyObject *third = THIRD();
1215 SET_TOP(second);
1216 SET_SECOND(third);
1217 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001219 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001220
Benjamin Petersonddd19492018-09-16 22:38:02 -07001221 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001222 PyObject *top = TOP();
1223 PyObject *second = SECOND();
1224 PyObject *third = THIRD();
1225 PyObject *fourth = FOURTH();
1226 SET_TOP(second);
1227 SET_SECOND(third);
1228 SET_THIRD(fourth);
1229 SET_FOURTH(top);
1230 FAST_DISPATCH();
1231 }
1232
Benjamin Petersonddd19492018-09-16 22:38:02 -07001233 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001234 PyObject *top = TOP();
1235 Py_INCREF(top);
1236 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001238 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001239
Benjamin Petersonddd19492018-09-16 22:38:02 -07001240 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001241 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001242 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001243 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001244 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001245 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001246 SET_TOP(top);
1247 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001248 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001249 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001250
Benjamin Petersonddd19492018-09-16 22:38:02 -07001251 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001252 PyObject *value = TOP();
1253 PyObject *res = PyNumber_Positive(value);
1254 Py_DECREF(value);
1255 SET_TOP(res);
1256 if (res == NULL)
1257 goto error;
1258 DISPATCH();
1259 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001260
Benjamin Petersonddd19492018-09-16 22:38:02 -07001261 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001262 PyObject *value = TOP();
1263 PyObject *res = PyNumber_Negative(value);
1264 Py_DECREF(value);
1265 SET_TOP(res);
1266 if (res == NULL)
1267 goto error;
1268 DISPATCH();
1269 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001270
Benjamin Petersonddd19492018-09-16 22:38:02 -07001271 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001272 PyObject *value = TOP();
1273 int err = PyObject_IsTrue(value);
1274 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 if (err == 0) {
1276 Py_INCREF(Py_True);
1277 SET_TOP(Py_True);
1278 DISPATCH();
1279 }
1280 else if (err > 0) {
1281 Py_INCREF(Py_False);
1282 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 DISPATCH();
1284 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001285 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001286 goto error;
1287 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001288
Benjamin Petersonddd19492018-09-16 22:38:02 -07001289 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001290 PyObject *value = TOP();
1291 PyObject *res = PyNumber_Invert(value);
1292 Py_DECREF(value);
1293 SET_TOP(res);
1294 if (res == NULL)
1295 goto error;
1296 DISPATCH();
1297 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001298
Benjamin Petersonddd19492018-09-16 22:38:02 -07001299 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001300 PyObject *exp = POP();
1301 PyObject *base = TOP();
1302 PyObject *res = PyNumber_Power(base, exp, Py_None);
1303 Py_DECREF(base);
1304 Py_DECREF(exp);
1305 SET_TOP(res);
1306 if (res == NULL)
1307 goto error;
1308 DISPATCH();
1309 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001310
Benjamin Petersonddd19492018-09-16 22:38:02 -07001311 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001312 PyObject *right = POP();
1313 PyObject *left = TOP();
1314 PyObject *res = PyNumber_Multiply(left, right);
1315 Py_DECREF(left);
1316 Py_DECREF(right);
1317 SET_TOP(res);
1318 if (res == NULL)
1319 goto error;
1320 DISPATCH();
1321 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001322
Benjamin Petersonddd19492018-09-16 22:38:02 -07001323 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001324 PyObject *right = POP();
1325 PyObject *left = TOP();
1326 PyObject *res = PyNumber_MatrixMultiply(left, right);
1327 Py_DECREF(left);
1328 Py_DECREF(right);
1329 SET_TOP(res);
1330 if (res == NULL)
1331 goto error;
1332 DISPATCH();
1333 }
1334
Benjamin Petersonddd19492018-09-16 22:38:02 -07001335 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001336 PyObject *divisor = POP();
1337 PyObject *dividend = TOP();
1338 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1339 Py_DECREF(dividend);
1340 Py_DECREF(divisor);
1341 SET_TOP(quotient);
1342 if (quotient == NULL)
1343 goto error;
1344 DISPATCH();
1345 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001346
Benjamin Petersonddd19492018-09-16 22:38:02 -07001347 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001348 PyObject *divisor = POP();
1349 PyObject *dividend = TOP();
1350 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1351 Py_DECREF(dividend);
1352 Py_DECREF(divisor);
1353 SET_TOP(quotient);
1354 if (quotient == NULL)
1355 goto error;
1356 DISPATCH();
1357 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001358
Benjamin Petersonddd19492018-09-16 22:38:02 -07001359 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001360 PyObject *divisor = POP();
1361 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001362 PyObject *res;
1363 if (PyUnicode_CheckExact(dividend) && (
1364 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1365 // fast path; string formatting, but not if the RHS is a str subclass
1366 // (see issue28598)
1367 res = PyUnicode_Format(dividend, divisor);
1368 } else {
1369 res = PyNumber_Remainder(dividend, divisor);
1370 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001371 Py_DECREF(divisor);
1372 Py_DECREF(dividend);
1373 SET_TOP(res);
1374 if (res == NULL)
1375 goto error;
1376 DISPATCH();
1377 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001378
Benjamin Petersonddd19492018-09-16 22:38:02 -07001379 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001380 PyObject *right = POP();
1381 PyObject *left = TOP();
1382 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001383 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1384 CPython using bytecode, it is simply worthless.
1385 See http://bugs.python.org/issue21955 and
1386 http://bugs.python.org/issue10044 for the discussion. In short,
1387 no patch shown any impact on a realistic benchmark, only a minor
1388 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001389 if (PyUnicode_CheckExact(left) &&
1390 PyUnicode_CheckExact(right)) {
1391 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001392 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001393 }
1394 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001395 sum = PyNumber_Add(left, right);
1396 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001397 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001398 Py_DECREF(right);
1399 SET_TOP(sum);
1400 if (sum == NULL)
1401 goto error;
1402 DISPATCH();
1403 }
1404
Benjamin Petersonddd19492018-09-16 22:38:02 -07001405 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001406 PyObject *right = POP();
1407 PyObject *left = TOP();
1408 PyObject *diff = PyNumber_Subtract(left, right);
1409 Py_DECREF(right);
1410 Py_DECREF(left);
1411 SET_TOP(diff);
1412 if (diff == NULL)
1413 goto error;
1414 DISPATCH();
1415 }
1416
Benjamin Petersonddd19492018-09-16 22:38:02 -07001417 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001418 PyObject *sub = POP();
1419 PyObject *container = TOP();
1420 PyObject *res = PyObject_GetItem(container, sub);
1421 Py_DECREF(container);
1422 Py_DECREF(sub);
1423 SET_TOP(res);
1424 if (res == NULL)
1425 goto error;
1426 DISPATCH();
1427 }
1428
Benjamin Petersonddd19492018-09-16 22:38:02 -07001429 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001430 PyObject *right = POP();
1431 PyObject *left = TOP();
1432 PyObject *res = PyNumber_Lshift(left, right);
1433 Py_DECREF(left);
1434 Py_DECREF(right);
1435 SET_TOP(res);
1436 if (res == NULL)
1437 goto error;
1438 DISPATCH();
1439 }
1440
Benjamin Petersonddd19492018-09-16 22:38:02 -07001441 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001442 PyObject *right = POP();
1443 PyObject *left = TOP();
1444 PyObject *res = PyNumber_Rshift(left, right);
1445 Py_DECREF(left);
1446 Py_DECREF(right);
1447 SET_TOP(res);
1448 if (res == NULL)
1449 goto error;
1450 DISPATCH();
1451 }
1452
Benjamin Petersonddd19492018-09-16 22:38:02 -07001453 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001454 PyObject *right = POP();
1455 PyObject *left = TOP();
1456 PyObject *res = PyNumber_And(left, right);
1457 Py_DECREF(left);
1458 Py_DECREF(right);
1459 SET_TOP(res);
1460 if (res == NULL)
1461 goto error;
1462 DISPATCH();
1463 }
1464
Benjamin Petersonddd19492018-09-16 22:38:02 -07001465 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001466 PyObject *right = POP();
1467 PyObject *left = TOP();
1468 PyObject *res = PyNumber_Xor(left, right);
1469 Py_DECREF(left);
1470 Py_DECREF(right);
1471 SET_TOP(res);
1472 if (res == NULL)
1473 goto error;
1474 DISPATCH();
1475 }
1476
Benjamin Petersonddd19492018-09-16 22:38:02 -07001477 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001478 PyObject *right = POP();
1479 PyObject *left = TOP();
1480 PyObject *res = PyNumber_Or(left, right);
1481 Py_DECREF(left);
1482 Py_DECREF(right);
1483 SET_TOP(res);
1484 if (res == NULL)
1485 goto error;
1486 DISPATCH();
1487 }
1488
Benjamin Petersonddd19492018-09-16 22:38:02 -07001489 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001490 PyObject *v = POP();
1491 PyObject *list = PEEK(oparg);
1492 int err;
1493 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001494 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001495 if (err != 0)
1496 goto error;
1497 PREDICT(JUMP_ABSOLUTE);
1498 DISPATCH();
1499 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001500
Benjamin Petersonddd19492018-09-16 22:38:02 -07001501 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001502 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001503 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001504 int err;
1505 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001506 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001507 if (err != 0)
1508 goto error;
1509 PREDICT(JUMP_ABSOLUTE);
1510 DISPATCH();
1511 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001512
Benjamin Petersonddd19492018-09-16 22:38:02 -07001513 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001514 PyObject *exp = POP();
1515 PyObject *base = TOP();
1516 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1517 Py_DECREF(base);
1518 Py_DECREF(exp);
1519 SET_TOP(res);
1520 if (res == NULL)
1521 goto error;
1522 DISPATCH();
1523 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001524
Benjamin Petersonddd19492018-09-16 22:38:02 -07001525 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001526 PyObject *right = POP();
1527 PyObject *left = TOP();
1528 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1529 Py_DECREF(left);
1530 Py_DECREF(right);
1531 SET_TOP(res);
1532 if (res == NULL)
1533 goto error;
1534 DISPATCH();
1535 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001536
Benjamin Petersonddd19492018-09-16 22:38:02 -07001537 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001538 PyObject *right = POP();
1539 PyObject *left = TOP();
1540 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1541 Py_DECREF(left);
1542 Py_DECREF(right);
1543 SET_TOP(res);
1544 if (res == NULL)
1545 goto error;
1546 DISPATCH();
1547 }
1548
Benjamin Petersonddd19492018-09-16 22:38:02 -07001549 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001550 PyObject *divisor = POP();
1551 PyObject *dividend = TOP();
1552 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1553 Py_DECREF(dividend);
1554 Py_DECREF(divisor);
1555 SET_TOP(quotient);
1556 if (quotient == NULL)
1557 goto error;
1558 DISPATCH();
1559 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001560
Benjamin Petersonddd19492018-09-16 22:38:02 -07001561 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001562 PyObject *divisor = POP();
1563 PyObject *dividend = TOP();
1564 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1565 Py_DECREF(dividend);
1566 Py_DECREF(divisor);
1567 SET_TOP(quotient);
1568 if (quotient == NULL)
1569 goto error;
1570 DISPATCH();
1571 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001572
Benjamin Petersonddd19492018-09-16 22:38:02 -07001573 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001574 PyObject *right = POP();
1575 PyObject *left = TOP();
1576 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1577 Py_DECREF(left);
1578 Py_DECREF(right);
1579 SET_TOP(mod);
1580 if (mod == NULL)
1581 goto error;
1582 DISPATCH();
1583 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001584
Benjamin Petersonddd19492018-09-16 22:38:02 -07001585 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001586 PyObject *right = POP();
1587 PyObject *left = TOP();
1588 PyObject *sum;
1589 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1590 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001591 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001592 }
1593 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001594 sum = PyNumber_InPlaceAdd(left, right);
1595 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001596 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001597 Py_DECREF(right);
1598 SET_TOP(sum);
1599 if (sum == NULL)
1600 goto error;
1601 DISPATCH();
1602 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001603
Benjamin Petersonddd19492018-09-16 22:38:02 -07001604 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001605 PyObject *right = POP();
1606 PyObject *left = TOP();
1607 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1608 Py_DECREF(left);
1609 Py_DECREF(right);
1610 SET_TOP(diff);
1611 if (diff == NULL)
1612 goto error;
1613 DISPATCH();
1614 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001615
Benjamin Petersonddd19492018-09-16 22:38:02 -07001616 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001617 PyObject *right = POP();
1618 PyObject *left = TOP();
1619 PyObject *res = PyNumber_InPlaceLshift(left, right);
1620 Py_DECREF(left);
1621 Py_DECREF(right);
1622 SET_TOP(res);
1623 if (res == NULL)
1624 goto error;
1625 DISPATCH();
1626 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001627
Benjamin Petersonddd19492018-09-16 22:38:02 -07001628 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001629 PyObject *right = POP();
1630 PyObject *left = TOP();
1631 PyObject *res = PyNumber_InPlaceRshift(left, right);
1632 Py_DECREF(left);
1633 Py_DECREF(right);
1634 SET_TOP(res);
1635 if (res == NULL)
1636 goto error;
1637 DISPATCH();
1638 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001639
Benjamin Petersonddd19492018-09-16 22:38:02 -07001640 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001641 PyObject *right = POP();
1642 PyObject *left = TOP();
1643 PyObject *res = PyNumber_InPlaceAnd(left, right);
1644 Py_DECREF(left);
1645 Py_DECREF(right);
1646 SET_TOP(res);
1647 if (res == NULL)
1648 goto error;
1649 DISPATCH();
1650 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001651
Benjamin Petersonddd19492018-09-16 22:38:02 -07001652 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001653 PyObject *right = POP();
1654 PyObject *left = TOP();
1655 PyObject *res = PyNumber_InPlaceXor(left, right);
1656 Py_DECREF(left);
1657 Py_DECREF(right);
1658 SET_TOP(res);
1659 if (res == NULL)
1660 goto error;
1661 DISPATCH();
1662 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001663
Benjamin Petersonddd19492018-09-16 22:38:02 -07001664 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001665 PyObject *right = POP();
1666 PyObject *left = TOP();
1667 PyObject *res = PyNumber_InPlaceOr(left, right);
1668 Py_DECREF(left);
1669 Py_DECREF(right);
1670 SET_TOP(res);
1671 if (res == NULL)
1672 goto error;
1673 DISPATCH();
1674 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001675
Benjamin Petersonddd19492018-09-16 22:38:02 -07001676 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001677 PyObject *sub = TOP();
1678 PyObject *container = SECOND();
1679 PyObject *v = THIRD();
1680 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001681 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001682 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001683 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001685 Py_DECREF(container);
1686 Py_DECREF(sub);
1687 if (err != 0)
1688 goto error;
1689 DISPATCH();
1690 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001691
Benjamin Petersonddd19492018-09-16 22:38:02 -07001692 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001693 PyObject *sub = TOP();
1694 PyObject *container = SECOND();
1695 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001696 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00001697 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001698 err = PyObject_DelItem(container, sub);
1699 Py_DECREF(container);
1700 Py_DECREF(sub);
1701 if (err != 0)
1702 goto error;
1703 DISPATCH();
1704 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001705
Benjamin Petersonddd19492018-09-16 22:38:02 -07001706 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01001707 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001708 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001709 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001710 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001711 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 PyErr_SetString(PyExc_RuntimeError,
1713 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001714 Py_DECREF(value);
1715 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001716 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001717 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001718 Py_DECREF(value);
1719 if (res == NULL)
1720 goto error;
1721 Py_DECREF(res);
1722 DISPATCH();
1723 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001724
Benjamin Petersonddd19492018-09-16 22:38:02 -07001725 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001726 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001727 switch (oparg) {
1728 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001729 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001730 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001731 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001732 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001733 /* fall through */
1734 case 0:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001735 if (do_raise(exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001736 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001737 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001738 break;
1739 default:
1740 PyErr_SetString(PyExc_SystemError,
1741 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742 break;
1743 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001744 goto error;
1745 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001746
Benjamin Petersonddd19492018-09-16 22:38:02 -07001747 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001748 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001749 assert(f->f_iblock == 0);
1750 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001751 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001752
Benjamin Petersonddd19492018-09-16 22:38:02 -07001753 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001754 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001755 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001756 PyObject *obj = TOP();
1757 PyTypeObject *type = Py_TYPE(obj);
1758
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001759 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001760 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001761 }
Yury Selivanov75445082015-05-11 22:57:16 -04001762
1763 if (getter != NULL) {
1764 iter = (*getter)(obj);
1765 Py_DECREF(obj);
1766 if (iter == NULL) {
1767 SET_TOP(NULL);
1768 goto error;
1769 }
1770 }
1771 else {
1772 SET_TOP(NULL);
1773 PyErr_Format(
1774 PyExc_TypeError,
1775 "'async for' requires an object with "
1776 "__aiter__ method, got %.100s",
1777 type->tp_name);
1778 Py_DECREF(obj);
1779 goto error;
1780 }
1781
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001782 if (Py_TYPE(iter)->tp_as_async == NULL ||
1783 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001784
Yury Selivanov398ff912017-03-02 22:20:00 -05001785 SET_TOP(NULL);
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001786 PyErr_Format(
1787 PyExc_TypeError,
1788 "'async for' received an object from __aiter__ "
1789 "that does not implement __anext__: %.100s",
1790 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001791 Py_DECREF(iter);
1792 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001793 }
1794
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001795 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001796 DISPATCH();
1797 }
1798
Benjamin Petersonddd19492018-09-16 22:38:02 -07001799 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001800 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001801 PyObject *next_iter = NULL;
1802 PyObject *awaitable = NULL;
1803 PyObject *aiter = TOP();
1804 PyTypeObject *type = Py_TYPE(aiter);
1805
Yury Selivanoveb636452016-09-08 22:01:51 -07001806 if (PyAsyncGen_CheckExact(aiter)) {
1807 awaitable = type->tp_as_async->am_anext(aiter);
1808 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001809 goto error;
1810 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001811 } else {
1812 if (type->tp_as_async != NULL){
1813 getter = type->tp_as_async->am_anext;
1814 }
Yury Selivanov75445082015-05-11 22:57:16 -04001815
Yury Selivanoveb636452016-09-08 22:01:51 -07001816 if (getter != NULL) {
1817 next_iter = (*getter)(aiter);
1818 if (next_iter == NULL) {
1819 goto error;
1820 }
1821 }
1822 else {
1823 PyErr_Format(
1824 PyExc_TypeError,
1825 "'async for' requires an iterator with "
1826 "__anext__ method, got %.100s",
1827 type->tp_name);
1828 goto error;
1829 }
Yury Selivanov75445082015-05-11 22:57:16 -04001830
Yury Selivanoveb636452016-09-08 22:01:51 -07001831 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1832 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001833 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001834 PyExc_TypeError,
1835 "'async for' received an invalid object "
1836 "from __anext__: %.100s",
1837 Py_TYPE(next_iter)->tp_name);
1838
1839 Py_DECREF(next_iter);
1840 goto error;
1841 } else {
1842 Py_DECREF(next_iter);
1843 }
1844 }
Yury Selivanov75445082015-05-11 22:57:16 -04001845
1846 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001847 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001848 DISPATCH();
1849 }
1850
Benjamin Petersonddd19492018-09-16 22:38:02 -07001851 case TARGET(GET_AWAITABLE): {
1852 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04001853 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04001854 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04001855
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03001856 if (iter == NULL) {
1857 format_awaitable_error(Py_TYPE(iterable),
1858 _Py_OPCODE(next_instr[-2]));
1859 }
1860
Yury Selivanov75445082015-05-11 22:57:16 -04001861 Py_DECREF(iterable);
1862
Yury Selivanovc724bae2016-03-02 11:30:46 -05001863 if (iter != NULL && PyCoro_CheckExact(iter)) {
1864 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
1865 if (yf != NULL) {
1866 /* `iter` is a coroutine object that is being
1867 awaited, `yf` is a pointer to the current awaitable
1868 being awaited on. */
1869 Py_DECREF(yf);
1870 Py_CLEAR(iter);
1871 PyErr_SetString(
1872 PyExc_RuntimeError,
1873 "coroutine is being awaited already");
1874 /* The code below jumps to `error` if `iter` is NULL. */
1875 }
1876 }
1877
Yury Selivanov75445082015-05-11 22:57:16 -04001878 SET_TOP(iter); /* Even if it's NULL */
1879
1880 if (iter == NULL) {
1881 goto error;
1882 }
1883
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001884 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001885 DISPATCH();
1886 }
1887
Benjamin Petersonddd19492018-09-16 22:38:02 -07001888 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001889 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001890 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001891 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001892 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
1893 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001894 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001895 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001896 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001897 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001898 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001899 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001900 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001901 Py_DECREF(v);
1902 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001903 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08001904 if (tstate->c_tracefunc != NULL
1905 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001906 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10001907 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001908 if (err < 0)
1909 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001910 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001911 SET_TOP(val);
1912 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001913 }
Martin Panter95f53c12016-07-18 08:23:26 +00001914 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001915 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001916 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01001917 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03001918 f->f_lasti -= sizeof(_Py_CODEUNIT);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001919 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001920 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001921
Benjamin Petersonddd19492018-09-16 22:38:02 -07001922 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001923 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07001924
1925 if (co->co_flags & CO_ASYNC_GENERATOR) {
1926 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
1927 Py_DECREF(retval);
1928 if (w == NULL) {
1929 retval = NULL;
1930 goto error;
1931 }
1932 retval = w;
1933 }
1934
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001935 f->f_stacktop = stack_pointer;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001936 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001937 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001938
Benjamin Petersonddd19492018-09-16 22:38:02 -07001939 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001940 PyObject *type, *value, *traceback;
1941 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001942 PyTryBlock *b = PyFrame_BlockPop(f);
1943 if (b->b_type != EXCEPT_HANDLER) {
1944 PyErr_SetString(PyExc_SystemError,
1945 "popped block is not an except handler");
1946 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001947 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001948 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
1949 STACK_LEVEL() <= (b)->b_level + 4);
1950 exc_info = tstate->exc_info;
1951 type = exc_info->exc_type;
1952 value = exc_info->exc_value;
1953 traceback = exc_info->exc_traceback;
1954 exc_info->exc_type = POP();
1955 exc_info->exc_value = POP();
1956 exc_info->exc_traceback = POP();
1957 Py_XDECREF(type);
1958 Py_XDECREF(value);
1959 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001960 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001961 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001962
Benjamin Petersonddd19492018-09-16 22:38:02 -07001963 case TARGET(POP_BLOCK): {
1964 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001965 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001966 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001967 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001968
Benjamin Petersonddd19492018-09-16 22:38:02 -07001969 case TARGET(POP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001970 /* If oparg is 0 at the top of the stack are 1 or 6 values:
1971 Either:
1972 - TOP = NULL or an integer
1973 or:
1974 - (TOP, SECOND, THIRD) = exc_info()
1975 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
1976
1977 If oparg is 1 the value for 'return' was additionally pushed
1978 at the top of the stack.
1979 */
1980 PyObject *res = NULL;
1981 if (oparg) {
1982 res = POP();
1983 }
1984 PyObject *exc = POP();
1985 if (exc == NULL || PyLong_CheckExact(exc)) {
1986 Py_XDECREF(exc);
1987 }
1988 else {
1989 Py_DECREF(exc);
1990 Py_DECREF(POP());
1991 Py_DECREF(POP());
1992
1993 PyObject *type, *value, *traceback;
1994 _PyErr_StackItem *exc_info;
1995 PyTryBlock *b = PyFrame_BlockPop(f);
1996 if (b->b_type != EXCEPT_HANDLER) {
1997 PyErr_SetString(PyExc_SystemError,
1998 "popped block is not an except handler");
1999 Py_XDECREF(res);
2000 goto error;
2001 }
2002 assert(STACK_LEVEL() == (b)->b_level + 3);
2003 exc_info = tstate->exc_info;
2004 type = exc_info->exc_type;
2005 value = exc_info->exc_value;
2006 traceback = exc_info->exc_traceback;
2007 exc_info->exc_type = POP();
2008 exc_info->exc_value = POP();
2009 exc_info->exc_traceback = POP();
2010 Py_XDECREF(type);
2011 Py_XDECREF(value);
2012 Py_XDECREF(traceback);
2013 }
2014 if (oparg) {
2015 PUSH(res);
2016 }
2017 DISPATCH();
2018 }
2019
Benjamin Petersonddd19492018-09-16 22:38:02 -07002020 case TARGET(CALL_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002021 PyObject *ret = PyLong_FromLong(INSTR_OFFSET());
2022 if (ret == NULL) {
2023 goto error;
2024 }
2025 PUSH(ret);
2026 JUMPBY(oparg);
2027 FAST_DISPATCH();
2028 }
2029
Benjamin Petersonddd19492018-09-16 22:38:02 -07002030 case TARGET(BEGIN_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002031 /* Push NULL onto the stack for using it in END_FINALLY,
2032 POP_FINALLY, WITH_CLEANUP_START and WITH_CLEANUP_FINISH.
2033 */
2034 PUSH(NULL);
2035 FAST_DISPATCH();
2036 }
2037
Benjamin Petersonddd19492018-09-16 22:38:02 -07002038 case TARGET(END_FINALLY): {
2039 PREDICTED(END_FINALLY);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002040 /* At the top of the stack are 1 or 6 values:
2041 Either:
2042 - TOP = NULL or an integer
2043 or:
2044 - (TOP, SECOND, THIRD) = exc_info()
2045 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2046 */
2047 PyObject *exc = POP();
2048 if (exc == NULL) {
2049 FAST_DISPATCH();
2050 }
2051 else if (PyLong_CheckExact(exc)) {
2052 int ret = _PyLong_AsInt(exc);
2053 Py_DECREF(exc);
2054 if (ret == -1 && PyErr_Occurred()) {
2055 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002056 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002057 JUMPTO(ret);
2058 FAST_DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002059 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002060 else {
2061 assert(PyExceptionClass_Check(exc));
2062 PyObject *val = POP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002063 PyObject *tb = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002064 PyErr_Restore(exc, val, tb);
2065 goto exception_unwind;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002066 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002067 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002068
Benjamin Petersonddd19492018-09-16 22:38:02 -07002069 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002070 PyObject *exc = POP();
2071 assert(PyExceptionClass_Check(exc));
2072 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2073 PyTryBlock *b = PyFrame_BlockPop(f);
2074 assert(b->b_type == EXCEPT_HANDLER);
2075 Py_DECREF(exc);
2076 UNWIND_EXCEPT_HANDLER(b);
2077 Py_DECREF(POP());
2078 JUMPBY(oparg);
2079 FAST_DISPATCH();
2080 }
2081 else {
2082 PyObject *val = POP();
2083 PyObject *tb = POP();
2084 PyErr_Restore(exc, val, tb);
2085 goto exception_unwind;
2086 }
2087 }
2088
Benjamin Petersonddd19492018-09-16 22:38:02 -07002089 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002090 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002091
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002092 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002093 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002094 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002095 if (bc == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002096 if (!PyErr_Occurred()) {
2097 PyErr_SetString(PyExc_NameError,
2098 "__build_class__ not found");
2099 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002100 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002101 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002102 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002103 }
2104 else {
2105 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2106 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002107 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002108 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2109 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002110 if (PyErr_ExceptionMatches(PyExc_KeyError))
2111 PyErr_SetString(PyExc_NameError,
2112 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002113 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002114 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002115 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002116 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002117 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002118 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002119
Benjamin Petersonddd19492018-09-16 22:38:02 -07002120 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002121 PyObject *name = GETITEM(names, oparg);
2122 PyObject *v = POP();
2123 PyObject *ns = f->f_locals;
2124 int err;
2125 if (ns == NULL) {
2126 PyErr_Format(PyExc_SystemError,
2127 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002128 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002129 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002130 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002131 if (PyDict_CheckExact(ns))
2132 err = PyDict_SetItem(ns, name, v);
2133 else
2134 err = PyObject_SetItem(ns, name, v);
2135 Py_DECREF(v);
2136 if (err != 0)
2137 goto error;
2138 DISPATCH();
2139 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002140
Benjamin Petersonddd19492018-09-16 22:38:02 -07002141 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002142 PyObject *name = GETITEM(names, oparg);
2143 PyObject *ns = f->f_locals;
2144 int err;
2145 if (ns == NULL) {
2146 PyErr_Format(PyExc_SystemError,
2147 "no locals when deleting %R", name);
2148 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002149 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002150 err = PyObject_DelItem(ns, name);
2151 if (err != 0) {
2152 format_exc_check_arg(PyExc_NameError,
2153 NAME_ERROR_MSG,
2154 name);
2155 goto error;
2156 }
2157 DISPATCH();
2158 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002159
Benjamin Petersonddd19492018-09-16 22:38:02 -07002160 case TARGET(UNPACK_SEQUENCE): {
2161 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002162 PyObject *seq = POP(), *item, **items;
2163 if (PyTuple_CheckExact(seq) &&
2164 PyTuple_GET_SIZE(seq) == oparg) {
2165 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002166 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002167 item = items[oparg];
2168 Py_INCREF(item);
2169 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002170 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002171 } else if (PyList_CheckExact(seq) &&
2172 PyList_GET_SIZE(seq) == oparg) {
2173 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002174 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002175 item = items[oparg];
2176 Py_INCREF(item);
2177 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002178 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002179 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002180 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002181 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002182 } else {
2183 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002184 Py_DECREF(seq);
2185 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002186 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002187 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002188 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002189 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002190
Benjamin Petersonddd19492018-09-16 22:38:02 -07002191 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002192 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2193 PyObject *seq = POP();
2194
2195 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2196 stack_pointer + totalargs)) {
2197 stack_pointer += totalargs;
2198 } else {
2199 Py_DECREF(seq);
2200 goto error;
2201 }
2202 Py_DECREF(seq);
2203 DISPATCH();
2204 }
2205
Benjamin Petersonddd19492018-09-16 22:38:02 -07002206 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002207 PyObject *name = GETITEM(names, oparg);
2208 PyObject *owner = TOP();
2209 PyObject *v = SECOND();
2210 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002211 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002212 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002213 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002214 Py_DECREF(owner);
2215 if (err != 0)
2216 goto error;
2217 DISPATCH();
2218 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002219
Benjamin Petersonddd19492018-09-16 22:38:02 -07002220 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002221 PyObject *name = GETITEM(names, oparg);
2222 PyObject *owner = POP();
2223 int err;
2224 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2225 Py_DECREF(owner);
2226 if (err != 0)
2227 goto error;
2228 DISPATCH();
2229 }
2230
Benjamin Petersonddd19492018-09-16 22:38:02 -07002231 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002232 PyObject *name = GETITEM(names, oparg);
2233 PyObject *v = POP();
2234 int err;
2235 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002236 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002237 if (err != 0)
2238 goto error;
2239 DISPATCH();
2240 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002241
Benjamin Petersonddd19492018-09-16 22:38:02 -07002242 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002243 PyObject *name = GETITEM(names, oparg);
2244 int err;
2245 err = PyDict_DelItem(f->f_globals, name);
2246 if (err != 0) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002247 if (PyErr_ExceptionMatches(PyExc_KeyError)) {
2248 format_exc_check_arg(
2249 PyExc_NameError, NAME_ERROR_MSG, name);
2250 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002251 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002252 }
2253 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002254 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002255
Benjamin Petersonddd19492018-09-16 22:38:02 -07002256 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002257 PyObject *name = GETITEM(names, oparg);
2258 PyObject *locals = f->f_locals;
2259 PyObject *v;
2260 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002261 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002262 "no locals when loading %R", name);
2263 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002264 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002265 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002266 v = PyDict_GetItemWithError(locals, name);
2267 if (v != NULL) {
2268 Py_INCREF(v);
2269 }
2270 else if (PyErr_Occurred()) {
2271 goto error;
2272 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002273 }
2274 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002275 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002276 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002277 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2278 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002279 PyErr_Clear();
2280 }
2281 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002282 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002283 v = PyDict_GetItemWithError(f->f_globals, name);
2284 if (v != NULL) {
2285 Py_INCREF(v);
2286 }
2287 else if (PyErr_Occurred()) {
2288 goto error;
2289 }
2290 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002291 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002292 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002293 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002294 if (!PyErr_Occurred()) {
2295 format_exc_check_arg(
Victor Stinnerb0b22422012-04-19 00:57:45 +02002296 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002297 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002298 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002299 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002300 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002301 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002302 }
2303 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002304 v = PyObject_GetItem(f->f_builtins, name);
2305 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002306 if (PyErr_ExceptionMatches(PyExc_KeyError))
2307 format_exc_check_arg(
2308 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002309 NAME_ERROR_MSG, name);
2310 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002311 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002312 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002313 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002314 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002315 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002316 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002317 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002318
Benjamin Petersonddd19492018-09-16 22:38:02 -07002319 case TARGET(LOAD_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002320 PyObject *name = GETITEM(names, oparg);
2321 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002322 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002323 && PyDict_CheckExact(f->f_builtins))
2324 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002325 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002326 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002327 name);
2328 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002329 if (!_PyErr_OCCURRED()) {
2330 /* _PyDict_LoadGlobal() returns NULL without raising
2331 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002332 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002333 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002334 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002335 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002336 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002337 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002338 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002339 else {
2340 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002341
2342 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002343 v = PyObject_GetItem(f->f_globals, name);
2344 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002345 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2346 goto error;
2347 PyErr_Clear();
2348
Victor Stinnerb4efc962015-11-20 09:24:02 +01002349 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002350 v = PyObject_GetItem(f->f_builtins, name);
2351 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002352 if (PyErr_ExceptionMatches(PyExc_KeyError))
2353 format_exc_check_arg(
2354 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002355 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002356 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002357 }
2358 }
2359 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002360 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002361 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002362 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002363
Benjamin Petersonddd19492018-09-16 22:38:02 -07002364 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002365 PyObject *v = GETLOCAL(oparg);
2366 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002367 SETLOCAL(oparg, NULL);
2368 DISPATCH();
2369 }
2370 format_exc_check_arg(
2371 PyExc_UnboundLocalError,
2372 UNBOUNDLOCAL_ERROR_MSG,
2373 PyTuple_GetItem(co->co_varnames, oparg)
2374 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002375 goto error;
2376 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002377
Benjamin Petersonddd19492018-09-16 22:38:02 -07002378 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002379 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002380 PyObject *oldobj = PyCell_GET(cell);
2381 if (oldobj != NULL) {
2382 PyCell_SET(cell, NULL);
2383 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002384 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002385 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002386 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002387 goto error;
2388 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002389
Benjamin Petersonddd19492018-09-16 22:38:02 -07002390 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002391 PyObject *cell = freevars[oparg];
2392 Py_INCREF(cell);
2393 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002394 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002395 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002396
Benjamin Petersonddd19492018-09-16 22:38:02 -07002397 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002398 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002399 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002400 assert(locals);
2401 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2402 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2403 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2404 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2405 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002406 value = PyDict_GetItemWithError(locals, name);
2407 if (value != NULL) {
2408 Py_INCREF(value);
2409 }
2410 else if (PyErr_Occurred()) {
2411 goto error;
2412 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002413 }
2414 else {
2415 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002416 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002417 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2418 goto error;
2419 PyErr_Clear();
2420 }
2421 }
2422 if (!value) {
2423 PyObject *cell = freevars[oparg];
2424 value = PyCell_GET(cell);
2425 if (value == NULL) {
2426 format_exc_unbound(co, oparg);
2427 goto error;
2428 }
2429 Py_INCREF(value);
2430 }
2431 PUSH(value);
2432 DISPATCH();
2433 }
2434
Benjamin Petersonddd19492018-09-16 22:38:02 -07002435 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002436 PyObject *cell = freevars[oparg];
2437 PyObject *value = PyCell_GET(cell);
2438 if (value == NULL) {
2439 format_exc_unbound(co, oparg);
2440 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002441 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002442 Py_INCREF(value);
2443 PUSH(value);
2444 DISPATCH();
2445 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002446
Benjamin Petersonddd19492018-09-16 22:38:02 -07002447 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002448 PyObject *v = POP();
2449 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002450 PyObject *oldobj = PyCell_GET(cell);
2451 PyCell_SET(cell, v);
2452 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002453 DISPATCH();
2454 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002455
Benjamin Petersonddd19492018-09-16 22:38:02 -07002456 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002457 PyObject *str;
2458 PyObject *empty = PyUnicode_New(0, 0);
2459 if (empty == NULL) {
2460 goto error;
2461 }
2462 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2463 Py_DECREF(empty);
2464 if (str == NULL)
2465 goto error;
2466 while (--oparg >= 0) {
2467 PyObject *item = POP();
2468 Py_DECREF(item);
2469 }
2470 PUSH(str);
2471 DISPATCH();
2472 }
2473
Benjamin Petersonddd19492018-09-16 22:38:02 -07002474 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002475 PyObject *tup = PyTuple_New(oparg);
2476 if (tup == NULL)
2477 goto error;
2478 while (--oparg >= 0) {
2479 PyObject *item = POP();
2480 PyTuple_SET_ITEM(tup, oparg, item);
2481 }
2482 PUSH(tup);
2483 DISPATCH();
2484 }
2485
Benjamin Petersonddd19492018-09-16 22:38:02 -07002486 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002487 PyObject *list = PyList_New(oparg);
2488 if (list == NULL)
2489 goto error;
2490 while (--oparg >= 0) {
2491 PyObject *item = POP();
2492 PyList_SET_ITEM(list, oparg, item);
2493 }
2494 PUSH(list);
2495 DISPATCH();
2496 }
2497
Benjamin Petersonddd19492018-09-16 22:38:02 -07002498 case TARGET(BUILD_TUPLE_UNPACK_WITH_CALL):
2499 case TARGET(BUILD_TUPLE_UNPACK):
2500 case TARGET(BUILD_LIST_UNPACK): {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002501 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002502 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002503 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002504 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002505
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002506 if (sum == NULL)
2507 goto error;
2508
2509 for (i = oparg; i > 0; i--) {
2510 PyObject *none_val;
2511
2512 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2513 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002514 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002515 PyErr_ExceptionMatches(PyExc_TypeError))
2516 {
2517 check_args_iterable(PEEK(1 + oparg), PEEK(i));
Serhiy Storchaka73442852016-10-02 10:33:46 +03002518 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002519 Py_DECREF(sum);
2520 goto error;
2521 }
2522 Py_DECREF(none_val);
2523 }
2524
2525 if (convert_to_tuple) {
2526 return_value = PyList_AsTuple(sum);
2527 Py_DECREF(sum);
2528 if (return_value == NULL)
2529 goto error;
2530 }
2531 else {
2532 return_value = sum;
2533 }
2534
2535 while (oparg--)
2536 Py_DECREF(POP());
2537 PUSH(return_value);
2538 DISPATCH();
2539 }
2540
Benjamin Petersonddd19492018-09-16 22:38:02 -07002541 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002542 PyObject *set = PySet_New(NULL);
2543 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002544 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002545 if (set == NULL)
2546 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002547 for (i = oparg; i > 0; i--) {
2548 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002549 if (err == 0)
2550 err = PySet_Add(set, item);
2551 Py_DECREF(item);
2552 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002553 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002554 if (err != 0) {
2555 Py_DECREF(set);
2556 goto error;
2557 }
2558 PUSH(set);
2559 DISPATCH();
2560 }
2561
Benjamin Petersonddd19492018-09-16 22:38:02 -07002562 case TARGET(BUILD_SET_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002563 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002564 PyObject *sum = PySet_New(NULL);
2565 if (sum == NULL)
2566 goto error;
2567
2568 for (i = oparg; i > 0; i--) {
2569 if (_PySet_Update(sum, PEEK(i)) < 0) {
2570 Py_DECREF(sum);
2571 goto error;
2572 }
2573 }
2574
2575 while (oparg--)
2576 Py_DECREF(POP());
2577 PUSH(sum);
2578 DISPATCH();
2579 }
2580
Benjamin Petersonddd19492018-09-16 22:38:02 -07002581 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002582 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002583 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2584 if (map == NULL)
2585 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002586 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002587 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002588 PyObject *key = PEEK(2*i);
2589 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002590 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002591 if (err != 0) {
2592 Py_DECREF(map);
2593 goto error;
2594 }
2595 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002596
2597 while (oparg--) {
2598 Py_DECREF(POP());
2599 Py_DECREF(POP());
2600 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002601 PUSH(map);
2602 DISPATCH();
2603 }
2604
Benjamin Petersonddd19492018-09-16 22:38:02 -07002605 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002606 _Py_IDENTIFIER(__annotations__);
2607 int err;
2608 PyObject *ann_dict;
2609 if (f->f_locals == NULL) {
2610 PyErr_Format(PyExc_SystemError,
2611 "no locals found when setting up annotations");
2612 goto error;
2613 }
2614 /* check if __annotations__ in locals()... */
2615 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002616 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002617 &PyId___annotations__);
2618 if (ann_dict == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002619 if (PyErr_Occurred()) {
2620 goto error;
2621 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002622 /* ...if not, create a new one */
2623 ann_dict = PyDict_New();
2624 if (ann_dict == NULL) {
2625 goto error;
2626 }
2627 err = _PyDict_SetItemId(f->f_locals,
2628 &PyId___annotations__, ann_dict);
2629 Py_DECREF(ann_dict);
2630 if (err != 0) {
2631 goto error;
2632 }
2633 }
2634 }
2635 else {
2636 /* do the same if locals() is not a dict */
2637 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2638 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002639 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002640 }
2641 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2642 if (ann_dict == NULL) {
2643 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2644 goto error;
2645 }
2646 PyErr_Clear();
2647 ann_dict = PyDict_New();
2648 if (ann_dict == NULL) {
2649 goto error;
2650 }
2651 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2652 Py_DECREF(ann_dict);
2653 if (err != 0) {
2654 goto error;
2655 }
2656 }
2657 else {
2658 Py_DECREF(ann_dict);
2659 }
2660 }
2661 DISPATCH();
2662 }
2663
Benjamin Petersonddd19492018-09-16 22:38:02 -07002664 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002665 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002666 PyObject *map;
2667 PyObject *keys = TOP();
2668 if (!PyTuple_CheckExact(keys) ||
2669 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2670 PyErr_SetString(PyExc_SystemError,
2671 "bad BUILD_CONST_KEY_MAP keys argument");
2672 goto error;
2673 }
2674 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2675 if (map == NULL) {
2676 goto error;
2677 }
2678 for (i = oparg; i > 0; i--) {
2679 int err;
2680 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2681 PyObject *value = PEEK(i + 1);
2682 err = PyDict_SetItem(map, key, value);
2683 if (err != 0) {
2684 Py_DECREF(map);
2685 goto error;
2686 }
2687 }
2688
2689 Py_DECREF(POP());
2690 while (oparg--) {
2691 Py_DECREF(POP());
2692 }
2693 PUSH(map);
2694 DISPATCH();
2695 }
2696
Benjamin Petersonddd19492018-09-16 22:38:02 -07002697 case TARGET(BUILD_MAP_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002698 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002699 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002700 if (sum == NULL)
2701 goto error;
2702
2703 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002704 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002705 if (PyDict_Update(sum, arg) < 0) {
2706 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2707 PyErr_Format(PyExc_TypeError,
Berker Peksag8e9045d2016-10-02 13:08:25 +03002708 "'%.200s' object is not a mapping",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002709 arg->ob_type->tp_name);
2710 }
2711 Py_DECREF(sum);
2712 goto error;
2713 }
2714 }
2715
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002716 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002717 Py_DECREF(POP());
2718 PUSH(sum);
2719 DISPATCH();
2720 }
2721
Benjamin Petersonddd19492018-09-16 22:38:02 -07002722 case TARGET(BUILD_MAP_UNPACK_WITH_CALL): {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002723 Py_ssize_t i;
2724 PyObject *sum = PyDict_New();
2725 if (sum == NULL)
2726 goto error;
2727
2728 for (i = oparg; i > 0; i--) {
2729 PyObject *arg = PEEK(i);
2730 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002731 Py_DECREF(sum);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02002732 format_kwargs_error(PEEK(2 + oparg), arg);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002733 goto error;
2734 }
2735 }
2736
2737 while (oparg--)
2738 Py_DECREF(POP());
2739 PUSH(sum);
2740 DISPATCH();
2741 }
2742
Benjamin Petersonddd19492018-09-16 22:38:02 -07002743 case TARGET(MAP_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002744 PyObject *key = TOP();
2745 PyObject *value = SECOND();
2746 PyObject *map;
2747 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002748 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002749 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002750 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002751 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002752 Py_DECREF(value);
2753 Py_DECREF(key);
2754 if (err != 0)
2755 goto error;
2756 PREDICT(JUMP_ABSOLUTE);
2757 DISPATCH();
2758 }
2759
Benjamin Petersonddd19492018-09-16 22:38:02 -07002760 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002761 PyObject *name = GETITEM(names, oparg);
2762 PyObject *owner = TOP();
2763 PyObject *res = PyObject_GetAttr(owner, name);
2764 Py_DECREF(owner);
2765 SET_TOP(res);
2766 if (res == NULL)
2767 goto error;
2768 DISPATCH();
2769 }
2770
Benjamin Petersonddd19492018-09-16 22:38:02 -07002771 case TARGET(COMPARE_OP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002772 PyObject *right = POP();
2773 PyObject *left = TOP();
2774 PyObject *res = cmp_outcome(oparg, left, right);
2775 Py_DECREF(left);
2776 Py_DECREF(right);
2777 SET_TOP(res);
2778 if (res == NULL)
2779 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002780 PREDICT(POP_JUMP_IF_FALSE);
2781 PREDICT(POP_JUMP_IF_TRUE);
2782 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002783 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002784
Benjamin Petersonddd19492018-09-16 22:38:02 -07002785 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002786 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002787 PyObject *fromlist = POP();
2788 PyObject *level = TOP();
2789 PyObject *res;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002790 res = import_name(f, name, fromlist, level);
2791 Py_DECREF(level);
2792 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002793 SET_TOP(res);
2794 if (res == NULL)
2795 goto error;
2796 DISPATCH();
2797 }
2798
Benjamin Petersonddd19492018-09-16 22:38:02 -07002799 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002800 PyObject *from = POP(), *locals;
2801 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002802 if (PyFrame_FastToLocalsWithError(f) < 0) {
2803 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002804 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002805 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002806
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002807 locals = f->f_locals;
2808 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002809 PyErr_SetString(PyExc_SystemError,
2810 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002811 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002812 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002813 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002814 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002815 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002816 Py_DECREF(from);
2817 if (err != 0)
2818 goto error;
2819 DISPATCH();
2820 }
Guido van Rossum25831651993-05-19 14:50:45 +00002821
Benjamin Petersonddd19492018-09-16 22:38:02 -07002822 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002823 PyObject *name = GETITEM(names, oparg);
2824 PyObject *from = TOP();
2825 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002826 res = import_from(from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002827 PUSH(res);
2828 if (res == NULL)
2829 goto error;
2830 DISPATCH();
2831 }
Thomas Wouters52152252000-08-17 22:55:00 +00002832
Benjamin Petersonddd19492018-09-16 22:38:02 -07002833 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002834 JUMPBY(oparg);
2835 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002836 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002837
Benjamin Petersonddd19492018-09-16 22:38:02 -07002838 case TARGET(POP_JUMP_IF_FALSE): {
2839 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002840 PyObject *cond = POP();
2841 int err;
2842 if (cond == Py_True) {
2843 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002844 FAST_DISPATCH();
2845 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002846 if (cond == Py_False) {
2847 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002848 JUMPTO(oparg);
2849 FAST_DISPATCH();
2850 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002851 err = PyObject_IsTrue(cond);
2852 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002853 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07002854 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002855 else if (err == 0)
2856 JUMPTO(oparg);
2857 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002858 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002859 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002860 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002861
Benjamin Petersonddd19492018-09-16 22:38:02 -07002862 case TARGET(POP_JUMP_IF_TRUE): {
2863 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002864 PyObject *cond = POP();
2865 int err;
2866 if (cond == Py_False) {
2867 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002868 FAST_DISPATCH();
2869 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002870 if (cond == Py_True) {
2871 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002872 JUMPTO(oparg);
2873 FAST_DISPATCH();
2874 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002875 err = PyObject_IsTrue(cond);
2876 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002877 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002878 JUMPTO(oparg);
2879 }
2880 else if (err == 0)
2881 ;
2882 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002883 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002884 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002885 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002886
Benjamin Petersonddd19492018-09-16 22:38:02 -07002887 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002888 PyObject *cond = TOP();
2889 int err;
2890 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002891 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002892 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002893 FAST_DISPATCH();
2894 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002895 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002896 JUMPTO(oparg);
2897 FAST_DISPATCH();
2898 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002899 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002900 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002901 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002902 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002903 }
2904 else if (err == 0)
2905 JUMPTO(oparg);
2906 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002907 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002908 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002909 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002910
Benjamin Petersonddd19492018-09-16 22:38:02 -07002911 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002912 PyObject *cond = TOP();
2913 int err;
2914 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002915 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002916 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002917 FAST_DISPATCH();
2918 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002919 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002920 JUMPTO(oparg);
2921 FAST_DISPATCH();
2922 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002923 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002924 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002925 JUMPTO(oparg);
2926 }
2927 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002928 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002929 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002930 }
2931 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002932 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002933 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002934 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002935
Benjamin Petersonddd19492018-09-16 22:38:02 -07002936 case TARGET(JUMP_ABSOLUTE): {
2937 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002938 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002939#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002940 /* Enabling this path speeds-up all while and for-loops by bypassing
2941 the per-loop checks for signals. By default, this should be turned-off
2942 because it prevents detection of a control-break in tight loops like
2943 "while 1: pass". Compile with this option turned-on when you need
2944 the speed-up and do not need break checking inside tight loops (ones
2945 that contain only instructions ending with FAST_DISPATCH).
2946 */
2947 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002948#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002949 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002950#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002951 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002952
Benjamin Petersonddd19492018-09-16 22:38:02 -07002953 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002954 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002955 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002956 PyObject *iter = PyObject_GetIter(iterable);
2957 Py_DECREF(iterable);
2958 SET_TOP(iter);
2959 if (iter == NULL)
2960 goto error;
2961 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002962 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04002963 DISPATCH();
2964 }
2965
Benjamin Petersonddd19492018-09-16 22:38:02 -07002966 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04002967 /* before: [obj]; after [getiter(obj)] */
2968 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04002969 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04002970 if (PyCoro_CheckExact(iterable)) {
2971 /* `iterable` is a coroutine */
2972 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
2973 /* and it is used in a 'yield from' expression of a
2974 regular generator. */
2975 Py_DECREF(iterable);
2976 SET_TOP(NULL);
2977 PyErr_SetString(PyExc_TypeError,
2978 "cannot 'yield from' a coroutine object "
2979 "in a non-coroutine generator");
2980 goto error;
2981 }
2982 }
2983 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04002984 /* `iterable` is not a generator. */
2985 iter = PyObject_GetIter(iterable);
2986 Py_DECREF(iterable);
2987 SET_TOP(iter);
2988 if (iter == NULL)
2989 goto error;
2990 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002991 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002992 DISPATCH();
2993 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002994
Benjamin Petersonddd19492018-09-16 22:38:02 -07002995 case TARGET(FOR_ITER): {
2996 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002997 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002998 PyObject *iter = TOP();
2999 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
3000 if (next != NULL) {
3001 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003002 PREDICT(STORE_FAST);
3003 PREDICT(UNPACK_SEQUENCE);
3004 DISPATCH();
3005 }
3006 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003007 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
3008 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08003009 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003010 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003011 PyErr_Clear();
3012 }
3013 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003014 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003015 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003016 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003017 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003018 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003019 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003020
Benjamin Petersonddd19492018-09-16 22:38:02 -07003021 case TARGET(SETUP_FINALLY): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003022 /* NOTE: If you add any new block-setup opcodes that
3023 are not try/except/finally handlers, you may need
3024 to update the PyGen_NeedsFinalizing() function.
3025 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003026
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003027 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003028 STACK_LEVEL());
3029 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003030 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003031
Benjamin Petersonddd19492018-09-16 22:38:02 -07003032 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003033 _Py_IDENTIFIER(__aexit__);
3034 _Py_IDENTIFIER(__aenter__);
3035
3036 PyObject *mgr = TOP();
3037 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
3038 *enter;
3039 PyObject *res;
3040 if (exit == NULL)
3041 goto error;
3042 SET_TOP(exit);
3043 enter = special_lookup(mgr, &PyId___aenter__);
3044 Py_DECREF(mgr);
3045 if (enter == NULL)
3046 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003047 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003048 Py_DECREF(enter);
3049 if (res == NULL)
3050 goto error;
3051 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003052 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003053 DISPATCH();
3054 }
3055
Benjamin Petersonddd19492018-09-16 22:38:02 -07003056 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003057 PyObject *res = POP();
3058 /* Setup the finally block before pushing the result
3059 of __aenter__ on the stack. */
3060 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3061 STACK_LEVEL());
3062 PUSH(res);
3063 DISPATCH();
3064 }
3065
Benjamin Petersonddd19492018-09-16 22:38:02 -07003066 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003067 _Py_IDENTIFIER(__exit__);
3068 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003069 PyObject *mgr = TOP();
Raymond Hettingera3fec152016-11-21 17:24:23 -08003070 PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003071 PyObject *res;
Raymond Hettingera3fec152016-11-21 17:24:23 -08003072 if (enter == NULL)
3073 goto error;
3074 exit = special_lookup(mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003075 if (exit == NULL) {
3076 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003077 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003078 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003079 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003080 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003081 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003082 Py_DECREF(enter);
3083 if (res == NULL)
3084 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003085 /* Setup the finally block before pushing the result
3086 of __enter__ on the stack. */
3087 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3088 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003089
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003090 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003091 DISPATCH();
3092 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003093
Benjamin Petersonddd19492018-09-16 22:38:02 -07003094 case TARGET(WITH_CLEANUP_START): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003095 /* At the top of the stack are 1 or 6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003096 how/why we entered the finally clause:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003097 - TOP = NULL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003098 - (TOP, SECOND, THIRD) = exc_info()
3099 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003100 Below them is EXIT, the context.__exit__ or context.__aexit__
3101 bound method.
3102 In the first case, we must call
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003103 EXIT(None, None, None)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003104 otherwise we must call
3105 EXIT(TOP, SECOND, THIRD)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003106
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003107 In the first case, we remove EXIT from the
3108 stack, leaving TOP, and push TOP on the stack.
3109 Otherwise we shift the bottom 3 values of the
3110 stack down, replace the empty spot with NULL, and push
3111 None on the stack.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003112
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003113 Finally we push the result of the call.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003114 */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003115 PyObject *stack[3];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003116 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003117 PyObject *exc, *val, *tb, *res;
3118
3119 val = tb = Py_None;
3120 exc = TOP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003121 if (exc == NULL) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003122 STACK_SHRINK(1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003123 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003124 SET_TOP(exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003125 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003126 }
3127 else {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003128 assert(PyExceptionClass_Check(exc));
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003129 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003130 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003131 val = SECOND();
3132 tb = THIRD();
3133 tp2 = FOURTH();
3134 exc2 = PEEK(5);
3135 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003136 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003137 SET_VALUE(7, tb2);
3138 SET_VALUE(6, exc2);
3139 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003140 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3141 SET_FOURTH(NULL);
3142 /* We just shifted the stack down, so we have
3143 to tell the except handler block that the
3144 values are lower than it expects. */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003145 assert(f->f_iblock > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003146 block = &f->f_blockstack[f->f_iblock - 1];
3147 assert(block->b_type == EXCEPT_HANDLER);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003148 assert(block->b_level > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003149 block->b_level--;
3150 }
Victor Stinner842cfff2016-12-01 14:45:31 +01003151
3152 stack[0] = exc;
3153 stack[1] = val;
3154 stack[2] = tb;
3155 res = _PyObject_FastCall(exit_func, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003156 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003157 if (res == NULL)
3158 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003159
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003160 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003161 PUSH(exc);
3162 PUSH(res);
3163 PREDICT(WITH_CLEANUP_FINISH);
3164 DISPATCH();
3165 }
3166
Benjamin Petersonddd19492018-09-16 22:38:02 -07003167 case TARGET(WITH_CLEANUP_FINISH): {
3168 PREDICTED(WITH_CLEANUP_FINISH);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003169 /* TOP = the result of calling the context.__exit__ bound method
3170 SECOND = either None or exception type
3171
3172 If SECOND is None below is NULL or the return address,
3173 otherwise below are 7 values representing an exception.
3174 */
Yury Selivanov75445082015-05-11 22:57:16 -04003175 PyObject *res = POP();
3176 PyObject *exc = POP();
3177 int err;
3178
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003179 if (exc != Py_None)
3180 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003181 else
3182 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003183
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003184 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003185 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003186
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003187 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003188 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003189 else if (err > 0) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003190 /* There was an exception and a True return.
3191 * We must manually unwind the EXCEPT_HANDLER block
3192 * which was created when the exception was caught,
Quan Tian3bd0d622018-10-20 05:30:03 +08003193 * otherwise the stack will be in an inconsistent state.
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003194 */
3195 PyTryBlock *b = PyFrame_BlockPop(f);
3196 assert(b->b_type == EXCEPT_HANDLER);
3197 UNWIND_EXCEPT_HANDLER(b);
3198 PUSH(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003199 }
3200 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003201 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003202 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003203
Benjamin Petersonddd19492018-09-16 22:38:02 -07003204 case TARGET(LOAD_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003205 /* Designed to work in tamdem with CALL_METHOD. */
3206 PyObject *name = GETITEM(names, oparg);
3207 PyObject *obj = TOP();
3208 PyObject *meth = NULL;
3209
3210 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3211
Yury Selivanovf2392132016-12-13 19:03:51 -05003212 if (meth == NULL) {
3213 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003214 goto error;
3215 }
3216
3217 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003218 /* We can bypass temporary bound method object.
3219 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003220
INADA Naoki015bce62017-01-16 17:23:30 +09003221 meth | self | arg1 | ... | argN
3222 */
3223 SET_TOP(meth);
3224 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003225 }
3226 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003227 /* meth is not an unbound method (but a regular attr, or
3228 something was returned by a descriptor protocol). Set
3229 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003230 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003231
3232 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003233 */
INADA Naoki015bce62017-01-16 17:23:30 +09003234 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003235 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003236 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003237 }
3238 DISPATCH();
3239 }
3240
Benjamin Petersonddd19492018-09-16 22:38:02 -07003241 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003242 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003243 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003244
3245 sp = stack_pointer;
3246
INADA Naoki015bce62017-01-16 17:23:30 +09003247 meth = PEEK(oparg + 2);
3248 if (meth == NULL) {
3249 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3250 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003251
3252 Stack layout:
3253
INADA Naoki015bce62017-01-16 17:23:30 +09003254 ... | NULL | callable | arg1 | ... | argN
3255 ^- TOP()
3256 ^- (-oparg)
3257 ^- (-oparg-1)
3258 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003259
Ville Skyttä49b27342017-08-03 09:00:59 +03003260 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003261 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003262 */
Yury Selivanovf2392132016-12-13 19:03:51 -05003263 res = call_function(&sp, oparg, NULL);
3264 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003265 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003266 }
3267 else {
3268 /* This is a method call. Stack layout:
3269
INADA Naoki015bce62017-01-16 17:23:30 +09003270 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003271 ^- TOP()
3272 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003273 ^- (-oparg-1)
3274 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003275
INADA Naoki015bce62017-01-16 17:23:30 +09003276 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003277 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003278 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003279 */
3280 res = call_function(&sp, oparg + 1, NULL);
3281 stack_pointer = sp;
3282 }
3283
3284 PUSH(res);
3285 if (res == NULL)
3286 goto error;
3287 DISPATCH();
3288 }
3289
Benjamin Petersonddd19492018-09-16 22:38:02 -07003290 case TARGET(CALL_FUNCTION): {
3291 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003292 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003293 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003294 res = call_function(&sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003295 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003296 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003297 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003298 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003299 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003300 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003301 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003302
Benjamin Petersonddd19492018-09-16 22:38:02 -07003303 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003304 PyObject **sp, *res, *names;
3305
3306 names = POP();
3307 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003308 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003309 res = call_function(&sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003310 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003311 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003312 Py_DECREF(names);
3313
3314 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003315 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003316 }
3317 DISPATCH();
3318 }
3319
Benjamin Petersonddd19492018-09-16 22:38:02 -07003320 case TARGET(CALL_FUNCTION_EX): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003321 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003322 if (oparg & 0x01) {
3323 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003324 if (!PyDict_CheckExact(kwargs)) {
3325 PyObject *d = PyDict_New();
3326 if (d == NULL)
3327 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003328 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003329 Py_DECREF(d);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003330 format_kwargs_error(SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003331 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003332 goto error;
3333 }
3334 Py_DECREF(kwargs);
3335 kwargs = d;
3336 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003337 assert(PyDict_CheckExact(kwargs));
3338 }
3339 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003340 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003341 if (!PyTuple_CheckExact(callargs)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003342 if (check_args_iterable(func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003343 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003344 goto error;
3345 }
3346 Py_SETREF(callargs, PySequence_Tuple(callargs));
3347 if (callargs == NULL) {
3348 goto error;
3349 }
3350 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003351 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003352
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003353 result = do_call_core(func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003354 Py_DECREF(func);
3355 Py_DECREF(callargs);
3356 Py_XDECREF(kwargs);
3357
3358 SET_TOP(result);
3359 if (result == NULL) {
3360 goto error;
3361 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003362 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003363 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003364
Benjamin Petersonddd19492018-09-16 22:38:02 -07003365 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003366 PyObject *qualname = POP();
3367 PyObject *codeobj = POP();
3368 PyFunctionObject *func = (PyFunctionObject *)
3369 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003370
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003371 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003372 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003373 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003374 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003375 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003376
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003377 if (oparg & 0x08) {
3378 assert(PyTuple_CheckExact(TOP()));
3379 func ->func_closure = POP();
3380 }
3381 if (oparg & 0x04) {
3382 assert(PyDict_CheckExact(TOP()));
3383 func->func_annotations = POP();
3384 }
3385 if (oparg & 0x02) {
3386 assert(PyDict_CheckExact(TOP()));
3387 func->func_kwdefaults = POP();
3388 }
3389 if (oparg & 0x01) {
3390 assert(PyTuple_CheckExact(TOP()));
3391 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003392 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003393
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003394 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003395 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003396 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003397
Benjamin Petersonddd19492018-09-16 22:38:02 -07003398 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003399 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003400 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003401 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003402 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003403 step = NULL;
3404 stop = POP();
3405 start = TOP();
3406 slice = PySlice_New(start, stop, step);
3407 Py_DECREF(start);
3408 Py_DECREF(stop);
3409 Py_XDECREF(step);
3410 SET_TOP(slice);
3411 if (slice == NULL)
3412 goto error;
3413 DISPATCH();
3414 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003415
Benjamin Petersonddd19492018-09-16 22:38:02 -07003416 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003417 /* Handles f-string value formatting. */
3418 PyObject *result;
3419 PyObject *fmt_spec;
3420 PyObject *value;
3421 PyObject *(*conv_fn)(PyObject *);
3422 int which_conversion = oparg & FVC_MASK;
3423 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3424
3425 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003426 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003427
3428 /* See if any conversion is specified. */
3429 switch (which_conversion) {
3430 case FVC_STR: conv_fn = PyObject_Str; break;
3431 case FVC_REPR: conv_fn = PyObject_Repr; break;
3432 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3433
3434 /* Must be 0 (meaning no conversion), since only four
3435 values are allowed by (oparg & FVC_MASK). */
3436 default: conv_fn = NULL; break;
3437 }
3438
3439 /* If there's a conversion function, call it and replace
3440 value with that result. Otherwise, just use value,
3441 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003442 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003443 result = conv_fn(value);
3444 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003445 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003446 Py_XDECREF(fmt_spec);
3447 goto error;
3448 }
3449 value = result;
3450 }
3451
3452 /* If value is a unicode object, and there's no fmt_spec,
3453 then we know the result of format(value) is value
3454 itself. In that case, skip calling format(). I plan to
3455 move this optimization in to PyObject_Format()
3456 itself. */
3457 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3458 /* Do nothing, just transfer ownership to result. */
3459 result = value;
3460 } else {
3461 /* Actually call format(). */
3462 result = PyObject_Format(value, fmt_spec);
3463 Py_DECREF(value);
3464 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003465 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003466 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003467 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003468 }
3469
Eric V. Smith135d5f42016-02-05 18:23:08 -05003470 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003471 DISPATCH();
3472 }
3473
Benjamin Petersonddd19492018-09-16 22:38:02 -07003474 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003475 int oldoparg = oparg;
3476 NEXTOPARG();
3477 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003478 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003479 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003480
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003481
Antoine Pitrou042b1282010-08-13 21:15:58 +00003482#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003483 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003484#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003485 default:
3486 fprintf(stderr,
3487 "XXX lineno: %d, opcode: %d\n",
3488 PyFrame_GetLineNumber(f),
3489 opcode);
3490 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003491 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003493 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003494
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003495 /* This should never be reached. Every opcode should end with DISPATCH()
3496 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003497 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003498
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003499error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003500 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003501#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003502 if (!PyErr_Occurred())
3503 PyErr_SetString(PyExc_SystemError,
3504 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003505#else
3506 assert(PyErr_Occurred());
3507#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003508
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003509 /* Log traceback info. */
3510 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003511
Benjamin Peterson51f46162013-01-23 08:38:47 -05003512 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003513 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3514 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003515
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003516exception_unwind:
3517 /* Unwind stacks if an exception occurred */
3518 while (f->f_iblock > 0) {
3519 /* Pop the current block. */
3520 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003522 if (b->b_type == EXCEPT_HANDLER) {
3523 UNWIND_EXCEPT_HANDLER(b);
3524 continue;
3525 }
3526 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003527 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003528 PyObject *exc, *val, *tb;
3529 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003530 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003531 /* Beware, this invalidates all b->b_* fields */
3532 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003533 PUSH(exc_info->exc_traceback);
3534 PUSH(exc_info->exc_value);
3535 if (exc_info->exc_type != NULL) {
3536 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003537 }
3538 else {
3539 Py_INCREF(Py_None);
3540 PUSH(Py_None);
3541 }
3542 PyErr_Fetch(&exc, &val, &tb);
3543 /* Make the raw exception data
3544 available to the handler,
3545 so a program can emulate the
3546 Python main loop. */
3547 PyErr_NormalizeException(
3548 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003549 if (tb != NULL)
3550 PyException_SetTraceback(val, tb);
3551 else
3552 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003553 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003554 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003555 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003556 exc_info->exc_value = val;
3557 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003558 if (tb == NULL)
3559 tb = Py_None;
3560 Py_INCREF(tb);
3561 PUSH(tb);
3562 PUSH(val);
3563 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003564 JUMPTO(handler);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003565 /* Resume normal execution */
3566 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003567 }
3568 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003569
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003570 /* End the loop as we still have an error */
3571 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003572 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003574 /* Pop remaining stack entries. */
3575 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003576 PyObject *o = POP();
3577 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003578 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003579
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003580 assert(retval == NULL);
3581 assert(PyErr_Occurred());
Guido van Rossumac7be682001-01-17 15:42:30 +00003582
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003583return_or_yield:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003584 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003585 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003586 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3587 tstate, f, PyTrace_RETURN, retval)) {
3588 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003589 }
3590 }
3591 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003592 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3593 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003594 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003595 }
3596 }
3597 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003599 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003600exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003601 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3602 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003603 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003604 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003605 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003606
Victor Stinnerefde1462015-03-21 15:04:43 +01003607 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003608}
3609
Benjamin Petersonb204a422011-06-05 22:04:07 -05003610static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003611format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3612{
3613 int err;
3614 Py_ssize_t len = PyList_GET_SIZE(names);
3615 PyObject *name_str, *comma, *tail, *tmp;
3616
3617 assert(PyList_CheckExact(names));
3618 assert(len >= 1);
3619 /* Deal with the joys of natural language. */
3620 switch (len) {
3621 case 1:
3622 name_str = PyList_GET_ITEM(names, 0);
3623 Py_INCREF(name_str);
3624 break;
3625 case 2:
3626 name_str = PyUnicode_FromFormat("%U and %U",
3627 PyList_GET_ITEM(names, len - 2),
3628 PyList_GET_ITEM(names, len - 1));
3629 break;
3630 default:
3631 tail = PyUnicode_FromFormat(", %U, and %U",
3632 PyList_GET_ITEM(names, len - 2),
3633 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003634 if (tail == NULL)
3635 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003636 /* Chop off the last two objects in the list. This shouldn't actually
3637 fail, but we can't be too careful. */
3638 err = PyList_SetSlice(names, len - 2, len, NULL);
3639 if (err == -1) {
3640 Py_DECREF(tail);
3641 return;
3642 }
3643 /* Stitch everything up into a nice comma-separated list. */
3644 comma = PyUnicode_FromString(", ");
3645 if (comma == NULL) {
3646 Py_DECREF(tail);
3647 return;
3648 }
3649 tmp = PyUnicode_Join(comma, names);
3650 Py_DECREF(comma);
3651 if (tmp == NULL) {
3652 Py_DECREF(tail);
3653 return;
3654 }
3655 name_str = PyUnicode_Concat(tmp, tail);
3656 Py_DECREF(tmp);
3657 Py_DECREF(tail);
3658 break;
3659 }
3660 if (name_str == NULL)
3661 return;
3662 PyErr_Format(PyExc_TypeError,
3663 "%U() missing %i required %s argument%s: %U",
3664 co->co_name,
3665 len,
3666 kind,
3667 len == 1 ? "" : "s",
3668 name_str);
3669 Py_DECREF(name_str);
3670}
3671
3672static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003673missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003674 PyObject **fastlocals)
3675{
Victor Stinner74319ae2016-08-25 00:04:09 +02003676 Py_ssize_t i, j = 0;
3677 Py_ssize_t start, end;
3678 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003679 const char *kind = positional ? "positional" : "keyword-only";
3680 PyObject *missing_names;
3681
3682 /* Compute the names of the arguments that are missing. */
3683 missing_names = PyList_New(missing);
3684 if (missing_names == NULL)
3685 return;
3686 if (positional) {
3687 start = 0;
3688 end = co->co_argcount - defcount;
3689 }
3690 else {
3691 start = co->co_argcount;
3692 end = start + co->co_kwonlyargcount;
3693 }
3694 for (i = start; i < end; i++) {
3695 if (GETLOCAL(i) == NULL) {
3696 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3697 PyObject *name = PyObject_Repr(raw);
3698 if (name == NULL) {
3699 Py_DECREF(missing_names);
3700 return;
3701 }
3702 PyList_SET_ITEM(missing_names, j++, name);
3703 }
3704 }
3705 assert(j == missing);
3706 format_missing(kind, co, missing_names);
3707 Py_DECREF(missing_names);
3708}
3709
3710static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003711too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3712 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003713{
3714 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003715 Py_ssize_t kwonly_given = 0;
3716 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003717 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003718 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003719
Benjamin Petersone109c702011-06-24 09:37:26 -05003720 assert((co->co_flags & CO_VARARGS) == 0);
3721 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003722 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3723 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003724 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003725 }
3726 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003727 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003728 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003729 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003730 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003731 }
3732 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003733 plural = (co_argcount != 1);
3734 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003735 }
3736 if (sig == NULL)
3737 return;
3738 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003739 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3740 kwonly_sig = PyUnicode_FromFormat(format,
3741 given != 1 ? "s" : "",
3742 kwonly_given,
3743 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003744 if (kwonly_sig == NULL) {
3745 Py_DECREF(sig);
3746 return;
3747 }
3748 }
3749 else {
3750 /* This will not fail. */
3751 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003752 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003753 }
3754 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003755 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003756 co->co_name,
3757 sig,
3758 plural ? "s" : "",
3759 given,
3760 kwonly_sig,
3761 given == 1 && !kwonly_given ? "was" : "were");
3762 Py_DECREF(sig);
3763 Py_DECREF(kwonly_sig);
3764}
3765
Guido van Rossumc2e20742006-02-27 22:32:47 +00003766/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02003767 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003768 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003769
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003770PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +02003771_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003772 PyObject *const *args, Py_ssize_t argcount,
3773 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003774 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003775 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003776 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003777 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003778{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003779 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003780 PyFrameObject *f;
3781 PyObject *retval = NULL;
3782 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003783 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003784 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003785 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3786 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003787 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003788
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003789 if (globals == NULL) {
3790 PyErr_SetString(PyExc_SystemError,
3791 "PyEval_EvalCodeEx: NULL globals");
3792 return NULL;
3793 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003794
Victor Stinnerc7020012016-08-16 23:40:29 +02003795 /* Create the frame */
Victor Stinner50b48572018-11-01 01:51:40 +01003796 tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003797 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003798 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003799 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003800 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003801 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003802 fastlocals = f->f_localsplus;
3803 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003804
Victor Stinnerc7020012016-08-16 23:40:29 +02003805 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003806 if (co->co_flags & CO_VARKEYWORDS) {
3807 kwdict = PyDict_New();
3808 if (kwdict == NULL)
3809 goto fail;
3810 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003811 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003812 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003813 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003814 SETLOCAL(i, kwdict);
3815 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003816 else {
3817 kwdict = NULL;
3818 }
3819
3820 /* Copy positional arguments into local variables */
3821 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003822 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003823 }
3824 else {
3825 n = argcount;
3826 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003827 for (i = 0; i < n; i++) {
3828 x = args[i];
3829 Py_INCREF(x);
3830 SETLOCAL(i, x);
3831 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003832
3833 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003834 if (co->co_flags & CO_VARARGS) {
Sergey Fedoseev234531b2019-02-25 21:59:12 +05003835 u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003836 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003837 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003838 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003839 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003840 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003841
Serhiy Storchakab7281052016-09-12 00:52:40 +03003842 /* Handle keyword arguments passed as two strided arrays */
3843 kwcount *= kwstep;
3844 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003845 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003846 PyObject *keyword = kwnames[i];
3847 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003848 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003849
Benjamin Petersonb204a422011-06-05 22:04:07 -05003850 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3851 PyErr_Format(PyExc_TypeError,
3852 "%U() keywords must be strings",
3853 co->co_name);
3854 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003855 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003856
Benjamin Petersonb204a422011-06-05 22:04:07 -05003857 /* Speed hack: do raw pointer compares. As names are
3858 normally interned this should almost always hit. */
3859 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3860 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003861 PyObject *name = co_varnames[j];
3862 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003863 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003864 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003865 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003866
Benjamin Petersonb204a422011-06-05 22:04:07 -05003867 /* Slow fallback, just in case */
3868 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003869 PyObject *name = co_varnames[j];
3870 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
3871 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003872 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003873 }
3874 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003875 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003876 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003877 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003878
Victor Stinner231d1f32017-01-11 02:12:06 +01003879 assert(j >= total_args);
3880 if (kwdict == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003881 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003882 "%U() got an unexpected keyword argument '%S'",
3883 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003884 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003885 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003886
Christian Heimes0bd447f2013-07-20 14:48:10 +02003887 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3888 goto fail;
3889 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003890 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02003891
Benjamin Petersonb204a422011-06-05 22:04:07 -05003892 kw_found:
3893 if (GETLOCAL(j) != NULL) {
3894 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003895 "%U() got multiple values for argument '%S'",
3896 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003897 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003898 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003899 Py_INCREF(value);
3900 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003901 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003902
3903 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003904 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003905 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003906 goto fail;
3907 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003908
3909 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003910 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02003911 Py_ssize_t m = co->co_argcount - defcount;
3912 Py_ssize_t missing = 0;
3913 for (i = argcount; i < m; i++) {
3914 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003915 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02003916 }
3917 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003918 if (missing) {
3919 missing_arguments(co, missing, defcount, fastlocals);
3920 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003921 }
3922 if (n > m)
3923 i = n - m;
3924 else
3925 i = 0;
3926 for (; i < defcount; i++) {
3927 if (GETLOCAL(m+i) == NULL) {
3928 PyObject *def = defs[i];
3929 Py_INCREF(def);
3930 SETLOCAL(m+i, def);
3931 }
3932 }
3933 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003934
3935 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003936 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02003937 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003938 for (i = co->co_argcount; i < total_args; i++) {
3939 PyObject *name;
3940 if (GETLOCAL(i) != NULL)
3941 continue;
3942 name = PyTuple_GET_ITEM(co->co_varnames, i);
3943 if (kwdefs != NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003944 PyObject *def = PyDict_GetItemWithError(kwdefs, name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003945 if (def) {
3946 Py_INCREF(def);
3947 SETLOCAL(i, def);
3948 continue;
3949 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003950 else if (PyErr_Occurred()) {
3951 goto fail;
3952 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003953 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003954 missing++;
3955 }
3956 if (missing) {
3957 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003958 goto fail;
3959 }
3960 }
3961
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003962 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003963 vars into frame. */
3964 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003965 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02003966 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05003967 /* Possibly account for the cell variable being an argument. */
3968 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07003969 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05003970 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05003971 /* Clear the local copy. */
3972 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003973 }
3974 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05003975 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003976 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05003977 if (c == NULL)
3978 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05003979 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003980 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003981
3982 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05003983 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3984 PyObject *o = PyTuple_GET_ITEM(closure, i);
3985 Py_INCREF(o);
3986 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003987 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003988
Yury Selivanoveb636452016-09-08 22:01:51 -07003989 /* Handle generator/coroutine/asynchronous generator */
3990 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003991 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04003992 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003993 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04003994
3995 if (is_coro && tstate->in_coroutine_wrapper) {
3996 assert(coro_wrapper != NULL);
3997 PyErr_Format(PyExc_RuntimeError,
3998 "coroutine wrapper %.200R attempted "
3999 "to recursively wrap %.200R",
4000 coro_wrapper,
4001 co);
4002 goto fail;
4003 }
Yury Selivanov75445082015-05-11 22:57:16 -04004004
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004005 /* Don't need to keep the reference to f_back, it will be set
4006 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004007 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004009 /* Create a new generator that owns the ready to run frame
4010 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004011 if (is_coro) {
4012 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004013 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4014 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004015 } else {
4016 gen = PyGen_NewWithQualName(f, name, qualname);
4017 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004018 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004019 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004020 }
INADA Naoki9c157762016-12-26 18:52:46 +09004021
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004022 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004023
Yury Selivanov94c22632015-06-04 10:16:51 -04004024 if (is_coro && coro_wrapper != NULL) {
4025 PyObject *wrapped;
4026 tstate->in_coroutine_wrapper = 1;
4027 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
4028 tstate->in_coroutine_wrapper = 0;
4029 return wrapped;
4030 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04004031
Yury Selivanov75445082015-05-11 22:57:16 -04004032 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004033 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004034
Victor Stinner59a73272016-12-09 18:51:13 +01004035 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004036
Thomas Woutersce272b62007-09-19 21:19:28 +00004037fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004038
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004039 /* decref'ing the frame can cause __del__ methods to get invoked,
4040 which can call back into Python. While we're done with the
4041 current Python frame (f), the associated C stack is still in use,
4042 so recursion_depth must be boosted for the duration.
4043 */
4044 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09004045 if (Py_REFCNT(f) > 1) {
4046 Py_DECREF(f);
4047 _PyObject_GC_TRACK(f);
4048 }
4049 else {
4050 ++tstate->recursion_depth;
4051 Py_DECREF(f);
4052 --tstate->recursion_depth;
4053 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004054 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004055}
4056
Victor Stinner40ee3012014-06-16 15:59:28 +02004057PyObject *
4058PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004059 PyObject *const *args, int argcount,
4060 PyObject *const *kws, int kwcount,
4061 PyObject *const *defs, int defcount,
4062 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004063{
4064 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004065 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004066 kws, kws != NULL ? kws + 1 : NULL,
4067 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004068 defs, defcount,
4069 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004070 NULL, NULL);
4071}
Tim Peters5ca576e2001-06-18 22:08:13 +00004072
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004073static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05004074special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004075{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004076 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004077 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004078 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05004079 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004080 return NULL;
4081 }
4082 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004083}
4084
4085
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004086/* Logic for the raise statement (too complicated for inlining).
4087 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004088static int
Collin Winter828f04a2007-08-31 00:04:24 +00004089do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004090{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004091 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004092
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004093 if (exc == NULL) {
4094 /* Reraise */
Victor Stinner50b48572018-11-01 01:51:40 +01004095 PyThreadState *tstate = _PyThreadState_GET();
Mark Shannonae3087c2017-10-22 22:41:51 +01004096 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004097 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004098 type = exc_info->exc_type;
4099 value = exc_info->exc_value;
4100 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004101 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004102 PyErr_SetString(PyExc_RuntimeError,
4103 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004104 return 0;
4105 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004106 Py_XINCREF(type);
4107 Py_XINCREF(value);
4108 Py_XINCREF(tb);
4109 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004110 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004111 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004112
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004113 /* We support the following forms of raise:
4114 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004115 raise <instance>
4116 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004117
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004118 if (PyExceptionClass_Check(exc)) {
4119 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004120 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004121 if (value == NULL)
4122 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004123 if (!PyExceptionInstance_Check(value)) {
4124 PyErr_Format(PyExc_TypeError,
4125 "calling %R should have returned an instance of "
4126 "BaseException, not %R",
4127 type, Py_TYPE(value));
4128 goto raise_error;
4129 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004130 }
4131 else if (PyExceptionInstance_Check(exc)) {
4132 value = exc;
4133 type = PyExceptionInstance_Class(exc);
4134 Py_INCREF(type);
4135 }
4136 else {
4137 /* Not something you can raise. You get an exception
4138 anyway, just not what you specified :-) */
4139 Py_DECREF(exc);
4140 PyErr_SetString(PyExc_TypeError,
4141 "exceptions must derive from BaseException");
4142 goto raise_error;
4143 }
Collin Winter828f04a2007-08-31 00:04:24 +00004144
Serhiy Storchakac0191582016-09-27 11:37:10 +03004145 assert(type != NULL);
4146 assert(value != NULL);
4147
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004148 if (cause) {
4149 PyObject *fixed_cause;
4150 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004151 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004152 if (fixed_cause == NULL)
4153 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004154 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004155 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004156 else if (PyExceptionInstance_Check(cause)) {
4157 fixed_cause = cause;
4158 }
4159 else if (cause == Py_None) {
4160 Py_DECREF(cause);
4161 fixed_cause = NULL;
4162 }
4163 else {
4164 PyErr_SetString(PyExc_TypeError,
4165 "exception causes must derive from "
4166 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004167 goto raise_error;
4168 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004169 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004170 }
Collin Winter828f04a2007-08-31 00:04:24 +00004171
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004172 PyErr_SetObject(type, value);
4173 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004174 Py_DECREF(value);
4175 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004176 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004177
4178raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004179 Py_XDECREF(value);
4180 Py_XDECREF(type);
4181 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004182 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004183}
4184
Tim Petersd6d010b2001-06-21 02:49:55 +00004185/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004186 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004187
Guido van Rossum0368b722007-05-11 16:50:42 +00004188 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4189 with a variable target.
4190*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004191
Barry Warsawe42b18f1997-08-25 22:13:04 +00004192static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004193unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004194{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004195 int i = 0, j = 0;
4196 Py_ssize_t ll = 0;
4197 PyObject *it; /* iter(v) */
4198 PyObject *w;
4199 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004201 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004202
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004203 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004204 if (it == NULL) {
4205 if (PyErr_ExceptionMatches(PyExc_TypeError) &&
4206 v->ob_type->tp_iter == NULL && !PySequence_Check(v))
4207 {
4208 PyErr_Format(PyExc_TypeError,
4209 "cannot unpack non-iterable %.200s object",
4210 v->ob_type->tp_name);
4211 }
4212 return 0;
4213 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004215 for (; i < argcnt; i++) {
4216 w = PyIter_Next(it);
4217 if (w == NULL) {
4218 /* Iterator done, via error or exhaustion. */
4219 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004220 if (argcntafter == -1) {
4221 PyErr_Format(PyExc_ValueError,
4222 "not enough values to unpack (expected %d, got %d)",
4223 argcnt, i);
4224 }
4225 else {
4226 PyErr_Format(PyExc_ValueError,
4227 "not enough values to unpack "
4228 "(expected at least %d, got %d)",
4229 argcnt + argcntafter, i);
4230 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004231 }
4232 goto Error;
4233 }
4234 *--sp = w;
4235 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004237 if (argcntafter == -1) {
4238 /* We better have exhausted the iterator now. */
4239 w = PyIter_Next(it);
4240 if (w == NULL) {
4241 if (PyErr_Occurred())
4242 goto Error;
4243 Py_DECREF(it);
4244 return 1;
4245 }
4246 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004247 PyErr_Format(PyExc_ValueError,
4248 "too many values to unpack (expected %d)",
4249 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004250 goto Error;
4251 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004253 l = PySequence_List(it);
4254 if (l == NULL)
4255 goto Error;
4256 *--sp = l;
4257 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004259 ll = PyList_GET_SIZE(l);
4260 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004261 PyErr_Format(PyExc_ValueError,
4262 "not enough values to unpack (expected at least %d, got %zd)",
4263 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004264 goto Error;
4265 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004267 /* Pop the "after-variable" args off the list. */
4268 for (j = argcntafter; j > 0; j--, i++) {
4269 *--sp = PyList_GET_ITEM(l, ll - j);
4270 }
4271 /* Resize the list. */
4272 Py_SIZE(l) = ll - argcntafter;
4273 Py_DECREF(it);
4274 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004275
Tim Petersd6d010b2001-06-21 02:49:55 +00004276Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004277 for (; i > 0; i--, sp++)
4278 Py_DECREF(*sp);
4279 Py_XDECREF(it);
4280 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004281}
4282
4283
Guido van Rossum96a42c81992-01-12 02:29:51 +00004284#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004285static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004286prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004287{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004288 printf("%s ", str);
4289 if (PyObject_Print(v, stdout, 0) != 0)
4290 PyErr_Clear(); /* Don't know what else to do */
4291 printf("\n");
4292 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004293}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004294#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004295
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004296static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004297call_exc_trace(Py_tracefunc func, PyObject *self,
4298 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004299{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004300 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004301 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004302 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004303 if (value == NULL) {
4304 value = Py_None;
4305 Py_INCREF(value);
4306 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004307 PyErr_NormalizeException(&type, &value, &orig_traceback);
4308 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004309 arg = PyTuple_Pack(3, type, value, traceback);
4310 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004311 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004312 return;
4313 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004314 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004315 Py_DECREF(arg);
4316 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004317 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004318 else {
4319 Py_XDECREF(type);
4320 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004321 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004322 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004323}
4324
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004325static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004326call_trace_protected(Py_tracefunc func, PyObject *obj,
4327 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004328 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004329{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004330 PyObject *type, *value, *traceback;
4331 int err;
4332 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004333 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004334 if (err == 0)
4335 {
4336 PyErr_Restore(type, value, traceback);
4337 return 0;
4338 }
4339 else {
4340 Py_XDECREF(type);
4341 Py_XDECREF(value);
4342 Py_XDECREF(traceback);
4343 return -1;
4344 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004345}
4346
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004347static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004348call_trace(Py_tracefunc func, PyObject *obj,
4349 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004350 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004351{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004352 int result;
4353 if (tstate->tracing)
4354 return 0;
4355 tstate->tracing++;
4356 tstate->use_tracing = 0;
4357 result = func(obj, frame, what, arg);
4358 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4359 || (tstate->c_profilefunc != NULL));
4360 tstate->tracing--;
4361 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004362}
4363
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004364PyObject *
4365_PyEval_CallTracing(PyObject *func, PyObject *args)
4366{
Victor Stinner50b48572018-11-01 01:51:40 +01004367 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004368 int save_tracing = tstate->tracing;
4369 int save_use_tracing = tstate->use_tracing;
4370 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004372 tstate->tracing = 0;
4373 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4374 || (tstate->c_profilefunc != NULL));
4375 result = PyObject_Call(func, args, NULL);
4376 tstate->tracing = save_tracing;
4377 tstate->use_tracing = save_use_tracing;
4378 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004379}
4380
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004381/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004382static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004383maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004384 PyThreadState *tstate, PyFrameObject *frame,
4385 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004386{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004387 int result = 0;
4388 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004390 /* If the last instruction executed isn't in the current
4391 instruction window, reset the window.
4392 */
4393 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4394 PyAddrPair bounds;
4395 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4396 &bounds);
4397 *instr_lb = bounds.ap_lower;
4398 *instr_ub = bounds.ap_upper;
4399 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004400 /* If the last instruction falls at the start of a line or if it
4401 represents a jump backwards, update the frame's line number and
4402 then call the trace function if we're tracing source lines.
4403 */
4404 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004405 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004406 if (frame->f_trace_lines) {
4407 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4408 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004409 }
George King20faa682017-10-18 17:44:22 -07004410 /* Always emit an opcode event if we're tracing all opcodes. */
4411 if (frame->f_trace_opcodes) {
4412 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4413 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004414 *instr_prev = frame->f_lasti;
4415 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004416}
4417
Fred Drake5755ce62001-06-27 19:19:46 +00004418void
4419PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004420{
Victor Stinner50b48572018-11-01 01:51:40 +01004421 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004422 PyObject *temp = tstate->c_profileobj;
4423 Py_XINCREF(arg);
4424 tstate->c_profilefunc = NULL;
4425 tstate->c_profileobj = NULL;
4426 /* Must make sure that tracing is not ignored if 'temp' is freed */
4427 tstate->use_tracing = tstate->c_tracefunc != NULL;
4428 Py_XDECREF(temp);
4429 tstate->c_profilefunc = func;
4430 tstate->c_profileobj = arg;
4431 /* Flag that tracing or profiling is turned on */
4432 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004433}
4434
4435void
4436PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4437{
Victor Stinner50b48572018-11-01 01:51:40 +01004438 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004439 PyObject *temp = tstate->c_traceobj;
4440 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4441 Py_XINCREF(arg);
4442 tstate->c_tracefunc = NULL;
4443 tstate->c_traceobj = NULL;
4444 /* Must make sure that profiling is not ignored if 'temp' is freed */
4445 tstate->use_tracing = tstate->c_profilefunc != NULL;
4446 Py_XDECREF(temp);
4447 tstate->c_tracefunc = func;
4448 tstate->c_traceobj = arg;
4449 /* Flag that tracing or profiling is turned on */
4450 tstate->use_tracing = ((func != NULL)
4451 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004452}
4453
Yury Selivanov75445082015-05-11 22:57:16 -04004454void
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004455_PyEval_SetCoroutineOriginTrackingDepth(int new_depth)
4456{
4457 assert(new_depth >= 0);
Victor Stinner50b48572018-11-01 01:51:40 +01004458 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004459 tstate->coroutine_origin_tracking_depth = new_depth;
4460}
4461
4462int
4463_PyEval_GetCoroutineOriginTrackingDepth(void)
4464{
Victor Stinner50b48572018-11-01 01:51:40 +01004465 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004466 return tstate->coroutine_origin_tracking_depth;
4467}
4468
4469void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004470_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004471{
Victor Stinner50b48572018-11-01 01:51:40 +01004472 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanov75445082015-05-11 22:57:16 -04004473
Yury Selivanov75445082015-05-11 22:57:16 -04004474 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004475 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004476}
4477
4478PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004479_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004480{
Victor Stinner50b48572018-11-01 01:51:40 +01004481 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanov75445082015-05-11 22:57:16 -04004482 return tstate->coroutine_wrapper;
4483}
4484
Yury Selivanoveb636452016-09-08 22:01:51 -07004485void
4486_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4487{
Victor Stinner50b48572018-11-01 01:51:40 +01004488 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004489
4490 Py_XINCREF(firstiter);
4491 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4492}
4493
4494PyObject *
4495_PyEval_GetAsyncGenFirstiter(void)
4496{
Victor Stinner50b48572018-11-01 01:51:40 +01004497 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004498 return tstate->async_gen_firstiter;
4499}
4500
4501void
4502_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4503{
Victor Stinner50b48572018-11-01 01:51:40 +01004504 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004505
4506 Py_XINCREF(finalizer);
4507 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4508}
4509
4510PyObject *
4511_PyEval_GetAsyncGenFinalizer(void)
4512{
Victor Stinner50b48572018-11-01 01:51:40 +01004513 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004514 return tstate->async_gen_finalizer;
4515}
4516
Guido van Rossumb209a111997-04-29 18:18:01 +00004517PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004518PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004519{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004520 PyFrameObject *current_frame = PyEval_GetFrame();
4521 if (current_frame == NULL)
Victor Stinnercaba55b2018-08-03 15:33:52 +02004522 return _PyInterpreterState_GET_UNSAFE()->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004523 else
4524 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004525}
4526
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004527/* Convenience function to get a builtin from its name */
4528PyObject *
4529_PyEval_GetBuiltinId(_Py_Identifier *name)
4530{
4531 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4532 if (attr) {
4533 Py_INCREF(attr);
4534 }
4535 else if (!PyErr_Occurred()) {
4536 PyErr_SetObject(PyExc_AttributeError, _PyUnicode_FromId(name));
4537 }
4538 return attr;
4539}
4540
Guido van Rossumb209a111997-04-29 18:18:01 +00004541PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004542PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004543{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004544 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004545 if (current_frame == NULL) {
4546 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004547 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004548 }
4549
4550 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4551 return NULL;
4552
4553 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004554 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004555}
4556
Guido van Rossumb209a111997-04-29 18:18:01 +00004557PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004558PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004559{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004560 PyFrameObject *current_frame = PyEval_GetFrame();
4561 if (current_frame == NULL)
4562 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004563
4564 assert(current_frame->f_globals != NULL);
4565 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004566}
4567
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004568PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004569PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004570{
Victor Stinner50b48572018-11-01 01:51:40 +01004571 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004572 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004573}
4574
Guido van Rossum6135a871995-01-09 17:53:26 +00004575int
Tim Peters5ba58662001-07-16 02:29:45 +00004576PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004577{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004578 PyFrameObject *current_frame = PyEval_GetFrame();
4579 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004581 if (current_frame != NULL) {
4582 const int codeflags = current_frame->f_code->co_flags;
4583 const int compilerflags = codeflags & PyCF_MASK;
4584 if (compilerflags) {
4585 result = 1;
4586 cf->cf_flags |= compilerflags;
4587 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004588#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004589 if (codeflags & CO_GENERATOR_ALLOWED) {
4590 result = 1;
4591 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4592 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004593#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004594 }
4595 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004596}
4597
Guido van Rossum3f5da241990-12-20 15:06:42 +00004598
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004599const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004600PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004601{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004602 if (PyMethod_Check(func))
4603 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4604 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004605 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004606 else if (PyCFunction_Check(func))
4607 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4608 else
4609 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004610}
4611
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004612const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004613PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004614{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004615 if (PyMethod_Check(func))
4616 return "()";
4617 else if (PyFunction_Check(func))
4618 return "()";
4619 else if (PyCFunction_Check(func))
4620 return "()";
4621 else
4622 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004623}
4624
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004625#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004626if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004627 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4628 tstate, tstate->frame, \
4629 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004630 x = NULL; \
4631 } \
4632 else { \
4633 x = call; \
4634 if (tstate->c_profilefunc != NULL) { \
4635 if (x == NULL) { \
4636 call_trace_protected(tstate->c_profilefunc, \
4637 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004638 tstate, tstate->frame, \
4639 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004640 /* XXX should pass (type, value, tb) */ \
4641 } else { \
4642 if (call_trace(tstate->c_profilefunc, \
4643 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004644 tstate, tstate->frame, \
4645 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004646 Py_DECREF(x); \
4647 x = NULL; \
4648 } \
4649 } \
4650 } \
4651 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004652} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004653 x = call; \
4654 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004655
Victor Stinner415c5102017-01-11 00:54:57 +01004656/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4657 to reduce the stack consumption. */
4658Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Benjamin Peterson4fd64b92016-09-09 14:57:58 -07004659call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004660{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004661 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004662 PyObject *func = *pfunc;
4663 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004664 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4665 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004666 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004668 /* Always dispatch PyCFunction first, because these are
4669 presumed to be the most frequent callable object.
4670 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004671 if (PyCFunction_Check(func)) {
Victor Stinner50b48572018-11-01 01:51:40 +01004672 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004673 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004674 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004675 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
Victor Stinner50b48572018-11-01 01:51:40 +01004676 PyThreadState *tstate = _PyThreadState_GET();
jdemeyer56868f92018-07-21 10:30:59 +02004677 if (nargs > 0 && tstate->use_tracing) {
4678 /* We need to create a temporary bound method as argument
4679 for profiling.
4680
4681 If nargs == 0, then this cannot work because we have no
4682 "self". In any case, the call itself would raise
4683 TypeError (foo needs an argument), so we just skip
4684 profiling. */
4685 PyObject *self = stack[0];
4686 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
jdemeyer147d9552018-07-23 18:41:20 +02004687 if (func != NULL) {
4688 C_TRACE(x, _PyCFunction_FastCallKeywords(func,
4689 stack+1, nargs-1,
4690 kwnames));
4691 Py_DECREF(func);
INADA Naoki93fac8d2017-03-07 14:24:37 +09004692 }
jdemeyer147d9552018-07-23 18:41:20 +02004693 else {
4694 x = NULL;
4695 }
INADA Naoki93fac8d2017-03-07 14:24:37 +09004696 }
4697 else {
4698 x = _PyMethodDescr_FastCallKeywords(func, stack, nargs, kwnames);
4699 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004700 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004701 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004702 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
Victor Stinnerb69ee8c2016-11-28 18:32:31 +01004703 /* Optimize access to bound methods. Reuse the Python stack
4704 to pass 'self' as the first argument, replace 'func'
4705 with 'self'. It avoids the creation of a new temporary tuple
4706 for arguments (to replace func with self) when the method uses
4707 FASTCALL. */
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004708 PyObject *self = PyMethod_GET_SELF(func);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004709 Py_INCREF(self);
4710 func = PyMethod_GET_FUNCTION(func);
4711 Py_INCREF(func);
4712 Py_SETREF(*pfunc, self);
4713 nargs++;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004714 stack--;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004715 }
4716 else {
4717 Py_INCREF(func);
4718 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004719
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004720 if (PyFunction_Check(func)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004721 x = _PyFunction_FastCallKeywords(func, stack, nargs, kwnames);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004722 }
4723 else {
4724 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4725 }
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004726 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004727 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004728
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004729 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4730
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004731 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004732 while ((*pp_stack) > pfunc) {
4733 w = EXT_POP(*pp_stack);
4734 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004735 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004736
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004737 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004738}
4739
Jeremy Hylton52820442001-01-03 23:52:36 +00004740static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004741do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004742{
jdemeyere89de732018-09-19 12:06:20 +02004743 PyObject *result;
4744
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004745 if (PyCFunction_Check(func)) {
Victor Stinner50b48572018-11-01 01:51:40 +01004746 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004747 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004748 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004749 }
jdemeyere89de732018-09-19 12:06:20 +02004750 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
Victor Stinner50b48572018-11-01 01:51:40 +01004751 PyThreadState *tstate = _PyThreadState_GET();
jdemeyere89de732018-09-19 12:06:20 +02004752 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
4753 if (nargs > 0 && tstate->use_tracing) {
4754 /* We need to create a temporary bound method as argument
4755 for profiling.
4756
4757 If nargs == 0, then this cannot work because we have no
4758 "self". In any case, the call itself would raise
4759 TypeError (foo needs an argument), so we just skip
4760 profiling. */
4761 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
4762 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4763 if (func == NULL) {
4764 return NULL;
4765 }
4766
4767 C_TRACE(result, _PyCFunction_FastCallDict(func,
Victor Stinnerd17a6932018-11-09 16:56:48 +01004768 &_PyTuple_ITEMS(callargs)[1],
jdemeyere89de732018-09-19 12:06:20 +02004769 nargs - 1,
4770 kwdict));
4771 Py_DECREF(func);
4772 return result;
4773 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004774 }
jdemeyere89de732018-09-19 12:06:20 +02004775 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00004776}
4777
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004778/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004779 nb_index slot defined, and store in *pi.
4780 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004781 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004782 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004783*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004784int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004785_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004786{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004787 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004788 Py_ssize_t x;
4789 if (PyIndex_Check(v)) {
4790 x = PyNumber_AsSsize_t(v, NULL);
4791 if (x == -1 && PyErr_Occurred())
4792 return 0;
4793 }
4794 else {
4795 PyErr_SetString(PyExc_TypeError,
4796 "slice indices must be integers or "
4797 "None or have an __index__ method");
4798 return 0;
4799 }
4800 *pi = x;
4801 }
4802 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004803}
4804
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004805int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004806_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004807{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004808 Py_ssize_t x;
4809 if (PyIndex_Check(v)) {
4810 x = PyNumber_AsSsize_t(v, NULL);
4811 if (x == -1 && PyErr_Occurred())
4812 return 0;
4813 }
4814 else {
4815 PyErr_SetString(PyExc_TypeError,
4816 "slice indices must be integers or "
4817 "have an __index__ method");
4818 return 0;
4819 }
4820 *pi = x;
4821 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004822}
4823
4824
Guido van Rossum486364b2007-06-30 05:01:58 +00004825#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004826 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004827
Guido van Rossumb209a111997-04-29 18:18:01 +00004828static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004829cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004830{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004831 int res = 0;
4832 switch (op) {
4833 case PyCmp_IS:
4834 res = (v == w);
4835 break;
4836 case PyCmp_IS_NOT:
4837 res = (v != w);
4838 break;
4839 case PyCmp_IN:
4840 res = PySequence_Contains(w, v);
4841 if (res < 0)
4842 return NULL;
4843 break;
4844 case PyCmp_NOT_IN:
4845 res = PySequence_Contains(w, v);
4846 if (res < 0)
4847 return NULL;
4848 res = !res;
4849 break;
4850 case PyCmp_EXC_MATCH:
4851 if (PyTuple_Check(w)) {
4852 Py_ssize_t i, length;
4853 length = PyTuple_Size(w);
4854 for (i = 0; i < length; i += 1) {
4855 PyObject *exc = PyTuple_GET_ITEM(w, i);
4856 if (!PyExceptionClass_Check(exc)) {
4857 PyErr_SetString(PyExc_TypeError,
4858 CANNOT_CATCH_MSG);
4859 return NULL;
4860 }
4861 }
4862 }
4863 else {
4864 if (!PyExceptionClass_Check(w)) {
4865 PyErr_SetString(PyExc_TypeError,
4866 CANNOT_CATCH_MSG);
4867 return NULL;
4868 }
4869 }
4870 res = PyErr_GivenExceptionMatches(v, w);
4871 break;
4872 default:
4873 return PyObject_RichCompare(v, w, op);
4874 }
4875 v = res ? Py_True : Py_False;
4876 Py_INCREF(v);
4877 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004878}
4879
Thomas Wouters52152252000-08-17 22:55:00 +00004880static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004881import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
4882{
4883 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004884 PyObject *import_func, *res;
4885 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004886
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004887 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004888 if (import_func == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004889 if (!PyErr_Occurred()) {
4890 PyErr_SetString(PyExc_ImportError, "__import__ not found");
4891 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004892 return NULL;
4893 }
4894
4895 /* Fast path for not overloaded __import__. */
Victor Stinnercaba55b2018-08-03 15:33:52 +02004896 if (import_func == _PyInterpreterState_GET_UNSAFE()->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004897 int ilevel = _PyLong_AsInt(level);
4898 if (ilevel == -1 && PyErr_Occurred()) {
4899 return NULL;
4900 }
4901 res = PyImport_ImportModuleLevelObject(
4902 name,
4903 f->f_globals,
4904 f->f_locals == NULL ? Py_None : f->f_locals,
4905 fromlist,
4906 ilevel);
4907 return res;
4908 }
4909
4910 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004911
4912 stack[0] = name;
4913 stack[1] = f->f_globals;
4914 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
4915 stack[3] = fromlist;
4916 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02004917 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004918 Py_DECREF(import_func);
4919 return res;
4920}
4921
4922static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004923import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004924{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004925 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02004926 _Py_IDENTIFIER(__name__);
Xiang Zhang4830f582017-03-21 11:13:42 +08004927 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004928
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004929 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02004930 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004931 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004932 /* Issue #17636: in case this failed because of a circular relative
4933 import, try to fallback on reading the module directly from
4934 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02004935 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07004936 if (pkgname == NULL) {
4937 goto error;
4938 }
Oren Milman6db70332017-09-19 14:23:01 +03004939 if (!PyUnicode_Check(pkgname)) {
4940 Py_CLEAR(pkgname);
4941 goto error;
4942 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004943 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07004944 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08004945 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004946 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07004947 }
Eric Snow3f9eee62017-09-15 16:35:20 -06004948 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004949 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07004950 if (x == NULL) {
4951 goto error;
4952 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004953 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004954 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07004955 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004956 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004957 if (pkgname == NULL) {
4958 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
4959 if (pkgname_or_unknown == NULL) {
4960 Py_XDECREF(pkgpath);
4961 return NULL;
4962 }
4963 } else {
4964 pkgname_or_unknown = pkgname;
4965 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004966
4967 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
4968 PyErr_Clear();
Xiang Zhang4830f582017-03-21 11:13:42 +08004969 errmsg = PyUnicode_FromFormat(
4970 "cannot import name %R from %R (unknown location)",
4971 name, pkgname_or_unknown
4972 );
4973 /* NULL check for errmsg done by PyErr_SetImportError. */
4974 PyErr_SetImportError(errmsg, pkgname, NULL);
4975 }
4976 else {
4977 errmsg = PyUnicode_FromFormat(
4978 "cannot import name %R from %R (%S)",
4979 name, pkgname_or_unknown, pkgpath
4980 );
4981 /* NULL check for errmsg done by PyErr_SetImportError. */
4982 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004983 }
4984
Xiang Zhang4830f582017-03-21 11:13:42 +08004985 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004986 Py_XDECREF(pkgname_or_unknown);
4987 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07004988 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00004989}
Guido van Rossumac7be682001-01-17 15:42:30 +00004990
Thomas Wouters52152252000-08-17 22:55:00 +00004991static int
4992import_all_from(PyObject *locals, PyObject *v)
4993{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004994 _Py_IDENTIFIER(__all__);
4995 _Py_IDENTIFIER(__dict__);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08004996 _Py_IDENTIFIER(__name__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004997 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004998 int skip_leading_underscores = 0;
4999 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005000
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005001 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5002 return -1; /* Unexpected error */
5003 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005004 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005005 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5006 return -1;
5007 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005008 if (dict == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005009 PyErr_SetString(PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005010 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005011 return -1;
5012 }
5013 all = PyMapping_Keys(dict);
5014 Py_DECREF(dict);
5015 if (all == NULL)
5016 return -1;
5017 skip_leading_underscores = 1;
5018 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005020 for (pos = 0, err = 0; ; pos++) {
5021 name = PySequence_GetItem(all, pos);
5022 if (name == NULL) {
5023 if (!PyErr_ExceptionMatches(PyExc_IndexError))
5024 err = -1;
5025 else
5026 PyErr_Clear();
5027 break;
5028 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005029 if (!PyUnicode_Check(name)) {
5030 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5031 if (modname == NULL) {
5032 Py_DECREF(name);
5033 err = -1;
5034 break;
5035 }
5036 if (!PyUnicode_Check(modname)) {
5037 PyErr_Format(PyExc_TypeError,
5038 "module __name__ must be a string, not %.100s",
5039 Py_TYPE(modname)->tp_name);
5040 }
5041 else {
5042 PyErr_Format(PyExc_TypeError,
5043 "%s in %U.%s must be str, not %.100s",
5044 skip_leading_underscores ? "Key" : "Item",
5045 modname,
5046 skip_leading_underscores ? "__dict__" : "__all__",
5047 Py_TYPE(name)->tp_name);
5048 }
5049 Py_DECREF(modname);
5050 Py_DECREF(name);
5051 err = -1;
5052 break;
5053 }
5054 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005055 if (PyUnicode_READY(name) == -1) {
5056 Py_DECREF(name);
5057 err = -1;
5058 break;
5059 }
5060 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5061 Py_DECREF(name);
5062 continue;
5063 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005064 }
5065 value = PyObject_GetAttr(v, name);
5066 if (value == NULL)
5067 err = -1;
5068 else if (PyDict_CheckExact(locals))
5069 err = PyDict_SetItem(locals, name, value);
5070 else
5071 err = PyObject_SetItem(locals, name, value);
5072 Py_DECREF(name);
5073 Py_XDECREF(value);
5074 if (err != 0)
5075 break;
5076 }
5077 Py_DECREF(all);
5078 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005079}
5080
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005081static int
5082check_args_iterable(PyObject *func, PyObject *args)
5083{
5084 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
5085 PyErr_Format(PyExc_TypeError,
5086 "%.200s%.200s argument after * "
5087 "must be an iterable, not %.200s",
5088 PyEval_GetFuncName(func),
5089 PyEval_GetFuncDesc(func),
5090 args->ob_type->tp_name);
5091 return -1;
5092 }
5093 return 0;
5094}
5095
5096static void
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005097format_kwargs_error(PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005098{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005099 /* _PyDict_MergeEx raises attribute
5100 * error (percolated from an attempt
5101 * to get 'keys' attribute) instead of
5102 * a type error if its second argument
5103 * is not a mapping.
5104 */
5105 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
5106 PyErr_Format(PyExc_TypeError,
5107 "%.200s%.200s argument after ** "
5108 "must be a mapping, not %.200s",
5109 PyEval_GetFuncName(func),
5110 PyEval_GetFuncDesc(func),
5111 kwargs->ob_type->tp_name);
5112 }
5113 else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
5114 PyObject *exc, *val, *tb;
5115 PyErr_Fetch(&exc, &val, &tb);
5116 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
5117 PyObject *key = PyTuple_GET_ITEM(val, 0);
5118 if (!PyUnicode_Check(key)) {
5119 PyErr_Format(PyExc_TypeError,
5120 "%.200s%.200s keywords must be strings",
5121 PyEval_GetFuncName(func),
5122 PyEval_GetFuncDesc(func));
5123 } else {
5124 PyErr_Format(PyExc_TypeError,
5125 "%.200s%.200s got multiple "
5126 "values for keyword argument '%U'",
5127 PyEval_GetFuncName(func),
5128 PyEval_GetFuncDesc(func),
5129 key);
5130 }
5131 Py_XDECREF(exc);
5132 Py_XDECREF(val);
5133 Py_XDECREF(tb);
5134 }
5135 else {
5136 PyErr_Restore(exc, val, tb);
5137 }
5138 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005139}
5140
Guido van Rossumac7be682001-01-17 15:42:30 +00005141static void
Neal Norwitzda059e32007-08-26 05:33:45 +00005142format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005143{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005144 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005146 if (!obj)
5147 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005148
Serhiy Storchaka06515832016-11-20 09:13:07 +02005149 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005150 if (!obj_str)
5151 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005152
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005153 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005154}
Guido van Rossum950361c1997-01-24 13:49:28 +00005155
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005156static void
5157format_exc_unbound(PyCodeObject *co, int oparg)
5158{
5159 PyObject *name;
5160 /* Don't stomp existing exception */
5161 if (PyErr_Occurred())
5162 return;
5163 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5164 name = PyTuple_GET_ITEM(co->co_cellvars,
5165 oparg);
5166 format_exc_check_arg(
5167 PyExc_UnboundLocalError,
5168 UNBOUNDLOCAL_ERROR_MSG,
5169 name);
5170 } else {
5171 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5172 PyTuple_GET_SIZE(co->co_cellvars));
5173 format_exc_check_arg(PyExc_NameError,
5174 UNBOUNDFREE_ERROR_MSG, name);
5175 }
5176}
5177
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005178static void
5179format_awaitable_error(PyTypeObject *type, int prevopcode)
5180{
5181 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5182 if (prevopcode == BEFORE_ASYNC_WITH) {
5183 PyErr_Format(PyExc_TypeError,
5184 "'async with' received an object from __aenter__ "
5185 "that does not implement __await__: %.100s",
5186 type->tp_name);
5187 }
5188 else if (prevopcode == WITH_CLEANUP_START) {
5189 PyErr_Format(PyExc_TypeError,
5190 "'async with' received an object from __aexit__ "
5191 "that does not implement __await__: %.100s",
5192 type->tp_name);
5193 }
5194 }
5195}
5196
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005197static PyObject *
5198unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005199 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005200{
5201 PyObject *res;
5202 if (Py_REFCNT(v) == 2) {
5203 /* In the common case, there are 2 references to the value
5204 * stored in 'variable' when the += is performed: one on the
5205 * value stack (in 'v') and one still stored in the
5206 * 'variable'. We try to delete the variable now to reduce
5207 * the refcnt to 1.
5208 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005209 int opcode, oparg;
5210 NEXTOPARG();
5211 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005212 case STORE_FAST:
5213 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005214 PyObject **fastlocals = f->f_localsplus;
5215 if (GETLOCAL(oparg) == v)
5216 SETLOCAL(oparg, NULL);
5217 break;
5218 }
5219 case STORE_DEREF:
5220 {
5221 PyObject **freevars = (f->f_localsplus +
5222 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005223 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005224 if (PyCell_GET(c) == v) {
5225 PyCell_SET(c, NULL);
5226 Py_DECREF(v);
5227 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005228 break;
5229 }
5230 case STORE_NAME:
5231 {
5232 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005233 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005234 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005235 if (locals && PyDict_CheckExact(locals)) {
5236 PyObject *w = PyDict_GetItemWithError(locals, name);
5237 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
5238 (w == NULL && PyErr_Occurred()))
5239 {
5240 Py_DECREF(v);
5241 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005242 }
5243 }
5244 break;
5245 }
5246 }
5247 }
5248 res = v;
5249 PyUnicode_Append(&res, w);
5250 return res;
5251}
5252
Guido van Rossum950361c1997-01-24 13:49:28 +00005253#ifdef DYNAMIC_EXECUTION_PROFILE
5254
Skip Montanarof118cb12001-10-15 20:51:38 +00005255static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005256getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005257{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005258 int i;
5259 PyObject *l = PyList_New(256);
5260 if (l == NULL) return NULL;
5261 for (i = 0; i < 256; i++) {
5262 PyObject *x = PyLong_FromLong(a[i]);
5263 if (x == NULL) {
5264 Py_DECREF(l);
5265 return NULL;
5266 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005267 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005268 }
5269 for (i = 0; i < 256; i++)
5270 a[i] = 0;
5271 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005272}
5273
5274PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005275_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005276{
5277#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005278 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005279#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005280 int i;
5281 PyObject *l = PyList_New(257);
5282 if (l == NULL) return NULL;
5283 for (i = 0; i < 257; i++) {
5284 PyObject *x = getarray(dxpairs[i]);
5285 if (x == NULL) {
5286 Py_DECREF(l);
5287 return NULL;
5288 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005289 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005290 }
5291 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005292#endif
5293}
5294
5295#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005296
5297Py_ssize_t
5298_PyEval_RequestCodeExtraIndex(freefunc free)
5299{
Victor Stinnercaba55b2018-08-03 15:33:52 +02005300 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -07005301 Py_ssize_t new_index;
5302
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005303 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005304 return -1;
5305 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005306 new_index = interp->co_extra_user_count++;
5307 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005308 return new_index;
5309}
Łukasz Langaa785c872016-09-09 17:37:37 -07005310
5311static void
5312dtrace_function_entry(PyFrameObject *f)
5313{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005314 const char *filename;
5315 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005316 int lineno;
5317
5318 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5319 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5320 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5321
5322 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5323}
5324
5325static void
5326dtrace_function_return(PyFrameObject *f)
5327{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005328 const char *filename;
5329 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005330 int lineno;
5331
5332 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5333 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5334 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5335
5336 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5337}
5338
5339/* DTrace equivalent of maybe_call_line_trace. */
5340static void
5341maybe_dtrace_line(PyFrameObject *frame,
5342 int *instr_lb, int *instr_ub, int *instr_prev)
5343{
5344 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005345 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005346
5347 /* If the last instruction executed isn't in the current
5348 instruction window, reset the window.
5349 */
5350 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5351 PyAddrPair bounds;
5352 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5353 &bounds);
5354 *instr_lb = bounds.ap_lower;
5355 *instr_ub = bounds.ap_upper;
5356 }
5357 /* If the last instruction falls at the start of a line or if
5358 it represents a jump backwards, update the frame's line
5359 number and call the trace function. */
5360 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5361 frame->f_lineno = line;
5362 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5363 if (!co_filename)
5364 co_filename = "?";
5365 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5366 if (!co_name)
5367 co_name = "?";
5368 PyDTrace_LINE(co_filename, co_name, line);
5369 }
5370 *instr_prev = frame->f_lasti;
5371}