blob: d6a0b335955e0982eb3583bc0d9e4522400ca523 [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());
Eric Snow8479a342019-03-08 23:44:33 -0700177
178 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
179 if (_PyRuntime.ceval.pending.lock == NULL) {
Victor Stinnere3f40702019-03-15 16:04:20 +0100180 Py_FatalError("Can't initialize threads for pending calls");
Eric Snow5be45a62019-03-08 22:47:07 -0700181 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000182}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000183
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000184void
Antoine Pitrou1df15362010-09-13 14:16:46 +0000185_PyEval_FiniThreads(void)
186{
187 if (!gil_created())
188 return;
189 destroy_gil();
190 assert(!gil_created());
191}
192
193void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000194PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000195{
Victor Stinner50b48572018-11-01 01:51:40 +0100196 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 if (tstate == NULL)
198 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
199 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000200}
201
202void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000203PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000204{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100206 We therefore avoid PyThreadState_Get() which dumps a fatal error
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 in debug mode.
208 */
Victor Stinner50b48572018-11-01 01:51:40 +0100209 drop_gil(_PyThreadState_GET());
Guido van Rossum25ce5661997-08-02 03:10:38 +0000210}
211
212void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000213PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000214{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 if (tstate == NULL)
216 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
217 /* Check someone has called PyEval_InitThreads() to create the lock */
218 assert(gil_created());
219 take_gil(tstate);
220 if (PyThreadState_Swap(tstate) != NULL)
221 Py_FatalError(
222 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000223}
224
225void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000226PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000227{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 if (tstate == NULL)
229 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
230 if (PyThreadState_Swap(NULL) != tstate)
231 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
232 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000233}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000234
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200235/* This function is called from PyOS_AfterFork_Child to destroy all threads
236 * which are not running in the child process, and clear internal locks
237 * which might be held by those threads.
238 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000239
240void
241PyEval_ReInitThreads(void)
242{
Victor Stinner50b48572018-11-01 01:51:40 +0100243 PyThreadState *current_tstate = _PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 if (!gil_created())
246 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 recreate_gil();
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200248 take_gil(current_tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700249
Eric Snow5be45a62019-03-08 22:47:07 -0700250 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
Eric Snow8479a342019-03-08 23:44:33 -0700251 if (_PyRuntime.ceval.pending.lock == NULL) {
252 Py_FatalError("Can't initialize threads for pending calls");
253 }
Jesse Nollera8513972008-07-17 16:49:17 +0000254
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200255 /* Destroy all threads except the current one */
256 _PyThreadState_DeleteExcept(current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000257}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000258
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000259/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600260 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000261
262void
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100263_PyEval_SignalAsyncExc(void)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000264{
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100265 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000266}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000267
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000268PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000269PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000270{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 PyThreadState *tstate = PyThreadState_Swap(NULL);
272 if (tstate == NULL)
273 Py_FatalError("PyEval_SaveThread: NULL tstate");
Victor Stinner2914bb32018-01-29 11:57:45 +0100274 assert(gil_created());
275 drop_gil(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000277}
278
279void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000280PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000281{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 if (tstate == NULL)
283 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Victor Stinner2914bb32018-01-29 11:57:45 +0100284 assert(gil_created());
285
286 int err = errno;
287 take_gil(tstate);
288 /* _Py_Finalizing is protected by the GIL */
289 if (_Py_IsFinalizing() && !_Py_CURRENTLY_FINALIZING(tstate)) {
290 drop_gil(tstate);
291 PyThread_exit_thread();
292 Py_UNREACHABLE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 }
Victor Stinner2914bb32018-01-29 11:57:45 +0100294 errno = err;
295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000297}
298
299
Guido van Rossuma9672091994-09-14 13:31:22 +0000300/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
301 signal handlers or Mac I/O completion routines) can schedule calls
302 to a function to be called synchronously.
303 The synchronous function is called with one void* argument.
304 It should return 0 for success or -1 for failure -- failure should
305 be accompanied by an exception.
306
307 If registry succeeds, the registry function returns 0; if it fails
308 (e.g. due to too many pending calls) it returns -1 (without setting
309 an exception condition).
310
311 Note that because registry may occur from within signal handlers,
312 or other asynchronous events, calling malloc() is unsafe!
313
Guido van Rossuma9672091994-09-14 13:31:22 +0000314 Any thread can schedule pending calls, but only the main thread
315 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000316 There is no facility to schedule calls to a particular thread, but
317 that should be easy to change, should that ever be required. In
318 that case, the static variables here should go into the python
319 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000320*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000321
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200322void
323_PyEval_SignalReceived(void)
324{
325 /* bpo-30703: Function called when the C signal handler of Python gets a
326 signal. We cannot queue a callback using Py_AddPendingCall() since
327 that function is not async-signal-safe. */
Eric Snowfdf282d2019-01-11 14:26:55 -0700328 SIGNAL_PENDING_SIGNALS();
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200329}
330
Eric Snow5be45a62019-03-08 22:47:07 -0700331/* Push one item onto the queue while holding the lock. */
332static int
Eric Snow842a2f02019-03-15 15:47:51 -0600333_push_pending_call(struct _pending_calls *pending,
334 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700335{
Eric Snow842a2f02019-03-15 15:47:51 -0600336 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700337 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600338 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700339 return -1; /* Queue full */
340 }
Eric Snow842a2f02019-03-15 15:47:51 -0600341 pending->calls[i].func = func;
342 pending->calls[i].arg = arg;
343 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700344 return 0;
345}
346
347/* Pop one item off the queue while holding the lock. */
348static void
Eric Snow842a2f02019-03-15 15:47:51 -0600349_pop_pending_call(struct _pending_calls *pending,
350 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700351{
Eric Snow842a2f02019-03-15 15:47:51 -0600352 int i = pending->first;
353 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700354 return; /* Queue empty */
355 }
356
Eric Snow842a2f02019-03-15 15:47:51 -0600357 *func = pending->calls[i].func;
358 *arg = pending->calls[i].arg;
359 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700360}
361
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200362/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000363 scheduling to be made from any thread, and even from an executing
364 callback.
365 */
366
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000367int
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100368Py_AddPendingCall(int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000369{
Eric Snow842a2f02019-03-15 15:47:51 -0600370 struct _pending_calls *pending = &_PyRuntime.ceval.pending;
371
372 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
373 if (pending->finishing) {
374 PyThread_release_lock(pending->lock);
375
376 PyObject *exc, *val, *tb;
377 PyErr_Fetch(&exc, &val, &tb);
378 PyErr_SetString(PyExc_SystemError,
379 "Py_AddPendingCall: cannot add pending calls "
380 "(Python shutting down)");
381 PyErr_Print();
382 PyErr_Restore(exc, val, tb);
383 return -1;
384 }
385 int result = _push_pending_call(pending, func, arg);
386 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 /* signal main loop */
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100389 SIGNAL_PENDING_CALLS();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000391}
392
Eric Snowfdf282d2019-01-11 14:26:55 -0700393static int
394handle_signals(void)
395{
Eric Snow5be45a62019-03-08 22:47:07 -0700396 /* Only handle signals on main thread. PyEval_InitThreads must
397 * have been called already.
398 */
399 if (PyThread_get_thread_ident() != _PyRuntime.main_thread) {
Eric Snowfdf282d2019-01-11 14:26:55 -0700400 return 0;
401 }
Eric Snow64d6cc82019-02-23 15:40:43 -0700402 /*
403 * Ensure that the thread isn't currently running some other
404 * interpreter.
405 */
406 if (_PyInterpreterState_GET_UNSAFE() != _PyRuntime.interpreters.main) {
407 return 0;
408 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700409
410 UNSIGNAL_PENDING_SIGNALS();
Eric Snow64d6cc82019-02-23 15:40:43 -0700411 if (_PyErr_CheckSignals() < 0) {
Eric Snowfdf282d2019-01-11 14:26:55 -0700412 SIGNAL_PENDING_SIGNALS(); /* We're not done yet */
413 return -1;
414 }
415 return 0;
416}
417
418static int
Eric Snow842a2f02019-03-15 15:47:51 -0600419make_pending_calls(struct _pending_calls* pending)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000420{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200421 static int busy = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000422
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100423 /* only service pending calls on main thread */
Eric Snow5be45a62019-03-08 22:47:07 -0700424 if (PyThread_get_thread_ident() != _PyRuntime.main_thread) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100425 return 0;
426 }
427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 /* don't perform recursive pending calls */
Eric Snowfdf282d2019-01-11 14:26:55 -0700429 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700431 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200432 busy = 1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200433 /* unsignal before starting to call callbacks, so that any callback
434 added in-between re-signals */
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100435 UNSIGNAL_PENDING_CALLS();
Eric Snowfdf282d2019-01-11 14:26:55 -0700436 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 /* perform a bounded number of calls, in case of recursion */
Eric Snowfdf282d2019-01-11 14:26:55 -0700439 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700440 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 void *arg = NULL;
442
443 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600444 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
445 _pop_pending_call(pending, &func, &arg);
446 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700447
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100448 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700449 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100450 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700451 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700452 res = func(arg);
453 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200454 goto error;
455 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200457
Charles-François Natalif23339a2011-07-23 18:15:43 +0200458 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700459 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200460
461error:
462 busy = 0;
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100463 SIGNAL_PENDING_CALLS();
Eric Snowfdf282d2019-01-11 14:26:55 -0700464 return res;
465}
466
Eric Snow842a2f02019-03-15 15:47:51 -0600467void
468_Py_FinishPendingCalls(void)
469{
470 struct _pending_calls *pending = &_PyRuntime.ceval.pending;
471
472 assert(PyGILState_Check());
473
474 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
475 pending->finishing = 1;
476 PyThread_release_lock(pending->lock);
477
478 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
479 return;
480 }
481
482 if (make_pending_calls(pending) < 0) {
483 PyObject *exc, *val, *tb;
484 PyErr_Fetch(&exc, &val, &tb);
485 PyErr_BadInternalCall();
486 _PyErr_ChainExceptions(exc, val, tb);
487 PyErr_Print();
488 }
489}
490
Eric Snowfdf282d2019-01-11 14:26:55 -0700491/* Py_MakePendingCalls() is a simple wrapper for the sake
492 of backward-compatibility. */
493int
494Py_MakePendingCalls(void)
495{
496 assert(PyGILState_Check());
497
498 /* Python signal handler doesn't really queue a callback: it only signals
499 that a signal was received, see _PyEval_SignalReceived(). */
500 int res = handle_signals();
501 if (res != 0) {
502 return res;
503 }
504
Eric Snow842a2f02019-03-15 15:47:51 -0600505 res = make_pending_calls(&_PyRuntime.ceval.pending);
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100506 if (res != 0) {
507 return res;
508 }
509
510 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000511}
512
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000513/* The interpreter's recursion limit */
514
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000515#ifndef Py_DEFAULT_RECURSION_LIMIT
516#define Py_DEFAULT_RECURSION_LIMIT 1000
517#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600518
Eric Snow05351c12017-09-05 21:43:08 -0700519int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000520
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600521void
522_PyEval_Initialize(struct _ceval_runtime_state *state)
523{
524 state->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
525 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
526 _gil_initialize(&state->gil);
527}
528
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000529int
530Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000531{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600532 return _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000533}
534
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000535void
536Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000537{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600538 _PyRuntime.ceval.recursion_limit = new_limit;
539 _Py_CheckRecursionLimit = _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000540}
541
Armin Rigo2b3eb402003-10-28 12:05:48 +0000542/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
543 if the recursion_depth reaches _Py_CheckRecursionLimit.
544 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
545 to guarantee that _Py_CheckRecursiveCall() is regularly called.
546 Without USE_STACKCHECK, there is no need for this. */
547int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300548_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000549{
Victor Stinner50b48572018-11-01 01:51:40 +0100550 PyThreadState *tstate = _PyThreadState_GET();
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600551 int recursion_limit = _PyRuntime.ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000552
553#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700554 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 if (PyOS_CheckStack()) {
556 --tstate->recursion_depth;
557 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
558 return -1;
559 }
pdox18967932017-10-25 23:03:01 -0700560 /* Needed for ABI backwards-compatibility (see bpo-31857) */
Eric Snow05351c12017-09-05 21:43:08 -0700561 _Py_CheckRecursionLimit = recursion_limit;
pdox18967932017-10-25 23:03:01 -0700562#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 if (tstate->recursion_critical)
564 /* Somebody asked that we don't check for recursion. */
565 return 0;
566 if (tstate->overflowed) {
567 if (tstate->recursion_depth > recursion_limit + 50) {
568 /* Overflowing while handling an overflow. Give up. */
569 Py_FatalError("Cannot recover from stack overflow.");
570 }
571 return 0;
572 }
573 if (tstate->recursion_depth > recursion_limit) {
574 --tstate->recursion_depth;
575 tstate->overflowed = 1;
Yury Selivanovf488fb42015-07-03 01:04:23 -0400576 PyErr_Format(PyExc_RecursionError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 "maximum recursion depth exceeded%s",
578 where);
579 return -1;
580 }
581 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000582}
583
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400584static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000585static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000586
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600587#define _Py_TracingPossible _PyRuntime.ceval.tracing_possible
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000588
Guido van Rossum374a9221991-04-04 10:40:29 +0000589
Guido van Rossumb209a111997-04-29 18:18:01 +0000590PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000591PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000592{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 return PyEval_EvalCodeEx(co,
594 globals, locals,
595 (PyObject **)NULL, 0,
596 (PyObject **)NULL, 0,
597 (PyObject **)NULL, 0,
598 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000599}
600
601
602/* Interpreter main loop */
603
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000604PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000605PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 /* This is for backward compatibility with extension modules that
607 used this API; core interpreter code should call
608 PyEval_EvalFrameEx() */
609 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000610}
611
612PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000613PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000614{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200615 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
616 return interp->eval_frame(f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700617}
618
Victor Stinnerc6944e72016-11-11 02:13:35 +0100619PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700620_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
621{
Guido van Rossum950361c1997-01-24 13:49:28 +0000622#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000624#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200625 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300626 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200627 int opcode; /* Current opcode */
628 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200629 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 PyObject *retval = NULL; /* Return value */
Victor Stinner50b48572018-11-01 01:51:40 +0100631 PyThreadState *tstate = _PyThreadState_GET();
Eric Snow7bda9de2019-03-08 17:25:54 -0700632 _Py_atomic_int *eval_breaker = &_PyRuntime.ceval.eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000634
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 is true when the line being executed has changed. The
640 initial values are such as to make this false the first
641 time it is tested. */
642 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000643
Serhiy Storchakaab874002016-09-11 13:48:15 +0300644 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 PyObject *names;
646 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000647
Brett Cannon368b4b72012-04-02 12:17:59 -0400648#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200649 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400650#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200651
Antoine Pitroub52ec782009-01-25 16:34:23 +0000652/* Computed GOTOs, or
653 the-optimization-commonly-but-improperly-known-as-"threaded code"
654 using gcc's labels-as-values extension
655 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
656
657 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000659 combined with a lookup table of jump addresses. However, since the
660 indirect jump instruction is shared by all opcodes, the CPU will have a
661 hard time making the right prediction for where to jump next (actually,
662 it will be always wrong except in the uncommon case of a sequence of
663 several identical opcodes).
664
665 "Threaded code" in contrast, uses an explicit jump table and an explicit
666 indirect jump instruction at the end of each opcode. Since the jump
667 instruction is at a different address for each opcode, the CPU will make a
668 separate prediction for each of these instructions, which is equivalent to
669 predicting the second opcode of each opcode pair. These predictions have
670 a much better chance to turn out valid, especially in small bytecode loops.
671
672 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000674 and potentially many more instructions (depending on the pipeline width).
675 A correctly predicted branch, however, is nearly free.
676
677 At the time of this writing, the "threaded code" version is up to 15-20%
678 faster than the normal "switch" version, depending on the compiler and the
679 CPU architecture.
680
681 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
682 because it would render the measurements invalid.
683
684
685 NOTE: care must be taken that the compiler doesn't try to "optimize" the
686 indirect jumps by sharing them between all opcodes. Such optimizations
687 can be disabled on gcc by using the -fno-gcse flag (or possibly
688 -fno-crossjumping).
689*/
690
Antoine Pitrou042b1282010-08-13 21:15:58 +0000691#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000692#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000693#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000694#endif
695
Antoine Pitrou042b1282010-08-13 21:15:58 +0000696#ifdef HAVE_COMPUTED_GOTOS
697 #ifndef USE_COMPUTED_GOTOS
698 #define USE_COMPUTED_GOTOS 1
699 #endif
700#else
701 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
702 #error "Computed gotos are not supported on this compiler."
703 #endif
704 #undef USE_COMPUTED_GOTOS
705 #define USE_COMPUTED_GOTOS 0
706#endif
707
708#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000709/* Import the static jump table */
710#include "opcode_targets.h"
711
Antoine Pitroub52ec782009-01-25 16:34:23 +0000712#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -0700713 op: \
714 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000715
Antoine Pitroub52ec782009-01-25 16:34:23 +0000716#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 { \
Eric Snow7bda9de2019-03-08 17:25:54 -0700718 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 FAST_DISPATCH(); \
720 } \
721 continue; \
722 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000723
724#ifdef LLTRACE
725#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700727 if (!lltrace && !_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300729 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300730 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 } \
732 goto fast_next_opcode; \
733 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000734#else
735#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700737 if (!_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000738 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300739 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300740 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 } \
742 goto fast_next_opcode; \
743 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000744#endif
745
746#else
Benjamin Petersonddd19492018-09-16 22:38:02 -0700747#define TARGET(op) op
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300748
Antoine Pitroub52ec782009-01-25 16:34:23 +0000749#define DISPATCH() continue
750#define FAST_DISPATCH() goto fast_next_opcode
751#endif
752
753
Neal Norwitza81d2202002-07-14 00:27:26 +0000754/* Tuple access macros */
755
756#ifndef Py_DEBUG
757#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
758#else
759#define GETITEM(v, i) PyTuple_GetItem((v), (i))
760#endif
761
Guido van Rossum374a9221991-04-04 10:40:29 +0000762/* Code access macros */
763
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300764/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600765#define INSTR_OFFSET() \
766 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300767#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300768 _Py_CODEUNIT word = *next_instr; \
769 opcode = _Py_OPCODE(word); \
770 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300771 next_instr++; \
772 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300773#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
774#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000775
Raymond Hettingerf606f872003-03-16 03:11:04 +0000776/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 Some opcodes tend to come in pairs thus making it possible to
778 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300779 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 Verifying the prediction costs a single high-speed test of a register
782 variable against a constant. If the pairing was good, then the
783 processor's own internal branch predication has a high likelihood of
784 success, resulting in a nearly zero-overhead transition to the
785 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300786 including its unpredictable switch-case branch. Combined with the
787 processor's internal branch prediction, a successful PREDICT has the
788 effect of making the two opcodes run as if they were a single new opcode
789 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000790
Georg Brandl86b2fb92008-07-16 03:43:04 +0000791 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 predictions turned-on and interpret the results as if some opcodes
793 had been combined or turn-off predictions so that the opcode frequency
794 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000795
796 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 the CPU to record separate branch prediction information for each
798 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000799
Raymond Hettingerf606f872003-03-16 03:11:04 +0000800*/
801
Antoine Pitrou042b1282010-08-13 21:15:58 +0000802#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000804#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300805#define PREDICT(op) \
806 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300807 _Py_CODEUNIT word = *next_instr; \
808 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300809 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300810 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300811 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300812 goto PRED_##op; \
813 } \
814 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000815#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300816#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000817
Raymond Hettingerf606f872003-03-16 03:11:04 +0000818
Guido van Rossum374a9221991-04-04 10:40:29 +0000819/* Stack manipulation macros */
820
Martin v. Löwis18e16552006-02-15 17:27:45 +0000821/* The stack can grow at most MAXINT deep, as co_nlocals and
822 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000823#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
824#define EMPTY() (STACK_LEVEL() == 0)
825#define TOP() (stack_pointer[-1])
826#define SECOND() (stack_pointer[-2])
827#define THIRD() (stack_pointer[-3])
828#define FOURTH() (stack_pointer[-4])
829#define PEEK(n) (stack_pointer[-(n)])
830#define SET_TOP(v) (stack_pointer[-1] = (v))
831#define SET_SECOND(v) (stack_pointer[-2] = (v))
832#define SET_THIRD(v) (stack_pointer[-3] = (v))
833#define SET_FOURTH(v) (stack_pointer[-4] = (v))
834#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
835#define BASIC_STACKADJ(n) (stack_pointer += n)
836#define BASIC_PUSH(v) (*stack_pointer++ = (v))
837#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000838
Guido van Rossum96a42c81992-01-12 02:29:51 +0000839#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000841 lltrace && prtrace(TOP(), "push")); \
842 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000844 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +0000845#define STACK_GROW(n) do { \
846 assert(n >= 0); \
847 (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000848 lltrace && prtrace(TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +0000849 assert(STACK_LEVEL() <= co->co_stacksize); \
850 } while (0)
851#define STACK_SHRINK(n) do { \
852 assert(n >= 0); \
853 (void)(lltrace && prtrace(TOP(), "stackadj")); \
854 (void)(BASIC_STACKADJ(-n)); \
855 assert(STACK_LEVEL() <= co->co_stacksize); \
856 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +0000857#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +0000858 prtrace((STACK_POINTER)[-1], "ext_pop")), \
859 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000860#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000861#define PUSH(v) BASIC_PUSH(v)
862#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +0000863#define STACK_GROW(n) BASIC_STACKADJ(n)
864#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000865#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000866#endif
867
Guido van Rossum681d79a1995-07-18 14:51:37 +0000868/* Local variable macros */
869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000871
872/* The SETLOCAL() macro must not DECREF the local variable in-place and
873 then store the new value; it must copy the old value to a temporary
874 value, then store the new value, and then DECREF the temporary value.
875 This is because it is possible that during the DECREF the frame is
876 accessed by other code (e.g. a __del__ method or gc.collect()) and the
877 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000879 GETLOCAL(i) = value; \
880 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000881
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000882
883#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 while (STACK_LEVEL() > (b)->b_level) { \
885 PyObject *v = POP(); \
886 Py_XDECREF(v); \
887 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000888
889#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300890 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +0100892 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 assert(STACK_LEVEL() >= (b)->b_level + 3); \
894 while (STACK_LEVEL() > (b)->b_level + 3) { \
895 value = POP(); \
896 Py_XDECREF(value); \
897 } \
Mark Shannonae3087c2017-10-22 22:41:51 +0100898 exc_info = tstate->exc_info; \
899 type = exc_info->exc_type; \
900 value = exc_info->exc_value; \
901 traceback = exc_info->exc_traceback; \
902 exc_info->exc_type = POP(); \
903 exc_info->exc_value = POP(); \
904 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 Py_XDECREF(type); \
906 Py_XDECREF(value); \
907 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300908 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000909
Guido van Rossuma027efa1997-05-05 20:56:21 +0000910/* Start of code */
911
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 /* push frame */
913 if (Py_EnterRecursiveCall(""))
914 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000915
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 if (tstate->use_tracing) {
919 if (tstate->c_tracefunc != NULL) {
920 /* tstate->c_tracefunc, if defined, is a
921 function that will be called on *every* entry
922 to a code block. Its return value, if not
923 None, is a function that will be called at
924 the start of each executed line of code.
925 (Actually, the function must return itself
926 in order to continue tracing.) The trace
927 functions are called with three arguments:
928 a pointer to the current frame, a string
929 indicating why the function is called, and
930 an argument which depends on the situation.
931 The global trace function is also called
932 whenever an exception is detected. */
933 if (call_trace_protected(tstate->c_tracefunc,
934 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100935 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 /* Trace function raised an error */
937 goto exit_eval_frame;
938 }
939 }
940 if (tstate->c_profilefunc != NULL) {
941 /* Similar for c_profilefunc, except it needn't
942 return itself and isn't called for "line" events */
943 if (call_trace_protected(tstate->c_profilefunc,
944 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100945 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 /* Profile function raised an error */
947 goto exit_eval_frame;
948 }
949 }
950 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000951
Łukasz Langaa785c872016-09-09 17:37:37 -0700952 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
953 dtrace_function_entry(f);
954
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 co = f->f_code;
956 names = co->co_names;
957 consts = co->co_consts;
958 fastlocals = f->f_localsplus;
959 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300960 assert(PyBytes_Check(co->co_code));
961 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +0300962 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
963 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
964 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300965 /*
966 f->f_lasti refers to the index of the last instruction,
967 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000968
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300969 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500970 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000971
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 When the PREDICT() macros are enabled, some opcode pairs follow in
973 direct succession without updating f->f_lasti. A successful
974 prediction effectively links the two codes together as if they
975 were a single new opcode; accordingly,f->f_lasti will point to
976 the first code in the pair (for instance, GET_ITER followed by
977 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300978 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300980 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300981 next_instr = first_instr;
982 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +0300983 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
984 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300985 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 stack_pointer = f->f_stacktop;
987 assert(stack_pointer != NULL);
988 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +0200989 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000990
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000991
Tim Peters5ca576e2001-06-18 22:08:13 +0000992#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200993 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000994#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000995
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400996 if (throwflag) /* support for generator.throw() */
997 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000998
Victor Stinnerace47d72013-07-18 01:41:08 +0200999#ifdef Py_DEBUG
1000 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001001 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001002 caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +02001003 assert(!PyErr_Occurred());
1004#endif
1005
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001006main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1009 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +02001010 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001011
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 /* Do periodic things. Doing this every time through
1013 the loop would add too much overhead, so we do it
1014 only every Nth instruction. We also do it if
1015 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1016 event needs attention (e.g. a signal handler or
1017 async I/O handler); see Py_AddPendingCall() and
1018 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001019
Eric Snow7bda9de2019-03-08 17:25:54 -07001020 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001021 opcode = _Py_OPCODE(*next_instr);
1022 if (opcode == SETUP_FINALLY ||
1023 opcode == SETUP_WITH ||
1024 opcode == BEFORE_ASYNC_WITH ||
1025 opcode == YIELD_FROM) {
1026 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001027 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001028 - If we're about to enter the 'with:'. It will prevent
1029 emitting a resource warning in the common idiom
1030 'with open(path) as file:'.
1031 - If we're about to enter the 'async with:'.
1032 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001033 *very* useful, but might help in some cases and it's
1034 traditional)
1035 - If we're resuming a chain of nested 'yield from' or
1036 'await' calls, then each frame is parked with YIELD_FROM
1037 as its next opcode. If the user hit control-C we want to
1038 wait until we've reached the innermost frame before
1039 running the signal handler and raising KeyboardInterrupt
1040 (see bpo-30039).
1041 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 goto fast_next_opcode;
1043 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001044
1045 if (_Py_atomic_load_relaxed(
1046 &_PyRuntime.ceval.signals_pending))
1047 {
1048 if (handle_signals() != 0) {
1049 goto error;
1050 }
1051 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001052 if (_Py_atomic_load_relaxed(
Victor Stinner4d61e6e2019-03-04 14:21:28 +01001053 &_PyRuntime.ceval.pending.calls_to_do))
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001054 {
Eric Snow842a2f02019-03-15 15:47:51 -06001055 if (make_pending_calls(&_PyRuntime.ceval.pending) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001056 goto error;
Eric Snowfdf282d2019-01-11 14:26:55 -07001057 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001059
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001060 if (_Py_atomic_load_relaxed(
1061 &_PyRuntime.ceval.gil_drop_request))
1062 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 /* Give another thread a chance */
1064 if (PyThreadState_Swap(NULL) != tstate)
1065 Py_FatalError("ceval: tstate mix-up");
1066 drop_gil(tstate);
1067
1068 /* Other threads may run now */
1069
1070 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001071
1072 /* Check if we should make a quick exit. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001073 if (_Py_IsFinalizing() &&
1074 !_Py_CURRENTLY_FINALIZING(tstate))
1075 {
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001076 drop_gil(tstate);
1077 PyThread_exit_thread();
1078 }
1079
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 if (PyThreadState_Swap(tstate) != NULL)
1081 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 }
1083 /* Check for asynchronous exceptions. */
1084 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001085 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001086 tstate->async_exc = NULL;
Victor Stinner4d61e6e2019-03-04 14:21:28 +01001087 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001088 PyErr_SetNone(exc);
1089 Py_DECREF(exc);
1090 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 }
1092 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001093
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 fast_next_opcode:
1095 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001096
Łukasz Langaa785c872016-09-09 17:37:37 -07001097 if (PyDTrace_LINE_ENABLED())
1098 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1099
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001101
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001102 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001103 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001104 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 /* see maybe_call_line_trace
1106 for expository comments */
1107 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001108
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 err = maybe_call_line_trace(tstate->c_tracefunc,
1110 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001111 tstate, f,
1112 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 /* Reload possibly changed frame fields */
1114 JUMPTO(f->f_lasti);
1115 if (f->f_stacktop != NULL) {
1116 stack_pointer = f->f_stacktop;
1117 f->f_stacktop = NULL;
1118 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001119 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001121 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001123
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001125
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001126 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001127 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001128#ifdef DYNAMIC_EXECUTION_PROFILE
1129#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 dxpairs[lastopcode][opcode]++;
1131 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001132#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001134#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001135
Guido van Rossum96a42c81992-01-12 02:29:51 +00001136#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 if (lltrace) {
1140 if (HAS_ARG(opcode)) {
1141 printf("%d: %d, %d\n",
1142 f->f_lasti, opcode, oparg);
1143 }
1144 else {
1145 printf("%d: %d\n",
1146 f->f_lasti, opcode);
1147 }
1148 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001149#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001152
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001154 It is essential that any operation that fails must goto error
1155 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001156
Benjamin Petersonddd19492018-09-16 22:38:02 -07001157 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001159 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001160
Benjamin Petersonddd19492018-09-16 22:38:02 -07001161 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001162 PyObject *value = GETLOCAL(oparg);
1163 if (value == NULL) {
1164 format_exc_check_arg(PyExc_UnboundLocalError,
1165 UNBOUNDLOCAL_ERROR_MSG,
1166 PyTuple_GetItem(co->co_varnames, oparg));
1167 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001169 Py_INCREF(value);
1170 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001172 }
1173
Benjamin Petersonddd19492018-09-16 22:38:02 -07001174 case TARGET(LOAD_CONST): {
1175 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001176 PyObject *value = GETITEM(consts, oparg);
1177 Py_INCREF(value);
1178 PUSH(value);
1179 FAST_DISPATCH();
1180 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001181
Benjamin Petersonddd19492018-09-16 22:38:02 -07001182 case TARGET(STORE_FAST): {
1183 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001184 PyObject *value = POP();
1185 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001187 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001188
Benjamin Petersonddd19492018-09-16 22:38:02 -07001189 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001190 PyObject *value = POP();
1191 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001193 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001194
Benjamin Petersonddd19492018-09-16 22:38:02 -07001195 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001196 PyObject *top = TOP();
1197 PyObject *second = SECOND();
1198 SET_TOP(second);
1199 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001201 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001202
Benjamin Petersonddd19492018-09-16 22:38:02 -07001203 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001204 PyObject *top = TOP();
1205 PyObject *second = SECOND();
1206 PyObject *third = THIRD();
1207 SET_TOP(second);
1208 SET_SECOND(third);
1209 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001211 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001212
Benjamin Petersonddd19492018-09-16 22:38:02 -07001213 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001214 PyObject *top = TOP();
1215 PyObject *second = SECOND();
1216 PyObject *third = THIRD();
1217 PyObject *fourth = FOURTH();
1218 SET_TOP(second);
1219 SET_SECOND(third);
1220 SET_THIRD(fourth);
1221 SET_FOURTH(top);
1222 FAST_DISPATCH();
1223 }
1224
Benjamin Petersonddd19492018-09-16 22:38:02 -07001225 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001226 PyObject *top = TOP();
1227 Py_INCREF(top);
1228 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001229 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001230 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001231
Benjamin Petersonddd19492018-09-16 22:38:02 -07001232 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001233 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001234 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001235 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001236 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001237 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001238 SET_TOP(top);
1239 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001240 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001241 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001242
Benjamin Petersonddd19492018-09-16 22:38:02 -07001243 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001244 PyObject *value = TOP();
1245 PyObject *res = PyNumber_Positive(value);
1246 Py_DECREF(value);
1247 SET_TOP(res);
1248 if (res == NULL)
1249 goto error;
1250 DISPATCH();
1251 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001252
Benjamin Petersonddd19492018-09-16 22:38:02 -07001253 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001254 PyObject *value = TOP();
1255 PyObject *res = PyNumber_Negative(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(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001264 PyObject *value = TOP();
1265 int err = PyObject_IsTrue(value);
1266 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 if (err == 0) {
1268 Py_INCREF(Py_True);
1269 SET_TOP(Py_True);
1270 DISPATCH();
1271 }
1272 else if (err > 0) {
1273 Py_INCREF(Py_False);
1274 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 DISPATCH();
1276 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001277 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001278 goto error;
1279 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001280
Benjamin Petersonddd19492018-09-16 22:38:02 -07001281 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001282 PyObject *value = TOP();
1283 PyObject *res = PyNumber_Invert(value);
1284 Py_DECREF(value);
1285 SET_TOP(res);
1286 if (res == NULL)
1287 goto error;
1288 DISPATCH();
1289 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001290
Benjamin Petersonddd19492018-09-16 22:38:02 -07001291 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001292 PyObject *exp = POP();
1293 PyObject *base = TOP();
1294 PyObject *res = PyNumber_Power(base, exp, Py_None);
1295 Py_DECREF(base);
1296 Py_DECREF(exp);
1297 SET_TOP(res);
1298 if (res == NULL)
1299 goto error;
1300 DISPATCH();
1301 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001302
Benjamin Petersonddd19492018-09-16 22:38:02 -07001303 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001304 PyObject *right = POP();
1305 PyObject *left = TOP();
1306 PyObject *res = PyNumber_Multiply(left, right);
1307 Py_DECREF(left);
1308 Py_DECREF(right);
1309 SET_TOP(res);
1310 if (res == NULL)
1311 goto error;
1312 DISPATCH();
1313 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001314
Benjamin Petersonddd19492018-09-16 22:38:02 -07001315 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001316 PyObject *right = POP();
1317 PyObject *left = TOP();
1318 PyObject *res = PyNumber_MatrixMultiply(left, right);
1319 Py_DECREF(left);
1320 Py_DECREF(right);
1321 SET_TOP(res);
1322 if (res == NULL)
1323 goto error;
1324 DISPATCH();
1325 }
1326
Benjamin Petersonddd19492018-09-16 22:38:02 -07001327 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001328 PyObject *divisor = POP();
1329 PyObject *dividend = TOP();
1330 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1331 Py_DECREF(dividend);
1332 Py_DECREF(divisor);
1333 SET_TOP(quotient);
1334 if (quotient == NULL)
1335 goto error;
1336 DISPATCH();
1337 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001338
Benjamin Petersonddd19492018-09-16 22:38:02 -07001339 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001340 PyObject *divisor = POP();
1341 PyObject *dividend = TOP();
1342 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1343 Py_DECREF(dividend);
1344 Py_DECREF(divisor);
1345 SET_TOP(quotient);
1346 if (quotient == NULL)
1347 goto error;
1348 DISPATCH();
1349 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001350
Benjamin Petersonddd19492018-09-16 22:38:02 -07001351 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001352 PyObject *divisor = POP();
1353 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001354 PyObject *res;
1355 if (PyUnicode_CheckExact(dividend) && (
1356 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1357 // fast path; string formatting, but not if the RHS is a str subclass
1358 // (see issue28598)
1359 res = PyUnicode_Format(dividend, divisor);
1360 } else {
1361 res = PyNumber_Remainder(dividend, divisor);
1362 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001363 Py_DECREF(divisor);
1364 Py_DECREF(dividend);
1365 SET_TOP(res);
1366 if (res == NULL)
1367 goto error;
1368 DISPATCH();
1369 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001370
Benjamin Petersonddd19492018-09-16 22:38:02 -07001371 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001372 PyObject *right = POP();
1373 PyObject *left = TOP();
1374 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001375 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1376 CPython using bytecode, it is simply worthless.
1377 See http://bugs.python.org/issue21955 and
1378 http://bugs.python.org/issue10044 for the discussion. In short,
1379 no patch shown any impact on a realistic benchmark, only a minor
1380 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001381 if (PyUnicode_CheckExact(left) &&
1382 PyUnicode_CheckExact(right)) {
1383 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001384 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001385 }
1386 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001387 sum = PyNumber_Add(left, right);
1388 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001389 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001390 Py_DECREF(right);
1391 SET_TOP(sum);
1392 if (sum == NULL)
1393 goto error;
1394 DISPATCH();
1395 }
1396
Benjamin Petersonddd19492018-09-16 22:38:02 -07001397 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001398 PyObject *right = POP();
1399 PyObject *left = TOP();
1400 PyObject *diff = PyNumber_Subtract(left, right);
1401 Py_DECREF(right);
1402 Py_DECREF(left);
1403 SET_TOP(diff);
1404 if (diff == NULL)
1405 goto error;
1406 DISPATCH();
1407 }
1408
Benjamin Petersonddd19492018-09-16 22:38:02 -07001409 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001410 PyObject *sub = POP();
1411 PyObject *container = TOP();
1412 PyObject *res = PyObject_GetItem(container, sub);
1413 Py_DECREF(container);
1414 Py_DECREF(sub);
1415 SET_TOP(res);
1416 if (res == NULL)
1417 goto error;
1418 DISPATCH();
1419 }
1420
Benjamin Petersonddd19492018-09-16 22:38:02 -07001421 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001422 PyObject *right = POP();
1423 PyObject *left = TOP();
1424 PyObject *res = PyNumber_Lshift(left, right);
1425 Py_DECREF(left);
1426 Py_DECREF(right);
1427 SET_TOP(res);
1428 if (res == NULL)
1429 goto error;
1430 DISPATCH();
1431 }
1432
Benjamin Petersonddd19492018-09-16 22:38:02 -07001433 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001434 PyObject *right = POP();
1435 PyObject *left = TOP();
1436 PyObject *res = PyNumber_Rshift(left, right);
1437 Py_DECREF(left);
1438 Py_DECREF(right);
1439 SET_TOP(res);
1440 if (res == NULL)
1441 goto error;
1442 DISPATCH();
1443 }
1444
Benjamin Petersonddd19492018-09-16 22:38:02 -07001445 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001446 PyObject *right = POP();
1447 PyObject *left = TOP();
1448 PyObject *res = PyNumber_And(left, right);
1449 Py_DECREF(left);
1450 Py_DECREF(right);
1451 SET_TOP(res);
1452 if (res == NULL)
1453 goto error;
1454 DISPATCH();
1455 }
1456
Benjamin Petersonddd19492018-09-16 22:38:02 -07001457 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001458 PyObject *right = POP();
1459 PyObject *left = TOP();
1460 PyObject *res = PyNumber_Xor(left, right);
1461 Py_DECREF(left);
1462 Py_DECREF(right);
1463 SET_TOP(res);
1464 if (res == NULL)
1465 goto error;
1466 DISPATCH();
1467 }
1468
Benjamin Petersonddd19492018-09-16 22:38:02 -07001469 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001470 PyObject *right = POP();
1471 PyObject *left = TOP();
1472 PyObject *res = PyNumber_Or(left, right);
1473 Py_DECREF(left);
1474 Py_DECREF(right);
1475 SET_TOP(res);
1476 if (res == NULL)
1477 goto error;
1478 DISPATCH();
1479 }
1480
Benjamin Petersonddd19492018-09-16 22:38:02 -07001481 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001482 PyObject *v = POP();
1483 PyObject *list = PEEK(oparg);
1484 int err;
1485 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001486 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001487 if (err != 0)
1488 goto error;
1489 PREDICT(JUMP_ABSOLUTE);
1490 DISPATCH();
1491 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001492
Benjamin Petersonddd19492018-09-16 22:38:02 -07001493 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001494 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001495 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001496 int err;
1497 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001498 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001499 if (err != 0)
1500 goto error;
1501 PREDICT(JUMP_ABSOLUTE);
1502 DISPATCH();
1503 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001504
Benjamin Petersonddd19492018-09-16 22:38:02 -07001505 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001506 PyObject *exp = POP();
1507 PyObject *base = TOP();
1508 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1509 Py_DECREF(base);
1510 Py_DECREF(exp);
1511 SET_TOP(res);
1512 if (res == NULL)
1513 goto error;
1514 DISPATCH();
1515 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001516
Benjamin Petersonddd19492018-09-16 22:38:02 -07001517 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001518 PyObject *right = POP();
1519 PyObject *left = TOP();
1520 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1521 Py_DECREF(left);
1522 Py_DECREF(right);
1523 SET_TOP(res);
1524 if (res == NULL)
1525 goto error;
1526 DISPATCH();
1527 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001528
Benjamin Petersonddd19492018-09-16 22:38:02 -07001529 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001530 PyObject *right = POP();
1531 PyObject *left = TOP();
1532 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1533 Py_DECREF(left);
1534 Py_DECREF(right);
1535 SET_TOP(res);
1536 if (res == NULL)
1537 goto error;
1538 DISPATCH();
1539 }
1540
Benjamin Petersonddd19492018-09-16 22:38:02 -07001541 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001542 PyObject *divisor = POP();
1543 PyObject *dividend = TOP();
1544 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1545 Py_DECREF(dividend);
1546 Py_DECREF(divisor);
1547 SET_TOP(quotient);
1548 if (quotient == NULL)
1549 goto error;
1550 DISPATCH();
1551 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001552
Benjamin Petersonddd19492018-09-16 22:38:02 -07001553 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001554 PyObject *divisor = POP();
1555 PyObject *dividend = TOP();
1556 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1557 Py_DECREF(dividend);
1558 Py_DECREF(divisor);
1559 SET_TOP(quotient);
1560 if (quotient == NULL)
1561 goto error;
1562 DISPATCH();
1563 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001564
Benjamin Petersonddd19492018-09-16 22:38:02 -07001565 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001566 PyObject *right = POP();
1567 PyObject *left = TOP();
1568 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1569 Py_DECREF(left);
1570 Py_DECREF(right);
1571 SET_TOP(mod);
1572 if (mod == NULL)
1573 goto error;
1574 DISPATCH();
1575 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001576
Benjamin Petersonddd19492018-09-16 22:38:02 -07001577 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001578 PyObject *right = POP();
1579 PyObject *left = TOP();
1580 PyObject *sum;
1581 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1582 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001583 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001584 }
1585 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001586 sum = PyNumber_InPlaceAdd(left, right);
1587 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001588 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001589 Py_DECREF(right);
1590 SET_TOP(sum);
1591 if (sum == NULL)
1592 goto error;
1593 DISPATCH();
1594 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001595
Benjamin Petersonddd19492018-09-16 22:38:02 -07001596 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001597 PyObject *right = POP();
1598 PyObject *left = TOP();
1599 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1600 Py_DECREF(left);
1601 Py_DECREF(right);
1602 SET_TOP(diff);
1603 if (diff == NULL)
1604 goto error;
1605 DISPATCH();
1606 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001607
Benjamin Petersonddd19492018-09-16 22:38:02 -07001608 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001609 PyObject *right = POP();
1610 PyObject *left = TOP();
1611 PyObject *res = PyNumber_InPlaceLshift(left, right);
1612 Py_DECREF(left);
1613 Py_DECREF(right);
1614 SET_TOP(res);
1615 if (res == NULL)
1616 goto error;
1617 DISPATCH();
1618 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001619
Benjamin Petersonddd19492018-09-16 22:38:02 -07001620 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001621 PyObject *right = POP();
1622 PyObject *left = TOP();
1623 PyObject *res = PyNumber_InPlaceRshift(left, right);
1624 Py_DECREF(left);
1625 Py_DECREF(right);
1626 SET_TOP(res);
1627 if (res == NULL)
1628 goto error;
1629 DISPATCH();
1630 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001631
Benjamin Petersonddd19492018-09-16 22:38:02 -07001632 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001633 PyObject *right = POP();
1634 PyObject *left = TOP();
1635 PyObject *res = PyNumber_InPlaceAnd(left, right);
1636 Py_DECREF(left);
1637 Py_DECREF(right);
1638 SET_TOP(res);
1639 if (res == NULL)
1640 goto error;
1641 DISPATCH();
1642 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001643
Benjamin Petersonddd19492018-09-16 22:38:02 -07001644 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001645 PyObject *right = POP();
1646 PyObject *left = TOP();
1647 PyObject *res = PyNumber_InPlaceXor(left, right);
1648 Py_DECREF(left);
1649 Py_DECREF(right);
1650 SET_TOP(res);
1651 if (res == NULL)
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(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001657 PyObject *right = POP();
1658 PyObject *left = TOP();
1659 PyObject *res = PyNumber_InPlaceOr(left, right);
1660 Py_DECREF(left);
1661 Py_DECREF(right);
1662 SET_TOP(res);
1663 if (res == NULL)
1664 goto error;
1665 DISPATCH();
1666 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001667
Benjamin Petersonddd19492018-09-16 22:38:02 -07001668 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001669 PyObject *sub = TOP();
1670 PyObject *container = SECOND();
1671 PyObject *v = THIRD();
1672 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001673 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001674 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001675 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001677 Py_DECREF(container);
1678 Py_DECREF(sub);
1679 if (err != 0)
1680 goto error;
1681 DISPATCH();
1682 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001683
Benjamin Petersonddd19492018-09-16 22:38:02 -07001684 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001685 PyObject *sub = TOP();
1686 PyObject *container = SECOND();
1687 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001688 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00001689 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001690 err = PyObject_DelItem(container, sub);
1691 Py_DECREF(container);
1692 Py_DECREF(sub);
1693 if (err != 0)
1694 goto error;
1695 DISPATCH();
1696 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001697
Benjamin Petersonddd19492018-09-16 22:38:02 -07001698 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01001699 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001700 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001701 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001702 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001703 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001704 PyErr_SetString(PyExc_RuntimeError,
1705 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001706 Py_DECREF(value);
1707 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001708 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001709 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001710 Py_DECREF(value);
1711 if (res == NULL)
1712 goto error;
1713 Py_DECREF(res);
1714 DISPATCH();
1715 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001716
Benjamin Petersonddd19492018-09-16 22:38:02 -07001717 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001718 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 switch (oparg) {
1720 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001721 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001722 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001723 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001724 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001725 /* fall through */
1726 case 0:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001727 if (do_raise(exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001728 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001729 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001730 break;
1731 default:
1732 PyErr_SetString(PyExc_SystemError,
1733 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001734 break;
1735 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001736 goto error;
1737 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001738
Benjamin Petersonddd19492018-09-16 22:38:02 -07001739 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001740 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001741 assert(f->f_iblock == 0);
1742 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001743 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001744
Benjamin Petersonddd19492018-09-16 22:38:02 -07001745 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001746 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001747 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001748 PyObject *obj = TOP();
1749 PyTypeObject *type = Py_TYPE(obj);
1750
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001751 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001752 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001753 }
Yury Selivanov75445082015-05-11 22:57:16 -04001754
1755 if (getter != NULL) {
1756 iter = (*getter)(obj);
1757 Py_DECREF(obj);
1758 if (iter == NULL) {
1759 SET_TOP(NULL);
1760 goto error;
1761 }
1762 }
1763 else {
1764 SET_TOP(NULL);
1765 PyErr_Format(
1766 PyExc_TypeError,
1767 "'async for' requires an object with "
1768 "__aiter__ method, got %.100s",
1769 type->tp_name);
1770 Py_DECREF(obj);
1771 goto error;
1772 }
1773
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001774 if (Py_TYPE(iter)->tp_as_async == NULL ||
1775 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001776
Yury Selivanov398ff912017-03-02 22:20:00 -05001777 SET_TOP(NULL);
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001778 PyErr_Format(
1779 PyExc_TypeError,
1780 "'async for' received an object from __aiter__ "
1781 "that does not implement __anext__: %.100s",
1782 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001783 Py_DECREF(iter);
1784 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001785 }
1786
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001787 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001788 DISPATCH();
1789 }
1790
Benjamin Petersonddd19492018-09-16 22:38:02 -07001791 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001792 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001793 PyObject *next_iter = NULL;
1794 PyObject *awaitable = NULL;
1795 PyObject *aiter = TOP();
1796 PyTypeObject *type = Py_TYPE(aiter);
1797
Yury Selivanoveb636452016-09-08 22:01:51 -07001798 if (PyAsyncGen_CheckExact(aiter)) {
1799 awaitable = type->tp_as_async->am_anext(aiter);
1800 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001801 goto error;
1802 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001803 } else {
1804 if (type->tp_as_async != NULL){
1805 getter = type->tp_as_async->am_anext;
1806 }
Yury Selivanov75445082015-05-11 22:57:16 -04001807
Yury Selivanoveb636452016-09-08 22:01:51 -07001808 if (getter != NULL) {
1809 next_iter = (*getter)(aiter);
1810 if (next_iter == NULL) {
1811 goto error;
1812 }
1813 }
1814 else {
1815 PyErr_Format(
1816 PyExc_TypeError,
1817 "'async for' requires an iterator with "
1818 "__anext__ method, got %.100s",
1819 type->tp_name);
1820 goto error;
1821 }
Yury Selivanov75445082015-05-11 22:57:16 -04001822
Yury Selivanoveb636452016-09-08 22:01:51 -07001823 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1824 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001825 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001826 PyExc_TypeError,
1827 "'async for' received an invalid object "
1828 "from __anext__: %.100s",
1829 Py_TYPE(next_iter)->tp_name);
1830
1831 Py_DECREF(next_iter);
1832 goto error;
1833 } else {
1834 Py_DECREF(next_iter);
1835 }
1836 }
Yury Selivanov75445082015-05-11 22:57:16 -04001837
1838 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001839 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001840 DISPATCH();
1841 }
1842
Benjamin Petersonddd19492018-09-16 22:38:02 -07001843 case TARGET(GET_AWAITABLE): {
1844 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04001845 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04001846 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04001847
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03001848 if (iter == NULL) {
1849 format_awaitable_error(Py_TYPE(iterable),
1850 _Py_OPCODE(next_instr[-2]));
1851 }
1852
Yury Selivanov75445082015-05-11 22:57:16 -04001853 Py_DECREF(iterable);
1854
Yury Selivanovc724bae2016-03-02 11:30:46 -05001855 if (iter != NULL && PyCoro_CheckExact(iter)) {
1856 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
1857 if (yf != NULL) {
1858 /* `iter` is a coroutine object that is being
1859 awaited, `yf` is a pointer to the current awaitable
1860 being awaited on. */
1861 Py_DECREF(yf);
1862 Py_CLEAR(iter);
1863 PyErr_SetString(
1864 PyExc_RuntimeError,
1865 "coroutine is being awaited already");
1866 /* The code below jumps to `error` if `iter` is NULL. */
1867 }
1868 }
1869
Yury Selivanov75445082015-05-11 22:57:16 -04001870 SET_TOP(iter); /* Even if it's NULL */
1871
1872 if (iter == NULL) {
1873 goto error;
1874 }
1875
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001876 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001877 DISPATCH();
1878 }
1879
Benjamin Petersonddd19492018-09-16 22:38:02 -07001880 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001881 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001882 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001883 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001884 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
1885 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001886 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001887 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001888 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001889 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001890 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001891 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001892 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001893 Py_DECREF(v);
1894 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001895 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08001896 if (tstate->c_tracefunc != NULL
1897 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001898 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10001899 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001900 if (err < 0)
1901 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001902 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001903 SET_TOP(val);
1904 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001905 }
Martin Panter95f53c12016-07-18 08:23:26 +00001906 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001907 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001908 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01001909 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03001910 f->f_lasti -= sizeof(_Py_CODEUNIT);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001911 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001912 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001913
Benjamin Petersonddd19492018-09-16 22:38:02 -07001914 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001915 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07001916
1917 if (co->co_flags & CO_ASYNC_GENERATOR) {
1918 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
1919 Py_DECREF(retval);
1920 if (w == NULL) {
1921 retval = NULL;
1922 goto error;
1923 }
1924 retval = w;
1925 }
1926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001927 f->f_stacktop = stack_pointer;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001928 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001929 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001930
Benjamin Petersonddd19492018-09-16 22:38:02 -07001931 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001932 PyObject *type, *value, *traceback;
1933 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001934 PyTryBlock *b = PyFrame_BlockPop(f);
1935 if (b->b_type != EXCEPT_HANDLER) {
1936 PyErr_SetString(PyExc_SystemError,
1937 "popped block is not an except handler");
1938 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001939 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001940 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
1941 STACK_LEVEL() <= (b)->b_level + 4);
1942 exc_info = tstate->exc_info;
1943 type = exc_info->exc_type;
1944 value = exc_info->exc_value;
1945 traceback = exc_info->exc_traceback;
1946 exc_info->exc_type = POP();
1947 exc_info->exc_value = POP();
1948 exc_info->exc_traceback = POP();
1949 Py_XDECREF(type);
1950 Py_XDECREF(value);
1951 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001952 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001953 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001954
Benjamin Petersonddd19492018-09-16 22:38:02 -07001955 case TARGET(POP_BLOCK): {
1956 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001957 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001958 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001959 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001960
Benjamin Petersonddd19492018-09-16 22:38:02 -07001961 case TARGET(POP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001962 /* If oparg is 0 at the top of the stack are 1 or 6 values:
1963 Either:
1964 - TOP = NULL or an integer
1965 or:
1966 - (TOP, SECOND, THIRD) = exc_info()
1967 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
1968
1969 If oparg is 1 the value for 'return' was additionally pushed
1970 at the top of the stack.
1971 */
1972 PyObject *res = NULL;
1973 if (oparg) {
1974 res = POP();
1975 }
1976 PyObject *exc = POP();
1977 if (exc == NULL || PyLong_CheckExact(exc)) {
1978 Py_XDECREF(exc);
1979 }
1980 else {
1981 Py_DECREF(exc);
1982 Py_DECREF(POP());
1983 Py_DECREF(POP());
1984
1985 PyObject *type, *value, *traceback;
1986 _PyErr_StackItem *exc_info;
1987 PyTryBlock *b = PyFrame_BlockPop(f);
1988 if (b->b_type != EXCEPT_HANDLER) {
1989 PyErr_SetString(PyExc_SystemError,
1990 "popped block is not an except handler");
1991 Py_XDECREF(res);
1992 goto error;
1993 }
1994 assert(STACK_LEVEL() == (b)->b_level + 3);
1995 exc_info = tstate->exc_info;
1996 type = exc_info->exc_type;
1997 value = exc_info->exc_value;
1998 traceback = exc_info->exc_traceback;
1999 exc_info->exc_type = POP();
2000 exc_info->exc_value = POP();
2001 exc_info->exc_traceback = POP();
2002 Py_XDECREF(type);
2003 Py_XDECREF(value);
2004 Py_XDECREF(traceback);
2005 }
2006 if (oparg) {
2007 PUSH(res);
2008 }
2009 DISPATCH();
2010 }
2011
Benjamin Petersonddd19492018-09-16 22:38:02 -07002012 case TARGET(CALL_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002013 PyObject *ret = PyLong_FromLong(INSTR_OFFSET());
2014 if (ret == NULL) {
2015 goto error;
2016 }
2017 PUSH(ret);
2018 JUMPBY(oparg);
2019 FAST_DISPATCH();
2020 }
2021
Benjamin Petersonddd19492018-09-16 22:38:02 -07002022 case TARGET(BEGIN_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002023 /* Push NULL onto the stack for using it in END_FINALLY,
2024 POP_FINALLY, WITH_CLEANUP_START and WITH_CLEANUP_FINISH.
2025 */
2026 PUSH(NULL);
2027 FAST_DISPATCH();
2028 }
2029
Benjamin Petersonddd19492018-09-16 22:38:02 -07002030 case TARGET(END_FINALLY): {
2031 PREDICTED(END_FINALLY);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002032 /* At the top of the stack are 1 or 6 values:
2033 Either:
2034 - TOP = NULL or an integer
2035 or:
2036 - (TOP, SECOND, THIRD) = exc_info()
2037 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2038 */
2039 PyObject *exc = POP();
2040 if (exc == NULL) {
2041 FAST_DISPATCH();
2042 }
2043 else if (PyLong_CheckExact(exc)) {
2044 int ret = _PyLong_AsInt(exc);
2045 Py_DECREF(exc);
2046 if (ret == -1 && PyErr_Occurred()) {
2047 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002048 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002049 JUMPTO(ret);
2050 FAST_DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002051 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002052 else {
2053 assert(PyExceptionClass_Check(exc));
2054 PyObject *val = POP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002055 PyObject *tb = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002056 PyErr_Restore(exc, val, tb);
2057 goto exception_unwind;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002058 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002059 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002060
Benjamin Petersonddd19492018-09-16 22:38:02 -07002061 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002062 PyObject *exc = POP();
2063 assert(PyExceptionClass_Check(exc));
2064 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2065 PyTryBlock *b = PyFrame_BlockPop(f);
2066 assert(b->b_type == EXCEPT_HANDLER);
2067 Py_DECREF(exc);
2068 UNWIND_EXCEPT_HANDLER(b);
2069 Py_DECREF(POP());
2070 JUMPBY(oparg);
2071 FAST_DISPATCH();
2072 }
2073 else {
2074 PyObject *val = POP();
2075 PyObject *tb = POP();
2076 PyErr_Restore(exc, val, tb);
2077 goto exception_unwind;
2078 }
2079 }
2080
Benjamin Petersonddd19492018-09-16 22:38:02 -07002081 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002082 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002083
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002084 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002085 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002086 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002087 if (bc == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002088 if (!PyErr_Occurred()) {
2089 PyErr_SetString(PyExc_NameError,
2090 "__build_class__ not found");
2091 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002092 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002093 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002094 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002095 }
2096 else {
2097 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2098 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002099 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002100 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2101 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002102 if (PyErr_ExceptionMatches(PyExc_KeyError))
2103 PyErr_SetString(PyExc_NameError,
2104 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002105 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002106 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002107 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002108 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002109 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002110 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002111
Benjamin Petersonddd19492018-09-16 22:38:02 -07002112 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002113 PyObject *name = GETITEM(names, oparg);
2114 PyObject *v = POP();
2115 PyObject *ns = f->f_locals;
2116 int err;
2117 if (ns == NULL) {
2118 PyErr_Format(PyExc_SystemError,
2119 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002120 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002121 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002122 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002123 if (PyDict_CheckExact(ns))
2124 err = PyDict_SetItem(ns, name, v);
2125 else
2126 err = PyObject_SetItem(ns, name, v);
2127 Py_DECREF(v);
2128 if (err != 0)
2129 goto error;
2130 DISPATCH();
2131 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002132
Benjamin Petersonddd19492018-09-16 22:38:02 -07002133 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002134 PyObject *name = GETITEM(names, oparg);
2135 PyObject *ns = f->f_locals;
2136 int err;
2137 if (ns == NULL) {
2138 PyErr_Format(PyExc_SystemError,
2139 "no locals when deleting %R", name);
2140 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002141 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002142 err = PyObject_DelItem(ns, name);
2143 if (err != 0) {
2144 format_exc_check_arg(PyExc_NameError,
2145 NAME_ERROR_MSG,
2146 name);
2147 goto error;
2148 }
2149 DISPATCH();
2150 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002151
Benjamin Petersonddd19492018-09-16 22:38:02 -07002152 case TARGET(UNPACK_SEQUENCE): {
2153 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002154 PyObject *seq = POP(), *item, **items;
2155 if (PyTuple_CheckExact(seq) &&
2156 PyTuple_GET_SIZE(seq) == oparg) {
2157 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002158 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002159 item = items[oparg];
2160 Py_INCREF(item);
2161 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002162 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002163 } else if (PyList_CheckExact(seq) &&
2164 PyList_GET_SIZE(seq) == oparg) {
2165 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002166 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002167 item = items[oparg];
2168 Py_INCREF(item);
2169 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002170 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002171 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002172 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002173 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002174 } else {
2175 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002176 Py_DECREF(seq);
2177 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002178 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002179 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002180 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002181 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002182
Benjamin Petersonddd19492018-09-16 22:38:02 -07002183 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002184 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2185 PyObject *seq = POP();
2186
2187 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2188 stack_pointer + totalargs)) {
2189 stack_pointer += totalargs;
2190 } else {
2191 Py_DECREF(seq);
2192 goto error;
2193 }
2194 Py_DECREF(seq);
2195 DISPATCH();
2196 }
2197
Benjamin Petersonddd19492018-09-16 22:38:02 -07002198 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002199 PyObject *name = GETITEM(names, oparg);
2200 PyObject *owner = TOP();
2201 PyObject *v = SECOND();
2202 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002203 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002204 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002205 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002206 Py_DECREF(owner);
2207 if (err != 0)
2208 goto error;
2209 DISPATCH();
2210 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002211
Benjamin Petersonddd19492018-09-16 22:38:02 -07002212 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002213 PyObject *name = GETITEM(names, oparg);
2214 PyObject *owner = POP();
2215 int err;
2216 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2217 Py_DECREF(owner);
2218 if (err != 0)
2219 goto error;
2220 DISPATCH();
2221 }
2222
Benjamin Petersonddd19492018-09-16 22:38:02 -07002223 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002224 PyObject *name = GETITEM(names, oparg);
2225 PyObject *v = POP();
2226 int err;
2227 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002228 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002229 if (err != 0)
2230 goto error;
2231 DISPATCH();
2232 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002233
Benjamin Petersonddd19492018-09-16 22:38:02 -07002234 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002235 PyObject *name = GETITEM(names, oparg);
2236 int err;
2237 err = PyDict_DelItem(f->f_globals, name);
2238 if (err != 0) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002239 if (PyErr_ExceptionMatches(PyExc_KeyError)) {
2240 format_exc_check_arg(
2241 PyExc_NameError, NAME_ERROR_MSG, name);
2242 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002243 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002244 }
2245 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002246 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002247
Benjamin Petersonddd19492018-09-16 22:38:02 -07002248 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002249 PyObject *name = GETITEM(names, oparg);
2250 PyObject *locals = f->f_locals;
2251 PyObject *v;
2252 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002253 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002254 "no locals when loading %R", name);
2255 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002256 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002257 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002258 v = PyDict_GetItemWithError(locals, name);
2259 if (v != NULL) {
2260 Py_INCREF(v);
2261 }
2262 else if (PyErr_Occurred()) {
2263 goto error;
2264 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002265 }
2266 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002267 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002268 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002269 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2270 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002271 PyErr_Clear();
2272 }
2273 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002274 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002275 v = PyDict_GetItemWithError(f->f_globals, name);
2276 if (v != NULL) {
2277 Py_INCREF(v);
2278 }
2279 else if (PyErr_Occurred()) {
2280 goto error;
2281 }
2282 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002283 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002284 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002285 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002286 if (!PyErr_Occurred()) {
2287 format_exc_check_arg(
Victor Stinnerb0b22422012-04-19 00:57:45 +02002288 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002289 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002290 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002291 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002292 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002293 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002294 }
2295 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002296 v = PyObject_GetItem(f->f_builtins, name);
2297 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002298 if (PyErr_ExceptionMatches(PyExc_KeyError))
2299 format_exc_check_arg(
2300 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002301 NAME_ERROR_MSG, name);
2302 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002303 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002304 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002305 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002306 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002307 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002308 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002309 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002310
Benjamin Petersonddd19492018-09-16 22:38:02 -07002311 case TARGET(LOAD_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002312 PyObject *name = GETITEM(names, oparg);
2313 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002314 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002315 && PyDict_CheckExact(f->f_builtins))
2316 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002317 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002318 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002319 name);
2320 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002321 if (!_PyErr_OCCURRED()) {
2322 /* _PyDict_LoadGlobal() returns NULL without raising
2323 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002324 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002325 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002326 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002327 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002328 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002329 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002330 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002331 else {
2332 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002333
2334 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002335 v = PyObject_GetItem(f->f_globals, name);
2336 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002337 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2338 goto error;
2339 PyErr_Clear();
2340
Victor Stinnerb4efc962015-11-20 09:24:02 +01002341 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002342 v = PyObject_GetItem(f->f_builtins, name);
2343 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002344 if (PyErr_ExceptionMatches(PyExc_KeyError))
2345 format_exc_check_arg(
2346 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002347 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002348 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002349 }
2350 }
2351 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002352 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002353 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002354 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002355
Benjamin Petersonddd19492018-09-16 22:38:02 -07002356 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002357 PyObject *v = GETLOCAL(oparg);
2358 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002359 SETLOCAL(oparg, NULL);
2360 DISPATCH();
2361 }
2362 format_exc_check_arg(
2363 PyExc_UnboundLocalError,
2364 UNBOUNDLOCAL_ERROR_MSG,
2365 PyTuple_GetItem(co->co_varnames, oparg)
2366 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002367 goto error;
2368 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002369
Benjamin Petersonddd19492018-09-16 22:38:02 -07002370 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002371 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002372 PyObject *oldobj = PyCell_GET(cell);
2373 if (oldobj != NULL) {
2374 PyCell_SET(cell, NULL);
2375 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002376 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002377 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002378 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002379 goto error;
2380 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002381
Benjamin Petersonddd19492018-09-16 22:38:02 -07002382 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002383 PyObject *cell = freevars[oparg];
2384 Py_INCREF(cell);
2385 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002386 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002387 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002388
Benjamin Petersonddd19492018-09-16 22:38:02 -07002389 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002390 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002391 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002392 assert(locals);
2393 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2394 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2395 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2396 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2397 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002398 value = PyDict_GetItemWithError(locals, name);
2399 if (value != NULL) {
2400 Py_INCREF(value);
2401 }
2402 else if (PyErr_Occurred()) {
2403 goto error;
2404 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002405 }
2406 else {
2407 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002408 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002409 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2410 goto error;
2411 PyErr_Clear();
2412 }
2413 }
2414 if (!value) {
2415 PyObject *cell = freevars[oparg];
2416 value = PyCell_GET(cell);
2417 if (value == NULL) {
2418 format_exc_unbound(co, oparg);
2419 goto error;
2420 }
2421 Py_INCREF(value);
2422 }
2423 PUSH(value);
2424 DISPATCH();
2425 }
2426
Benjamin Petersonddd19492018-09-16 22:38:02 -07002427 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002428 PyObject *cell = freevars[oparg];
2429 PyObject *value = PyCell_GET(cell);
2430 if (value == NULL) {
2431 format_exc_unbound(co, oparg);
2432 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002433 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002434 Py_INCREF(value);
2435 PUSH(value);
2436 DISPATCH();
2437 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002438
Benjamin Petersonddd19492018-09-16 22:38:02 -07002439 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002440 PyObject *v = POP();
2441 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002442 PyObject *oldobj = PyCell_GET(cell);
2443 PyCell_SET(cell, v);
2444 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002445 DISPATCH();
2446 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002447
Benjamin Petersonddd19492018-09-16 22:38:02 -07002448 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002449 PyObject *str;
2450 PyObject *empty = PyUnicode_New(0, 0);
2451 if (empty == NULL) {
2452 goto error;
2453 }
2454 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2455 Py_DECREF(empty);
2456 if (str == NULL)
2457 goto error;
2458 while (--oparg >= 0) {
2459 PyObject *item = POP();
2460 Py_DECREF(item);
2461 }
2462 PUSH(str);
2463 DISPATCH();
2464 }
2465
Benjamin Petersonddd19492018-09-16 22:38:02 -07002466 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002467 PyObject *tup = PyTuple_New(oparg);
2468 if (tup == NULL)
2469 goto error;
2470 while (--oparg >= 0) {
2471 PyObject *item = POP();
2472 PyTuple_SET_ITEM(tup, oparg, item);
2473 }
2474 PUSH(tup);
2475 DISPATCH();
2476 }
2477
Benjamin Petersonddd19492018-09-16 22:38:02 -07002478 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002479 PyObject *list = PyList_New(oparg);
2480 if (list == NULL)
2481 goto error;
2482 while (--oparg >= 0) {
2483 PyObject *item = POP();
2484 PyList_SET_ITEM(list, oparg, item);
2485 }
2486 PUSH(list);
2487 DISPATCH();
2488 }
2489
Benjamin Petersonddd19492018-09-16 22:38:02 -07002490 case TARGET(BUILD_TUPLE_UNPACK_WITH_CALL):
2491 case TARGET(BUILD_TUPLE_UNPACK):
2492 case TARGET(BUILD_LIST_UNPACK): {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002493 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002494 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002495 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002496 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002497
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002498 if (sum == NULL)
2499 goto error;
2500
2501 for (i = oparg; i > 0; i--) {
2502 PyObject *none_val;
2503
2504 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2505 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002506 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002507 PyErr_ExceptionMatches(PyExc_TypeError))
2508 {
2509 check_args_iterable(PEEK(1 + oparg), PEEK(i));
Serhiy Storchaka73442852016-10-02 10:33:46 +03002510 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002511 Py_DECREF(sum);
2512 goto error;
2513 }
2514 Py_DECREF(none_val);
2515 }
2516
2517 if (convert_to_tuple) {
2518 return_value = PyList_AsTuple(sum);
2519 Py_DECREF(sum);
2520 if (return_value == NULL)
2521 goto error;
2522 }
2523 else {
2524 return_value = sum;
2525 }
2526
2527 while (oparg--)
2528 Py_DECREF(POP());
2529 PUSH(return_value);
2530 DISPATCH();
2531 }
2532
Benjamin Petersonddd19492018-09-16 22:38:02 -07002533 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002534 PyObject *set = PySet_New(NULL);
2535 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002536 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002537 if (set == NULL)
2538 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002539 for (i = oparg; i > 0; i--) {
2540 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002541 if (err == 0)
2542 err = PySet_Add(set, item);
2543 Py_DECREF(item);
2544 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002545 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002546 if (err != 0) {
2547 Py_DECREF(set);
2548 goto error;
2549 }
2550 PUSH(set);
2551 DISPATCH();
2552 }
2553
Benjamin Petersonddd19492018-09-16 22:38:02 -07002554 case TARGET(BUILD_SET_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002555 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002556 PyObject *sum = PySet_New(NULL);
2557 if (sum == NULL)
2558 goto error;
2559
2560 for (i = oparg; i > 0; i--) {
2561 if (_PySet_Update(sum, PEEK(i)) < 0) {
2562 Py_DECREF(sum);
2563 goto error;
2564 }
2565 }
2566
2567 while (oparg--)
2568 Py_DECREF(POP());
2569 PUSH(sum);
2570 DISPATCH();
2571 }
2572
Benjamin Petersonddd19492018-09-16 22:38:02 -07002573 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002574 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002575 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2576 if (map == NULL)
2577 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002578 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002579 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002580 PyObject *key = PEEK(2*i);
2581 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002582 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002583 if (err != 0) {
2584 Py_DECREF(map);
2585 goto error;
2586 }
2587 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002588
2589 while (oparg--) {
2590 Py_DECREF(POP());
2591 Py_DECREF(POP());
2592 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002593 PUSH(map);
2594 DISPATCH();
2595 }
2596
Benjamin Petersonddd19492018-09-16 22:38:02 -07002597 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002598 _Py_IDENTIFIER(__annotations__);
2599 int err;
2600 PyObject *ann_dict;
2601 if (f->f_locals == NULL) {
2602 PyErr_Format(PyExc_SystemError,
2603 "no locals found when setting up annotations");
2604 goto error;
2605 }
2606 /* check if __annotations__ in locals()... */
2607 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002608 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002609 &PyId___annotations__);
2610 if (ann_dict == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002611 if (PyErr_Occurred()) {
2612 goto error;
2613 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002614 /* ...if not, create a new one */
2615 ann_dict = PyDict_New();
2616 if (ann_dict == NULL) {
2617 goto error;
2618 }
2619 err = _PyDict_SetItemId(f->f_locals,
2620 &PyId___annotations__, ann_dict);
2621 Py_DECREF(ann_dict);
2622 if (err != 0) {
2623 goto error;
2624 }
2625 }
2626 }
2627 else {
2628 /* do the same if locals() is not a dict */
2629 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2630 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002631 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002632 }
2633 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2634 if (ann_dict == NULL) {
2635 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2636 goto error;
2637 }
2638 PyErr_Clear();
2639 ann_dict = PyDict_New();
2640 if (ann_dict == NULL) {
2641 goto error;
2642 }
2643 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2644 Py_DECREF(ann_dict);
2645 if (err != 0) {
2646 goto error;
2647 }
2648 }
2649 else {
2650 Py_DECREF(ann_dict);
2651 }
2652 }
2653 DISPATCH();
2654 }
2655
Benjamin Petersonddd19492018-09-16 22:38:02 -07002656 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002657 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002658 PyObject *map;
2659 PyObject *keys = TOP();
2660 if (!PyTuple_CheckExact(keys) ||
2661 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2662 PyErr_SetString(PyExc_SystemError,
2663 "bad BUILD_CONST_KEY_MAP keys argument");
2664 goto error;
2665 }
2666 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2667 if (map == NULL) {
2668 goto error;
2669 }
2670 for (i = oparg; i > 0; i--) {
2671 int err;
2672 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2673 PyObject *value = PEEK(i + 1);
2674 err = PyDict_SetItem(map, key, value);
2675 if (err != 0) {
2676 Py_DECREF(map);
2677 goto error;
2678 }
2679 }
2680
2681 Py_DECREF(POP());
2682 while (oparg--) {
2683 Py_DECREF(POP());
2684 }
2685 PUSH(map);
2686 DISPATCH();
2687 }
2688
Benjamin Petersonddd19492018-09-16 22:38:02 -07002689 case TARGET(BUILD_MAP_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002690 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002691 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002692 if (sum == NULL)
2693 goto error;
2694
2695 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002696 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002697 if (PyDict_Update(sum, arg) < 0) {
2698 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2699 PyErr_Format(PyExc_TypeError,
Berker Peksag8e9045d2016-10-02 13:08:25 +03002700 "'%.200s' object is not a mapping",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002701 arg->ob_type->tp_name);
2702 }
2703 Py_DECREF(sum);
2704 goto error;
2705 }
2706 }
2707
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002708 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002709 Py_DECREF(POP());
2710 PUSH(sum);
2711 DISPATCH();
2712 }
2713
Benjamin Petersonddd19492018-09-16 22:38:02 -07002714 case TARGET(BUILD_MAP_UNPACK_WITH_CALL): {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002715 Py_ssize_t i;
2716 PyObject *sum = PyDict_New();
2717 if (sum == NULL)
2718 goto error;
2719
2720 for (i = oparg; i > 0; i--) {
2721 PyObject *arg = PEEK(i);
2722 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002723 Py_DECREF(sum);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02002724 format_kwargs_error(PEEK(2 + oparg), arg);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002725 goto error;
2726 }
2727 }
2728
2729 while (oparg--)
2730 Py_DECREF(POP());
2731 PUSH(sum);
2732 DISPATCH();
2733 }
2734
Benjamin Petersonddd19492018-09-16 22:38:02 -07002735 case TARGET(MAP_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002736 PyObject *key = TOP();
2737 PyObject *value = SECOND();
2738 PyObject *map;
2739 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002740 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002741 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002742 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002743 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002744 Py_DECREF(value);
2745 Py_DECREF(key);
2746 if (err != 0)
2747 goto error;
2748 PREDICT(JUMP_ABSOLUTE);
2749 DISPATCH();
2750 }
2751
Benjamin Petersonddd19492018-09-16 22:38:02 -07002752 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002753 PyObject *name = GETITEM(names, oparg);
2754 PyObject *owner = TOP();
2755 PyObject *res = PyObject_GetAttr(owner, name);
2756 Py_DECREF(owner);
2757 SET_TOP(res);
2758 if (res == NULL)
2759 goto error;
2760 DISPATCH();
2761 }
2762
Benjamin Petersonddd19492018-09-16 22:38:02 -07002763 case TARGET(COMPARE_OP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002764 PyObject *right = POP();
2765 PyObject *left = TOP();
2766 PyObject *res = cmp_outcome(oparg, left, right);
2767 Py_DECREF(left);
2768 Py_DECREF(right);
2769 SET_TOP(res);
2770 if (res == NULL)
2771 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002772 PREDICT(POP_JUMP_IF_FALSE);
2773 PREDICT(POP_JUMP_IF_TRUE);
2774 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002775 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002776
Benjamin Petersonddd19492018-09-16 22:38:02 -07002777 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002778 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002779 PyObject *fromlist = POP();
2780 PyObject *level = TOP();
2781 PyObject *res;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002782 res = import_name(f, name, fromlist, level);
2783 Py_DECREF(level);
2784 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002785 SET_TOP(res);
2786 if (res == NULL)
2787 goto error;
2788 DISPATCH();
2789 }
2790
Benjamin Petersonddd19492018-09-16 22:38:02 -07002791 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002792 PyObject *from = POP(), *locals;
2793 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002794 if (PyFrame_FastToLocalsWithError(f) < 0) {
2795 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002796 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002797 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002798
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002799 locals = f->f_locals;
2800 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002801 PyErr_SetString(PyExc_SystemError,
2802 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002803 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002804 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002805 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002806 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002807 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002808 Py_DECREF(from);
2809 if (err != 0)
2810 goto error;
2811 DISPATCH();
2812 }
Guido van Rossum25831651993-05-19 14:50:45 +00002813
Benjamin Petersonddd19492018-09-16 22:38:02 -07002814 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002815 PyObject *name = GETITEM(names, oparg);
2816 PyObject *from = TOP();
2817 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002818 res = import_from(from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002819 PUSH(res);
2820 if (res == NULL)
2821 goto error;
2822 DISPATCH();
2823 }
Thomas Wouters52152252000-08-17 22:55:00 +00002824
Benjamin Petersonddd19492018-09-16 22:38:02 -07002825 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002826 JUMPBY(oparg);
2827 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002828 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002829
Benjamin Petersonddd19492018-09-16 22:38:02 -07002830 case TARGET(POP_JUMP_IF_FALSE): {
2831 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002832 PyObject *cond = POP();
2833 int err;
2834 if (cond == Py_True) {
2835 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002836 FAST_DISPATCH();
2837 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002838 if (cond == Py_False) {
2839 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002840 JUMPTO(oparg);
2841 FAST_DISPATCH();
2842 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002843 err = PyObject_IsTrue(cond);
2844 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002845 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07002846 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002847 else if (err == 0)
2848 JUMPTO(oparg);
2849 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002850 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002851 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002852 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002853
Benjamin Petersonddd19492018-09-16 22:38:02 -07002854 case TARGET(POP_JUMP_IF_TRUE): {
2855 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002856 PyObject *cond = POP();
2857 int err;
2858 if (cond == Py_False) {
2859 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002860 FAST_DISPATCH();
2861 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002862 if (cond == Py_True) {
2863 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002864 JUMPTO(oparg);
2865 FAST_DISPATCH();
2866 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002867 err = PyObject_IsTrue(cond);
2868 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002869 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002870 JUMPTO(oparg);
2871 }
2872 else if (err == 0)
2873 ;
2874 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002875 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002876 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002877 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002878
Benjamin Petersonddd19492018-09-16 22:38:02 -07002879 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002880 PyObject *cond = TOP();
2881 int err;
2882 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002883 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002884 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002885 FAST_DISPATCH();
2886 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002887 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002888 JUMPTO(oparg);
2889 FAST_DISPATCH();
2890 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002891 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002892 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002893 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002894 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002895 }
2896 else if (err == 0)
2897 JUMPTO(oparg);
2898 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002899 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002900 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002901 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002902
Benjamin Petersonddd19492018-09-16 22:38:02 -07002903 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002904 PyObject *cond = TOP();
2905 int err;
2906 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002907 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002908 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002909 FAST_DISPATCH();
2910 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002911 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002912 JUMPTO(oparg);
2913 FAST_DISPATCH();
2914 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002915 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002916 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002917 JUMPTO(oparg);
2918 }
2919 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002920 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002921 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002922 }
2923 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002924 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002925 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002926 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002927
Benjamin Petersonddd19492018-09-16 22:38:02 -07002928 case TARGET(JUMP_ABSOLUTE): {
2929 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002930 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002931#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002932 /* Enabling this path speeds-up all while and for-loops by bypassing
2933 the per-loop checks for signals. By default, this should be turned-off
2934 because it prevents detection of a control-break in tight loops like
2935 "while 1: pass". Compile with this option turned-on when you need
2936 the speed-up and do not need break checking inside tight loops (ones
2937 that contain only instructions ending with FAST_DISPATCH).
2938 */
2939 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002940#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002941 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002942#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002943 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002944
Benjamin Petersonddd19492018-09-16 22:38:02 -07002945 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002946 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002947 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002948 PyObject *iter = PyObject_GetIter(iterable);
2949 Py_DECREF(iterable);
2950 SET_TOP(iter);
2951 if (iter == NULL)
2952 goto error;
2953 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002954 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04002955 DISPATCH();
2956 }
2957
Benjamin Petersonddd19492018-09-16 22:38:02 -07002958 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04002959 /* before: [obj]; after [getiter(obj)] */
2960 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04002961 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04002962 if (PyCoro_CheckExact(iterable)) {
2963 /* `iterable` is a coroutine */
2964 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
2965 /* and it is used in a 'yield from' expression of a
2966 regular generator. */
2967 Py_DECREF(iterable);
2968 SET_TOP(NULL);
2969 PyErr_SetString(PyExc_TypeError,
2970 "cannot 'yield from' a coroutine object "
2971 "in a non-coroutine generator");
2972 goto error;
2973 }
2974 }
2975 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04002976 /* `iterable` is not a generator. */
2977 iter = PyObject_GetIter(iterable);
2978 Py_DECREF(iterable);
2979 SET_TOP(iter);
2980 if (iter == NULL)
2981 goto error;
2982 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002983 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002984 DISPATCH();
2985 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002986
Benjamin Petersonddd19492018-09-16 22:38:02 -07002987 case TARGET(FOR_ITER): {
2988 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002989 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002990 PyObject *iter = TOP();
2991 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
2992 if (next != NULL) {
2993 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002994 PREDICT(STORE_FAST);
2995 PREDICT(UNPACK_SEQUENCE);
2996 DISPATCH();
2997 }
2998 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002999 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
3000 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08003001 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003002 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003003 PyErr_Clear();
3004 }
3005 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003006 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003007 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003008 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003009 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003010 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003011 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003012
Benjamin Petersonddd19492018-09-16 22:38:02 -07003013 case TARGET(SETUP_FINALLY): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003014 /* NOTE: If you add any new block-setup opcodes that
3015 are not try/except/finally handlers, you may need
3016 to update the PyGen_NeedsFinalizing() function.
3017 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003018
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003019 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003020 STACK_LEVEL());
3021 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003022 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003023
Benjamin Petersonddd19492018-09-16 22:38:02 -07003024 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003025 _Py_IDENTIFIER(__aexit__);
3026 _Py_IDENTIFIER(__aenter__);
3027
3028 PyObject *mgr = TOP();
3029 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
3030 *enter;
3031 PyObject *res;
3032 if (exit == NULL)
3033 goto error;
3034 SET_TOP(exit);
3035 enter = special_lookup(mgr, &PyId___aenter__);
3036 Py_DECREF(mgr);
3037 if (enter == NULL)
3038 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003039 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003040 Py_DECREF(enter);
3041 if (res == NULL)
3042 goto error;
3043 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003044 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003045 DISPATCH();
3046 }
3047
Benjamin Petersonddd19492018-09-16 22:38:02 -07003048 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003049 PyObject *res = POP();
3050 /* Setup the finally block before pushing the result
3051 of __aenter__ on the stack. */
3052 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3053 STACK_LEVEL());
3054 PUSH(res);
3055 DISPATCH();
3056 }
3057
Benjamin Petersonddd19492018-09-16 22:38:02 -07003058 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003059 _Py_IDENTIFIER(__exit__);
3060 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003061 PyObject *mgr = TOP();
Raymond Hettingera3fec152016-11-21 17:24:23 -08003062 PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003063 PyObject *res;
Raymond Hettingera3fec152016-11-21 17:24:23 -08003064 if (enter == NULL)
3065 goto error;
3066 exit = special_lookup(mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003067 if (exit == NULL) {
3068 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003069 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003070 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003071 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003072 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003073 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003074 Py_DECREF(enter);
3075 if (res == NULL)
3076 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003077 /* Setup the finally block before pushing the result
3078 of __enter__ on the stack. */
3079 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3080 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003081
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003082 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003083 DISPATCH();
3084 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003085
Benjamin Petersonddd19492018-09-16 22:38:02 -07003086 case TARGET(WITH_CLEANUP_START): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003087 /* At the top of the stack are 1 or 6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003088 how/why we entered the finally clause:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003089 - TOP = NULL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003090 - (TOP, SECOND, THIRD) = exc_info()
3091 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003092 Below them is EXIT, the context.__exit__ or context.__aexit__
3093 bound method.
3094 In the first case, we must call
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003095 EXIT(None, None, None)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003096 otherwise we must call
3097 EXIT(TOP, SECOND, THIRD)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003098
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003099 In the first case, we remove EXIT from the
3100 stack, leaving TOP, and push TOP on the stack.
3101 Otherwise we shift the bottom 3 values of the
3102 stack down, replace the empty spot with NULL, and push
3103 None on the stack.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003104
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003105 Finally we push the result of the call.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003106 */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003107 PyObject *stack[3];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003108 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003109 PyObject *exc, *val, *tb, *res;
3110
3111 val = tb = Py_None;
3112 exc = TOP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003113 if (exc == NULL) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003114 STACK_SHRINK(1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003115 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003116 SET_TOP(exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003117 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003118 }
3119 else {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003120 assert(PyExceptionClass_Check(exc));
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003121 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003122 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003123 val = SECOND();
3124 tb = THIRD();
3125 tp2 = FOURTH();
3126 exc2 = PEEK(5);
3127 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003128 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003129 SET_VALUE(7, tb2);
3130 SET_VALUE(6, exc2);
3131 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003132 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3133 SET_FOURTH(NULL);
3134 /* We just shifted the stack down, so we have
3135 to tell the except handler block that the
3136 values are lower than it expects. */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003137 assert(f->f_iblock > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003138 block = &f->f_blockstack[f->f_iblock - 1];
3139 assert(block->b_type == EXCEPT_HANDLER);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003140 assert(block->b_level > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003141 block->b_level--;
3142 }
Victor Stinner842cfff2016-12-01 14:45:31 +01003143
3144 stack[0] = exc;
3145 stack[1] = val;
3146 stack[2] = tb;
3147 res = _PyObject_FastCall(exit_func, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003148 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003149 if (res == NULL)
3150 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003151
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003152 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003153 PUSH(exc);
3154 PUSH(res);
3155 PREDICT(WITH_CLEANUP_FINISH);
3156 DISPATCH();
3157 }
3158
Benjamin Petersonddd19492018-09-16 22:38:02 -07003159 case TARGET(WITH_CLEANUP_FINISH): {
3160 PREDICTED(WITH_CLEANUP_FINISH);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003161 /* TOP = the result of calling the context.__exit__ bound method
3162 SECOND = either None or exception type
3163
3164 If SECOND is None below is NULL or the return address,
3165 otherwise below are 7 values representing an exception.
3166 */
Yury Selivanov75445082015-05-11 22:57:16 -04003167 PyObject *res = POP();
3168 PyObject *exc = POP();
3169 int err;
3170
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003171 if (exc != Py_None)
3172 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003173 else
3174 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003175
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003176 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003177 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003178
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003179 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003180 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003181 else if (err > 0) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003182 /* There was an exception and a True return.
3183 * We must manually unwind the EXCEPT_HANDLER block
3184 * which was created when the exception was caught,
Quan Tian3bd0d622018-10-20 05:30:03 +08003185 * otherwise the stack will be in an inconsistent state.
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003186 */
3187 PyTryBlock *b = PyFrame_BlockPop(f);
3188 assert(b->b_type == EXCEPT_HANDLER);
3189 UNWIND_EXCEPT_HANDLER(b);
3190 PUSH(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003191 }
3192 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003193 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003194 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003195
Benjamin Petersonddd19492018-09-16 22:38:02 -07003196 case TARGET(LOAD_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003197 /* Designed to work in tamdem with CALL_METHOD. */
3198 PyObject *name = GETITEM(names, oparg);
3199 PyObject *obj = TOP();
3200 PyObject *meth = NULL;
3201
3202 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3203
Yury Selivanovf2392132016-12-13 19:03:51 -05003204 if (meth == NULL) {
3205 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003206 goto error;
3207 }
3208
3209 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003210 /* We can bypass temporary bound method object.
3211 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003212
INADA Naoki015bce62017-01-16 17:23:30 +09003213 meth | self | arg1 | ... | argN
3214 */
3215 SET_TOP(meth);
3216 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003217 }
3218 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003219 /* meth is not an unbound method (but a regular attr, or
3220 something was returned by a descriptor protocol). Set
3221 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003222 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003223
3224 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003225 */
INADA Naoki015bce62017-01-16 17:23:30 +09003226 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003227 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003228 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003229 }
3230 DISPATCH();
3231 }
3232
Benjamin Petersonddd19492018-09-16 22:38:02 -07003233 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003234 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003235 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003236
3237 sp = stack_pointer;
3238
INADA Naoki015bce62017-01-16 17:23:30 +09003239 meth = PEEK(oparg + 2);
3240 if (meth == NULL) {
3241 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3242 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003243
3244 Stack layout:
3245
INADA Naoki015bce62017-01-16 17:23:30 +09003246 ... | NULL | callable | arg1 | ... | argN
3247 ^- TOP()
3248 ^- (-oparg)
3249 ^- (-oparg-1)
3250 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003251
Ville Skyttä49b27342017-08-03 09:00:59 +03003252 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003253 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003254 */
Yury Selivanovf2392132016-12-13 19:03:51 -05003255 res = call_function(&sp, oparg, NULL);
3256 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003257 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003258 }
3259 else {
3260 /* This is a method call. Stack layout:
3261
INADA Naoki015bce62017-01-16 17:23:30 +09003262 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003263 ^- TOP()
3264 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003265 ^- (-oparg-1)
3266 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003267
INADA Naoki015bce62017-01-16 17:23:30 +09003268 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003269 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003270 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003271 */
3272 res = call_function(&sp, oparg + 1, NULL);
3273 stack_pointer = sp;
3274 }
3275
3276 PUSH(res);
3277 if (res == NULL)
3278 goto error;
3279 DISPATCH();
3280 }
3281
Benjamin Petersonddd19492018-09-16 22:38:02 -07003282 case TARGET(CALL_FUNCTION): {
3283 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003284 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003285 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003286 res = call_function(&sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003287 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003288 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003289 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003290 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003291 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003292 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003293 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003294
Benjamin Petersonddd19492018-09-16 22:38:02 -07003295 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003296 PyObject **sp, *res, *names;
3297
3298 names = POP();
3299 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003300 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003301 res = call_function(&sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003302 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003303 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003304 Py_DECREF(names);
3305
3306 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003307 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003308 }
3309 DISPATCH();
3310 }
3311
Benjamin Petersonddd19492018-09-16 22:38:02 -07003312 case TARGET(CALL_FUNCTION_EX): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003313 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003314 if (oparg & 0x01) {
3315 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003316 if (!PyDict_CheckExact(kwargs)) {
3317 PyObject *d = PyDict_New();
3318 if (d == NULL)
3319 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003320 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003321 Py_DECREF(d);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003322 format_kwargs_error(SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003323 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003324 goto error;
3325 }
3326 Py_DECREF(kwargs);
3327 kwargs = d;
3328 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003329 assert(PyDict_CheckExact(kwargs));
3330 }
3331 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003332 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003333 if (!PyTuple_CheckExact(callargs)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003334 if (check_args_iterable(func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003335 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003336 goto error;
3337 }
3338 Py_SETREF(callargs, PySequence_Tuple(callargs));
3339 if (callargs == NULL) {
3340 goto error;
3341 }
3342 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003343 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003344
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003345 result = do_call_core(func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003346 Py_DECREF(func);
3347 Py_DECREF(callargs);
3348 Py_XDECREF(kwargs);
3349
3350 SET_TOP(result);
3351 if (result == NULL) {
3352 goto error;
3353 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003354 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003355 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003356
Benjamin Petersonddd19492018-09-16 22:38:02 -07003357 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003358 PyObject *qualname = POP();
3359 PyObject *codeobj = POP();
3360 PyFunctionObject *func = (PyFunctionObject *)
3361 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003362
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003363 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003364 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003365 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003366 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003367 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003368
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003369 if (oparg & 0x08) {
3370 assert(PyTuple_CheckExact(TOP()));
3371 func ->func_closure = POP();
3372 }
3373 if (oparg & 0x04) {
3374 assert(PyDict_CheckExact(TOP()));
3375 func->func_annotations = POP();
3376 }
3377 if (oparg & 0x02) {
3378 assert(PyDict_CheckExact(TOP()));
3379 func->func_kwdefaults = POP();
3380 }
3381 if (oparg & 0x01) {
3382 assert(PyTuple_CheckExact(TOP()));
3383 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003384 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003385
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003386 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003387 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003388 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003389
Benjamin Petersonddd19492018-09-16 22:38:02 -07003390 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003391 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003392 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003393 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003394 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003395 step = NULL;
3396 stop = POP();
3397 start = TOP();
3398 slice = PySlice_New(start, stop, step);
3399 Py_DECREF(start);
3400 Py_DECREF(stop);
3401 Py_XDECREF(step);
3402 SET_TOP(slice);
3403 if (slice == NULL)
3404 goto error;
3405 DISPATCH();
3406 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003407
Benjamin Petersonddd19492018-09-16 22:38:02 -07003408 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003409 /* Handles f-string value formatting. */
3410 PyObject *result;
3411 PyObject *fmt_spec;
3412 PyObject *value;
3413 PyObject *(*conv_fn)(PyObject *);
3414 int which_conversion = oparg & FVC_MASK;
3415 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3416
3417 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003418 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003419
3420 /* See if any conversion is specified. */
3421 switch (which_conversion) {
3422 case FVC_STR: conv_fn = PyObject_Str; break;
3423 case FVC_REPR: conv_fn = PyObject_Repr; break;
3424 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3425
3426 /* Must be 0 (meaning no conversion), since only four
3427 values are allowed by (oparg & FVC_MASK). */
3428 default: conv_fn = NULL; break;
3429 }
3430
3431 /* If there's a conversion function, call it and replace
3432 value with that result. Otherwise, just use value,
3433 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003434 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003435 result = conv_fn(value);
3436 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003437 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003438 Py_XDECREF(fmt_spec);
3439 goto error;
3440 }
3441 value = result;
3442 }
3443
3444 /* If value is a unicode object, and there's no fmt_spec,
3445 then we know the result of format(value) is value
3446 itself. In that case, skip calling format(). I plan to
3447 move this optimization in to PyObject_Format()
3448 itself. */
3449 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3450 /* Do nothing, just transfer ownership to result. */
3451 result = value;
3452 } else {
3453 /* Actually call format(). */
3454 result = PyObject_Format(value, fmt_spec);
3455 Py_DECREF(value);
3456 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003457 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003458 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003459 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003460 }
3461
Eric V. Smith135d5f42016-02-05 18:23:08 -05003462 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003463 DISPATCH();
3464 }
3465
Benjamin Petersonddd19492018-09-16 22:38:02 -07003466 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003467 int oldoparg = oparg;
3468 NEXTOPARG();
3469 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003470 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003471 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003472
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003473
Antoine Pitrou042b1282010-08-13 21:15:58 +00003474#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003475 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003476#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003477 default:
3478 fprintf(stderr,
3479 "XXX lineno: %d, opcode: %d\n",
3480 PyFrame_GetLineNumber(f),
3481 opcode);
3482 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003483 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003485 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003486
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003487 /* This should never be reached. Every opcode should end with DISPATCH()
3488 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003489 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003490
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003491error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003492 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003493#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003494 if (!PyErr_Occurred())
3495 PyErr_SetString(PyExc_SystemError,
3496 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003497#else
3498 assert(PyErr_Occurred());
3499#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003500
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003501 /* Log traceback info. */
3502 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003503
Benjamin Peterson51f46162013-01-23 08:38:47 -05003504 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003505 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3506 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003507
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003508exception_unwind:
3509 /* Unwind stacks if an exception occurred */
3510 while (f->f_iblock > 0) {
3511 /* Pop the current block. */
3512 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003514 if (b->b_type == EXCEPT_HANDLER) {
3515 UNWIND_EXCEPT_HANDLER(b);
3516 continue;
3517 }
3518 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003519 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003520 PyObject *exc, *val, *tb;
3521 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003522 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003523 /* Beware, this invalidates all b->b_* fields */
3524 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003525 PUSH(exc_info->exc_traceback);
3526 PUSH(exc_info->exc_value);
3527 if (exc_info->exc_type != NULL) {
3528 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003529 }
3530 else {
3531 Py_INCREF(Py_None);
3532 PUSH(Py_None);
3533 }
3534 PyErr_Fetch(&exc, &val, &tb);
3535 /* Make the raw exception data
3536 available to the handler,
3537 so a program can emulate the
3538 Python main loop. */
3539 PyErr_NormalizeException(
3540 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003541 if (tb != NULL)
3542 PyException_SetTraceback(val, tb);
3543 else
3544 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003545 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003546 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003547 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003548 exc_info->exc_value = val;
3549 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003550 if (tb == NULL)
3551 tb = Py_None;
3552 Py_INCREF(tb);
3553 PUSH(tb);
3554 PUSH(val);
3555 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003556 JUMPTO(handler);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003557 /* Resume normal execution */
3558 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003559 }
3560 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003561
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003562 /* End the loop as we still have an error */
3563 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003564 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003566 /* Pop remaining stack entries. */
3567 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003568 PyObject *o = POP();
3569 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003570 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003571
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003572 assert(retval == NULL);
3573 assert(PyErr_Occurred());
Guido van Rossumac7be682001-01-17 15:42:30 +00003574
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003575return_or_yield:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003576 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003577 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003578 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3579 tstate, f, PyTrace_RETURN, retval)) {
3580 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003581 }
3582 }
3583 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003584 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3585 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003586 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003587 }
3588 }
3589 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003591 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003592exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003593 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3594 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003595 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003596 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003597 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003598
Victor Stinnerefde1462015-03-21 15:04:43 +01003599 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003600}
3601
Benjamin Petersonb204a422011-06-05 22:04:07 -05003602static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003603format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3604{
3605 int err;
3606 Py_ssize_t len = PyList_GET_SIZE(names);
3607 PyObject *name_str, *comma, *tail, *tmp;
3608
3609 assert(PyList_CheckExact(names));
3610 assert(len >= 1);
3611 /* Deal with the joys of natural language. */
3612 switch (len) {
3613 case 1:
3614 name_str = PyList_GET_ITEM(names, 0);
3615 Py_INCREF(name_str);
3616 break;
3617 case 2:
3618 name_str = PyUnicode_FromFormat("%U and %U",
3619 PyList_GET_ITEM(names, len - 2),
3620 PyList_GET_ITEM(names, len - 1));
3621 break;
3622 default:
3623 tail = PyUnicode_FromFormat(", %U, and %U",
3624 PyList_GET_ITEM(names, len - 2),
3625 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003626 if (tail == NULL)
3627 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003628 /* Chop off the last two objects in the list. This shouldn't actually
3629 fail, but we can't be too careful. */
3630 err = PyList_SetSlice(names, len - 2, len, NULL);
3631 if (err == -1) {
3632 Py_DECREF(tail);
3633 return;
3634 }
3635 /* Stitch everything up into a nice comma-separated list. */
3636 comma = PyUnicode_FromString(", ");
3637 if (comma == NULL) {
3638 Py_DECREF(tail);
3639 return;
3640 }
3641 tmp = PyUnicode_Join(comma, names);
3642 Py_DECREF(comma);
3643 if (tmp == NULL) {
3644 Py_DECREF(tail);
3645 return;
3646 }
3647 name_str = PyUnicode_Concat(tmp, tail);
3648 Py_DECREF(tmp);
3649 Py_DECREF(tail);
3650 break;
3651 }
3652 if (name_str == NULL)
3653 return;
3654 PyErr_Format(PyExc_TypeError,
3655 "%U() missing %i required %s argument%s: %U",
3656 co->co_name,
3657 len,
3658 kind,
3659 len == 1 ? "" : "s",
3660 name_str);
3661 Py_DECREF(name_str);
3662}
3663
3664static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003665missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003666 PyObject **fastlocals)
3667{
Victor Stinner74319ae2016-08-25 00:04:09 +02003668 Py_ssize_t i, j = 0;
3669 Py_ssize_t start, end;
3670 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003671 const char *kind = positional ? "positional" : "keyword-only";
3672 PyObject *missing_names;
3673
3674 /* Compute the names of the arguments that are missing. */
3675 missing_names = PyList_New(missing);
3676 if (missing_names == NULL)
3677 return;
3678 if (positional) {
3679 start = 0;
3680 end = co->co_argcount - defcount;
3681 }
3682 else {
3683 start = co->co_argcount;
3684 end = start + co->co_kwonlyargcount;
3685 }
3686 for (i = start; i < end; i++) {
3687 if (GETLOCAL(i) == NULL) {
3688 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3689 PyObject *name = PyObject_Repr(raw);
3690 if (name == NULL) {
3691 Py_DECREF(missing_names);
3692 return;
3693 }
3694 PyList_SET_ITEM(missing_names, j++, name);
3695 }
3696 }
3697 assert(j == missing);
3698 format_missing(kind, co, missing_names);
3699 Py_DECREF(missing_names);
3700}
3701
3702static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003703too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3704 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003705{
3706 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003707 Py_ssize_t kwonly_given = 0;
3708 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003709 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003710 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003711
Benjamin Petersone109c702011-06-24 09:37:26 -05003712 assert((co->co_flags & CO_VARARGS) == 0);
3713 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003714 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3715 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003716 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003717 }
3718 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003719 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003720 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003721 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003722 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003723 }
3724 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003725 plural = (co_argcount != 1);
3726 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003727 }
3728 if (sig == NULL)
3729 return;
3730 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003731 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3732 kwonly_sig = PyUnicode_FromFormat(format,
3733 given != 1 ? "s" : "",
3734 kwonly_given,
3735 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003736 if (kwonly_sig == NULL) {
3737 Py_DECREF(sig);
3738 return;
3739 }
3740 }
3741 else {
3742 /* This will not fail. */
3743 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003744 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003745 }
3746 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003747 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003748 co->co_name,
3749 sig,
3750 plural ? "s" : "",
3751 given,
3752 kwonly_sig,
3753 given == 1 && !kwonly_given ? "was" : "were");
3754 Py_DECREF(sig);
3755 Py_DECREF(kwonly_sig);
3756}
3757
Guido van Rossumc2e20742006-02-27 22:32:47 +00003758/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02003759 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003760 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003761
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003762PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +02003763_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003764 PyObject *const *args, Py_ssize_t argcount,
3765 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003766 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003767 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003768 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003769 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003770{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003771 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003772 PyFrameObject *f;
3773 PyObject *retval = NULL;
3774 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003775 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003776 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003777 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3778 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003779 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003781 if (globals == NULL) {
3782 PyErr_SetString(PyExc_SystemError,
3783 "PyEval_EvalCodeEx: NULL globals");
3784 return NULL;
3785 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003786
Victor Stinnerc7020012016-08-16 23:40:29 +02003787 /* Create the frame */
Victor Stinner50b48572018-11-01 01:51:40 +01003788 tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003789 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003790 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003791 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003792 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003793 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003794 fastlocals = f->f_localsplus;
3795 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003796
Victor Stinnerc7020012016-08-16 23:40:29 +02003797 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003798 if (co->co_flags & CO_VARKEYWORDS) {
3799 kwdict = PyDict_New();
3800 if (kwdict == NULL)
3801 goto fail;
3802 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003803 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003804 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003805 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003806 SETLOCAL(i, kwdict);
3807 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003808 else {
3809 kwdict = NULL;
3810 }
3811
3812 /* Copy positional arguments into local variables */
3813 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003814 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003815 }
3816 else {
3817 n = argcount;
3818 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003819 for (i = 0; i < n; i++) {
3820 x = args[i];
3821 Py_INCREF(x);
3822 SETLOCAL(i, x);
3823 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003824
3825 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003826 if (co->co_flags & CO_VARARGS) {
Sergey Fedoseev234531b2019-02-25 21:59:12 +05003827 u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003828 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003829 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003830 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003831 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003832 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003833
Serhiy Storchakab7281052016-09-12 00:52:40 +03003834 /* Handle keyword arguments passed as two strided arrays */
3835 kwcount *= kwstep;
3836 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003837 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003838 PyObject *keyword = kwnames[i];
3839 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003840 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003841
Benjamin Petersonb204a422011-06-05 22:04:07 -05003842 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3843 PyErr_Format(PyExc_TypeError,
3844 "%U() keywords must be strings",
3845 co->co_name);
3846 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003847 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003848
Benjamin Petersonb204a422011-06-05 22:04:07 -05003849 /* Speed hack: do raw pointer compares. As names are
3850 normally interned this should almost always hit. */
3851 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3852 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003853 PyObject *name = co_varnames[j];
3854 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003855 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003856 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003857 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003858
Benjamin Petersonb204a422011-06-05 22:04:07 -05003859 /* Slow fallback, just in case */
3860 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003861 PyObject *name = co_varnames[j];
3862 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
3863 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003864 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003865 }
3866 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003867 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003868 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003869 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003870
Victor Stinner231d1f32017-01-11 02:12:06 +01003871 assert(j >= total_args);
3872 if (kwdict == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003873 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003874 "%U() got an unexpected keyword argument '%S'",
3875 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003876 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003877 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003878
Christian Heimes0bd447f2013-07-20 14:48:10 +02003879 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3880 goto fail;
3881 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003882 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02003883
Benjamin Petersonb204a422011-06-05 22:04:07 -05003884 kw_found:
3885 if (GETLOCAL(j) != NULL) {
3886 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003887 "%U() got multiple values for argument '%S'",
3888 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003889 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003890 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003891 Py_INCREF(value);
3892 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003893 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003894
3895 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003896 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003897 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003898 goto fail;
3899 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003900
3901 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003902 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02003903 Py_ssize_t m = co->co_argcount - defcount;
3904 Py_ssize_t missing = 0;
3905 for (i = argcount; i < m; i++) {
3906 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003907 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02003908 }
3909 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003910 if (missing) {
3911 missing_arguments(co, missing, defcount, fastlocals);
3912 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003913 }
3914 if (n > m)
3915 i = n - m;
3916 else
3917 i = 0;
3918 for (; i < defcount; i++) {
3919 if (GETLOCAL(m+i) == NULL) {
3920 PyObject *def = defs[i];
3921 Py_INCREF(def);
3922 SETLOCAL(m+i, def);
3923 }
3924 }
3925 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003926
3927 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003928 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02003929 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003930 for (i = co->co_argcount; i < total_args; i++) {
3931 PyObject *name;
3932 if (GETLOCAL(i) != NULL)
3933 continue;
3934 name = PyTuple_GET_ITEM(co->co_varnames, i);
3935 if (kwdefs != NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003936 PyObject *def = PyDict_GetItemWithError(kwdefs, name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003937 if (def) {
3938 Py_INCREF(def);
3939 SETLOCAL(i, def);
3940 continue;
3941 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003942 else if (PyErr_Occurred()) {
3943 goto fail;
3944 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003945 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003946 missing++;
3947 }
3948 if (missing) {
3949 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003950 goto fail;
3951 }
3952 }
3953
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003954 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003955 vars into frame. */
3956 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003957 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02003958 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05003959 /* Possibly account for the cell variable being an argument. */
3960 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07003961 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05003962 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05003963 /* Clear the local copy. */
3964 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003965 }
3966 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05003967 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003968 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05003969 if (c == NULL)
3970 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05003971 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003972 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003973
3974 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05003975 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3976 PyObject *o = PyTuple_GET_ITEM(closure, i);
3977 Py_INCREF(o);
3978 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003979 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003980
Yury Selivanoveb636452016-09-08 22:01:51 -07003981 /* Handle generator/coroutine/asynchronous generator */
3982 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003983 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04003984 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003985 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04003986
3987 if (is_coro && tstate->in_coroutine_wrapper) {
3988 assert(coro_wrapper != NULL);
3989 PyErr_Format(PyExc_RuntimeError,
3990 "coroutine wrapper %.200R attempted "
3991 "to recursively wrap %.200R",
3992 coro_wrapper,
3993 co);
3994 goto fail;
3995 }
Yury Selivanov75445082015-05-11 22:57:16 -04003996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003997 /* Don't need to keep the reference to f_back, it will be set
3998 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003999 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004000
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004001 /* Create a new generator that owns the ready to run frame
4002 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004003 if (is_coro) {
4004 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004005 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4006 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004007 } else {
4008 gen = PyGen_NewWithQualName(f, name, qualname);
4009 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004010 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004011 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004012 }
INADA Naoki9c157762016-12-26 18:52:46 +09004013
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004014 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004015
Yury Selivanov94c22632015-06-04 10:16:51 -04004016 if (is_coro && coro_wrapper != NULL) {
4017 PyObject *wrapped;
4018 tstate->in_coroutine_wrapper = 1;
4019 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
4020 tstate->in_coroutine_wrapper = 0;
4021 return wrapped;
4022 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04004023
Yury Selivanov75445082015-05-11 22:57:16 -04004024 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004025 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004026
Victor Stinner59a73272016-12-09 18:51:13 +01004027 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004028
Thomas Woutersce272b62007-09-19 21:19:28 +00004029fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004030
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004031 /* decref'ing the frame can cause __del__ methods to get invoked,
4032 which can call back into Python. While we're done with the
4033 current Python frame (f), the associated C stack is still in use,
4034 so recursion_depth must be boosted for the duration.
4035 */
4036 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09004037 if (Py_REFCNT(f) > 1) {
4038 Py_DECREF(f);
4039 _PyObject_GC_TRACK(f);
4040 }
4041 else {
4042 ++tstate->recursion_depth;
4043 Py_DECREF(f);
4044 --tstate->recursion_depth;
4045 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004046 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004047}
4048
Victor Stinner40ee3012014-06-16 15:59:28 +02004049PyObject *
4050PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004051 PyObject *const *args, int argcount,
4052 PyObject *const *kws, int kwcount,
4053 PyObject *const *defs, int defcount,
4054 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004055{
4056 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004057 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004058 kws, kws != NULL ? kws + 1 : NULL,
4059 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004060 defs, defcount,
4061 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004062 NULL, NULL);
4063}
Tim Peters5ca576e2001-06-18 22:08:13 +00004064
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004065static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05004066special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004067{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004068 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004069 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004070 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05004071 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004072 return NULL;
4073 }
4074 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004075}
4076
4077
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004078/* Logic for the raise statement (too complicated for inlining).
4079 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004080static int
Collin Winter828f04a2007-08-31 00:04:24 +00004081do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004082{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004083 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004085 if (exc == NULL) {
4086 /* Reraise */
Victor Stinner50b48572018-11-01 01:51:40 +01004087 PyThreadState *tstate = _PyThreadState_GET();
Mark Shannonae3087c2017-10-22 22:41:51 +01004088 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004089 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004090 type = exc_info->exc_type;
4091 value = exc_info->exc_value;
4092 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004093 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004094 PyErr_SetString(PyExc_RuntimeError,
4095 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004096 return 0;
4097 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004098 Py_XINCREF(type);
4099 Py_XINCREF(value);
4100 Py_XINCREF(tb);
4101 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004102 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004103 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004104
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004105 /* We support the following forms of raise:
4106 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004107 raise <instance>
4108 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004109
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004110 if (PyExceptionClass_Check(exc)) {
4111 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004112 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004113 if (value == NULL)
4114 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004115 if (!PyExceptionInstance_Check(value)) {
4116 PyErr_Format(PyExc_TypeError,
4117 "calling %R should have returned an instance of "
4118 "BaseException, not %R",
4119 type, Py_TYPE(value));
4120 goto raise_error;
4121 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004122 }
4123 else if (PyExceptionInstance_Check(exc)) {
4124 value = exc;
4125 type = PyExceptionInstance_Class(exc);
4126 Py_INCREF(type);
4127 }
4128 else {
4129 /* Not something you can raise. You get an exception
4130 anyway, just not what you specified :-) */
4131 Py_DECREF(exc);
4132 PyErr_SetString(PyExc_TypeError,
4133 "exceptions must derive from BaseException");
4134 goto raise_error;
4135 }
Collin Winter828f04a2007-08-31 00:04:24 +00004136
Serhiy Storchakac0191582016-09-27 11:37:10 +03004137 assert(type != NULL);
4138 assert(value != NULL);
4139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004140 if (cause) {
4141 PyObject *fixed_cause;
4142 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004143 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004144 if (fixed_cause == NULL)
4145 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004146 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004147 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004148 else if (PyExceptionInstance_Check(cause)) {
4149 fixed_cause = cause;
4150 }
4151 else if (cause == Py_None) {
4152 Py_DECREF(cause);
4153 fixed_cause = NULL;
4154 }
4155 else {
4156 PyErr_SetString(PyExc_TypeError,
4157 "exception causes must derive from "
4158 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004159 goto raise_error;
4160 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004161 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004162 }
Collin Winter828f04a2007-08-31 00:04:24 +00004163
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004164 PyErr_SetObject(type, value);
4165 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004166 Py_DECREF(value);
4167 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004168 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004169
4170raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004171 Py_XDECREF(value);
4172 Py_XDECREF(type);
4173 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004174 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004175}
4176
Tim Petersd6d010b2001-06-21 02:49:55 +00004177/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004178 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004179
Guido van Rossum0368b722007-05-11 16:50:42 +00004180 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4181 with a variable target.
4182*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004183
Barry Warsawe42b18f1997-08-25 22:13:04 +00004184static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004185unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004186{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004187 int i = 0, j = 0;
4188 Py_ssize_t ll = 0;
4189 PyObject *it; /* iter(v) */
4190 PyObject *w;
4191 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004193 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004195 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004196 if (it == NULL) {
4197 if (PyErr_ExceptionMatches(PyExc_TypeError) &&
4198 v->ob_type->tp_iter == NULL && !PySequence_Check(v))
4199 {
4200 PyErr_Format(PyExc_TypeError,
4201 "cannot unpack non-iterable %.200s object",
4202 v->ob_type->tp_name);
4203 }
4204 return 0;
4205 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004207 for (; i < argcnt; i++) {
4208 w = PyIter_Next(it);
4209 if (w == NULL) {
4210 /* Iterator done, via error or exhaustion. */
4211 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004212 if (argcntafter == -1) {
4213 PyErr_Format(PyExc_ValueError,
4214 "not enough values to unpack (expected %d, got %d)",
4215 argcnt, i);
4216 }
4217 else {
4218 PyErr_Format(PyExc_ValueError,
4219 "not enough values to unpack "
4220 "(expected at least %d, got %d)",
4221 argcnt + argcntafter, i);
4222 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004223 }
4224 goto Error;
4225 }
4226 *--sp = w;
4227 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004229 if (argcntafter == -1) {
4230 /* We better have exhausted the iterator now. */
4231 w = PyIter_Next(it);
4232 if (w == NULL) {
4233 if (PyErr_Occurred())
4234 goto Error;
4235 Py_DECREF(it);
4236 return 1;
4237 }
4238 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004239 PyErr_Format(PyExc_ValueError,
4240 "too many values to unpack (expected %d)",
4241 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004242 goto Error;
4243 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004245 l = PySequence_List(it);
4246 if (l == NULL)
4247 goto Error;
4248 *--sp = l;
4249 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004251 ll = PyList_GET_SIZE(l);
4252 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004253 PyErr_Format(PyExc_ValueError,
4254 "not enough values to unpack (expected at least %d, got %zd)",
4255 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004256 goto Error;
4257 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004259 /* Pop the "after-variable" args off the list. */
4260 for (j = argcntafter; j > 0; j--, i++) {
4261 *--sp = PyList_GET_ITEM(l, ll - j);
4262 }
4263 /* Resize the list. */
4264 Py_SIZE(l) = ll - argcntafter;
4265 Py_DECREF(it);
4266 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004267
Tim Petersd6d010b2001-06-21 02:49:55 +00004268Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004269 for (; i > 0; i--, sp++)
4270 Py_DECREF(*sp);
4271 Py_XDECREF(it);
4272 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004273}
4274
4275
Guido van Rossum96a42c81992-01-12 02:29:51 +00004276#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004277static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004278prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004279{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004280 printf("%s ", str);
4281 if (PyObject_Print(v, stdout, 0) != 0)
4282 PyErr_Clear(); /* Don't know what else to do */
4283 printf("\n");
4284 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004285}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004286#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004287
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004288static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004289call_exc_trace(Py_tracefunc func, PyObject *self,
4290 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004291{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004292 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004293 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004294 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004295 if (value == NULL) {
4296 value = Py_None;
4297 Py_INCREF(value);
4298 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004299 PyErr_NormalizeException(&type, &value, &orig_traceback);
4300 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004301 arg = PyTuple_Pack(3, type, value, traceback);
4302 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004303 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004304 return;
4305 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004306 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004307 Py_DECREF(arg);
4308 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004309 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004310 else {
4311 Py_XDECREF(type);
4312 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004313 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004314 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004315}
4316
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004317static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004318call_trace_protected(Py_tracefunc func, PyObject *obj,
4319 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004320 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004321{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004322 PyObject *type, *value, *traceback;
4323 int err;
4324 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004325 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004326 if (err == 0)
4327 {
4328 PyErr_Restore(type, value, traceback);
4329 return 0;
4330 }
4331 else {
4332 Py_XDECREF(type);
4333 Py_XDECREF(value);
4334 Py_XDECREF(traceback);
4335 return -1;
4336 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004337}
4338
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004339static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004340call_trace(Py_tracefunc func, PyObject *obj,
4341 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004342 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004343{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004344 int result;
4345 if (tstate->tracing)
4346 return 0;
4347 tstate->tracing++;
4348 tstate->use_tracing = 0;
4349 result = func(obj, frame, what, arg);
4350 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4351 || (tstate->c_profilefunc != NULL));
4352 tstate->tracing--;
4353 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004354}
4355
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004356PyObject *
4357_PyEval_CallTracing(PyObject *func, PyObject *args)
4358{
Victor Stinner50b48572018-11-01 01:51:40 +01004359 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004360 int save_tracing = tstate->tracing;
4361 int save_use_tracing = tstate->use_tracing;
4362 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004364 tstate->tracing = 0;
4365 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4366 || (tstate->c_profilefunc != NULL));
4367 result = PyObject_Call(func, args, NULL);
4368 tstate->tracing = save_tracing;
4369 tstate->use_tracing = save_use_tracing;
4370 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004371}
4372
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004373/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004374static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004375maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004376 PyThreadState *tstate, PyFrameObject *frame,
4377 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004378{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004379 int result = 0;
4380 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004382 /* If the last instruction executed isn't in the current
4383 instruction window, reset the window.
4384 */
4385 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4386 PyAddrPair bounds;
4387 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4388 &bounds);
4389 *instr_lb = bounds.ap_lower;
4390 *instr_ub = bounds.ap_upper;
4391 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004392 /* If the last instruction falls at the start of a line or if it
4393 represents a jump backwards, update the frame's line number and
4394 then call the trace function if we're tracing source lines.
4395 */
4396 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004397 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004398 if (frame->f_trace_lines) {
4399 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4400 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004401 }
George King20faa682017-10-18 17:44:22 -07004402 /* Always emit an opcode event if we're tracing all opcodes. */
4403 if (frame->f_trace_opcodes) {
4404 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4405 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004406 *instr_prev = frame->f_lasti;
4407 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004408}
4409
Fred Drake5755ce62001-06-27 19:19:46 +00004410void
4411PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004412{
Victor Stinner50b48572018-11-01 01:51:40 +01004413 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004414 PyObject *temp = tstate->c_profileobj;
4415 Py_XINCREF(arg);
4416 tstate->c_profilefunc = NULL;
4417 tstate->c_profileobj = NULL;
4418 /* Must make sure that tracing is not ignored if 'temp' is freed */
4419 tstate->use_tracing = tstate->c_tracefunc != NULL;
4420 Py_XDECREF(temp);
4421 tstate->c_profilefunc = func;
4422 tstate->c_profileobj = arg;
4423 /* Flag that tracing or profiling is turned on */
4424 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004425}
4426
4427void
4428PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4429{
Victor Stinner50b48572018-11-01 01:51:40 +01004430 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004431 PyObject *temp = tstate->c_traceobj;
4432 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4433 Py_XINCREF(arg);
4434 tstate->c_tracefunc = NULL;
4435 tstate->c_traceobj = NULL;
4436 /* Must make sure that profiling is not ignored if 'temp' is freed */
4437 tstate->use_tracing = tstate->c_profilefunc != NULL;
4438 Py_XDECREF(temp);
4439 tstate->c_tracefunc = func;
4440 tstate->c_traceobj = arg;
4441 /* Flag that tracing or profiling is turned on */
4442 tstate->use_tracing = ((func != NULL)
4443 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004444}
4445
Yury Selivanov75445082015-05-11 22:57:16 -04004446void
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004447_PyEval_SetCoroutineOriginTrackingDepth(int new_depth)
4448{
4449 assert(new_depth >= 0);
Victor Stinner50b48572018-11-01 01:51:40 +01004450 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004451 tstate->coroutine_origin_tracking_depth = new_depth;
4452}
4453
4454int
4455_PyEval_GetCoroutineOriginTrackingDepth(void)
4456{
Victor Stinner50b48572018-11-01 01:51:40 +01004457 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004458 return tstate->coroutine_origin_tracking_depth;
4459}
4460
4461void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004462_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004463{
Victor Stinner50b48572018-11-01 01:51:40 +01004464 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanov75445082015-05-11 22:57:16 -04004465
Yury Selivanov75445082015-05-11 22:57:16 -04004466 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004467 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004468}
4469
4470PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004471_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004472{
Victor Stinner50b48572018-11-01 01:51:40 +01004473 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanov75445082015-05-11 22:57:16 -04004474 return tstate->coroutine_wrapper;
4475}
4476
Yury Selivanoveb636452016-09-08 22:01:51 -07004477void
4478_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4479{
Victor Stinner50b48572018-11-01 01:51:40 +01004480 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004481
4482 Py_XINCREF(firstiter);
4483 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4484}
4485
4486PyObject *
4487_PyEval_GetAsyncGenFirstiter(void)
4488{
Victor Stinner50b48572018-11-01 01:51:40 +01004489 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004490 return tstate->async_gen_firstiter;
4491}
4492
4493void
4494_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4495{
Victor Stinner50b48572018-11-01 01:51:40 +01004496 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004497
4498 Py_XINCREF(finalizer);
4499 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4500}
4501
4502PyObject *
4503_PyEval_GetAsyncGenFinalizer(void)
4504{
Victor Stinner50b48572018-11-01 01:51:40 +01004505 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004506 return tstate->async_gen_finalizer;
4507}
4508
Guido van Rossumb209a111997-04-29 18:18:01 +00004509PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004510PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004511{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004512 PyFrameObject *current_frame = PyEval_GetFrame();
4513 if (current_frame == NULL)
Victor Stinnercaba55b2018-08-03 15:33:52 +02004514 return _PyInterpreterState_GET_UNSAFE()->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004515 else
4516 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004517}
4518
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004519/* Convenience function to get a builtin from its name */
4520PyObject *
4521_PyEval_GetBuiltinId(_Py_Identifier *name)
4522{
4523 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4524 if (attr) {
4525 Py_INCREF(attr);
4526 }
4527 else if (!PyErr_Occurred()) {
4528 PyErr_SetObject(PyExc_AttributeError, _PyUnicode_FromId(name));
4529 }
4530 return attr;
4531}
4532
Guido van Rossumb209a111997-04-29 18:18:01 +00004533PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004534PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004535{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004536 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004537 if (current_frame == NULL) {
4538 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004539 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004540 }
4541
4542 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4543 return NULL;
4544
4545 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004546 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004547}
4548
Guido van Rossumb209a111997-04-29 18:18:01 +00004549PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004550PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004551{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004552 PyFrameObject *current_frame = PyEval_GetFrame();
4553 if (current_frame == NULL)
4554 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004555
4556 assert(current_frame->f_globals != NULL);
4557 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004558}
4559
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004560PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004561PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004562{
Victor Stinner50b48572018-11-01 01:51:40 +01004563 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004564 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004565}
4566
Guido van Rossum6135a871995-01-09 17:53:26 +00004567int
Tim Peters5ba58662001-07-16 02:29:45 +00004568PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004569{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004570 PyFrameObject *current_frame = PyEval_GetFrame();
4571 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004573 if (current_frame != NULL) {
4574 const int codeflags = current_frame->f_code->co_flags;
4575 const int compilerflags = codeflags & PyCF_MASK;
4576 if (compilerflags) {
4577 result = 1;
4578 cf->cf_flags |= compilerflags;
4579 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004580#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004581 if (codeflags & CO_GENERATOR_ALLOWED) {
4582 result = 1;
4583 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4584 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004585#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004586 }
4587 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004588}
4589
Guido van Rossum3f5da241990-12-20 15:06:42 +00004590
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004591const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004592PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004593{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004594 if (PyMethod_Check(func))
4595 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4596 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004597 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004598 else if (PyCFunction_Check(func))
4599 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4600 else
4601 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004602}
4603
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004604const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004605PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004606{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004607 if (PyMethod_Check(func))
4608 return "()";
4609 else if (PyFunction_Check(func))
4610 return "()";
4611 else if (PyCFunction_Check(func))
4612 return "()";
4613 else
4614 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004615}
4616
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004617#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004618if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004619 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4620 tstate, tstate->frame, \
4621 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004622 x = NULL; \
4623 } \
4624 else { \
4625 x = call; \
4626 if (tstate->c_profilefunc != NULL) { \
4627 if (x == NULL) { \
4628 call_trace_protected(tstate->c_profilefunc, \
4629 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004630 tstate, tstate->frame, \
4631 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004632 /* XXX should pass (type, value, tb) */ \
4633 } else { \
4634 if (call_trace(tstate->c_profilefunc, \
4635 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004636 tstate, tstate->frame, \
4637 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004638 Py_DECREF(x); \
4639 x = NULL; \
4640 } \
4641 } \
4642 } \
4643 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004644} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004645 x = call; \
4646 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004647
Victor Stinner415c5102017-01-11 00:54:57 +01004648/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4649 to reduce the stack consumption. */
4650Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Benjamin Peterson4fd64b92016-09-09 14:57:58 -07004651call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004652{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004653 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004654 PyObject *func = *pfunc;
4655 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004656 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4657 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004658 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004660 /* Always dispatch PyCFunction first, because these are
4661 presumed to be the most frequent callable object.
4662 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004663 if (PyCFunction_Check(func)) {
Victor Stinner50b48572018-11-01 01:51:40 +01004664 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004665 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004666 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004667 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
Victor Stinner50b48572018-11-01 01:51:40 +01004668 PyThreadState *tstate = _PyThreadState_GET();
jdemeyer56868f92018-07-21 10:30:59 +02004669 if (nargs > 0 && tstate->use_tracing) {
4670 /* We need to create a temporary bound method as argument
4671 for profiling.
4672
4673 If nargs == 0, then this cannot work because we have no
4674 "self". In any case, the call itself would raise
4675 TypeError (foo needs an argument), so we just skip
4676 profiling. */
4677 PyObject *self = stack[0];
4678 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
jdemeyer147d9552018-07-23 18:41:20 +02004679 if (func != NULL) {
4680 C_TRACE(x, _PyCFunction_FastCallKeywords(func,
4681 stack+1, nargs-1,
4682 kwnames));
4683 Py_DECREF(func);
INADA Naoki93fac8d2017-03-07 14:24:37 +09004684 }
jdemeyer147d9552018-07-23 18:41:20 +02004685 else {
4686 x = NULL;
4687 }
INADA Naoki93fac8d2017-03-07 14:24:37 +09004688 }
4689 else {
4690 x = _PyMethodDescr_FastCallKeywords(func, stack, nargs, kwnames);
4691 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004692 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004693 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004694 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
Victor Stinnerb69ee8c2016-11-28 18:32:31 +01004695 /* Optimize access to bound methods. Reuse the Python stack
4696 to pass 'self' as the first argument, replace 'func'
4697 with 'self'. It avoids the creation of a new temporary tuple
4698 for arguments (to replace func with self) when the method uses
4699 FASTCALL. */
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004700 PyObject *self = PyMethod_GET_SELF(func);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004701 Py_INCREF(self);
4702 func = PyMethod_GET_FUNCTION(func);
4703 Py_INCREF(func);
4704 Py_SETREF(*pfunc, self);
4705 nargs++;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004706 stack--;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004707 }
4708 else {
4709 Py_INCREF(func);
4710 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004711
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004712 if (PyFunction_Check(func)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004713 x = _PyFunction_FastCallKeywords(func, stack, nargs, kwnames);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004714 }
4715 else {
4716 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4717 }
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004718 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004719 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004720
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004721 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4722
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004723 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004724 while ((*pp_stack) > pfunc) {
4725 w = EXT_POP(*pp_stack);
4726 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004727 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004729 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004730}
4731
Jeremy Hylton52820442001-01-03 23:52:36 +00004732static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004733do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004734{
jdemeyere89de732018-09-19 12:06:20 +02004735 PyObject *result;
4736
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004737 if (PyCFunction_Check(func)) {
Victor Stinner50b48572018-11-01 01:51:40 +01004738 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004739 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004740 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004741 }
jdemeyere89de732018-09-19 12:06:20 +02004742 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
Victor Stinner50b48572018-11-01 01:51:40 +01004743 PyThreadState *tstate = _PyThreadState_GET();
jdemeyere89de732018-09-19 12:06:20 +02004744 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
4745 if (nargs > 0 && tstate->use_tracing) {
4746 /* We need to create a temporary bound method as argument
4747 for profiling.
4748
4749 If nargs == 0, then this cannot work because we have no
4750 "self". In any case, the call itself would raise
4751 TypeError (foo needs an argument), so we just skip
4752 profiling. */
4753 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
4754 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4755 if (func == NULL) {
4756 return NULL;
4757 }
4758
4759 C_TRACE(result, _PyCFunction_FastCallDict(func,
Victor Stinnerd17a6932018-11-09 16:56:48 +01004760 &_PyTuple_ITEMS(callargs)[1],
jdemeyere89de732018-09-19 12:06:20 +02004761 nargs - 1,
4762 kwdict));
4763 Py_DECREF(func);
4764 return result;
4765 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004766 }
jdemeyere89de732018-09-19 12:06:20 +02004767 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00004768}
4769
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004770/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004771 nb_index slot defined, and store in *pi.
4772 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004773 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004774 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004775*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004776int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004777_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004778{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004779 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004780 Py_ssize_t x;
4781 if (PyIndex_Check(v)) {
4782 x = PyNumber_AsSsize_t(v, NULL);
4783 if (x == -1 && PyErr_Occurred())
4784 return 0;
4785 }
4786 else {
4787 PyErr_SetString(PyExc_TypeError,
4788 "slice indices must be integers or "
4789 "None or have an __index__ method");
4790 return 0;
4791 }
4792 *pi = x;
4793 }
4794 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004795}
4796
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004797int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004798_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004799{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004800 Py_ssize_t x;
4801 if (PyIndex_Check(v)) {
4802 x = PyNumber_AsSsize_t(v, NULL);
4803 if (x == -1 && PyErr_Occurred())
4804 return 0;
4805 }
4806 else {
4807 PyErr_SetString(PyExc_TypeError,
4808 "slice indices must be integers or "
4809 "have an __index__ method");
4810 return 0;
4811 }
4812 *pi = x;
4813 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004814}
4815
4816
Guido van Rossum486364b2007-06-30 05:01:58 +00004817#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004818 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004819
Guido van Rossumb209a111997-04-29 18:18:01 +00004820static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004821cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004822{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004823 int res = 0;
4824 switch (op) {
4825 case PyCmp_IS:
4826 res = (v == w);
4827 break;
4828 case PyCmp_IS_NOT:
4829 res = (v != w);
4830 break;
4831 case PyCmp_IN:
4832 res = PySequence_Contains(w, v);
4833 if (res < 0)
4834 return NULL;
4835 break;
4836 case PyCmp_NOT_IN:
4837 res = PySequence_Contains(w, v);
4838 if (res < 0)
4839 return NULL;
4840 res = !res;
4841 break;
4842 case PyCmp_EXC_MATCH:
4843 if (PyTuple_Check(w)) {
4844 Py_ssize_t i, length;
4845 length = PyTuple_Size(w);
4846 for (i = 0; i < length; i += 1) {
4847 PyObject *exc = PyTuple_GET_ITEM(w, i);
4848 if (!PyExceptionClass_Check(exc)) {
4849 PyErr_SetString(PyExc_TypeError,
4850 CANNOT_CATCH_MSG);
4851 return NULL;
4852 }
4853 }
4854 }
4855 else {
4856 if (!PyExceptionClass_Check(w)) {
4857 PyErr_SetString(PyExc_TypeError,
4858 CANNOT_CATCH_MSG);
4859 return NULL;
4860 }
4861 }
4862 res = PyErr_GivenExceptionMatches(v, w);
4863 break;
4864 default:
4865 return PyObject_RichCompare(v, w, op);
4866 }
4867 v = res ? Py_True : Py_False;
4868 Py_INCREF(v);
4869 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004870}
4871
Thomas Wouters52152252000-08-17 22:55:00 +00004872static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004873import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
4874{
4875 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004876 PyObject *import_func, *res;
4877 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004878
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004879 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004880 if (import_func == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004881 if (!PyErr_Occurred()) {
4882 PyErr_SetString(PyExc_ImportError, "__import__ not found");
4883 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004884 return NULL;
4885 }
4886
4887 /* Fast path for not overloaded __import__. */
Victor Stinnercaba55b2018-08-03 15:33:52 +02004888 if (import_func == _PyInterpreterState_GET_UNSAFE()->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004889 int ilevel = _PyLong_AsInt(level);
4890 if (ilevel == -1 && PyErr_Occurred()) {
4891 return NULL;
4892 }
4893 res = PyImport_ImportModuleLevelObject(
4894 name,
4895 f->f_globals,
4896 f->f_locals == NULL ? Py_None : f->f_locals,
4897 fromlist,
4898 ilevel);
4899 return res;
4900 }
4901
4902 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004903
4904 stack[0] = name;
4905 stack[1] = f->f_globals;
4906 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
4907 stack[3] = fromlist;
4908 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02004909 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004910 Py_DECREF(import_func);
4911 return res;
4912}
4913
4914static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004915import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004916{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004917 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02004918 _Py_IDENTIFIER(__name__);
Xiang Zhang4830f582017-03-21 11:13:42 +08004919 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004920
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004921 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02004922 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004923 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004924 /* Issue #17636: in case this failed because of a circular relative
4925 import, try to fallback on reading the module directly from
4926 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02004927 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07004928 if (pkgname == NULL) {
4929 goto error;
4930 }
Oren Milman6db70332017-09-19 14:23:01 +03004931 if (!PyUnicode_Check(pkgname)) {
4932 Py_CLEAR(pkgname);
4933 goto error;
4934 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004935 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07004936 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08004937 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004938 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07004939 }
Eric Snow3f9eee62017-09-15 16:35:20 -06004940 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004941 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07004942 if (x == NULL) {
4943 goto error;
4944 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004945 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004946 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07004947 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004948 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004949 if (pkgname == NULL) {
4950 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
4951 if (pkgname_or_unknown == NULL) {
4952 Py_XDECREF(pkgpath);
4953 return NULL;
4954 }
4955 } else {
4956 pkgname_or_unknown = pkgname;
4957 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004958
4959 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
4960 PyErr_Clear();
Xiang Zhang4830f582017-03-21 11:13:42 +08004961 errmsg = PyUnicode_FromFormat(
4962 "cannot import name %R from %R (unknown location)",
4963 name, pkgname_or_unknown
4964 );
4965 /* NULL check for errmsg done by PyErr_SetImportError. */
4966 PyErr_SetImportError(errmsg, pkgname, NULL);
4967 }
4968 else {
4969 errmsg = PyUnicode_FromFormat(
4970 "cannot import name %R from %R (%S)",
4971 name, pkgname_or_unknown, pkgpath
4972 );
4973 /* NULL check for errmsg done by PyErr_SetImportError. */
4974 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004975 }
4976
Xiang Zhang4830f582017-03-21 11:13:42 +08004977 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004978 Py_XDECREF(pkgname_or_unknown);
4979 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07004980 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00004981}
Guido van Rossumac7be682001-01-17 15:42:30 +00004982
Thomas Wouters52152252000-08-17 22:55:00 +00004983static int
4984import_all_from(PyObject *locals, PyObject *v)
4985{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004986 _Py_IDENTIFIER(__all__);
4987 _Py_IDENTIFIER(__dict__);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08004988 _Py_IDENTIFIER(__name__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004989 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004990 int skip_leading_underscores = 0;
4991 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004992
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004993 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
4994 return -1; /* Unexpected error */
4995 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004996 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004997 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
4998 return -1;
4999 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005000 if (dict == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005001 PyErr_SetString(PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005002 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005003 return -1;
5004 }
5005 all = PyMapping_Keys(dict);
5006 Py_DECREF(dict);
5007 if (all == NULL)
5008 return -1;
5009 skip_leading_underscores = 1;
5010 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005011
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005012 for (pos = 0, err = 0; ; pos++) {
5013 name = PySequence_GetItem(all, pos);
5014 if (name == NULL) {
5015 if (!PyErr_ExceptionMatches(PyExc_IndexError))
5016 err = -1;
5017 else
5018 PyErr_Clear();
5019 break;
5020 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005021 if (!PyUnicode_Check(name)) {
5022 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5023 if (modname == NULL) {
5024 Py_DECREF(name);
5025 err = -1;
5026 break;
5027 }
5028 if (!PyUnicode_Check(modname)) {
5029 PyErr_Format(PyExc_TypeError,
5030 "module __name__ must be a string, not %.100s",
5031 Py_TYPE(modname)->tp_name);
5032 }
5033 else {
5034 PyErr_Format(PyExc_TypeError,
5035 "%s in %U.%s must be str, not %.100s",
5036 skip_leading_underscores ? "Key" : "Item",
5037 modname,
5038 skip_leading_underscores ? "__dict__" : "__all__",
5039 Py_TYPE(name)->tp_name);
5040 }
5041 Py_DECREF(modname);
5042 Py_DECREF(name);
5043 err = -1;
5044 break;
5045 }
5046 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005047 if (PyUnicode_READY(name) == -1) {
5048 Py_DECREF(name);
5049 err = -1;
5050 break;
5051 }
5052 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5053 Py_DECREF(name);
5054 continue;
5055 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005056 }
5057 value = PyObject_GetAttr(v, name);
5058 if (value == NULL)
5059 err = -1;
5060 else if (PyDict_CheckExact(locals))
5061 err = PyDict_SetItem(locals, name, value);
5062 else
5063 err = PyObject_SetItem(locals, name, value);
5064 Py_DECREF(name);
5065 Py_XDECREF(value);
5066 if (err != 0)
5067 break;
5068 }
5069 Py_DECREF(all);
5070 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005071}
5072
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005073static int
5074check_args_iterable(PyObject *func, PyObject *args)
5075{
5076 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
5077 PyErr_Format(PyExc_TypeError,
5078 "%.200s%.200s argument after * "
5079 "must be an iterable, not %.200s",
5080 PyEval_GetFuncName(func),
5081 PyEval_GetFuncDesc(func),
5082 args->ob_type->tp_name);
5083 return -1;
5084 }
5085 return 0;
5086}
5087
5088static void
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005089format_kwargs_error(PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005090{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005091 /* _PyDict_MergeEx raises attribute
5092 * error (percolated from an attempt
5093 * to get 'keys' attribute) instead of
5094 * a type error if its second argument
5095 * is not a mapping.
5096 */
5097 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
5098 PyErr_Format(PyExc_TypeError,
5099 "%.200s%.200s argument after ** "
5100 "must be a mapping, not %.200s",
5101 PyEval_GetFuncName(func),
5102 PyEval_GetFuncDesc(func),
5103 kwargs->ob_type->tp_name);
5104 }
5105 else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
5106 PyObject *exc, *val, *tb;
5107 PyErr_Fetch(&exc, &val, &tb);
5108 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
5109 PyObject *key = PyTuple_GET_ITEM(val, 0);
5110 if (!PyUnicode_Check(key)) {
5111 PyErr_Format(PyExc_TypeError,
5112 "%.200s%.200s keywords must be strings",
5113 PyEval_GetFuncName(func),
5114 PyEval_GetFuncDesc(func));
5115 } else {
5116 PyErr_Format(PyExc_TypeError,
5117 "%.200s%.200s got multiple "
5118 "values for keyword argument '%U'",
5119 PyEval_GetFuncName(func),
5120 PyEval_GetFuncDesc(func),
5121 key);
5122 }
5123 Py_XDECREF(exc);
5124 Py_XDECREF(val);
5125 Py_XDECREF(tb);
5126 }
5127 else {
5128 PyErr_Restore(exc, val, tb);
5129 }
5130 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005131}
5132
Guido van Rossumac7be682001-01-17 15:42:30 +00005133static void
Neal Norwitzda059e32007-08-26 05:33:45 +00005134format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005135{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005136 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005137
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005138 if (!obj)
5139 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005140
Serhiy Storchaka06515832016-11-20 09:13:07 +02005141 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005142 if (!obj_str)
5143 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005144
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005145 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005146}
Guido van Rossum950361c1997-01-24 13:49:28 +00005147
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005148static void
5149format_exc_unbound(PyCodeObject *co, int oparg)
5150{
5151 PyObject *name;
5152 /* Don't stomp existing exception */
5153 if (PyErr_Occurred())
5154 return;
5155 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5156 name = PyTuple_GET_ITEM(co->co_cellvars,
5157 oparg);
5158 format_exc_check_arg(
5159 PyExc_UnboundLocalError,
5160 UNBOUNDLOCAL_ERROR_MSG,
5161 name);
5162 } else {
5163 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5164 PyTuple_GET_SIZE(co->co_cellvars));
5165 format_exc_check_arg(PyExc_NameError,
5166 UNBOUNDFREE_ERROR_MSG, name);
5167 }
5168}
5169
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005170static void
5171format_awaitable_error(PyTypeObject *type, int prevopcode)
5172{
5173 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5174 if (prevopcode == BEFORE_ASYNC_WITH) {
5175 PyErr_Format(PyExc_TypeError,
5176 "'async with' received an object from __aenter__ "
5177 "that does not implement __await__: %.100s",
5178 type->tp_name);
5179 }
5180 else if (prevopcode == WITH_CLEANUP_START) {
5181 PyErr_Format(PyExc_TypeError,
5182 "'async with' received an object from __aexit__ "
5183 "that does not implement __await__: %.100s",
5184 type->tp_name);
5185 }
5186 }
5187}
5188
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005189static PyObject *
5190unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005191 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005192{
5193 PyObject *res;
5194 if (Py_REFCNT(v) == 2) {
5195 /* In the common case, there are 2 references to the value
5196 * stored in 'variable' when the += is performed: one on the
5197 * value stack (in 'v') and one still stored in the
5198 * 'variable'. We try to delete the variable now to reduce
5199 * the refcnt to 1.
5200 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005201 int opcode, oparg;
5202 NEXTOPARG();
5203 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005204 case STORE_FAST:
5205 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005206 PyObject **fastlocals = f->f_localsplus;
5207 if (GETLOCAL(oparg) == v)
5208 SETLOCAL(oparg, NULL);
5209 break;
5210 }
5211 case STORE_DEREF:
5212 {
5213 PyObject **freevars = (f->f_localsplus +
5214 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005215 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005216 if (PyCell_GET(c) == v) {
5217 PyCell_SET(c, NULL);
5218 Py_DECREF(v);
5219 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005220 break;
5221 }
5222 case STORE_NAME:
5223 {
5224 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005225 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005226 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005227 if (locals && PyDict_CheckExact(locals)) {
5228 PyObject *w = PyDict_GetItemWithError(locals, name);
5229 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
5230 (w == NULL && PyErr_Occurred()))
5231 {
5232 Py_DECREF(v);
5233 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005234 }
5235 }
5236 break;
5237 }
5238 }
5239 }
5240 res = v;
5241 PyUnicode_Append(&res, w);
5242 return res;
5243}
5244
Guido van Rossum950361c1997-01-24 13:49:28 +00005245#ifdef DYNAMIC_EXECUTION_PROFILE
5246
Skip Montanarof118cb12001-10-15 20:51:38 +00005247static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005248getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005249{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005250 int i;
5251 PyObject *l = PyList_New(256);
5252 if (l == NULL) return NULL;
5253 for (i = 0; i < 256; i++) {
5254 PyObject *x = PyLong_FromLong(a[i]);
5255 if (x == NULL) {
5256 Py_DECREF(l);
5257 return NULL;
5258 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005259 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005260 }
5261 for (i = 0; i < 256; i++)
5262 a[i] = 0;
5263 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005264}
5265
5266PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005267_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005268{
5269#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005270 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005271#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005272 int i;
5273 PyObject *l = PyList_New(257);
5274 if (l == NULL) return NULL;
5275 for (i = 0; i < 257; i++) {
5276 PyObject *x = getarray(dxpairs[i]);
5277 if (x == NULL) {
5278 Py_DECREF(l);
5279 return NULL;
5280 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005281 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005282 }
5283 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005284#endif
5285}
5286
5287#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005288
5289Py_ssize_t
5290_PyEval_RequestCodeExtraIndex(freefunc free)
5291{
Victor Stinnercaba55b2018-08-03 15:33:52 +02005292 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -07005293 Py_ssize_t new_index;
5294
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005295 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005296 return -1;
5297 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005298 new_index = interp->co_extra_user_count++;
5299 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005300 return new_index;
5301}
Łukasz Langaa785c872016-09-09 17:37:37 -07005302
5303static void
5304dtrace_function_entry(PyFrameObject *f)
5305{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005306 const char *filename;
5307 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005308 int lineno;
5309
5310 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5311 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5312 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5313
5314 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5315}
5316
5317static void
5318dtrace_function_return(PyFrameObject *f)
5319{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005320 const char *filename;
5321 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005322 int lineno;
5323
5324 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5325 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5326 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5327
5328 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5329}
5330
5331/* DTrace equivalent of maybe_call_line_trace. */
5332static void
5333maybe_dtrace_line(PyFrameObject *frame,
5334 int *instr_lb, int *instr_ub, int *instr_prev)
5335{
5336 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005337 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005338
5339 /* If the last instruction executed isn't in the current
5340 instruction window, reset the window.
5341 */
5342 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5343 PyAddrPair bounds;
5344 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5345 &bounds);
5346 *instr_lb = bounds.ap_lower;
5347 *instr_ub = bounds.ap_upper;
5348 }
5349 /* If the last instruction falls at the start of a line or if
5350 it represents a jump backwards, update the frame's line
5351 number and call the trace function. */
5352 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5353 frame->f_lineno = line;
5354 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5355 if (!co_filename)
5356 co_filename = "?";
5357 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5358 if (!co_name)
5359 co_name = "?";
5360 PyDTrace_LINE(co_filename, co_name, line);
5361 }
5362 *instr_prev = frame->f_lasti;
5363}