blob: 4e139cec20fed8680f84f3ab3df6cd87b476910f [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Thomas Wouters477c8d52006-05-27 19:21:47 +00009/* enable more aggressive intra-module optimizations, where available */
10#define PY_LOCAL_AGGRESSIVE
11
Guido van Rossumb209a111997-04-29 18:18:01 +000012#include "Python.h"
Victor Stinnerbcda8f12018-11-21 22:27:47 +010013#include "pycore_object.h"
Victor Stinner621cebe2018-11-12 16:53:38 +010014#include "pycore_pystate.h"
Victor Stinnerec13b932018-11-25 23:56:17 +010015#include "pycore_tupleobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000016
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000017#include "code.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040018#include "dictobject.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000019#include "frameobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000020#include "opcode.h"
Łukasz Langaa785c872016-09-09 17:37:37 -070021#include "pydtrace.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040022#include "setobject.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000023#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000024
Guido van Rossumc6004111993-11-05 10:22:19 +000025#include <ctype.h>
26
Guido van Rossum408027e1996-12-30 16:17:54 +000027#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000028/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000029#define LLTRACE 1 /* Low-level trace feature */
30#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000031#endif
32
Yury Selivanovf2392132016-12-13 19:03:51 -050033/* Private API for the LOAD_METHOD opcode. */
34extern int _PyObject_GetMethod(PyObject *, PyObject *, PyObject **);
35
Jeremy Hylton52820442001-01-03 23:52:36 +000036typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000037
Guido van Rossum374a9221991-04-04 10:40:29 +000038/* Forward declarations */
Eric Snow2ebc5ce2017-09-07 23:51:28 -060039Py_LOCAL_INLINE(PyObject *) call_function(PyObject ***, Py_ssize_t,
40 PyObject *);
Victor Stinnerf9b760f2016-09-09 10:17:08 -070041static PyObject * do_call_core(PyObject *, PyObject *, PyObject *);
Jeremy Hylton52820442001-01-03 23:52:36 +000042
Guido van Rossum0a066c01992-03-27 17:29:15 +000043#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000044static int lltrace;
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020045static int prtrace(PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000046#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010047static int call_trace(Py_tracefunc, PyObject *,
48 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000050static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010051 PyThreadState *, PyFrameObject *,
52 int, PyObject *);
53static void call_exc_trace(Py_tracefunc, PyObject *,
54 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000055static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Eric Snow2ebc5ce2017-09-07 23:51:28 -060056 PyThreadState *, PyFrameObject *,
57 int *, int *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070058static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *);
59static void dtrace_function_entry(PyFrameObject *);
60static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000061
Thomas Wouters477c8d52006-05-27 19:21:47 +000062static PyObject * cmp_outcome(int, PyObject *, PyObject *);
Eric Snow2ebc5ce2017-09-07 23:51:28 -060063static PyObject * import_name(PyFrameObject *, PyObject *, PyObject *,
64 PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +000065static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +000066static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +000067static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +000068static void format_exc_unbound(PyCodeObject *co, int oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +020069static PyObject * unicode_concatenate(PyObject *, PyObject *,
Serhiy Storchakaab874002016-09-11 13:48:15 +030070 PyFrameObject *, const _Py_CODEUNIT *);
Benjamin Petersonce798522012-01-22 11:24:29 -050071static PyObject * special_lookup(PyObject *, _Py_Identifier *);
Serhiy Storchaka25e4f772017-08-03 11:37:15 +030072static int check_args_iterable(PyObject *func, PyObject *vararg);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +020073static void format_kwargs_error(PyObject *func, PyObject *kwargs);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +030074static void format_awaitable_error(PyTypeObject *, int);
Guido van Rossum374a9221991-04-04 10:40:29 +000075
Paul Prescode68140d2000-08-30 20:25:01 +000076#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000077 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000078#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000080#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 "free variable '%.200s' referenced before assignment" \
82 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000083
Guido van Rossum950361c1997-01-24 13:49:28 +000084/* Dynamic execution profile */
85#ifdef DYNAMIC_EXECUTION_PROFILE
86#ifdef DXPAIRS
87static long dxpairs[257][256];
88#define dxp dxpairs[256]
89#else
90static long dxp[256];
91#endif
92#endif
93
Eric Snow2ebc5ce2017-09-07 23:51:28 -060094#define GIL_REQUEST _Py_atomic_load_relaxed(&_PyRuntime.ceval.gil_drop_request)
Benjamin Petersond2be5b42010-09-10 22:47:02 +000095
Jeffrey Yasskin39370832010-05-03 19:29:34 +000096/* This can set eval_breaker to 0 even though gil_drop_request became
97 1. We believe this is all right because the eval loop will release
98 the GIL eventually anyway. */
Eric Snowef4ac962019-02-24 15:40:47 -080099#define COMPUTE_EVAL_BREAKER(interp) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 _Py_atomic_store_relaxed( \
Eric Snowef4ac962019-02-24 15:40:47 -0800101 &interp->ceval.eval_breaker, \
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000102 GIL_REQUEST | \
Eric Snowfdf282d2019-01-11 14:26:55 -0700103 _Py_atomic_load_relaxed(&_PyRuntime.ceval.signals_pending) | \
Eric Snowef4ac962019-02-24 15:40:47 -0800104 _Py_atomic_load_relaxed(&interp->ceval.pending.calls_to_do) | \
105 interp->ceval.pending.async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000106
Eric Snowef4ac962019-02-24 15:40:47 -0800107#define SET_GIL_DROP_REQUEST(interp) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 do { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600109 _Py_atomic_store_relaxed(&_PyRuntime.ceval.gil_drop_request, 1); \
Eric Snowef4ac962019-02-24 15:40:47 -0800110 _Py_atomic_store_relaxed(&interp->ceval.eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000112
Eric Snowef4ac962019-02-24 15:40:47 -0800113#define RESET_GIL_DROP_REQUEST(interp) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 do { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600115 _Py_atomic_store_relaxed(&_PyRuntime.ceval.gil_drop_request, 0); \
Eric Snowef4ac962019-02-24 15:40:47 -0800116 COMPUTE_EVAL_BREAKER(interp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000118
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000119/* Pending calls are only modified under pending_lock */
Eric Snowef4ac962019-02-24 15:40:47 -0800120#define SIGNAL_PENDING_CALLS(interp) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 do { \
Eric Snowef4ac962019-02-24 15:40:47 -0800122 _Py_atomic_store_relaxed(&interp->ceval.pending.calls_to_do, 1); \
123 _Py_atomic_store_relaxed(&interp->ceval.eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000125
Eric Snowef4ac962019-02-24 15:40:47 -0800126#define UNSIGNAL_PENDING_CALLS(interp) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 do { \
Eric Snowef4ac962019-02-24 15:40:47 -0800128 _Py_atomic_store_relaxed(&interp->ceval.pending.calls_to_do, 0); \
129 COMPUTE_EVAL_BREAKER(interp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000130 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000131
Eric Snowfdf282d2019-01-11 14:26:55 -0700132#define SIGNAL_PENDING_SIGNALS() \
133 do { \
134 _Py_atomic_store_relaxed(&_PyRuntime.ceval.signals_pending, 1); \
Eric Snowef4ac962019-02-24 15:40:47 -0800135 _Py_atomic_store_relaxed(&_PyRuntime.interpreters.main->ceval.eval_breaker, 1); \
Eric Snowfdf282d2019-01-11 14:26:55 -0700136 } while (0)
137
138#define UNSIGNAL_PENDING_SIGNALS() \
139 do { \
140 _Py_atomic_store_relaxed(&_PyRuntime.ceval.signals_pending, 0); \
Eric Snowef4ac962019-02-24 15:40:47 -0800141 COMPUTE_EVAL_BREAKER(_PyRuntime.interpreters.main); \
Eric Snowfdf282d2019-01-11 14:26:55 -0700142 } while (0)
143
Eric Snowef4ac962019-02-24 15:40:47 -0800144#define SIGNAL_ASYNC_EXC(interp) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 do { \
Eric Snowef4ac962019-02-24 15:40:47 -0800146 interp->ceval.pending.async_exc = 1; \
147 _Py_atomic_store_relaxed(&interp->ceval.eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000149
Eric Snowef4ac962019-02-24 15:40:47 -0800150#define UNSIGNAL_ASYNC_EXC(interp) \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600151 do { \
Eric Snowef4ac962019-02-24 15:40:47 -0800152 interp->ceval.pending.async_exc = 0; \
153 COMPUTE_EVAL_BREAKER(interp); \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600154 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000155
156
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000157#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000158#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000159#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000160#include "pythread.h"
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000161#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000162
Tim Peters7f468f22004-10-11 02:40:51 +0000163int
164PyEval_ThreadsInitialized(void)
165{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000167}
168
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000169void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000170PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000171{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 if (gil_created())
173 return;
Inada Naoki001fee12019-02-20 10:00:09 +0900174 PyThread_init_thread();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 create_gil();
Victor Stinner50b48572018-11-01 01:51:40 +0100176 take_gil(_PyThreadState_GET());
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000177}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000178
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000179void
Antoine Pitrou1df15362010-09-13 14:16:46 +0000180_PyEval_FiniThreads(void)
181{
182 if (!gil_created())
183 return;
184 destroy_gil();
185 assert(!gil_created());
186}
187
188void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000189PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000190{
Victor Stinner50b48572018-11-01 01:51:40 +0100191 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 if (tstate == NULL)
193 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
194 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000195}
196
197void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000198PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000199{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100201 We therefore avoid PyThreadState_Get() which dumps a fatal error
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 in debug mode.
203 */
Victor Stinner50b48572018-11-01 01:51:40 +0100204 drop_gil(_PyThreadState_GET());
Guido van Rossum25ce5661997-08-02 03:10:38 +0000205}
206
207void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000208PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000209{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 if (tstate == NULL)
211 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
212 /* Check someone has called PyEval_InitThreads() to create the lock */
213 assert(gil_created());
214 take_gil(tstate);
215 if (PyThreadState_Swap(tstate) != NULL)
216 Py_FatalError(
217 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000218}
219
220void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000221PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000222{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 if (tstate == NULL)
224 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
225 if (PyThreadState_Swap(NULL) != tstate)
226 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
227 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000228}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000229
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200230/* This function is called from PyOS_AfterFork_Child to destroy all threads
231 * which are not running in the child process, and clear internal locks
232 * which might be held by those threads.
233 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000234
235void
236PyEval_ReInitThreads(void)
237{
Victor Stinner50b48572018-11-01 01:51:40 +0100238 PyThreadState *current_tstate = _PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 if (!gil_created())
241 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 recreate_gil();
Eric Snowef4ac962019-02-24 15:40:47 -0800243 // This will be reset in make_pending_calls() below.
244 current_tstate->interp->ceval.pending.lock = NULL;
245
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200246 take_gil(current_tstate);
Eric Snowef4ac962019-02-24 15:40:47 -0800247 _PyRuntime.main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000248
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200249 /* Destroy all threads except the current one */
250 _PyThreadState_DeleteExcept(current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000251}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000252
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000253/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600254 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000255
256void
Eric Snowef4ac962019-02-24 15:40:47 -0800257_PyEval_SignalAsyncExc(PyInterpreterState *interp)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000258{
Eric Snowef4ac962019-02-24 15:40:47 -0800259 SIGNAL_ASYNC_EXC(interp);
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000260}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000261
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000262PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000263PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000264{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 PyThreadState *tstate = PyThreadState_Swap(NULL);
266 if (tstate == NULL)
267 Py_FatalError("PyEval_SaveThread: NULL tstate");
Victor Stinner2914bb32018-01-29 11:57:45 +0100268 assert(gil_created());
269 drop_gil(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000271}
272
273void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000274PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000275{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 if (tstate == NULL)
277 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Victor Stinner2914bb32018-01-29 11:57:45 +0100278 assert(gil_created());
279
280 int err = errno;
281 take_gil(tstate);
282 /* _Py_Finalizing is protected by the GIL */
283 if (_Py_IsFinalizing() && !_Py_CURRENTLY_FINALIZING(tstate)) {
284 drop_gil(tstate);
285 PyThread_exit_thread();
286 Py_UNREACHABLE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 }
Victor Stinner2914bb32018-01-29 11:57:45 +0100288 errno = err;
289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000291}
292
293
Guido van Rossuma9672091994-09-14 13:31:22 +0000294/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
295 signal handlers or Mac I/O completion routines) can schedule calls
296 to a function to be called synchronously.
297 The synchronous function is called with one void* argument.
298 It should return 0 for success or -1 for failure -- failure should
299 be accompanied by an exception.
300
301 If registry succeeds, the registry function returns 0; if it fails
302 (e.g. due to too many pending calls) it returns -1 (without setting
303 an exception condition).
304
305 Note that because registry may occur from within signal handlers,
306 or other asynchronous events, calling malloc() is unsafe!
307
Guido van Rossuma9672091994-09-14 13:31:22 +0000308 Any thread can schedule pending calls, but only the main thread
309 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000310 There is no facility to schedule calls to a particular thread, but
311 that should be easy to change, should that ever be required. In
312 that case, the static variables here should go into the python
313 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000314*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000315
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200316void
317_PyEval_SignalReceived(void)
318{
319 /* bpo-30703: Function called when the C signal handler of Python gets a
320 signal. We cannot queue a callback using Py_AddPendingCall() since
321 that function is not async-signal-safe. */
Eric Snowfdf282d2019-01-11 14:26:55 -0700322 SIGNAL_PENDING_SIGNALS();
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200323}
324
Eric Snowef4ac962019-02-24 15:40:47 -0800325static int
326_add_pending_call(PyInterpreterState *interp, unsigned long thread_id, int (*func)(void *), void *arg)
327{
328 int i = interp->ceval.pending.last;
329 int j = (i + 1) % NPENDINGCALLS;
330 if (j == interp->ceval.pending.first) {
331 return -1; /* Queue full */
332 }
333 interp->ceval.pending.calls[i].thread_id = thread_id;
334 interp->ceval.pending.calls[i].func = func;
335 interp->ceval.pending.calls[i].arg = arg;
336 interp->ceval.pending.last = j;
337 return 0;
338}
339
340/* pop one item off the queue while holding the lock */
341static void
342_pop_pending_call(PyInterpreterState *interp, int (**func)(void *), void **arg)
343{
344 int i = interp->ceval.pending.first;
345 if (i == interp->ceval.pending.last) {
346 return; /* Queue empty */
347 }
348
349 *func = interp->ceval.pending.calls[i].func;
350 *arg = interp->ceval.pending.calls[i].arg;
351 interp->ceval.pending.first = (i + 1) % NPENDINGCALLS;
352
353 unsigned long thread_id = interp->ceval.pending.calls[i].thread_id;
354 if (thread_id && PyThread_get_thread_ident() != thread_id) {
355 // Thread mismatch, so move it to the end of the list
356 // and start over.
357 _Py_AddPendingCall(interp, thread_id, *func, *arg);
358 return;
359 }
360}
361
362int
363Py_AddPendingCall(int (*func)(void *), void *arg)
364{
365 PyInterpreterState *interp = _PyRuntime.interpreters.main;
366 return _Py_AddPendingCall(interp, _PyRuntime.main_thread, func, arg);
367}
368
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200369/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000370 scheduling to be made from any thread, and even from an executing
371 callback.
372 */
373
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000374int
Eric Snowef4ac962019-02-24 15:40:47 -0800375_Py_AddPendingCall(PyInterpreterState *interp, unsigned long thread_id, int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000376{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 /* try a few times for the lock. Since this mechanism is used
378 * for signal handling (on the main thread), there is a (slim)
379 * chance that a signal is delivered on the same thread while we
380 * hold the lock during the Py_MakePendingCalls() function.
381 * This avoids a deadlock in that case.
382 * Note that signals can be delivered on any thread. In particular,
383 * on Windows, a SIGINT is delivered on a system-created worker
384 * thread.
385 * We also check for lock being NULL, in the unlikely case that
386 * this function is called before any bytecode evaluation takes place.
387 */
Eric Snowef4ac962019-02-24 15:40:47 -0800388 PyThread_type_lock lock = interp->ceval.pending.lock;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 if (lock != NULL) {
Eric Snowef4ac962019-02-24 15:40:47 -0800390 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 for (i = 0; i<100; i++) {
392 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
393 break;
394 }
395 if (i == 100)
396 return -1;
397 }
398
Eric Snowef4ac962019-02-24 15:40:47 -0800399 int result = -1;
400 if (interp->finalizing) {
401 PyObject *exc, *val, *tb;
402 PyErr_Fetch(&exc, &val, &tb);
403 PyErr_SetString(PyExc_SystemError, "Py_AddPendingCall: cannot add pending calls (interpreter shutting down)");
404 PyErr_Print();
405 PyErr_Restore(exc, val, tb);
406 goto done;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 }
Eric Snowef4ac962019-02-24 15:40:47 -0800408
409 result = _add_pending_call(interp, thread_id, func, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 /* signal main loop */
Eric Snowef4ac962019-02-24 15:40:47 -0800411 SIGNAL_PENDING_CALLS(interp);
412
413done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 if (lock != NULL)
415 PyThread_release_lock(lock);
416 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000417}
418
Eric Snowfdf282d2019-01-11 14:26:55 -0700419static int
420handle_signals(void)
421{
422 /* Only handle signals on main thread. */
Eric Snowef4ac962019-02-24 15:40:47 -0800423 if (PyThread_get_thread_ident() != _PyRuntime.main_thread) {
Eric Snowfdf282d2019-01-11 14:26:55 -0700424 return 0;
425 }
Eric Snow64d6cc82019-02-23 15:40:43 -0700426 /*
427 * Ensure that the thread isn't currently running some other
428 * interpreter.
429 */
430 if (_PyInterpreterState_GET_UNSAFE() != _PyRuntime.interpreters.main) {
431 return 0;
432 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700433
434 UNSIGNAL_PENDING_SIGNALS();
Eric Snow64d6cc82019-02-23 15:40:43 -0700435 if (_PyErr_CheckSignals() < 0) {
Eric Snowfdf282d2019-01-11 14:26:55 -0700436 SIGNAL_PENDING_SIGNALS(); /* We're not done yet */
437 return -1;
438 }
439 return 0;
440}
441
442static int
Eric Snowef4ac962019-02-24 15:40:47 -0800443make_pending_calls(PyInterpreterState *interp)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000444{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200445 static int busy = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 /* don't perform recursive pending calls */
Eric Snowfdf282d2019-01-11 14:26:55 -0700448 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700450 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200451 busy = 1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200452 /* unsignal before starting to call callbacks, so that any callback
453 added in-between re-signals */
Eric Snowef4ac962019-02-24 15:40:47 -0800454 UNSIGNAL_PENDING_CALLS(interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700455 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200456
Eric Snowef4ac962019-02-24 15:40:47 -0800457 if (!interp->ceval.pending.lock) {
Eric Snowfdf282d2019-01-11 14:26:55 -0700458 /* initial allocation of the lock */
Eric Snowef4ac962019-02-24 15:40:47 -0800459 interp->ceval.pending.lock = PyThread_allocate_lock();
460 if (interp->ceval.pending.lock == NULL) {
Eric Snowfdf282d2019-01-11 14:26:55 -0700461 res = -1;
462 goto error;
463 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200464 }
465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 /* perform a bounded number of calls, in case of recursion */
Eric Snowfdf282d2019-01-11 14:26:55 -0700467 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snowef4ac962019-02-24 15:40:47 -0800468 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 void *arg = NULL;
470
471 /* pop one item off the queue while holding the lock */
Eric Snowef4ac962019-02-24 15:40:47 -0800472 PyThread_acquire_lock(interp->ceval.pending.lock, WAIT_LOCK);
473 _pop_pending_call(interp, &func, &arg);
474 PyThread_release_lock(interp->ceval.pending.lock);
475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 /* having released the lock, perform the callback */
Eric Snowef4ac962019-02-24 15:40:47 -0800477 if (func == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 break;
Eric Snowef4ac962019-02-24 15:40:47 -0800479 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700480 res = func(arg);
481 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200482 goto error;
483 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200485
Charles-François Natalif23339a2011-07-23 18:15:43 +0200486 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700487 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200488
489error:
490 busy = 0;
Eric Snowef4ac962019-02-24 15:40:47 -0800491 SIGNAL_PENDING_CALLS(interp); /* We're not done yet */
Eric Snowfdf282d2019-01-11 14:26:55 -0700492 return res;
493}
494
Eric Snowef4ac962019-02-24 15:40:47 -0800495int
496_Py_MakePendingCalls(PyInterpreterState *interp)
497{
498 assert(PyGILState_Check());
499
500 return make_pending_calls(interp);
501}
502
Eric Snowfdf282d2019-01-11 14:26:55 -0700503/* Py_MakePendingCalls() is a simple wrapper for the sake
504 of backward-compatibility. */
505int
506Py_MakePendingCalls(void)
507{
508 assert(PyGILState_Check());
509
510 /* Python signal handler doesn't really queue a callback: it only signals
511 that a signal was received, see _PyEval_SignalReceived(). */
512 int res = handle_signals();
513 if (res != 0) {
514 return res;
515 }
516
Eric Snowef4ac962019-02-24 15:40:47 -0800517 PyInterpreterState *interp = _PyRuntime.interpreters.main;
518 return make_pending_calls(interp);
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000519}
520
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000521/* The interpreter's recursion limit */
522
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000523#ifndef Py_DEFAULT_RECURSION_LIMIT
524#define Py_DEFAULT_RECURSION_LIMIT 1000
525#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600526
Eric Snow05351c12017-09-05 21:43:08 -0700527int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000528
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600529void
530_PyEval_Initialize(struct _ceval_runtime_state *state)
531{
532 state->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
533 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
534 _gil_initialize(&state->gil);
535}
536
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000537int
538Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000539{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600540 return _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000541}
542
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000543void
544Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000545{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600546 _PyRuntime.ceval.recursion_limit = new_limit;
547 _Py_CheckRecursionLimit = _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000548}
549
Armin Rigo2b3eb402003-10-28 12:05:48 +0000550/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
551 if the recursion_depth reaches _Py_CheckRecursionLimit.
552 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
553 to guarantee that _Py_CheckRecursiveCall() is regularly called.
554 Without USE_STACKCHECK, there is no need for this. */
555int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300556_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000557{
Victor Stinner50b48572018-11-01 01:51:40 +0100558 PyThreadState *tstate = _PyThreadState_GET();
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600559 int recursion_limit = _PyRuntime.ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000560
561#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700562 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 if (PyOS_CheckStack()) {
564 --tstate->recursion_depth;
565 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
566 return -1;
567 }
pdox18967932017-10-25 23:03:01 -0700568 /* Needed for ABI backwards-compatibility (see bpo-31857) */
Eric Snow05351c12017-09-05 21:43:08 -0700569 _Py_CheckRecursionLimit = recursion_limit;
pdox18967932017-10-25 23:03:01 -0700570#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 if (tstate->recursion_critical)
572 /* Somebody asked that we don't check for recursion. */
573 return 0;
574 if (tstate->overflowed) {
575 if (tstate->recursion_depth > recursion_limit + 50) {
576 /* Overflowing while handling an overflow. Give up. */
577 Py_FatalError("Cannot recover from stack overflow.");
578 }
579 return 0;
580 }
581 if (tstate->recursion_depth > recursion_limit) {
582 --tstate->recursion_depth;
583 tstate->overflowed = 1;
Yury Selivanovf488fb42015-07-03 01:04:23 -0400584 PyErr_Format(PyExc_RecursionError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 "maximum recursion depth exceeded%s",
586 where);
587 return -1;
588 }
589 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000590}
591
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400592static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000593static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000594
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600595#define _Py_TracingPossible _PyRuntime.ceval.tracing_possible
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000596
Guido van Rossum374a9221991-04-04 10:40:29 +0000597
Guido van Rossumb209a111997-04-29 18:18:01 +0000598PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000599PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000600{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 return PyEval_EvalCodeEx(co,
602 globals, locals,
603 (PyObject **)NULL, 0,
604 (PyObject **)NULL, 0,
605 (PyObject **)NULL, 0,
606 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000607}
608
609
610/* Interpreter main loop */
611
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000612PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000613PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 /* This is for backward compatibility with extension modules that
615 used this API; core interpreter code should call
616 PyEval_EvalFrameEx() */
617 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000618}
619
620PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000621PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000622{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200623 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
624 return interp->eval_frame(f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700625}
626
Victor Stinnerc6944e72016-11-11 02:13:35 +0100627PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700628_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
629{
Guido van Rossum950361c1997-01-24 13:49:28 +0000630#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000632#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200633 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300634 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200635 int opcode; /* Current opcode */
636 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200637 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 PyObject *retval = NULL; /* Return value */
Victor Stinner50b48572018-11-01 01:51:40 +0100639 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 is true when the line being executed has changed. The
647 initial values are such as to make this false the first
648 time it is tested. */
649 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000650
Serhiy Storchakaab874002016-09-11 13:48:15 +0300651 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 PyObject *names;
653 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000654
Brett Cannon368b4b72012-04-02 12:17:59 -0400655#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200656 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400657#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200658
Antoine Pitroub52ec782009-01-25 16:34:23 +0000659/* Computed GOTOs, or
660 the-optimization-commonly-but-improperly-known-as-"threaded code"
661 using gcc's labels-as-values extension
662 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
663
664 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000666 combined with a lookup table of jump addresses. However, since the
667 indirect jump instruction is shared by all opcodes, the CPU will have a
668 hard time making the right prediction for where to jump next (actually,
669 it will be always wrong except in the uncommon case of a sequence of
670 several identical opcodes).
671
672 "Threaded code" in contrast, uses an explicit jump table and an explicit
673 indirect jump instruction at the end of each opcode. Since the jump
674 instruction is at a different address for each opcode, the CPU will make a
675 separate prediction for each of these instructions, which is equivalent to
676 predicting the second opcode of each opcode pair. These predictions have
677 a much better chance to turn out valid, especially in small bytecode loops.
678
679 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000681 and potentially many more instructions (depending on the pipeline width).
682 A correctly predicted branch, however, is nearly free.
683
684 At the time of this writing, the "threaded code" version is up to 15-20%
685 faster than the normal "switch" version, depending on the compiler and the
686 CPU architecture.
687
688 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
689 because it would render the measurements invalid.
690
691
692 NOTE: care must be taken that the compiler doesn't try to "optimize" the
693 indirect jumps by sharing them between all opcodes. Such optimizations
694 can be disabled on gcc by using the -fno-gcse flag (or possibly
695 -fno-crossjumping).
696*/
697
Antoine Pitrou042b1282010-08-13 21:15:58 +0000698#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000699#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000700#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000701#endif
702
Antoine Pitrou042b1282010-08-13 21:15:58 +0000703#ifdef HAVE_COMPUTED_GOTOS
704 #ifndef USE_COMPUTED_GOTOS
705 #define USE_COMPUTED_GOTOS 1
706 #endif
707#else
708 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
709 #error "Computed gotos are not supported on this compiler."
710 #endif
711 #undef USE_COMPUTED_GOTOS
712 #define USE_COMPUTED_GOTOS 0
713#endif
714
715#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000716/* Import the static jump table */
717#include "opcode_targets.h"
718
Antoine Pitroub52ec782009-01-25 16:34:23 +0000719#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -0700720 op: \
721 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000722
Antoine Pitroub52ec782009-01-25 16:34:23 +0000723#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 { \
Eric Snowef4ac962019-02-24 15:40:47 -0800725 if (!_Py_atomic_load_relaxed(&tstate->interp->ceval.eval_breaker)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 FAST_DISPATCH(); \
727 } \
728 continue; \
729 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000730
731#ifdef LLTRACE
732#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700734 if (!lltrace && !_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300736 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300737 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000738 } \
739 goto fast_next_opcode; \
740 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000741#else
742#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700744 if (!_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300746 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300747 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000748 } \
749 goto fast_next_opcode; \
750 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000751#endif
752
753#else
Benjamin Petersonddd19492018-09-16 22:38:02 -0700754#define TARGET(op) op
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300755
Antoine Pitroub52ec782009-01-25 16:34:23 +0000756#define DISPATCH() continue
757#define FAST_DISPATCH() goto fast_next_opcode
758#endif
759
760
Neal Norwitza81d2202002-07-14 00:27:26 +0000761/* Tuple access macros */
762
763#ifndef Py_DEBUG
764#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
765#else
766#define GETITEM(v, i) PyTuple_GetItem((v), (i))
767#endif
768
Guido van Rossum374a9221991-04-04 10:40:29 +0000769/* Code access macros */
770
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300771/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600772#define INSTR_OFFSET() \
773 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300774#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300775 _Py_CODEUNIT word = *next_instr; \
776 opcode = _Py_OPCODE(word); \
777 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300778 next_instr++; \
779 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300780#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
781#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000782
Raymond Hettingerf606f872003-03-16 03:11:04 +0000783/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 Some opcodes tend to come in pairs thus making it possible to
785 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300786 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000787
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 Verifying the prediction costs a single high-speed test of a register
789 variable against a constant. If the pairing was good, then the
790 processor's own internal branch predication has a high likelihood of
791 success, resulting in a nearly zero-overhead transition to the
792 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300793 including its unpredictable switch-case branch. Combined with the
794 processor's internal branch prediction, a successful PREDICT has the
795 effect of making the two opcodes run as if they were a single new opcode
796 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000797
Georg Brandl86b2fb92008-07-16 03:43:04 +0000798 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 predictions turned-on and interpret the results as if some opcodes
800 had been combined or turn-off predictions so that the opcode frequency
801 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000802
803 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 the CPU to record separate branch prediction information for each
805 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000806
Raymond Hettingerf606f872003-03-16 03:11:04 +0000807*/
808
Antoine Pitrou042b1282010-08-13 21:15:58 +0000809#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000811#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300812#define PREDICT(op) \
813 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300814 _Py_CODEUNIT word = *next_instr; \
815 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300816 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300817 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300818 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300819 goto PRED_##op; \
820 } \
821 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000822#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300823#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000824
Raymond Hettingerf606f872003-03-16 03:11:04 +0000825
Guido van Rossum374a9221991-04-04 10:40:29 +0000826/* Stack manipulation macros */
827
Martin v. Löwis18e16552006-02-15 17:27:45 +0000828/* The stack can grow at most MAXINT deep, as co_nlocals and
829 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000830#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
831#define EMPTY() (STACK_LEVEL() == 0)
832#define TOP() (stack_pointer[-1])
833#define SECOND() (stack_pointer[-2])
834#define THIRD() (stack_pointer[-3])
835#define FOURTH() (stack_pointer[-4])
836#define PEEK(n) (stack_pointer[-(n)])
837#define SET_TOP(v) (stack_pointer[-1] = (v))
838#define SET_SECOND(v) (stack_pointer[-2] = (v))
839#define SET_THIRD(v) (stack_pointer[-3] = (v))
840#define SET_FOURTH(v) (stack_pointer[-4] = (v))
841#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
842#define BASIC_STACKADJ(n) (stack_pointer += n)
843#define BASIC_PUSH(v) (*stack_pointer++ = (v))
844#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000845
Guido van Rossum96a42c81992-01-12 02:29:51 +0000846#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000848 lltrace && prtrace(TOP(), "push")); \
849 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000851 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +0000852#define STACK_GROW(n) do { \
853 assert(n >= 0); \
854 (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000855 lltrace && prtrace(TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +0000856 assert(STACK_LEVEL() <= co->co_stacksize); \
857 } while (0)
858#define STACK_SHRINK(n) do { \
859 assert(n >= 0); \
860 (void)(lltrace && prtrace(TOP(), "stackadj")); \
861 (void)(BASIC_STACKADJ(-n)); \
862 assert(STACK_LEVEL() <= co->co_stacksize); \
863 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +0000864#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +0000865 prtrace((STACK_POINTER)[-1], "ext_pop")), \
866 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000867#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000868#define PUSH(v) BASIC_PUSH(v)
869#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +0000870#define STACK_GROW(n) BASIC_STACKADJ(n)
871#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000872#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000873#endif
874
Guido van Rossum681d79a1995-07-18 14:51:37 +0000875/* Local variable macros */
876
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000878
879/* The SETLOCAL() macro must not DECREF the local variable in-place and
880 then store the new value; it must copy the old value to a temporary
881 value, then store the new value, and then DECREF the temporary value.
882 This is because it is possible that during the DECREF the frame is
883 accessed by other code (e.g. a __del__ method or gc.collect()) and the
884 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000886 GETLOCAL(i) = value; \
887 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000888
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000889
890#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 while (STACK_LEVEL() > (b)->b_level) { \
892 PyObject *v = POP(); \
893 Py_XDECREF(v); \
894 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000895
896#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300897 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +0100899 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 assert(STACK_LEVEL() >= (b)->b_level + 3); \
901 while (STACK_LEVEL() > (b)->b_level + 3) { \
902 value = POP(); \
903 Py_XDECREF(value); \
904 } \
Mark Shannonae3087c2017-10-22 22:41:51 +0100905 exc_info = tstate->exc_info; \
906 type = exc_info->exc_type; \
907 value = exc_info->exc_value; \
908 traceback = exc_info->exc_traceback; \
909 exc_info->exc_type = POP(); \
910 exc_info->exc_value = POP(); \
911 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 Py_XDECREF(type); \
913 Py_XDECREF(value); \
914 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300915 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000916
Guido van Rossuma027efa1997-05-05 20:56:21 +0000917/* Start of code */
918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 /* push frame */
920 if (Py_EnterRecursiveCall(""))
921 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000924
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 if (tstate->use_tracing) {
926 if (tstate->c_tracefunc != NULL) {
927 /* tstate->c_tracefunc, if defined, is a
928 function that will be called on *every* entry
929 to a code block. Its return value, if not
930 None, is a function that will be called at
931 the start of each executed line of code.
932 (Actually, the function must return itself
933 in order to continue tracing.) The trace
934 functions are called with three arguments:
935 a pointer to the current frame, a string
936 indicating why the function is called, and
937 an argument which depends on the situation.
938 The global trace function is also called
939 whenever an exception is detected. */
940 if (call_trace_protected(tstate->c_tracefunc,
941 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100942 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 /* Trace function raised an error */
944 goto exit_eval_frame;
945 }
946 }
947 if (tstate->c_profilefunc != NULL) {
948 /* Similar for c_profilefunc, except it needn't
949 return itself and isn't called for "line" events */
950 if (call_trace_protected(tstate->c_profilefunc,
951 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100952 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 /* Profile function raised an error */
954 goto exit_eval_frame;
955 }
956 }
957 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000958
Łukasz Langaa785c872016-09-09 17:37:37 -0700959 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
960 dtrace_function_entry(f);
961
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 co = f->f_code;
963 names = co->co_names;
964 consts = co->co_consts;
965 fastlocals = f->f_localsplus;
966 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300967 assert(PyBytes_Check(co->co_code));
968 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +0300969 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
970 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
971 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300972 /*
973 f->f_lasti refers to the index of the last instruction,
974 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000975
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300976 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500977 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000978
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 When the PREDICT() macros are enabled, some opcode pairs follow in
980 direct succession without updating f->f_lasti. A successful
981 prediction effectively links the two codes together as if they
982 were a single new opcode; accordingly,f->f_lasti will point to
983 the first code in the pair (for instance, GET_ITER followed by
984 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300985 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300987 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300988 next_instr = first_instr;
989 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +0300990 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
991 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300992 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 stack_pointer = f->f_stacktop;
994 assert(stack_pointer != NULL);
995 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +0200996 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000997
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000998
Tim Peters5ca576e2001-06-18 22:08:13 +0000999#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001000 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001001#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001002
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001003 if (throwflag) /* support for generator.throw() */
1004 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001005
Victor Stinnerace47d72013-07-18 01:41:08 +02001006#ifdef Py_DEBUG
1007 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001008 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001009 caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +02001010 assert(!PyErr_Occurred());
1011#endif
1012
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001013main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1016 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +02001017 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001018
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 /* Do periodic things. Doing this every time through
1020 the loop would add too much overhead, so we do it
1021 only every Nth instruction. We also do it if
1022 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1023 event needs attention (e.g. a signal handler or
1024 async I/O handler); see Py_AddPendingCall() and
1025 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001026
Eric Snowef4ac962019-02-24 15:40:47 -08001027 if (_Py_atomic_load_relaxed(&(tstate->interp->ceval.eval_breaker))) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001028 opcode = _Py_OPCODE(*next_instr);
1029 if (opcode == SETUP_FINALLY ||
1030 opcode == SETUP_WITH ||
1031 opcode == BEFORE_ASYNC_WITH ||
1032 opcode == YIELD_FROM) {
1033 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001034 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001035 - If we're about to enter the 'with:'. It will prevent
1036 emitting a resource warning in the common idiom
1037 'with open(path) as file:'.
1038 - If we're about to enter the 'async with:'.
1039 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001040 *very* useful, but might help in some cases and it's
1041 traditional)
1042 - If we're resuming a chain of nested 'yield from' or
1043 'await' calls, then each frame is parked with YIELD_FROM
1044 as its next opcode. If the user hit control-C we want to
1045 wait until we've reached the innermost frame before
1046 running the signal handler and raising KeyboardInterrupt
1047 (see bpo-30039).
1048 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 goto fast_next_opcode;
1050 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001051
1052 if (_Py_atomic_load_relaxed(
1053 &_PyRuntime.ceval.signals_pending))
1054 {
1055 if (handle_signals() != 0) {
1056 goto error;
1057 }
1058 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001059 if (_Py_atomic_load_relaxed(
Eric Snowef4ac962019-02-24 15:40:47 -08001060 &(tstate->interp->ceval.pending.calls_to_do)))
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001061 {
Eric Snowef4ac962019-02-24 15:40:47 -08001062 if (_Py_MakePendingCalls(tstate->interp) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001063 goto error;
Eric Snowfdf282d2019-01-11 14:26:55 -07001064 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001066
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001067 if (_Py_atomic_load_relaxed(
1068 &_PyRuntime.ceval.gil_drop_request))
1069 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 /* Give another thread a chance */
1071 if (PyThreadState_Swap(NULL) != tstate)
1072 Py_FatalError("ceval: tstate mix-up");
1073 drop_gil(tstate);
1074
1075 /* Other threads may run now */
1076
1077 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001078
1079 /* Check if we should make a quick exit. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001080 if (_Py_IsFinalizing() &&
1081 !_Py_CURRENTLY_FINALIZING(tstate))
1082 {
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001083 drop_gil(tstate);
1084 PyThread_exit_thread();
1085 }
1086
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 if (PyThreadState_Swap(tstate) != NULL)
1088 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 }
1090 /* Check for asynchronous exceptions. */
1091 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001092 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 tstate->async_exc = NULL;
Eric Snowef4ac962019-02-24 15:40:47 -08001094 UNSIGNAL_ASYNC_EXC(tstate->interp);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001095 PyErr_SetNone(exc);
1096 Py_DECREF(exc);
1097 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 }
1099 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001100
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 fast_next_opcode:
1102 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001103
Łukasz Langaa785c872016-09-09 17:37:37 -07001104 if (PyDTrace_LINE_ENABLED())
1105 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1106
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001108
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001110 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001111 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001112 /* see maybe_call_line_trace
1113 for expository comments */
1114 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 err = maybe_call_line_trace(tstate->c_tracefunc,
1117 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001118 tstate, f,
1119 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 /* Reload possibly changed frame fields */
1121 JUMPTO(f->f_lasti);
1122 if (f->f_stacktop != NULL) {
1123 stack_pointer = f->f_stacktop;
1124 f->f_stacktop = NULL;
1125 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001126 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001128 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001130
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001132
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001133 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001134 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001135#ifdef DYNAMIC_EXECUTION_PROFILE
1136#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 dxpairs[lastopcode][opcode]++;
1138 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001139#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001141#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001142
Guido van Rossum96a42c81992-01-12 02:29:51 +00001143#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 if (lltrace) {
1147 if (HAS_ARG(opcode)) {
1148 printf("%d: %d, %d\n",
1149 f->f_lasti, opcode, oparg);
1150 }
1151 else {
1152 printf("%d: %d\n",
1153 f->f_lasti, opcode);
1154 }
1155 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001156#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001157
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001159
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001160 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001161 It is essential that any operation that fails must goto error
1162 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001163
Benjamin Petersonddd19492018-09-16 22:38:02 -07001164 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001165 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001166 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001167
Benjamin Petersonddd19492018-09-16 22:38:02 -07001168 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001169 PyObject *value = GETLOCAL(oparg);
1170 if (value == NULL) {
1171 format_exc_check_arg(PyExc_UnboundLocalError,
1172 UNBOUNDLOCAL_ERROR_MSG,
1173 PyTuple_GetItem(co->co_varnames, oparg));
1174 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001176 Py_INCREF(value);
1177 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001178 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001179 }
1180
Benjamin Petersonddd19492018-09-16 22:38:02 -07001181 case TARGET(LOAD_CONST): {
1182 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001183 PyObject *value = GETITEM(consts, oparg);
1184 Py_INCREF(value);
1185 PUSH(value);
1186 FAST_DISPATCH();
1187 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001188
Benjamin Petersonddd19492018-09-16 22:38:02 -07001189 case TARGET(STORE_FAST): {
1190 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001191 PyObject *value = POP();
1192 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001194 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001195
Benjamin Petersonddd19492018-09-16 22:38:02 -07001196 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001197 PyObject *value = POP();
1198 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001200 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001201
Benjamin Petersonddd19492018-09-16 22:38:02 -07001202 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001203 PyObject *top = TOP();
1204 PyObject *second = SECOND();
1205 SET_TOP(second);
1206 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001208 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001209
Benjamin Petersonddd19492018-09-16 22:38:02 -07001210 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001211 PyObject *top = TOP();
1212 PyObject *second = SECOND();
1213 PyObject *third = THIRD();
1214 SET_TOP(second);
1215 SET_SECOND(third);
1216 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001218 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001219
Benjamin Petersonddd19492018-09-16 22:38:02 -07001220 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001221 PyObject *top = TOP();
1222 PyObject *second = SECOND();
1223 PyObject *third = THIRD();
1224 PyObject *fourth = FOURTH();
1225 SET_TOP(second);
1226 SET_SECOND(third);
1227 SET_THIRD(fourth);
1228 SET_FOURTH(top);
1229 FAST_DISPATCH();
1230 }
1231
Benjamin Petersonddd19492018-09-16 22:38:02 -07001232 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001233 PyObject *top = TOP();
1234 Py_INCREF(top);
1235 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001237 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001238
Benjamin Petersonddd19492018-09-16 22:38:02 -07001239 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001240 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001241 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001242 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001243 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001244 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001245 SET_TOP(top);
1246 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001247 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001248 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001249
Benjamin Petersonddd19492018-09-16 22:38:02 -07001250 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001251 PyObject *value = TOP();
1252 PyObject *res = PyNumber_Positive(value);
1253 Py_DECREF(value);
1254 SET_TOP(res);
1255 if (res == NULL)
1256 goto error;
1257 DISPATCH();
1258 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001259
Benjamin Petersonddd19492018-09-16 22:38:02 -07001260 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001261 PyObject *value = TOP();
1262 PyObject *res = PyNumber_Negative(value);
1263 Py_DECREF(value);
1264 SET_TOP(res);
1265 if (res == NULL)
1266 goto error;
1267 DISPATCH();
1268 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001269
Benjamin Petersonddd19492018-09-16 22:38:02 -07001270 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001271 PyObject *value = TOP();
1272 int err = PyObject_IsTrue(value);
1273 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 if (err == 0) {
1275 Py_INCREF(Py_True);
1276 SET_TOP(Py_True);
1277 DISPATCH();
1278 }
1279 else if (err > 0) {
1280 Py_INCREF(Py_False);
1281 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 DISPATCH();
1283 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001284 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001285 goto error;
1286 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001287
Benjamin Petersonddd19492018-09-16 22:38:02 -07001288 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001289 PyObject *value = TOP();
1290 PyObject *res = PyNumber_Invert(value);
1291 Py_DECREF(value);
1292 SET_TOP(res);
1293 if (res == NULL)
1294 goto error;
1295 DISPATCH();
1296 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001297
Benjamin Petersonddd19492018-09-16 22:38:02 -07001298 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001299 PyObject *exp = POP();
1300 PyObject *base = TOP();
1301 PyObject *res = PyNumber_Power(base, exp, Py_None);
1302 Py_DECREF(base);
1303 Py_DECREF(exp);
1304 SET_TOP(res);
1305 if (res == NULL)
1306 goto error;
1307 DISPATCH();
1308 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001309
Benjamin Petersonddd19492018-09-16 22:38:02 -07001310 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001311 PyObject *right = POP();
1312 PyObject *left = TOP();
1313 PyObject *res = PyNumber_Multiply(left, right);
1314 Py_DECREF(left);
1315 Py_DECREF(right);
1316 SET_TOP(res);
1317 if (res == NULL)
1318 goto error;
1319 DISPATCH();
1320 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001321
Benjamin Petersonddd19492018-09-16 22:38:02 -07001322 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001323 PyObject *right = POP();
1324 PyObject *left = TOP();
1325 PyObject *res = PyNumber_MatrixMultiply(left, right);
1326 Py_DECREF(left);
1327 Py_DECREF(right);
1328 SET_TOP(res);
1329 if (res == NULL)
1330 goto error;
1331 DISPATCH();
1332 }
1333
Benjamin Petersonddd19492018-09-16 22:38:02 -07001334 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001335 PyObject *divisor = POP();
1336 PyObject *dividend = TOP();
1337 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1338 Py_DECREF(dividend);
1339 Py_DECREF(divisor);
1340 SET_TOP(quotient);
1341 if (quotient == NULL)
1342 goto error;
1343 DISPATCH();
1344 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001345
Benjamin Petersonddd19492018-09-16 22:38:02 -07001346 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001347 PyObject *divisor = POP();
1348 PyObject *dividend = TOP();
1349 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1350 Py_DECREF(dividend);
1351 Py_DECREF(divisor);
1352 SET_TOP(quotient);
1353 if (quotient == NULL)
1354 goto error;
1355 DISPATCH();
1356 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001357
Benjamin Petersonddd19492018-09-16 22:38:02 -07001358 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001359 PyObject *divisor = POP();
1360 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001361 PyObject *res;
1362 if (PyUnicode_CheckExact(dividend) && (
1363 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1364 // fast path; string formatting, but not if the RHS is a str subclass
1365 // (see issue28598)
1366 res = PyUnicode_Format(dividend, divisor);
1367 } else {
1368 res = PyNumber_Remainder(dividend, divisor);
1369 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001370 Py_DECREF(divisor);
1371 Py_DECREF(dividend);
1372 SET_TOP(res);
1373 if (res == NULL)
1374 goto error;
1375 DISPATCH();
1376 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001377
Benjamin Petersonddd19492018-09-16 22:38:02 -07001378 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001379 PyObject *right = POP();
1380 PyObject *left = TOP();
1381 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001382 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1383 CPython using bytecode, it is simply worthless.
1384 See http://bugs.python.org/issue21955 and
1385 http://bugs.python.org/issue10044 for the discussion. In short,
1386 no patch shown any impact on a realistic benchmark, only a minor
1387 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001388 if (PyUnicode_CheckExact(left) &&
1389 PyUnicode_CheckExact(right)) {
1390 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001391 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001392 }
1393 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001394 sum = PyNumber_Add(left, right);
1395 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001396 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001397 Py_DECREF(right);
1398 SET_TOP(sum);
1399 if (sum == NULL)
1400 goto error;
1401 DISPATCH();
1402 }
1403
Benjamin Petersonddd19492018-09-16 22:38:02 -07001404 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001405 PyObject *right = POP();
1406 PyObject *left = TOP();
1407 PyObject *diff = PyNumber_Subtract(left, right);
1408 Py_DECREF(right);
1409 Py_DECREF(left);
1410 SET_TOP(diff);
1411 if (diff == NULL)
1412 goto error;
1413 DISPATCH();
1414 }
1415
Benjamin Petersonddd19492018-09-16 22:38:02 -07001416 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001417 PyObject *sub = POP();
1418 PyObject *container = TOP();
1419 PyObject *res = PyObject_GetItem(container, sub);
1420 Py_DECREF(container);
1421 Py_DECREF(sub);
1422 SET_TOP(res);
1423 if (res == NULL)
1424 goto error;
1425 DISPATCH();
1426 }
1427
Benjamin Petersonddd19492018-09-16 22:38:02 -07001428 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001429 PyObject *right = POP();
1430 PyObject *left = TOP();
1431 PyObject *res = PyNumber_Lshift(left, right);
1432 Py_DECREF(left);
1433 Py_DECREF(right);
1434 SET_TOP(res);
1435 if (res == NULL)
1436 goto error;
1437 DISPATCH();
1438 }
1439
Benjamin Petersonddd19492018-09-16 22:38:02 -07001440 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001441 PyObject *right = POP();
1442 PyObject *left = TOP();
1443 PyObject *res = PyNumber_Rshift(left, right);
1444 Py_DECREF(left);
1445 Py_DECREF(right);
1446 SET_TOP(res);
1447 if (res == NULL)
1448 goto error;
1449 DISPATCH();
1450 }
1451
Benjamin Petersonddd19492018-09-16 22:38:02 -07001452 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001453 PyObject *right = POP();
1454 PyObject *left = TOP();
1455 PyObject *res = PyNumber_And(left, right);
1456 Py_DECREF(left);
1457 Py_DECREF(right);
1458 SET_TOP(res);
1459 if (res == NULL)
1460 goto error;
1461 DISPATCH();
1462 }
1463
Benjamin Petersonddd19492018-09-16 22:38:02 -07001464 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001465 PyObject *right = POP();
1466 PyObject *left = TOP();
1467 PyObject *res = PyNumber_Xor(left, right);
1468 Py_DECREF(left);
1469 Py_DECREF(right);
1470 SET_TOP(res);
1471 if (res == NULL)
1472 goto error;
1473 DISPATCH();
1474 }
1475
Benjamin Petersonddd19492018-09-16 22:38:02 -07001476 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001477 PyObject *right = POP();
1478 PyObject *left = TOP();
1479 PyObject *res = PyNumber_Or(left, right);
1480 Py_DECREF(left);
1481 Py_DECREF(right);
1482 SET_TOP(res);
1483 if (res == NULL)
1484 goto error;
1485 DISPATCH();
1486 }
1487
Benjamin Petersonddd19492018-09-16 22:38:02 -07001488 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001489 PyObject *v = POP();
1490 PyObject *list = PEEK(oparg);
1491 int err;
1492 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001493 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001494 if (err != 0)
1495 goto error;
1496 PREDICT(JUMP_ABSOLUTE);
1497 DISPATCH();
1498 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001499
Benjamin Petersonddd19492018-09-16 22:38:02 -07001500 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001501 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001502 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001503 int err;
1504 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001505 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001506 if (err != 0)
1507 goto error;
1508 PREDICT(JUMP_ABSOLUTE);
1509 DISPATCH();
1510 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001511
Benjamin Petersonddd19492018-09-16 22:38:02 -07001512 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001513 PyObject *exp = POP();
1514 PyObject *base = TOP();
1515 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1516 Py_DECREF(base);
1517 Py_DECREF(exp);
1518 SET_TOP(res);
1519 if (res == NULL)
1520 goto error;
1521 DISPATCH();
1522 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001523
Benjamin Petersonddd19492018-09-16 22:38:02 -07001524 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001525 PyObject *right = POP();
1526 PyObject *left = TOP();
1527 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1528 Py_DECREF(left);
1529 Py_DECREF(right);
1530 SET_TOP(res);
1531 if (res == NULL)
1532 goto error;
1533 DISPATCH();
1534 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001535
Benjamin Petersonddd19492018-09-16 22:38:02 -07001536 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001537 PyObject *right = POP();
1538 PyObject *left = TOP();
1539 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1540 Py_DECREF(left);
1541 Py_DECREF(right);
1542 SET_TOP(res);
1543 if (res == NULL)
1544 goto error;
1545 DISPATCH();
1546 }
1547
Benjamin Petersonddd19492018-09-16 22:38:02 -07001548 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001549 PyObject *divisor = POP();
1550 PyObject *dividend = TOP();
1551 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1552 Py_DECREF(dividend);
1553 Py_DECREF(divisor);
1554 SET_TOP(quotient);
1555 if (quotient == NULL)
1556 goto error;
1557 DISPATCH();
1558 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001559
Benjamin Petersonddd19492018-09-16 22:38:02 -07001560 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001561 PyObject *divisor = POP();
1562 PyObject *dividend = TOP();
1563 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1564 Py_DECREF(dividend);
1565 Py_DECREF(divisor);
1566 SET_TOP(quotient);
1567 if (quotient == NULL)
1568 goto error;
1569 DISPATCH();
1570 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001571
Benjamin Petersonddd19492018-09-16 22:38:02 -07001572 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001573 PyObject *right = POP();
1574 PyObject *left = TOP();
1575 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1576 Py_DECREF(left);
1577 Py_DECREF(right);
1578 SET_TOP(mod);
1579 if (mod == NULL)
1580 goto error;
1581 DISPATCH();
1582 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001583
Benjamin Petersonddd19492018-09-16 22:38:02 -07001584 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001585 PyObject *right = POP();
1586 PyObject *left = TOP();
1587 PyObject *sum;
1588 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1589 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001590 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001591 }
1592 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001593 sum = PyNumber_InPlaceAdd(left, right);
1594 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001595 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001596 Py_DECREF(right);
1597 SET_TOP(sum);
1598 if (sum == NULL)
1599 goto error;
1600 DISPATCH();
1601 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001602
Benjamin Petersonddd19492018-09-16 22:38:02 -07001603 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001604 PyObject *right = POP();
1605 PyObject *left = TOP();
1606 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1607 Py_DECREF(left);
1608 Py_DECREF(right);
1609 SET_TOP(diff);
1610 if (diff == NULL)
1611 goto error;
1612 DISPATCH();
1613 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001614
Benjamin Petersonddd19492018-09-16 22:38:02 -07001615 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001616 PyObject *right = POP();
1617 PyObject *left = TOP();
1618 PyObject *res = PyNumber_InPlaceLshift(left, right);
1619 Py_DECREF(left);
1620 Py_DECREF(right);
1621 SET_TOP(res);
1622 if (res == NULL)
1623 goto error;
1624 DISPATCH();
1625 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001626
Benjamin Petersonddd19492018-09-16 22:38:02 -07001627 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001628 PyObject *right = POP();
1629 PyObject *left = TOP();
1630 PyObject *res = PyNumber_InPlaceRshift(left, right);
1631 Py_DECREF(left);
1632 Py_DECREF(right);
1633 SET_TOP(res);
1634 if (res == NULL)
1635 goto error;
1636 DISPATCH();
1637 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001638
Benjamin Petersonddd19492018-09-16 22:38:02 -07001639 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001640 PyObject *right = POP();
1641 PyObject *left = TOP();
1642 PyObject *res = PyNumber_InPlaceAnd(left, right);
1643 Py_DECREF(left);
1644 Py_DECREF(right);
1645 SET_TOP(res);
1646 if (res == NULL)
1647 goto error;
1648 DISPATCH();
1649 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001650
Benjamin Petersonddd19492018-09-16 22:38:02 -07001651 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001652 PyObject *right = POP();
1653 PyObject *left = TOP();
1654 PyObject *res = PyNumber_InPlaceXor(left, right);
1655 Py_DECREF(left);
1656 Py_DECREF(right);
1657 SET_TOP(res);
1658 if (res == NULL)
1659 goto error;
1660 DISPATCH();
1661 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001662
Benjamin Petersonddd19492018-09-16 22:38:02 -07001663 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001664 PyObject *right = POP();
1665 PyObject *left = TOP();
1666 PyObject *res = PyNumber_InPlaceOr(left, right);
1667 Py_DECREF(left);
1668 Py_DECREF(right);
1669 SET_TOP(res);
1670 if (res == NULL)
1671 goto error;
1672 DISPATCH();
1673 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001674
Benjamin Petersonddd19492018-09-16 22:38:02 -07001675 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001676 PyObject *sub = TOP();
1677 PyObject *container = SECOND();
1678 PyObject *v = THIRD();
1679 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001680 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001681 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001682 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001684 Py_DECREF(container);
1685 Py_DECREF(sub);
1686 if (err != 0)
1687 goto error;
1688 DISPATCH();
1689 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001690
Benjamin Petersonddd19492018-09-16 22:38:02 -07001691 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001692 PyObject *sub = TOP();
1693 PyObject *container = SECOND();
1694 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001695 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00001696 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001697 err = PyObject_DelItem(container, sub);
1698 Py_DECREF(container);
1699 Py_DECREF(sub);
1700 if (err != 0)
1701 goto error;
1702 DISPATCH();
1703 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001704
Benjamin Petersonddd19492018-09-16 22:38:02 -07001705 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01001706 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001707 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001708 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001709 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001710 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001711 PyErr_SetString(PyExc_RuntimeError,
1712 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001713 Py_DECREF(value);
1714 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001715 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001716 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001717 Py_DECREF(value);
1718 if (res == NULL)
1719 goto error;
1720 Py_DECREF(res);
1721 DISPATCH();
1722 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001723
Benjamin Petersonddd19492018-09-16 22:38:02 -07001724 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001725 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001726 switch (oparg) {
1727 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001728 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001729 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001730 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001731 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001732 /* fall through */
1733 case 0:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001734 if (do_raise(exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001735 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001736 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 break;
1738 default:
1739 PyErr_SetString(PyExc_SystemError,
1740 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001741 break;
1742 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001743 goto error;
1744 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001745
Benjamin Petersonddd19492018-09-16 22:38:02 -07001746 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001747 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001748 assert(f->f_iblock == 0);
1749 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001750 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001751
Benjamin Petersonddd19492018-09-16 22:38:02 -07001752 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001753 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001754 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001755 PyObject *obj = TOP();
1756 PyTypeObject *type = Py_TYPE(obj);
1757
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001758 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001759 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001760 }
Yury Selivanov75445082015-05-11 22:57:16 -04001761
1762 if (getter != NULL) {
1763 iter = (*getter)(obj);
1764 Py_DECREF(obj);
1765 if (iter == NULL) {
1766 SET_TOP(NULL);
1767 goto error;
1768 }
1769 }
1770 else {
1771 SET_TOP(NULL);
1772 PyErr_Format(
1773 PyExc_TypeError,
1774 "'async for' requires an object with "
1775 "__aiter__ method, got %.100s",
1776 type->tp_name);
1777 Py_DECREF(obj);
1778 goto error;
1779 }
1780
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001781 if (Py_TYPE(iter)->tp_as_async == NULL ||
1782 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001783
Yury Selivanov398ff912017-03-02 22:20:00 -05001784 SET_TOP(NULL);
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001785 PyErr_Format(
1786 PyExc_TypeError,
1787 "'async for' received an object from __aiter__ "
1788 "that does not implement __anext__: %.100s",
1789 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001790 Py_DECREF(iter);
1791 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001792 }
1793
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001794 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001795 DISPATCH();
1796 }
1797
Benjamin Petersonddd19492018-09-16 22:38:02 -07001798 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001799 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001800 PyObject *next_iter = NULL;
1801 PyObject *awaitable = NULL;
1802 PyObject *aiter = TOP();
1803 PyTypeObject *type = Py_TYPE(aiter);
1804
Yury Selivanoveb636452016-09-08 22:01:51 -07001805 if (PyAsyncGen_CheckExact(aiter)) {
1806 awaitable = type->tp_as_async->am_anext(aiter);
1807 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001808 goto error;
1809 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001810 } else {
1811 if (type->tp_as_async != NULL){
1812 getter = type->tp_as_async->am_anext;
1813 }
Yury Selivanov75445082015-05-11 22:57:16 -04001814
Yury Selivanoveb636452016-09-08 22:01:51 -07001815 if (getter != NULL) {
1816 next_iter = (*getter)(aiter);
1817 if (next_iter == NULL) {
1818 goto error;
1819 }
1820 }
1821 else {
1822 PyErr_Format(
1823 PyExc_TypeError,
1824 "'async for' requires an iterator with "
1825 "__anext__ method, got %.100s",
1826 type->tp_name);
1827 goto error;
1828 }
Yury Selivanov75445082015-05-11 22:57:16 -04001829
Yury Selivanoveb636452016-09-08 22:01:51 -07001830 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1831 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001832 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001833 PyExc_TypeError,
1834 "'async for' received an invalid object "
1835 "from __anext__: %.100s",
1836 Py_TYPE(next_iter)->tp_name);
1837
1838 Py_DECREF(next_iter);
1839 goto error;
1840 } else {
1841 Py_DECREF(next_iter);
1842 }
1843 }
Yury Selivanov75445082015-05-11 22:57:16 -04001844
1845 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001846 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001847 DISPATCH();
1848 }
1849
Benjamin Petersonddd19492018-09-16 22:38:02 -07001850 case TARGET(GET_AWAITABLE): {
1851 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04001852 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04001853 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04001854
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03001855 if (iter == NULL) {
1856 format_awaitable_error(Py_TYPE(iterable),
1857 _Py_OPCODE(next_instr[-2]));
1858 }
1859
Yury Selivanov75445082015-05-11 22:57:16 -04001860 Py_DECREF(iterable);
1861
Yury Selivanovc724bae2016-03-02 11:30:46 -05001862 if (iter != NULL && PyCoro_CheckExact(iter)) {
1863 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
1864 if (yf != NULL) {
1865 /* `iter` is a coroutine object that is being
1866 awaited, `yf` is a pointer to the current awaitable
1867 being awaited on. */
1868 Py_DECREF(yf);
1869 Py_CLEAR(iter);
1870 PyErr_SetString(
1871 PyExc_RuntimeError,
1872 "coroutine is being awaited already");
1873 /* The code below jumps to `error` if `iter` is NULL. */
1874 }
1875 }
1876
Yury Selivanov75445082015-05-11 22:57:16 -04001877 SET_TOP(iter); /* Even if it's NULL */
1878
1879 if (iter == NULL) {
1880 goto error;
1881 }
1882
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001883 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001884 DISPATCH();
1885 }
1886
Benjamin Petersonddd19492018-09-16 22:38:02 -07001887 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001888 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001889 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001890 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001891 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
1892 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001893 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001894 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001895 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001896 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001897 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001898 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001899 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001900 Py_DECREF(v);
1901 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001902 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08001903 if (tstate->c_tracefunc != NULL
1904 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001905 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10001906 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001907 if (err < 0)
1908 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001909 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001910 SET_TOP(val);
1911 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001912 }
Martin Panter95f53c12016-07-18 08:23:26 +00001913 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001914 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001915 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01001916 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03001917 f->f_lasti -= sizeof(_Py_CODEUNIT);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001918 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001919 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001920
Benjamin Petersonddd19492018-09-16 22:38:02 -07001921 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001922 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07001923
1924 if (co->co_flags & CO_ASYNC_GENERATOR) {
1925 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
1926 Py_DECREF(retval);
1927 if (w == NULL) {
1928 retval = NULL;
1929 goto error;
1930 }
1931 retval = w;
1932 }
1933
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001934 f->f_stacktop = stack_pointer;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001935 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001936 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001937
Benjamin Petersonddd19492018-09-16 22:38:02 -07001938 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001939 PyObject *type, *value, *traceback;
1940 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001941 PyTryBlock *b = PyFrame_BlockPop(f);
1942 if (b->b_type != EXCEPT_HANDLER) {
1943 PyErr_SetString(PyExc_SystemError,
1944 "popped block is not an except handler");
1945 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001946 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001947 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
1948 STACK_LEVEL() <= (b)->b_level + 4);
1949 exc_info = tstate->exc_info;
1950 type = exc_info->exc_type;
1951 value = exc_info->exc_value;
1952 traceback = exc_info->exc_traceback;
1953 exc_info->exc_type = POP();
1954 exc_info->exc_value = POP();
1955 exc_info->exc_traceback = POP();
1956 Py_XDECREF(type);
1957 Py_XDECREF(value);
1958 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001959 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001960 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001961
Benjamin Petersonddd19492018-09-16 22:38:02 -07001962 case TARGET(POP_BLOCK): {
1963 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001964 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001965 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001966 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001967
Benjamin Petersonddd19492018-09-16 22:38:02 -07001968 case TARGET(POP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001969 /* If oparg is 0 at the top of the stack are 1 or 6 values:
1970 Either:
1971 - TOP = NULL or an integer
1972 or:
1973 - (TOP, SECOND, THIRD) = exc_info()
1974 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
1975
1976 If oparg is 1 the value for 'return' was additionally pushed
1977 at the top of the stack.
1978 */
1979 PyObject *res = NULL;
1980 if (oparg) {
1981 res = POP();
1982 }
1983 PyObject *exc = POP();
1984 if (exc == NULL || PyLong_CheckExact(exc)) {
1985 Py_XDECREF(exc);
1986 }
1987 else {
1988 Py_DECREF(exc);
1989 Py_DECREF(POP());
1990 Py_DECREF(POP());
1991
1992 PyObject *type, *value, *traceback;
1993 _PyErr_StackItem *exc_info;
1994 PyTryBlock *b = PyFrame_BlockPop(f);
1995 if (b->b_type != EXCEPT_HANDLER) {
1996 PyErr_SetString(PyExc_SystemError,
1997 "popped block is not an except handler");
1998 Py_XDECREF(res);
1999 goto error;
2000 }
2001 assert(STACK_LEVEL() == (b)->b_level + 3);
2002 exc_info = tstate->exc_info;
2003 type = exc_info->exc_type;
2004 value = exc_info->exc_value;
2005 traceback = exc_info->exc_traceback;
2006 exc_info->exc_type = POP();
2007 exc_info->exc_value = POP();
2008 exc_info->exc_traceback = POP();
2009 Py_XDECREF(type);
2010 Py_XDECREF(value);
2011 Py_XDECREF(traceback);
2012 }
2013 if (oparg) {
2014 PUSH(res);
2015 }
2016 DISPATCH();
2017 }
2018
Benjamin Petersonddd19492018-09-16 22:38:02 -07002019 case TARGET(CALL_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002020 PyObject *ret = PyLong_FromLong(INSTR_OFFSET());
2021 if (ret == NULL) {
2022 goto error;
2023 }
2024 PUSH(ret);
2025 JUMPBY(oparg);
2026 FAST_DISPATCH();
2027 }
2028
Benjamin Petersonddd19492018-09-16 22:38:02 -07002029 case TARGET(BEGIN_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002030 /* Push NULL onto the stack for using it in END_FINALLY,
2031 POP_FINALLY, WITH_CLEANUP_START and WITH_CLEANUP_FINISH.
2032 */
2033 PUSH(NULL);
2034 FAST_DISPATCH();
2035 }
2036
Benjamin Petersonddd19492018-09-16 22:38:02 -07002037 case TARGET(END_FINALLY): {
2038 PREDICTED(END_FINALLY);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002039 /* At the top of the stack are 1 or 6 values:
2040 Either:
2041 - TOP = NULL or an integer
2042 or:
2043 - (TOP, SECOND, THIRD) = exc_info()
2044 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2045 */
2046 PyObject *exc = POP();
2047 if (exc == NULL) {
2048 FAST_DISPATCH();
2049 }
2050 else if (PyLong_CheckExact(exc)) {
2051 int ret = _PyLong_AsInt(exc);
2052 Py_DECREF(exc);
2053 if (ret == -1 && PyErr_Occurred()) {
2054 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002055 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002056 JUMPTO(ret);
2057 FAST_DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002058 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002059 else {
2060 assert(PyExceptionClass_Check(exc));
2061 PyObject *val = POP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002062 PyObject *tb = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002063 PyErr_Restore(exc, val, tb);
2064 goto exception_unwind;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002065 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002066 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002067
Benjamin Petersonddd19492018-09-16 22:38:02 -07002068 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002069 PyObject *exc = POP();
2070 assert(PyExceptionClass_Check(exc));
2071 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2072 PyTryBlock *b = PyFrame_BlockPop(f);
2073 assert(b->b_type == EXCEPT_HANDLER);
2074 Py_DECREF(exc);
2075 UNWIND_EXCEPT_HANDLER(b);
2076 Py_DECREF(POP());
2077 JUMPBY(oparg);
2078 FAST_DISPATCH();
2079 }
2080 else {
2081 PyObject *val = POP();
2082 PyObject *tb = POP();
2083 PyErr_Restore(exc, val, tb);
2084 goto exception_unwind;
2085 }
2086 }
2087
Benjamin Petersonddd19492018-09-16 22:38:02 -07002088 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002089 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002090
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002091 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002092 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002093 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
2094 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002095 PyErr_SetString(PyExc_NameError,
2096 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002097 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002098 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002099 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002100 }
2101 else {
2102 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2103 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002104 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002105 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2106 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002107 if (PyErr_ExceptionMatches(PyExc_KeyError))
2108 PyErr_SetString(PyExc_NameError,
2109 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002110 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002111 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002112 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002113 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002114 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002115 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002116
Benjamin Petersonddd19492018-09-16 22:38:02 -07002117 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002118 PyObject *name = GETITEM(names, oparg);
2119 PyObject *v = POP();
2120 PyObject *ns = f->f_locals;
2121 int err;
2122 if (ns == NULL) {
2123 PyErr_Format(PyExc_SystemError,
2124 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002125 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002126 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002127 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002128 if (PyDict_CheckExact(ns))
2129 err = PyDict_SetItem(ns, name, v);
2130 else
2131 err = PyObject_SetItem(ns, name, v);
2132 Py_DECREF(v);
2133 if (err != 0)
2134 goto error;
2135 DISPATCH();
2136 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002137
Benjamin Petersonddd19492018-09-16 22:38:02 -07002138 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002139 PyObject *name = GETITEM(names, oparg);
2140 PyObject *ns = f->f_locals;
2141 int err;
2142 if (ns == NULL) {
2143 PyErr_Format(PyExc_SystemError,
2144 "no locals when deleting %R", name);
2145 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002146 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002147 err = PyObject_DelItem(ns, name);
2148 if (err != 0) {
2149 format_exc_check_arg(PyExc_NameError,
2150 NAME_ERROR_MSG,
2151 name);
2152 goto error;
2153 }
2154 DISPATCH();
2155 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002156
Benjamin Petersonddd19492018-09-16 22:38:02 -07002157 case TARGET(UNPACK_SEQUENCE): {
2158 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002159 PyObject *seq = POP(), *item, **items;
2160 if (PyTuple_CheckExact(seq) &&
2161 PyTuple_GET_SIZE(seq) == oparg) {
2162 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002163 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002164 item = items[oparg];
2165 Py_INCREF(item);
2166 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002167 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002168 } else if (PyList_CheckExact(seq) &&
2169 PyList_GET_SIZE(seq) == oparg) {
2170 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002171 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002172 item = items[oparg];
2173 Py_INCREF(item);
2174 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002175 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002176 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002177 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002178 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002179 } else {
2180 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002181 Py_DECREF(seq);
2182 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002183 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002184 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002185 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002186 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002187
Benjamin Petersonddd19492018-09-16 22:38:02 -07002188 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002189 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2190 PyObject *seq = POP();
2191
2192 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2193 stack_pointer + totalargs)) {
2194 stack_pointer += totalargs;
2195 } else {
2196 Py_DECREF(seq);
2197 goto error;
2198 }
2199 Py_DECREF(seq);
2200 DISPATCH();
2201 }
2202
Benjamin Petersonddd19492018-09-16 22:38:02 -07002203 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002204 PyObject *name = GETITEM(names, oparg);
2205 PyObject *owner = TOP();
2206 PyObject *v = SECOND();
2207 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002208 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002209 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002210 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002211 Py_DECREF(owner);
2212 if (err != 0)
2213 goto error;
2214 DISPATCH();
2215 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002216
Benjamin Petersonddd19492018-09-16 22:38:02 -07002217 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002218 PyObject *name = GETITEM(names, oparg);
2219 PyObject *owner = POP();
2220 int err;
2221 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2222 Py_DECREF(owner);
2223 if (err != 0)
2224 goto error;
2225 DISPATCH();
2226 }
2227
Benjamin Petersonddd19492018-09-16 22:38:02 -07002228 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002229 PyObject *name = GETITEM(names, oparg);
2230 PyObject *v = POP();
2231 int err;
2232 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002233 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002234 if (err != 0)
2235 goto error;
2236 DISPATCH();
2237 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002238
Benjamin Petersonddd19492018-09-16 22:38:02 -07002239 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002240 PyObject *name = GETITEM(names, oparg);
2241 int err;
2242 err = PyDict_DelItem(f->f_globals, name);
2243 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002244 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002245 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002246 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002247 }
2248 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002249 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002250
Benjamin Petersonddd19492018-09-16 22:38:02 -07002251 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002252 PyObject *name = GETITEM(names, oparg);
2253 PyObject *locals = f->f_locals;
2254 PyObject *v;
2255 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002256 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002257 "no locals when loading %R", name);
2258 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002259 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002260 if (PyDict_CheckExact(locals)) {
2261 v = PyDict_GetItem(locals, name);
2262 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002263 }
2264 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002265 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002266 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002267 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2268 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002269 PyErr_Clear();
2270 }
2271 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002272 if (v == NULL) {
2273 v = PyDict_GetItem(f->f_globals, name);
2274 Py_XINCREF(v);
2275 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002276 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002277 v = PyDict_GetItem(f->f_builtins, name);
2278 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002279 format_exc_check_arg(
2280 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002281 NAME_ERROR_MSG, name);
2282 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002283 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002284 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002285 }
2286 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002287 v = PyObject_GetItem(f->f_builtins, name);
2288 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002289 if (PyErr_ExceptionMatches(PyExc_KeyError))
2290 format_exc_check_arg(
2291 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002292 NAME_ERROR_MSG, name);
2293 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002294 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002295 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002296 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002297 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002298 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002299 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002300 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002301
Benjamin Petersonddd19492018-09-16 22:38:02 -07002302 case TARGET(LOAD_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002303 PyObject *name = GETITEM(names, oparg);
2304 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002305 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002306 && PyDict_CheckExact(f->f_builtins))
2307 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002308 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002309 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002310 name);
2311 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002312 if (!_PyErr_OCCURRED()) {
2313 /* _PyDict_LoadGlobal() returns NULL without raising
2314 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002315 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002316 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002317 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002318 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002319 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002320 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002321 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002322 else {
2323 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002324
2325 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002326 v = PyObject_GetItem(f->f_globals, name);
2327 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002328 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2329 goto error;
2330 PyErr_Clear();
2331
Victor Stinnerb4efc962015-11-20 09:24:02 +01002332 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002333 v = PyObject_GetItem(f->f_builtins, name);
2334 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002335 if (PyErr_ExceptionMatches(PyExc_KeyError))
2336 format_exc_check_arg(
2337 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002338 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002339 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002340 }
2341 }
2342 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002343 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002344 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002345 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002346
Benjamin Petersonddd19492018-09-16 22:38:02 -07002347 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002348 PyObject *v = GETLOCAL(oparg);
2349 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002350 SETLOCAL(oparg, NULL);
2351 DISPATCH();
2352 }
2353 format_exc_check_arg(
2354 PyExc_UnboundLocalError,
2355 UNBOUNDLOCAL_ERROR_MSG,
2356 PyTuple_GetItem(co->co_varnames, oparg)
2357 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002358 goto error;
2359 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002360
Benjamin Petersonddd19492018-09-16 22:38:02 -07002361 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002362 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002363 PyObject *oldobj = PyCell_GET(cell);
2364 if (oldobj != NULL) {
2365 PyCell_SET(cell, NULL);
2366 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002367 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002368 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002369 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002370 goto error;
2371 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002372
Benjamin Petersonddd19492018-09-16 22:38:02 -07002373 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002374 PyObject *cell = freevars[oparg];
2375 Py_INCREF(cell);
2376 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002377 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002378 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002379
Benjamin Petersonddd19492018-09-16 22:38:02 -07002380 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002381 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002382 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002383 assert(locals);
2384 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2385 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2386 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2387 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2388 if (PyDict_CheckExact(locals)) {
2389 value = PyDict_GetItem(locals, name);
2390 Py_XINCREF(value);
2391 }
2392 else {
2393 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002394 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002395 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2396 goto error;
2397 PyErr_Clear();
2398 }
2399 }
2400 if (!value) {
2401 PyObject *cell = freevars[oparg];
2402 value = PyCell_GET(cell);
2403 if (value == NULL) {
2404 format_exc_unbound(co, oparg);
2405 goto error;
2406 }
2407 Py_INCREF(value);
2408 }
2409 PUSH(value);
2410 DISPATCH();
2411 }
2412
Benjamin Petersonddd19492018-09-16 22:38:02 -07002413 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002414 PyObject *cell = freevars[oparg];
2415 PyObject *value = PyCell_GET(cell);
2416 if (value == NULL) {
2417 format_exc_unbound(co, oparg);
2418 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002419 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002420 Py_INCREF(value);
2421 PUSH(value);
2422 DISPATCH();
2423 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002424
Benjamin Petersonddd19492018-09-16 22:38:02 -07002425 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002426 PyObject *v = POP();
2427 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002428 PyObject *oldobj = PyCell_GET(cell);
2429 PyCell_SET(cell, v);
2430 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002431 DISPATCH();
2432 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002433
Benjamin Petersonddd19492018-09-16 22:38:02 -07002434 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002435 PyObject *str;
2436 PyObject *empty = PyUnicode_New(0, 0);
2437 if (empty == NULL) {
2438 goto error;
2439 }
2440 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2441 Py_DECREF(empty);
2442 if (str == NULL)
2443 goto error;
2444 while (--oparg >= 0) {
2445 PyObject *item = POP();
2446 Py_DECREF(item);
2447 }
2448 PUSH(str);
2449 DISPATCH();
2450 }
2451
Benjamin Petersonddd19492018-09-16 22:38:02 -07002452 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002453 PyObject *tup = PyTuple_New(oparg);
2454 if (tup == NULL)
2455 goto error;
2456 while (--oparg >= 0) {
2457 PyObject *item = POP();
2458 PyTuple_SET_ITEM(tup, oparg, item);
2459 }
2460 PUSH(tup);
2461 DISPATCH();
2462 }
2463
Benjamin Petersonddd19492018-09-16 22:38:02 -07002464 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002465 PyObject *list = PyList_New(oparg);
2466 if (list == NULL)
2467 goto error;
2468 while (--oparg >= 0) {
2469 PyObject *item = POP();
2470 PyList_SET_ITEM(list, oparg, item);
2471 }
2472 PUSH(list);
2473 DISPATCH();
2474 }
2475
Benjamin Petersonddd19492018-09-16 22:38:02 -07002476 case TARGET(BUILD_TUPLE_UNPACK_WITH_CALL):
2477 case TARGET(BUILD_TUPLE_UNPACK):
2478 case TARGET(BUILD_LIST_UNPACK): {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002479 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002480 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002481 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002482 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002483
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002484 if (sum == NULL)
2485 goto error;
2486
2487 for (i = oparg; i > 0; i--) {
2488 PyObject *none_val;
2489
2490 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2491 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002492 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002493 PyErr_ExceptionMatches(PyExc_TypeError))
2494 {
2495 check_args_iterable(PEEK(1 + oparg), PEEK(i));
Serhiy Storchaka73442852016-10-02 10:33:46 +03002496 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002497 Py_DECREF(sum);
2498 goto error;
2499 }
2500 Py_DECREF(none_val);
2501 }
2502
2503 if (convert_to_tuple) {
2504 return_value = PyList_AsTuple(sum);
2505 Py_DECREF(sum);
2506 if (return_value == NULL)
2507 goto error;
2508 }
2509 else {
2510 return_value = sum;
2511 }
2512
2513 while (oparg--)
2514 Py_DECREF(POP());
2515 PUSH(return_value);
2516 DISPATCH();
2517 }
2518
Benjamin Petersonddd19492018-09-16 22:38:02 -07002519 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002520 PyObject *set = PySet_New(NULL);
2521 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002522 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002523 if (set == NULL)
2524 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002525 for (i = oparg; i > 0; i--) {
2526 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002527 if (err == 0)
2528 err = PySet_Add(set, item);
2529 Py_DECREF(item);
2530 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002531 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002532 if (err != 0) {
2533 Py_DECREF(set);
2534 goto error;
2535 }
2536 PUSH(set);
2537 DISPATCH();
2538 }
2539
Benjamin Petersonddd19492018-09-16 22:38:02 -07002540 case TARGET(BUILD_SET_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002541 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002542 PyObject *sum = PySet_New(NULL);
2543 if (sum == NULL)
2544 goto error;
2545
2546 for (i = oparg; i > 0; i--) {
2547 if (_PySet_Update(sum, PEEK(i)) < 0) {
2548 Py_DECREF(sum);
2549 goto error;
2550 }
2551 }
2552
2553 while (oparg--)
2554 Py_DECREF(POP());
2555 PUSH(sum);
2556 DISPATCH();
2557 }
2558
Benjamin Petersonddd19492018-09-16 22:38:02 -07002559 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002560 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002561 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2562 if (map == NULL)
2563 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002564 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002565 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002566 PyObject *key = PEEK(2*i);
2567 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002568 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002569 if (err != 0) {
2570 Py_DECREF(map);
2571 goto error;
2572 }
2573 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002574
2575 while (oparg--) {
2576 Py_DECREF(POP());
2577 Py_DECREF(POP());
2578 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002579 PUSH(map);
2580 DISPATCH();
2581 }
2582
Benjamin Petersonddd19492018-09-16 22:38:02 -07002583 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002584 _Py_IDENTIFIER(__annotations__);
2585 int err;
2586 PyObject *ann_dict;
2587 if (f->f_locals == NULL) {
2588 PyErr_Format(PyExc_SystemError,
2589 "no locals found when setting up annotations");
2590 goto error;
2591 }
2592 /* check if __annotations__ in locals()... */
2593 if (PyDict_CheckExact(f->f_locals)) {
2594 ann_dict = _PyDict_GetItemId(f->f_locals,
2595 &PyId___annotations__);
2596 if (ann_dict == NULL) {
2597 /* ...if not, create a new one */
2598 ann_dict = PyDict_New();
2599 if (ann_dict == NULL) {
2600 goto error;
2601 }
2602 err = _PyDict_SetItemId(f->f_locals,
2603 &PyId___annotations__, ann_dict);
2604 Py_DECREF(ann_dict);
2605 if (err != 0) {
2606 goto error;
2607 }
2608 }
2609 }
2610 else {
2611 /* do the same if locals() is not a dict */
2612 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2613 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002614 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002615 }
2616 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2617 if (ann_dict == NULL) {
2618 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2619 goto error;
2620 }
2621 PyErr_Clear();
2622 ann_dict = PyDict_New();
2623 if (ann_dict == NULL) {
2624 goto error;
2625 }
2626 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2627 Py_DECREF(ann_dict);
2628 if (err != 0) {
2629 goto error;
2630 }
2631 }
2632 else {
2633 Py_DECREF(ann_dict);
2634 }
2635 }
2636 DISPATCH();
2637 }
2638
Benjamin Petersonddd19492018-09-16 22:38:02 -07002639 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002640 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002641 PyObject *map;
2642 PyObject *keys = TOP();
2643 if (!PyTuple_CheckExact(keys) ||
2644 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2645 PyErr_SetString(PyExc_SystemError,
2646 "bad BUILD_CONST_KEY_MAP keys argument");
2647 goto error;
2648 }
2649 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2650 if (map == NULL) {
2651 goto error;
2652 }
2653 for (i = oparg; i > 0; i--) {
2654 int err;
2655 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2656 PyObject *value = PEEK(i + 1);
2657 err = PyDict_SetItem(map, key, value);
2658 if (err != 0) {
2659 Py_DECREF(map);
2660 goto error;
2661 }
2662 }
2663
2664 Py_DECREF(POP());
2665 while (oparg--) {
2666 Py_DECREF(POP());
2667 }
2668 PUSH(map);
2669 DISPATCH();
2670 }
2671
Benjamin Petersonddd19492018-09-16 22:38:02 -07002672 case TARGET(BUILD_MAP_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002673 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002674 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002675 if (sum == NULL)
2676 goto error;
2677
2678 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002679 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002680 if (PyDict_Update(sum, arg) < 0) {
2681 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2682 PyErr_Format(PyExc_TypeError,
Berker Peksag8e9045d2016-10-02 13:08:25 +03002683 "'%.200s' object is not a mapping",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002684 arg->ob_type->tp_name);
2685 }
2686 Py_DECREF(sum);
2687 goto error;
2688 }
2689 }
2690
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002691 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002692 Py_DECREF(POP());
2693 PUSH(sum);
2694 DISPATCH();
2695 }
2696
Benjamin Petersonddd19492018-09-16 22:38:02 -07002697 case TARGET(BUILD_MAP_UNPACK_WITH_CALL): {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002698 Py_ssize_t i;
2699 PyObject *sum = PyDict_New();
2700 if (sum == NULL)
2701 goto error;
2702
2703 for (i = oparg; i > 0; i--) {
2704 PyObject *arg = PEEK(i);
2705 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002706 Py_DECREF(sum);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02002707 format_kwargs_error(PEEK(2 + oparg), arg);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002708 goto error;
2709 }
2710 }
2711
2712 while (oparg--)
2713 Py_DECREF(POP());
2714 PUSH(sum);
2715 DISPATCH();
2716 }
2717
Benjamin Petersonddd19492018-09-16 22:38:02 -07002718 case TARGET(MAP_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002719 PyObject *key = TOP();
2720 PyObject *value = SECOND();
2721 PyObject *map;
2722 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002723 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002724 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002725 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002726 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002727 Py_DECREF(value);
2728 Py_DECREF(key);
2729 if (err != 0)
2730 goto error;
2731 PREDICT(JUMP_ABSOLUTE);
2732 DISPATCH();
2733 }
2734
Benjamin Petersonddd19492018-09-16 22:38:02 -07002735 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002736 PyObject *name = GETITEM(names, oparg);
2737 PyObject *owner = TOP();
2738 PyObject *res = PyObject_GetAttr(owner, name);
2739 Py_DECREF(owner);
2740 SET_TOP(res);
2741 if (res == NULL)
2742 goto error;
2743 DISPATCH();
2744 }
2745
Benjamin Petersonddd19492018-09-16 22:38:02 -07002746 case TARGET(COMPARE_OP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002747 PyObject *right = POP();
2748 PyObject *left = TOP();
2749 PyObject *res = cmp_outcome(oparg, left, right);
2750 Py_DECREF(left);
2751 Py_DECREF(right);
2752 SET_TOP(res);
2753 if (res == NULL)
2754 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002755 PREDICT(POP_JUMP_IF_FALSE);
2756 PREDICT(POP_JUMP_IF_TRUE);
2757 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002758 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002759
Benjamin Petersonddd19492018-09-16 22:38:02 -07002760 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002761 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002762 PyObject *fromlist = POP();
2763 PyObject *level = TOP();
2764 PyObject *res;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002765 res = import_name(f, name, fromlist, level);
2766 Py_DECREF(level);
2767 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002768 SET_TOP(res);
2769 if (res == NULL)
2770 goto error;
2771 DISPATCH();
2772 }
2773
Benjamin Petersonddd19492018-09-16 22:38:02 -07002774 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002775 PyObject *from = POP(), *locals;
2776 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002777 if (PyFrame_FastToLocalsWithError(f) < 0) {
2778 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002779 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002780 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002781
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002782 locals = f->f_locals;
2783 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002784 PyErr_SetString(PyExc_SystemError,
2785 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002786 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002787 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002788 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002789 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002790 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002791 Py_DECREF(from);
2792 if (err != 0)
2793 goto error;
2794 DISPATCH();
2795 }
Guido van Rossum25831651993-05-19 14:50:45 +00002796
Benjamin Petersonddd19492018-09-16 22:38:02 -07002797 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002798 PyObject *name = GETITEM(names, oparg);
2799 PyObject *from = TOP();
2800 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002801 res = import_from(from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002802 PUSH(res);
2803 if (res == NULL)
2804 goto error;
2805 DISPATCH();
2806 }
Thomas Wouters52152252000-08-17 22:55:00 +00002807
Benjamin Petersonddd19492018-09-16 22:38:02 -07002808 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002809 JUMPBY(oparg);
2810 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002811 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002812
Benjamin Petersonddd19492018-09-16 22:38:02 -07002813 case TARGET(POP_JUMP_IF_FALSE): {
2814 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002815 PyObject *cond = POP();
2816 int err;
2817 if (cond == Py_True) {
2818 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002819 FAST_DISPATCH();
2820 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002821 if (cond == Py_False) {
2822 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002823 JUMPTO(oparg);
2824 FAST_DISPATCH();
2825 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002826 err = PyObject_IsTrue(cond);
2827 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002828 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07002829 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002830 else if (err == 0)
2831 JUMPTO(oparg);
2832 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002833 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002834 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002835 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002836
Benjamin Petersonddd19492018-09-16 22:38:02 -07002837 case TARGET(POP_JUMP_IF_TRUE): {
2838 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002839 PyObject *cond = POP();
2840 int err;
2841 if (cond == Py_False) {
2842 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002843 FAST_DISPATCH();
2844 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002845 if (cond == Py_True) {
2846 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002847 JUMPTO(oparg);
2848 FAST_DISPATCH();
2849 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002850 err = PyObject_IsTrue(cond);
2851 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002852 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002853 JUMPTO(oparg);
2854 }
2855 else if (err == 0)
2856 ;
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 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002861
Benjamin Petersonddd19492018-09-16 22:38:02 -07002862 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002863 PyObject *cond = TOP();
2864 int err;
2865 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002866 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002867 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_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002871 JUMPTO(oparg);
2872 FAST_DISPATCH();
2873 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002874 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002875 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002876 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002877 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002878 }
2879 else if (err == 0)
2880 JUMPTO(oparg);
2881 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002882 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002883 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002884 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002885
Benjamin Petersonddd19492018-09-16 22:38:02 -07002886 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002887 PyObject *cond = TOP();
2888 int err;
2889 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002890 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002891 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002892 FAST_DISPATCH();
2893 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002894 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002895 JUMPTO(oparg);
2896 FAST_DISPATCH();
2897 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002898 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002899 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002900 JUMPTO(oparg);
2901 }
2902 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002903 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002904 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002905 }
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 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002910
Benjamin Petersonddd19492018-09-16 22:38:02 -07002911 case TARGET(JUMP_ABSOLUTE): {
2912 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002913 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002914#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002915 /* Enabling this path speeds-up all while and for-loops by bypassing
2916 the per-loop checks for signals. By default, this should be turned-off
2917 because it prevents detection of a control-break in tight loops like
2918 "while 1: pass". Compile with this option turned-on when you need
2919 the speed-up and do not need break checking inside tight loops (ones
2920 that contain only instructions ending with FAST_DISPATCH).
2921 */
2922 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002923#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002924 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002925#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002926 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002927
Benjamin Petersonddd19492018-09-16 22:38:02 -07002928 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002929 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002930 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002931 PyObject *iter = PyObject_GetIter(iterable);
2932 Py_DECREF(iterable);
2933 SET_TOP(iter);
2934 if (iter == NULL)
2935 goto error;
2936 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002937 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04002938 DISPATCH();
2939 }
2940
Benjamin Petersonddd19492018-09-16 22:38:02 -07002941 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04002942 /* before: [obj]; after [getiter(obj)] */
2943 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04002944 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04002945 if (PyCoro_CheckExact(iterable)) {
2946 /* `iterable` is a coroutine */
2947 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
2948 /* and it is used in a 'yield from' expression of a
2949 regular generator. */
2950 Py_DECREF(iterable);
2951 SET_TOP(NULL);
2952 PyErr_SetString(PyExc_TypeError,
2953 "cannot 'yield from' a coroutine object "
2954 "in a non-coroutine generator");
2955 goto error;
2956 }
2957 }
2958 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04002959 /* `iterable` is not a generator. */
2960 iter = PyObject_GetIter(iterable);
2961 Py_DECREF(iterable);
2962 SET_TOP(iter);
2963 if (iter == NULL)
2964 goto error;
2965 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002966 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002967 DISPATCH();
2968 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002969
Benjamin Petersonddd19492018-09-16 22:38:02 -07002970 case TARGET(FOR_ITER): {
2971 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002972 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002973 PyObject *iter = TOP();
2974 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
2975 if (next != NULL) {
2976 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002977 PREDICT(STORE_FAST);
2978 PREDICT(UNPACK_SEQUENCE);
2979 DISPATCH();
2980 }
2981 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002982 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2983 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08002984 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002985 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002986 PyErr_Clear();
2987 }
2988 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00002989 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002990 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002991 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002992 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002993 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002994 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002995
Benjamin Petersonddd19492018-09-16 22:38:02 -07002996 case TARGET(SETUP_FINALLY): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002997 /* NOTE: If you add any new block-setup opcodes that
2998 are not try/except/finally handlers, you may need
2999 to update the PyGen_NeedsFinalizing() function.
3000 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003001
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003002 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003003 STACK_LEVEL());
3004 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003005 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003006
Benjamin Petersonddd19492018-09-16 22:38:02 -07003007 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003008 _Py_IDENTIFIER(__aexit__);
3009 _Py_IDENTIFIER(__aenter__);
3010
3011 PyObject *mgr = TOP();
3012 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
3013 *enter;
3014 PyObject *res;
3015 if (exit == NULL)
3016 goto error;
3017 SET_TOP(exit);
3018 enter = special_lookup(mgr, &PyId___aenter__);
3019 Py_DECREF(mgr);
3020 if (enter == NULL)
3021 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003022 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003023 Py_DECREF(enter);
3024 if (res == NULL)
3025 goto error;
3026 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003027 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003028 DISPATCH();
3029 }
3030
Benjamin Petersonddd19492018-09-16 22:38:02 -07003031 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003032 PyObject *res = POP();
3033 /* Setup the finally block before pushing the result
3034 of __aenter__ on the stack. */
3035 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3036 STACK_LEVEL());
3037 PUSH(res);
3038 DISPATCH();
3039 }
3040
Benjamin Petersonddd19492018-09-16 22:38:02 -07003041 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003042 _Py_IDENTIFIER(__exit__);
3043 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003044 PyObject *mgr = TOP();
Raymond Hettingera3fec152016-11-21 17:24:23 -08003045 PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003046 PyObject *res;
Raymond Hettingera3fec152016-11-21 17:24:23 -08003047 if (enter == NULL)
3048 goto error;
3049 exit = special_lookup(mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003050 if (exit == NULL) {
3051 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003052 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003053 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003054 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003055 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003056 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003057 Py_DECREF(enter);
3058 if (res == NULL)
3059 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003060 /* Setup the finally block before pushing the result
3061 of __enter__ on the stack. */
3062 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3063 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003064
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003065 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003066 DISPATCH();
3067 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003068
Benjamin Petersonddd19492018-09-16 22:38:02 -07003069 case TARGET(WITH_CLEANUP_START): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003070 /* At the top of the stack are 1 or 6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003071 how/why we entered the finally clause:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003072 - TOP = NULL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003073 - (TOP, SECOND, THIRD) = exc_info()
3074 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003075 Below them is EXIT, the context.__exit__ or context.__aexit__
3076 bound method.
3077 In the first case, we must call
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003078 EXIT(None, None, None)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003079 otherwise we must call
3080 EXIT(TOP, SECOND, THIRD)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003081
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003082 In the first case, we remove EXIT from the
3083 stack, leaving TOP, and push TOP on the stack.
3084 Otherwise we shift the bottom 3 values of the
3085 stack down, replace the empty spot with NULL, and push
3086 None on the stack.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003087
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003088 Finally we push the result of the call.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003089 */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003090 PyObject *stack[3];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003091 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003092 PyObject *exc, *val, *tb, *res;
3093
3094 val = tb = Py_None;
3095 exc = TOP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003096 if (exc == NULL) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003097 STACK_SHRINK(1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003098 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003099 SET_TOP(exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003100 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003101 }
3102 else {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003103 assert(PyExceptionClass_Check(exc));
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003104 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003105 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003106 val = SECOND();
3107 tb = THIRD();
3108 tp2 = FOURTH();
3109 exc2 = PEEK(5);
3110 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003111 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003112 SET_VALUE(7, tb2);
3113 SET_VALUE(6, exc2);
3114 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003115 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3116 SET_FOURTH(NULL);
3117 /* We just shifted the stack down, so we have
3118 to tell the except handler block that the
3119 values are lower than it expects. */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003120 assert(f->f_iblock > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003121 block = &f->f_blockstack[f->f_iblock - 1];
3122 assert(block->b_type == EXCEPT_HANDLER);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003123 assert(block->b_level > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003124 block->b_level--;
3125 }
Victor Stinner842cfff2016-12-01 14:45:31 +01003126
3127 stack[0] = exc;
3128 stack[1] = val;
3129 stack[2] = tb;
3130 res = _PyObject_FastCall(exit_func, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003131 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003132 if (res == NULL)
3133 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003134
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003135 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003136 PUSH(exc);
3137 PUSH(res);
3138 PREDICT(WITH_CLEANUP_FINISH);
3139 DISPATCH();
3140 }
3141
Benjamin Petersonddd19492018-09-16 22:38:02 -07003142 case TARGET(WITH_CLEANUP_FINISH): {
3143 PREDICTED(WITH_CLEANUP_FINISH);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003144 /* TOP = the result of calling the context.__exit__ bound method
3145 SECOND = either None or exception type
3146
3147 If SECOND is None below is NULL or the return address,
3148 otherwise below are 7 values representing an exception.
3149 */
Yury Selivanov75445082015-05-11 22:57:16 -04003150 PyObject *res = POP();
3151 PyObject *exc = POP();
3152 int err;
3153
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003154 if (exc != Py_None)
3155 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003156 else
3157 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003158
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003159 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003160 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003162 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003163 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003164 else if (err > 0) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003165 /* There was an exception and a True return.
3166 * We must manually unwind the EXCEPT_HANDLER block
3167 * which was created when the exception was caught,
Quan Tian3bd0d622018-10-20 05:30:03 +08003168 * otherwise the stack will be in an inconsistent state.
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003169 */
3170 PyTryBlock *b = PyFrame_BlockPop(f);
3171 assert(b->b_type == EXCEPT_HANDLER);
3172 UNWIND_EXCEPT_HANDLER(b);
3173 PUSH(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003174 }
3175 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003176 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003177 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003178
Benjamin Petersonddd19492018-09-16 22:38:02 -07003179 case TARGET(LOAD_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003180 /* Designed to work in tamdem with CALL_METHOD. */
3181 PyObject *name = GETITEM(names, oparg);
3182 PyObject *obj = TOP();
3183 PyObject *meth = NULL;
3184
3185 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3186
Yury Selivanovf2392132016-12-13 19:03:51 -05003187 if (meth == NULL) {
3188 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003189 goto error;
3190 }
3191
3192 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003193 /* We can bypass temporary bound method object.
3194 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003195
INADA Naoki015bce62017-01-16 17:23:30 +09003196 meth | self | arg1 | ... | argN
3197 */
3198 SET_TOP(meth);
3199 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003200 }
3201 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003202 /* meth is not an unbound method (but a regular attr, or
3203 something was returned by a descriptor protocol). Set
3204 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003205 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003206
3207 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003208 */
INADA Naoki015bce62017-01-16 17:23:30 +09003209 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003210 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003211 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003212 }
3213 DISPATCH();
3214 }
3215
Benjamin Petersonddd19492018-09-16 22:38:02 -07003216 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003217 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003218 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003219
3220 sp = stack_pointer;
3221
INADA Naoki015bce62017-01-16 17:23:30 +09003222 meth = PEEK(oparg + 2);
3223 if (meth == NULL) {
3224 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3225 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003226
3227 Stack layout:
3228
INADA Naoki015bce62017-01-16 17:23:30 +09003229 ... | NULL | callable | arg1 | ... | argN
3230 ^- TOP()
3231 ^- (-oparg)
3232 ^- (-oparg-1)
3233 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003234
Ville Skyttä49b27342017-08-03 09:00:59 +03003235 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003236 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003237 */
Yury Selivanovf2392132016-12-13 19:03:51 -05003238 res = call_function(&sp, oparg, NULL);
3239 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003240 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003241 }
3242 else {
3243 /* This is a method call. Stack layout:
3244
INADA Naoki015bce62017-01-16 17:23:30 +09003245 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003246 ^- TOP()
3247 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003248 ^- (-oparg-1)
3249 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003250
INADA Naoki015bce62017-01-16 17:23:30 +09003251 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003252 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003253 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003254 */
3255 res = call_function(&sp, oparg + 1, NULL);
3256 stack_pointer = sp;
3257 }
3258
3259 PUSH(res);
3260 if (res == NULL)
3261 goto error;
3262 DISPATCH();
3263 }
3264
Benjamin Petersonddd19492018-09-16 22:38:02 -07003265 case TARGET(CALL_FUNCTION): {
3266 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003267 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003268 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003269 res = call_function(&sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003270 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003271 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003272 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003273 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003274 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003275 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003276 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003277
Benjamin Petersonddd19492018-09-16 22:38:02 -07003278 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003279 PyObject **sp, *res, *names;
3280
3281 names = POP();
3282 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003283 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003284 res = call_function(&sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003285 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003286 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003287 Py_DECREF(names);
3288
3289 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003290 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003291 }
3292 DISPATCH();
3293 }
3294
Benjamin Petersonddd19492018-09-16 22:38:02 -07003295 case TARGET(CALL_FUNCTION_EX): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003296 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003297 if (oparg & 0x01) {
3298 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003299 if (!PyDict_CheckExact(kwargs)) {
3300 PyObject *d = PyDict_New();
3301 if (d == NULL)
3302 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003303 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003304 Py_DECREF(d);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003305 format_kwargs_error(SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003306 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003307 goto error;
3308 }
3309 Py_DECREF(kwargs);
3310 kwargs = d;
3311 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003312 assert(PyDict_CheckExact(kwargs));
3313 }
3314 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003315 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003316 if (!PyTuple_CheckExact(callargs)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003317 if (check_args_iterable(func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003318 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003319 goto error;
3320 }
3321 Py_SETREF(callargs, PySequence_Tuple(callargs));
3322 if (callargs == NULL) {
3323 goto error;
3324 }
3325 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003326 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003327
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003328 result = do_call_core(func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003329 Py_DECREF(func);
3330 Py_DECREF(callargs);
3331 Py_XDECREF(kwargs);
3332
3333 SET_TOP(result);
3334 if (result == NULL) {
3335 goto error;
3336 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003337 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003338 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003339
Benjamin Petersonddd19492018-09-16 22:38:02 -07003340 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003341 PyObject *qualname = POP();
3342 PyObject *codeobj = POP();
3343 PyFunctionObject *func = (PyFunctionObject *)
3344 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003345
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003346 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003347 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003348 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003349 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003350 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003351
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003352 if (oparg & 0x08) {
3353 assert(PyTuple_CheckExact(TOP()));
3354 func ->func_closure = POP();
3355 }
3356 if (oparg & 0x04) {
3357 assert(PyDict_CheckExact(TOP()));
3358 func->func_annotations = POP();
3359 }
3360 if (oparg & 0x02) {
3361 assert(PyDict_CheckExact(TOP()));
3362 func->func_kwdefaults = POP();
3363 }
3364 if (oparg & 0x01) {
3365 assert(PyTuple_CheckExact(TOP()));
3366 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003367 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003368
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003369 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003370 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003371 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003372
Benjamin Petersonddd19492018-09-16 22:38:02 -07003373 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003374 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003375 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003376 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003377 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003378 step = NULL;
3379 stop = POP();
3380 start = TOP();
3381 slice = PySlice_New(start, stop, step);
3382 Py_DECREF(start);
3383 Py_DECREF(stop);
3384 Py_XDECREF(step);
3385 SET_TOP(slice);
3386 if (slice == NULL)
3387 goto error;
3388 DISPATCH();
3389 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003390
Benjamin Petersonddd19492018-09-16 22:38:02 -07003391 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003392 /* Handles f-string value formatting. */
3393 PyObject *result;
3394 PyObject *fmt_spec;
3395 PyObject *value;
3396 PyObject *(*conv_fn)(PyObject *);
3397 int which_conversion = oparg & FVC_MASK;
3398 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3399
3400 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003401 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003402
3403 /* See if any conversion is specified. */
3404 switch (which_conversion) {
3405 case FVC_STR: conv_fn = PyObject_Str; break;
3406 case FVC_REPR: conv_fn = PyObject_Repr; break;
3407 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3408
3409 /* Must be 0 (meaning no conversion), since only four
3410 values are allowed by (oparg & FVC_MASK). */
3411 default: conv_fn = NULL; break;
3412 }
3413
3414 /* If there's a conversion function, call it and replace
3415 value with that result. Otherwise, just use value,
3416 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003417 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003418 result = conv_fn(value);
3419 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003420 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003421 Py_XDECREF(fmt_spec);
3422 goto error;
3423 }
3424 value = result;
3425 }
3426
3427 /* If value is a unicode object, and there's no fmt_spec,
3428 then we know the result of format(value) is value
3429 itself. In that case, skip calling format(). I plan to
3430 move this optimization in to PyObject_Format()
3431 itself. */
3432 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3433 /* Do nothing, just transfer ownership to result. */
3434 result = value;
3435 } else {
3436 /* Actually call format(). */
3437 result = PyObject_Format(value, fmt_spec);
3438 Py_DECREF(value);
3439 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003440 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003441 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003442 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003443 }
3444
Eric V. Smith135d5f42016-02-05 18:23:08 -05003445 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003446 DISPATCH();
3447 }
3448
Benjamin Petersonddd19492018-09-16 22:38:02 -07003449 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003450 int oldoparg = oparg;
3451 NEXTOPARG();
3452 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003453 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003454 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003455
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003456
Antoine Pitrou042b1282010-08-13 21:15:58 +00003457#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003458 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003459#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003460 default:
3461 fprintf(stderr,
3462 "XXX lineno: %d, opcode: %d\n",
3463 PyFrame_GetLineNumber(f),
3464 opcode);
3465 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003466 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003467
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003468 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003469
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003470 /* This should never be reached. Every opcode should end with DISPATCH()
3471 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003472 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003473
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003474error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003475 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003476#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003477 if (!PyErr_Occurred())
3478 PyErr_SetString(PyExc_SystemError,
3479 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003480#else
3481 assert(PyErr_Occurred());
3482#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003483
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003484 /* Log traceback info. */
3485 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003486
Benjamin Peterson51f46162013-01-23 08:38:47 -05003487 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003488 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3489 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003490
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003491exception_unwind:
3492 /* Unwind stacks if an exception occurred */
3493 while (f->f_iblock > 0) {
3494 /* Pop the current block. */
3495 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003497 if (b->b_type == EXCEPT_HANDLER) {
3498 UNWIND_EXCEPT_HANDLER(b);
3499 continue;
3500 }
3501 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003502 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003503 PyObject *exc, *val, *tb;
3504 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003505 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003506 /* Beware, this invalidates all b->b_* fields */
3507 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003508 PUSH(exc_info->exc_traceback);
3509 PUSH(exc_info->exc_value);
3510 if (exc_info->exc_type != NULL) {
3511 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003512 }
3513 else {
3514 Py_INCREF(Py_None);
3515 PUSH(Py_None);
3516 }
3517 PyErr_Fetch(&exc, &val, &tb);
3518 /* Make the raw exception data
3519 available to the handler,
3520 so a program can emulate the
3521 Python main loop. */
3522 PyErr_NormalizeException(
3523 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003524 if (tb != NULL)
3525 PyException_SetTraceback(val, tb);
3526 else
3527 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003528 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003529 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003530 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003531 exc_info->exc_value = val;
3532 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003533 if (tb == NULL)
3534 tb = Py_None;
3535 Py_INCREF(tb);
3536 PUSH(tb);
3537 PUSH(val);
3538 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003539 JUMPTO(handler);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003540 /* Resume normal execution */
3541 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003542 }
3543 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003544
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003545 /* End the loop as we still have an error */
3546 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003547 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003549 /* Pop remaining stack entries. */
3550 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003551 PyObject *o = POP();
3552 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003553 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003554
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003555 assert(retval == NULL);
3556 assert(PyErr_Occurred());
Guido van Rossumac7be682001-01-17 15:42:30 +00003557
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003558return_or_yield:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003559 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003560 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003561 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3562 tstate, f, PyTrace_RETURN, retval)) {
3563 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003564 }
3565 }
3566 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003567 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3568 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003569 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003570 }
3571 }
3572 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003574 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003575exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003576 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3577 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003578 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003579 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003580 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003581
Victor Stinnerefde1462015-03-21 15:04:43 +01003582 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003583}
3584
Benjamin Petersonb204a422011-06-05 22:04:07 -05003585static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003586format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3587{
3588 int err;
3589 Py_ssize_t len = PyList_GET_SIZE(names);
3590 PyObject *name_str, *comma, *tail, *tmp;
3591
3592 assert(PyList_CheckExact(names));
3593 assert(len >= 1);
3594 /* Deal with the joys of natural language. */
3595 switch (len) {
3596 case 1:
3597 name_str = PyList_GET_ITEM(names, 0);
3598 Py_INCREF(name_str);
3599 break;
3600 case 2:
3601 name_str = PyUnicode_FromFormat("%U and %U",
3602 PyList_GET_ITEM(names, len - 2),
3603 PyList_GET_ITEM(names, len - 1));
3604 break;
3605 default:
3606 tail = PyUnicode_FromFormat(", %U, and %U",
3607 PyList_GET_ITEM(names, len - 2),
3608 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003609 if (tail == NULL)
3610 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003611 /* Chop off the last two objects in the list. This shouldn't actually
3612 fail, but we can't be too careful. */
3613 err = PyList_SetSlice(names, len - 2, len, NULL);
3614 if (err == -1) {
3615 Py_DECREF(tail);
3616 return;
3617 }
3618 /* Stitch everything up into a nice comma-separated list. */
3619 comma = PyUnicode_FromString(", ");
3620 if (comma == NULL) {
3621 Py_DECREF(tail);
3622 return;
3623 }
3624 tmp = PyUnicode_Join(comma, names);
3625 Py_DECREF(comma);
3626 if (tmp == NULL) {
3627 Py_DECREF(tail);
3628 return;
3629 }
3630 name_str = PyUnicode_Concat(tmp, tail);
3631 Py_DECREF(tmp);
3632 Py_DECREF(tail);
3633 break;
3634 }
3635 if (name_str == NULL)
3636 return;
3637 PyErr_Format(PyExc_TypeError,
3638 "%U() missing %i required %s argument%s: %U",
3639 co->co_name,
3640 len,
3641 kind,
3642 len == 1 ? "" : "s",
3643 name_str);
3644 Py_DECREF(name_str);
3645}
3646
3647static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003648missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003649 PyObject **fastlocals)
3650{
Victor Stinner74319ae2016-08-25 00:04:09 +02003651 Py_ssize_t i, j = 0;
3652 Py_ssize_t start, end;
3653 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003654 const char *kind = positional ? "positional" : "keyword-only";
3655 PyObject *missing_names;
3656
3657 /* Compute the names of the arguments that are missing. */
3658 missing_names = PyList_New(missing);
3659 if (missing_names == NULL)
3660 return;
3661 if (positional) {
3662 start = 0;
3663 end = co->co_argcount - defcount;
3664 }
3665 else {
3666 start = co->co_argcount;
3667 end = start + co->co_kwonlyargcount;
3668 }
3669 for (i = start; i < end; i++) {
3670 if (GETLOCAL(i) == NULL) {
3671 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3672 PyObject *name = PyObject_Repr(raw);
3673 if (name == NULL) {
3674 Py_DECREF(missing_names);
3675 return;
3676 }
3677 PyList_SET_ITEM(missing_names, j++, name);
3678 }
3679 }
3680 assert(j == missing);
3681 format_missing(kind, co, missing_names);
3682 Py_DECREF(missing_names);
3683}
3684
3685static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003686too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3687 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003688{
3689 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003690 Py_ssize_t kwonly_given = 0;
3691 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003692 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003693 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003694
Benjamin Petersone109c702011-06-24 09:37:26 -05003695 assert((co->co_flags & CO_VARARGS) == 0);
3696 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003697 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3698 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003699 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003700 }
3701 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003702 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003703 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003704 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003705 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003706 }
3707 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003708 plural = (co_argcount != 1);
3709 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003710 }
3711 if (sig == NULL)
3712 return;
3713 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003714 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3715 kwonly_sig = PyUnicode_FromFormat(format,
3716 given != 1 ? "s" : "",
3717 kwonly_given,
3718 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003719 if (kwonly_sig == NULL) {
3720 Py_DECREF(sig);
3721 return;
3722 }
3723 }
3724 else {
3725 /* This will not fail. */
3726 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003727 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003728 }
3729 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003730 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003731 co->co_name,
3732 sig,
3733 plural ? "s" : "",
3734 given,
3735 kwonly_sig,
3736 given == 1 && !kwonly_given ? "was" : "were");
3737 Py_DECREF(sig);
3738 Py_DECREF(kwonly_sig);
3739}
3740
Guido van Rossumc2e20742006-02-27 22:32:47 +00003741/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02003742 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003743 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003744
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003745PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +02003746_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003747 PyObject *const *args, Py_ssize_t argcount,
3748 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003749 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003750 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003751 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003752 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003753{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003754 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003755 PyFrameObject *f;
3756 PyObject *retval = NULL;
3757 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003758 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003759 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003760 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3761 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003762 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003764 if (globals == NULL) {
3765 PyErr_SetString(PyExc_SystemError,
3766 "PyEval_EvalCodeEx: NULL globals");
3767 return NULL;
3768 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003769
Victor Stinnerc7020012016-08-16 23:40:29 +02003770 /* Create the frame */
Victor Stinner50b48572018-11-01 01:51:40 +01003771 tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003772 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003773 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003774 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003775 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003776 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003777 fastlocals = f->f_localsplus;
3778 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003779
Victor Stinnerc7020012016-08-16 23:40:29 +02003780 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003781 if (co->co_flags & CO_VARKEYWORDS) {
3782 kwdict = PyDict_New();
3783 if (kwdict == NULL)
3784 goto fail;
3785 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003786 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003787 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003788 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003789 SETLOCAL(i, kwdict);
3790 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003791 else {
3792 kwdict = NULL;
3793 }
3794
3795 /* Copy positional arguments into local variables */
3796 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003797 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003798 }
3799 else {
3800 n = argcount;
3801 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003802 for (i = 0; i < n; i++) {
3803 x = args[i];
3804 Py_INCREF(x);
3805 SETLOCAL(i, x);
3806 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003807
3808 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003809 if (co->co_flags & CO_VARARGS) {
3810 u = PyTuple_New(argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003811 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003812 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003813 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003814 SETLOCAL(total_args, u);
3815 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003816 x = args[i];
3817 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003818 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003819 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003820 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003821
Serhiy Storchakab7281052016-09-12 00:52:40 +03003822 /* Handle keyword arguments passed as two strided arrays */
3823 kwcount *= kwstep;
3824 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003825 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003826 PyObject *keyword = kwnames[i];
3827 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003828 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003829
Benjamin Petersonb204a422011-06-05 22:04:07 -05003830 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3831 PyErr_Format(PyExc_TypeError,
3832 "%U() keywords must be strings",
3833 co->co_name);
3834 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003835 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003836
Benjamin Petersonb204a422011-06-05 22:04:07 -05003837 /* Speed hack: do raw pointer compares. As names are
3838 normally interned this should almost always hit. */
3839 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3840 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003841 PyObject *name = co_varnames[j];
3842 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003843 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003844 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003845 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003846
Benjamin Petersonb204a422011-06-05 22:04:07 -05003847 /* Slow fallback, just in case */
3848 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003849 PyObject *name = co_varnames[j];
3850 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
3851 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003852 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003853 }
3854 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003855 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003856 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003857 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003858
Victor Stinner231d1f32017-01-11 02:12:06 +01003859 assert(j >= total_args);
3860 if (kwdict == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003861 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003862 "%U() got an unexpected keyword argument '%S'",
3863 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003864 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003865 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003866
Christian Heimes0bd447f2013-07-20 14:48:10 +02003867 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3868 goto fail;
3869 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003870 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02003871
Benjamin Petersonb204a422011-06-05 22:04:07 -05003872 kw_found:
3873 if (GETLOCAL(j) != NULL) {
3874 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003875 "%U() got multiple values for argument '%S'",
3876 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003877 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003878 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003879 Py_INCREF(value);
3880 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003881 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003882
3883 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003884 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003885 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003886 goto fail;
3887 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003888
3889 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003890 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02003891 Py_ssize_t m = co->co_argcount - defcount;
3892 Py_ssize_t missing = 0;
3893 for (i = argcount; i < m; i++) {
3894 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003895 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02003896 }
3897 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003898 if (missing) {
3899 missing_arguments(co, missing, defcount, fastlocals);
3900 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003901 }
3902 if (n > m)
3903 i = n - m;
3904 else
3905 i = 0;
3906 for (; i < defcount; i++) {
3907 if (GETLOCAL(m+i) == NULL) {
3908 PyObject *def = defs[i];
3909 Py_INCREF(def);
3910 SETLOCAL(m+i, def);
3911 }
3912 }
3913 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003914
3915 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003916 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02003917 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003918 for (i = co->co_argcount; i < total_args; i++) {
3919 PyObject *name;
3920 if (GETLOCAL(i) != NULL)
3921 continue;
3922 name = PyTuple_GET_ITEM(co->co_varnames, i);
3923 if (kwdefs != NULL) {
3924 PyObject *def = PyDict_GetItem(kwdefs, name);
3925 if (def) {
3926 Py_INCREF(def);
3927 SETLOCAL(i, def);
3928 continue;
3929 }
3930 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003931 missing++;
3932 }
3933 if (missing) {
3934 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003935 goto fail;
3936 }
3937 }
3938
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003939 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003940 vars into frame. */
3941 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003942 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02003943 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05003944 /* Possibly account for the cell variable being an argument. */
3945 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07003946 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05003947 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05003948 /* Clear the local copy. */
3949 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003950 }
3951 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05003952 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003953 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05003954 if (c == NULL)
3955 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05003956 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003957 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003958
3959 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05003960 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3961 PyObject *o = PyTuple_GET_ITEM(closure, i);
3962 Py_INCREF(o);
3963 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003964 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003965
Yury Selivanoveb636452016-09-08 22:01:51 -07003966 /* Handle generator/coroutine/asynchronous generator */
3967 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003968 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04003969 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003970 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04003971
3972 if (is_coro && tstate->in_coroutine_wrapper) {
3973 assert(coro_wrapper != NULL);
3974 PyErr_Format(PyExc_RuntimeError,
3975 "coroutine wrapper %.200R attempted "
3976 "to recursively wrap %.200R",
3977 coro_wrapper,
3978 co);
3979 goto fail;
3980 }
Yury Selivanov75445082015-05-11 22:57:16 -04003981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003982 /* Don't need to keep the reference to f_back, it will be set
3983 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003984 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003985
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003986 /* Create a new generator that owns the ready to run frame
3987 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04003988 if (is_coro) {
3989 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07003990 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
3991 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003992 } else {
3993 gen = PyGen_NewWithQualName(f, name, qualname);
3994 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003995 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003996 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003997 }
INADA Naoki9c157762016-12-26 18:52:46 +09003998
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003999 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004000
Yury Selivanov94c22632015-06-04 10:16:51 -04004001 if (is_coro && coro_wrapper != NULL) {
4002 PyObject *wrapped;
4003 tstate->in_coroutine_wrapper = 1;
4004 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
4005 tstate->in_coroutine_wrapper = 0;
4006 return wrapped;
4007 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04004008
Yury Selivanov75445082015-05-11 22:57:16 -04004009 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004010 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004011
Victor Stinner59a73272016-12-09 18:51:13 +01004012 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004013
Thomas Woutersce272b62007-09-19 21:19:28 +00004014fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004016 /* decref'ing the frame can cause __del__ methods to get invoked,
4017 which can call back into Python. While we're done with the
4018 current Python frame (f), the associated C stack is still in use,
4019 so recursion_depth must be boosted for the duration.
4020 */
4021 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09004022 if (Py_REFCNT(f) > 1) {
4023 Py_DECREF(f);
4024 _PyObject_GC_TRACK(f);
4025 }
4026 else {
4027 ++tstate->recursion_depth;
4028 Py_DECREF(f);
4029 --tstate->recursion_depth;
4030 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004031 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004032}
4033
Victor Stinner40ee3012014-06-16 15:59:28 +02004034PyObject *
4035PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004036 PyObject *const *args, int argcount,
4037 PyObject *const *kws, int kwcount,
4038 PyObject *const *defs, int defcount,
4039 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004040{
4041 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004042 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004043 kws, kws != NULL ? kws + 1 : NULL,
4044 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004045 defs, defcount,
4046 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004047 NULL, NULL);
4048}
Tim Peters5ca576e2001-06-18 22:08:13 +00004049
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004050static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05004051special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004052{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004053 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004054 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004055 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05004056 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004057 return NULL;
4058 }
4059 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004060}
4061
4062
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004063/* Logic for the raise statement (too complicated for inlining).
4064 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004065static int
Collin Winter828f04a2007-08-31 00:04:24 +00004066do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004067{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004068 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004070 if (exc == NULL) {
4071 /* Reraise */
Victor Stinner50b48572018-11-01 01:51:40 +01004072 PyThreadState *tstate = _PyThreadState_GET();
Mark Shannonae3087c2017-10-22 22:41:51 +01004073 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004074 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004075 type = exc_info->exc_type;
4076 value = exc_info->exc_value;
4077 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004078 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004079 PyErr_SetString(PyExc_RuntimeError,
4080 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004081 return 0;
4082 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004083 Py_XINCREF(type);
4084 Py_XINCREF(value);
4085 Py_XINCREF(tb);
4086 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004087 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004088 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004090 /* We support the following forms of raise:
4091 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004092 raise <instance>
4093 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004094
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004095 if (PyExceptionClass_Check(exc)) {
4096 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004097 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004098 if (value == NULL)
4099 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004100 if (!PyExceptionInstance_Check(value)) {
4101 PyErr_Format(PyExc_TypeError,
4102 "calling %R should have returned an instance of "
4103 "BaseException, not %R",
4104 type, Py_TYPE(value));
4105 goto raise_error;
4106 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004107 }
4108 else if (PyExceptionInstance_Check(exc)) {
4109 value = exc;
4110 type = PyExceptionInstance_Class(exc);
4111 Py_INCREF(type);
4112 }
4113 else {
4114 /* Not something you can raise. You get an exception
4115 anyway, just not what you specified :-) */
4116 Py_DECREF(exc);
4117 PyErr_SetString(PyExc_TypeError,
4118 "exceptions must derive from BaseException");
4119 goto raise_error;
4120 }
Collin Winter828f04a2007-08-31 00:04:24 +00004121
Serhiy Storchakac0191582016-09-27 11:37:10 +03004122 assert(type != NULL);
4123 assert(value != NULL);
4124
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004125 if (cause) {
4126 PyObject *fixed_cause;
4127 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004128 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004129 if (fixed_cause == NULL)
4130 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004131 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004132 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004133 else if (PyExceptionInstance_Check(cause)) {
4134 fixed_cause = cause;
4135 }
4136 else if (cause == Py_None) {
4137 Py_DECREF(cause);
4138 fixed_cause = NULL;
4139 }
4140 else {
4141 PyErr_SetString(PyExc_TypeError,
4142 "exception causes must derive from "
4143 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004144 goto raise_error;
4145 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004146 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004147 }
Collin Winter828f04a2007-08-31 00:04:24 +00004148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004149 PyErr_SetObject(type, value);
4150 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004151 Py_DECREF(value);
4152 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004153 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004154
4155raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004156 Py_XDECREF(value);
4157 Py_XDECREF(type);
4158 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004159 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004160}
4161
Tim Petersd6d010b2001-06-21 02:49:55 +00004162/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004163 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004164
Guido van Rossum0368b722007-05-11 16:50:42 +00004165 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4166 with a variable target.
4167*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004168
Barry Warsawe42b18f1997-08-25 22:13:04 +00004169static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004170unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004171{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004172 int i = 0, j = 0;
4173 Py_ssize_t ll = 0;
4174 PyObject *it; /* iter(v) */
4175 PyObject *w;
4176 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004178 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004179
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004180 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004181 if (it == NULL) {
4182 if (PyErr_ExceptionMatches(PyExc_TypeError) &&
4183 v->ob_type->tp_iter == NULL && !PySequence_Check(v))
4184 {
4185 PyErr_Format(PyExc_TypeError,
4186 "cannot unpack non-iterable %.200s object",
4187 v->ob_type->tp_name);
4188 }
4189 return 0;
4190 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004192 for (; i < argcnt; i++) {
4193 w = PyIter_Next(it);
4194 if (w == NULL) {
4195 /* Iterator done, via error or exhaustion. */
4196 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004197 if (argcntafter == -1) {
4198 PyErr_Format(PyExc_ValueError,
4199 "not enough values to unpack (expected %d, got %d)",
4200 argcnt, i);
4201 }
4202 else {
4203 PyErr_Format(PyExc_ValueError,
4204 "not enough values to unpack "
4205 "(expected at least %d, got %d)",
4206 argcnt + argcntafter, i);
4207 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004208 }
4209 goto Error;
4210 }
4211 *--sp = w;
4212 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004214 if (argcntafter == -1) {
4215 /* We better have exhausted the iterator now. */
4216 w = PyIter_Next(it);
4217 if (w == NULL) {
4218 if (PyErr_Occurred())
4219 goto Error;
4220 Py_DECREF(it);
4221 return 1;
4222 }
4223 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004224 PyErr_Format(PyExc_ValueError,
4225 "too many values to unpack (expected %d)",
4226 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004227 goto Error;
4228 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004230 l = PySequence_List(it);
4231 if (l == NULL)
4232 goto Error;
4233 *--sp = l;
4234 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004235
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004236 ll = PyList_GET_SIZE(l);
4237 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004238 PyErr_Format(PyExc_ValueError,
4239 "not enough values to unpack (expected at least %d, got %zd)",
4240 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004241 goto Error;
4242 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004244 /* Pop the "after-variable" args off the list. */
4245 for (j = argcntafter; j > 0; j--, i++) {
4246 *--sp = PyList_GET_ITEM(l, ll - j);
4247 }
4248 /* Resize the list. */
4249 Py_SIZE(l) = ll - argcntafter;
4250 Py_DECREF(it);
4251 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004252
Tim Petersd6d010b2001-06-21 02:49:55 +00004253Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004254 for (; i > 0; i--, sp++)
4255 Py_DECREF(*sp);
4256 Py_XDECREF(it);
4257 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004258}
4259
4260
Guido van Rossum96a42c81992-01-12 02:29:51 +00004261#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004262static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004263prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004264{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004265 printf("%s ", str);
4266 if (PyObject_Print(v, stdout, 0) != 0)
4267 PyErr_Clear(); /* Don't know what else to do */
4268 printf("\n");
4269 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004270}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004271#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004272
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004273static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004274call_exc_trace(Py_tracefunc func, PyObject *self,
4275 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004276{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004277 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004278 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004279 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004280 if (value == NULL) {
4281 value = Py_None;
4282 Py_INCREF(value);
4283 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004284 PyErr_NormalizeException(&type, &value, &orig_traceback);
4285 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004286 arg = PyTuple_Pack(3, type, value, traceback);
4287 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004288 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004289 return;
4290 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004291 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004292 Py_DECREF(arg);
4293 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004294 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004295 else {
4296 Py_XDECREF(type);
4297 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004298 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004299 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004300}
4301
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004302static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004303call_trace_protected(Py_tracefunc func, PyObject *obj,
4304 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004305 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004306{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004307 PyObject *type, *value, *traceback;
4308 int err;
4309 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004310 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004311 if (err == 0)
4312 {
4313 PyErr_Restore(type, value, traceback);
4314 return 0;
4315 }
4316 else {
4317 Py_XDECREF(type);
4318 Py_XDECREF(value);
4319 Py_XDECREF(traceback);
4320 return -1;
4321 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004322}
4323
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004324static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004325call_trace(Py_tracefunc func, PyObject *obj,
4326 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004327 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004328{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004329 int result;
4330 if (tstate->tracing)
4331 return 0;
4332 tstate->tracing++;
4333 tstate->use_tracing = 0;
4334 result = func(obj, frame, what, arg);
4335 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4336 || (tstate->c_profilefunc != NULL));
4337 tstate->tracing--;
4338 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004339}
4340
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004341PyObject *
4342_PyEval_CallTracing(PyObject *func, PyObject *args)
4343{
Victor Stinner50b48572018-11-01 01:51:40 +01004344 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004345 int save_tracing = tstate->tracing;
4346 int save_use_tracing = tstate->use_tracing;
4347 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004348
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004349 tstate->tracing = 0;
4350 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4351 || (tstate->c_profilefunc != NULL));
4352 result = PyObject_Call(func, args, NULL);
4353 tstate->tracing = save_tracing;
4354 tstate->use_tracing = save_use_tracing;
4355 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004356}
4357
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004358/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004359static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004360maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004361 PyThreadState *tstate, PyFrameObject *frame,
4362 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004363{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004364 int result = 0;
4365 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004367 /* If the last instruction executed isn't in the current
4368 instruction window, reset the window.
4369 */
4370 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4371 PyAddrPair bounds;
4372 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4373 &bounds);
4374 *instr_lb = bounds.ap_lower;
4375 *instr_ub = bounds.ap_upper;
4376 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004377 /* If the last instruction falls at the start of a line or if it
4378 represents a jump backwards, update the frame's line number and
4379 then call the trace function if we're tracing source lines.
4380 */
4381 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004382 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004383 if (frame->f_trace_lines) {
4384 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4385 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004386 }
George King20faa682017-10-18 17:44:22 -07004387 /* Always emit an opcode event if we're tracing all opcodes. */
4388 if (frame->f_trace_opcodes) {
4389 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4390 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004391 *instr_prev = frame->f_lasti;
4392 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004393}
4394
Fred Drake5755ce62001-06-27 19:19:46 +00004395void
4396PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004397{
Victor Stinner50b48572018-11-01 01:51:40 +01004398 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004399 PyObject *temp = tstate->c_profileobj;
4400 Py_XINCREF(arg);
4401 tstate->c_profilefunc = NULL;
4402 tstate->c_profileobj = NULL;
4403 /* Must make sure that tracing is not ignored if 'temp' is freed */
4404 tstate->use_tracing = tstate->c_tracefunc != NULL;
4405 Py_XDECREF(temp);
4406 tstate->c_profilefunc = func;
4407 tstate->c_profileobj = arg;
4408 /* Flag that tracing or profiling is turned on */
4409 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004410}
4411
4412void
4413PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4414{
Victor Stinner50b48572018-11-01 01:51:40 +01004415 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004416 PyObject *temp = tstate->c_traceobj;
4417 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4418 Py_XINCREF(arg);
4419 tstate->c_tracefunc = NULL;
4420 tstate->c_traceobj = NULL;
4421 /* Must make sure that profiling is not ignored if 'temp' is freed */
4422 tstate->use_tracing = tstate->c_profilefunc != NULL;
4423 Py_XDECREF(temp);
4424 tstate->c_tracefunc = func;
4425 tstate->c_traceobj = arg;
4426 /* Flag that tracing or profiling is turned on */
4427 tstate->use_tracing = ((func != NULL)
4428 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004429}
4430
Yury Selivanov75445082015-05-11 22:57:16 -04004431void
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004432_PyEval_SetCoroutineOriginTrackingDepth(int new_depth)
4433{
4434 assert(new_depth >= 0);
Victor Stinner50b48572018-11-01 01:51:40 +01004435 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004436 tstate->coroutine_origin_tracking_depth = new_depth;
4437}
4438
4439int
4440_PyEval_GetCoroutineOriginTrackingDepth(void)
4441{
Victor Stinner50b48572018-11-01 01:51:40 +01004442 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004443 return tstate->coroutine_origin_tracking_depth;
4444}
4445
4446void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004447_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004448{
Victor Stinner50b48572018-11-01 01:51:40 +01004449 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanov75445082015-05-11 22:57:16 -04004450
Yury Selivanov75445082015-05-11 22:57:16 -04004451 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004452 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004453}
4454
4455PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004456_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004457{
Victor Stinner50b48572018-11-01 01:51:40 +01004458 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanov75445082015-05-11 22:57:16 -04004459 return tstate->coroutine_wrapper;
4460}
4461
Yury Selivanoveb636452016-09-08 22:01:51 -07004462void
4463_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4464{
Victor Stinner50b48572018-11-01 01:51:40 +01004465 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004466
4467 Py_XINCREF(firstiter);
4468 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4469}
4470
4471PyObject *
4472_PyEval_GetAsyncGenFirstiter(void)
4473{
Victor Stinner50b48572018-11-01 01:51:40 +01004474 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004475 return tstate->async_gen_firstiter;
4476}
4477
4478void
4479_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4480{
Victor Stinner50b48572018-11-01 01:51:40 +01004481 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004482
4483 Py_XINCREF(finalizer);
4484 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4485}
4486
4487PyObject *
4488_PyEval_GetAsyncGenFinalizer(void)
4489{
Victor Stinner50b48572018-11-01 01:51:40 +01004490 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004491 return tstate->async_gen_finalizer;
4492}
4493
Guido van Rossumb209a111997-04-29 18:18:01 +00004494PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004495PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004496{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004497 PyFrameObject *current_frame = PyEval_GetFrame();
4498 if (current_frame == NULL)
Victor Stinnercaba55b2018-08-03 15:33:52 +02004499 return _PyInterpreterState_GET_UNSAFE()->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004500 else
4501 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004502}
4503
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004504/* Convenience function to get a builtin from its name */
4505PyObject *
4506_PyEval_GetBuiltinId(_Py_Identifier *name)
4507{
4508 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4509 if (attr) {
4510 Py_INCREF(attr);
4511 }
4512 else if (!PyErr_Occurred()) {
4513 PyErr_SetObject(PyExc_AttributeError, _PyUnicode_FromId(name));
4514 }
4515 return attr;
4516}
4517
Guido van Rossumb209a111997-04-29 18:18:01 +00004518PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004519PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004520{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004521 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004522 if (current_frame == NULL) {
4523 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004524 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004525 }
4526
4527 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4528 return NULL;
4529
4530 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004531 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004532}
4533
Guido van Rossumb209a111997-04-29 18:18:01 +00004534PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004535PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004536{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004537 PyFrameObject *current_frame = PyEval_GetFrame();
4538 if (current_frame == NULL)
4539 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004540
4541 assert(current_frame->f_globals != NULL);
4542 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004543}
4544
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004545PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004546PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004547{
Victor Stinner50b48572018-11-01 01:51:40 +01004548 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004549 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004550}
4551
Guido van Rossum6135a871995-01-09 17:53:26 +00004552int
Tim Peters5ba58662001-07-16 02:29:45 +00004553PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004554{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004555 PyFrameObject *current_frame = PyEval_GetFrame();
4556 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004558 if (current_frame != NULL) {
4559 const int codeflags = current_frame->f_code->co_flags;
4560 const int compilerflags = codeflags & PyCF_MASK;
4561 if (compilerflags) {
4562 result = 1;
4563 cf->cf_flags |= compilerflags;
4564 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004565#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004566 if (codeflags & CO_GENERATOR_ALLOWED) {
4567 result = 1;
4568 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4569 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004570#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004571 }
4572 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004573}
4574
Guido van Rossum3f5da241990-12-20 15:06:42 +00004575
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004576const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004577PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004578{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004579 if (PyMethod_Check(func))
4580 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4581 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004582 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004583 else if (PyCFunction_Check(func))
4584 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4585 else
4586 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004587}
4588
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004589const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004590PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004591{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004592 if (PyMethod_Check(func))
4593 return "()";
4594 else if (PyFunction_Check(func))
4595 return "()";
4596 else if (PyCFunction_Check(func))
4597 return "()";
4598 else
4599 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004600}
4601
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004602#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004603if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004604 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4605 tstate, tstate->frame, \
4606 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004607 x = NULL; \
4608 } \
4609 else { \
4610 x = call; \
4611 if (tstate->c_profilefunc != NULL) { \
4612 if (x == NULL) { \
4613 call_trace_protected(tstate->c_profilefunc, \
4614 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004615 tstate, tstate->frame, \
4616 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004617 /* XXX should pass (type, value, tb) */ \
4618 } else { \
4619 if (call_trace(tstate->c_profilefunc, \
4620 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004621 tstate, tstate->frame, \
4622 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004623 Py_DECREF(x); \
4624 x = NULL; \
4625 } \
4626 } \
4627 } \
4628 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004629} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004630 x = call; \
4631 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004632
Victor Stinner415c5102017-01-11 00:54:57 +01004633/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4634 to reduce the stack consumption. */
4635Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Benjamin Peterson4fd64b92016-09-09 14:57:58 -07004636call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004637{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004638 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004639 PyObject *func = *pfunc;
4640 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004641 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4642 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004643 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004645 /* Always dispatch PyCFunction first, because these are
4646 presumed to be the most frequent callable object.
4647 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004648 if (PyCFunction_Check(func)) {
Victor Stinner50b48572018-11-01 01:51:40 +01004649 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004650 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004651 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004652 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
Victor Stinner50b48572018-11-01 01:51:40 +01004653 PyThreadState *tstate = _PyThreadState_GET();
jdemeyer56868f92018-07-21 10:30:59 +02004654 if (nargs > 0 && tstate->use_tracing) {
4655 /* We need to create a temporary bound method as argument
4656 for profiling.
4657
4658 If nargs == 0, then this cannot work because we have no
4659 "self". In any case, the call itself would raise
4660 TypeError (foo needs an argument), so we just skip
4661 profiling. */
4662 PyObject *self = stack[0];
4663 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
jdemeyer147d9552018-07-23 18:41:20 +02004664 if (func != NULL) {
4665 C_TRACE(x, _PyCFunction_FastCallKeywords(func,
4666 stack+1, nargs-1,
4667 kwnames));
4668 Py_DECREF(func);
INADA Naoki93fac8d2017-03-07 14:24:37 +09004669 }
jdemeyer147d9552018-07-23 18:41:20 +02004670 else {
4671 x = NULL;
4672 }
INADA Naoki93fac8d2017-03-07 14:24:37 +09004673 }
4674 else {
4675 x = _PyMethodDescr_FastCallKeywords(func, stack, nargs, kwnames);
4676 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004677 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004678 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004679 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
Victor Stinnerb69ee8c2016-11-28 18:32:31 +01004680 /* Optimize access to bound methods. Reuse the Python stack
4681 to pass 'self' as the first argument, replace 'func'
4682 with 'self'. It avoids the creation of a new temporary tuple
4683 for arguments (to replace func with self) when the method uses
4684 FASTCALL. */
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004685 PyObject *self = PyMethod_GET_SELF(func);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004686 Py_INCREF(self);
4687 func = PyMethod_GET_FUNCTION(func);
4688 Py_INCREF(func);
4689 Py_SETREF(*pfunc, self);
4690 nargs++;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004691 stack--;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004692 }
4693 else {
4694 Py_INCREF(func);
4695 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004696
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004697 if (PyFunction_Check(func)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004698 x = _PyFunction_FastCallKeywords(func, stack, nargs, kwnames);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004699 }
4700 else {
4701 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4702 }
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004703 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004704 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004705
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004706 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4707
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004708 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004709 while ((*pp_stack) > pfunc) {
4710 w = EXT_POP(*pp_stack);
4711 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004712 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004714 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004715}
4716
Jeremy Hylton52820442001-01-03 23:52:36 +00004717static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004718do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004719{
jdemeyere89de732018-09-19 12:06:20 +02004720 PyObject *result;
4721
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004722 if (PyCFunction_Check(func)) {
Victor Stinner50b48572018-11-01 01:51:40 +01004723 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004724 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004725 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004726 }
jdemeyere89de732018-09-19 12:06:20 +02004727 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
Victor Stinner50b48572018-11-01 01:51:40 +01004728 PyThreadState *tstate = _PyThreadState_GET();
jdemeyere89de732018-09-19 12:06:20 +02004729 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
4730 if (nargs > 0 && tstate->use_tracing) {
4731 /* We need to create a temporary bound method as argument
4732 for profiling.
4733
4734 If nargs == 0, then this cannot work because we have no
4735 "self". In any case, the call itself would raise
4736 TypeError (foo needs an argument), so we just skip
4737 profiling. */
4738 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
4739 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4740 if (func == NULL) {
4741 return NULL;
4742 }
4743
4744 C_TRACE(result, _PyCFunction_FastCallDict(func,
Victor Stinnerd17a6932018-11-09 16:56:48 +01004745 &_PyTuple_ITEMS(callargs)[1],
jdemeyere89de732018-09-19 12:06:20 +02004746 nargs - 1,
4747 kwdict));
4748 Py_DECREF(func);
4749 return result;
4750 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004751 }
jdemeyere89de732018-09-19 12:06:20 +02004752 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00004753}
4754
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004755/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004756 nb_index slot defined, and store in *pi.
4757 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004758 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004759 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004760*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004761int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004762_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004763{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004764 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004765 Py_ssize_t x;
4766 if (PyIndex_Check(v)) {
4767 x = PyNumber_AsSsize_t(v, NULL);
4768 if (x == -1 && PyErr_Occurred())
4769 return 0;
4770 }
4771 else {
4772 PyErr_SetString(PyExc_TypeError,
4773 "slice indices must be integers or "
4774 "None or have an __index__ method");
4775 return 0;
4776 }
4777 *pi = x;
4778 }
4779 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004780}
4781
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004782int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004783_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004784{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004785 Py_ssize_t x;
4786 if (PyIndex_Check(v)) {
4787 x = PyNumber_AsSsize_t(v, NULL);
4788 if (x == -1 && PyErr_Occurred())
4789 return 0;
4790 }
4791 else {
4792 PyErr_SetString(PyExc_TypeError,
4793 "slice indices must be integers or "
4794 "have an __index__ method");
4795 return 0;
4796 }
4797 *pi = x;
4798 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004799}
4800
4801
Guido van Rossum486364b2007-06-30 05:01:58 +00004802#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004803 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004804
Guido van Rossumb209a111997-04-29 18:18:01 +00004805static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004806cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004807{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004808 int res = 0;
4809 switch (op) {
4810 case PyCmp_IS:
4811 res = (v == w);
4812 break;
4813 case PyCmp_IS_NOT:
4814 res = (v != w);
4815 break;
4816 case PyCmp_IN:
4817 res = PySequence_Contains(w, v);
4818 if (res < 0)
4819 return NULL;
4820 break;
4821 case PyCmp_NOT_IN:
4822 res = PySequence_Contains(w, v);
4823 if (res < 0)
4824 return NULL;
4825 res = !res;
4826 break;
4827 case PyCmp_EXC_MATCH:
4828 if (PyTuple_Check(w)) {
4829 Py_ssize_t i, length;
4830 length = PyTuple_Size(w);
4831 for (i = 0; i < length; i += 1) {
4832 PyObject *exc = PyTuple_GET_ITEM(w, i);
4833 if (!PyExceptionClass_Check(exc)) {
4834 PyErr_SetString(PyExc_TypeError,
4835 CANNOT_CATCH_MSG);
4836 return NULL;
4837 }
4838 }
4839 }
4840 else {
4841 if (!PyExceptionClass_Check(w)) {
4842 PyErr_SetString(PyExc_TypeError,
4843 CANNOT_CATCH_MSG);
4844 return NULL;
4845 }
4846 }
4847 res = PyErr_GivenExceptionMatches(v, w);
4848 break;
4849 default:
4850 return PyObject_RichCompare(v, w, op);
4851 }
4852 v = res ? Py_True : Py_False;
4853 Py_INCREF(v);
4854 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004855}
4856
Thomas Wouters52152252000-08-17 22:55:00 +00004857static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004858import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
4859{
4860 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004861 PyObject *import_func, *res;
4862 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004863
4864 import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
4865 if (import_func == NULL) {
4866 PyErr_SetString(PyExc_ImportError, "__import__ not found");
4867 return NULL;
4868 }
4869
4870 /* Fast path for not overloaded __import__. */
Victor Stinnercaba55b2018-08-03 15:33:52 +02004871 if (import_func == _PyInterpreterState_GET_UNSAFE()->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004872 int ilevel = _PyLong_AsInt(level);
4873 if (ilevel == -1 && PyErr_Occurred()) {
4874 return NULL;
4875 }
4876 res = PyImport_ImportModuleLevelObject(
4877 name,
4878 f->f_globals,
4879 f->f_locals == NULL ? Py_None : f->f_locals,
4880 fromlist,
4881 ilevel);
4882 return res;
4883 }
4884
4885 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004886
4887 stack[0] = name;
4888 stack[1] = f->f_globals;
4889 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
4890 stack[3] = fromlist;
4891 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02004892 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004893 Py_DECREF(import_func);
4894 return res;
4895}
4896
4897static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004898import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004899{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004900 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02004901 _Py_IDENTIFIER(__name__);
Xiang Zhang4830f582017-03-21 11:13:42 +08004902 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004903
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004904 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02004905 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004906 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004907 /* Issue #17636: in case this failed because of a circular relative
4908 import, try to fallback on reading the module directly from
4909 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02004910 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07004911 if (pkgname == NULL) {
4912 goto error;
4913 }
Oren Milman6db70332017-09-19 14:23:01 +03004914 if (!PyUnicode_Check(pkgname)) {
4915 Py_CLEAR(pkgname);
4916 goto error;
4917 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004918 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07004919 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08004920 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004921 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07004922 }
Eric Snow3f9eee62017-09-15 16:35:20 -06004923 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004924 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07004925 if (x == NULL) {
4926 goto error;
4927 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004928 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004929 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07004930 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004931 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004932 if (pkgname == NULL) {
4933 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
4934 if (pkgname_or_unknown == NULL) {
4935 Py_XDECREF(pkgpath);
4936 return NULL;
4937 }
4938 } else {
4939 pkgname_or_unknown = pkgname;
4940 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004941
4942 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
4943 PyErr_Clear();
Xiang Zhang4830f582017-03-21 11:13:42 +08004944 errmsg = PyUnicode_FromFormat(
4945 "cannot import name %R from %R (unknown location)",
4946 name, pkgname_or_unknown
4947 );
4948 /* NULL check for errmsg done by PyErr_SetImportError. */
4949 PyErr_SetImportError(errmsg, pkgname, NULL);
4950 }
4951 else {
4952 errmsg = PyUnicode_FromFormat(
4953 "cannot import name %R from %R (%S)",
4954 name, pkgname_or_unknown, pkgpath
4955 );
4956 /* NULL check for errmsg done by PyErr_SetImportError. */
4957 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004958 }
4959
Xiang Zhang4830f582017-03-21 11:13:42 +08004960 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004961 Py_XDECREF(pkgname_or_unknown);
4962 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07004963 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00004964}
Guido van Rossumac7be682001-01-17 15:42:30 +00004965
Thomas Wouters52152252000-08-17 22:55:00 +00004966static int
4967import_all_from(PyObject *locals, PyObject *v)
4968{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004969 _Py_IDENTIFIER(__all__);
4970 _Py_IDENTIFIER(__dict__);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08004971 _Py_IDENTIFIER(__name__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004972 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004973 int skip_leading_underscores = 0;
4974 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004975
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004976 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
4977 return -1; /* Unexpected error */
4978 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004979 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004980 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
4981 return -1;
4982 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004983 if (dict == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004984 PyErr_SetString(PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004985 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004986 return -1;
4987 }
4988 all = PyMapping_Keys(dict);
4989 Py_DECREF(dict);
4990 if (all == NULL)
4991 return -1;
4992 skip_leading_underscores = 1;
4993 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004995 for (pos = 0, err = 0; ; pos++) {
4996 name = PySequence_GetItem(all, pos);
4997 if (name == NULL) {
4998 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4999 err = -1;
5000 else
5001 PyErr_Clear();
5002 break;
5003 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005004 if (!PyUnicode_Check(name)) {
5005 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5006 if (modname == NULL) {
5007 Py_DECREF(name);
5008 err = -1;
5009 break;
5010 }
5011 if (!PyUnicode_Check(modname)) {
5012 PyErr_Format(PyExc_TypeError,
5013 "module __name__ must be a string, not %.100s",
5014 Py_TYPE(modname)->tp_name);
5015 }
5016 else {
5017 PyErr_Format(PyExc_TypeError,
5018 "%s in %U.%s must be str, not %.100s",
5019 skip_leading_underscores ? "Key" : "Item",
5020 modname,
5021 skip_leading_underscores ? "__dict__" : "__all__",
5022 Py_TYPE(name)->tp_name);
5023 }
5024 Py_DECREF(modname);
5025 Py_DECREF(name);
5026 err = -1;
5027 break;
5028 }
5029 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005030 if (PyUnicode_READY(name) == -1) {
5031 Py_DECREF(name);
5032 err = -1;
5033 break;
5034 }
5035 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5036 Py_DECREF(name);
5037 continue;
5038 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005039 }
5040 value = PyObject_GetAttr(v, name);
5041 if (value == NULL)
5042 err = -1;
5043 else if (PyDict_CheckExact(locals))
5044 err = PyDict_SetItem(locals, name, value);
5045 else
5046 err = PyObject_SetItem(locals, name, value);
5047 Py_DECREF(name);
5048 Py_XDECREF(value);
5049 if (err != 0)
5050 break;
5051 }
5052 Py_DECREF(all);
5053 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005054}
5055
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005056static int
5057check_args_iterable(PyObject *func, PyObject *args)
5058{
5059 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
5060 PyErr_Format(PyExc_TypeError,
5061 "%.200s%.200s argument after * "
5062 "must be an iterable, not %.200s",
5063 PyEval_GetFuncName(func),
5064 PyEval_GetFuncDesc(func),
5065 args->ob_type->tp_name);
5066 return -1;
5067 }
5068 return 0;
5069}
5070
5071static void
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005072format_kwargs_error(PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005073{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005074 /* _PyDict_MergeEx raises attribute
5075 * error (percolated from an attempt
5076 * to get 'keys' attribute) instead of
5077 * a type error if its second argument
5078 * is not a mapping.
5079 */
5080 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
5081 PyErr_Format(PyExc_TypeError,
5082 "%.200s%.200s argument after ** "
5083 "must be a mapping, not %.200s",
5084 PyEval_GetFuncName(func),
5085 PyEval_GetFuncDesc(func),
5086 kwargs->ob_type->tp_name);
5087 }
5088 else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
5089 PyObject *exc, *val, *tb;
5090 PyErr_Fetch(&exc, &val, &tb);
5091 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
5092 PyObject *key = PyTuple_GET_ITEM(val, 0);
5093 if (!PyUnicode_Check(key)) {
5094 PyErr_Format(PyExc_TypeError,
5095 "%.200s%.200s keywords must be strings",
5096 PyEval_GetFuncName(func),
5097 PyEval_GetFuncDesc(func));
5098 } else {
5099 PyErr_Format(PyExc_TypeError,
5100 "%.200s%.200s got multiple "
5101 "values for keyword argument '%U'",
5102 PyEval_GetFuncName(func),
5103 PyEval_GetFuncDesc(func),
5104 key);
5105 }
5106 Py_XDECREF(exc);
5107 Py_XDECREF(val);
5108 Py_XDECREF(tb);
5109 }
5110 else {
5111 PyErr_Restore(exc, val, tb);
5112 }
5113 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005114}
5115
Guido van Rossumac7be682001-01-17 15:42:30 +00005116static void
Neal Norwitzda059e32007-08-26 05:33:45 +00005117format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005118{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005119 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005121 if (!obj)
5122 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005123
Serhiy Storchaka06515832016-11-20 09:13:07 +02005124 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005125 if (!obj_str)
5126 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005127
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005128 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005129}
Guido van Rossum950361c1997-01-24 13:49:28 +00005130
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005131static void
5132format_exc_unbound(PyCodeObject *co, int oparg)
5133{
5134 PyObject *name;
5135 /* Don't stomp existing exception */
5136 if (PyErr_Occurred())
5137 return;
5138 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5139 name = PyTuple_GET_ITEM(co->co_cellvars,
5140 oparg);
5141 format_exc_check_arg(
5142 PyExc_UnboundLocalError,
5143 UNBOUNDLOCAL_ERROR_MSG,
5144 name);
5145 } else {
5146 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5147 PyTuple_GET_SIZE(co->co_cellvars));
5148 format_exc_check_arg(PyExc_NameError,
5149 UNBOUNDFREE_ERROR_MSG, name);
5150 }
5151}
5152
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005153static void
5154format_awaitable_error(PyTypeObject *type, int prevopcode)
5155{
5156 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5157 if (prevopcode == BEFORE_ASYNC_WITH) {
5158 PyErr_Format(PyExc_TypeError,
5159 "'async with' received an object from __aenter__ "
5160 "that does not implement __await__: %.100s",
5161 type->tp_name);
5162 }
5163 else if (prevopcode == WITH_CLEANUP_START) {
5164 PyErr_Format(PyExc_TypeError,
5165 "'async with' received an object from __aexit__ "
5166 "that does not implement __await__: %.100s",
5167 type->tp_name);
5168 }
5169 }
5170}
5171
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005172static PyObject *
5173unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005174 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005175{
5176 PyObject *res;
5177 if (Py_REFCNT(v) == 2) {
5178 /* In the common case, there are 2 references to the value
5179 * stored in 'variable' when the += is performed: one on the
5180 * value stack (in 'v') and one still stored in the
5181 * 'variable'. We try to delete the variable now to reduce
5182 * the refcnt to 1.
5183 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005184 int opcode, oparg;
5185 NEXTOPARG();
5186 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005187 case STORE_FAST:
5188 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005189 PyObject **fastlocals = f->f_localsplus;
5190 if (GETLOCAL(oparg) == v)
5191 SETLOCAL(oparg, NULL);
5192 break;
5193 }
5194 case STORE_DEREF:
5195 {
5196 PyObject **freevars = (f->f_localsplus +
5197 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005198 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005199 if (PyCell_GET(c) == v) {
5200 PyCell_SET(c, NULL);
5201 Py_DECREF(v);
5202 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005203 break;
5204 }
5205 case STORE_NAME:
5206 {
5207 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005208 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005209 PyObject *locals = f->f_locals;
Serhiy Storchaka8905fcc2018-12-11 08:38:03 +02005210 if (locals && PyDict_CheckExact(locals) &&
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005211 PyDict_GetItem(locals, name) == v) {
5212 if (PyDict_DelItem(locals, name) != 0) {
5213 PyErr_Clear();
5214 }
5215 }
5216 break;
5217 }
5218 }
5219 }
5220 res = v;
5221 PyUnicode_Append(&res, w);
5222 return res;
5223}
5224
Guido van Rossum950361c1997-01-24 13:49:28 +00005225#ifdef DYNAMIC_EXECUTION_PROFILE
5226
Skip Montanarof118cb12001-10-15 20:51:38 +00005227static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005228getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005229{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005230 int i;
5231 PyObject *l = PyList_New(256);
5232 if (l == NULL) return NULL;
5233 for (i = 0; i < 256; i++) {
5234 PyObject *x = PyLong_FromLong(a[i]);
5235 if (x == NULL) {
5236 Py_DECREF(l);
5237 return NULL;
5238 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005239 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005240 }
5241 for (i = 0; i < 256; i++)
5242 a[i] = 0;
5243 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005244}
5245
5246PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005247_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005248{
5249#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005250 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005251#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005252 int i;
5253 PyObject *l = PyList_New(257);
5254 if (l == NULL) return NULL;
5255 for (i = 0; i < 257; i++) {
5256 PyObject *x = getarray(dxpairs[i]);
5257 if (x == NULL) {
5258 Py_DECREF(l);
5259 return NULL;
5260 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005261 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005262 }
5263 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005264#endif
5265}
5266
5267#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005268
5269Py_ssize_t
5270_PyEval_RequestCodeExtraIndex(freefunc free)
5271{
Victor Stinnercaba55b2018-08-03 15:33:52 +02005272 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -07005273 Py_ssize_t new_index;
5274
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005275 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005276 return -1;
5277 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005278 new_index = interp->co_extra_user_count++;
5279 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005280 return new_index;
5281}
Łukasz Langaa785c872016-09-09 17:37:37 -07005282
5283static void
5284dtrace_function_entry(PyFrameObject *f)
5285{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005286 const char *filename;
5287 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005288 int lineno;
5289
5290 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5291 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5292 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5293
5294 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5295}
5296
5297static void
5298dtrace_function_return(PyFrameObject *f)
5299{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005300 const char *filename;
5301 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005302 int lineno;
5303
5304 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5305 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5306 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5307
5308 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5309}
5310
5311/* DTrace equivalent of maybe_call_line_trace. */
5312static void
5313maybe_dtrace_line(PyFrameObject *frame,
5314 int *instr_lb, int *instr_ub, int *instr_prev)
5315{
5316 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005317 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005318
5319 /* If the last instruction executed isn't in the current
5320 instruction window, reset the window.
5321 */
5322 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5323 PyAddrPair bounds;
5324 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5325 &bounds);
5326 *instr_lb = bounds.ap_lower;
5327 *instr_ub = bounds.ap_upper;
5328 }
5329 /* If the last instruction falls at the start of a line or if
5330 it represents a jump backwards, update the frame's line
5331 number and call the trace function. */
5332 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5333 frame->f_lineno = line;
5334 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5335 if (!co_filename)
5336 co_filename = "?";
5337 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5338 if (!co_name)
5339 co_name = "?";
5340 PyDTrace_LINE(co_filename, co_name, line);
5341 }
5342 *instr_prev = frame->f_lasti;
5343}