blob: b311248c6a2073252de867c8d332cf73354da405 [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. */
Victor Stinner4d61e6e2019-03-04 14:21:28 +010099#define COMPUTE_EVAL_BREAKER() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 _Py_atomic_store_relaxed( \
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100101 &_PyRuntime.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) | \
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100104 _Py_atomic_load_relaxed(&_PyRuntime.ceval.pending.calls_to_do) | \
105 _PyRuntime.ceval.pending.async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000106
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100107#define SET_GIL_DROP_REQUEST() \
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); \
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100110 _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000112
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100113#define RESET_GIL_DROP_REQUEST() \
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); \
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100116 COMPUTE_EVAL_BREAKER(); \
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 */
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100120#define SIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 do { \
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100122 _Py_atomic_store_relaxed(&_PyRuntime.ceval.pending.calls_to_do, 1); \
123 _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000125
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100126#define UNSIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 do { \
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100128 _Py_atomic_store_relaxed(&_PyRuntime.ceval.pending.calls_to_do, 0); \
129 COMPUTE_EVAL_BREAKER(); \
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); \
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100135 _Py_atomic_store_relaxed(&_PyRuntime.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); \
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100141 COMPUTE_EVAL_BREAKER(); \
Eric Snowfdf282d2019-01-11 14:26:55 -0700142 } while (0)
143
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100144#define SIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 do { \
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100146 _PyRuntime.ceval.pending.async_exc = 1; \
147 _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000149
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100150#define UNSIGNAL_ASYNC_EXC() \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600151 do { \
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100152 _PyRuntime.ceval.pending.async_exc = 0; \
153 COMPUTE_EVAL_BREAKER(); \
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());
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100177 _PyRuntime.ceval.pending.main_thread = PyThread_get_thread_ident();
178 if (!_PyRuntime.ceval.pending.lock)
179 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000180}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000181
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000182void
Antoine Pitrou1df15362010-09-13 14:16:46 +0000183_PyEval_FiniThreads(void)
184{
185 if (!gil_created())
186 return;
187 destroy_gil();
188 assert(!gil_created());
189}
190
191void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000192PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000193{
Victor Stinner50b48572018-11-01 01:51:40 +0100194 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 if (tstate == NULL)
196 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
197 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000198}
199
200void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000201PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000202{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100204 We therefore avoid PyThreadState_Get() which dumps a fatal error
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 in debug mode.
206 */
Victor Stinner50b48572018-11-01 01:51:40 +0100207 drop_gil(_PyThreadState_GET());
Guido van Rossum25ce5661997-08-02 03:10:38 +0000208}
209
210void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000211PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000212{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 if (tstate == NULL)
214 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
215 /* Check someone has called PyEval_InitThreads() to create the lock */
216 assert(gil_created());
217 take_gil(tstate);
218 if (PyThreadState_Swap(tstate) != NULL)
219 Py_FatalError(
220 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000221}
222
223void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000224PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000225{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 if (tstate == NULL)
227 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
228 if (PyThreadState_Swap(NULL) != tstate)
229 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
230 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000231}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000232
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200233/* This function is called from PyOS_AfterFork_Child to destroy all threads
234 * which are not running in the child process, and clear internal locks
235 * which might be held by those threads.
236 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000237
238void
239PyEval_ReInitThreads(void)
240{
Victor Stinner50b48572018-11-01 01:51:40 +0100241 PyThreadState *current_tstate = _PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 if (!gil_created())
244 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 recreate_gil();
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100246 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200247 take_gil(current_tstate);
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100248 _PyRuntime.ceval.pending.main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000249
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200250 /* Destroy all threads except the current one */
251 _PyThreadState_DeleteExcept(current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000252}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000253
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000254/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600255 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000256
257void
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100258_PyEval_SignalAsyncExc(void)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000259{
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100260 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000261}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000262
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000263PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000264PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000265{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 PyThreadState *tstate = PyThreadState_Swap(NULL);
267 if (tstate == NULL)
268 Py_FatalError("PyEval_SaveThread: NULL tstate");
Victor Stinner2914bb32018-01-29 11:57:45 +0100269 assert(gil_created());
270 drop_gil(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000272}
273
274void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000275PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000276{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 if (tstate == NULL)
278 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Victor Stinner2914bb32018-01-29 11:57:45 +0100279 assert(gil_created());
280
281 int err = errno;
282 take_gil(tstate);
283 /* _Py_Finalizing is protected by the GIL */
284 if (_Py_IsFinalizing() && !_Py_CURRENTLY_FINALIZING(tstate)) {
285 drop_gil(tstate);
286 PyThread_exit_thread();
287 Py_UNREACHABLE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 }
Victor Stinner2914bb32018-01-29 11:57:45 +0100289 errno = err;
290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000292}
293
294
Guido van Rossuma9672091994-09-14 13:31:22 +0000295/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
296 signal handlers or Mac I/O completion routines) can schedule calls
297 to a function to be called synchronously.
298 The synchronous function is called with one void* argument.
299 It should return 0 for success or -1 for failure -- failure should
300 be accompanied by an exception.
301
302 If registry succeeds, the registry function returns 0; if it fails
303 (e.g. due to too many pending calls) it returns -1 (without setting
304 an exception condition).
305
306 Note that because registry may occur from within signal handlers,
307 or other asynchronous events, calling malloc() is unsafe!
308
Guido van Rossuma9672091994-09-14 13:31:22 +0000309 Any thread can schedule pending calls, but only the main thread
310 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000311 There is no facility to schedule calls to a particular thread, but
312 that should be easy to change, should that ever be required. In
313 that case, the static variables here should go into the python
314 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000315*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000316
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200317void
318_PyEval_SignalReceived(void)
319{
320 /* bpo-30703: Function called when the C signal handler of Python gets a
321 signal. We cannot queue a callback using Py_AddPendingCall() since
322 that function is not async-signal-safe. */
Eric Snowfdf282d2019-01-11 14:26:55 -0700323 SIGNAL_PENDING_SIGNALS();
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200324}
325
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200326/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000327 scheduling to be made from any thread, and even from an executing
328 callback.
329 */
330
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000331int
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100332Py_AddPendingCall(int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000333{
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100334 int i, j, result=0;
335 PyThread_type_lock lock = _PyRuntime.ceval.pending.lock;
336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000337 /* try a few times for the lock. Since this mechanism is used
338 * for signal handling (on the main thread), there is a (slim)
339 * chance that a signal is delivered on the same thread while we
340 * hold the lock during the Py_MakePendingCalls() function.
341 * This avoids a deadlock in that case.
342 * Note that signals can be delivered on any thread. In particular,
343 * on Windows, a SIGINT is delivered on a system-created worker
344 * thread.
345 * We also check for lock being NULL, in the unlikely case that
346 * this function is called before any bytecode evaluation takes place.
347 */
348 if (lock != NULL) {
349 for (i = 0; i<100; i++) {
350 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
351 break;
352 }
353 if (i == 100)
354 return -1;
355 }
356
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100357 i = _PyRuntime.ceval.pending.last;
358 j = (i + 1) % NPENDINGCALLS;
359 if (j == _PyRuntime.ceval.pending.first) {
360 result = -1; /* Queue full */
361 } else {
362 _PyRuntime.ceval.pending.calls[i].func = func;
363 _PyRuntime.ceval.pending.calls[i].arg = arg;
364 _PyRuntime.ceval.pending.last = j;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 }
366 /* signal main loop */
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100367 SIGNAL_PENDING_CALLS();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 if (lock != NULL)
369 PyThread_release_lock(lock);
370 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000371}
372
Eric Snowfdf282d2019-01-11 14:26:55 -0700373static int
374handle_signals(void)
375{
376 /* Only handle signals on main thread. */
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100377 if (_PyRuntime.ceval.pending.main_thread &&
378 PyThread_get_thread_ident() != _PyRuntime.ceval.pending.main_thread)
379 {
Eric Snowfdf282d2019-01-11 14:26:55 -0700380 return 0;
381 }
Eric Snow64d6cc82019-02-23 15:40:43 -0700382 /*
383 * Ensure that the thread isn't currently running some other
384 * interpreter.
385 */
386 if (_PyInterpreterState_GET_UNSAFE() != _PyRuntime.interpreters.main) {
387 return 0;
388 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700389
390 UNSIGNAL_PENDING_SIGNALS();
Eric Snow64d6cc82019-02-23 15:40:43 -0700391 if (_PyErr_CheckSignals() < 0) {
Eric Snowfdf282d2019-01-11 14:26:55 -0700392 SIGNAL_PENDING_SIGNALS(); /* We're not done yet */
393 return -1;
394 }
395 return 0;
396}
397
398static int
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100399make_pending_calls(void)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000400{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200401 static int busy = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000402
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100403 /* only service pending calls on main thread */
404 if (_PyRuntime.ceval.pending.main_thread &&
405 PyThread_get_thread_ident() != _PyRuntime.ceval.pending.main_thread)
406 {
407 return 0;
408 }
409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 /* don't perform recursive pending calls */
Eric Snowfdf282d2019-01-11 14:26:55 -0700411 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700413 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200414 busy = 1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200415 /* unsignal before starting to call callbacks, so that any callback
416 added in-between re-signals */
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100417 UNSIGNAL_PENDING_CALLS();
Eric Snowfdf282d2019-01-11 14:26:55 -0700418 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200419
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100420 if (!_PyRuntime.ceval.pending.lock) {
Eric Snowfdf282d2019-01-11 14:26:55 -0700421 /* initial allocation of the lock */
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100422 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
423 if (_PyRuntime.ceval.pending.lock == NULL) {
Eric Snowfdf282d2019-01-11 14:26:55 -0700424 res = -1;
425 goto error;
426 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200427 }
428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 /* perform a bounded number of calls, in case of recursion */
Eric Snowfdf282d2019-01-11 14:26:55 -0700430 for (int i=0; i<NPENDINGCALLS; i++) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100431 int j;
432 int (*func)(void *);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000433 void *arg = NULL;
434
435 /* pop one item off the queue while holding the lock */
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100436 PyThread_acquire_lock(_PyRuntime.ceval.pending.lock, WAIT_LOCK);
437 j = _PyRuntime.ceval.pending.first;
438 if (j == _PyRuntime.ceval.pending.last) {
439 func = NULL; /* Queue empty */
440 } else {
441 func = _PyRuntime.ceval.pending.calls[j].func;
442 arg = _PyRuntime.ceval.pending.calls[j].arg;
443 _PyRuntime.ceval.pending.first = (j + 1) % NPENDINGCALLS;
Eric Snowef4ac962019-02-24 15:40:47 -0800444 }
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100445 PyThread_release_lock(_PyRuntime.ceval.pending.lock);
446 /* having released the lock, perform the callback */
447 if (func == NULL)
448 break;
Eric Snowfdf282d2019-01-11 14:26:55 -0700449 res = func(arg);
450 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200451 goto error;
452 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200454
Charles-François Natalif23339a2011-07-23 18:15:43 +0200455 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700456 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200457
458error:
459 busy = 0;
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100460 SIGNAL_PENDING_CALLS();
Eric Snowfdf282d2019-01-11 14:26:55 -0700461 return res;
462}
463
464/* Py_MakePendingCalls() is a simple wrapper for the sake
465 of backward-compatibility. */
466int
467Py_MakePendingCalls(void)
468{
469 assert(PyGILState_Check());
470
471 /* Python signal handler doesn't really queue a callback: it only signals
472 that a signal was received, see _PyEval_SignalReceived(). */
473 int res = handle_signals();
474 if (res != 0) {
475 return res;
476 }
477
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100478 res = make_pending_calls();
479 if (res != 0) {
480 return res;
481 }
482
483 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000484}
485
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000486/* The interpreter's recursion limit */
487
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000488#ifndef Py_DEFAULT_RECURSION_LIMIT
489#define Py_DEFAULT_RECURSION_LIMIT 1000
490#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600491
Eric Snow05351c12017-09-05 21:43:08 -0700492int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000493
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600494void
495_PyEval_Initialize(struct _ceval_runtime_state *state)
496{
497 state->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
498 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
499 _gil_initialize(&state->gil);
500}
501
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000502int
503Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000504{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600505 return _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000506}
507
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000508void
509Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000510{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600511 _PyRuntime.ceval.recursion_limit = new_limit;
512 _Py_CheckRecursionLimit = _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000513}
514
Armin Rigo2b3eb402003-10-28 12:05:48 +0000515/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
516 if the recursion_depth reaches _Py_CheckRecursionLimit.
517 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
518 to guarantee that _Py_CheckRecursiveCall() is regularly called.
519 Without USE_STACKCHECK, there is no need for this. */
520int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300521_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000522{
Victor Stinner50b48572018-11-01 01:51:40 +0100523 PyThreadState *tstate = _PyThreadState_GET();
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600524 int recursion_limit = _PyRuntime.ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000525
526#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700527 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 if (PyOS_CheckStack()) {
529 --tstate->recursion_depth;
530 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
531 return -1;
532 }
pdox18967932017-10-25 23:03:01 -0700533 /* Needed for ABI backwards-compatibility (see bpo-31857) */
Eric Snow05351c12017-09-05 21:43:08 -0700534 _Py_CheckRecursionLimit = recursion_limit;
pdox18967932017-10-25 23:03:01 -0700535#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 if (tstate->recursion_critical)
537 /* Somebody asked that we don't check for recursion. */
538 return 0;
539 if (tstate->overflowed) {
540 if (tstate->recursion_depth > recursion_limit + 50) {
541 /* Overflowing while handling an overflow. Give up. */
542 Py_FatalError("Cannot recover from stack overflow.");
543 }
544 return 0;
545 }
546 if (tstate->recursion_depth > recursion_limit) {
547 --tstate->recursion_depth;
548 tstate->overflowed = 1;
Yury Selivanovf488fb42015-07-03 01:04:23 -0400549 PyErr_Format(PyExc_RecursionError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 "maximum recursion depth exceeded%s",
551 where);
552 return -1;
553 }
554 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000555}
556
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400557static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000558static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000559
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600560#define _Py_TracingPossible _PyRuntime.ceval.tracing_possible
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000561
Guido van Rossum374a9221991-04-04 10:40:29 +0000562
Guido van Rossumb209a111997-04-29 18:18:01 +0000563PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000564PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000565{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 return PyEval_EvalCodeEx(co,
567 globals, locals,
568 (PyObject **)NULL, 0,
569 (PyObject **)NULL, 0,
570 (PyObject **)NULL, 0,
571 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000572}
573
574
575/* Interpreter main loop */
576
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000577PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000578PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 /* This is for backward compatibility with extension modules that
580 used this API; core interpreter code should call
581 PyEval_EvalFrameEx() */
582 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000583}
584
585PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000586PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000587{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200588 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
589 return interp->eval_frame(f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700590}
591
Victor Stinnerc6944e72016-11-11 02:13:35 +0100592PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700593_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
594{
Guido van Rossum950361c1997-01-24 13:49:28 +0000595#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000597#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200598 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300599 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200600 int opcode; /* Current opcode */
601 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200602 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 PyObject *retval = NULL; /* Return value */
Victor Stinner50b48572018-11-01 01:51:40 +0100604 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000608
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000609 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611 is true when the line being executed has changed. The
612 initial values are such as to make this false the first
613 time it is tested. */
614 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000615
Serhiy Storchakaab874002016-09-11 13:48:15 +0300616 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000617 PyObject *names;
618 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000619
Brett Cannon368b4b72012-04-02 12:17:59 -0400620#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200621 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400622#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200623
Antoine Pitroub52ec782009-01-25 16:34:23 +0000624/* Computed GOTOs, or
625 the-optimization-commonly-but-improperly-known-as-"threaded code"
626 using gcc's labels-as-values extension
627 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
628
629 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000631 combined with a lookup table of jump addresses. However, since the
632 indirect jump instruction is shared by all opcodes, the CPU will have a
633 hard time making the right prediction for where to jump next (actually,
634 it will be always wrong except in the uncommon case of a sequence of
635 several identical opcodes).
636
637 "Threaded code" in contrast, uses an explicit jump table and an explicit
638 indirect jump instruction at the end of each opcode. Since the jump
639 instruction is at a different address for each opcode, the CPU will make a
640 separate prediction for each of these instructions, which is equivalent to
641 predicting the second opcode of each opcode pair. These predictions have
642 a much better chance to turn out valid, especially in small bytecode loops.
643
644 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000646 and potentially many more instructions (depending on the pipeline width).
647 A correctly predicted branch, however, is nearly free.
648
649 At the time of this writing, the "threaded code" version is up to 15-20%
650 faster than the normal "switch" version, depending on the compiler and the
651 CPU architecture.
652
653 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
654 because it would render the measurements invalid.
655
656
657 NOTE: care must be taken that the compiler doesn't try to "optimize" the
658 indirect jumps by sharing them between all opcodes. Such optimizations
659 can be disabled on gcc by using the -fno-gcse flag (or possibly
660 -fno-crossjumping).
661*/
662
Antoine Pitrou042b1282010-08-13 21:15:58 +0000663#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000664#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000665#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000666#endif
667
Antoine Pitrou042b1282010-08-13 21:15:58 +0000668#ifdef HAVE_COMPUTED_GOTOS
669 #ifndef USE_COMPUTED_GOTOS
670 #define USE_COMPUTED_GOTOS 1
671 #endif
672#else
673 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
674 #error "Computed gotos are not supported on this compiler."
675 #endif
676 #undef USE_COMPUTED_GOTOS
677 #define USE_COMPUTED_GOTOS 0
678#endif
679
680#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000681/* Import the static jump table */
682#include "opcode_targets.h"
683
Antoine Pitroub52ec782009-01-25 16:34:23 +0000684#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -0700685 op: \
686 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000687
Antoine Pitroub52ec782009-01-25 16:34:23 +0000688#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 { \
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100690 if (!_Py_atomic_load_relaxed(&_PyRuntime.ceval.eval_breaker)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 FAST_DISPATCH(); \
692 } \
693 continue; \
694 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000695
696#ifdef LLTRACE
697#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700699 if (!lltrace && !_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300701 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300702 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 } \
704 goto fast_next_opcode; \
705 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000706#else
707#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700709 if (!_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300711 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300712 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 } \
714 goto fast_next_opcode; \
715 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000716#endif
717
718#else
Benjamin Petersonddd19492018-09-16 22:38:02 -0700719#define TARGET(op) op
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300720
Antoine Pitroub52ec782009-01-25 16:34:23 +0000721#define DISPATCH() continue
722#define FAST_DISPATCH() goto fast_next_opcode
723#endif
724
725
Neal Norwitza81d2202002-07-14 00:27:26 +0000726/* Tuple access macros */
727
728#ifndef Py_DEBUG
729#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
730#else
731#define GETITEM(v, i) PyTuple_GetItem((v), (i))
732#endif
733
Guido van Rossum374a9221991-04-04 10:40:29 +0000734/* Code access macros */
735
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300736/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600737#define INSTR_OFFSET() \
738 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300739#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300740 _Py_CODEUNIT word = *next_instr; \
741 opcode = _Py_OPCODE(word); \
742 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300743 next_instr++; \
744 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300745#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
746#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000747
Raymond Hettingerf606f872003-03-16 03:11:04 +0000748/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 Some opcodes tend to come in pairs thus making it possible to
750 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300751 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 Verifying the prediction costs a single high-speed test of a register
754 variable against a constant. If the pairing was good, then the
755 processor's own internal branch predication has a high likelihood of
756 success, resulting in a nearly zero-overhead transition to the
757 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300758 including its unpredictable switch-case branch. Combined with the
759 processor's internal branch prediction, a successful PREDICT has the
760 effect of making the two opcodes run as if they were a single new opcode
761 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000762
Georg Brandl86b2fb92008-07-16 03:43:04 +0000763 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 predictions turned-on and interpret the results as if some opcodes
765 had been combined or turn-off predictions so that the opcode frequency
766 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000767
768 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 the CPU to record separate branch prediction information for each
770 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000771
Raymond Hettingerf606f872003-03-16 03:11:04 +0000772*/
773
Antoine Pitrou042b1282010-08-13 21:15:58 +0000774#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000776#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300777#define PREDICT(op) \
778 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300779 _Py_CODEUNIT word = *next_instr; \
780 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300781 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300782 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300783 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300784 goto PRED_##op; \
785 } \
786 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000787#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300788#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000789
Raymond Hettingerf606f872003-03-16 03:11:04 +0000790
Guido van Rossum374a9221991-04-04 10:40:29 +0000791/* Stack manipulation macros */
792
Martin v. Löwis18e16552006-02-15 17:27:45 +0000793/* The stack can grow at most MAXINT deep, as co_nlocals and
794 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000795#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
796#define EMPTY() (STACK_LEVEL() == 0)
797#define TOP() (stack_pointer[-1])
798#define SECOND() (stack_pointer[-2])
799#define THIRD() (stack_pointer[-3])
800#define FOURTH() (stack_pointer[-4])
801#define PEEK(n) (stack_pointer[-(n)])
802#define SET_TOP(v) (stack_pointer[-1] = (v))
803#define SET_SECOND(v) (stack_pointer[-2] = (v))
804#define SET_THIRD(v) (stack_pointer[-3] = (v))
805#define SET_FOURTH(v) (stack_pointer[-4] = (v))
806#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
807#define BASIC_STACKADJ(n) (stack_pointer += n)
808#define BASIC_PUSH(v) (*stack_pointer++ = (v))
809#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000810
Guido van Rossum96a42c81992-01-12 02:29:51 +0000811#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000813 lltrace && prtrace(TOP(), "push")); \
814 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000816 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +0000817#define STACK_GROW(n) do { \
818 assert(n >= 0); \
819 (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000820 lltrace && prtrace(TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +0000821 assert(STACK_LEVEL() <= co->co_stacksize); \
822 } while (0)
823#define STACK_SHRINK(n) do { \
824 assert(n >= 0); \
825 (void)(lltrace && prtrace(TOP(), "stackadj")); \
826 (void)(BASIC_STACKADJ(-n)); \
827 assert(STACK_LEVEL() <= co->co_stacksize); \
828 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +0000829#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +0000830 prtrace((STACK_POINTER)[-1], "ext_pop")), \
831 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000832#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000833#define PUSH(v) BASIC_PUSH(v)
834#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +0000835#define STACK_GROW(n) BASIC_STACKADJ(n)
836#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000837#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000838#endif
839
Guido van Rossum681d79a1995-07-18 14:51:37 +0000840/* Local variable macros */
841
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000843
844/* The SETLOCAL() macro must not DECREF the local variable in-place and
845 then store the new value; it must copy the old value to a temporary
846 value, then store the new value, and then DECREF the temporary value.
847 This is because it is possible that during the DECREF the frame is
848 accessed by other code (e.g. a __del__ method or gc.collect()) and the
849 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000851 GETLOCAL(i) = value; \
852 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000853
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000854
855#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 while (STACK_LEVEL() > (b)->b_level) { \
857 PyObject *v = POP(); \
858 Py_XDECREF(v); \
859 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000860
861#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300862 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +0100864 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 assert(STACK_LEVEL() >= (b)->b_level + 3); \
866 while (STACK_LEVEL() > (b)->b_level + 3) { \
867 value = POP(); \
868 Py_XDECREF(value); \
869 } \
Mark Shannonae3087c2017-10-22 22:41:51 +0100870 exc_info = tstate->exc_info; \
871 type = exc_info->exc_type; \
872 value = exc_info->exc_value; \
873 traceback = exc_info->exc_traceback; \
874 exc_info->exc_type = POP(); \
875 exc_info->exc_value = POP(); \
876 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 Py_XDECREF(type); \
878 Py_XDECREF(value); \
879 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300880 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000881
Guido van Rossuma027efa1997-05-05 20:56:21 +0000882/* Start of code */
883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 /* push frame */
885 if (Py_EnterRecursiveCall(""))
886 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000887
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000889
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 if (tstate->use_tracing) {
891 if (tstate->c_tracefunc != NULL) {
892 /* tstate->c_tracefunc, if defined, is a
893 function that will be called on *every* entry
894 to a code block. Its return value, if not
895 None, is a function that will be called at
896 the start of each executed line of code.
897 (Actually, the function must return itself
898 in order to continue tracing.) The trace
899 functions are called with three arguments:
900 a pointer to the current frame, a string
901 indicating why the function is called, and
902 an argument which depends on the situation.
903 The global trace function is also called
904 whenever an exception is detected. */
905 if (call_trace_protected(tstate->c_tracefunc,
906 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100907 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 /* Trace function raised an error */
909 goto exit_eval_frame;
910 }
911 }
912 if (tstate->c_profilefunc != NULL) {
913 /* Similar for c_profilefunc, except it needn't
914 return itself and isn't called for "line" events */
915 if (call_trace_protected(tstate->c_profilefunc,
916 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100917 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 /* Profile function raised an error */
919 goto exit_eval_frame;
920 }
921 }
922 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000923
Łukasz Langaa785c872016-09-09 17:37:37 -0700924 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
925 dtrace_function_entry(f);
926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 co = f->f_code;
928 names = co->co_names;
929 consts = co->co_consts;
930 fastlocals = f->f_localsplus;
931 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300932 assert(PyBytes_Check(co->co_code));
933 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +0300934 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
935 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
936 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300937 /*
938 f->f_lasti refers to the index of the last instruction,
939 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000940
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300941 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500942 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 When the PREDICT() macros are enabled, some opcode pairs follow in
945 direct succession without updating f->f_lasti. A successful
946 prediction effectively links the two codes together as if they
947 were a single new opcode; accordingly,f->f_lasti will point to
948 the first code in the pair (for instance, GET_ITER followed by
949 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300950 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300952 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300953 next_instr = first_instr;
954 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +0300955 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
956 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300957 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 stack_pointer = f->f_stacktop;
959 assert(stack_pointer != NULL);
960 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +0200961 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000962
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000963
Tim Peters5ca576e2001-06-18 22:08:13 +0000964#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200965 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000966#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000967
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400968 if (throwflag) /* support for generator.throw() */
969 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000970
Victor Stinnerace47d72013-07-18 01:41:08 +0200971#ifdef Py_DEBUG
972 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +0100973 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +0000974 caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +0200975 assert(!PyErr_Occurred());
976#endif
977
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200978main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 assert(stack_pointer >= f->f_valuestack); /* else underflow */
981 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +0200982 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 /* Do periodic things. Doing this every time through
985 the loop would add too much overhead, so we do it
986 only every Nth instruction. We also do it if
987 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
988 event needs attention (e.g. a signal handler or
989 async I/O handler); see Py_AddPendingCall() and
990 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000991
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100992 if (_Py_atomic_load_relaxed(&_PyRuntime.ceval.eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +0300993 opcode = _Py_OPCODE(*next_instr);
994 if (opcode == SETUP_FINALLY ||
995 opcode == SETUP_WITH ||
996 opcode == BEFORE_ASYNC_WITH ||
997 opcode == YIELD_FROM) {
998 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -0700999 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001000 - If we're about to enter the 'with:'. It will prevent
1001 emitting a resource warning in the common idiom
1002 'with open(path) as file:'.
1003 - If we're about to enter the 'async with:'.
1004 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001005 *very* useful, but might help in some cases and it's
1006 traditional)
1007 - If we're resuming a chain of nested 'yield from' or
1008 'await' calls, then each frame is parked with YIELD_FROM
1009 as its next opcode. If the user hit control-C we want to
1010 wait until we've reached the innermost frame before
1011 running the signal handler and raising KeyboardInterrupt
1012 (see bpo-30039).
1013 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 goto fast_next_opcode;
1015 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001016
1017 if (_Py_atomic_load_relaxed(
1018 &_PyRuntime.ceval.signals_pending))
1019 {
1020 if (handle_signals() != 0) {
1021 goto error;
1022 }
1023 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001024 if (_Py_atomic_load_relaxed(
Victor Stinner4d61e6e2019-03-04 14:21:28 +01001025 &_PyRuntime.ceval.pending.calls_to_do))
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001026 {
Victor Stinner4d61e6e2019-03-04 14:21:28 +01001027 if (make_pending_calls() != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001028 goto error;
Eric Snowfdf282d2019-01-11 14:26:55 -07001029 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001031
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001032 if (_Py_atomic_load_relaxed(
1033 &_PyRuntime.ceval.gil_drop_request))
1034 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 /* Give another thread a chance */
1036 if (PyThreadState_Swap(NULL) != tstate)
1037 Py_FatalError("ceval: tstate mix-up");
1038 drop_gil(tstate);
1039
1040 /* Other threads may run now */
1041
1042 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001043
1044 /* Check if we should make a quick exit. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001045 if (_Py_IsFinalizing() &&
1046 !_Py_CURRENTLY_FINALIZING(tstate))
1047 {
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001048 drop_gil(tstate);
1049 PyThread_exit_thread();
1050 }
1051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 if (PyThreadState_Swap(tstate) != NULL)
1053 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 }
1055 /* Check for asynchronous exceptions. */
1056 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001057 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 tstate->async_exc = NULL;
Victor Stinner4d61e6e2019-03-04 14:21:28 +01001059 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001060 PyErr_SetNone(exc);
1061 Py_DECREF(exc);
1062 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 }
1064 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 fast_next_opcode:
1067 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001068
Łukasz Langaa785c872016-09-09 17:37:37 -07001069 if (PyDTrace_LINE_ENABLED())
1070 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1071
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001075 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001076 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 /* see maybe_call_line_trace
1078 for expository comments */
1079 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001080
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081 err = maybe_call_line_trace(tstate->c_tracefunc,
1082 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001083 tstate, f,
1084 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 /* Reload possibly changed frame fields */
1086 JUMPTO(f->f_lasti);
1087 if (f->f_stacktop != NULL) {
1088 stack_pointer = f->f_stacktop;
1089 f->f_stacktop = NULL;
1090 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001091 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001093 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001095
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001097
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001098 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001099 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001100#ifdef DYNAMIC_EXECUTION_PROFILE
1101#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001102 dxpairs[lastopcode][opcode]++;
1103 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001104#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001106#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001107
Guido van Rossum96a42c81992-01-12 02:29:51 +00001108#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001110
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 if (lltrace) {
1112 if (HAS_ARG(opcode)) {
1113 printf("%d: %d, %d\n",
1114 f->f_lasti, opcode, oparg);
1115 }
1116 else {
1117 printf("%d: %d\n",
1118 f->f_lasti, opcode);
1119 }
1120 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001121#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001122
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001124
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001126 It is essential that any operation that fails must goto error
1127 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001128
Benjamin Petersonddd19492018-09-16 22:38:02 -07001129 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001131 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001132
Benjamin Petersonddd19492018-09-16 22:38:02 -07001133 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001134 PyObject *value = GETLOCAL(oparg);
1135 if (value == NULL) {
1136 format_exc_check_arg(PyExc_UnboundLocalError,
1137 UNBOUNDLOCAL_ERROR_MSG,
1138 PyTuple_GetItem(co->co_varnames, oparg));
1139 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001141 Py_INCREF(value);
1142 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001144 }
1145
Benjamin Petersonddd19492018-09-16 22:38:02 -07001146 case TARGET(LOAD_CONST): {
1147 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001148 PyObject *value = GETITEM(consts, oparg);
1149 Py_INCREF(value);
1150 PUSH(value);
1151 FAST_DISPATCH();
1152 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001153
Benjamin Petersonddd19492018-09-16 22:38:02 -07001154 case TARGET(STORE_FAST): {
1155 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001156 PyObject *value = POP();
1157 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001159 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001160
Benjamin Petersonddd19492018-09-16 22:38:02 -07001161 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001162 PyObject *value = POP();
1163 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001165 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001166
Benjamin Petersonddd19492018-09-16 22:38:02 -07001167 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001168 PyObject *top = TOP();
1169 PyObject *second = SECOND();
1170 SET_TOP(second);
1171 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001173 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001174
Benjamin Petersonddd19492018-09-16 22:38:02 -07001175 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001176 PyObject *top = TOP();
1177 PyObject *second = SECOND();
1178 PyObject *third = THIRD();
1179 SET_TOP(second);
1180 SET_SECOND(third);
1181 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001183 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001184
Benjamin Petersonddd19492018-09-16 22:38:02 -07001185 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001186 PyObject *top = TOP();
1187 PyObject *second = SECOND();
1188 PyObject *third = THIRD();
1189 PyObject *fourth = FOURTH();
1190 SET_TOP(second);
1191 SET_SECOND(third);
1192 SET_THIRD(fourth);
1193 SET_FOURTH(top);
1194 FAST_DISPATCH();
1195 }
1196
Benjamin Petersonddd19492018-09-16 22:38:02 -07001197 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001198 PyObject *top = TOP();
1199 Py_INCREF(top);
1200 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001202 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001203
Benjamin Petersonddd19492018-09-16 22:38:02 -07001204 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001205 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001206 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001207 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001208 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001209 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001210 SET_TOP(top);
1211 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001212 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001213 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001214
Benjamin Petersonddd19492018-09-16 22:38:02 -07001215 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001216 PyObject *value = TOP();
1217 PyObject *res = PyNumber_Positive(value);
1218 Py_DECREF(value);
1219 SET_TOP(res);
1220 if (res == NULL)
1221 goto error;
1222 DISPATCH();
1223 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001224
Benjamin Petersonddd19492018-09-16 22:38:02 -07001225 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001226 PyObject *value = TOP();
1227 PyObject *res = PyNumber_Negative(value);
1228 Py_DECREF(value);
1229 SET_TOP(res);
1230 if (res == NULL)
1231 goto error;
1232 DISPATCH();
1233 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001234
Benjamin Petersonddd19492018-09-16 22:38:02 -07001235 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001236 PyObject *value = TOP();
1237 int err = PyObject_IsTrue(value);
1238 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 if (err == 0) {
1240 Py_INCREF(Py_True);
1241 SET_TOP(Py_True);
1242 DISPATCH();
1243 }
1244 else if (err > 0) {
1245 Py_INCREF(Py_False);
1246 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 DISPATCH();
1248 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001249 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001250 goto error;
1251 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001252
Benjamin Petersonddd19492018-09-16 22:38:02 -07001253 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001254 PyObject *value = TOP();
1255 PyObject *res = PyNumber_Invert(value);
1256 Py_DECREF(value);
1257 SET_TOP(res);
1258 if (res == NULL)
1259 goto error;
1260 DISPATCH();
1261 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001262
Benjamin Petersonddd19492018-09-16 22:38:02 -07001263 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001264 PyObject *exp = POP();
1265 PyObject *base = TOP();
1266 PyObject *res = PyNumber_Power(base, exp, Py_None);
1267 Py_DECREF(base);
1268 Py_DECREF(exp);
1269 SET_TOP(res);
1270 if (res == NULL)
1271 goto error;
1272 DISPATCH();
1273 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001274
Benjamin Petersonddd19492018-09-16 22:38:02 -07001275 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001276 PyObject *right = POP();
1277 PyObject *left = TOP();
1278 PyObject *res = PyNumber_Multiply(left, right);
1279 Py_DECREF(left);
1280 Py_DECREF(right);
1281 SET_TOP(res);
1282 if (res == NULL)
1283 goto error;
1284 DISPATCH();
1285 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001286
Benjamin Petersonddd19492018-09-16 22:38:02 -07001287 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001288 PyObject *right = POP();
1289 PyObject *left = TOP();
1290 PyObject *res = PyNumber_MatrixMultiply(left, right);
1291 Py_DECREF(left);
1292 Py_DECREF(right);
1293 SET_TOP(res);
1294 if (res == NULL)
1295 goto error;
1296 DISPATCH();
1297 }
1298
Benjamin Petersonddd19492018-09-16 22:38:02 -07001299 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001300 PyObject *divisor = POP();
1301 PyObject *dividend = TOP();
1302 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1303 Py_DECREF(dividend);
1304 Py_DECREF(divisor);
1305 SET_TOP(quotient);
1306 if (quotient == NULL)
1307 goto error;
1308 DISPATCH();
1309 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001310
Benjamin Petersonddd19492018-09-16 22:38:02 -07001311 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001312 PyObject *divisor = POP();
1313 PyObject *dividend = TOP();
1314 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1315 Py_DECREF(dividend);
1316 Py_DECREF(divisor);
1317 SET_TOP(quotient);
1318 if (quotient == NULL)
1319 goto error;
1320 DISPATCH();
1321 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001322
Benjamin Petersonddd19492018-09-16 22:38:02 -07001323 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001324 PyObject *divisor = POP();
1325 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001326 PyObject *res;
1327 if (PyUnicode_CheckExact(dividend) && (
1328 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1329 // fast path; string formatting, but not if the RHS is a str subclass
1330 // (see issue28598)
1331 res = PyUnicode_Format(dividend, divisor);
1332 } else {
1333 res = PyNumber_Remainder(dividend, divisor);
1334 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001335 Py_DECREF(divisor);
1336 Py_DECREF(dividend);
1337 SET_TOP(res);
1338 if (res == NULL)
1339 goto error;
1340 DISPATCH();
1341 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001342
Benjamin Petersonddd19492018-09-16 22:38:02 -07001343 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001344 PyObject *right = POP();
1345 PyObject *left = TOP();
1346 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001347 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1348 CPython using bytecode, it is simply worthless.
1349 See http://bugs.python.org/issue21955 and
1350 http://bugs.python.org/issue10044 for the discussion. In short,
1351 no patch shown any impact on a realistic benchmark, only a minor
1352 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001353 if (PyUnicode_CheckExact(left) &&
1354 PyUnicode_CheckExact(right)) {
1355 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001356 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001357 }
1358 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001359 sum = PyNumber_Add(left, right);
1360 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001361 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001362 Py_DECREF(right);
1363 SET_TOP(sum);
1364 if (sum == NULL)
1365 goto error;
1366 DISPATCH();
1367 }
1368
Benjamin Petersonddd19492018-09-16 22:38:02 -07001369 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001370 PyObject *right = POP();
1371 PyObject *left = TOP();
1372 PyObject *diff = PyNumber_Subtract(left, right);
1373 Py_DECREF(right);
1374 Py_DECREF(left);
1375 SET_TOP(diff);
1376 if (diff == NULL)
1377 goto error;
1378 DISPATCH();
1379 }
1380
Benjamin Petersonddd19492018-09-16 22:38:02 -07001381 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001382 PyObject *sub = POP();
1383 PyObject *container = TOP();
1384 PyObject *res = PyObject_GetItem(container, sub);
1385 Py_DECREF(container);
1386 Py_DECREF(sub);
1387 SET_TOP(res);
1388 if (res == NULL)
1389 goto error;
1390 DISPATCH();
1391 }
1392
Benjamin Petersonddd19492018-09-16 22:38:02 -07001393 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001394 PyObject *right = POP();
1395 PyObject *left = TOP();
1396 PyObject *res = PyNumber_Lshift(left, right);
1397 Py_DECREF(left);
1398 Py_DECREF(right);
1399 SET_TOP(res);
1400 if (res == NULL)
1401 goto error;
1402 DISPATCH();
1403 }
1404
Benjamin Petersonddd19492018-09-16 22:38:02 -07001405 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001406 PyObject *right = POP();
1407 PyObject *left = TOP();
1408 PyObject *res = PyNumber_Rshift(left, right);
1409 Py_DECREF(left);
1410 Py_DECREF(right);
1411 SET_TOP(res);
1412 if (res == NULL)
1413 goto error;
1414 DISPATCH();
1415 }
1416
Benjamin Petersonddd19492018-09-16 22:38:02 -07001417 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001418 PyObject *right = POP();
1419 PyObject *left = TOP();
1420 PyObject *res = PyNumber_And(left, right);
1421 Py_DECREF(left);
1422 Py_DECREF(right);
1423 SET_TOP(res);
1424 if (res == NULL)
1425 goto error;
1426 DISPATCH();
1427 }
1428
Benjamin Petersonddd19492018-09-16 22:38:02 -07001429 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001430 PyObject *right = POP();
1431 PyObject *left = TOP();
1432 PyObject *res = PyNumber_Xor(left, right);
1433 Py_DECREF(left);
1434 Py_DECREF(right);
1435 SET_TOP(res);
1436 if (res == NULL)
1437 goto error;
1438 DISPATCH();
1439 }
1440
Benjamin Petersonddd19492018-09-16 22:38:02 -07001441 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001442 PyObject *right = POP();
1443 PyObject *left = TOP();
1444 PyObject *res = PyNumber_Or(left, right);
1445 Py_DECREF(left);
1446 Py_DECREF(right);
1447 SET_TOP(res);
1448 if (res == NULL)
1449 goto error;
1450 DISPATCH();
1451 }
1452
Benjamin Petersonddd19492018-09-16 22:38:02 -07001453 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001454 PyObject *v = POP();
1455 PyObject *list = PEEK(oparg);
1456 int err;
1457 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001458 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001459 if (err != 0)
1460 goto error;
1461 PREDICT(JUMP_ABSOLUTE);
1462 DISPATCH();
1463 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001464
Benjamin Petersonddd19492018-09-16 22:38:02 -07001465 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001466 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001467 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001468 int err;
1469 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001470 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001471 if (err != 0)
1472 goto error;
1473 PREDICT(JUMP_ABSOLUTE);
1474 DISPATCH();
1475 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001476
Benjamin Petersonddd19492018-09-16 22:38:02 -07001477 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001478 PyObject *exp = POP();
1479 PyObject *base = TOP();
1480 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1481 Py_DECREF(base);
1482 Py_DECREF(exp);
1483 SET_TOP(res);
1484 if (res == NULL)
1485 goto error;
1486 DISPATCH();
1487 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001488
Benjamin Petersonddd19492018-09-16 22:38:02 -07001489 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001490 PyObject *right = POP();
1491 PyObject *left = TOP();
1492 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1493 Py_DECREF(left);
1494 Py_DECREF(right);
1495 SET_TOP(res);
1496 if (res == NULL)
1497 goto error;
1498 DISPATCH();
1499 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001500
Benjamin Petersonddd19492018-09-16 22:38:02 -07001501 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001502 PyObject *right = POP();
1503 PyObject *left = TOP();
1504 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1505 Py_DECREF(left);
1506 Py_DECREF(right);
1507 SET_TOP(res);
1508 if (res == NULL)
1509 goto error;
1510 DISPATCH();
1511 }
1512
Benjamin Petersonddd19492018-09-16 22:38:02 -07001513 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001514 PyObject *divisor = POP();
1515 PyObject *dividend = TOP();
1516 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1517 Py_DECREF(dividend);
1518 Py_DECREF(divisor);
1519 SET_TOP(quotient);
1520 if (quotient == NULL)
1521 goto error;
1522 DISPATCH();
1523 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001524
Benjamin Petersonddd19492018-09-16 22:38:02 -07001525 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001526 PyObject *divisor = POP();
1527 PyObject *dividend = TOP();
1528 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1529 Py_DECREF(dividend);
1530 Py_DECREF(divisor);
1531 SET_TOP(quotient);
1532 if (quotient == NULL)
1533 goto error;
1534 DISPATCH();
1535 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001536
Benjamin Petersonddd19492018-09-16 22:38:02 -07001537 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001538 PyObject *right = POP();
1539 PyObject *left = TOP();
1540 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1541 Py_DECREF(left);
1542 Py_DECREF(right);
1543 SET_TOP(mod);
1544 if (mod == NULL)
1545 goto error;
1546 DISPATCH();
1547 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001548
Benjamin Petersonddd19492018-09-16 22:38:02 -07001549 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001550 PyObject *right = POP();
1551 PyObject *left = TOP();
1552 PyObject *sum;
1553 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1554 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001555 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001556 }
1557 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001558 sum = PyNumber_InPlaceAdd(left, right);
1559 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001560 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001561 Py_DECREF(right);
1562 SET_TOP(sum);
1563 if (sum == NULL)
1564 goto error;
1565 DISPATCH();
1566 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001567
Benjamin Petersonddd19492018-09-16 22:38:02 -07001568 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001569 PyObject *right = POP();
1570 PyObject *left = TOP();
1571 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1572 Py_DECREF(left);
1573 Py_DECREF(right);
1574 SET_TOP(diff);
1575 if (diff == NULL)
1576 goto error;
1577 DISPATCH();
1578 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001579
Benjamin Petersonddd19492018-09-16 22:38:02 -07001580 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001581 PyObject *right = POP();
1582 PyObject *left = TOP();
1583 PyObject *res = PyNumber_InPlaceLshift(left, right);
1584 Py_DECREF(left);
1585 Py_DECREF(right);
1586 SET_TOP(res);
1587 if (res == NULL)
1588 goto error;
1589 DISPATCH();
1590 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001591
Benjamin Petersonddd19492018-09-16 22:38:02 -07001592 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001593 PyObject *right = POP();
1594 PyObject *left = TOP();
1595 PyObject *res = PyNumber_InPlaceRshift(left, right);
1596 Py_DECREF(left);
1597 Py_DECREF(right);
1598 SET_TOP(res);
1599 if (res == NULL)
1600 goto error;
1601 DISPATCH();
1602 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001603
Benjamin Petersonddd19492018-09-16 22:38:02 -07001604 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001605 PyObject *right = POP();
1606 PyObject *left = TOP();
1607 PyObject *res = PyNumber_InPlaceAnd(left, right);
1608 Py_DECREF(left);
1609 Py_DECREF(right);
1610 SET_TOP(res);
1611 if (res == NULL)
1612 goto error;
1613 DISPATCH();
1614 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001615
Benjamin Petersonddd19492018-09-16 22:38:02 -07001616 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001617 PyObject *right = POP();
1618 PyObject *left = TOP();
1619 PyObject *res = PyNumber_InPlaceXor(left, right);
1620 Py_DECREF(left);
1621 Py_DECREF(right);
1622 SET_TOP(res);
1623 if (res == NULL)
1624 goto error;
1625 DISPATCH();
1626 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001627
Benjamin Petersonddd19492018-09-16 22:38:02 -07001628 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001629 PyObject *right = POP();
1630 PyObject *left = TOP();
1631 PyObject *res = PyNumber_InPlaceOr(left, right);
1632 Py_DECREF(left);
1633 Py_DECREF(right);
1634 SET_TOP(res);
1635 if (res == NULL)
1636 goto error;
1637 DISPATCH();
1638 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001639
Benjamin Petersonddd19492018-09-16 22:38:02 -07001640 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001641 PyObject *sub = TOP();
1642 PyObject *container = SECOND();
1643 PyObject *v = THIRD();
1644 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001645 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001646 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001647 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001649 Py_DECREF(container);
1650 Py_DECREF(sub);
1651 if (err != 0)
1652 goto error;
1653 DISPATCH();
1654 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001655
Benjamin Petersonddd19492018-09-16 22:38:02 -07001656 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001657 PyObject *sub = TOP();
1658 PyObject *container = SECOND();
1659 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001660 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00001661 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001662 err = PyObject_DelItem(container, sub);
1663 Py_DECREF(container);
1664 Py_DECREF(sub);
1665 if (err != 0)
1666 goto error;
1667 DISPATCH();
1668 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001669
Benjamin Petersonddd19492018-09-16 22:38:02 -07001670 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01001671 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001672 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001673 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001674 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001675 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 PyErr_SetString(PyExc_RuntimeError,
1677 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001678 Py_DECREF(value);
1679 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001681 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001682 Py_DECREF(value);
1683 if (res == NULL)
1684 goto error;
1685 Py_DECREF(res);
1686 DISPATCH();
1687 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001688
Benjamin Petersonddd19492018-09-16 22:38:02 -07001689 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001690 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001691 switch (oparg) {
1692 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001693 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001694 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001695 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001696 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001697 /* fall through */
1698 case 0:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001699 if (do_raise(exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001700 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001701 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702 break;
1703 default:
1704 PyErr_SetString(PyExc_SystemError,
1705 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001706 break;
1707 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001708 goto error;
1709 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001710
Benjamin Petersonddd19492018-09-16 22:38:02 -07001711 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001713 assert(f->f_iblock == 0);
1714 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001715 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001716
Benjamin Petersonddd19492018-09-16 22:38:02 -07001717 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001718 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001719 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001720 PyObject *obj = TOP();
1721 PyTypeObject *type = Py_TYPE(obj);
1722
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001723 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001724 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001725 }
Yury Selivanov75445082015-05-11 22:57:16 -04001726
1727 if (getter != NULL) {
1728 iter = (*getter)(obj);
1729 Py_DECREF(obj);
1730 if (iter == NULL) {
1731 SET_TOP(NULL);
1732 goto error;
1733 }
1734 }
1735 else {
1736 SET_TOP(NULL);
1737 PyErr_Format(
1738 PyExc_TypeError,
1739 "'async for' requires an object with "
1740 "__aiter__ method, got %.100s",
1741 type->tp_name);
1742 Py_DECREF(obj);
1743 goto error;
1744 }
1745
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001746 if (Py_TYPE(iter)->tp_as_async == NULL ||
1747 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001748
Yury Selivanov398ff912017-03-02 22:20:00 -05001749 SET_TOP(NULL);
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001750 PyErr_Format(
1751 PyExc_TypeError,
1752 "'async for' received an object from __aiter__ "
1753 "that does not implement __anext__: %.100s",
1754 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001755 Py_DECREF(iter);
1756 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001757 }
1758
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001759 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001760 DISPATCH();
1761 }
1762
Benjamin Petersonddd19492018-09-16 22:38:02 -07001763 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001764 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001765 PyObject *next_iter = NULL;
1766 PyObject *awaitable = NULL;
1767 PyObject *aiter = TOP();
1768 PyTypeObject *type = Py_TYPE(aiter);
1769
Yury Selivanoveb636452016-09-08 22:01:51 -07001770 if (PyAsyncGen_CheckExact(aiter)) {
1771 awaitable = type->tp_as_async->am_anext(aiter);
1772 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001773 goto error;
1774 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001775 } else {
1776 if (type->tp_as_async != NULL){
1777 getter = type->tp_as_async->am_anext;
1778 }
Yury Selivanov75445082015-05-11 22:57:16 -04001779
Yury Selivanoveb636452016-09-08 22:01:51 -07001780 if (getter != NULL) {
1781 next_iter = (*getter)(aiter);
1782 if (next_iter == NULL) {
1783 goto error;
1784 }
1785 }
1786 else {
1787 PyErr_Format(
1788 PyExc_TypeError,
1789 "'async for' requires an iterator with "
1790 "__anext__ method, got %.100s",
1791 type->tp_name);
1792 goto error;
1793 }
Yury Selivanov75445082015-05-11 22:57:16 -04001794
Yury Selivanoveb636452016-09-08 22:01:51 -07001795 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1796 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001797 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001798 PyExc_TypeError,
1799 "'async for' received an invalid object "
1800 "from __anext__: %.100s",
1801 Py_TYPE(next_iter)->tp_name);
1802
1803 Py_DECREF(next_iter);
1804 goto error;
1805 } else {
1806 Py_DECREF(next_iter);
1807 }
1808 }
Yury Selivanov75445082015-05-11 22:57:16 -04001809
1810 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001811 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001812 DISPATCH();
1813 }
1814
Benjamin Petersonddd19492018-09-16 22:38:02 -07001815 case TARGET(GET_AWAITABLE): {
1816 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04001817 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04001818 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04001819
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03001820 if (iter == NULL) {
1821 format_awaitable_error(Py_TYPE(iterable),
1822 _Py_OPCODE(next_instr[-2]));
1823 }
1824
Yury Selivanov75445082015-05-11 22:57:16 -04001825 Py_DECREF(iterable);
1826
Yury Selivanovc724bae2016-03-02 11:30:46 -05001827 if (iter != NULL && PyCoro_CheckExact(iter)) {
1828 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
1829 if (yf != NULL) {
1830 /* `iter` is a coroutine object that is being
1831 awaited, `yf` is a pointer to the current awaitable
1832 being awaited on. */
1833 Py_DECREF(yf);
1834 Py_CLEAR(iter);
1835 PyErr_SetString(
1836 PyExc_RuntimeError,
1837 "coroutine is being awaited already");
1838 /* The code below jumps to `error` if `iter` is NULL. */
1839 }
1840 }
1841
Yury Selivanov75445082015-05-11 22:57:16 -04001842 SET_TOP(iter); /* Even if it's NULL */
1843
1844 if (iter == NULL) {
1845 goto error;
1846 }
1847
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001848 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001849 DISPATCH();
1850 }
1851
Benjamin Petersonddd19492018-09-16 22:38:02 -07001852 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001853 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001854 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001855 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001856 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
1857 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001858 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001859 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001860 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001861 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001862 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001863 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001864 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001865 Py_DECREF(v);
1866 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001867 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08001868 if (tstate->c_tracefunc != NULL
1869 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001870 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10001871 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001872 if (err < 0)
1873 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001874 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001875 SET_TOP(val);
1876 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001877 }
Martin Panter95f53c12016-07-18 08:23:26 +00001878 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001879 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001880 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01001881 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03001882 f->f_lasti -= sizeof(_Py_CODEUNIT);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001883 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001884 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001885
Benjamin Petersonddd19492018-09-16 22:38:02 -07001886 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001887 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07001888
1889 if (co->co_flags & CO_ASYNC_GENERATOR) {
1890 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
1891 Py_DECREF(retval);
1892 if (w == NULL) {
1893 retval = NULL;
1894 goto error;
1895 }
1896 retval = w;
1897 }
1898
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001899 f->f_stacktop = stack_pointer;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001900 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001901 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001902
Benjamin Petersonddd19492018-09-16 22:38:02 -07001903 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001904 PyObject *type, *value, *traceback;
1905 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001906 PyTryBlock *b = PyFrame_BlockPop(f);
1907 if (b->b_type != EXCEPT_HANDLER) {
1908 PyErr_SetString(PyExc_SystemError,
1909 "popped block is not an except handler");
1910 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001911 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001912 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
1913 STACK_LEVEL() <= (b)->b_level + 4);
1914 exc_info = tstate->exc_info;
1915 type = exc_info->exc_type;
1916 value = exc_info->exc_value;
1917 traceback = exc_info->exc_traceback;
1918 exc_info->exc_type = POP();
1919 exc_info->exc_value = POP();
1920 exc_info->exc_traceback = POP();
1921 Py_XDECREF(type);
1922 Py_XDECREF(value);
1923 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001925 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001926
Benjamin Petersonddd19492018-09-16 22:38:02 -07001927 case TARGET(POP_BLOCK): {
1928 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001929 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001931 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001932
Benjamin Petersonddd19492018-09-16 22:38:02 -07001933 case TARGET(POP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001934 /* If oparg is 0 at the top of the stack are 1 or 6 values:
1935 Either:
1936 - TOP = NULL or an integer
1937 or:
1938 - (TOP, SECOND, THIRD) = exc_info()
1939 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
1940
1941 If oparg is 1 the value for 'return' was additionally pushed
1942 at the top of the stack.
1943 */
1944 PyObject *res = NULL;
1945 if (oparg) {
1946 res = POP();
1947 }
1948 PyObject *exc = POP();
1949 if (exc == NULL || PyLong_CheckExact(exc)) {
1950 Py_XDECREF(exc);
1951 }
1952 else {
1953 Py_DECREF(exc);
1954 Py_DECREF(POP());
1955 Py_DECREF(POP());
1956
1957 PyObject *type, *value, *traceback;
1958 _PyErr_StackItem *exc_info;
1959 PyTryBlock *b = PyFrame_BlockPop(f);
1960 if (b->b_type != EXCEPT_HANDLER) {
1961 PyErr_SetString(PyExc_SystemError,
1962 "popped block is not an except handler");
1963 Py_XDECREF(res);
1964 goto error;
1965 }
1966 assert(STACK_LEVEL() == (b)->b_level + 3);
1967 exc_info = tstate->exc_info;
1968 type = exc_info->exc_type;
1969 value = exc_info->exc_value;
1970 traceback = exc_info->exc_traceback;
1971 exc_info->exc_type = POP();
1972 exc_info->exc_value = POP();
1973 exc_info->exc_traceback = POP();
1974 Py_XDECREF(type);
1975 Py_XDECREF(value);
1976 Py_XDECREF(traceback);
1977 }
1978 if (oparg) {
1979 PUSH(res);
1980 }
1981 DISPATCH();
1982 }
1983
Benjamin Petersonddd19492018-09-16 22:38:02 -07001984 case TARGET(CALL_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001985 PyObject *ret = PyLong_FromLong(INSTR_OFFSET());
1986 if (ret == NULL) {
1987 goto error;
1988 }
1989 PUSH(ret);
1990 JUMPBY(oparg);
1991 FAST_DISPATCH();
1992 }
1993
Benjamin Petersonddd19492018-09-16 22:38:02 -07001994 case TARGET(BEGIN_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001995 /* Push NULL onto the stack for using it in END_FINALLY,
1996 POP_FINALLY, WITH_CLEANUP_START and WITH_CLEANUP_FINISH.
1997 */
1998 PUSH(NULL);
1999 FAST_DISPATCH();
2000 }
2001
Benjamin Petersonddd19492018-09-16 22:38:02 -07002002 case TARGET(END_FINALLY): {
2003 PREDICTED(END_FINALLY);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002004 /* At the top of the stack are 1 or 6 values:
2005 Either:
2006 - TOP = NULL or an integer
2007 or:
2008 - (TOP, SECOND, THIRD) = exc_info()
2009 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2010 */
2011 PyObject *exc = POP();
2012 if (exc == NULL) {
2013 FAST_DISPATCH();
2014 }
2015 else if (PyLong_CheckExact(exc)) {
2016 int ret = _PyLong_AsInt(exc);
2017 Py_DECREF(exc);
2018 if (ret == -1 && PyErr_Occurred()) {
2019 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002020 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002021 JUMPTO(ret);
2022 FAST_DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002023 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002024 else {
2025 assert(PyExceptionClass_Check(exc));
2026 PyObject *val = POP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002027 PyObject *tb = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002028 PyErr_Restore(exc, val, tb);
2029 goto exception_unwind;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002030 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002031 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002032
Benjamin Petersonddd19492018-09-16 22:38:02 -07002033 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002034 PyObject *exc = POP();
2035 assert(PyExceptionClass_Check(exc));
2036 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2037 PyTryBlock *b = PyFrame_BlockPop(f);
2038 assert(b->b_type == EXCEPT_HANDLER);
2039 Py_DECREF(exc);
2040 UNWIND_EXCEPT_HANDLER(b);
2041 Py_DECREF(POP());
2042 JUMPBY(oparg);
2043 FAST_DISPATCH();
2044 }
2045 else {
2046 PyObject *val = POP();
2047 PyObject *tb = POP();
2048 PyErr_Restore(exc, val, tb);
2049 goto exception_unwind;
2050 }
2051 }
2052
Benjamin Petersonddd19492018-09-16 22:38:02 -07002053 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002054 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002055
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002056 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002057 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002058 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002059 if (bc == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002060 if (!PyErr_Occurred()) {
2061 PyErr_SetString(PyExc_NameError,
2062 "__build_class__ not found");
2063 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002064 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002065 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002066 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002067 }
2068 else {
2069 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2070 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002071 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002072 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2073 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002074 if (PyErr_ExceptionMatches(PyExc_KeyError))
2075 PyErr_SetString(PyExc_NameError,
2076 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002077 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002078 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002079 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002080 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002081 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002082 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002083
Benjamin Petersonddd19492018-09-16 22:38:02 -07002084 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002085 PyObject *name = GETITEM(names, oparg);
2086 PyObject *v = POP();
2087 PyObject *ns = f->f_locals;
2088 int err;
2089 if (ns == NULL) {
2090 PyErr_Format(PyExc_SystemError,
2091 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002092 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002093 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002094 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002095 if (PyDict_CheckExact(ns))
2096 err = PyDict_SetItem(ns, name, v);
2097 else
2098 err = PyObject_SetItem(ns, name, v);
2099 Py_DECREF(v);
2100 if (err != 0)
2101 goto error;
2102 DISPATCH();
2103 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002104
Benjamin Petersonddd19492018-09-16 22:38:02 -07002105 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002106 PyObject *name = GETITEM(names, oparg);
2107 PyObject *ns = f->f_locals;
2108 int err;
2109 if (ns == NULL) {
2110 PyErr_Format(PyExc_SystemError,
2111 "no locals when deleting %R", name);
2112 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002113 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002114 err = PyObject_DelItem(ns, name);
2115 if (err != 0) {
2116 format_exc_check_arg(PyExc_NameError,
2117 NAME_ERROR_MSG,
2118 name);
2119 goto error;
2120 }
2121 DISPATCH();
2122 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002123
Benjamin Petersonddd19492018-09-16 22:38:02 -07002124 case TARGET(UNPACK_SEQUENCE): {
2125 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002126 PyObject *seq = POP(), *item, **items;
2127 if (PyTuple_CheckExact(seq) &&
2128 PyTuple_GET_SIZE(seq) == oparg) {
2129 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002130 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002131 item = items[oparg];
2132 Py_INCREF(item);
2133 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002134 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002135 } else if (PyList_CheckExact(seq) &&
2136 PyList_GET_SIZE(seq) == oparg) {
2137 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002138 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002139 item = items[oparg];
2140 Py_INCREF(item);
2141 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002142 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002143 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002144 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002145 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002146 } else {
2147 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002148 Py_DECREF(seq);
2149 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002150 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002151 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002152 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002153 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002154
Benjamin Petersonddd19492018-09-16 22:38:02 -07002155 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002156 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2157 PyObject *seq = POP();
2158
2159 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2160 stack_pointer + totalargs)) {
2161 stack_pointer += totalargs;
2162 } else {
2163 Py_DECREF(seq);
2164 goto error;
2165 }
2166 Py_DECREF(seq);
2167 DISPATCH();
2168 }
2169
Benjamin Petersonddd19492018-09-16 22:38:02 -07002170 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002171 PyObject *name = GETITEM(names, oparg);
2172 PyObject *owner = TOP();
2173 PyObject *v = SECOND();
2174 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002175 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002176 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002177 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002178 Py_DECREF(owner);
2179 if (err != 0)
2180 goto error;
2181 DISPATCH();
2182 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002183
Benjamin Petersonddd19492018-09-16 22:38:02 -07002184 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002185 PyObject *name = GETITEM(names, oparg);
2186 PyObject *owner = POP();
2187 int err;
2188 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2189 Py_DECREF(owner);
2190 if (err != 0)
2191 goto error;
2192 DISPATCH();
2193 }
2194
Benjamin Petersonddd19492018-09-16 22:38:02 -07002195 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002196 PyObject *name = GETITEM(names, oparg);
2197 PyObject *v = POP();
2198 int err;
2199 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002200 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002201 if (err != 0)
2202 goto error;
2203 DISPATCH();
2204 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002205
Benjamin Petersonddd19492018-09-16 22:38:02 -07002206 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002207 PyObject *name = GETITEM(names, oparg);
2208 int err;
2209 err = PyDict_DelItem(f->f_globals, name);
2210 if (err != 0) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002211 if (PyErr_ExceptionMatches(PyExc_KeyError)) {
2212 format_exc_check_arg(
2213 PyExc_NameError, NAME_ERROR_MSG, name);
2214 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002215 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002216 }
2217 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002218 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002219
Benjamin Petersonddd19492018-09-16 22:38:02 -07002220 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002221 PyObject *name = GETITEM(names, oparg);
2222 PyObject *locals = f->f_locals;
2223 PyObject *v;
2224 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002225 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002226 "no locals when loading %R", name);
2227 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002228 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002229 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002230 v = PyDict_GetItemWithError(locals, name);
2231 if (v != NULL) {
2232 Py_INCREF(v);
2233 }
2234 else if (PyErr_Occurred()) {
2235 goto error;
2236 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002237 }
2238 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002239 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002240 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002241 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2242 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002243 PyErr_Clear();
2244 }
2245 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002246 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002247 v = PyDict_GetItemWithError(f->f_globals, name);
2248 if (v != NULL) {
2249 Py_INCREF(v);
2250 }
2251 else if (PyErr_Occurred()) {
2252 goto error;
2253 }
2254 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002255 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002256 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002257 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002258 if (!PyErr_Occurred()) {
2259 format_exc_check_arg(
Victor Stinnerb0b22422012-04-19 00:57:45 +02002260 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002261 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002262 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002263 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002264 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002265 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002266 }
2267 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002268 v = PyObject_GetItem(f->f_builtins, name);
2269 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002270 if (PyErr_ExceptionMatches(PyExc_KeyError))
2271 format_exc_check_arg(
2272 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002273 NAME_ERROR_MSG, name);
2274 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002275 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002276 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002277 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002279 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002280 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002281 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002282
Benjamin Petersonddd19492018-09-16 22:38:02 -07002283 case TARGET(LOAD_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002284 PyObject *name = GETITEM(names, oparg);
2285 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002286 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002287 && PyDict_CheckExact(f->f_builtins))
2288 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002289 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002290 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002291 name);
2292 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002293 if (!_PyErr_OCCURRED()) {
2294 /* _PyDict_LoadGlobal() returns NULL without raising
2295 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002296 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002297 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002298 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002299 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002300 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002301 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002302 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002303 else {
2304 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002305
2306 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002307 v = PyObject_GetItem(f->f_globals, name);
2308 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002309 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2310 goto error;
2311 PyErr_Clear();
2312
Victor Stinnerb4efc962015-11-20 09:24:02 +01002313 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002314 v = PyObject_GetItem(f->f_builtins, name);
2315 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002316 if (PyErr_ExceptionMatches(PyExc_KeyError))
2317 format_exc_check_arg(
2318 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002319 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002320 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002321 }
2322 }
2323 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002324 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002325 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002326 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002327
Benjamin Petersonddd19492018-09-16 22:38:02 -07002328 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002329 PyObject *v = GETLOCAL(oparg);
2330 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002331 SETLOCAL(oparg, NULL);
2332 DISPATCH();
2333 }
2334 format_exc_check_arg(
2335 PyExc_UnboundLocalError,
2336 UNBOUNDLOCAL_ERROR_MSG,
2337 PyTuple_GetItem(co->co_varnames, oparg)
2338 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002339 goto error;
2340 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002341
Benjamin Petersonddd19492018-09-16 22:38:02 -07002342 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002343 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002344 PyObject *oldobj = PyCell_GET(cell);
2345 if (oldobj != NULL) {
2346 PyCell_SET(cell, NULL);
2347 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002348 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002349 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002350 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002351 goto error;
2352 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002353
Benjamin Petersonddd19492018-09-16 22:38:02 -07002354 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002355 PyObject *cell = freevars[oparg];
2356 Py_INCREF(cell);
2357 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002358 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002359 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002360
Benjamin Petersonddd19492018-09-16 22:38:02 -07002361 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002362 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002363 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002364 assert(locals);
2365 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2366 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2367 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2368 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2369 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002370 value = PyDict_GetItemWithError(locals, name);
2371 if (value != NULL) {
2372 Py_INCREF(value);
2373 }
2374 else if (PyErr_Occurred()) {
2375 goto error;
2376 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002377 }
2378 else {
2379 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002380 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002381 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2382 goto error;
2383 PyErr_Clear();
2384 }
2385 }
2386 if (!value) {
2387 PyObject *cell = freevars[oparg];
2388 value = PyCell_GET(cell);
2389 if (value == NULL) {
2390 format_exc_unbound(co, oparg);
2391 goto error;
2392 }
2393 Py_INCREF(value);
2394 }
2395 PUSH(value);
2396 DISPATCH();
2397 }
2398
Benjamin Petersonddd19492018-09-16 22:38:02 -07002399 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002400 PyObject *cell = freevars[oparg];
2401 PyObject *value = PyCell_GET(cell);
2402 if (value == NULL) {
2403 format_exc_unbound(co, oparg);
2404 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002405 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002406 Py_INCREF(value);
2407 PUSH(value);
2408 DISPATCH();
2409 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002410
Benjamin Petersonddd19492018-09-16 22:38:02 -07002411 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002412 PyObject *v = POP();
2413 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002414 PyObject *oldobj = PyCell_GET(cell);
2415 PyCell_SET(cell, v);
2416 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002417 DISPATCH();
2418 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002419
Benjamin Petersonddd19492018-09-16 22:38:02 -07002420 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002421 PyObject *str;
2422 PyObject *empty = PyUnicode_New(0, 0);
2423 if (empty == NULL) {
2424 goto error;
2425 }
2426 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2427 Py_DECREF(empty);
2428 if (str == NULL)
2429 goto error;
2430 while (--oparg >= 0) {
2431 PyObject *item = POP();
2432 Py_DECREF(item);
2433 }
2434 PUSH(str);
2435 DISPATCH();
2436 }
2437
Benjamin Petersonddd19492018-09-16 22:38:02 -07002438 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002439 PyObject *tup = PyTuple_New(oparg);
2440 if (tup == NULL)
2441 goto error;
2442 while (--oparg >= 0) {
2443 PyObject *item = POP();
2444 PyTuple_SET_ITEM(tup, oparg, item);
2445 }
2446 PUSH(tup);
2447 DISPATCH();
2448 }
2449
Benjamin Petersonddd19492018-09-16 22:38:02 -07002450 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002451 PyObject *list = PyList_New(oparg);
2452 if (list == NULL)
2453 goto error;
2454 while (--oparg >= 0) {
2455 PyObject *item = POP();
2456 PyList_SET_ITEM(list, oparg, item);
2457 }
2458 PUSH(list);
2459 DISPATCH();
2460 }
2461
Benjamin Petersonddd19492018-09-16 22:38:02 -07002462 case TARGET(BUILD_TUPLE_UNPACK_WITH_CALL):
2463 case TARGET(BUILD_TUPLE_UNPACK):
2464 case TARGET(BUILD_LIST_UNPACK): {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002465 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002466 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002467 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002468 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002469
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002470 if (sum == NULL)
2471 goto error;
2472
2473 for (i = oparg; i > 0; i--) {
2474 PyObject *none_val;
2475
2476 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2477 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002478 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002479 PyErr_ExceptionMatches(PyExc_TypeError))
2480 {
2481 check_args_iterable(PEEK(1 + oparg), PEEK(i));
Serhiy Storchaka73442852016-10-02 10:33:46 +03002482 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002483 Py_DECREF(sum);
2484 goto error;
2485 }
2486 Py_DECREF(none_val);
2487 }
2488
2489 if (convert_to_tuple) {
2490 return_value = PyList_AsTuple(sum);
2491 Py_DECREF(sum);
2492 if (return_value == NULL)
2493 goto error;
2494 }
2495 else {
2496 return_value = sum;
2497 }
2498
2499 while (oparg--)
2500 Py_DECREF(POP());
2501 PUSH(return_value);
2502 DISPATCH();
2503 }
2504
Benjamin Petersonddd19492018-09-16 22:38:02 -07002505 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002506 PyObject *set = PySet_New(NULL);
2507 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002508 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002509 if (set == NULL)
2510 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002511 for (i = oparg; i > 0; i--) {
2512 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002513 if (err == 0)
2514 err = PySet_Add(set, item);
2515 Py_DECREF(item);
2516 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002517 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002518 if (err != 0) {
2519 Py_DECREF(set);
2520 goto error;
2521 }
2522 PUSH(set);
2523 DISPATCH();
2524 }
2525
Benjamin Petersonddd19492018-09-16 22:38:02 -07002526 case TARGET(BUILD_SET_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002527 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002528 PyObject *sum = PySet_New(NULL);
2529 if (sum == NULL)
2530 goto error;
2531
2532 for (i = oparg; i > 0; i--) {
2533 if (_PySet_Update(sum, PEEK(i)) < 0) {
2534 Py_DECREF(sum);
2535 goto error;
2536 }
2537 }
2538
2539 while (oparg--)
2540 Py_DECREF(POP());
2541 PUSH(sum);
2542 DISPATCH();
2543 }
2544
Benjamin Petersonddd19492018-09-16 22:38:02 -07002545 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002546 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002547 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2548 if (map == NULL)
2549 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002550 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002551 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002552 PyObject *key = PEEK(2*i);
2553 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002554 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002555 if (err != 0) {
2556 Py_DECREF(map);
2557 goto error;
2558 }
2559 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002560
2561 while (oparg--) {
2562 Py_DECREF(POP());
2563 Py_DECREF(POP());
2564 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002565 PUSH(map);
2566 DISPATCH();
2567 }
2568
Benjamin Petersonddd19492018-09-16 22:38:02 -07002569 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002570 _Py_IDENTIFIER(__annotations__);
2571 int err;
2572 PyObject *ann_dict;
2573 if (f->f_locals == NULL) {
2574 PyErr_Format(PyExc_SystemError,
2575 "no locals found when setting up annotations");
2576 goto error;
2577 }
2578 /* check if __annotations__ in locals()... */
2579 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002580 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002581 &PyId___annotations__);
2582 if (ann_dict == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002583 if (PyErr_Occurred()) {
2584 goto error;
2585 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002586 /* ...if not, create a new one */
2587 ann_dict = PyDict_New();
2588 if (ann_dict == NULL) {
2589 goto error;
2590 }
2591 err = _PyDict_SetItemId(f->f_locals,
2592 &PyId___annotations__, ann_dict);
2593 Py_DECREF(ann_dict);
2594 if (err != 0) {
2595 goto error;
2596 }
2597 }
2598 }
2599 else {
2600 /* do the same if locals() is not a dict */
2601 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2602 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002603 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002604 }
2605 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2606 if (ann_dict == NULL) {
2607 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2608 goto error;
2609 }
2610 PyErr_Clear();
2611 ann_dict = PyDict_New();
2612 if (ann_dict == NULL) {
2613 goto error;
2614 }
2615 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2616 Py_DECREF(ann_dict);
2617 if (err != 0) {
2618 goto error;
2619 }
2620 }
2621 else {
2622 Py_DECREF(ann_dict);
2623 }
2624 }
2625 DISPATCH();
2626 }
2627
Benjamin Petersonddd19492018-09-16 22:38:02 -07002628 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002629 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002630 PyObject *map;
2631 PyObject *keys = TOP();
2632 if (!PyTuple_CheckExact(keys) ||
2633 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2634 PyErr_SetString(PyExc_SystemError,
2635 "bad BUILD_CONST_KEY_MAP keys argument");
2636 goto error;
2637 }
2638 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2639 if (map == NULL) {
2640 goto error;
2641 }
2642 for (i = oparg; i > 0; i--) {
2643 int err;
2644 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2645 PyObject *value = PEEK(i + 1);
2646 err = PyDict_SetItem(map, key, value);
2647 if (err != 0) {
2648 Py_DECREF(map);
2649 goto error;
2650 }
2651 }
2652
2653 Py_DECREF(POP());
2654 while (oparg--) {
2655 Py_DECREF(POP());
2656 }
2657 PUSH(map);
2658 DISPATCH();
2659 }
2660
Benjamin Petersonddd19492018-09-16 22:38:02 -07002661 case TARGET(BUILD_MAP_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002662 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002663 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002664 if (sum == NULL)
2665 goto error;
2666
2667 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002668 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002669 if (PyDict_Update(sum, arg) < 0) {
2670 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2671 PyErr_Format(PyExc_TypeError,
Berker Peksag8e9045d2016-10-02 13:08:25 +03002672 "'%.200s' object is not a mapping",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002673 arg->ob_type->tp_name);
2674 }
2675 Py_DECREF(sum);
2676 goto error;
2677 }
2678 }
2679
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002680 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002681 Py_DECREF(POP());
2682 PUSH(sum);
2683 DISPATCH();
2684 }
2685
Benjamin Petersonddd19492018-09-16 22:38:02 -07002686 case TARGET(BUILD_MAP_UNPACK_WITH_CALL): {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002687 Py_ssize_t i;
2688 PyObject *sum = PyDict_New();
2689 if (sum == NULL)
2690 goto error;
2691
2692 for (i = oparg; i > 0; i--) {
2693 PyObject *arg = PEEK(i);
2694 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002695 Py_DECREF(sum);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02002696 format_kwargs_error(PEEK(2 + oparg), arg);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002697 goto error;
2698 }
2699 }
2700
2701 while (oparg--)
2702 Py_DECREF(POP());
2703 PUSH(sum);
2704 DISPATCH();
2705 }
2706
Benjamin Petersonddd19492018-09-16 22:38:02 -07002707 case TARGET(MAP_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002708 PyObject *key = TOP();
2709 PyObject *value = SECOND();
2710 PyObject *map;
2711 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002712 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002713 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002714 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002715 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002716 Py_DECREF(value);
2717 Py_DECREF(key);
2718 if (err != 0)
2719 goto error;
2720 PREDICT(JUMP_ABSOLUTE);
2721 DISPATCH();
2722 }
2723
Benjamin Petersonddd19492018-09-16 22:38:02 -07002724 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002725 PyObject *name = GETITEM(names, oparg);
2726 PyObject *owner = TOP();
2727 PyObject *res = PyObject_GetAttr(owner, name);
2728 Py_DECREF(owner);
2729 SET_TOP(res);
2730 if (res == NULL)
2731 goto error;
2732 DISPATCH();
2733 }
2734
Benjamin Petersonddd19492018-09-16 22:38:02 -07002735 case TARGET(COMPARE_OP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002736 PyObject *right = POP();
2737 PyObject *left = TOP();
2738 PyObject *res = cmp_outcome(oparg, left, right);
2739 Py_DECREF(left);
2740 Py_DECREF(right);
2741 SET_TOP(res);
2742 if (res == NULL)
2743 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002744 PREDICT(POP_JUMP_IF_FALSE);
2745 PREDICT(POP_JUMP_IF_TRUE);
2746 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002747 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002748
Benjamin Petersonddd19492018-09-16 22:38:02 -07002749 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002750 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002751 PyObject *fromlist = POP();
2752 PyObject *level = TOP();
2753 PyObject *res;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002754 res = import_name(f, name, fromlist, level);
2755 Py_DECREF(level);
2756 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002757 SET_TOP(res);
2758 if (res == NULL)
2759 goto error;
2760 DISPATCH();
2761 }
2762
Benjamin Petersonddd19492018-09-16 22:38:02 -07002763 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002764 PyObject *from = POP(), *locals;
2765 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002766 if (PyFrame_FastToLocalsWithError(f) < 0) {
2767 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002768 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002769 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002770
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002771 locals = f->f_locals;
2772 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002773 PyErr_SetString(PyExc_SystemError,
2774 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002775 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002776 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002777 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002778 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002779 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002780 Py_DECREF(from);
2781 if (err != 0)
2782 goto error;
2783 DISPATCH();
2784 }
Guido van Rossum25831651993-05-19 14:50:45 +00002785
Benjamin Petersonddd19492018-09-16 22:38:02 -07002786 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002787 PyObject *name = GETITEM(names, oparg);
2788 PyObject *from = TOP();
2789 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002790 res = import_from(from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002791 PUSH(res);
2792 if (res == NULL)
2793 goto error;
2794 DISPATCH();
2795 }
Thomas Wouters52152252000-08-17 22:55:00 +00002796
Benjamin Petersonddd19492018-09-16 22:38:02 -07002797 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002798 JUMPBY(oparg);
2799 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002800 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002801
Benjamin Petersonddd19492018-09-16 22:38:02 -07002802 case TARGET(POP_JUMP_IF_FALSE): {
2803 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002804 PyObject *cond = POP();
2805 int err;
2806 if (cond == Py_True) {
2807 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002808 FAST_DISPATCH();
2809 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002810 if (cond == Py_False) {
2811 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002812 JUMPTO(oparg);
2813 FAST_DISPATCH();
2814 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002815 err = PyObject_IsTrue(cond);
2816 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002817 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07002818 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002819 else if (err == 0)
2820 JUMPTO(oparg);
2821 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002822 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002823 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002824 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002825
Benjamin Petersonddd19492018-09-16 22:38:02 -07002826 case TARGET(POP_JUMP_IF_TRUE): {
2827 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002828 PyObject *cond = POP();
2829 int err;
2830 if (cond == Py_False) {
2831 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002832 FAST_DISPATCH();
2833 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002834 if (cond == Py_True) {
2835 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002836 JUMPTO(oparg);
2837 FAST_DISPATCH();
2838 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002839 err = PyObject_IsTrue(cond);
2840 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002841 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002842 JUMPTO(oparg);
2843 }
2844 else if (err == 0)
2845 ;
2846 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002847 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002848 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002849 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002850
Benjamin Petersonddd19492018-09-16 22:38:02 -07002851 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002852 PyObject *cond = TOP();
2853 int err;
2854 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002855 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002856 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002857 FAST_DISPATCH();
2858 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002859 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002860 JUMPTO(oparg);
2861 FAST_DISPATCH();
2862 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002863 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002864 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002865 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002866 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002867 }
2868 else if (err == 0)
2869 JUMPTO(oparg);
2870 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002871 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002872 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002873 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002874
Benjamin Petersonddd19492018-09-16 22:38:02 -07002875 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002876 PyObject *cond = TOP();
2877 int err;
2878 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002879 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002880 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002881 FAST_DISPATCH();
2882 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002883 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002884 JUMPTO(oparg);
2885 FAST_DISPATCH();
2886 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002887 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002888 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002889 JUMPTO(oparg);
2890 }
2891 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002892 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002893 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002894 }
2895 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002896 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002897 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002898 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002899
Benjamin Petersonddd19492018-09-16 22:38:02 -07002900 case TARGET(JUMP_ABSOLUTE): {
2901 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002902 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002903#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002904 /* Enabling this path speeds-up all while and for-loops by bypassing
2905 the per-loop checks for signals. By default, this should be turned-off
2906 because it prevents detection of a control-break in tight loops like
2907 "while 1: pass". Compile with this option turned-on when you need
2908 the speed-up and do not need break checking inside tight loops (ones
2909 that contain only instructions ending with FAST_DISPATCH).
2910 */
2911 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002912#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002913 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002914#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002915 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002916
Benjamin Petersonddd19492018-09-16 22:38:02 -07002917 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002918 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002919 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002920 PyObject *iter = PyObject_GetIter(iterable);
2921 Py_DECREF(iterable);
2922 SET_TOP(iter);
2923 if (iter == NULL)
2924 goto error;
2925 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002926 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04002927 DISPATCH();
2928 }
2929
Benjamin Petersonddd19492018-09-16 22:38:02 -07002930 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04002931 /* before: [obj]; after [getiter(obj)] */
2932 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04002933 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04002934 if (PyCoro_CheckExact(iterable)) {
2935 /* `iterable` is a coroutine */
2936 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
2937 /* and it is used in a 'yield from' expression of a
2938 regular generator. */
2939 Py_DECREF(iterable);
2940 SET_TOP(NULL);
2941 PyErr_SetString(PyExc_TypeError,
2942 "cannot 'yield from' a coroutine object "
2943 "in a non-coroutine generator");
2944 goto error;
2945 }
2946 }
2947 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04002948 /* `iterable` is not a generator. */
2949 iter = PyObject_GetIter(iterable);
2950 Py_DECREF(iterable);
2951 SET_TOP(iter);
2952 if (iter == NULL)
2953 goto error;
2954 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002955 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002956 DISPATCH();
2957 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002958
Benjamin Petersonddd19492018-09-16 22:38:02 -07002959 case TARGET(FOR_ITER): {
2960 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002961 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002962 PyObject *iter = TOP();
2963 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
2964 if (next != NULL) {
2965 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002966 PREDICT(STORE_FAST);
2967 PREDICT(UNPACK_SEQUENCE);
2968 DISPATCH();
2969 }
2970 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002971 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2972 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08002973 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002974 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002975 PyErr_Clear();
2976 }
2977 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00002978 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002979 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002980 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002981 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002982 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002983 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002984
Benjamin Petersonddd19492018-09-16 22:38:02 -07002985 case TARGET(SETUP_FINALLY): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002986 /* NOTE: If you add any new block-setup opcodes that
2987 are not try/except/finally handlers, you may need
2988 to update the PyGen_NeedsFinalizing() function.
2989 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002990
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002991 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002992 STACK_LEVEL());
2993 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002994 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002995
Benjamin Petersonddd19492018-09-16 22:38:02 -07002996 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04002997 _Py_IDENTIFIER(__aexit__);
2998 _Py_IDENTIFIER(__aenter__);
2999
3000 PyObject *mgr = TOP();
3001 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
3002 *enter;
3003 PyObject *res;
3004 if (exit == NULL)
3005 goto error;
3006 SET_TOP(exit);
3007 enter = special_lookup(mgr, &PyId___aenter__);
3008 Py_DECREF(mgr);
3009 if (enter == NULL)
3010 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003011 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003012 Py_DECREF(enter);
3013 if (res == NULL)
3014 goto error;
3015 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003016 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003017 DISPATCH();
3018 }
3019
Benjamin Petersonddd19492018-09-16 22:38:02 -07003020 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003021 PyObject *res = POP();
3022 /* Setup the finally block before pushing the result
3023 of __aenter__ on the stack. */
3024 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3025 STACK_LEVEL());
3026 PUSH(res);
3027 DISPATCH();
3028 }
3029
Benjamin Petersonddd19492018-09-16 22:38:02 -07003030 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003031 _Py_IDENTIFIER(__exit__);
3032 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003033 PyObject *mgr = TOP();
Raymond Hettingera3fec152016-11-21 17:24:23 -08003034 PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003035 PyObject *res;
Raymond Hettingera3fec152016-11-21 17:24:23 -08003036 if (enter == NULL)
3037 goto error;
3038 exit = special_lookup(mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003039 if (exit == NULL) {
3040 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003041 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003042 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003043 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003044 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003045 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003046 Py_DECREF(enter);
3047 if (res == NULL)
3048 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003049 /* Setup the finally block before pushing the result
3050 of __enter__ on the stack. */
3051 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3052 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003053
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003054 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003055 DISPATCH();
3056 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003057
Benjamin Petersonddd19492018-09-16 22:38:02 -07003058 case TARGET(WITH_CLEANUP_START): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003059 /* At the top of the stack are 1 or 6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003060 how/why we entered the finally clause:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003061 - TOP = NULL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003062 - (TOP, SECOND, THIRD) = exc_info()
3063 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003064 Below them is EXIT, the context.__exit__ or context.__aexit__
3065 bound method.
3066 In the first case, we must call
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003067 EXIT(None, None, None)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003068 otherwise we must call
3069 EXIT(TOP, SECOND, THIRD)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003070
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003071 In the first case, we remove EXIT from the
3072 stack, leaving TOP, and push TOP on the stack.
3073 Otherwise we shift the bottom 3 values of the
3074 stack down, replace the empty spot with NULL, and push
3075 None on the stack.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003076
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003077 Finally we push the result of the call.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003078 */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003079 PyObject *stack[3];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003080 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003081 PyObject *exc, *val, *tb, *res;
3082
3083 val = tb = Py_None;
3084 exc = TOP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003085 if (exc == NULL) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003086 STACK_SHRINK(1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003087 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003088 SET_TOP(exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003089 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003090 }
3091 else {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003092 assert(PyExceptionClass_Check(exc));
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003093 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003094 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003095 val = SECOND();
3096 tb = THIRD();
3097 tp2 = FOURTH();
3098 exc2 = PEEK(5);
3099 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003100 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003101 SET_VALUE(7, tb2);
3102 SET_VALUE(6, exc2);
3103 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003104 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3105 SET_FOURTH(NULL);
3106 /* We just shifted the stack down, so we have
3107 to tell the except handler block that the
3108 values are lower than it expects. */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003109 assert(f->f_iblock > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003110 block = &f->f_blockstack[f->f_iblock - 1];
3111 assert(block->b_type == EXCEPT_HANDLER);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003112 assert(block->b_level > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003113 block->b_level--;
3114 }
Victor Stinner842cfff2016-12-01 14:45:31 +01003115
3116 stack[0] = exc;
3117 stack[1] = val;
3118 stack[2] = tb;
3119 res = _PyObject_FastCall(exit_func, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003120 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003121 if (res == NULL)
3122 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003123
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003124 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003125 PUSH(exc);
3126 PUSH(res);
3127 PREDICT(WITH_CLEANUP_FINISH);
3128 DISPATCH();
3129 }
3130
Benjamin Petersonddd19492018-09-16 22:38:02 -07003131 case TARGET(WITH_CLEANUP_FINISH): {
3132 PREDICTED(WITH_CLEANUP_FINISH);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003133 /* TOP = the result of calling the context.__exit__ bound method
3134 SECOND = either None or exception type
3135
3136 If SECOND is None below is NULL or the return address,
3137 otherwise below are 7 values representing an exception.
3138 */
Yury Selivanov75445082015-05-11 22:57:16 -04003139 PyObject *res = POP();
3140 PyObject *exc = POP();
3141 int err;
3142
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003143 if (exc != Py_None)
3144 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003145 else
3146 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003147
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003148 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003149 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003151 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003152 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003153 else if (err > 0) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003154 /* There was an exception and a True return.
3155 * We must manually unwind the EXCEPT_HANDLER block
3156 * which was created when the exception was caught,
Quan Tian3bd0d622018-10-20 05:30:03 +08003157 * otherwise the stack will be in an inconsistent state.
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003158 */
3159 PyTryBlock *b = PyFrame_BlockPop(f);
3160 assert(b->b_type == EXCEPT_HANDLER);
3161 UNWIND_EXCEPT_HANDLER(b);
3162 PUSH(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003163 }
3164 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003165 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003166 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003167
Benjamin Petersonddd19492018-09-16 22:38:02 -07003168 case TARGET(LOAD_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003169 /* Designed to work in tamdem with CALL_METHOD. */
3170 PyObject *name = GETITEM(names, oparg);
3171 PyObject *obj = TOP();
3172 PyObject *meth = NULL;
3173
3174 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3175
Yury Selivanovf2392132016-12-13 19:03:51 -05003176 if (meth == NULL) {
3177 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003178 goto error;
3179 }
3180
3181 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003182 /* We can bypass temporary bound method object.
3183 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003184
INADA Naoki015bce62017-01-16 17:23:30 +09003185 meth | self | arg1 | ... | argN
3186 */
3187 SET_TOP(meth);
3188 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003189 }
3190 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003191 /* meth is not an unbound method (but a regular attr, or
3192 something was returned by a descriptor protocol). Set
3193 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003194 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003195
3196 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003197 */
INADA Naoki015bce62017-01-16 17:23:30 +09003198 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003199 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003200 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003201 }
3202 DISPATCH();
3203 }
3204
Benjamin Petersonddd19492018-09-16 22:38:02 -07003205 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003206 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003207 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003208
3209 sp = stack_pointer;
3210
INADA Naoki015bce62017-01-16 17:23:30 +09003211 meth = PEEK(oparg + 2);
3212 if (meth == NULL) {
3213 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3214 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003215
3216 Stack layout:
3217
INADA Naoki015bce62017-01-16 17:23:30 +09003218 ... | NULL | callable | arg1 | ... | argN
3219 ^- TOP()
3220 ^- (-oparg)
3221 ^- (-oparg-1)
3222 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003223
Ville Skyttä49b27342017-08-03 09:00:59 +03003224 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003225 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003226 */
Yury Selivanovf2392132016-12-13 19:03:51 -05003227 res = call_function(&sp, oparg, NULL);
3228 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003229 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003230 }
3231 else {
3232 /* This is a method call. Stack layout:
3233
INADA Naoki015bce62017-01-16 17:23:30 +09003234 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003235 ^- TOP()
3236 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003237 ^- (-oparg-1)
3238 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003239
INADA Naoki015bce62017-01-16 17:23:30 +09003240 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003241 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003242 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003243 */
3244 res = call_function(&sp, oparg + 1, NULL);
3245 stack_pointer = sp;
3246 }
3247
3248 PUSH(res);
3249 if (res == NULL)
3250 goto error;
3251 DISPATCH();
3252 }
3253
Benjamin Petersonddd19492018-09-16 22:38:02 -07003254 case TARGET(CALL_FUNCTION): {
3255 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003256 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003257 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003258 res = call_function(&sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003259 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003260 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003261 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003262 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003263 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003264 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003265 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003266
Benjamin Petersonddd19492018-09-16 22:38:02 -07003267 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003268 PyObject **sp, *res, *names;
3269
3270 names = POP();
3271 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003272 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003273 res = call_function(&sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003274 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003275 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003276 Py_DECREF(names);
3277
3278 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003279 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003280 }
3281 DISPATCH();
3282 }
3283
Benjamin Petersonddd19492018-09-16 22:38:02 -07003284 case TARGET(CALL_FUNCTION_EX): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003285 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003286 if (oparg & 0x01) {
3287 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003288 if (!PyDict_CheckExact(kwargs)) {
3289 PyObject *d = PyDict_New();
3290 if (d == NULL)
3291 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003292 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003293 Py_DECREF(d);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003294 format_kwargs_error(SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003295 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003296 goto error;
3297 }
3298 Py_DECREF(kwargs);
3299 kwargs = d;
3300 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003301 assert(PyDict_CheckExact(kwargs));
3302 }
3303 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003304 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003305 if (!PyTuple_CheckExact(callargs)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003306 if (check_args_iterable(func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003307 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003308 goto error;
3309 }
3310 Py_SETREF(callargs, PySequence_Tuple(callargs));
3311 if (callargs == NULL) {
3312 goto error;
3313 }
3314 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003315 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003316
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003317 result = do_call_core(func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003318 Py_DECREF(func);
3319 Py_DECREF(callargs);
3320 Py_XDECREF(kwargs);
3321
3322 SET_TOP(result);
3323 if (result == NULL) {
3324 goto error;
3325 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003326 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003327 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003328
Benjamin Petersonddd19492018-09-16 22:38:02 -07003329 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003330 PyObject *qualname = POP();
3331 PyObject *codeobj = POP();
3332 PyFunctionObject *func = (PyFunctionObject *)
3333 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003334
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003335 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003336 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003337 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003338 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003339 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003340
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003341 if (oparg & 0x08) {
3342 assert(PyTuple_CheckExact(TOP()));
3343 func ->func_closure = POP();
3344 }
3345 if (oparg & 0x04) {
3346 assert(PyDict_CheckExact(TOP()));
3347 func->func_annotations = POP();
3348 }
3349 if (oparg & 0x02) {
3350 assert(PyDict_CheckExact(TOP()));
3351 func->func_kwdefaults = POP();
3352 }
3353 if (oparg & 0x01) {
3354 assert(PyTuple_CheckExact(TOP()));
3355 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003356 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003357
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003358 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003359 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003360 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003361
Benjamin Petersonddd19492018-09-16 22:38:02 -07003362 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003363 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003364 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003365 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003366 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003367 step = NULL;
3368 stop = POP();
3369 start = TOP();
3370 slice = PySlice_New(start, stop, step);
3371 Py_DECREF(start);
3372 Py_DECREF(stop);
3373 Py_XDECREF(step);
3374 SET_TOP(slice);
3375 if (slice == NULL)
3376 goto error;
3377 DISPATCH();
3378 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003379
Benjamin Petersonddd19492018-09-16 22:38:02 -07003380 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003381 /* Handles f-string value formatting. */
3382 PyObject *result;
3383 PyObject *fmt_spec;
3384 PyObject *value;
3385 PyObject *(*conv_fn)(PyObject *);
3386 int which_conversion = oparg & FVC_MASK;
3387 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3388
3389 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003390 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003391
3392 /* See if any conversion is specified. */
3393 switch (which_conversion) {
3394 case FVC_STR: conv_fn = PyObject_Str; break;
3395 case FVC_REPR: conv_fn = PyObject_Repr; break;
3396 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3397
3398 /* Must be 0 (meaning no conversion), since only four
3399 values are allowed by (oparg & FVC_MASK). */
3400 default: conv_fn = NULL; break;
3401 }
3402
3403 /* If there's a conversion function, call it and replace
3404 value with that result. Otherwise, just use value,
3405 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003406 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003407 result = conv_fn(value);
3408 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003409 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003410 Py_XDECREF(fmt_spec);
3411 goto error;
3412 }
3413 value = result;
3414 }
3415
3416 /* If value is a unicode object, and there's no fmt_spec,
3417 then we know the result of format(value) is value
3418 itself. In that case, skip calling format(). I plan to
3419 move this optimization in to PyObject_Format()
3420 itself. */
3421 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3422 /* Do nothing, just transfer ownership to result. */
3423 result = value;
3424 } else {
3425 /* Actually call format(). */
3426 result = PyObject_Format(value, fmt_spec);
3427 Py_DECREF(value);
3428 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003429 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003430 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003431 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003432 }
3433
Eric V. Smith135d5f42016-02-05 18:23:08 -05003434 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003435 DISPATCH();
3436 }
3437
Benjamin Petersonddd19492018-09-16 22:38:02 -07003438 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003439 int oldoparg = oparg;
3440 NEXTOPARG();
3441 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003442 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003443 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003444
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003445
Antoine Pitrou042b1282010-08-13 21:15:58 +00003446#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003447 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003448#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003449 default:
3450 fprintf(stderr,
3451 "XXX lineno: %d, opcode: %d\n",
3452 PyFrame_GetLineNumber(f),
3453 opcode);
3454 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003455 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003457 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003458
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003459 /* This should never be reached. Every opcode should end with DISPATCH()
3460 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003461 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003462
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003463error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003464 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003465#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003466 if (!PyErr_Occurred())
3467 PyErr_SetString(PyExc_SystemError,
3468 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003469#else
3470 assert(PyErr_Occurred());
3471#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003472
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003473 /* Log traceback info. */
3474 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003475
Benjamin Peterson51f46162013-01-23 08:38:47 -05003476 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003477 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3478 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003479
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003480exception_unwind:
3481 /* Unwind stacks if an exception occurred */
3482 while (f->f_iblock > 0) {
3483 /* Pop the current block. */
3484 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003486 if (b->b_type == EXCEPT_HANDLER) {
3487 UNWIND_EXCEPT_HANDLER(b);
3488 continue;
3489 }
3490 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003491 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003492 PyObject *exc, *val, *tb;
3493 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003494 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003495 /* Beware, this invalidates all b->b_* fields */
3496 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003497 PUSH(exc_info->exc_traceback);
3498 PUSH(exc_info->exc_value);
3499 if (exc_info->exc_type != NULL) {
3500 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003501 }
3502 else {
3503 Py_INCREF(Py_None);
3504 PUSH(Py_None);
3505 }
3506 PyErr_Fetch(&exc, &val, &tb);
3507 /* Make the raw exception data
3508 available to the handler,
3509 so a program can emulate the
3510 Python main loop. */
3511 PyErr_NormalizeException(
3512 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003513 if (tb != NULL)
3514 PyException_SetTraceback(val, tb);
3515 else
3516 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003517 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003518 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003519 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003520 exc_info->exc_value = val;
3521 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003522 if (tb == NULL)
3523 tb = Py_None;
3524 Py_INCREF(tb);
3525 PUSH(tb);
3526 PUSH(val);
3527 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003528 JUMPTO(handler);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003529 /* Resume normal execution */
3530 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003531 }
3532 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003533
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003534 /* End the loop as we still have an error */
3535 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003536 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003538 /* Pop remaining stack entries. */
3539 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003540 PyObject *o = POP();
3541 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003542 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003543
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003544 assert(retval == NULL);
3545 assert(PyErr_Occurred());
Guido van Rossumac7be682001-01-17 15:42:30 +00003546
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003547return_or_yield:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003548 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003549 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003550 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3551 tstate, f, PyTrace_RETURN, retval)) {
3552 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003553 }
3554 }
3555 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003556 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3557 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003558 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003559 }
3560 }
3561 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003563 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003564exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003565 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3566 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003567 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003568 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003569 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003570
Victor Stinnerefde1462015-03-21 15:04:43 +01003571 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003572}
3573
Benjamin Petersonb204a422011-06-05 22:04:07 -05003574static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003575format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3576{
3577 int err;
3578 Py_ssize_t len = PyList_GET_SIZE(names);
3579 PyObject *name_str, *comma, *tail, *tmp;
3580
3581 assert(PyList_CheckExact(names));
3582 assert(len >= 1);
3583 /* Deal with the joys of natural language. */
3584 switch (len) {
3585 case 1:
3586 name_str = PyList_GET_ITEM(names, 0);
3587 Py_INCREF(name_str);
3588 break;
3589 case 2:
3590 name_str = PyUnicode_FromFormat("%U and %U",
3591 PyList_GET_ITEM(names, len - 2),
3592 PyList_GET_ITEM(names, len - 1));
3593 break;
3594 default:
3595 tail = PyUnicode_FromFormat(", %U, and %U",
3596 PyList_GET_ITEM(names, len - 2),
3597 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003598 if (tail == NULL)
3599 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003600 /* Chop off the last two objects in the list. This shouldn't actually
3601 fail, but we can't be too careful. */
3602 err = PyList_SetSlice(names, len - 2, len, NULL);
3603 if (err == -1) {
3604 Py_DECREF(tail);
3605 return;
3606 }
3607 /* Stitch everything up into a nice comma-separated list. */
3608 comma = PyUnicode_FromString(", ");
3609 if (comma == NULL) {
3610 Py_DECREF(tail);
3611 return;
3612 }
3613 tmp = PyUnicode_Join(comma, names);
3614 Py_DECREF(comma);
3615 if (tmp == NULL) {
3616 Py_DECREF(tail);
3617 return;
3618 }
3619 name_str = PyUnicode_Concat(tmp, tail);
3620 Py_DECREF(tmp);
3621 Py_DECREF(tail);
3622 break;
3623 }
3624 if (name_str == NULL)
3625 return;
3626 PyErr_Format(PyExc_TypeError,
3627 "%U() missing %i required %s argument%s: %U",
3628 co->co_name,
3629 len,
3630 kind,
3631 len == 1 ? "" : "s",
3632 name_str);
3633 Py_DECREF(name_str);
3634}
3635
3636static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003637missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003638 PyObject **fastlocals)
3639{
Victor Stinner74319ae2016-08-25 00:04:09 +02003640 Py_ssize_t i, j = 0;
3641 Py_ssize_t start, end;
3642 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003643 const char *kind = positional ? "positional" : "keyword-only";
3644 PyObject *missing_names;
3645
3646 /* Compute the names of the arguments that are missing. */
3647 missing_names = PyList_New(missing);
3648 if (missing_names == NULL)
3649 return;
3650 if (positional) {
3651 start = 0;
3652 end = co->co_argcount - defcount;
3653 }
3654 else {
3655 start = co->co_argcount;
3656 end = start + co->co_kwonlyargcount;
3657 }
3658 for (i = start; i < end; i++) {
3659 if (GETLOCAL(i) == NULL) {
3660 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3661 PyObject *name = PyObject_Repr(raw);
3662 if (name == NULL) {
3663 Py_DECREF(missing_names);
3664 return;
3665 }
3666 PyList_SET_ITEM(missing_names, j++, name);
3667 }
3668 }
3669 assert(j == missing);
3670 format_missing(kind, co, missing_names);
3671 Py_DECREF(missing_names);
3672}
3673
3674static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003675too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3676 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003677{
3678 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003679 Py_ssize_t kwonly_given = 0;
3680 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003681 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003682 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003683
Benjamin Petersone109c702011-06-24 09:37:26 -05003684 assert((co->co_flags & CO_VARARGS) == 0);
3685 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003686 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3687 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003688 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003689 }
3690 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003691 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003692 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003693 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003694 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003695 }
3696 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003697 plural = (co_argcount != 1);
3698 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003699 }
3700 if (sig == NULL)
3701 return;
3702 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003703 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3704 kwonly_sig = PyUnicode_FromFormat(format,
3705 given != 1 ? "s" : "",
3706 kwonly_given,
3707 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003708 if (kwonly_sig == NULL) {
3709 Py_DECREF(sig);
3710 return;
3711 }
3712 }
3713 else {
3714 /* This will not fail. */
3715 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003716 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003717 }
3718 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003719 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003720 co->co_name,
3721 sig,
3722 plural ? "s" : "",
3723 given,
3724 kwonly_sig,
3725 given == 1 && !kwonly_given ? "was" : "were");
3726 Py_DECREF(sig);
3727 Py_DECREF(kwonly_sig);
3728}
3729
Guido van Rossumc2e20742006-02-27 22:32:47 +00003730/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02003731 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003732 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003733
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003734PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +02003735_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003736 PyObject *const *args, Py_ssize_t argcount,
3737 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003738 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003739 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003740 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003741 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003742{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003743 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003744 PyFrameObject *f;
3745 PyObject *retval = NULL;
3746 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003747 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003748 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003749 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3750 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003751 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003753 if (globals == NULL) {
3754 PyErr_SetString(PyExc_SystemError,
3755 "PyEval_EvalCodeEx: NULL globals");
3756 return NULL;
3757 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003758
Victor Stinnerc7020012016-08-16 23:40:29 +02003759 /* Create the frame */
Victor Stinner50b48572018-11-01 01:51:40 +01003760 tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003761 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003762 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003763 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003764 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003765 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003766 fastlocals = f->f_localsplus;
3767 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003768
Victor Stinnerc7020012016-08-16 23:40:29 +02003769 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003770 if (co->co_flags & CO_VARKEYWORDS) {
3771 kwdict = PyDict_New();
3772 if (kwdict == NULL)
3773 goto fail;
3774 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003775 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003776 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003777 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003778 SETLOCAL(i, kwdict);
3779 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003780 else {
3781 kwdict = NULL;
3782 }
3783
3784 /* Copy positional arguments into local variables */
3785 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003786 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003787 }
3788 else {
3789 n = argcount;
3790 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003791 for (i = 0; i < n; i++) {
3792 x = args[i];
3793 Py_INCREF(x);
3794 SETLOCAL(i, x);
3795 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003796
3797 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003798 if (co->co_flags & CO_VARARGS) {
Sergey Fedoseev234531b2019-02-25 21:59:12 +05003799 u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003800 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003801 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003802 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003803 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003804 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003805
Serhiy Storchakab7281052016-09-12 00:52:40 +03003806 /* Handle keyword arguments passed as two strided arrays */
3807 kwcount *= kwstep;
3808 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003809 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003810 PyObject *keyword = kwnames[i];
3811 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003812 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003813
Benjamin Petersonb204a422011-06-05 22:04:07 -05003814 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3815 PyErr_Format(PyExc_TypeError,
3816 "%U() keywords must be strings",
3817 co->co_name);
3818 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003819 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003820
Benjamin Petersonb204a422011-06-05 22:04:07 -05003821 /* Speed hack: do raw pointer compares. As names are
3822 normally interned this should almost always hit. */
3823 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3824 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003825 PyObject *name = co_varnames[j];
3826 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003827 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003828 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003829 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003830
Benjamin Petersonb204a422011-06-05 22:04:07 -05003831 /* Slow fallback, just in case */
3832 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003833 PyObject *name = co_varnames[j];
3834 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
3835 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003836 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003837 }
3838 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003839 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003840 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003841 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003842
Victor Stinner231d1f32017-01-11 02:12:06 +01003843 assert(j >= total_args);
3844 if (kwdict == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003845 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003846 "%U() got an unexpected keyword argument '%S'",
3847 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003848 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003849 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003850
Christian Heimes0bd447f2013-07-20 14:48:10 +02003851 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3852 goto fail;
3853 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003854 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02003855
Benjamin Petersonb204a422011-06-05 22:04:07 -05003856 kw_found:
3857 if (GETLOCAL(j) != NULL) {
3858 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003859 "%U() got multiple values for argument '%S'",
3860 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003861 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003862 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003863 Py_INCREF(value);
3864 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003865 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003866
3867 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003868 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003869 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003870 goto fail;
3871 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003872
3873 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003874 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02003875 Py_ssize_t m = co->co_argcount - defcount;
3876 Py_ssize_t missing = 0;
3877 for (i = argcount; i < m; i++) {
3878 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003879 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02003880 }
3881 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003882 if (missing) {
3883 missing_arguments(co, missing, defcount, fastlocals);
3884 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003885 }
3886 if (n > m)
3887 i = n - m;
3888 else
3889 i = 0;
3890 for (; i < defcount; i++) {
3891 if (GETLOCAL(m+i) == NULL) {
3892 PyObject *def = defs[i];
3893 Py_INCREF(def);
3894 SETLOCAL(m+i, def);
3895 }
3896 }
3897 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003898
3899 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003900 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02003901 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003902 for (i = co->co_argcount; i < total_args; i++) {
3903 PyObject *name;
3904 if (GETLOCAL(i) != NULL)
3905 continue;
3906 name = PyTuple_GET_ITEM(co->co_varnames, i);
3907 if (kwdefs != NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003908 PyObject *def = PyDict_GetItemWithError(kwdefs, name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003909 if (def) {
3910 Py_INCREF(def);
3911 SETLOCAL(i, def);
3912 continue;
3913 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003914 else if (PyErr_Occurred()) {
3915 goto fail;
3916 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003917 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003918 missing++;
3919 }
3920 if (missing) {
3921 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003922 goto fail;
3923 }
3924 }
3925
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003926 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003927 vars into frame. */
3928 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003929 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02003930 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05003931 /* Possibly account for the cell variable being an argument. */
3932 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07003933 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05003934 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05003935 /* Clear the local copy. */
3936 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003937 }
3938 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05003939 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003940 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05003941 if (c == NULL)
3942 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05003943 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003944 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003945
3946 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05003947 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3948 PyObject *o = PyTuple_GET_ITEM(closure, i);
3949 Py_INCREF(o);
3950 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003951 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003952
Yury Selivanoveb636452016-09-08 22:01:51 -07003953 /* Handle generator/coroutine/asynchronous generator */
3954 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003955 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04003956 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003957 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04003958
3959 if (is_coro && tstate->in_coroutine_wrapper) {
3960 assert(coro_wrapper != NULL);
3961 PyErr_Format(PyExc_RuntimeError,
3962 "coroutine wrapper %.200R attempted "
3963 "to recursively wrap %.200R",
3964 coro_wrapper,
3965 co);
3966 goto fail;
3967 }
Yury Selivanov75445082015-05-11 22:57:16 -04003968
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003969 /* Don't need to keep the reference to f_back, it will be set
3970 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003971 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003973 /* Create a new generator that owns the ready to run frame
3974 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04003975 if (is_coro) {
3976 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07003977 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
3978 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003979 } else {
3980 gen = PyGen_NewWithQualName(f, name, qualname);
3981 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003982 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003983 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003984 }
INADA Naoki9c157762016-12-26 18:52:46 +09003985
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003986 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04003987
Yury Selivanov94c22632015-06-04 10:16:51 -04003988 if (is_coro && coro_wrapper != NULL) {
3989 PyObject *wrapped;
3990 tstate->in_coroutine_wrapper = 1;
3991 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
3992 tstate->in_coroutine_wrapper = 0;
3993 return wrapped;
3994 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04003995
Yury Selivanov75445082015-05-11 22:57:16 -04003996 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003997 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003998
Victor Stinner59a73272016-12-09 18:51:13 +01003999 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004000
Thomas Woutersce272b62007-09-19 21:19:28 +00004001fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004002
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004003 /* decref'ing the frame can cause __del__ methods to get invoked,
4004 which can call back into Python. While we're done with the
4005 current Python frame (f), the associated C stack is still in use,
4006 so recursion_depth must be boosted for the duration.
4007 */
4008 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09004009 if (Py_REFCNT(f) > 1) {
4010 Py_DECREF(f);
4011 _PyObject_GC_TRACK(f);
4012 }
4013 else {
4014 ++tstate->recursion_depth;
4015 Py_DECREF(f);
4016 --tstate->recursion_depth;
4017 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004018 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004019}
4020
Victor Stinner40ee3012014-06-16 15:59:28 +02004021PyObject *
4022PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004023 PyObject *const *args, int argcount,
4024 PyObject *const *kws, int kwcount,
4025 PyObject *const *defs, int defcount,
4026 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004027{
4028 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004029 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004030 kws, kws != NULL ? kws + 1 : NULL,
4031 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004032 defs, defcount,
4033 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004034 NULL, NULL);
4035}
Tim Peters5ca576e2001-06-18 22:08:13 +00004036
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004037static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05004038special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004039{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004040 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004041 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004042 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05004043 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004044 return NULL;
4045 }
4046 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004047}
4048
4049
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004050/* Logic for the raise statement (too complicated for inlining).
4051 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004052static int
Collin Winter828f04a2007-08-31 00:04:24 +00004053do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004054{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004055 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004057 if (exc == NULL) {
4058 /* Reraise */
Victor Stinner50b48572018-11-01 01:51:40 +01004059 PyThreadState *tstate = _PyThreadState_GET();
Mark Shannonae3087c2017-10-22 22:41:51 +01004060 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004061 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004062 type = exc_info->exc_type;
4063 value = exc_info->exc_value;
4064 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004065 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004066 PyErr_SetString(PyExc_RuntimeError,
4067 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004068 return 0;
4069 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004070 Py_XINCREF(type);
4071 Py_XINCREF(value);
4072 Py_XINCREF(tb);
4073 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004074 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004075 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004076
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004077 /* We support the following forms of raise:
4078 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004079 raise <instance>
4080 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004081
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004082 if (PyExceptionClass_Check(exc)) {
4083 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004084 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004085 if (value == NULL)
4086 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004087 if (!PyExceptionInstance_Check(value)) {
4088 PyErr_Format(PyExc_TypeError,
4089 "calling %R should have returned an instance of "
4090 "BaseException, not %R",
4091 type, Py_TYPE(value));
4092 goto raise_error;
4093 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004094 }
4095 else if (PyExceptionInstance_Check(exc)) {
4096 value = exc;
4097 type = PyExceptionInstance_Class(exc);
4098 Py_INCREF(type);
4099 }
4100 else {
4101 /* Not something you can raise. You get an exception
4102 anyway, just not what you specified :-) */
4103 Py_DECREF(exc);
4104 PyErr_SetString(PyExc_TypeError,
4105 "exceptions must derive from BaseException");
4106 goto raise_error;
4107 }
Collin Winter828f04a2007-08-31 00:04:24 +00004108
Serhiy Storchakac0191582016-09-27 11:37:10 +03004109 assert(type != NULL);
4110 assert(value != NULL);
4111
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004112 if (cause) {
4113 PyObject *fixed_cause;
4114 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004115 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004116 if (fixed_cause == NULL)
4117 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004118 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004119 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004120 else if (PyExceptionInstance_Check(cause)) {
4121 fixed_cause = cause;
4122 }
4123 else if (cause == Py_None) {
4124 Py_DECREF(cause);
4125 fixed_cause = NULL;
4126 }
4127 else {
4128 PyErr_SetString(PyExc_TypeError,
4129 "exception causes must derive from "
4130 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004131 goto raise_error;
4132 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004133 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004134 }
Collin Winter828f04a2007-08-31 00:04:24 +00004135
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004136 PyErr_SetObject(type, value);
4137 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004138 Py_DECREF(value);
4139 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004140 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004141
4142raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004143 Py_XDECREF(value);
4144 Py_XDECREF(type);
4145 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004146 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004147}
4148
Tim Petersd6d010b2001-06-21 02:49:55 +00004149/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004150 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004151
Guido van Rossum0368b722007-05-11 16:50:42 +00004152 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4153 with a variable target.
4154*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004155
Barry Warsawe42b18f1997-08-25 22:13:04 +00004156static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004157unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004158{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004159 int i = 0, j = 0;
4160 Py_ssize_t ll = 0;
4161 PyObject *it; /* iter(v) */
4162 PyObject *w;
4163 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004165 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004167 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004168 if (it == NULL) {
4169 if (PyErr_ExceptionMatches(PyExc_TypeError) &&
4170 v->ob_type->tp_iter == NULL && !PySequence_Check(v))
4171 {
4172 PyErr_Format(PyExc_TypeError,
4173 "cannot unpack non-iterable %.200s object",
4174 v->ob_type->tp_name);
4175 }
4176 return 0;
4177 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004178
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004179 for (; i < argcnt; i++) {
4180 w = PyIter_Next(it);
4181 if (w == NULL) {
4182 /* Iterator done, via error or exhaustion. */
4183 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004184 if (argcntafter == -1) {
4185 PyErr_Format(PyExc_ValueError,
4186 "not enough values to unpack (expected %d, got %d)",
4187 argcnt, i);
4188 }
4189 else {
4190 PyErr_Format(PyExc_ValueError,
4191 "not enough values to unpack "
4192 "(expected at least %d, got %d)",
4193 argcnt + argcntafter, i);
4194 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004195 }
4196 goto Error;
4197 }
4198 *--sp = w;
4199 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004201 if (argcntafter == -1) {
4202 /* We better have exhausted the iterator now. */
4203 w = PyIter_Next(it);
4204 if (w == NULL) {
4205 if (PyErr_Occurred())
4206 goto Error;
4207 Py_DECREF(it);
4208 return 1;
4209 }
4210 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004211 PyErr_Format(PyExc_ValueError,
4212 "too many values to unpack (expected %d)",
4213 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004214 goto Error;
4215 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004217 l = PySequence_List(it);
4218 if (l == NULL)
4219 goto Error;
4220 *--sp = l;
4221 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004223 ll = PyList_GET_SIZE(l);
4224 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004225 PyErr_Format(PyExc_ValueError,
4226 "not enough values to unpack (expected at least %d, got %zd)",
4227 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004228 goto Error;
4229 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004230
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004231 /* Pop the "after-variable" args off the list. */
4232 for (j = argcntafter; j > 0; j--, i++) {
4233 *--sp = PyList_GET_ITEM(l, ll - j);
4234 }
4235 /* Resize the list. */
4236 Py_SIZE(l) = ll - argcntafter;
4237 Py_DECREF(it);
4238 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004239
Tim Petersd6d010b2001-06-21 02:49:55 +00004240Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004241 for (; i > 0; i--, sp++)
4242 Py_DECREF(*sp);
4243 Py_XDECREF(it);
4244 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004245}
4246
4247
Guido van Rossum96a42c81992-01-12 02:29:51 +00004248#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004249static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004250prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004251{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004252 printf("%s ", str);
4253 if (PyObject_Print(v, stdout, 0) != 0)
4254 PyErr_Clear(); /* Don't know what else to do */
4255 printf("\n");
4256 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004257}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004258#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004259
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004260static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004261call_exc_trace(Py_tracefunc func, PyObject *self,
4262 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004263{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004264 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004265 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004266 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004267 if (value == NULL) {
4268 value = Py_None;
4269 Py_INCREF(value);
4270 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004271 PyErr_NormalizeException(&type, &value, &orig_traceback);
4272 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004273 arg = PyTuple_Pack(3, type, value, traceback);
4274 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004275 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004276 return;
4277 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004278 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004279 Py_DECREF(arg);
4280 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004281 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004282 else {
4283 Py_XDECREF(type);
4284 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004285 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004286 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004287}
4288
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004289static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004290call_trace_protected(Py_tracefunc func, PyObject *obj,
4291 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004292 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004293{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004294 PyObject *type, *value, *traceback;
4295 int err;
4296 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004297 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004298 if (err == 0)
4299 {
4300 PyErr_Restore(type, value, traceback);
4301 return 0;
4302 }
4303 else {
4304 Py_XDECREF(type);
4305 Py_XDECREF(value);
4306 Py_XDECREF(traceback);
4307 return -1;
4308 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004309}
4310
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004311static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004312call_trace(Py_tracefunc func, PyObject *obj,
4313 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004314 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004315{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004316 int result;
4317 if (tstate->tracing)
4318 return 0;
4319 tstate->tracing++;
4320 tstate->use_tracing = 0;
4321 result = func(obj, frame, what, arg);
4322 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4323 || (tstate->c_profilefunc != NULL));
4324 tstate->tracing--;
4325 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004326}
4327
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004328PyObject *
4329_PyEval_CallTracing(PyObject *func, PyObject *args)
4330{
Victor Stinner50b48572018-11-01 01:51:40 +01004331 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004332 int save_tracing = tstate->tracing;
4333 int save_use_tracing = tstate->use_tracing;
4334 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004336 tstate->tracing = 0;
4337 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4338 || (tstate->c_profilefunc != NULL));
4339 result = PyObject_Call(func, args, NULL);
4340 tstate->tracing = save_tracing;
4341 tstate->use_tracing = save_use_tracing;
4342 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004343}
4344
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004345/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004346static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004347maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004348 PyThreadState *tstate, PyFrameObject *frame,
4349 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004350{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004351 int result = 0;
4352 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004354 /* If the last instruction executed isn't in the current
4355 instruction window, reset the window.
4356 */
4357 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4358 PyAddrPair bounds;
4359 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4360 &bounds);
4361 *instr_lb = bounds.ap_lower;
4362 *instr_ub = bounds.ap_upper;
4363 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004364 /* If the last instruction falls at the start of a line or if it
4365 represents a jump backwards, update the frame's line number and
4366 then call the trace function if we're tracing source lines.
4367 */
4368 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004369 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004370 if (frame->f_trace_lines) {
4371 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4372 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004373 }
George King20faa682017-10-18 17:44:22 -07004374 /* Always emit an opcode event if we're tracing all opcodes. */
4375 if (frame->f_trace_opcodes) {
4376 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4377 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004378 *instr_prev = frame->f_lasti;
4379 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004380}
4381
Fred Drake5755ce62001-06-27 19:19:46 +00004382void
4383PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004384{
Victor Stinner50b48572018-11-01 01:51:40 +01004385 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004386 PyObject *temp = tstate->c_profileobj;
4387 Py_XINCREF(arg);
4388 tstate->c_profilefunc = NULL;
4389 tstate->c_profileobj = NULL;
4390 /* Must make sure that tracing is not ignored if 'temp' is freed */
4391 tstate->use_tracing = tstate->c_tracefunc != NULL;
4392 Py_XDECREF(temp);
4393 tstate->c_profilefunc = func;
4394 tstate->c_profileobj = arg;
4395 /* Flag that tracing or profiling is turned on */
4396 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004397}
4398
4399void
4400PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4401{
Victor Stinner50b48572018-11-01 01:51:40 +01004402 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004403 PyObject *temp = tstate->c_traceobj;
4404 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4405 Py_XINCREF(arg);
4406 tstate->c_tracefunc = NULL;
4407 tstate->c_traceobj = NULL;
4408 /* Must make sure that profiling is not ignored if 'temp' is freed */
4409 tstate->use_tracing = tstate->c_profilefunc != NULL;
4410 Py_XDECREF(temp);
4411 tstate->c_tracefunc = func;
4412 tstate->c_traceobj = arg;
4413 /* Flag that tracing or profiling is turned on */
4414 tstate->use_tracing = ((func != NULL)
4415 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004416}
4417
Yury Selivanov75445082015-05-11 22:57:16 -04004418void
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004419_PyEval_SetCoroutineOriginTrackingDepth(int new_depth)
4420{
4421 assert(new_depth >= 0);
Victor Stinner50b48572018-11-01 01:51:40 +01004422 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004423 tstate->coroutine_origin_tracking_depth = new_depth;
4424}
4425
4426int
4427_PyEval_GetCoroutineOriginTrackingDepth(void)
4428{
Victor Stinner50b48572018-11-01 01:51:40 +01004429 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004430 return tstate->coroutine_origin_tracking_depth;
4431}
4432
4433void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004434_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004435{
Victor Stinner50b48572018-11-01 01:51:40 +01004436 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanov75445082015-05-11 22:57:16 -04004437
Yury Selivanov75445082015-05-11 22:57:16 -04004438 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004439 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004440}
4441
4442PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004443_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004444{
Victor Stinner50b48572018-11-01 01:51:40 +01004445 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanov75445082015-05-11 22:57:16 -04004446 return tstate->coroutine_wrapper;
4447}
4448
Yury Selivanoveb636452016-09-08 22:01:51 -07004449void
4450_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4451{
Victor Stinner50b48572018-11-01 01:51:40 +01004452 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004453
4454 Py_XINCREF(firstiter);
4455 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4456}
4457
4458PyObject *
4459_PyEval_GetAsyncGenFirstiter(void)
4460{
Victor Stinner50b48572018-11-01 01:51:40 +01004461 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004462 return tstate->async_gen_firstiter;
4463}
4464
4465void
4466_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4467{
Victor Stinner50b48572018-11-01 01:51:40 +01004468 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004469
4470 Py_XINCREF(finalizer);
4471 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4472}
4473
4474PyObject *
4475_PyEval_GetAsyncGenFinalizer(void)
4476{
Victor Stinner50b48572018-11-01 01:51:40 +01004477 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004478 return tstate->async_gen_finalizer;
4479}
4480
Guido van Rossumb209a111997-04-29 18:18:01 +00004481PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004482PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004483{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004484 PyFrameObject *current_frame = PyEval_GetFrame();
4485 if (current_frame == NULL)
Victor Stinnercaba55b2018-08-03 15:33:52 +02004486 return _PyInterpreterState_GET_UNSAFE()->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004487 else
4488 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004489}
4490
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004491/* Convenience function to get a builtin from its name */
4492PyObject *
4493_PyEval_GetBuiltinId(_Py_Identifier *name)
4494{
4495 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4496 if (attr) {
4497 Py_INCREF(attr);
4498 }
4499 else if (!PyErr_Occurred()) {
4500 PyErr_SetObject(PyExc_AttributeError, _PyUnicode_FromId(name));
4501 }
4502 return attr;
4503}
4504
Guido van Rossumb209a111997-04-29 18:18:01 +00004505PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004506PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004507{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004508 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004509 if (current_frame == NULL) {
4510 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004511 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004512 }
4513
4514 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4515 return NULL;
4516
4517 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004518 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004519}
4520
Guido van Rossumb209a111997-04-29 18:18:01 +00004521PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004522PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004523{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004524 PyFrameObject *current_frame = PyEval_GetFrame();
4525 if (current_frame == NULL)
4526 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004527
4528 assert(current_frame->f_globals != NULL);
4529 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004530}
4531
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004532PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004533PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004534{
Victor Stinner50b48572018-11-01 01:51:40 +01004535 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004536 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004537}
4538
Guido van Rossum6135a871995-01-09 17:53:26 +00004539int
Tim Peters5ba58662001-07-16 02:29:45 +00004540PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004541{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004542 PyFrameObject *current_frame = PyEval_GetFrame();
4543 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004545 if (current_frame != NULL) {
4546 const int codeflags = current_frame->f_code->co_flags;
4547 const int compilerflags = codeflags & PyCF_MASK;
4548 if (compilerflags) {
4549 result = 1;
4550 cf->cf_flags |= compilerflags;
4551 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004552#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004553 if (codeflags & CO_GENERATOR_ALLOWED) {
4554 result = 1;
4555 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4556 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004557#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004558 }
4559 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004560}
4561
Guido van Rossum3f5da241990-12-20 15:06:42 +00004562
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004563const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004564PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004565{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004566 if (PyMethod_Check(func))
4567 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4568 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004569 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004570 else if (PyCFunction_Check(func))
4571 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4572 else
4573 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004574}
4575
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004576const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004577PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004578{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004579 if (PyMethod_Check(func))
4580 return "()";
4581 else if (PyFunction_Check(func))
4582 return "()";
4583 else if (PyCFunction_Check(func))
4584 return "()";
4585 else
4586 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004587}
4588
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004589#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004590if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004591 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4592 tstate, tstate->frame, \
4593 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004594 x = NULL; \
4595 } \
4596 else { \
4597 x = call; \
4598 if (tstate->c_profilefunc != NULL) { \
4599 if (x == NULL) { \
4600 call_trace_protected(tstate->c_profilefunc, \
4601 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004602 tstate, tstate->frame, \
4603 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004604 /* XXX should pass (type, value, tb) */ \
4605 } else { \
4606 if (call_trace(tstate->c_profilefunc, \
4607 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004608 tstate, tstate->frame, \
4609 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004610 Py_DECREF(x); \
4611 x = NULL; \
4612 } \
4613 } \
4614 } \
4615 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004616} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004617 x = call; \
4618 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004619
Victor Stinner415c5102017-01-11 00:54:57 +01004620/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4621 to reduce the stack consumption. */
4622Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Benjamin Peterson4fd64b92016-09-09 14:57:58 -07004623call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004624{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004625 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004626 PyObject *func = *pfunc;
4627 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004628 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4629 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004630 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004632 /* Always dispatch PyCFunction first, because these are
4633 presumed to be the most frequent callable object.
4634 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004635 if (PyCFunction_Check(func)) {
Victor Stinner50b48572018-11-01 01:51:40 +01004636 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004637 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004638 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004639 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
Victor Stinner50b48572018-11-01 01:51:40 +01004640 PyThreadState *tstate = _PyThreadState_GET();
jdemeyer56868f92018-07-21 10:30:59 +02004641 if (nargs > 0 && tstate->use_tracing) {
4642 /* We need to create a temporary bound method as argument
4643 for profiling.
4644
4645 If nargs == 0, then this cannot work because we have no
4646 "self". In any case, the call itself would raise
4647 TypeError (foo needs an argument), so we just skip
4648 profiling. */
4649 PyObject *self = stack[0];
4650 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
jdemeyer147d9552018-07-23 18:41:20 +02004651 if (func != NULL) {
4652 C_TRACE(x, _PyCFunction_FastCallKeywords(func,
4653 stack+1, nargs-1,
4654 kwnames));
4655 Py_DECREF(func);
INADA Naoki93fac8d2017-03-07 14:24:37 +09004656 }
jdemeyer147d9552018-07-23 18:41:20 +02004657 else {
4658 x = NULL;
4659 }
INADA Naoki93fac8d2017-03-07 14:24:37 +09004660 }
4661 else {
4662 x = _PyMethodDescr_FastCallKeywords(func, stack, nargs, kwnames);
4663 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004664 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004665 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004666 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
Victor Stinnerb69ee8c2016-11-28 18:32:31 +01004667 /* Optimize access to bound methods. Reuse the Python stack
4668 to pass 'self' as the first argument, replace 'func'
4669 with 'self'. It avoids the creation of a new temporary tuple
4670 for arguments (to replace func with self) when the method uses
4671 FASTCALL. */
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004672 PyObject *self = PyMethod_GET_SELF(func);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004673 Py_INCREF(self);
4674 func = PyMethod_GET_FUNCTION(func);
4675 Py_INCREF(func);
4676 Py_SETREF(*pfunc, self);
4677 nargs++;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004678 stack--;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004679 }
4680 else {
4681 Py_INCREF(func);
4682 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004683
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004684 if (PyFunction_Check(func)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004685 x = _PyFunction_FastCallKeywords(func, stack, nargs, kwnames);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004686 }
4687 else {
4688 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4689 }
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004690 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004691 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004692
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004693 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4694
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004695 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004696 while ((*pp_stack) > pfunc) {
4697 w = EXT_POP(*pp_stack);
4698 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004699 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004701 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004702}
4703
Jeremy Hylton52820442001-01-03 23:52:36 +00004704static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004705do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004706{
jdemeyere89de732018-09-19 12:06:20 +02004707 PyObject *result;
4708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004709 if (PyCFunction_Check(func)) {
Victor Stinner50b48572018-11-01 01:51:40 +01004710 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004711 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004712 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004713 }
jdemeyere89de732018-09-19 12:06:20 +02004714 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
Victor Stinner50b48572018-11-01 01:51:40 +01004715 PyThreadState *tstate = _PyThreadState_GET();
jdemeyere89de732018-09-19 12:06:20 +02004716 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
4717 if (nargs > 0 && tstate->use_tracing) {
4718 /* We need to create a temporary bound method as argument
4719 for profiling.
4720
4721 If nargs == 0, then this cannot work because we have no
4722 "self". In any case, the call itself would raise
4723 TypeError (foo needs an argument), so we just skip
4724 profiling. */
4725 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
4726 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4727 if (func == NULL) {
4728 return NULL;
4729 }
4730
4731 C_TRACE(result, _PyCFunction_FastCallDict(func,
Victor Stinnerd17a6932018-11-09 16:56:48 +01004732 &_PyTuple_ITEMS(callargs)[1],
jdemeyere89de732018-09-19 12:06:20 +02004733 nargs - 1,
4734 kwdict));
4735 Py_DECREF(func);
4736 return result;
4737 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004738 }
jdemeyere89de732018-09-19 12:06:20 +02004739 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00004740}
4741
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004742/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004743 nb_index slot defined, and store in *pi.
4744 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004745 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004746 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004747*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004748int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004749_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004750{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004751 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004752 Py_ssize_t x;
4753 if (PyIndex_Check(v)) {
4754 x = PyNumber_AsSsize_t(v, NULL);
4755 if (x == -1 && PyErr_Occurred())
4756 return 0;
4757 }
4758 else {
4759 PyErr_SetString(PyExc_TypeError,
4760 "slice indices must be integers or "
4761 "None or have an __index__ method");
4762 return 0;
4763 }
4764 *pi = x;
4765 }
4766 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004767}
4768
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004769int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004770_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004771{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004772 Py_ssize_t x;
4773 if (PyIndex_Check(v)) {
4774 x = PyNumber_AsSsize_t(v, NULL);
4775 if (x == -1 && PyErr_Occurred())
4776 return 0;
4777 }
4778 else {
4779 PyErr_SetString(PyExc_TypeError,
4780 "slice indices must be integers or "
4781 "have an __index__ method");
4782 return 0;
4783 }
4784 *pi = x;
4785 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004786}
4787
4788
Guido van Rossum486364b2007-06-30 05:01:58 +00004789#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004790 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004791
Guido van Rossumb209a111997-04-29 18:18:01 +00004792static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004793cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004794{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004795 int res = 0;
4796 switch (op) {
4797 case PyCmp_IS:
4798 res = (v == w);
4799 break;
4800 case PyCmp_IS_NOT:
4801 res = (v != w);
4802 break;
4803 case PyCmp_IN:
4804 res = PySequence_Contains(w, v);
4805 if (res < 0)
4806 return NULL;
4807 break;
4808 case PyCmp_NOT_IN:
4809 res = PySequence_Contains(w, v);
4810 if (res < 0)
4811 return NULL;
4812 res = !res;
4813 break;
4814 case PyCmp_EXC_MATCH:
4815 if (PyTuple_Check(w)) {
4816 Py_ssize_t i, length;
4817 length = PyTuple_Size(w);
4818 for (i = 0; i < length; i += 1) {
4819 PyObject *exc = PyTuple_GET_ITEM(w, i);
4820 if (!PyExceptionClass_Check(exc)) {
4821 PyErr_SetString(PyExc_TypeError,
4822 CANNOT_CATCH_MSG);
4823 return NULL;
4824 }
4825 }
4826 }
4827 else {
4828 if (!PyExceptionClass_Check(w)) {
4829 PyErr_SetString(PyExc_TypeError,
4830 CANNOT_CATCH_MSG);
4831 return NULL;
4832 }
4833 }
4834 res = PyErr_GivenExceptionMatches(v, w);
4835 break;
4836 default:
4837 return PyObject_RichCompare(v, w, op);
4838 }
4839 v = res ? Py_True : Py_False;
4840 Py_INCREF(v);
4841 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004842}
4843
Thomas Wouters52152252000-08-17 22:55:00 +00004844static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004845import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
4846{
4847 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004848 PyObject *import_func, *res;
4849 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004850
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004851 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004852 if (import_func == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004853 if (!PyErr_Occurred()) {
4854 PyErr_SetString(PyExc_ImportError, "__import__ not found");
4855 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004856 return NULL;
4857 }
4858
4859 /* Fast path for not overloaded __import__. */
Victor Stinnercaba55b2018-08-03 15:33:52 +02004860 if (import_func == _PyInterpreterState_GET_UNSAFE()->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004861 int ilevel = _PyLong_AsInt(level);
4862 if (ilevel == -1 && PyErr_Occurred()) {
4863 return NULL;
4864 }
4865 res = PyImport_ImportModuleLevelObject(
4866 name,
4867 f->f_globals,
4868 f->f_locals == NULL ? Py_None : f->f_locals,
4869 fromlist,
4870 ilevel);
4871 return res;
4872 }
4873
4874 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004875
4876 stack[0] = name;
4877 stack[1] = f->f_globals;
4878 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
4879 stack[3] = fromlist;
4880 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02004881 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004882 Py_DECREF(import_func);
4883 return res;
4884}
4885
4886static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004887import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004888{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004889 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02004890 _Py_IDENTIFIER(__name__);
Xiang Zhang4830f582017-03-21 11:13:42 +08004891 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004892
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004893 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02004894 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004895 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004896 /* Issue #17636: in case this failed because of a circular relative
4897 import, try to fallback on reading the module directly from
4898 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02004899 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07004900 if (pkgname == NULL) {
4901 goto error;
4902 }
Oren Milman6db70332017-09-19 14:23:01 +03004903 if (!PyUnicode_Check(pkgname)) {
4904 Py_CLEAR(pkgname);
4905 goto error;
4906 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004907 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07004908 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08004909 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004910 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07004911 }
Eric Snow3f9eee62017-09-15 16:35:20 -06004912 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004913 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07004914 if (x == NULL) {
4915 goto error;
4916 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004917 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004918 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07004919 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004920 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004921 if (pkgname == NULL) {
4922 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
4923 if (pkgname_or_unknown == NULL) {
4924 Py_XDECREF(pkgpath);
4925 return NULL;
4926 }
4927 } else {
4928 pkgname_or_unknown = pkgname;
4929 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004930
4931 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
4932 PyErr_Clear();
Xiang Zhang4830f582017-03-21 11:13:42 +08004933 errmsg = PyUnicode_FromFormat(
4934 "cannot import name %R from %R (unknown location)",
4935 name, pkgname_or_unknown
4936 );
4937 /* NULL check for errmsg done by PyErr_SetImportError. */
4938 PyErr_SetImportError(errmsg, pkgname, NULL);
4939 }
4940 else {
4941 errmsg = PyUnicode_FromFormat(
4942 "cannot import name %R from %R (%S)",
4943 name, pkgname_or_unknown, pkgpath
4944 );
4945 /* NULL check for errmsg done by PyErr_SetImportError. */
4946 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004947 }
4948
Xiang Zhang4830f582017-03-21 11:13:42 +08004949 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004950 Py_XDECREF(pkgname_or_unknown);
4951 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07004952 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00004953}
Guido van Rossumac7be682001-01-17 15:42:30 +00004954
Thomas Wouters52152252000-08-17 22:55:00 +00004955static int
4956import_all_from(PyObject *locals, PyObject *v)
4957{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004958 _Py_IDENTIFIER(__all__);
4959 _Py_IDENTIFIER(__dict__);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08004960 _Py_IDENTIFIER(__name__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004961 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004962 int skip_leading_underscores = 0;
4963 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004964
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004965 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
4966 return -1; /* Unexpected error */
4967 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004968 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004969 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
4970 return -1;
4971 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004972 if (dict == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004973 PyErr_SetString(PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004974 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004975 return -1;
4976 }
4977 all = PyMapping_Keys(dict);
4978 Py_DECREF(dict);
4979 if (all == NULL)
4980 return -1;
4981 skip_leading_underscores = 1;
4982 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004984 for (pos = 0, err = 0; ; pos++) {
4985 name = PySequence_GetItem(all, pos);
4986 if (name == NULL) {
4987 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4988 err = -1;
4989 else
4990 PyErr_Clear();
4991 break;
4992 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08004993 if (!PyUnicode_Check(name)) {
4994 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
4995 if (modname == NULL) {
4996 Py_DECREF(name);
4997 err = -1;
4998 break;
4999 }
5000 if (!PyUnicode_Check(modname)) {
5001 PyErr_Format(PyExc_TypeError,
5002 "module __name__ must be a string, not %.100s",
5003 Py_TYPE(modname)->tp_name);
5004 }
5005 else {
5006 PyErr_Format(PyExc_TypeError,
5007 "%s in %U.%s must be str, not %.100s",
5008 skip_leading_underscores ? "Key" : "Item",
5009 modname,
5010 skip_leading_underscores ? "__dict__" : "__all__",
5011 Py_TYPE(name)->tp_name);
5012 }
5013 Py_DECREF(modname);
5014 Py_DECREF(name);
5015 err = -1;
5016 break;
5017 }
5018 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005019 if (PyUnicode_READY(name) == -1) {
5020 Py_DECREF(name);
5021 err = -1;
5022 break;
5023 }
5024 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5025 Py_DECREF(name);
5026 continue;
5027 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005028 }
5029 value = PyObject_GetAttr(v, name);
5030 if (value == NULL)
5031 err = -1;
5032 else if (PyDict_CheckExact(locals))
5033 err = PyDict_SetItem(locals, name, value);
5034 else
5035 err = PyObject_SetItem(locals, name, value);
5036 Py_DECREF(name);
5037 Py_XDECREF(value);
5038 if (err != 0)
5039 break;
5040 }
5041 Py_DECREF(all);
5042 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005043}
5044
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005045static int
5046check_args_iterable(PyObject *func, PyObject *args)
5047{
5048 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
5049 PyErr_Format(PyExc_TypeError,
5050 "%.200s%.200s argument after * "
5051 "must be an iterable, not %.200s",
5052 PyEval_GetFuncName(func),
5053 PyEval_GetFuncDesc(func),
5054 args->ob_type->tp_name);
5055 return -1;
5056 }
5057 return 0;
5058}
5059
5060static void
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005061format_kwargs_error(PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005062{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005063 /* _PyDict_MergeEx raises attribute
5064 * error (percolated from an attempt
5065 * to get 'keys' attribute) instead of
5066 * a type error if its second argument
5067 * is not a mapping.
5068 */
5069 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
5070 PyErr_Format(PyExc_TypeError,
5071 "%.200s%.200s argument after ** "
5072 "must be a mapping, not %.200s",
5073 PyEval_GetFuncName(func),
5074 PyEval_GetFuncDesc(func),
5075 kwargs->ob_type->tp_name);
5076 }
5077 else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
5078 PyObject *exc, *val, *tb;
5079 PyErr_Fetch(&exc, &val, &tb);
5080 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
5081 PyObject *key = PyTuple_GET_ITEM(val, 0);
5082 if (!PyUnicode_Check(key)) {
5083 PyErr_Format(PyExc_TypeError,
5084 "%.200s%.200s keywords must be strings",
5085 PyEval_GetFuncName(func),
5086 PyEval_GetFuncDesc(func));
5087 } else {
5088 PyErr_Format(PyExc_TypeError,
5089 "%.200s%.200s got multiple "
5090 "values for keyword argument '%U'",
5091 PyEval_GetFuncName(func),
5092 PyEval_GetFuncDesc(func),
5093 key);
5094 }
5095 Py_XDECREF(exc);
5096 Py_XDECREF(val);
5097 Py_XDECREF(tb);
5098 }
5099 else {
5100 PyErr_Restore(exc, val, tb);
5101 }
5102 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005103}
5104
Guido van Rossumac7be682001-01-17 15:42:30 +00005105static void
Neal Norwitzda059e32007-08-26 05:33:45 +00005106format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005107{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005108 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005109
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005110 if (!obj)
5111 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005112
Serhiy Storchaka06515832016-11-20 09:13:07 +02005113 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005114 if (!obj_str)
5115 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005116
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005117 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005118}
Guido van Rossum950361c1997-01-24 13:49:28 +00005119
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005120static void
5121format_exc_unbound(PyCodeObject *co, int oparg)
5122{
5123 PyObject *name;
5124 /* Don't stomp existing exception */
5125 if (PyErr_Occurred())
5126 return;
5127 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5128 name = PyTuple_GET_ITEM(co->co_cellvars,
5129 oparg);
5130 format_exc_check_arg(
5131 PyExc_UnboundLocalError,
5132 UNBOUNDLOCAL_ERROR_MSG,
5133 name);
5134 } else {
5135 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5136 PyTuple_GET_SIZE(co->co_cellvars));
5137 format_exc_check_arg(PyExc_NameError,
5138 UNBOUNDFREE_ERROR_MSG, name);
5139 }
5140}
5141
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005142static void
5143format_awaitable_error(PyTypeObject *type, int prevopcode)
5144{
5145 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5146 if (prevopcode == BEFORE_ASYNC_WITH) {
5147 PyErr_Format(PyExc_TypeError,
5148 "'async with' received an object from __aenter__ "
5149 "that does not implement __await__: %.100s",
5150 type->tp_name);
5151 }
5152 else if (prevopcode == WITH_CLEANUP_START) {
5153 PyErr_Format(PyExc_TypeError,
5154 "'async with' received an object from __aexit__ "
5155 "that does not implement __await__: %.100s",
5156 type->tp_name);
5157 }
5158 }
5159}
5160
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005161static PyObject *
5162unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005163 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005164{
5165 PyObject *res;
5166 if (Py_REFCNT(v) == 2) {
5167 /* In the common case, there are 2 references to the value
5168 * stored in 'variable' when the += is performed: one on the
5169 * value stack (in 'v') and one still stored in the
5170 * 'variable'. We try to delete the variable now to reduce
5171 * the refcnt to 1.
5172 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005173 int opcode, oparg;
5174 NEXTOPARG();
5175 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005176 case STORE_FAST:
5177 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005178 PyObject **fastlocals = f->f_localsplus;
5179 if (GETLOCAL(oparg) == v)
5180 SETLOCAL(oparg, NULL);
5181 break;
5182 }
5183 case STORE_DEREF:
5184 {
5185 PyObject **freevars = (f->f_localsplus +
5186 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005187 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005188 if (PyCell_GET(c) == v) {
5189 PyCell_SET(c, NULL);
5190 Py_DECREF(v);
5191 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005192 break;
5193 }
5194 case STORE_NAME:
5195 {
5196 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005197 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005198 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005199 if (locals && PyDict_CheckExact(locals)) {
5200 PyObject *w = PyDict_GetItemWithError(locals, name);
5201 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
5202 (w == NULL && PyErr_Occurred()))
5203 {
5204 Py_DECREF(v);
5205 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005206 }
5207 }
5208 break;
5209 }
5210 }
5211 }
5212 res = v;
5213 PyUnicode_Append(&res, w);
5214 return res;
5215}
5216
Guido van Rossum950361c1997-01-24 13:49:28 +00005217#ifdef DYNAMIC_EXECUTION_PROFILE
5218
Skip Montanarof118cb12001-10-15 20:51:38 +00005219static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005220getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005221{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005222 int i;
5223 PyObject *l = PyList_New(256);
5224 if (l == NULL) return NULL;
5225 for (i = 0; i < 256; i++) {
5226 PyObject *x = PyLong_FromLong(a[i]);
5227 if (x == NULL) {
5228 Py_DECREF(l);
5229 return NULL;
5230 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005231 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005232 }
5233 for (i = 0; i < 256; i++)
5234 a[i] = 0;
5235 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005236}
5237
5238PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005239_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005240{
5241#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005242 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005243#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005244 int i;
5245 PyObject *l = PyList_New(257);
5246 if (l == NULL) return NULL;
5247 for (i = 0; i < 257; i++) {
5248 PyObject *x = getarray(dxpairs[i]);
5249 if (x == NULL) {
5250 Py_DECREF(l);
5251 return NULL;
5252 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005253 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005254 }
5255 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005256#endif
5257}
5258
5259#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005260
5261Py_ssize_t
5262_PyEval_RequestCodeExtraIndex(freefunc free)
5263{
Victor Stinnercaba55b2018-08-03 15:33:52 +02005264 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -07005265 Py_ssize_t new_index;
5266
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005267 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005268 return -1;
5269 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005270 new_index = interp->co_extra_user_count++;
5271 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005272 return new_index;
5273}
Łukasz Langaa785c872016-09-09 17:37:37 -07005274
5275static void
5276dtrace_function_entry(PyFrameObject *f)
5277{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005278 const char *filename;
5279 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005280 int lineno;
5281
5282 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5283 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5284 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5285
5286 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5287}
5288
5289static void
5290dtrace_function_return(PyFrameObject *f)
5291{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005292 const char *filename;
5293 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005294 int lineno;
5295
5296 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5297 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5298 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5299
5300 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5301}
5302
5303/* DTrace equivalent of maybe_call_line_trace. */
5304static void
5305maybe_dtrace_line(PyFrameObject *frame,
5306 int *instr_lb, int *instr_ub, int *instr_prev)
5307{
5308 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005309 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005310
5311 /* If the last instruction executed isn't in the current
5312 instruction window, reset the window.
5313 */
5314 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5315 PyAddrPair bounds;
5316 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5317 &bounds);
5318 *instr_lb = bounds.ap_lower;
5319 *instr_ub = bounds.ap_upper;
5320 }
5321 /* If the last instruction falls at the start of a line or if
5322 it represents a jump backwards, update the frame's line
5323 number and call the trace function. */
5324 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5325 frame->f_lineno = line;
5326 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5327 if (!co_filename)
5328 co_filename = "?";
5329 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5330 if (!co_name)
5331 co_name = "?";
5332 PyDTrace_LINE(co_filename, co_name, line);
5333 }
5334 *instr_prev = frame->f_lasti;
5335}