blob: 373cde9a17bba328b02009d22f645467c08e6871 [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) {
180 return 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
333_push_pending_call(int (*func)(void *), void *arg)
334{
335 int i = _PyRuntime.ceval.pending.last;
336 int j = (i + 1) % NPENDINGCALLS;
337 if (j == _PyRuntime.ceval.pending.first) {
338 return -1; /* Queue full */
339 }
340 _PyRuntime.ceval.pending.calls[i].func = func;
341 _PyRuntime.ceval.pending.calls[i].arg = arg;
342 _PyRuntime.ceval.pending.last = j;
343 return 0;
344}
345
346/* Pop one item off the queue while holding the lock. */
347static void
348_pop_pending_call(int (**func)(void *), void **arg)
349{
350 int i = _PyRuntime.ceval.pending.first;
351 if (i == _PyRuntime.ceval.pending.last) {
352 return; /* Queue empty */
353 }
354
355 *func = _PyRuntime.ceval.pending.calls[i].func;
356 *arg = _PyRuntime.ceval.pending.calls[i].arg;
357 _PyRuntime.ceval.pending.first = (i + 1) % NPENDINGCALLS;
358}
359
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200360/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000361 scheduling to be made from any thread, and even from an executing
362 callback.
363 */
364
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000365int
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100366Py_AddPendingCall(int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000367{
Eric Snow8479a342019-03-08 23:44:33 -0700368 PyThread_acquire_lock(_PyRuntime.ceval.pending.lock, WAIT_LOCK);
Eric Snow5be45a62019-03-08 22:47:07 -0700369 int result = _push_pending_call(func, arg);
Eric Snow8479a342019-03-08 23:44:33 -0700370 PyThread_release_lock(_PyRuntime.ceval.pending.lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 /* signal main loop */
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100373 SIGNAL_PENDING_CALLS();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000375}
376
Eric Snowfdf282d2019-01-11 14:26:55 -0700377static int
378handle_signals(void)
379{
Eric Snow5be45a62019-03-08 22:47:07 -0700380 /* Only handle signals on main thread. PyEval_InitThreads must
381 * have been called already.
382 */
383 if (PyThread_get_thread_ident() != _PyRuntime.main_thread) {
Eric Snowfdf282d2019-01-11 14:26:55 -0700384 return 0;
385 }
Eric Snow64d6cc82019-02-23 15:40:43 -0700386 /*
387 * Ensure that the thread isn't currently running some other
388 * interpreter.
389 */
390 if (_PyInterpreterState_GET_UNSAFE() != _PyRuntime.interpreters.main) {
391 return 0;
392 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700393
394 UNSIGNAL_PENDING_SIGNALS();
Eric Snow64d6cc82019-02-23 15:40:43 -0700395 if (_PyErr_CheckSignals() < 0) {
Eric Snowfdf282d2019-01-11 14:26:55 -0700396 SIGNAL_PENDING_SIGNALS(); /* We're not done yet */
397 return -1;
398 }
399 return 0;
400}
401
402static int
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100403make_pending_calls(void)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000404{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200405 static int busy = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000406
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100407 /* only service pending calls on main thread */
Eric Snow5be45a62019-03-08 22:47:07 -0700408 if (PyThread_get_thread_ident() != _PyRuntime.main_thread) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100409 return 0;
410 }
411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 /* don't perform recursive pending calls */
Eric Snowfdf282d2019-01-11 14:26:55 -0700413 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700415 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200416 busy = 1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200417 /* unsignal before starting to call callbacks, so that any callback
418 added in-between re-signals */
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100419 UNSIGNAL_PENDING_CALLS();
Eric Snowfdf282d2019-01-11 14:26:55 -0700420 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 /* perform a bounded number of calls, in case of recursion */
Eric Snowfdf282d2019-01-11 14:26:55 -0700423 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700424 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 void *arg = NULL;
426
427 /* pop one item off the queue while holding the lock */
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100428 PyThread_acquire_lock(_PyRuntime.ceval.pending.lock, WAIT_LOCK);
Eric Snow5be45a62019-03-08 22:47:07 -0700429 _pop_pending_call(&func, &arg);
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100430 PyThread_release_lock(_PyRuntime.ceval.pending.lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700431
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100432 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700433 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100434 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700435 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700436 res = func(arg);
437 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200438 goto error;
439 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200441
Charles-François Natalif23339a2011-07-23 18:15:43 +0200442 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700443 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200444
445error:
446 busy = 0;
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100447 SIGNAL_PENDING_CALLS();
Eric Snowfdf282d2019-01-11 14:26:55 -0700448 return res;
449}
450
451/* Py_MakePendingCalls() is a simple wrapper for the sake
452 of backward-compatibility. */
453int
454Py_MakePendingCalls(void)
455{
456 assert(PyGILState_Check());
457
458 /* Python signal handler doesn't really queue a callback: it only signals
459 that a signal was received, see _PyEval_SignalReceived(). */
460 int res = handle_signals();
461 if (res != 0) {
462 return res;
463 }
464
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100465 res = make_pending_calls();
466 if (res != 0) {
467 return res;
468 }
469
470 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000471}
472
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000473/* The interpreter's recursion limit */
474
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000475#ifndef Py_DEFAULT_RECURSION_LIMIT
476#define Py_DEFAULT_RECURSION_LIMIT 1000
477#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600478
Eric Snow05351c12017-09-05 21:43:08 -0700479int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000480
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600481void
482_PyEval_Initialize(struct _ceval_runtime_state *state)
483{
484 state->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
485 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
486 _gil_initialize(&state->gil);
487}
488
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000489int
490Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000491{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600492 return _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000493}
494
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000495void
496Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000497{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600498 _PyRuntime.ceval.recursion_limit = new_limit;
499 _Py_CheckRecursionLimit = _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000500}
501
Armin Rigo2b3eb402003-10-28 12:05:48 +0000502/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
503 if the recursion_depth reaches _Py_CheckRecursionLimit.
504 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
505 to guarantee that _Py_CheckRecursiveCall() is regularly called.
506 Without USE_STACKCHECK, there is no need for this. */
507int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300508_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000509{
Victor Stinner50b48572018-11-01 01:51:40 +0100510 PyThreadState *tstate = _PyThreadState_GET();
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600511 int recursion_limit = _PyRuntime.ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000512
513#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700514 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 if (PyOS_CheckStack()) {
516 --tstate->recursion_depth;
517 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
518 return -1;
519 }
pdox18967932017-10-25 23:03:01 -0700520 /* Needed for ABI backwards-compatibility (see bpo-31857) */
Eric Snow05351c12017-09-05 21:43:08 -0700521 _Py_CheckRecursionLimit = recursion_limit;
pdox18967932017-10-25 23:03:01 -0700522#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 if (tstate->recursion_critical)
524 /* Somebody asked that we don't check for recursion. */
525 return 0;
526 if (tstate->overflowed) {
527 if (tstate->recursion_depth > recursion_limit + 50) {
528 /* Overflowing while handling an overflow. Give up. */
529 Py_FatalError("Cannot recover from stack overflow.");
530 }
531 return 0;
532 }
533 if (tstate->recursion_depth > recursion_limit) {
534 --tstate->recursion_depth;
535 tstate->overflowed = 1;
Yury Selivanovf488fb42015-07-03 01:04:23 -0400536 PyErr_Format(PyExc_RecursionError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 "maximum recursion depth exceeded%s",
538 where);
539 return -1;
540 }
541 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000542}
543
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400544static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000545static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000546
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600547#define _Py_TracingPossible _PyRuntime.ceval.tracing_possible
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000548
Guido van Rossum374a9221991-04-04 10:40:29 +0000549
Guido van Rossumb209a111997-04-29 18:18:01 +0000550PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000551PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000552{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 return PyEval_EvalCodeEx(co,
554 globals, locals,
555 (PyObject **)NULL, 0,
556 (PyObject **)NULL, 0,
557 (PyObject **)NULL, 0,
558 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000559}
560
561
562/* Interpreter main loop */
563
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000564PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000565PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 /* This is for backward compatibility with extension modules that
567 used this API; core interpreter code should call
568 PyEval_EvalFrameEx() */
569 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000570}
571
572PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000573PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000574{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200575 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
576 return interp->eval_frame(f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700577}
578
Victor Stinnerc6944e72016-11-11 02:13:35 +0100579PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700580_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
581{
Guido van Rossum950361c1997-01-24 13:49:28 +0000582#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000584#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200585 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300586 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200587 int opcode; /* Current opcode */
588 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200589 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 PyObject *retval = NULL; /* Return value */
Victor Stinner50b48572018-11-01 01:51:40 +0100591 PyThreadState *tstate = _PyThreadState_GET();
Eric Snow7bda9de2019-03-08 17:25:54 -0700592 _Py_atomic_int *eval_breaker = &_PyRuntime.ceval.eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 is true when the line being executed has changed. The
600 initial values are such as to make this false the first
601 time it is tested. */
602 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000603
Serhiy Storchakaab874002016-09-11 13:48:15 +0300604 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 PyObject *names;
606 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000607
Brett Cannon368b4b72012-04-02 12:17:59 -0400608#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200609 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400610#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200611
Antoine Pitroub52ec782009-01-25 16:34:23 +0000612/* Computed GOTOs, or
613 the-optimization-commonly-but-improperly-known-as-"threaded code"
614 using gcc's labels-as-values extension
615 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
616
617 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000619 combined with a lookup table of jump addresses. However, since the
620 indirect jump instruction is shared by all opcodes, the CPU will have a
621 hard time making the right prediction for where to jump next (actually,
622 it will be always wrong except in the uncommon case of a sequence of
623 several identical opcodes).
624
625 "Threaded code" in contrast, uses an explicit jump table and an explicit
626 indirect jump instruction at the end of each opcode. Since the jump
627 instruction is at a different address for each opcode, the CPU will make a
628 separate prediction for each of these instructions, which is equivalent to
629 predicting the second opcode of each opcode pair. These predictions have
630 a much better chance to turn out valid, especially in small bytecode loops.
631
632 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000634 and potentially many more instructions (depending on the pipeline width).
635 A correctly predicted branch, however, is nearly free.
636
637 At the time of this writing, the "threaded code" version is up to 15-20%
638 faster than the normal "switch" version, depending on the compiler and the
639 CPU architecture.
640
641 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
642 because it would render the measurements invalid.
643
644
645 NOTE: care must be taken that the compiler doesn't try to "optimize" the
646 indirect jumps by sharing them between all opcodes. Such optimizations
647 can be disabled on gcc by using the -fno-gcse flag (or possibly
648 -fno-crossjumping).
649*/
650
Antoine Pitrou042b1282010-08-13 21:15:58 +0000651#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000652#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000653#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000654#endif
655
Antoine Pitrou042b1282010-08-13 21:15:58 +0000656#ifdef HAVE_COMPUTED_GOTOS
657 #ifndef USE_COMPUTED_GOTOS
658 #define USE_COMPUTED_GOTOS 1
659 #endif
660#else
661 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
662 #error "Computed gotos are not supported on this compiler."
663 #endif
664 #undef USE_COMPUTED_GOTOS
665 #define USE_COMPUTED_GOTOS 0
666#endif
667
668#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000669/* Import the static jump table */
670#include "opcode_targets.h"
671
Antoine Pitroub52ec782009-01-25 16:34:23 +0000672#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -0700673 op: \
674 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000675
Antoine Pitroub52ec782009-01-25 16:34:23 +0000676#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 { \
Eric Snow7bda9de2019-03-08 17:25:54 -0700678 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 FAST_DISPATCH(); \
680 } \
681 continue; \
682 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000683
684#ifdef LLTRACE
685#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700687 if (!lltrace && !_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300689 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300690 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 } \
692 goto fast_next_opcode; \
693 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000694#else
695#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700697 if (!_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300699 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300700 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 } \
702 goto fast_next_opcode; \
703 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000704#endif
705
706#else
Benjamin Petersonddd19492018-09-16 22:38:02 -0700707#define TARGET(op) op
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300708
Antoine Pitroub52ec782009-01-25 16:34:23 +0000709#define DISPATCH() continue
710#define FAST_DISPATCH() goto fast_next_opcode
711#endif
712
713
Neal Norwitza81d2202002-07-14 00:27:26 +0000714/* Tuple access macros */
715
716#ifndef Py_DEBUG
717#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
718#else
719#define GETITEM(v, i) PyTuple_GetItem((v), (i))
720#endif
721
Guido van Rossum374a9221991-04-04 10:40:29 +0000722/* Code access macros */
723
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300724/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600725#define INSTR_OFFSET() \
726 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300727#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300728 _Py_CODEUNIT word = *next_instr; \
729 opcode = _Py_OPCODE(word); \
730 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300731 next_instr++; \
732 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300733#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
734#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000735
Raymond Hettingerf606f872003-03-16 03:11:04 +0000736/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 Some opcodes tend to come in pairs thus making it possible to
738 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300739 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000740
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 Verifying the prediction costs a single high-speed test of a register
742 variable against a constant. If the pairing was good, then the
743 processor's own internal branch predication has a high likelihood of
744 success, resulting in a nearly zero-overhead transition to the
745 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300746 including its unpredictable switch-case branch. Combined with the
747 processor's internal branch prediction, a successful PREDICT has the
748 effect of making the two opcodes run as if they were a single new opcode
749 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000750
Georg Brandl86b2fb92008-07-16 03:43:04 +0000751 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 predictions turned-on and interpret the results as if some opcodes
753 had been combined or turn-off predictions so that the opcode frequency
754 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000755
756 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 the CPU to record separate branch prediction information for each
758 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000759
Raymond Hettingerf606f872003-03-16 03:11:04 +0000760*/
761
Antoine Pitrou042b1282010-08-13 21:15:58 +0000762#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000764#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300765#define PREDICT(op) \
766 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300767 _Py_CODEUNIT word = *next_instr; \
768 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300769 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300770 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300771 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300772 goto PRED_##op; \
773 } \
774 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000775#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300776#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000777
Raymond Hettingerf606f872003-03-16 03:11:04 +0000778
Guido van Rossum374a9221991-04-04 10:40:29 +0000779/* Stack manipulation macros */
780
Martin v. Löwis18e16552006-02-15 17:27:45 +0000781/* The stack can grow at most MAXINT deep, as co_nlocals and
782 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000783#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
784#define EMPTY() (STACK_LEVEL() == 0)
785#define TOP() (stack_pointer[-1])
786#define SECOND() (stack_pointer[-2])
787#define THIRD() (stack_pointer[-3])
788#define FOURTH() (stack_pointer[-4])
789#define PEEK(n) (stack_pointer[-(n)])
790#define SET_TOP(v) (stack_pointer[-1] = (v))
791#define SET_SECOND(v) (stack_pointer[-2] = (v))
792#define SET_THIRD(v) (stack_pointer[-3] = (v))
793#define SET_FOURTH(v) (stack_pointer[-4] = (v))
794#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
795#define BASIC_STACKADJ(n) (stack_pointer += n)
796#define BASIC_PUSH(v) (*stack_pointer++ = (v))
797#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000798
Guido van Rossum96a42c81992-01-12 02:29:51 +0000799#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000801 lltrace && prtrace(TOP(), "push")); \
802 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000804 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +0000805#define STACK_GROW(n) do { \
806 assert(n >= 0); \
807 (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000808 lltrace && prtrace(TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +0000809 assert(STACK_LEVEL() <= co->co_stacksize); \
810 } while (0)
811#define STACK_SHRINK(n) do { \
812 assert(n >= 0); \
813 (void)(lltrace && prtrace(TOP(), "stackadj")); \
814 (void)(BASIC_STACKADJ(-n)); \
815 assert(STACK_LEVEL() <= co->co_stacksize); \
816 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +0000817#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +0000818 prtrace((STACK_POINTER)[-1], "ext_pop")), \
819 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000820#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000821#define PUSH(v) BASIC_PUSH(v)
822#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +0000823#define STACK_GROW(n) BASIC_STACKADJ(n)
824#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000825#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000826#endif
827
Guido van Rossum681d79a1995-07-18 14:51:37 +0000828/* Local variable macros */
829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000831
832/* The SETLOCAL() macro must not DECREF the local variable in-place and
833 then store the new value; it must copy the old value to a temporary
834 value, then store the new value, and then DECREF the temporary value.
835 This is because it is possible that during the DECREF the frame is
836 accessed by other code (e.g. a __del__ method or gc.collect()) and the
837 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000839 GETLOCAL(i) = value; \
840 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000841
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000842
843#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 while (STACK_LEVEL() > (b)->b_level) { \
845 PyObject *v = POP(); \
846 Py_XDECREF(v); \
847 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000848
849#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300850 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +0100852 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 assert(STACK_LEVEL() >= (b)->b_level + 3); \
854 while (STACK_LEVEL() > (b)->b_level + 3) { \
855 value = POP(); \
856 Py_XDECREF(value); \
857 } \
Mark Shannonae3087c2017-10-22 22:41:51 +0100858 exc_info = tstate->exc_info; \
859 type = exc_info->exc_type; \
860 value = exc_info->exc_value; \
861 traceback = exc_info->exc_traceback; \
862 exc_info->exc_type = POP(); \
863 exc_info->exc_value = POP(); \
864 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 Py_XDECREF(type); \
866 Py_XDECREF(value); \
867 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300868 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000869
Guido van Rossuma027efa1997-05-05 20:56:21 +0000870/* Start of code */
871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 /* push frame */
873 if (Py_EnterRecursiveCall(""))
874 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000875
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 if (tstate->use_tracing) {
879 if (tstate->c_tracefunc != NULL) {
880 /* tstate->c_tracefunc, if defined, is a
881 function that will be called on *every* entry
882 to a code block. Its return value, if not
883 None, is a function that will be called at
884 the start of each executed line of code.
885 (Actually, the function must return itself
886 in order to continue tracing.) The trace
887 functions are called with three arguments:
888 a pointer to the current frame, a string
889 indicating why the function is called, and
890 an argument which depends on the situation.
891 The global trace function is also called
892 whenever an exception is detected. */
893 if (call_trace_protected(tstate->c_tracefunc,
894 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100895 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 /* Trace function raised an error */
897 goto exit_eval_frame;
898 }
899 }
900 if (tstate->c_profilefunc != NULL) {
901 /* Similar for c_profilefunc, except it needn't
902 return itself and isn't called for "line" events */
903 if (call_trace_protected(tstate->c_profilefunc,
904 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100905 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 /* Profile function raised an error */
907 goto exit_eval_frame;
908 }
909 }
910 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000911
Łukasz Langaa785c872016-09-09 17:37:37 -0700912 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
913 dtrace_function_entry(f);
914
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 co = f->f_code;
916 names = co->co_names;
917 consts = co->co_consts;
918 fastlocals = f->f_localsplus;
919 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300920 assert(PyBytes_Check(co->co_code));
921 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +0300922 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
923 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
924 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300925 /*
926 f->f_lasti refers to the index of the last instruction,
927 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000928
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300929 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500930 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000931
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 When the PREDICT() macros are enabled, some opcode pairs follow in
933 direct succession without updating f->f_lasti. A successful
934 prediction effectively links the two codes together as if they
935 were a single new opcode; accordingly,f->f_lasti will point to
936 the first code in the pair (for instance, GET_ITER followed by
937 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300938 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300940 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300941 next_instr = first_instr;
942 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +0300943 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
944 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300945 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 stack_pointer = f->f_stacktop;
947 assert(stack_pointer != NULL);
948 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +0200949 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000950
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000951
Tim Peters5ca576e2001-06-18 22:08:13 +0000952#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200953 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000954#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000955
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400956 if (throwflag) /* support for generator.throw() */
957 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000958
Victor Stinnerace47d72013-07-18 01:41:08 +0200959#ifdef Py_DEBUG
960 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +0100961 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +0000962 caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +0200963 assert(!PyErr_Occurred());
964#endif
965
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200966main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 assert(stack_pointer >= f->f_valuestack); /* else underflow */
969 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +0200970 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000971
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 /* Do periodic things. Doing this every time through
973 the loop would add too much overhead, so we do it
974 only every Nth instruction. We also do it if
975 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
976 event needs attention (e.g. a signal handler or
977 async I/O handler); see Py_AddPendingCall() and
978 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000979
Eric Snow7bda9de2019-03-08 17:25:54 -0700980 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +0300981 opcode = _Py_OPCODE(*next_instr);
982 if (opcode == SETUP_FINALLY ||
983 opcode == SETUP_WITH ||
984 opcode == BEFORE_ASYNC_WITH ||
985 opcode == YIELD_FROM) {
986 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -0700987 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +0300988 - If we're about to enter the 'with:'. It will prevent
989 emitting a resource warning in the common idiom
990 'with open(path) as file:'.
991 - If we're about to enter the 'async with:'.
992 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -0700993 *very* useful, but might help in some cases and it's
994 traditional)
995 - If we're resuming a chain of nested 'yield from' or
996 'await' calls, then each frame is parked with YIELD_FROM
997 as its next opcode. If the user hit control-C we want to
998 wait until we've reached the innermost frame before
999 running the signal handler and raising KeyboardInterrupt
1000 (see bpo-30039).
1001 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 goto fast_next_opcode;
1003 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001004
1005 if (_Py_atomic_load_relaxed(
1006 &_PyRuntime.ceval.signals_pending))
1007 {
1008 if (handle_signals() != 0) {
1009 goto error;
1010 }
1011 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001012 if (_Py_atomic_load_relaxed(
Victor Stinner4d61e6e2019-03-04 14:21:28 +01001013 &_PyRuntime.ceval.pending.calls_to_do))
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001014 {
Victor Stinner4d61e6e2019-03-04 14:21:28 +01001015 if (make_pending_calls() != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001016 goto error;
Eric Snowfdf282d2019-01-11 14:26:55 -07001017 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001019
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001020 if (_Py_atomic_load_relaxed(
1021 &_PyRuntime.ceval.gil_drop_request))
1022 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 /* Give another thread a chance */
1024 if (PyThreadState_Swap(NULL) != tstate)
1025 Py_FatalError("ceval: tstate mix-up");
1026 drop_gil(tstate);
1027
1028 /* Other threads may run now */
1029
1030 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001031
1032 /* Check if we should make a quick exit. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001033 if (_Py_IsFinalizing() &&
1034 !_Py_CURRENTLY_FINALIZING(tstate))
1035 {
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001036 drop_gil(tstate);
1037 PyThread_exit_thread();
1038 }
1039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 if (PyThreadState_Swap(tstate) != NULL)
1041 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 }
1043 /* Check for asynchronous exceptions. */
1044 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001045 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001046 tstate->async_exc = NULL;
Victor Stinner4d61e6e2019-03-04 14:21:28 +01001047 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001048 PyErr_SetNone(exc);
1049 Py_DECREF(exc);
1050 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 }
1052 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001053
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 fast_next_opcode:
1055 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001056
Łukasz Langaa785c872016-09-09 17:37:37 -07001057 if (PyDTrace_LINE_ENABLED())
1058 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001061
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001063 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001064 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 /* see maybe_call_line_trace
1066 for expository comments */
1067 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001068
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 err = maybe_call_line_trace(tstate->c_tracefunc,
1070 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001071 tstate, f,
1072 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 /* Reload possibly changed frame fields */
1074 JUMPTO(f->f_lasti);
1075 if (f->f_stacktop != NULL) {
1076 stack_pointer = f->f_stacktop;
1077 f->f_stacktop = NULL;
1078 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001079 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001081 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001083
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001085
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001086 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001087 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001088#ifdef DYNAMIC_EXECUTION_PROFILE
1089#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 dxpairs[lastopcode][opcode]++;
1091 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001092#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001094#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001095
Guido van Rossum96a42c81992-01-12 02:29:51 +00001096#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001098
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 if (lltrace) {
1100 if (HAS_ARG(opcode)) {
1101 printf("%d: %d, %d\n",
1102 f->f_lasti, opcode, oparg);
1103 }
1104 else {
1105 printf("%d: %d\n",
1106 f->f_lasti, opcode);
1107 }
1108 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001109#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001110
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001112
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001114 It is essential that any operation that fails must goto error
1115 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001116
Benjamin Petersonddd19492018-09-16 22:38:02 -07001117 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001119 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001120
Benjamin Petersonddd19492018-09-16 22:38:02 -07001121 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001122 PyObject *value = GETLOCAL(oparg);
1123 if (value == NULL) {
1124 format_exc_check_arg(PyExc_UnboundLocalError,
1125 UNBOUNDLOCAL_ERROR_MSG,
1126 PyTuple_GetItem(co->co_varnames, oparg));
1127 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001128 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001129 Py_INCREF(value);
1130 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001132 }
1133
Benjamin Petersonddd19492018-09-16 22:38:02 -07001134 case TARGET(LOAD_CONST): {
1135 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001136 PyObject *value = GETITEM(consts, oparg);
1137 Py_INCREF(value);
1138 PUSH(value);
1139 FAST_DISPATCH();
1140 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001141
Benjamin Petersonddd19492018-09-16 22:38:02 -07001142 case TARGET(STORE_FAST): {
1143 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001144 PyObject *value = POP();
1145 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001147 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001148
Benjamin Petersonddd19492018-09-16 22:38:02 -07001149 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001150 PyObject *value = POP();
1151 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001153 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001154
Benjamin Petersonddd19492018-09-16 22:38:02 -07001155 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001156 PyObject *top = TOP();
1157 PyObject *second = SECOND();
1158 SET_TOP(second);
1159 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001160 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001161 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001162
Benjamin Petersonddd19492018-09-16 22:38:02 -07001163 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001164 PyObject *top = TOP();
1165 PyObject *second = SECOND();
1166 PyObject *third = THIRD();
1167 SET_TOP(second);
1168 SET_SECOND(third);
1169 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001170 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001171 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001172
Benjamin Petersonddd19492018-09-16 22:38:02 -07001173 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001174 PyObject *top = TOP();
1175 PyObject *second = SECOND();
1176 PyObject *third = THIRD();
1177 PyObject *fourth = FOURTH();
1178 SET_TOP(second);
1179 SET_SECOND(third);
1180 SET_THIRD(fourth);
1181 SET_FOURTH(top);
1182 FAST_DISPATCH();
1183 }
1184
Benjamin Petersonddd19492018-09-16 22:38:02 -07001185 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001186 PyObject *top = TOP();
1187 Py_INCREF(top);
1188 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001190 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001191
Benjamin Petersonddd19492018-09-16 22:38:02 -07001192 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001193 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001194 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001195 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001196 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001197 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001198 SET_TOP(top);
1199 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001200 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001201 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001202
Benjamin Petersonddd19492018-09-16 22:38:02 -07001203 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001204 PyObject *value = TOP();
1205 PyObject *res = PyNumber_Positive(value);
1206 Py_DECREF(value);
1207 SET_TOP(res);
1208 if (res == NULL)
1209 goto error;
1210 DISPATCH();
1211 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001212
Benjamin Petersonddd19492018-09-16 22:38:02 -07001213 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001214 PyObject *value = TOP();
1215 PyObject *res = PyNumber_Negative(value);
1216 Py_DECREF(value);
1217 SET_TOP(res);
1218 if (res == NULL)
1219 goto error;
1220 DISPATCH();
1221 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001222
Benjamin Petersonddd19492018-09-16 22:38:02 -07001223 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001224 PyObject *value = TOP();
1225 int err = PyObject_IsTrue(value);
1226 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 if (err == 0) {
1228 Py_INCREF(Py_True);
1229 SET_TOP(Py_True);
1230 DISPATCH();
1231 }
1232 else if (err > 0) {
1233 Py_INCREF(Py_False);
1234 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 DISPATCH();
1236 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001237 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001238 goto error;
1239 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001240
Benjamin Petersonddd19492018-09-16 22:38:02 -07001241 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001242 PyObject *value = TOP();
1243 PyObject *res = PyNumber_Invert(value);
1244 Py_DECREF(value);
1245 SET_TOP(res);
1246 if (res == NULL)
1247 goto error;
1248 DISPATCH();
1249 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001250
Benjamin Petersonddd19492018-09-16 22:38:02 -07001251 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001252 PyObject *exp = POP();
1253 PyObject *base = TOP();
1254 PyObject *res = PyNumber_Power(base, exp, Py_None);
1255 Py_DECREF(base);
1256 Py_DECREF(exp);
1257 SET_TOP(res);
1258 if (res == NULL)
1259 goto error;
1260 DISPATCH();
1261 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001262
Benjamin Petersonddd19492018-09-16 22:38:02 -07001263 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001264 PyObject *right = POP();
1265 PyObject *left = TOP();
1266 PyObject *res = PyNumber_Multiply(left, right);
1267 Py_DECREF(left);
1268 Py_DECREF(right);
1269 SET_TOP(res);
1270 if (res == NULL)
1271 goto error;
1272 DISPATCH();
1273 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001274
Benjamin Petersonddd19492018-09-16 22:38:02 -07001275 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001276 PyObject *right = POP();
1277 PyObject *left = TOP();
1278 PyObject *res = PyNumber_MatrixMultiply(left, right);
1279 Py_DECREF(left);
1280 Py_DECREF(right);
1281 SET_TOP(res);
1282 if (res == NULL)
1283 goto error;
1284 DISPATCH();
1285 }
1286
Benjamin Petersonddd19492018-09-16 22:38:02 -07001287 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001288 PyObject *divisor = POP();
1289 PyObject *dividend = TOP();
1290 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1291 Py_DECREF(dividend);
1292 Py_DECREF(divisor);
1293 SET_TOP(quotient);
1294 if (quotient == NULL)
1295 goto error;
1296 DISPATCH();
1297 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001298
Benjamin Petersonddd19492018-09-16 22:38:02 -07001299 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001300 PyObject *divisor = POP();
1301 PyObject *dividend = TOP();
1302 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1303 Py_DECREF(dividend);
1304 Py_DECREF(divisor);
1305 SET_TOP(quotient);
1306 if (quotient == NULL)
1307 goto error;
1308 DISPATCH();
1309 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001310
Benjamin Petersonddd19492018-09-16 22:38:02 -07001311 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001312 PyObject *divisor = POP();
1313 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001314 PyObject *res;
1315 if (PyUnicode_CheckExact(dividend) && (
1316 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1317 // fast path; string formatting, but not if the RHS is a str subclass
1318 // (see issue28598)
1319 res = PyUnicode_Format(dividend, divisor);
1320 } else {
1321 res = PyNumber_Remainder(dividend, divisor);
1322 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001323 Py_DECREF(divisor);
1324 Py_DECREF(dividend);
1325 SET_TOP(res);
1326 if (res == NULL)
1327 goto error;
1328 DISPATCH();
1329 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001330
Benjamin Petersonddd19492018-09-16 22:38:02 -07001331 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001332 PyObject *right = POP();
1333 PyObject *left = TOP();
1334 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001335 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1336 CPython using bytecode, it is simply worthless.
1337 See http://bugs.python.org/issue21955 and
1338 http://bugs.python.org/issue10044 for the discussion. In short,
1339 no patch shown any impact on a realistic benchmark, only a minor
1340 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001341 if (PyUnicode_CheckExact(left) &&
1342 PyUnicode_CheckExact(right)) {
1343 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001344 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001345 }
1346 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001347 sum = PyNumber_Add(left, right);
1348 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001349 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001350 Py_DECREF(right);
1351 SET_TOP(sum);
1352 if (sum == NULL)
1353 goto error;
1354 DISPATCH();
1355 }
1356
Benjamin Petersonddd19492018-09-16 22:38:02 -07001357 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001358 PyObject *right = POP();
1359 PyObject *left = TOP();
1360 PyObject *diff = PyNumber_Subtract(left, right);
1361 Py_DECREF(right);
1362 Py_DECREF(left);
1363 SET_TOP(diff);
1364 if (diff == NULL)
1365 goto error;
1366 DISPATCH();
1367 }
1368
Benjamin Petersonddd19492018-09-16 22:38:02 -07001369 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001370 PyObject *sub = POP();
1371 PyObject *container = TOP();
1372 PyObject *res = PyObject_GetItem(container, sub);
1373 Py_DECREF(container);
1374 Py_DECREF(sub);
1375 SET_TOP(res);
1376 if (res == NULL)
1377 goto error;
1378 DISPATCH();
1379 }
1380
Benjamin Petersonddd19492018-09-16 22:38:02 -07001381 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001382 PyObject *right = POP();
1383 PyObject *left = TOP();
1384 PyObject *res = PyNumber_Lshift(left, right);
1385 Py_DECREF(left);
1386 Py_DECREF(right);
1387 SET_TOP(res);
1388 if (res == NULL)
1389 goto error;
1390 DISPATCH();
1391 }
1392
Benjamin Petersonddd19492018-09-16 22:38:02 -07001393 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001394 PyObject *right = POP();
1395 PyObject *left = TOP();
1396 PyObject *res = PyNumber_Rshift(left, right);
1397 Py_DECREF(left);
1398 Py_DECREF(right);
1399 SET_TOP(res);
1400 if (res == NULL)
1401 goto error;
1402 DISPATCH();
1403 }
1404
Benjamin Petersonddd19492018-09-16 22:38:02 -07001405 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001406 PyObject *right = POP();
1407 PyObject *left = TOP();
1408 PyObject *res = PyNumber_And(left, right);
1409 Py_DECREF(left);
1410 Py_DECREF(right);
1411 SET_TOP(res);
1412 if (res == NULL)
1413 goto error;
1414 DISPATCH();
1415 }
1416
Benjamin Petersonddd19492018-09-16 22:38:02 -07001417 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001418 PyObject *right = POP();
1419 PyObject *left = TOP();
1420 PyObject *res = PyNumber_Xor(left, right);
1421 Py_DECREF(left);
1422 Py_DECREF(right);
1423 SET_TOP(res);
1424 if (res == NULL)
1425 goto error;
1426 DISPATCH();
1427 }
1428
Benjamin Petersonddd19492018-09-16 22:38:02 -07001429 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001430 PyObject *right = POP();
1431 PyObject *left = TOP();
1432 PyObject *res = PyNumber_Or(left, right);
1433 Py_DECREF(left);
1434 Py_DECREF(right);
1435 SET_TOP(res);
1436 if (res == NULL)
1437 goto error;
1438 DISPATCH();
1439 }
1440
Benjamin Petersonddd19492018-09-16 22:38:02 -07001441 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001442 PyObject *v = POP();
1443 PyObject *list = PEEK(oparg);
1444 int err;
1445 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001446 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001447 if (err != 0)
1448 goto error;
1449 PREDICT(JUMP_ABSOLUTE);
1450 DISPATCH();
1451 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001452
Benjamin Petersonddd19492018-09-16 22:38:02 -07001453 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001454 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001455 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001456 int err;
1457 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001458 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001459 if (err != 0)
1460 goto error;
1461 PREDICT(JUMP_ABSOLUTE);
1462 DISPATCH();
1463 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001464
Benjamin Petersonddd19492018-09-16 22:38:02 -07001465 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001466 PyObject *exp = POP();
1467 PyObject *base = TOP();
1468 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1469 Py_DECREF(base);
1470 Py_DECREF(exp);
1471 SET_TOP(res);
1472 if (res == NULL)
1473 goto error;
1474 DISPATCH();
1475 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001476
Benjamin Petersonddd19492018-09-16 22:38:02 -07001477 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001478 PyObject *right = POP();
1479 PyObject *left = TOP();
1480 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1481 Py_DECREF(left);
1482 Py_DECREF(right);
1483 SET_TOP(res);
1484 if (res == NULL)
1485 goto error;
1486 DISPATCH();
1487 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001488
Benjamin Petersonddd19492018-09-16 22:38:02 -07001489 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001490 PyObject *right = POP();
1491 PyObject *left = TOP();
1492 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1493 Py_DECREF(left);
1494 Py_DECREF(right);
1495 SET_TOP(res);
1496 if (res == NULL)
1497 goto error;
1498 DISPATCH();
1499 }
1500
Benjamin Petersonddd19492018-09-16 22:38:02 -07001501 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001502 PyObject *divisor = POP();
1503 PyObject *dividend = TOP();
1504 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1505 Py_DECREF(dividend);
1506 Py_DECREF(divisor);
1507 SET_TOP(quotient);
1508 if (quotient == NULL)
1509 goto error;
1510 DISPATCH();
1511 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001512
Benjamin Petersonddd19492018-09-16 22:38:02 -07001513 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001514 PyObject *divisor = POP();
1515 PyObject *dividend = TOP();
1516 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1517 Py_DECREF(dividend);
1518 Py_DECREF(divisor);
1519 SET_TOP(quotient);
1520 if (quotient == NULL)
1521 goto error;
1522 DISPATCH();
1523 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001524
Benjamin Petersonddd19492018-09-16 22:38:02 -07001525 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001526 PyObject *right = POP();
1527 PyObject *left = TOP();
1528 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1529 Py_DECREF(left);
1530 Py_DECREF(right);
1531 SET_TOP(mod);
1532 if (mod == NULL)
1533 goto error;
1534 DISPATCH();
1535 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001536
Benjamin Petersonddd19492018-09-16 22:38:02 -07001537 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001538 PyObject *right = POP();
1539 PyObject *left = TOP();
1540 PyObject *sum;
1541 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1542 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001543 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001544 }
1545 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001546 sum = PyNumber_InPlaceAdd(left, right);
1547 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001548 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001549 Py_DECREF(right);
1550 SET_TOP(sum);
1551 if (sum == NULL)
1552 goto error;
1553 DISPATCH();
1554 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001555
Benjamin Petersonddd19492018-09-16 22:38:02 -07001556 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001557 PyObject *right = POP();
1558 PyObject *left = TOP();
1559 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1560 Py_DECREF(left);
1561 Py_DECREF(right);
1562 SET_TOP(diff);
1563 if (diff == NULL)
1564 goto error;
1565 DISPATCH();
1566 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001567
Benjamin Petersonddd19492018-09-16 22:38:02 -07001568 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001569 PyObject *right = POP();
1570 PyObject *left = TOP();
1571 PyObject *res = PyNumber_InPlaceLshift(left, right);
1572 Py_DECREF(left);
1573 Py_DECREF(right);
1574 SET_TOP(res);
1575 if (res == NULL)
1576 goto error;
1577 DISPATCH();
1578 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001579
Benjamin Petersonddd19492018-09-16 22:38:02 -07001580 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001581 PyObject *right = POP();
1582 PyObject *left = TOP();
1583 PyObject *res = PyNumber_InPlaceRshift(left, right);
1584 Py_DECREF(left);
1585 Py_DECREF(right);
1586 SET_TOP(res);
1587 if (res == NULL)
1588 goto error;
1589 DISPATCH();
1590 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001591
Benjamin Petersonddd19492018-09-16 22:38:02 -07001592 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001593 PyObject *right = POP();
1594 PyObject *left = TOP();
1595 PyObject *res = PyNumber_InPlaceAnd(left, right);
1596 Py_DECREF(left);
1597 Py_DECREF(right);
1598 SET_TOP(res);
1599 if (res == NULL)
1600 goto error;
1601 DISPATCH();
1602 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001603
Benjamin Petersonddd19492018-09-16 22:38:02 -07001604 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001605 PyObject *right = POP();
1606 PyObject *left = TOP();
1607 PyObject *res = PyNumber_InPlaceXor(left, right);
1608 Py_DECREF(left);
1609 Py_DECREF(right);
1610 SET_TOP(res);
1611 if (res == NULL)
1612 goto error;
1613 DISPATCH();
1614 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001615
Benjamin Petersonddd19492018-09-16 22:38:02 -07001616 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001617 PyObject *right = POP();
1618 PyObject *left = TOP();
1619 PyObject *res = PyNumber_InPlaceOr(left, right);
1620 Py_DECREF(left);
1621 Py_DECREF(right);
1622 SET_TOP(res);
1623 if (res == NULL)
1624 goto error;
1625 DISPATCH();
1626 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001627
Benjamin Petersonddd19492018-09-16 22:38:02 -07001628 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001629 PyObject *sub = TOP();
1630 PyObject *container = SECOND();
1631 PyObject *v = THIRD();
1632 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001633 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001634 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001635 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001637 Py_DECREF(container);
1638 Py_DECREF(sub);
1639 if (err != 0)
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(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001645 PyObject *sub = TOP();
1646 PyObject *container = SECOND();
1647 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001648 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00001649 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001650 err = PyObject_DelItem(container, sub);
1651 Py_DECREF(container);
1652 Py_DECREF(sub);
1653 if (err != 0)
1654 goto error;
1655 DISPATCH();
1656 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001657
Benjamin Petersonddd19492018-09-16 22:38:02 -07001658 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01001659 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001660 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001661 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001662 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001663 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664 PyErr_SetString(PyExc_RuntimeError,
1665 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001666 Py_DECREF(value);
1667 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001668 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001669 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001670 Py_DECREF(value);
1671 if (res == NULL)
1672 goto error;
1673 Py_DECREF(res);
1674 DISPATCH();
1675 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001676
Benjamin Petersonddd19492018-09-16 22:38:02 -07001677 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001678 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679 switch (oparg) {
1680 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001681 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001682 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001684 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001685 /* fall through */
1686 case 0:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001687 if (do_raise(exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001688 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001689 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001690 break;
1691 default:
1692 PyErr_SetString(PyExc_SystemError,
1693 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001694 break;
1695 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001696 goto error;
1697 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001698
Benjamin Petersonddd19492018-09-16 22:38:02 -07001699 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001701 assert(f->f_iblock == 0);
1702 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001703 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001704
Benjamin Petersonddd19492018-09-16 22:38:02 -07001705 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001706 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001707 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001708 PyObject *obj = TOP();
1709 PyTypeObject *type = Py_TYPE(obj);
1710
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001711 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001712 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001713 }
Yury Selivanov75445082015-05-11 22:57:16 -04001714
1715 if (getter != NULL) {
1716 iter = (*getter)(obj);
1717 Py_DECREF(obj);
1718 if (iter == NULL) {
1719 SET_TOP(NULL);
1720 goto error;
1721 }
1722 }
1723 else {
1724 SET_TOP(NULL);
1725 PyErr_Format(
1726 PyExc_TypeError,
1727 "'async for' requires an object with "
1728 "__aiter__ method, got %.100s",
1729 type->tp_name);
1730 Py_DECREF(obj);
1731 goto error;
1732 }
1733
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001734 if (Py_TYPE(iter)->tp_as_async == NULL ||
1735 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001736
Yury Selivanov398ff912017-03-02 22:20:00 -05001737 SET_TOP(NULL);
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001738 PyErr_Format(
1739 PyExc_TypeError,
1740 "'async for' received an object from __aiter__ "
1741 "that does not implement __anext__: %.100s",
1742 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001743 Py_DECREF(iter);
1744 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001745 }
1746
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001747 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001748 DISPATCH();
1749 }
1750
Benjamin Petersonddd19492018-09-16 22:38:02 -07001751 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001752 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001753 PyObject *next_iter = NULL;
1754 PyObject *awaitable = NULL;
1755 PyObject *aiter = TOP();
1756 PyTypeObject *type = Py_TYPE(aiter);
1757
Yury Selivanoveb636452016-09-08 22:01:51 -07001758 if (PyAsyncGen_CheckExact(aiter)) {
1759 awaitable = type->tp_as_async->am_anext(aiter);
1760 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001761 goto error;
1762 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001763 } else {
1764 if (type->tp_as_async != NULL){
1765 getter = type->tp_as_async->am_anext;
1766 }
Yury Selivanov75445082015-05-11 22:57:16 -04001767
Yury Selivanoveb636452016-09-08 22:01:51 -07001768 if (getter != NULL) {
1769 next_iter = (*getter)(aiter);
1770 if (next_iter == NULL) {
1771 goto error;
1772 }
1773 }
1774 else {
1775 PyErr_Format(
1776 PyExc_TypeError,
1777 "'async for' requires an iterator with "
1778 "__anext__ method, got %.100s",
1779 type->tp_name);
1780 goto error;
1781 }
Yury Selivanov75445082015-05-11 22:57:16 -04001782
Yury Selivanoveb636452016-09-08 22:01:51 -07001783 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1784 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001785 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001786 PyExc_TypeError,
1787 "'async for' received an invalid object "
1788 "from __anext__: %.100s",
1789 Py_TYPE(next_iter)->tp_name);
1790
1791 Py_DECREF(next_iter);
1792 goto error;
1793 } else {
1794 Py_DECREF(next_iter);
1795 }
1796 }
Yury Selivanov75445082015-05-11 22:57:16 -04001797
1798 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001799 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001800 DISPATCH();
1801 }
1802
Benjamin Petersonddd19492018-09-16 22:38:02 -07001803 case TARGET(GET_AWAITABLE): {
1804 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04001805 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04001806 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04001807
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03001808 if (iter == NULL) {
1809 format_awaitable_error(Py_TYPE(iterable),
1810 _Py_OPCODE(next_instr[-2]));
1811 }
1812
Yury Selivanov75445082015-05-11 22:57:16 -04001813 Py_DECREF(iterable);
1814
Yury Selivanovc724bae2016-03-02 11:30:46 -05001815 if (iter != NULL && PyCoro_CheckExact(iter)) {
1816 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
1817 if (yf != NULL) {
1818 /* `iter` is a coroutine object that is being
1819 awaited, `yf` is a pointer to the current awaitable
1820 being awaited on. */
1821 Py_DECREF(yf);
1822 Py_CLEAR(iter);
1823 PyErr_SetString(
1824 PyExc_RuntimeError,
1825 "coroutine is being awaited already");
1826 /* The code below jumps to `error` if `iter` is NULL. */
1827 }
1828 }
1829
Yury Selivanov75445082015-05-11 22:57:16 -04001830 SET_TOP(iter); /* Even if it's NULL */
1831
1832 if (iter == NULL) {
1833 goto error;
1834 }
1835
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001836 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001837 DISPATCH();
1838 }
1839
Benjamin Petersonddd19492018-09-16 22:38:02 -07001840 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001841 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001842 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001843 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001844 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
1845 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001846 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001847 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001848 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001849 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001850 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001851 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001852 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001853 Py_DECREF(v);
1854 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001855 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08001856 if (tstate->c_tracefunc != NULL
1857 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001858 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10001859 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001860 if (err < 0)
1861 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001862 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001863 SET_TOP(val);
1864 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001865 }
Martin Panter95f53c12016-07-18 08:23:26 +00001866 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001867 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001868 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01001869 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03001870 f->f_lasti -= sizeof(_Py_CODEUNIT);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001871 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001872 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001873
Benjamin Petersonddd19492018-09-16 22:38:02 -07001874 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001875 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07001876
1877 if (co->co_flags & CO_ASYNC_GENERATOR) {
1878 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
1879 Py_DECREF(retval);
1880 if (w == NULL) {
1881 retval = NULL;
1882 goto error;
1883 }
1884 retval = w;
1885 }
1886
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001887 f->f_stacktop = stack_pointer;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001888 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001889 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001890
Benjamin Petersonddd19492018-09-16 22:38:02 -07001891 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001892 PyObject *type, *value, *traceback;
1893 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001894 PyTryBlock *b = PyFrame_BlockPop(f);
1895 if (b->b_type != EXCEPT_HANDLER) {
1896 PyErr_SetString(PyExc_SystemError,
1897 "popped block is not an except handler");
1898 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001899 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001900 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
1901 STACK_LEVEL() <= (b)->b_level + 4);
1902 exc_info = tstate->exc_info;
1903 type = exc_info->exc_type;
1904 value = exc_info->exc_value;
1905 traceback = exc_info->exc_traceback;
1906 exc_info->exc_type = POP();
1907 exc_info->exc_value = POP();
1908 exc_info->exc_traceback = POP();
1909 Py_XDECREF(type);
1910 Py_XDECREF(value);
1911 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001912 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001913 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001914
Benjamin Petersonddd19492018-09-16 22:38:02 -07001915 case TARGET(POP_BLOCK): {
1916 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001917 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001918 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001919 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001920
Benjamin Petersonddd19492018-09-16 22:38:02 -07001921 case TARGET(POP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001922 /* If oparg is 0 at the top of the stack are 1 or 6 values:
1923 Either:
1924 - TOP = NULL or an integer
1925 or:
1926 - (TOP, SECOND, THIRD) = exc_info()
1927 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
1928
1929 If oparg is 1 the value for 'return' was additionally pushed
1930 at the top of the stack.
1931 */
1932 PyObject *res = NULL;
1933 if (oparg) {
1934 res = POP();
1935 }
1936 PyObject *exc = POP();
1937 if (exc == NULL || PyLong_CheckExact(exc)) {
1938 Py_XDECREF(exc);
1939 }
1940 else {
1941 Py_DECREF(exc);
1942 Py_DECREF(POP());
1943 Py_DECREF(POP());
1944
1945 PyObject *type, *value, *traceback;
1946 _PyErr_StackItem *exc_info;
1947 PyTryBlock *b = PyFrame_BlockPop(f);
1948 if (b->b_type != EXCEPT_HANDLER) {
1949 PyErr_SetString(PyExc_SystemError,
1950 "popped block is not an except handler");
1951 Py_XDECREF(res);
1952 goto error;
1953 }
1954 assert(STACK_LEVEL() == (b)->b_level + 3);
1955 exc_info = tstate->exc_info;
1956 type = exc_info->exc_type;
1957 value = exc_info->exc_value;
1958 traceback = exc_info->exc_traceback;
1959 exc_info->exc_type = POP();
1960 exc_info->exc_value = POP();
1961 exc_info->exc_traceback = POP();
1962 Py_XDECREF(type);
1963 Py_XDECREF(value);
1964 Py_XDECREF(traceback);
1965 }
1966 if (oparg) {
1967 PUSH(res);
1968 }
1969 DISPATCH();
1970 }
1971
Benjamin Petersonddd19492018-09-16 22:38:02 -07001972 case TARGET(CALL_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001973 PyObject *ret = PyLong_FromLong(INSTR_OFFSET());
1974 if (ret == NULL) {
1975 goto error;
1976 }
1977 PUSH(ret);
1978 JUMPBY(oparg);
1979 FAST_DISPATCH();
1980 }
1981
Benjamin Petersonddd19492018-09-16 22:38:02 -07001982 case TARGET(BEGIN_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001983 /* Push NULL onto the stack for using it in END_FINALLY,
1984 POP_FINALLY, WITH_CLEANUP_START and WITH_CLEANUP_FINISH.
1985 */
1986 PUSH(NULL);
1987 FAST_DISPATCH();
1988 }
1989
Benjamin Petersonddd19492018-09-16 22:38:02 -07001990 case TARGET(END_FINALLY): {
1991 PREDICTED(END_FINALLY);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001992 /* At the top of the stack are 1 or 6 values:
1993 Either:
1994 - TOP = NULL or an integer
1995 or:
1996 - (TOP, SECOND, THIRD) = exc_info()
1997 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
1998 */
1999 PyObject *exc = POP();
2000 if (exc == NULL) {
2001 FAST_DISPATCH();
2002 }
2003 else if (PyLong_CheckExact(exc)) {
2004 int ret = _PyLong_AsInt(exc);
2005 Py_DECREF(exc);
2006 if (ret == -1 && PyErr_Occurred()) {
2007 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002008 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002009 JUMPTO(ret);
2010 FAST_DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002011 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002012 else {
2013 assert(PyExceptionClass_Check(exc));
2014 PyObject *val = POP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002015 PyObject *tb = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002016 PyErr_Restore(exc, val, tb);
2017 goto exception_unwind;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002018 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002019 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002020
Benjamin Petersonddd19492018-09-16 22:38:02 -07002021 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002022 PyObject *exc = POP();
2023 assert(PyExceptionClass_Check(exc));
2024 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2025 PyTryBlock *b = PyFrame_BlockPop(f);
2026 assert(b->b_type == EXCEPT_HANDLER);
2027 Py_DECREF(exc);
2028 UNWIND_EXCEPT_HANDLER(b);
2029 Py_DECREF(POP());
2030 JUMPBY(oparg);
2031 FAST_DISPATCH();
2032 }
2033 else {
2034 PyObject *val = POP();
2035 PyObject *tb = POP();
2036 PyErr_Restore(exc, val, tb);
2037 goto exception_unwind;
2038 }
2039 }
2040
Benjamin Petersonddd19492018-09-16 22:38:02 -07002041 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002042 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002043
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002044 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002045 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002046 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002047 if (bc == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002048 if (!PyErr_Occurred()) {
2049 PyErr_SetString(PyExc_NameError,
2050 "__build_class__ not found");
2051 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002052 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002053 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002054 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002055 }
2056 else {
2057 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2058 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002059 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002060 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2061 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002062 if (PyErr_ExceptionMatches(PyExc_KeyError))
2063 PyErr_SetString(PyExc_NameError,
2064 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002065 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002066 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002067 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002068 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002069 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002070 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002071
Benjamin Petersonddd19492018-09-16 22:38:02 -07002072 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002073 PyObject *name = GETITEM(names, oparg);
2074 PyObject *v = POP();
2075 PyObject *ns = f->f_locals;
2076 int err;
2077 if (ns == NULL) {
2078 PyErr_Format(PyExc_SystemError,
2079 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002080 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002081 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002082 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002083 if (PyDict_CheckExact(ns))
2084 err = PyDict_SetItem(ns, name, v);
2085 else
2086 err = PyObject_SetItem(ns, name, v);
2087 Py_DECREF(v);
2088 if (err != 0)
2089 goto error;
2090 DISPATCH();
2091 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002092
Benjamin Petersonddd19492018-09-16 22:38:02 -07002093 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002094 PyObject *name = GETITEM(names, oparg);
2095 PyObject *ns = f->f_locals;
2096 int err;
2097 if (ns == NULL) {
2098 PyErr_Format(PyExc_SystemError,
2099 "no locals when deleting %R", name);
2100 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002101 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002102 err = PyObject_DelItem(ns, name);
2103 if (err != 0) {
2104 format_exc_check_arg(PyExc_NameError,
2105 NAME_ERROR_MSG,
2106 name);
2107 goto error;
2108 }
2109 DISPATCH();
2110 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002111
Benjamin Petersonddd19492018-09-16 22:38:02 -07002112 case TARGET(UNPACK_SEQUENCE): {
2113 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002114 PyObject *seq = POP(), *item, **items;
2115 if (PyTuple_CheckExact(seq) &&
2116 PyTuple_GET_SIZE(seq) == oparg) {
2117 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002118 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002119 item = items[oparg];
2120 Py_INCREF(item);
2121 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002122 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002123 } else if (PyList_CheckExact(seq) &&
2124 PyList_GET_SIZE(seq) == oparg) {
2125 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002126 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002127 item = items[oparg];
2128 Py_INCREF(item);
2129 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002130 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002131 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002132 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002133 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002134 } else {
2135 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002136 Py_DECREF(seq);
2137 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002138 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002139 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002140 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002141 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002142
Benjamin Petersonddd19492018-09-16 22:38:02 -07002143 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002144 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2145 PyObject *seq = POP();
2146
2147 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2148 stack_pointer + totalargs)) {
2149 stack_pointer += totalargs;
2150 } else {
2151 Py_DECREF(seq);
2152 goto error;
2153 }
2154 Py_DECREF(seq);
2155 DISPATCH();
2156 }
2157
Benjamin Petersonddd19492018-09-16 22:38:02 -07002158 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002159 PyObject *name = GETITEM(names, oparg);
2160 PyObject *owner = TOP();
2161 PyObject *v = SECOND();
2162 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002163 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002164 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002165 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002166 Py_DECREF(owner);
2167 if (err != 0)
2168 goto error;
2169 DISPATCH();
2170 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002171
Benjamin Petersonddd19492018-09-16 22:38:02 -07002172 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002173 PyObject *name = GETITEM(names, oparg);
2174 PyObject *owner = POP();
2175 int err;
2176 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2177 Py_DECREF(owner);
2178 if (err != 0)
2179 goto error;
2180 DISPATCH();
2181 }
2182
Benjamin Petersonddd19492018-09-16 22:38:02 -07002183 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002184 PyObject *name = GETITEM(names, oparg);
2185 PyObject *v = POP();
2186 int err;
2187 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002188 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002189 if (err != 0)
2190 goto error;
2191 DISPATCH();
2192 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002193
Benjamin Petersonddd19492018-09-16 22:38:02 -07002194 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002195 PyObject *name = GETITEM(names, oparg);
2196 int err;
2197 err = PyDict_DelItem(f->f_globals, name);
2198 if (err != 0) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002199 if (PyErr_ExceptionMatches(PyExc_KeyError)) {
2200 format_exc_check_arg(
2201 PyExc_NameError, NAME_ERROR_MSG, name);
2202 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002203 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002204 }
2205 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002206 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002207
Benjamin Petersonddd19492018-09-16 22:38:02 -07002208 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002209 PyObject *name = GETITEM(names, oparg);
2210 PyObject *locals = f->f_locals;
2211 PyObject *v;
2212 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002213 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002214 "no locals when loading %R", name);
2215 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002216 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002217 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002218 v = PyDict_GetItemWithError(locals, name);
2219 if (v != NULL) {
2220 Py_INCREF(v);
2221 }
2222 else if (PyErr_Occurred()) {
2223 goto error;
2224 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002225 }
2226 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002227 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002228 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002229 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2230 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002231 PyErr_Clear();
2232 }
2233 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002234 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002235 v = PyDict_GetItemWithError(f->f_globals, name);
2236 if (v != NULL) {
2237 Py_INCREF(v);
2238 }
2239 else if (PyErr_Occurred()) {
2240 goto error;
2241 }
2242 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002243 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002244 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002245 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002246 if (!PyErr_Occurred()) {
2247 format_exc_check_arg(
Victor Stinnerb0b22422012-04-19 00:57:45 +02002248 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002249 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002250 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002251 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002252 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002253 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002254 }
2255 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002256 v = PyObject_GetItem(f->f_builtins, name);
2257 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002258 if (PyErr_ExceptionMatches(PyExc_KeyError))
2259 format_exc_check_arg(
2260 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002261 NAME_ERROR_MSG, name);
2262 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002263 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002264 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002265 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002266 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002267 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002268 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002269 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002270
Benjamin Petersonddd19492018-09-16 22:38:02 -07002271 case TARGET(LOAD_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002272 PyObject *name = GETITEM(names, oparg);
2273 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002274 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002275 && PyDict_CheckExact(f->f_builtins))
2276 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002277 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002278 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002279 name);
2280 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002281 if (!_PyErr_OCCURRED()) {
2282 /* _PyDict_LoadGlobal() returns NULL without raising
2283 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002284 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002285 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002286 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002287 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002288 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002289 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002290 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002291 else {
2292 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002293
2294 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002295 v = PyObject_GetItem(f->f_globals, name);
2296 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002297 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2298 goto error;
2299 PyErr_Clear();
2300
Victor Stinnerb4efc962015-11-20 09:24:02 +01002301 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002302 v = PyObject_GetItem(f->f_builtins, name);
2303 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002304 if (PyErr_ExceptionMatches(PyExc_KeyError))
2305 format_exc_check_arg(
2306 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002307 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002308 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002309 }
2310 }
2311 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002312 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002313 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002314 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002315
Benjamin Petersonddd19492018-09-16 22:38:02 -07002316 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002317 PyObject *v = GETLOCAL(oparg);
2318 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002319 SETLOCAL(oparg, NULL);
2320 DISPATCH();
2321 }
2322 format_exc_check_arg(
2323 PyExc_UnboundLocalError,
2324 UNBOUNDLOCAL_ERROR_MSG,
2325 PyTuple_GetItem(co->co_varnames, oparg)
2326 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002327 goto error;
2328 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002329
Benjamin Petersonddd19492018-09-16 22:38:02 -07002330 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002331 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002332 PyObject *oldobj = PyCell_GET(cell);
2333 if (oldobj != NULL) {
2334 PyCell_SET(cell, NULL);
2335 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002336 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002337 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002338 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002339 goto error;
2340 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002341
Benjamin Petersonddd19492018-09-16 22:38:02 -07002342 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002343 PyObject *cell = freevars[oparg];
2344 Py_INCREF(cell);
2345 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002346 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002347 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002348
Benjamin Petersonddd19492018-09-16 22:38:02 -07002349 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002350 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002351 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002352 assert(locals);
2353 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2354 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2355 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2356 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2357 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002358 value = PyDict_GetItemWithError(locals, name);
2359 if (value != NULL) {
2360 Py_INCREF(value);
2361 }
2362 else if (PyErr_Occurred()) {
2363 goto error;
2364 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002365 }
2366 else {
2367 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002368 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002369 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2370 goto error;
2371 PyErr_Clear();
2372 }
2373 }
2374 if (!value) {
2375 PyObject *cell = freevars[oparg];
2376 value = PyCell_GET(cell);
2377 if (value == NULL) {
2378 format_exc_unbound(co, oparg);
2379 goto error;
2380 }
2381 Py_INCREF(value);
2382 }
2383 PUSH(value);
2384 DISPATCH();
2385 }
2386
Benjamin Petersonddd19492018-09-16 22:38:02 -07002387 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002388 PyObject *cell = freevars[oparg];
2389 PyObject *value = PyCell_GET(cell);
2390 if (value == NULL) {
2391 format_exc_unbound(co, oparg);
2392 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002393 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002394 Py_INCREF(value);
2395 PUSH(value);
2396 DISPATCH();
2397 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002398
Benjamin Petersonddd19492018-09-16 22:38:02 -07002399 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002400 PyObject *v = POP();
2401 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002402 PyObject *oldobj = PyCell_GET(cell);
2403 PyCell_SET(cell, v);
2404 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002405 DISPATCH();
2406 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002407
Benjamin Petersonddd19492018-09-16 22:38:02 -07002408 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002409 PyObject *str;
2410 PyObject *empty = PyUnicode_New(0, 0);
2411 if (empty == NULL) {
2412 goto error;
2413 }
2414 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2415 Py_DECREF(empty);
2416 if (str == NULL)
2417 goto error;
2418 while (--oparg >= 0) {
2419 PyObject *item = POP();
2420 Py_DECREF(item);
2421 }
2422 PUSH(str);
2423 DISPATCH();
2424 }
2425
Benjamin Petersonddd19492018-09-16 22:38:02 -07002426 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002427 PyObject *tup = PyTuple_New(oparg);
2428 if (tup == NULL)
2429 goto error;
2430 while (--oparg >= 0) {
2431 PyObject *item = POP();
2432 PyTuple_SET_ITEM(tup, oparg, item);
2433 }
2434 PUSH(tup);
2435 DISPATCH();
2436 }
2437
Benjamin Petersonddd19492018-09-16 22:38:02 -07002438 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002439 PyObject *list = PyList_New(oparg);
2440 if (list == NULL)
2441 goto error;
2442 while (--oparg >= 0) {
2443 PyObject *item = POP();
2444 PyList_SET_ITEM(list, oparg, item);
2445 }
2446 PUSH(list);
2447 DISPATCH();
2448 }
2449
Benjamin Petersonddd19492018-09-16 22:38:02 -07002450 case TARGET(BUILD_TUPLE_UNPACK_WITH_CALL):
2451 case TARGET(BUILD_TUPLE_UNPACK):
2452 case TARGET(BUILD_LIST_UNPACK): {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002453 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002454 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002455 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002456 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002457
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002458 if (sum == NULL)
2459 goto error;
2460
2461 for (i = oparg; i > 0; i--) {
2462 PyObject *none_val;
2463
2464 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2465 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002466 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002467 PyErr_ExceptionMatches(PyExc_TypeError))
2468 {
2469 check_args_iterable(PEEK(1 + oparg), PEEK(i));
Serhiy Storchaka73442852016-10-02 10:33:46 +03002470 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002471 Py_DECREF(sum);
2472 goto error;
2473 }
2474 Py_DECREF(none_val);
2475 }
2476
2477 if (convert_to_tuple) {
2478 return_value = PyList_AsTuple(sum);
2479 Py_DECREF(sum);
2480 if (return_value == NULL)
2481 goto error;
2482 }
2483 else {
2484 return_value = sum;
2485 }
2486
2487 while (oparg--)
2488 Py_DECREF(POP());
2489 PUSH(return_value);
2490 DISPATCH();
2491 }
2492
Benjamin Petersonddd19492018-09-16 22:38:02 -07002493 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002494 PyObject *set = PySet_New(NULL);
2495 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002496 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002497 if (set == NULL)
2498 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002499 for (i = oparg; i > 0; i--) {
2500 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002501 if (err == 0)
2502 err = PySet_Add(set, item);
2503 Py_DECREF(item);
2504 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002505 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002506 if (err != 0) {
2507 Py_DECREF(set);
2508 goto error;
2509 }
2510 PUSH(set);
2511 DISPATCH();
2512 }
2513
Benjamin Petersonddd19492018-09-16 22:38:02 -07002514 case TARGET(BUILD_SET_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002515 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002516 PyObject *sum = PySet_New(NULL);
2517 if (sum == NULL)
2518 goto error;
2519
2520 for (i = oparg; i > 0; i--) {
2521 if (_PySet_Update(sum, PEEK(i)) < 0) {
2522 Py_DECREF(sum);
2523 goto error;
2524 }
2525 }
2526
2527 while (oparg--)
2528 Py_DECREF(POP());
2529 PUSH(sum);
2530 DISPATCH();
2531 }
2532
Benjamin Petersonddd19492018-09-16 22:38:02 -07002533 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002534 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002535 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2536 if (map == NULL)
2537 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002538 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002539 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002540 PyObject *key = PEEK(2*i);
2541 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002542 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002543 if (err != 0) {
2544 Py_DECREF(map);
2545 goto error;
2546 }
2547 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002548
2549 while (oparg--) {
2550 Py_DECREF(POP());
2551 Py_DECREF(POP());
2552 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002553 PUSH(map);
2554 DISPATCH();
2555 }
2556
Benjamin Petersonddd19492018-09-16 22:38:02 -07002557 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002558 _Py_IDENTIFIER(__annotations__);
2559 int err;
2560 PyObject *ann_dict;
2561 if (f->f_locals == NULL) {
2562 PyErr_Format(PyExc_SystemError,
2563 "no locals found when setting up annotations");
2564 goto error;
2565 }
2566 /* check if __annotations__ in locals()... */
2567 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002568 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002569 &PyId___annotations__);
2570 if (ann_dict == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002571 if (PyErr_Occurred()) {
2572 goto error;
2573 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002574 /* ...if not, create a new one */
2575 ann_dict = PyDict_New();
2576 if (ann_dict == NULL) {
2577 goto error;
2578 }
2579 err = _PyDict_SetItemId(f->f_locals,
2580 &PyId___annotations__, ann_dict);
2581 Py_DECREF(ann_dict);
2582 if (err != 0) {
2583 goto error;
2584 }
2585 }
2586 }
2587 else {
2588 /* do the same if locals() is not a dict */
2589 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2590 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002591 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002592 }
2593 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2594 if (ann_dict == NULL) {
2595 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2596 goto error;
2597 }
2598 PyErr_Clear();
2599 ann_dict = PyDict_New();
2600 if (ann_dict == NULL) {
2601 goto error;
2602 }
2603 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2604 Py_DECREF(ann_dict);
2605 if (err != 0) {
2606 goto error;
2607 }
2608 }
2609 else {
2610 Py_DECREF(ann_dict);
2611 }
2612 }
2613 DISPATCH();
2614 }
2615
Benjamin Petersonddd19492018-09-16 22:38:02 -07002616 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002617 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002618 PyObject *map;
2619 PyObject *keys = TOP();
2620 if (!PyTuple_CheckExact(keys) ||
2621 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2622 PyErr_SetString(PyExc_SystemError,
2623 "bad BUILD_CONST_KEY_MAP keys argument");
2624 goto error;
2625 }
2626 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2627 if (map == NULL) {
2628 goto error;
2629 }
2630 for (i = oparg; i > 0; i--) {
2631 int err;
2632 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2633 PyObject *value = PEEK(i + 1);
2634 err = PyDict_SetItem(map, key, value);
2635 if (err != 0) {
2636 Py_DECREF(map);
2637 goto error;
2638 }
2639 }
2640
2641 Py_DECREF(POP());
2642 while (oparg--) {
2643 Py_DECREF(POP());
2644 }
2645 PUSH(map);
2646 DISPATCH();
2647 }
2648
Benjamin Petersonddd19492018-09-16 22:38:02 -07002649 case TARGET(BUILD_MAP_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002650 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002651 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002652 if (sum == NULL)
2653 goto error;
2654
2655 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002656 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002657 if (PyDict_Update(sum, arg) < 0) {
2658 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2659 PyErr_Format(PyExc_TypeError,
Berker Peksag8e9045d2016-10-02 13:08:25 +03002660 "'%.200s' object is not a mapping",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002661 arg->ob_type->tp_name);
2662 }
2663 Py_DECREF(sum);
2664 goto error;
2665 }
2666 }
2667
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002668 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002669 Py_DECREF(POP());
2670 PUSH(sum);
2671 DISPATCH();
2672 }
2673
Benjamin Petersonddd19492018-09-16 22:38:02 -07002674 case TARGET(BUILD_MAP_UNPACK_WITH_CALL): {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002675 Py_ssize_t i;
2676 PyObject *sum = PyDict_New();
2677 if (sum == NULL)
2678 goto error;
2679
2680 for (i = oparg; i > 0; i--) {
2681 PyObject *arg = PEEK(i);
2682 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002683 Py_DECREF(sum);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02002684 format_kwargs_error(PEEK(2 + oparg), arg);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002685 goto error;
2686 }
2687 }
2688
2689 while (oparg--)
2690 Py_DECREF(POP());
2691 PUSH(sum);
2692 DISPATCH();
2693 }
2694
Benjamin Petersonddd19492018-09-16 22:38:02 -07002695 case TARGET(MAP_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002696 PyObject *key = TOP();
2697 PyObject *value = SECOND();
2698 PyObject *map;
2699 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002700 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002701 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002702 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002703 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002704 Py_DECREF(value);
2705 Py_DECREF(key);
2706 if (err != 0)
2707 goto error;
2708 PREDICT(JUMP_ABSOLUTE);
2709 DISPATCH();
2710 }
2711
Benjamin Petersonddd19492018-09-16 22:38:02 -07002712 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002713 PyObject *name = GETITEM(names, oparg);
2714 PyObject *owner = TOP();
2715 PyObject *res = PyObject_GetAttr(owner, name);
2716 Py_DECREF(owner);
2717 SET_TOP(res);
2718 if (res == NULL)
2719 goto error;
2720 DISPATCH();
2721 }
2722
Benjamin Petersonddd19492018-09-16 22:38:02 -07002723 case TARGET(COMPARE_OP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002724 PyObject *right = POP();
2725 PyObject *left = TOP();
2726 PyObject *res = cmp_outcome(oparg, left, right);
2727 Py_DECREF(left);
2728 Py_DECREF(right);
2729 SET_TOP(res);
2730 if (res == NULL)
2731 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002732 PREDICT(POP_JUMP_IF_FALSE);
2733 PREDICT(POP_JUMP_IF_TRUE);
2734 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002735 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002736
Benjamin Petersonddd19492018-09-16 22:38:02 -07002737 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002738 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002739 PyObject *fromlist = POP();
2740 PyObject *level = TOP();
2741 PyObject *res;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002742 res = import_name(f, name, fromlist, level);
2743 Py_DECREF(level);
2744 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002745 SET_TOP(res);
2746 if (res == NULL)
2747 goto error;
2748 DISPATCH();
2749 }
2750
Benjamin Petersonddd19492018-09-16 22:38:02 -07002751 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002752 PyObject *from = POP(), *locals;
2753 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002754 if (PyFrame_FastToLocalsWithError(f) < 0) {
2755 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002756 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002757 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002758
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002759 locals = f->f_locals;
2760 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002761 PyErr_SetString(PyExc_SystemError,
2762 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002763 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002764 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002765 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002766 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002767 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002768 Py_DECREF(from);
2769 if (err != 0)
2770 goto error;
2771 DISPATCH();
2772 }
Guido van Rossum25831651993-05-19 14:50:45 +00002773
Benjamin Petersonddd19492018-09-16 22:38:02 -07002774 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002775 PyObject *name = GETITEM(names, oparg);
2776 PyObject *from = TOP();
2777 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002778 res = import_from(from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002779 PUSH(res);
2780 if (res == NULL)
2781 goto error;
2782 DISPATCH();
2783 }
Thomas Wouters52152252000-08-17 22:55:00 +00002784
Benjamin Petersonddd19492018-09-16 22:38:02 -07002785 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002786 JUMPBY(oparg);
2787 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002788 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002789
Benjamin Petersonddd19492018-09-16 22:38:02 -07002790 case TARGET(POP_JUMP_IF_FALSE): {
2791 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002792 PyObject *cond = POP();
2793 int err;
2794 if (cond == Py_True) {
2795 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002796 FAST_DISPATCH();
2797 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002798 if (cond == Py_False) {
2799 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002800 JUMPTO(oparg);
2801 FAST_DISPATCH();
2802 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002803 err = PyObject_IsTrue(cond);
2804 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002805 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07002806 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002807 else if (err == 0)
2808 JUMPTO(oparg);
2809 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002810 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002811 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002812 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002813
Benjamin Petersonddd19492018-09-16 22:38:02 -07002814 case TARGET(POP_JUMP_IF_TRUE): {
2815 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002816 PyObject *cond = POP();
2817 int err;
2818 if (cond == Py_False) {
2819 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002820 FAST_DISPATCH();
2821 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002822 if (cond == Py_True) {
2823 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002824 JUMPTO(oparg);
2825 FAST_DISPATCH();
2826 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002827 err = PyObject_IsTrue(cond);
2828 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002829 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002830 JUMPTO(oparg);
2831 }
2832 else if (err == 0)
2833 ;
2834 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002835 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002836 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002837 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002838
Benjamin Petersonddd19492018-09-16 22:38:02 -07002839 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002840 PyObject *cond = TOP();
2841 int err;
2842 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002843 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002844 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002845 FAST_DISPATCH();
2846 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002847 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002848 JUMPTO(oparg);
2849 FAST_DISPATCH();
2850 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002851 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002852 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002853 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002854 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002855 }
2856 else if (err == 0)
2857 JUMPTO(oparg);
2858 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002859 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002860 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002861 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002862
Benjamin Petersonddd19492018-09-16 22:38:02 -07002863 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002864 PyObject *cond = TOP();
2865 int err;
2866 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002867 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002868 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002869 FAST_DISPATCH();
2870 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002871 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002872 JUMPTO(oparg);
2873 FAST_DISPATCH();
2874 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002875 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002876 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002877 JUMPTO(oparg);
2878 }
2879 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002880 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002881 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002882 }
2883 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002884 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002885 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002886 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002887
Benjamin Petersonddd19492018-09-16 22:38:02 -07002888 case TARGET(JUMP_ABSOLUTE): {
2889 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002890 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002891#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002892 /* Enabling this path speeds-up all while and for-loops by bypassing
2893 the per-loop checks for signals. By default, this should be turned-off
2894 because it prevents detection of a control-break in tight loops like
2895 "while 1: pass". Compile with this option turned-on when you need
2896 the speed-up and do not need break checking inside tight loops (ones
2897 that contain only instructions ending with FAST_DISPATCH).
2898 */
2899 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002900#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002902#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002903 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002904
Benjamin Petersonddd19492018-09-16 22:38:02 -07002905 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002906 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002907 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002908 PyObject *iter = PyObject_GetIter(iterable);
2909 Py_DECREF(iterable);
2910 SET_TOP(iter);
2911 if (iter == NULL)
2912 goto error;
2913 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002914 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04002915 DISPATCH();
2916 }
2917
Benjamin Petersonddd19492018-09-16 22:38:02 -07002918 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04002919 /* before: [obj]; after [getiter(obj)] */
2920 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04002921 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04002922 if (PyCoro_CheckExact(iterable)) {
2923 /* `iterable` is a coroutine */
2924 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
2925 /* and it is used in a 'yield from' expression of a
2926 regular generator. */
2927 Py_DECREF(iterable);
2928 SET_TOP(NULL);
2929 PyErr_SetString(PyExc_TypeError,
2930 "cannot 'yield from' a coroutine object "
2931 "in a non-coroutine generator");
2932 goto error;
2933 }
2934 }
2935 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04002936 /* `iterable` is not a generator. */
2937 iter = PyObject_GetIter(iterable);
2938 Py_DECREF(iterable);
2939 SET_TOP(iter);
2940 if (iter == NULL)
2941 goto error;
2942 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002943 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002944 DISPATCH();
2945 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002946
Benjamin Petersonddd19492018-09-16 22:38:02 -07002947 case TARGET(FOR_ITER): {
2948 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002949 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002950 PyObject *iter = TOP();
2951 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
2952 if (next != NULL) {
2953 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002954 PREDICT(STORE_FAST);
2955 PREDICT(UNPACK_SEQUENCE);
2956 DISPATCH();
2957 }
2958 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002959 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2960 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08002961 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002962 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002963 PyErr_Clear();
2964 }
2965 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00002966 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002967 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002968 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002969 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002970 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002971 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002972
Benjamin Petersonddd19492018-09-16 22:38:02 -07002973 case TARGET(SETUP_FINALLY): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002974 /* NOTE: If you add any new block-setup opcodes that
2975 are not try/except/finally handlers, you may need
2976 to update the PyGen_NeedsFinalizing() function.
2977 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002978
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002979 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002980 STACK_LEVEL());
2981 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002982 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002983
Benjamin Petersonddd19492018-09-16 22:38:02 -07002984 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04002985 _Py_IDENTIFIER(__aexit__);
2986 _Py_IDENTIFIER(__aenter__);
2987
2988 PyObject *mgr = TOP();
2989 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
2990 *enter;
2991 PyObject *res;
2992 if (exit == NULL)
2993 goto error;
2994 SET_TOP(exit);
2995 enter = special_lookup(mgr, &PyId___aenter__);
2996 Py_DECREF(mgr);
2997 if (enter == NULL)
2998 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01002999 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003000 Py_DECREF(enter);
3001 if (res == NULL)
3002 goto error;
3003 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003004 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003005 DISPATCH();
3006 }
3007
Benjamin Petersonddd19492018-09-16 22:38:02 -07003008 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003009 PyObject *res = POP();
3010 /* Setup the finally block before pushing the result
3011 of __aenter__ on the stack. */
3012 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3013 STACK_LEVEL());
3014 PUSH(res);
3015 DISPATCH();
3016 }
3017
Benjamin Petersonddd19492018-09-16 22:38:02 -07003018 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003019 _Py_IDENTIFIER(__exit__);
3020 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003021 PyObject *mgr = TOP();
Raymond Hettingera3fec152016-11-21 17:24:23 -08003022 PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003023 PyObject *res;
Raymond Hettingera3fec152016-11-21 17:24:23 -08003024 if (enter == NULL)
3025 goto error;
3026 exit = special_lookup(mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003027 if (exit == NULL) {
3028 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003029 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003030 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003031 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003032 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003033 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003034 Py_DECREF(enter);
3035 if (res == NULL)
3036 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003037 /* Setup the finally block before pushing the result
3038 of __enter__ on the stack. */
3039 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3040 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003041
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003042 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003043 DISPATCH();
3044 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003045
Benjamin Petersonddd19492018-09-16 22:38:02 -07003046 case TARGET(WITH_CLEANUP_START): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003047 /* At the top of the stack are 1 or 6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003048 how/why we entered the finally clause:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003049 - TOP = NULL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003050 - (TOP, SECOND, THIRD) = exc_info()
3051 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003052 Below them is EXIT, the context.__exit__ or context.__aexit__
3053 bound method.
3054 In the first case, we must call
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003055 EXIT(None, None, None)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003056 otherwise we must call
3057 EXIT(TOP, SECOND, THIRD)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003058
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003059 In the first case, we remove EXIT from the
3060 stack, leaving TOP, and push TOP on the stack.
3061 Otherwise we shift the bottom 3 values of the
3062 stack down, replace the empty spot with NULL, and push
3063 None on the stack.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003064
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003065 Finally we push the result of the call.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003066 */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003067 PyObject *stack[3];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003068 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003069 PyObject *exc, *val, *tb, *res;
3070
3071 val = tb = Py_None;
3072 exc = TOP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003073 if (exc == NULL) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003074 STACK_SHRINK(1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003075 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003076 SET_TOP(exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003077 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003078 }
3079 else {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003080 assert(PyExceptionClass_Check(exc));
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003081 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003082 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003083 val = SECOND();
3084 tb = THIRD();
3085 tp2 = FOURTH();
3086 exc2 = PEEK(5);
3087 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003088 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003089 SET_VALUE(7, tb2);
3090 SET_VALUE(6, exc2);
3091 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003092 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3093 SET_FOURTH(NULL);
3094 /* We just shifted the stack down, so we have
3095 to tell the except handler block that the
3096 values are lower than it expects. */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003097 assert(f->f_iblock > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003098 block = &f->f_blockstack[f->f_iblock - 1];
3099 assert(block->b_type == EXCEPT_HANDLER);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003100 assert(block->b_level > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003101 block->b_level--;
3102 }
Victor Stinner842cfff2016-12-01 14:45:31 +01003103
3104 stack[0] = exc;
3105 stack[1] = val;
3106 stack[2] = tb;
3107 res = _PyObject_FastCall(exit_func, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003108 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003109 if (res == NULL)
3110 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003111
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003112 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003113 PUSH(exc);
3114 PUSH(res);
3115 PREDICT(WITH_CLEANUP_FINISH);
3116 DISPATCH();
3117 }
3118
Benjamin Petersonddd19492018-09-16 22:38:02 -07003119 case TARGET(WITH_CLEANUP_FINISH): {
3120 PREDICTED(WITH_CLEANUP_FINISH);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003121 /* TOP = the result of calling the context.__exit__ bound method
3122 SECOND = either None or exception type
3123
3124 If SECOND is None below is NULL or the return address,
3125 otherwise below are 7 values representing an exception.
3126 */
Yury Selivanov75445082015-05-11 22:57:16 -04003127 PyObject *res = POP();
3128 PyObject *exc = POP();
3129 int err;
3130
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003131 if (exc != Py_None)
3132 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003133 else
3134 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003135
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003136 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003137 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003139 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003140 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003141 else if (err > 0) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003142 /* There was an exception and a True return.
3143 * We must manually unwind the EXCEPT_HANDLER block
3144 * which was created when the exception was caught,
Quan Tian3bd0d622018-10-20 05:30:03 +08003145 * otherwise the stack will be in an inconsistent state.
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003146 */
3147 PyTryBlock *b = PyFrame_BlockPop(f);
3148 assert(b->b_type == EXCEPT_HANDLER);
3149 UNWIND_EXCEPT_HANDLER(b);
3150 PUSH(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003151 }
3152 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003153 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003154 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003155
Benjamin Petersonddd19492018-09-16 22:38:02 -07003156 case TARGET(LOAD_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003157 /* Designed to work in tamdem with CALL_METHOD. */
3158 PyObject *name = GETITEM(names, oparg);
3159 PyObject *obj = TOP();
3160 PyObject *meth = NULL;
3161
3162 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3163
Yury Selivanovf2392132016-12-13 19:03:51 -05003164 if (meth == NULL) {
3165 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003166 goto error;
3167 }
3168
3169 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003170 /* We can bypass temporary bound method object.
3171 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003172
INADA Naoki015bce62017-01-16 17:23:30 +09003173 meth | self | arg1 | ... | argN
3174 */
3175 SET_TOP(meth);
3176 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003177 }
3178 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003179 /* meth is not an unbound method (but a regular attr, or
3180 something was returned by a descriptor protocol). Set
3181 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003182 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003183
3184 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003185 */
INADA Naoki015bce62017-01-16 17:23:30 +09003186 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003187 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003188 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003189 }
3190 DISPATCH();
3191 }
3192
Benjamin Petersonddd19492018-09-16 22:38:02 -07003193 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003194 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003195 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003196
3197 sp = stack_pointer;
3198
INADA Naoki015bce62017-01-16 17:23:30 +09003199 meth = PEEK(oparg + 2);
3200 if (meth == NULL) {
3201 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3202 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003203
3204 Stack layout:
3205
INADA Naoki015bce62017-01-16 17:23:30 +09003206 ... | NULL | callable | arg1 | ... | argN
3207 ^- TOP()
3208 ^- (-oparg)
3209 ^- (-oparg-1)
3210 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003211
Ville Skyttä49b27342017-08-03 09:00:59 +03003212 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003213 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003214 */
Yury Selivanovf2392132016-12-13 19:03:51 -05003215 res = call_function(&sp, oparg, NULL);
3216 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003217 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003218 }
3219 else {
3220 /* This is a method call. Stack layout:
3221
INADA Naoki015bce62017-01-16 17:23:30 +09003222 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003223 ^- TOP()
3224 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003225 ^- (-oparg-1)
3226 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003227
INADA Naoki015bce62017-01-16 17:23:30 +09003228 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003229 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003230 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003231 */
3232 res = call_function(&sp, oparg + 1, NULL);
3233 stack_pointer = sp;
3234 }
3235
3236 PUSH(res);
3237 if (res == NULL)
3238 goto error;
3239 DISPATCH();
3240 }
3241
Benjamin Petersonddd19492018-09-16 22:38:02 -07003242 case TARGET(CALL_FUNCTION): {
3243 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003244 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003245 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003246 res = call_function(&sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003247 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003248 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003249 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003250 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003251 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003252 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003253 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003254
Benjamin Petersonddd19492018-09-16 22:38:02 -07003255 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003256 PyObject **sp, *res, *names;
3257
3258 names = POP();
3259 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003260 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003261 res = call_function(&sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003262 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003263 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003264 Py_DECREF(names);
3265
3266 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003267 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003268 }
3269 DISPATCH();
3270 }
3271
Benjamin Petersonddd19492018-09-16 22:38:02 -07003272 case TARGET(CALL_FUNCTION_EX): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003273 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003274 if (oparg & 0x01) {
3275 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003276 if (!PyDict_CheckExact(kwargs)) {
3277 PyObject *d = PyDict_New();
3278 if (d == NULL)
3279 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003280 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003281 Py_DECREF(d);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003282 format_kwargs_error(SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003283 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003284 goto error;
3285 }
3286 Py_DECREF(kwargs);
3287 kwargs = d;
3288 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003289 assert(PyDict_CheckExact(kwargs));
3290 }
3291 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003292 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003293 if (!PyTuple_CheckExact(callargs)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003294 if (check_args_iterable(func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003295 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003296 goto error;
3297 }
3298 Py_SETREF(callargs, PySequence_Tuple(callargs));
3299 if (callargs == NULL) {
3300 goto error;
3301 }
3302 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003303 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003304
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003305 result = do_call_core(func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003306 Py_DECREF(func);
3307 Py_DECREF(callargs);
3308 Py_XDECREF(kwargs);
3309
3310 SET_TOP(result);
3311 if (result == NULL) {
3312 goto error;
3313 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003314 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003315 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003316
Benjamin Petersonddd19492018-09-16 22:38:02 -07003317 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003318 PyObject *qualname = POP();
3319 PyObject *codeobj = POP();
3320 PyFunctionObject *func = (PyFunctionObject *)
3321 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003322
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003323 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003324 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003325 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003326 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003327 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003328
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003329 if (oparg & 0x08) {
3330 assert(PyTuple_CheckExact(TOP()));
3331 func ->func_closure = POP();
3332 }
3333 if (oparg & 0x04) {
3334 assert(PyDict_CheckExact(TOP()));
3335 func->func_annotations = POP();
3336 }
3337 if (oparg & 0x02) {
3338 assert(PyDict_CheckExact(TOP()));
3339 func->func_kwdefaults = POP();
3340 }
3341 if (oparg & 0x01) {
3342 assert(PyTuple_CheckExact(TOP()));
3343 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003344 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003345
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003346 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003347 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003348 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003349
Benjamin Petersonddd19492018-09-16 22:38:02 -07003350 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003351 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003352 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003353 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003354 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003355 step = NULL;
3356 stop = POP();
3357 start = TOP();
3358 slice = PySlice_New(start, stop, step);
3359 Py_DECREF(start);
3360 Py_DECREF(stop);
3361 Py_XDECREF(step);
3362 SET_TOP(slice);
3363 if (slice == NULL)
3364 goto error;
3365 DISPATCH();
3366 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003367
Benjamin Petersonddd19492018-09-16 22:38:02 -07003368 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003369 /* Handles f-string value formatting. */
3370 PyObject *result;
3371 PyObject *fmt_spec;
3372 PyObject *value;
3373 PyObject *(*conv_fn)(PyObject *);
3374 int which_conversion = oparg & FVC_MASK;
3375 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3376
3377 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003378 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003379
3380 /* See if any conversion is specified. */
3381 switch (which_conversion) {
3382 case FVC_STR: conv_fn = PyObject_Str; break;
3383 case FVC_REPR: conv_fn = PyObject_Repr; break;
3384 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3385
3386 /* Must be 0 (meaning no conversion), since only four
3387 values are allowed by (oparg & FVC_MASK). */
3388 default: conv_fn = NULL; break;
3389 }
3390
3391 /* If there's a conversion function, call it and replace
3392 value with that result. Otherwise, just use value,
3393 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003394 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003395 result = conv_fn(value);
3396 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003397 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003398 Py_XDECREF(fmt_spec);
3399 goto error;
3400 }
3401 value = result;
3402 }
3403
3404 /* If value is a unicode object, and there's no fmt_spec,
3405 then we know the result of format(value) is value
3406 itself. In that case, skip calling format(). I plan to
3407 move this optimization in to PyObject_Format()
3408 itself. */
3409 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3410 /* Do nothing, just transfer ownership to result. */
3411 result = value;
3412 } else {
3413 /* Actually call format(). */
3414 result = PyObject_Format(value, fmt_spec);
3415 Py_DECREF(value);
3416 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003417 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003418 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003419 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003420 }
3421
Eric V. Smith135d5f42016-02-05 18:23:08 -05003422 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003423 DISPATCH();
3424 }
3425
Benjamin Petersonddd19492018-09-16 22:38:02 -07003426 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003427 int oldoparg = oparg;
3428 NEXTOPARG();
3429 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003430 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003431 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003432
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003433
Antoine Pitrou042b1282010-08-13 21:15:58 +00003434#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003435 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003436#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003437 default:
3438 fprintf(stderr,
3439 "XXX lineno: %d, opcode: %d\n",
3440 PyFrame_GetLineNumber(f),
3441 opcode);
3442 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003443 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003445 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003446
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003447 /* This should never be reached. Every opcode should end with DISPATCH()
3448 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003449 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003450
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003451error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003452 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003453#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003454 if (!PyErr_Occurred())
3455 PyErr_SetString(PyExc_SystemError,
3456 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003457#else
3458 assert(PyErr_Occurred());
3459#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003460
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003461 /* Log traceback info. */
3462 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003463
Benjamin Peterson51f46162013-01-23 08:38:47 -05003464 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003465 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3466 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003467
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003468exception_unwind:
3469 /* Unwind stacks if an exception occurred */
3470 while (f->f_iblock > 0) {
3471 /* Pop the current block. */
3472 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003474 if (b->b_type == EXCEPT_HANDLER) {
3475 UNWIND_EXCEPT_HANDLER(b);
3476 continue;
3477 }
3478 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003479 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003480 PyObject *exc, *val, *tb;
3481 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003482 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003483 /* Beware, this invalidates all b->b_* fields */
3484 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003485 PUSH(exc_info->exc_traceback);
3486 PUSH(exc_info->exc_value);
3487 if (exc_info->exc_type != NULL) {
3488 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003489 }
3490 else {
3491 Py_INCREF(Py_None);
3492 PUSH(Py_None);
3493 }
3494 PyErr_Fetch(&exc, &val, &tb);
3495 /* Make the raw exception data
3496 available to the handler,
3497 so a program can emulate the
3498 Python main loop. */
3499 PyErr_NormalizeException(
3500 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003501 if (tb != NULL)
3502 PyException_SetTraceback(val, tb);
3503 else
3504 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003505 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003506 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003507 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003508 exc_info->exc_value = val;
3509 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003510 if (tb == NULL)
3511 tb = Py_None;
3512 Py_INCREF(tb);
3513 PUSH(tb);
3514 PUSH(val);
3515 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003516 JUMPTO(handler);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003517 /* Resume normal execution */
3518 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003519 }
3520 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003521
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003522 /* End the loop as we still have an error */
3523 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003524 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003526 /* Pop remaining stack entries. */
3527 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003528 PyObject *o = POP();
3529 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003530 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003531
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003532 assert(retval == NULL);
3533 assert(PyErr_Occurred());
Guido van Rossumac7be682001-01-17 15:42:30 +00003534
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003535return_or_yield:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003536 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003537 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003538 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3539 tstate, f, PyTrace_RETURN, retval)) {
3540 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003541 }
3542 }
3543 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003544 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3545 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003546 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003547 }
3548 }
3549 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003551 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003552exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003553 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3554 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003555 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003556 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003557 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003558
Victor Stinnerefde1462015-03-21 15:04:43 +01003559 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003560}
3561
Benjamin Petersonb204a422011-06-05 22:04:07 -05003562static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003563format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3564{
3565 int err;
3566 Py_ssize_t len = PyList_GET_SIZE(names);
3567 PyObject *name_str, *comma, *tail, *tmp;
3568
3569 assert(PyList_CheckExact(names));
3570 assert(len >= 1);
3571 /* Deal with the joys of natural language. */
3572 switch (len) {
3573 case 1:
3574 name_str = PyList_GET_ITEM(names, 0);
3575 Py_INCREF(name_str);
3576 break;
3577 case 2:
3578 name_str = PyUnicode_FromFormat("%U and %U",
3579 PyList_GET_ITEM(names, len - 2),
3580 PyList_GET_ITEM(names, len - 1));
3581 break;
3582 default:
3583 tail = PyUnicode_FromFormat(", %U, and %U",
3584 PyList_GET_ITEM(names, len - 2),
3585 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003586 if (tail == NULL)
3587 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003588 /* Chop off the last two objects in the list. This shouldn't actually
3589 fail, but we can't be too careful. */
3590 err = PyList_SetSlice(names, len - 2, len, NULL);
3591 if (err == -1) {
3592 Py_DECREF(tail);
3593 return;
3594 }
3595 /* Stitch everything up into a nice comma-separated list. */
3596 comma = PyUnicode_FromString(", ");
3597 if (comma == NULL) {
3598 Py_DECREF(tail);
3599 return;
3600 }
3601 tmp = PyUnicode_Join(comma, names);
3602 Py_DECREF(comma);
3603 if (tmp == NULL) {
3604 Py_DECREF(tail);
3605 return;
3606 }
3607 name_str = PyUnicode_Concat(tmp, tail);
3608 Py_DECREF(tmp);
3609 Py_DECREF(tail);
3610 break;
3611 }
3612 if (name_str == NULL)
3613 return;
3614 PyErr_Format(PyExc_TypeError,
3615 "%U() missing %i required %s argument%s: %U",
3616 co->co_name,
3617 len,
3618 kind,
3619 len == 1 ? "" : "s",
3620 name_str);
3621 Py_DECREF(name_str);
3622}
3623
3624static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003625missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003626 PyObject **fastlocals)
3627{
Victor Stinner74319ae2016-08-25 00:04:09 +02003628 Py_ssize_t i, j = 0;
3629 Py_ssize_t start, end;
3630 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003631 const char *kind = positional ? "positional" : "keyword-only";
3632 PyObject *missing_names;
3633
3634 /* Compute the names of the arguments that are missing. */
3635 missing_names = PyList_New(missing);
3636 if (missing_names == NULL)
3637 return;
3638 if (positional) {
3639 start = 0;
3640 end = co->co_argcount - defcount;
3641 }
3642 else {
3643 start = co->co_argcount;
3644 end = start + co->co_kwonlyargcount;
3645 }
3646 for (i = start; i < end; i++) {
3647 if (GETLOCAL(i) == NULL) {
3648 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3649 PyObject *name = PyObject_Repr(raw);
3650 if (name == NULL) {
3651 Py_DECREF(missing_names);
3652 return;
3653 }
3654 PyList_SET_ITEM(missing_names, j++, name);
3655 }
3656 }
3657 assert(j == missing);
3658 format_missing(kind, co, missing_names);
3659 Py_DECREF(missing_names);
3660}
3661
3662static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003663too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3664 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003665{
3666 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003667 Py_ssize_t kwonly_given = 0;
3668 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003669 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003670 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003671
Benjamin Petersone109c702011-06-24 09:37:26 -05003672 assert((co->co_flags & CO_VARARGS) == 0);
3673 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003674 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3675 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003676 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003677 }
3678 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003679 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003680 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003681 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003682 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003683 }
3684 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003685 plural = (co_argcount != 1);
3686 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003687 }
3688 if (sig == NULL)
3689 return;
3690 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003691 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3692 kwonly_sig = PyUnicode_FromFormat(format,
3693 given != 1 ? "s" : "",
3694 kwonly_given,
3695 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003696 if (kwonly_sig == NULL) {
3697 Py_DECREF(sig);
3698 return;
3699 }
3700 }
3701 else {
3702 /* This will not fail. */
3703 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003704 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003705 }
3706 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003707 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003708 co->co_name,
3709 sig,
3710 plural ? "s" : "",
3711 given,
3712 kwonly_sig,
3713 given == 1 && !kwonly_given ? "was" : "were");
3714 Py_DECREF(sig);
3715 Py_DECREF(kwonly_sig);
3716}
3717
Guido van Rossumc2e20742006-02-27 22:32:47 +00003718/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02003719 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003720 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003721
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003722PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +02003723_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003724 PyObject *const *args, Py_ssize_t argcount,
3725 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003726 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003727 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003728 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003729 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003730{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003731 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003732 PyFrameObject *f;
3733 PyObject *retval = NULL;
3734 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003735 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003736 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003737 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3738 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003739 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003740
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003741 if (globals == NULL) {
3742 PyErr_SetString(PyExc_SystemError,
3743 "PyEval_EvalCodeEx: NULL globals");
3744 return NULL;
3745 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003746
Victor Stinnerc7020012016-08-16 23:40:29 +02003747 /* Create the frame */
Victor Stinner50b48572018-11-01 01:51:40 +01003748 tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003749 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003750 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003751 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003752 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003753 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003754 fastlocals = f->f_localsplus;
3755 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003756
Victor Stinnerc7020012016-08-16 23:40:29 +02003757 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003758 if (co->co_flags & CO_VARKEYWORDS) {
3759 kwdict = PyDict_New();
3760 if (kwdict == NULL)
3761 goto fail;
3762 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003763 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003764 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003765 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003766 SETLOCAL(i, kwdict);
3767 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003768 else {
3769 kwdict = NULL;
3770 }
3771
3772 /* Copy positional arguments into local variables */
3773 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003774 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003775 }
3776 else {
3777 n = argcount;
3778 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003779 for (i = 0; i < n; i++) {
3780 x = args[i];
3781 Py_INCREF(x);
3782 SETLOCAL(i, x);
3783 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003784
3785 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003786 if (co->co_flags & CO_VARARGS) {
Sergey Fedoseev234531b2019-02-25 21:59:12 +05003787 u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003788 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003789 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003790 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003791 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003792 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003793
Serhiy Storchakab7281052016-09-12 00:52:40 +03003794 /* Handle keyword arguments passed as two strided arrays */
3795 kwcount *= kwstep;
3796 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003797 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003798 PyObject *keyword = kwnames[i];
3799 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003800 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003801
Benjamin Petersonb204a422011-06-05 22:04:07 -05003802 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3803 PyErr_Format(PyExc_TypeError,
3804 "%U() keywords must be strings",
3805 co->co_name);
3806 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003807 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003808
Benjamin Petersonb204a422011-06-05 22:04:07 -05003809 /* Speed hack: do raw pointer compares. As names are
3810 normally interned this should almost always hit. */
3811 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3812 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003813 PyObject *name = co_varnames[j];
3814 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003815 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003816 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003817 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003818
Benjamin Petersonb204a422011-06-05 22:04:07 -05003819 /* Slow fallback, just in case */
3820 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003821 PyObject *name = co_varnames[j];
3822 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
3823 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003824 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003825 }
3826 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003827 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003828 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003829 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003830
Victor Stinner231d1f32017-01-11 02:12:06 +01003831 assert(j >= total_args);
3832 if (kwdict == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003833 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003834 "%U() got an unexpected keyword argument '%S'",
3835 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003836 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003837 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003838
Christian Heimes0bd447f2013-07-20 14:48:10 +02003839 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3840 goto fail;
3841 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003842 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02003843
Benjamin Petersonb204a422011-06-05 22:04:07 -05003844 kw_found:
3845 if (GETLOCAL(j) != NULL) {
3846 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003847 "%U() got multiple values for argument '%S'",
3848 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003849 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003850 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003851 Py_INCREF(value);
3852 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003853 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003854
3855 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003856 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003857 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003858 goto fail;
3859 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003860
3861 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003862 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02003863 Py_ssize_t m = co->co_argcount - defcount;
3864 Py_ssize_t missing = 0;
3865 for (i = argcount; i < m; i++) {
3866 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003867 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02003868 }
3869 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003870 if (missing) {
3871 missing_arguments(co, missing, defcount, fastlocals);
3872 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003873 }
3874 if (n > m)
3875 i = n - m;
3876 else
3877 i = 0;
3878 for (; i < defcount; i++) {
3879 if (GETLOCAL(m+i) == NULL) {
3880 PyObject *def = defs[i];
3881 Py_INCREF(def);
3882 SETLOCAL(m+i, def);
3883 }
3884 }
3885 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003886
3887 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003888 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02003889 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003890 for (i = co->co_argcount; i < total_args; i++) {
3891 PyObject *name;
3892 if (GETLOCAL(i) != NULL)
3893 continue;
3894 name = PyTuple_GET_ITEM(co->co_varnames, i);
3895 if (kwdefs != NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003896 PyObject *def = PyDict_GetItemWithError(kwdefs, name);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003897 if (def) {
3898 Py_INCREF(def);
3899 SETLOCAL(i, def);
3900 continue;
3901 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003902 else if (PyErr_Occurred()) {
3903 goto fail;
3904 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003905 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003906 missing++;
3907 }
3908 if (missing) {
3909 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003910 goto fail;
3911 }
3912 }
3913
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003914 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003915 vars into frame. */
3916 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003917 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02003918 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05003919 /* Possibly account for the cell variable being an argument. */
3920 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07003921 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05003922 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05003923 /* Clear the local copy. */
3924 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003925 }
3926 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05003927 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003928 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05003929 if (c == NULL)
3930 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05003931 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003932 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003933
3934 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05003935 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3936 PyObject *o = PyTuple_GET_ITEM(closure, i);
3937 Py_INCREF(o);
3938 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003939 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003940
Yury Selivanoveb636452016-09-08 22:01:51 -07003941 /* Handle generator/coroutine/asynchronous generator */
3942 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003943 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04003944 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003945 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04003946
3947 if (is_coro && tstate->in_coroutine_wrapper) {
3948 assert(coro_wrapper != NULL);
3949 PyErr_Format(PyExc_RuntimeError,
3950 "coroutine wrapper %.200R attempted "
3951 "to recursively wrap %.200R",
3952 coro_wrapper,
3953 co);
3954 goto fail;
3955 }
Yury Selivanov75445082015-05-11 22:57:16 -04003956
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003957 /* Don't need to keep the reference to f_back, it will be set
3958 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003959 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003961 /* Create a new generator that owns the ready to run frame
3962 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04003963 if (is_coro) {
3964 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07003965 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
3966 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003967 } else {
3968 gen = PyGen_NewWithQualName(f, name, qualname);
3969 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003970 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003971 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003972 }
INADA Naoki9c157762016-12-26 18:52:46 +09003973
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003974 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04003975
Yury Selivanov94c22632015-06-04 10:16:51 -04003976 if (is_coro && coro_wrapper != NULL) {
3977 PyObject *wrapped;
3978 tstate->in_coroutine_wrapper = 1;
3979 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
3980 tstate->in_coroutine_wrapper = 0;
3981 return wrapped;
3982 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04003983
Yury Selivanov75445082015-05-11 22:57:16 -04003984 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003985 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003986
Victor Stinner59a73272016-12-09 18:51:13 +01003987 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003988
Thomas Woutersce272b62007-09-19 21:19:28 +00003989fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003990
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003991 /* decref'ing the frame can cause __del__ methods to get invoked,
3992 which can call back into Python. While we're done with the
3993 current Python frame (f), the associated C stack is still in use,
3994 so recursion_depth must be boosted for the duration.
3995 */
3996 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003997 if (Py_REFCNT(f) > 1) {
3998 Py_DECREF(f);
3999 _PyObject_GC_TRACK(f);
4000 }
4001 else {
4002 ++tstate->recursion_depth;
4003 Py_DECREF(f);
4004 --tstate->recursion_depth;
4005 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004006 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004007}
4008
Victor Stinner40ee3012014-06-16 15:59:28 +02004009PyObject *
4010PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004011 PyObject *const *args, int argcount,
4012 PyObject *const *kws, int kwcount,
4013 PyObject *const *defs, int defcount,
4014 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004015{
4016 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004017 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004018 kws, kws != NULL ? kws + 1 : NULL,
4019 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004020 defs, defcount,
4021 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004022 NULL, NULL);
4023}
Tim Peters5ca576e2001-06-18 22:08:13 +00004024
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004025static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05004026special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004027{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004028 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004029 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004030 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05004031 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004032 return NULL;
4033 }
4034 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004035}
4036
4037
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004038/* Logic for the raise statement (too complicated for inlining).
4039 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004040static int
Collin Winter828f04a2007-08-31 00:04:24 +00004041do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004042{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004043 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004045 if (exc == NULL) {
4046 /* Reraise */
Victor Stinner50b48572018-11-01 01:51:40 +01004047 PyThreadState *tstate = _PyThreadState_GET();
Mark Shannonae3087c2017-10-22 22:41:51 +01004048 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004049 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004050 type = exc_info->exc_type;
4051 value = exc_info->exc_value;
4052 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004053 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004054 PyErr_SetString(PyExc_RuntimeError,
4055 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004056 return 0;
4057 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004058 Py_XINCREF(type);
4059 Py_XINCREF(value);
4060 Py_XINCREF(tb);
4061 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004062 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004063 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004065 /* We support the following forms of raise:
4066 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004067 raise <instance>
4068 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004070 if (PyExceptionClass_Check(exc)) {
4071 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004072 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004073 if (value == NULL)
4074 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004075 if (!PyExceptionInstance_Check(value)) {
4076 PyErr_Format(PyExc_TypeError,
4077 "calling %R should have returned an instance of "
4078 "BaseException, not %R",
4079 type, Py_TYPE(value));
4080 goto raise_error;
4081 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004082 }
4083 else if (PyExceptionInstance_Check(exc)) {
4084 value = exc;
4085 type = PyExceptionInstance_Class(exc);
4086 Py_INCREF(type);
4087 }
4088 else {
4089 /* Not something you can raise. You get an exception
4090 anyway, just not what you specified :-) */
4091 Py_DECREF(exc);
4092 PyErr_SetString(PyExc_TypeError,
4093 "exceptions must derive from BaseException");
4094 goto raise_error;
4095 }
Collin Winter828f04a2007-08-31 00:04:24 +00004096
Serhiy Storchakac0191582016-09-27 11:37:10 +03004097 assert(type != NULL);
4098 assert(value != NULL);
4099
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004100 if (cause) {
4101 PyObject *fixed_cause;
4102 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004103 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004104 if (fixed_cause == NULL)
4105 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004106 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004107 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004108 else if (PyExceptionInstance_Check(cause)) {
4109 fixed_cause = cause;
4110 }
4111 else if (cause == Py_None) {
4112 Py_DECREF(cause);
4113 fixed_cause = NULL;
4114 }
4115 else {
4116 PyErr_SetString(PyExc_TypeError,
4117 "exception causes must derive from "
4118 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004119 goto raise_error;
4120 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004121 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004122 }
Collin Winter828f04a2007-08-31 00:04:24 +00004123
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004124 PyErr_SetObject(type, value);
4125 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004126 Py_DECREF(value);
4127 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004128 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004129
4130raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004131 Py_XDECREF(value);
4132 Py_XDECREF(type);
4133 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004134 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004135}
4136
Tim Petersd6d010b2001-06-21 02:49:55 +00004137/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004138 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004139
Guido van Rossum0368b722007-05-11 16:50:42 +00004140 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4141 with a variable target.
4142*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004143
Barry Warsawe42b18f1997-08-25 22:13:04 +00004144static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004145unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004146{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004147 int i = 0, j = 0;
4148 Py_ssize_t ll = 0;
4149 PyObject *it; /* iter(v) */
4150 PyObject *w;
4151 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004152
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004153 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004155 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004156 if (it == NULL) {
4157 if (PyErr_ExceptionMatches(PyExc_TypeError) &&
4158 v->ob_type->tp_iter == NULL && !PySequence_Check(v))
4159 {
4160 PyErr_Format(PyExc_TypeError,
4161 "cannot unpack non-iterable %.200s object",
4162 v->ob_type->tp_name);
4163 }
4164 return 0;
4165 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004167 for (; i < argcnt; i++) {
4168 w = PyIter_Next(it);
4169 if (w == NULL) {
4170 /* Iterator done, via error or exhaustion. */
4171 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004172 if (argcntafter == -1) {
4173 PyErr_Format(PyExc_ValueError,
4174 "not enough values to unpack (expected %d, got %d)",
4175 argcnt, i);
4176 }
4177 else {
4178 PyErr_Format(PyExc_ValueError,
4179 "not enough values to unpack "
4180 "(expected at least %d, got %d)",
4181 argcnt + argcntafter, i);
4182 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004183 }
4184 goto Error;
4185 }
4186 *--sp = w;
4187 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004189 if (argcntafter == -1) {
4190 /* We better have exhausted the iterator now. */
4191 w = PyIter_Next(it);
4192 if (w == NULL) {
4193 if (PyErr_Occurred())
4194 goto Error;
4195 Py_DECREF(it);
4196 return 1;
4197 }
4198 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004199 PyErr_Format(PyExc_ValueError,
4200 "too many values to unpack (expected %d)",
4201 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004202 goto Error;
4203 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004204
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004205 l = PySequence_List(it);
4206 if (l == NULL)
4207 goto Error;
4208 *--sp = l;
4209 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004211 ll = PyList_GET_SIZE(l);
4212 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004213 PyErr_Format(PyExc_ValueError,
4214 "not enough values to unpack (expected at least %d, got %zd)",
4215 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004216 goto Error;
4217 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004219 /* Pop the "after-variable" args off the list. */
4220 for (j = argcntafter; j > 0; j--, i++) {
4221 *--sp = PyList_GET_ITEM(l, ll - j);
4222 }
4223 /* Resize the list. */
4224 Py_SIZE(l) = ll - argcntafter;
4225 Py_DECREF(it);
4226 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004227
Tim Petersd6d010b2001-06-21 02:49:55 +00004228Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004229 for (; i > 0; i--, sp++)
4230 Py_DECREF(*sp);
4231 Py_XDECREF(it);
4232 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004233}
4234
4235
Guido van Rossum96a42c81992-01-12 02:29:51 +00004236#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004237static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004238prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004239{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004240 printf("%s ", str);
4241 if (PyObject_Print(v, stdout, 0) != 0)
4242 PyErr_Clear(); /* Don't know what else to do */
4243 printf("\n");
4244 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004245}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004246#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004247
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004248static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004249call_exc_trace(Py_tracefunc func, PyObject *self,
4250 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004251{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004252 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004253 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004254 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004255 if (value == NULL) {
4256 value = Py_None;
4257 Py_INCREF(value);
4258 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004259 PyErr_NormalizeException(&type, &value, &orig_traceback);
4260 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004261 arg = PyTuple_Pack(3, type, value, traceback);
4262 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004263 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004264 return;
4265 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004266 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004267 Py_DECREF(arg);
4268 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004269 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004270 else {
4271 Py_XDECREF(type);
4272 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004273 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004274 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004275}
4276
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004277static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004278call_trace_protected(Py_tracefunc func, PyObject *obj,
4279 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004280 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004281{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004282 PyObject *type, *value, *traceback;
4283 int err;
4284 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004285 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004286 if (err == 0)
4287 {
4288 PyErr_Restore(type, value, traceback);
4289 return 0;
4290 }
4291 else {
4292 Py_XDECREF(type);
4293 Py_XDECREF(value);
4294 Py_XDECREF(traceback);
4295 return -1;
4296 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004297}
4298
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004299static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004300call_trace(Py_tracefunc func, PyObject *obj,
4301 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004302 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004303{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004304 int result;
4305 if (tstate->tracing)
4306 return 0;
4307 tstate->tracing++;
4308 tstate->use_tracing = 0;
4309 result = func(obj, frame, what, arg);
4310 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4311 || (tstate->c_profilefunc != NULL));
4312 tstate->tracing--;
4313 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004314}
4315
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004316PyObject *
4317_PyEval_CallTracing(PyObject *func, PyObject *args)
4318{
Victor Stinner50b48572018-11-01 01:51:40 +01004319 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004320 int save_tracing = tstate->tracing;
4321 int save_use_tracing = tstate->use_tracing;
4322 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004324 tstate->tracing = 0;
4325 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4326 || (tstate->c_profilefunc != NULL));
4327 result = PyObject_Call(func, args, NULL);
4328 tstate->tracing = save_tracing;
4329 tstate->use_tracing = save_use_tracing;
4330 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004331}
4332
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004333/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004334static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004335maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004336 PyThreadState *tstate, PyFrameObject *frame,
4337 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004338{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004339 int result = 0;
4340 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004342 /* If the last instruction executed isn't in the current
4343 instruction window, reset the window.
4344 */
4345 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4346 PyAddrPair bounds;
4347 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4348 &bounds);
4349 *instr_lb = bounds.ap_lower;
4350 *instr_ub = bounds.ap_upper;
4351 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004352 /* If the last instruction falls at the start of a line or if it
4353 represents a jump backwards, update the frame's line number and
4354 then call the trace function if we're tracing source lines.
4355 */
4356 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004357 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004358 if (frame->f_trace_lines) {
4359 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4360 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004361 }
George King20faa682017-10-18 17:44:22 -07004362 /* Always emit an opcode event if we're tracing all opcodes. */
4363 if (frame->f_trace_opcodes) {
4364 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4365 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004366 *instr_prev = frame->f_lasti;
4367 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004368}
4369
Fred Drake5755ce62001-06-27 19:19:46 +00004370void
4371PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004372{
Victor Stinner50b48572018-11-01 01:51:40 +01004373 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004374 PyObject *temp = tstate->c_profileobj;
4375 Py_XINCREF(arg);
4376 tstate->c_profilefunc = NULL;
4377 tstate->c_profileobj = NULL;
4378 /* Must make sure that tracing is not ignored if 'temp' is freed */
4379 tstate->use_tracing = tstate->c_tracefunc != NULL;
4380 Py_XDECREF(temp);
4381 tstate->c_profilefunc = func;
4382 tstate->c_profileobj = arg;
4383 /* Flag that tracing or profiling is turned on */
4384 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004385}
4386
4387void
4388PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4389{
Victor Stinner50b48572018-11-01 01:51:40 +01004390 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004391 PyObject *temp = tstate->c_traceobj;
4392 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4393 Py_XINCREF(arg);
4394 tstate->c_tracefunc = NULL;
4395 tstate->c_traceobj = NULL;
4396 /* Must make sure that profiling is not ignored if 'temp' is freed */
4397 tstate->use_tracing = tstate->c_profilefunc != NULL;
4398 Py_XDECREF(temp);
4399 tstate->c_tracefunc = func;
4400 tstate->c_traceobj = arg;
4401 /* Flag that tracing or profiling is turned on */
4402 tstate->use_tracing = ((func != NULL)
4403 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004404}
4405
Yury Selivanov75445082015-05-11 22:57:16 -04004406void
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004407_PyEval_SetCoroutineOriginTrackingDepth(int new_depth)
4408{
4409 assert(new_depth >= 0);
Victor Stinner50b48572018-11-01 01:51:40 +01004410 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004411 tstate->coroutine_origin_tracking_depth = new_depth;
4412}
4413
4414int
4415_PyEval_GetCoroutineOriginTrackingDepth(void)
4416{
Victor Stinner50b48572018-11-01 01:51:40 +01004417 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004418 return tstate->coroutine_origin_tracking_depth;
4419}
4420
4421void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004422_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004423{
Victor Stinner50b48572018-11-01 01:51:40 +01004424 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanov75445082015-05-11 22:57:16 -04004425
Yury Selivanov75445082015-05-11 22:57:16 -04004426 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004427 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004428}
4429
4430PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004431_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004432{
Victor Stinner50b48572018-11-01 01:51:40 +01004433 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanov75445082015-05-11 22:57:16 -04004434 return tstate->coroutine_wrapper;
4435}
4436
Yury Selivanoveb636452016-09-08 22:01:51 -07004437void
4438_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4439{
Victor Stinner50b48572018-11-01 01:51:40 +01004440 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004441
4442 Py_XINCREF(firstiter);
4443 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4444}
4445
4446PyObject *
4447_PyEval_GetAsyncGenFirstiter(void)
4448{
Victor Stinner50b48572018-11-01 01:51:40 +01004449 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004450 return tstate->async_gen_firstiter;
4451}
4452
4453void
4454_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4455{
Victor Stinner50b48572018-11-01 01:51:40 +01004456 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004457
4458 Py_XINCREF(finalizer);
4459 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4460}
4461
4462PyObject *
4463_PyEval_GetAsyncGenFinalizer(void)
4464{
Victor Stinner50b48572018-11-01 01:51:40 +01004465 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004466 return tstate->async_gen_finalizer;
4467}
4468
Guido van Rossumb209a111997-04-29 18:18:01 +00004469PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004470PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004471{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004472 PyFrameObject *current_frame = PyEval_GetFrame();
4473 if (current_frame == NULL)
Victor Stinnercaba55b2018-08-03 15:33:52 +02004474 return _PyInterpreterState_GET_UNSAFE()->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004475 else
4476 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004477}
4478
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004479/* Convenience function to get a builtin from its name */
4480PyObject *
4481_PyEval_GetBuiltinId(_Py_Identifier *name)
4482{
4483 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4484 if (attr) {
4485 Py_INCREF(attr);
4486 }
4487 else if (!PyErr_Occurred()) {
4488 PyErr_SetObject(PyExc_AttributeError, _PyUnicode_FromId(name));
4489 }
4490 return attr;
4491}
4492
Guido van Rossumb209a111997-04-29 18:18:01 +00004493PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004494PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004495{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004496 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004497 if (current_frame == NULL) {
4498 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004499 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004500 }
4501
4502 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4503 return NULL;
4504
4505 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004506 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004507}
4508
Guido van Rossumb209a111997-04-29 18:18:01 +00004509PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004510PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004511{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004512 PyFrameObject *current_frame = PyEval_GetFrame();
4513 if (current_frame == NULL)
4514 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004515
4516 assert(current_frame->f_globals != NULL);
4517 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004518}
4519
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004520PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004521PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004522{
Victor Stinner50b48572018-11-01 01:51:40 +01004523 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004524 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004525}
4526
Guido van Rossum6135a871995-01-09 17:53:26 +00004527int
Tim Peters5ba58662001-07-16 02:29:45 +00004528PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004529{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004530 PyFrameObject *current_frame = PyEval_GetFrame();
4531 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004533 if (current_frame != NULL) {
4534 const int codeflags = current_frame->f_code->co_flags;
4535 const int compilerflags = codeflags & PyCF_MASK;
4536 if (compilerflags) {
4537 result = 1;
4538 cf->cf_flags |= compilerflags;
4539 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004540#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004541 if (codeflags & CO_GENERATOR_ALLOWED) {
4542 result = 1;
4543 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4544 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004545#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004546 }
4547 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004548}
4549
Guido van Rossum3f5da241990-12-20 15:06:42 +00004550
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004551const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004552PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004553{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004554 if (PyMethod_Check(func))
4555 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4556 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004557 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004558 else if (PyCFunction_Check(func))
4559 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4560 else
4561 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004562}
4563
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004564const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004565PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004566{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004567 if (PyMethod_Check(func))
4568 return "()";
4569 else if (PyFunction_Check(func))
4570 return "()";
4571 else if (PyCFunction_Check(func))
4572 return "()";
4573 else
4574 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004575}
4576
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004577#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004578if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004579 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4580 tstate, tstate->frame, \
4581 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004582 x = NULL; \
4583 } \
4584 else { \
4585 x = call; \
4586 if (tstate->c_profilefunc != NULL) { \
4587 if (x == NULL) { \
4588 call_trace_protected(tstate->c_profilefunc, \
4589 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004590 tstate, tstate->frame, \
4591 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004592 /* XXX should pass (type, value, tb) */ \
4593 } else { \
4594 if (call_trace(tstate->c_profilefunc, \
4595 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004596 tstate, tstate->frame, \
4597 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004598 Py_DECREF(x); \
4599 x = NULL; \
4600 } \
4601 } \
4602 } \
4603 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004604} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004605 x = call; \
4606 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004607
Victor Stinner415c5102017-01-11 00:54:57 +01004608/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4609 to reduce the stack consumption. */
4610Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Benjamin Peterson4fd64b92016-09-09 14:57:58 -07004611call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004612{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004613 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004614 PyObject *func = *pfunc;
4615 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004616 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4617 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004618 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004620 /* Always dispatch PyCFunction first, because these are
4621 presumed to be the most frequent callable object.
4622 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004623 if (PyCFunction_Check(func)) {
Victor Stinner50b48572018-11-01 01:51:40 +01004624 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004625 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004626 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004627 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
Victor Stinner50b48572018-11-01 01:51:40 +01004628 PyThreadState *tstate = _PyThreadState_GET();
jdemeyer56868f92018-07-21 10:30:59 +02004629 if (nargs > 0 && tstate->use_tracing) {
4630 /* We need to create a temporary bound method as argument
4631 for profiling.
4632
4633 If nargs == 0, then this cannot work because we have no
4634 "self". In any case, the call itself would raise
4635 TypeError (foo needs an argument), so we just skip
4636 profiling. */
4637 PyObject *self = stack[0];
4638 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
jdemeyer147d9552018-07-23 18:41:20 +02004639 if (func != NULL) {
4640 C_TRACE(x, _PyCFunction_FastCallKeywords(func,
4641 stack+1, nargs-1,
4642 kwnames));
4643 Py_DECREF(func);
INADA Naoki93fac8d2017-03-07 14:24:37 +09004644 }
jdemeyer147d9552018-07-23 18:41:20 +02004645 else {
4646 x = NULL;
4647 }
INADA Naoki93fac8d2017-03-07 14:24:37 +09004648 }
4649 else {
4650 x = _PyMethodDescr_FastCallKeywords(func, stack, nargs, kwnames);
4651 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004652 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004653 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004654 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
Victor Stinnerb69ee8c2016-11-28 18:32:31 +01004655 /* Optimize access to bound methods. Reuse the Python stack
4656 to pass 'self' as the first argument, replace 'func'
4657 with 'self'. It avoids the creation of a new temporary tuple
4658 for arguments (to replace func with self) when the method uses
4659 FASTCALL. */
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004660 PyObject *self = PyMethod_GET_SELF(func);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004661 Py_INCREF(self);
4662 func = PyMethod_GET_FUNCTION(func);
4663 Py_INCREF(func);
4664 Py_SETREF(*pfunc, self);
4665 nargs++;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004666 stack--;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004667 }
4668 else {
4669 Py_INCREF(func);
4670 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004671
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004672 if (PyFunction_Check(func)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004673 x = _PyFunction_FastCallKeywords(func, stack, nargs, kwnames);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004674 }
4675 else {
4676 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4677 }
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004678 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004679 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004680
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004681 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4682
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004683 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004684 while ((*pp_stack) > pfunc) {
4685 w = EXT_POP(*pp_stack);
4686 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004687 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004688
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004689 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004690}
4691
Jeremy Hylton52820442001-01-03 23:52:36 +00004692static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004693do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004694{
jdemeyere89de732018-09-19 12:06:20 +02004695 PyObject *result;
4696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004697 if (PyCFunction_Check(func)) {
Victor Stinner50b48572018-11-01 01:51:40 +01004698 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004699 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004700 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004701 }
jdemeyere89de732018-09-19 12:06:20 +02004702 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
Victor Stinner50b48572018-11-01 01:51:40 +01004703 PyThreadState *tstate = _PyThreadState_GET();
jdemeyere89de732018-09-19 12:06:20 +02004704 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
4705 if (nargs > 0 && tstate->use_tracing) {
4706 /* We need to create a temporary bound method as argument
4707 for profiling.
4708
4709 If nargs == 0, then this cannot work because we have no
4710 "self". In any case, the call itself would raise
4711 TypeError (foo needs an argument), so we just skip
4712 profiling. */
4713 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
4714 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4715 if (func == NULL) {
4716 return NULL;
4717 }
4718
4719 C_TRACE(result, _PyCFunction_FastCallDict(func,
Victor Stinnerd17a6932018-11-09 16:56:48 +01004720 &_PyTuple_ITEMS(callargs)[1],
jdemeyere89de732018-09-19 12:06:20 +02004721 nargs - 1,
4722 kwdict));
4723 Py_DECREF(func);
4724 return result;
4725 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004726 }
jdemeyere89de732018-09-19 12:06:20 +02004727 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00004728}
4729
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004730/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004731 nb_index slot defined, and store in *pi.
4732 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004733 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004734 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004735*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004736int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004737_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004738{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004739 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004740 Py_ssize_t x;
4741 if (PyIndex_Check(v)) {
4742 x = PyNumber_AsSsize_t(v, NULL);
4743 if (x == -1 && PyErr_Occurred())
4744 return 0;
4745 }
4746 else {
4747 PyErr_SetString(PyExc_TypeError,
4748 "slice indices must be integers or "
4749 "None or have an __index__ method");
4750 return 0;
4751 }
4752 *pi = x;
4753 }
4754 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004755}
4756
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004757int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004758_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004759{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004760 Py_ssize_t x;
4761 if (PyIndex_Check(v)) {
4762 x = PyNumber_AsSsize_t(v, NULL);
4763 if (x == -1 && PyErr_Occurred())
4764 return 0;
4765 }
4766 else {
4767 PyErr_SetString(PyExc_TypeError,
4768 "slice indices must be integers or "
4769 "have an __index__ method");
4770 return 0;
4771 }
4772 *pi = x;
4773 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004774}
4775
4776
Guido van Rossum486364b2007-06-30 05:01:58 +00004777#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004778 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004779
Guido van Rossumb209a111997-04-29 18:18:01 +00004780static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004781cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004782{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004783 int res = 0;
4784 switch (op) {
4785 case PyCmp_IS:
4786 res = (v == w);
4787 break;
4788 case PyCmp_IS_NOT:
4789 res = (v != w);
4790 break;
4791 case PyCmp_IN:
4792 res = PySequence_Contains(w, v);
4793 if (res < 0)
4794 return NULL;
4795 break;
4796 case PyCmp_NOT_IN:
4797 res = PySequence_Contains(w, v);
4798 if (res < 0)
4799 return NULL;
4800 res = !res;
4801 break;
4802 case PyCmp_EXC_MATCH:
4803 if (PyTuple_Check(w)) {
4804 Py_ssize_t i, length;
4805 length = PyTuple_Size(w);
4806 for (i = 0; i < length; i += 1) {
4807 PyObject *exc = PyTuple_GET_ITEM(w, i);
4808 if (!PyExceptionClass_Check(exc)) {
4809 PyErr_SetString(PyExc_TypeError,
4810 CANNOT_CATCH_MSG);
4811 return NULL;
4812 }
4813 }
4814 }
4815 else {
4816 if (!PyExceptionClass_Check(w)) {
4817 PyErr_SetString(PyExc_TypeError,
4818 CANNOT_CATCH_MSG);
4819 return NULL;
4820 }
4821 }
4822 res = PyErr_GivenExceptionMatches(v, w);
4823 break;
4824 default:
4825 return PyObject_RichCompare(v, w, op);
4826 }
4827 v = res ? Py_True : Py_False;
4828 Py_INCREF(v);
4829 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004830}
4831
Thomas Wouters52152252000-08-17 22:55:00 +00004832static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004833import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
4834{
4835 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004836 PyObject *import_func, *res;
4837 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004838
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004839 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004840 if (import_func == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004841 if (!PyErr_Occurred()) {
4842 PyErr_SetString(PyExc_ImportError, "__import__ not found");
4843 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004844 return NULL;
4845 }
4846
4847 /* Fast path for not overloaded __import__. */
Victor Stinnercaba55b2018-08-03 15:33:52 +02004848 if (import_func == _PyInterpreterState_GET_UNSAFE()->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004849 int ilevel = _PyLong_AsInt(level);
4850 if (ilevel == -1 && PyErr_Occurred()) {
4851 return NULL;
4852 }
4853 res = PyImport_ImportModuleLevelObject(
4854 name,
4855 f->f_globals,
4856 f->f_locals == NULL ? Py_None : f->f_locals,
4857 fromlist,
4858 ilevel);
4859 return res;
4860 }
4861
4862 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004863
4864 stack[0] = name;
4865 stack[1] = f->f_globals;
4866 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
4867 stack[3] = fromlist;
4868 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02004869 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004870 Py_DECREF(import_func);
4871 return res;
4872}
4873
4874static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004875import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004876{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004877 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02004878 _Py_IDENTIFIER(__name__);
Xiang Zhang4830f582017-03-21 11:13:42 +08004879 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004880
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004881 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02004882 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004883 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004884 /* Issue #17636: in case this failed because of a circular relative
4885 import, try to fallback on reading the module directly from
4886 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02004887 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07004888 if (pkgname == NULL) {
4889 goto error;
4890 }
Oren Milman6db70332017-09-19 14:23:01 +03004891 if (!PyUnicode_Check(pkgname)) {
4892 Py_CLEAR(pkgname);
4893 goto error;
4894 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004895 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07004896 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08004897 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004898 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07004899 }
Eric Snow3f9eee62017-09-15 16:35:20 -06004900 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004901 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07004902 if (x == NULL) {
4903 goto error;
4904 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004905 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004906 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07004907 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004908 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004909 if (pkgname == NULL) {
4910 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
4911 if (pkgname_or_unknown == NULL) {
4912 Py_XDECREF(pkgpath);
4913 return NULL;
4914 }
4915 } else {
4916 pkgname_or_unknown = pkgname;
4917 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004918
4919 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
4920 PyErr_Clear();
Xiang Zhang4830f582017-03-21 11:13:42 +08004921 errmsg = PyUnicode_FromFormat(
4922 "cannot import name %R from %R (unknown location)",
4923 name, pkgname_or_unknown
4924 );
4925 /* NULL check for errmsg done by PyErr_SetImportError. */
4926 PyErr_SetImportError(errmsg, pkgname, NULL);
4927 }
4928 else {
4929 errmsg = PyUnicode_FromFormat(
4930 "cannot import name %R from %R (%S)",
4931 name, pkgname_or_unknown, pkgpath
4932 );
4933 /* NULL check for errmsg done by PyErr_SetImportError. */
4934 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004935 }
4936
Xiang Zhang4830f582017-03-21 11:13:42 +08004937 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004938 Py_XDECREF(pkgname_or_unknown);
4939 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07004940 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00004941}
Guido van Rossumac7be682001-01-17 15:42:30 +00004942
Thomas Wouters52152252000-08-17 22:55:00 +00004943static int
4944import_all_from(PyObject *locals, PyObject *v)
4945{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004946 _Py_IDENTIFIER(__all__);
4947 _Py_IDENTIFIER(__dict__);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08004948 _Py_IDENTIFIER(__name__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004949 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004950 int skip_leading_underscores = 0;
4951 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004952
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004953 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
4954 return -1; /* Unexpected error */
4955 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004956 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004957 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
4958 return -1;
4959 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004960 if (dict == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004961 PyErr_SetString(PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004962 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004963 return -1;
4964 }
4965 all = PyMapping_Keys(dict);
4966 Py_DECREF(dict);
4967 if (all == NULL)
4968 return -1;
4969 skip_leading_underscores = 1;
4970 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004971
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004972 for (pos = 0, err = 0; ; pos++) {
4973 name = PySequence_GetItem(all, pos);
4974 if (name == NULL) {
4975 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4976 err = -1;
4977 else
4978 PyErr_Clear();
4979 break;
4980 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08004981 if (!PyUnicode_Check(name)) {
4982 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
4983 if (modname == NULL) {
4984 Py_DECREF(name);
4985 err = -1;
4986 break;
4987 }
4988 if (!PyUnicode_Check(modname)) {
4989 PyErr_Format(PyExc_TypeError,
4990 "module __name__ must be a string, not %.100s",
4991 Py_TYPE(modname)->tp_name);
4992 }
4993 else {
4994 PyErr_Format(PyExc_TypeError,
4995 "%s in %U.%s must be str, not %.100s",
4996 skip_leading_underscores ? "Key" : "Item",
4997 modname,
4998 skip_leading_underscores ? "__dict__" : "__all__",
4999 Py_TYPE(name)->tp_name);
5000 }
5001 Py_DECREF(modname);
5002 Py_DECREF(name);
5003 err = -1;
5004 break;
5005 }
5006 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005007 if (PyUnicode_READY(name) == -1) {
5008 Py_DECREF(name);
5009 err = -1;
5010 break;
5011 }
5012 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5013 Py_DECREF(name);
5014 continue;
5015 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005016 }
5017 value = PyObject_GetAttr(v, name);
5018 if (value == NULL)
5019 err = -1;
5020 else if (PyDict_CheckExact(locals))
5021 err = PyDict_SetItem(locals, name, value);
5022 else
5023 err = PyObject_SetItem(locals, name, value);
5024 Py_DECREF(name);
5025 Py_XDECREF(value);
5026 if (err != 0)
5027 break;
5028 }
5029 Py_DECREF(all);
5030 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005031}
5032
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005033static int
5034check_args_iterable(PyObject *func, PyObject *args)
5035{
5036 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
5037 PyErr_Format(PyExc_TypeError,
5038 "%.200s%.200s argument after * "
5039 "must be an iterable, not %.200s",
5040 PyEval_GetFuncName(func),
5041 PyEval_GetFuncDesc(func),
5042 args->ob_type->tp_name);
5043 return -1;
5044 }
5045 return 0;
5046}
5047
5048static void
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005049format_kwargs_error(PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005050{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005051 /* _PyDict_MergeEx raises attribute
5052 * error (percolated from an attempt
5053 * to get 'keys' attribute) instead of
5054 * a type error if its second argument
5055 * is not a mapping.
5056 */
5057 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
5058 PyErr_Format(PyExc_TypeError,
5059 "%.200s%.200s argument after ** "
5060 "must be a mapping, not %.200s",
5061 PyEval_GetFuncName(func),
5062 PyEval_GetFuncDesc(func),
5063 kwargs->ob_type->tp_name);
5064 }
5065 else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
5066 PyObject *exc, *val, *tb;
5067 PyErr_Fetch(&exc, &val, &tb);
5068 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
5069 PyObject *key = PyTuple_GET_ITEM(val, 0);
5070 if (!PyUnicode_Check(key)) {
5071 PyErr_Format(PyExc_TypeError,
5072 "%.200s%.200s keywords must be strings",
5073 PyEval_GetFuncName(func),
5074 PyEval_GetFuncDesc(func));
5075 } else {
5076 PyErr_Format(PyExc_TypeError,
5077 "%.200s%.200s got multiple "
5078 "values for keyword argument '%U'",
5079 PyEval_GetFuncName(func),
5080 PyEval_GetFuncDesc(func),
5081 key);
5082 }
5083 Py_XDECREF(exc);
5084 Py_XDECREF(val);
5085 Py_XDECREF(tb);
5086 }
5087 else {
5088 PyErr_Restore(exc, val, tb);
5089 }
5090 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005091}
5092
Guido van Rossumac7be682001-01-17 15:42:30 +00005093static void
Neal Norwitzda059e32007-08-26 05:33:45 +00005094format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005095{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005096 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005097
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005098 if (!obj)
5099 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005100
Serhiy Storchaka06515832016-11-20 09:13:07 +02005101 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005102 if (!obj_str)
5103 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005104
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005105 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005106}
Guido van Rossum950361c1997-01-24 13:49:28 +00005107
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005108static void
5109format_exc_unbound(PyCodeObject *co, int oparg)
5110{
5111 PyObject *name;
5112 /* Don't stomp existing exception */
5113 if (PyErr_Occurred())
5114 return;
5115 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5116 name = PyTuple_GET_ITEM(co->co_cellvars,
5117 oparg);
5118 format_exc_check_arg(
5119 PyExc_UnboundLocalError,
5120 UNBOUNDLOCAL_ERROR_MSG,
5121 name);
5122 } else {
5123 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5124 PyTuple_GET_SIZE(co->co_cellvars));
5125 format_exc_check_arg(PyExc_NameError,
5126 UNBOUNDFREE_ERROR_MSG, name);
5127 }
5128}
5129
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005130static void
5131format_awaitable_error(PyTypeObject *type, int prevopcode)
5132{
5133 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5134 if (prevopcode == BEFORE_ASYNC_WITH) {
5135 PyErr_Format(PyExc_TypeError,
5136 "'async with' received an object from __aenter__ "
5137 "that does not implement __await__: %.100s",
5138 type->tp_name);
5139 }
5140 else if (prevopcode == WITH_CLEANUP_START) {
5141 PyErr_Format(PyExc_TypeError,
5142 "'async with' received an object from __aexit__ "
5143 "that does not implement __await__: %.100s",
5144 type->tp_name);
5145 }
5146 }
5147}
5148
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005149static PyObject *
5150unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005151 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005152{
5153 PyObject *res;
5154 if (Py_REFCNT(v) == 2) {
5155 /* In the common case, there are 2 references to the value
5156 * stored in 'variable' when the += is performed: one on the
5157 * value stack (in 'v') and one still stored in the
5158 * 'variable'. We try to delete the variable now to reduce
5159 * the refcnt to 1.
5160 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005161 int opcode, oparg;
5162 NEXTOPARG();
5163 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005164 case STORE_FAST:
5165 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005166 PyObject **fastlocals = f->f_localsplus;
5167 if (GETLOCAL(oparg) == v)
5168 SETLOCAL(oparg, NULL);
5169 break;
5170 }
5171 case STORE_DEREF:
5172 {
5173 PyObject **freevars = (f->f_localsplus +
5174 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005175 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005176 if (PyCell_GET(c) == v) {
5177 PyCell_SET(c, NULL);
5178 Py_DECREF(v);
5179 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005180 break;
5181 }
5182 case STORE_NAME:
5183 {
5184 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005185 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005186 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005187 if (locals && PyDict_CheckExact(locals)) {
5188 PyObject *w = PyDict_GetItemWithError(locals, name);
5189 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
5190 (w == NULL && PyErr_Occurred()))
5191 {
5192 Py_DECREF(v);
5193 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005194 }
5195 }
5196 break;
5197 }
5198 }
5199 }
5200 res = v;
5201 PyUnicode_Append(&res, w);
5202 return res;
5203}
5204
Guido van Rossum950361c1997-01-24 13:49:28 +00005205#ifdef DYNAMIC_EXECUTION_PROFILE
5206
Skip Montanarof118cb12001-10-15 20:51:38 +00005207static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005208getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005209{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005210 int i;
5211 PyObject *l = PyList_New(256);
5212 if (l == NULL) return NULL;
5213 for (i = 0; i < 256; i++) {
5214 PyObject *x = PyLong_FromLong(a[i]);
5215 if (x == NULL) {
5216 Py_DECREF(l);
5217 return NULL;
5218 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005219 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005220 }
5221 for (i = 0; i < 256; i++)
5222 a[i] = 0;
5223 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005224}
5225
5226PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005227_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005228{
5229#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005230 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005231#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005232 int i;
5233 PyObject *l = PyList_New(257);
5234 if (l == NULL) return NULL;
5235 for (i = 0; i < 257; i++) {
5236 PyObject *x = getarray(dxpairs[i]);
5237 if (x == NULL) {
5238 Py_DECREF(l);
5239 return NULL;
5240 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005241 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005242 }
5243 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005244#endif
5245}
5246
5247#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005248
5249Py_ssize_t
5250_PyEval_RequestCodeExtraIndex(freefunc free)
5251{
Victor Stinnercaba55b2018-08-03 15:33:52 +02005252 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -07005253 Py_ssize_t new_index;
5254
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005255 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005256 return -1;
5257 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005258 new_index = interp->co_extra_user_count++;
5259 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005260 return new_index;
5261}
Łukasz Langaa785c872016-09-09 17:37:37 -07005262
5263static void
5264dtrace_function_entry(PyFrameObject *f)
5265{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005266 const char *filename;
5267 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005268 int lineno;
5269
5270 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5271 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5272 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5273
5274 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5275}
5276
5277static void
5278dtrace_function_return(PyFrameObject *f)
5279{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005280 const char *filename;
5281 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005282 int lineno;
5283
5284 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5285 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5286 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5287
5288 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5289}
5290
5291/* DTrace equivalent of maybe_call_line_trace. */
5292static void
5293maybe_dtrace_line(PyFrameObject *frame,
5294 int *instr_lb, int *instr_ub, int *instr_prev)
5295{
5296 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005297 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005298
5299 /* If the last instruction executed isn't in the current
5300 instruction window, reset the window.
5301 */
5302 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5303 PyAddrPair bounds;
5304 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5305 &bounds);
5306 *instr_lb = bounds.ap_lower;
5307 *instr_ub = bounds.ap_upper;
5308 }
5309 /* If the last instruction falls at the start of a line or if
5310 it represents a jump backwards, update the frame's line
5311 number and call the trace function. */
5312 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5313 frame->f_lineno = line;
5314 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5315 if (!co_filename)
5316 co_filename = "?";
5317 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5318 if (!co_name)
5319 co_name = "?";
5320 PyDTrace_LINE(co_filename, co_name, line);
5321 }
5322 *instr_prev = frame->f_lasti;
5323}