blob: 03456f6d2ab4f6fc8ae17614bba87a3b3963f4da [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. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +000099#define COMPUTE_EVAL_BREAKER() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 _Py_atomic_store_relaxed( \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600101 &_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) | \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600104 _Py_atomic_load_relaxed(&_PyRuntime.ceval.pending.calls_to_do) | \
105 _PyRuntime.ceval.pending.async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000106
107#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); \
110 _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
113#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); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 COMPUTE_EVAL_BREAKER(); \
117 } 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 */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000120#define SIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 do { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600122 _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
126#define UNSIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 do { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600128 _Py_atomic_store_relaxed(&_PyRuntime.ceval.pending.calls_to_do, 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 COMPUTE_EVAL_BREAKER(); \
130 } 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); \
135 _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \
136 } while (0)
137
138#define UNSIGNAL_PENDING_SIGNALS() \
139 do { \
140 _Py_atomic_store_relaxed(&_PyRuntime.ceval.signals_pending, 0); \
141 COMPUTE_EVAL_BREAKER(); \
142 } while (0)
143
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000144#define SIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 do { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600146 _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
150#define UNSIGNAL_ASYNC_EXC() \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600151 do { \
152 _PyRuntime.ceval.pending.async_exc = 0; \
153 COMPUTE_EVAL_BREAKER(); \
154 } 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 Snow2ebc5ce2017-09-07 23:51:28 -0600177 _PyRuntime.ceval.pending.main_thread = PyThread_get_thread_ident();
178 if (!_PyRuntime.ceval.pending.lock)
179 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000180}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000181
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000182void
Antoine Pitrou1df15362010-09-13 14:16:46 +0000183_PyEval_FiniThreads(void)
184{
185 if (!gil_created())
186 return;
187 destroy_gil();
188 assert(!gil_created());
189}
190
191void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000192PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000193{
Victor Stinner50b48572018-11-01 01:51:40 +0100194 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 if (tstate == NULL)
196 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
197 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000198}
199
200void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000201PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000202{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100204 We therefore avoid PyThreadState_Get() which dumps a fatal error
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 in debug mode.
206 */
Victor Stinner50b48572018-11-01 01:51:40 +0100207 drop_gil(_PyThreadState_GET());
Guido van Rossum25ce5661997-08-02 03:10:38 +0000208}
209
210void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000211PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000212{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 if (tstate == NULL)
214 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
215 /* Check someone has called PyEval_InitThreads() to create the lock */
216 assert(gil_created());
217 take_gil(tstate);
218 if (PyThreadState_Swap(tstate) != NULL)
219 Py_FatalError(
220 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000221}
222
223void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000224PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000225{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 if (tstate == NULL)
227 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
228 if (PyThreadState_Swap(NULL) != tstate)
229 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
230 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000231}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000232
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200233/* This function is called from PyOS_AfterFork_Child to destroy all threads
234 * which are not running in the child process, and clear internal locks
235 * which might be held by those threads.
236 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000237
238void
239PyEval_ReInitThreads(void)
240{
Victor Stinner50b48572018-11-01 01:51:40 +0100241 PyThreadState *current_tstate = _PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 if (!gil_created())
244 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 recreate_gil();
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600246 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200247 take_gil(current_tstate);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600248 _PyRuntime.ceval.pending.main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000249
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200250 /* Destroy all threads except the current one */
251 _PyThreadState_DeleteExcept(current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000252}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000253
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000254/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600255 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000256
257void
258_PyEval_SignalAsyncExc(void)
259{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000261}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000262
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000263PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000264PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000265{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 PyThreadState *tstate = PyThreadState_Swap(NULL);
267 if (tstate == NULL)
268 Py_FatalError("PyEval_SaveThread: NULL tstate");
Victor Stinner2914bb32018-01-29 11:57:45 +0100269 assert(gil_created());
270 drop_gil(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000272}
273
274void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000275PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000276{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 if (tstate == NULL)
278 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Victor Stinner2914bb32018-01-29 11:57:45 +0100279 assert(gil_created());
280
281 int err = errno;
282 take_gil(tstate);
283 /* _Py_Finalizing is protected by the GIL */
284 if (_Py_IsFinalizing() && !_Py_CURRENTLY_FINALIZING(tstate)) {
285 drop_gil(tstate);
286 PyThread_exit_thread();
287 Py_UNREACHABLE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 }
Victor Stinner2914bb32018-01-29 11:57:45 +0100289 errno = err;
290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000292}
293
294
Guido van Rossuma9672091994-09-14 13:31:22 +0000295/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
296 signal handlers or Mac I/O completion routines) can schedule calls
297 to a function to be called synchronously.
298 The synchronous function is called with one void* argument.
299 It should return 0 for success or -1 for failure -- failure should
300 be accompanied by an exception.
301
302 If registry succeeds, the registry function returns 0; if it fails
303 (e.g. due to too many pending calls) it returns -1 (without setting
304 an exception condition).
305
306 Note that because registry may occur from within signal handlers,
307 or other asynchronous events, calling malloc() is unsafe!
308
Guido van Rossuma9672091994-09-14 13:31:22 +0000309 Any thread can schedule pending calls, but only the main thread
310 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000311 There is no facility to schedule calls to a particular thread, but
312 that should be easy to change, should that ever be required. In
313 that case, the static variables here should go into the python
314 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000315*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000316
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200317void
318_PyEval_SignalReceived(void)
319{
320 /* bpo-30703: Function called when the C signal handler of Python gets a
321 signal. We cannot queue a callback using Py_AddPendingCall() since
322 that function is not async-signal-safe. */
Eric Snowfdf282d2019-01-11 14:26:55 -0700323 SIGNAL_PENDING_SIGNALS();
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200324}
325
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200326/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000327 scheduling to be made from any thread, and even from an executing
328 callback.
329 */
330
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000331int
332Py_AddPendingCall(int (*func)(void *), void *arg)
333{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 int i, j, result=0;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600335 PyThread_type_lock lock = _PyRuntime.ceval.pending.lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000337 /* try a few times for the lock. Since this mechanism is used
338 * for signal handling (on the main thread), there is a (slim)
339 * chance that a signal is delivered on the same thread while we
340 * hold the lock during the Py_MakePendingCalls() function.
341 * This avoids a deadlock in that case.
342 * Note that signals can be delivered on any thread. In particular,
343 * on Windows, a SIGINT is delivered on a system-created worker
344 * thread.
345 * We also check for lock being NULL, in the unlikely case that
346 * this function is called before any bytecode evaluation takes place.
347 */
348 if (lock != NULL) {
349 for (i = 0; i<100; i++) {
350 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
351 break;
352 }
353 if (i == 100)
354 return -1;
355 }
356
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600357 i = _PyRuntime.ceval.pending.last;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 j = (i + 1) % NPENDINGCALLS;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600359 if (j == _PyRuntime.ceval.pending.first) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 result = -1; /* Queue full */
361 } else {
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600362 _PyRuntime.ceval.pending.calls[i].func = func;
363 _PyRuntime.ceval.pending.calls[i].arg = arg;
364 _PyRuntime.ceval.pending.last = j;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 }
366 /* signal main loop */
367 SIGNAL_PENDING_CALLS();
368 if (lock != NULL)
369 PyThread_release_lock(lock);
370 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000371}
372
Eric Snowfdf282d2019-01-11 14:26:55 -0700373static int
374handle_signals(void)
375{
376 /* Only handle signals on main thread. */
377 if (_PyRuntime.ceval.pending.main_thread &&
378 PyThread_get_thread_ident() != _PyRuntime.ceval.pending.main_thread)
379 {
380 return 0;
381 }
382
383 UNSIGNAL_PENDING_SIGNALS();
384 if (PyErr_CheckSignals() < 0) {
385 SIGNAL_PENDING_SIGNALS(); /* We're not done yet */
386 return -1;
387 }
388 return 0;
389}
390
391static int
392make_pending_calls(void)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000393{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200394 static int busy = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 /* only service pending calls on main thread */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600397 if (_PyRuntime.ceval.pending.main_thread &&
398 PyThread_get_thread_ident() != _PyRuntime.ceval.pending.main_thread)
399 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 return 0;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600401 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 /* don't perform recursive pending calls */
Eric Snowfdf282d2019-01-11 14:26:55 -0700404 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700406 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200407 busy = 1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200408 /* unsignal before starting to call callbacks, so that any callback
409 added in-between re-signals */
410 UNSIGNAL_PENDING_CALLS();
Eric Snowfdf282d2019-01-11 14:26:55 -0700411 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200412
Eric Snowfdf282d2019-01-11 14:26:55 -0700413 if (!_PyRuntime.ceval.pending.lock) {
414 /* initial allocation of the lock */
415 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
416 if (_PyRuntime.ceval.pending.lock == NULL) {
417 res = -1;
418 goto error;
419 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200420 }
421
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++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 int j;
425 int (*func)(void *);
426 void *arg = NULL;
427
428 /* pop one item off the queue while holding the lock */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600429 PyThread_acquire_lock(_PyRuntime.ceval.pending.lock, WAIT_LOCK);
430 j = _PyRuntime.ceval.pending.first;
431 if (j == _PyRuntime.ceval.pending.last) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 func = NULL; /* Queue empty */
433 } else {
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600434 func = _PyRuntime.ceval.pending.calls[j].func;
435 arg = _PyRuntime.ceval.pending.calls[j].arg;
436 _PyRuntime.ceval.pending.first = (j + 1) % NPENDINGCALLS;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600438 PyThread_release_lock(_PyRuntime.ceval.pending.lock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 /* having released the lock, perform the callback */
440 if (func == NULL)
441 break;
Eric Snowfdf282d2019-01-11 14:26:55 -0700442 res = func(arg);
443 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200444 goto error;
445 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200447
Charles-François Natalif23339a2011-07-23 18:15:43 +0200448 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700449 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200450
451error:
452 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700453 SIGNAL_PENDING_CALLS();
454 return res;
455}
456
457/* Py_MakePendingCalls() is a simple wrapper for the sake
458 of backward-compatibility. */
459int
460Py_MakePendingCalls(void)
461{
462 assert(PyGILState_Check());
463
464 /* Python signal handler doesn't really queue a callback: it only signals
465 that a signal was received, see _PyEval_SignalReceived(). */
466 int res = handle_signals();
467 if (res != 0) {
468 return res;
469 }
470
471 res = make_pending_calls();
472 if (res != 0) {
473 return res;
474 }
475
476 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000477}
478
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000479/* The interpreter's recursion limit */
480
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000481#ifndef Py_DEFAULT_RECURSION_LIMIT
482#define Py_DEFAULT_RECURSION_LIMIT 1000
483#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600484
Eric Snow05351c12017-09-05 21:43:08 -0700485int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000486
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600487void
488_PyEval_Initialize(struct _ceval_runtime_state *state)
489{
490 state->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
491 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
492 _gil_initialize(&state->gil);
493}
494
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000495int
496Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000497{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600498 return _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000499}
500
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000501void
502Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000503{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600504 _PyRuntime.ceval.recursion_limit = new_limit;
505 _Py_CheckRecursionLimit = _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000506}
507
Armin Rigo2b3eb402003-10-28 12:05:48 +0000508/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
509 if the recursion_depth reaches _Py_CheckRecursionLimit.
510 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
511 to guarantee that _Py_CheckRecursiveCall() is regularly called.
512 Without USE_STACKCHECK, there is no need for this. */
513int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300514_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000515{
Victor Stinner50b48572018-11-01 01:51:40 +0100516 PyThreadState *tstate = _PyThreadState_GET();
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600517 int recursion_limit = _PyRuntime.ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000518
519#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700520 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 if (PyOS_CheckStack()) {
522 --tstate->recursion_depth;
523 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
524 return -1;
525 }
pdox18967932017-10-25 23:03:01 -0700526 /* Needed for ABI backwards-compatibility (see bpo-31857) */
Eric Snow05351c12017-09-05 21:43:08 -0700527 _Py_CheckRecursionLimit = recursion_limit;
pdox18967932017-10-25 23:03:01 -0700528#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 if (tstate->recursion_critical)
530 /* Somebody asked that we don't check for recursion. */
531 return 0;
532 if (tstate->overflowed) {
533 if (tstate->recursion_depth > recursion_limit + 50) {
534 /* Overflowing while handling an overflow. Give up. */
535 Py_FatalError("Cannot recover from stack overflow.");
536 }
537 return 0;
538 }
539 if (tstate->recursion_depth > recursion_limit) {
540 --tstate->recursion_depth;
541 tstate->overflowed = 1;
Yury Selivanovf488fb42015-07-03 01:04:23 -0400542 PyErr_Format(PyExc_RecursionError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 "maximum recursion depth exceeded%s",
544 where);
545 return -1;
546 }
547 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000548}
549
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400550static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000551static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000552
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600553#define _Py_TracingPossible _PyRuntime.ceval.tracing_possible
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000554
Guido van Rossum374a9221991-04-04 10:40:29 +0000555
Guido van Rossumb209a111997-04-29 18:18:01 +0000556PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000557PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000558{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 return PyEval_EvalCodeEx(co,
560 globals, locals,
561 (PyObject **)NULL, 0,
562 (PyObject **)NULL, 0,
563 (PyObject **)NULL, 0,
564 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000565}
566
567
568/* Interpreter main loop */
569
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000570PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000571PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 /* This is for backward compatibility with extension modules that
573 used this API; core interpreter code should call
574 PyEval_EvalFrameEx() */
575 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000576}
577
578PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000579PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000580{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200581 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
582 return interp->eval_frame(f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700583}
584
Victor Stinnerc6944e72016-11-11 02:13:35 +0100585PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700586_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
587{
Guido van Rossum950361c1997-01-24 13:49:28 +0000588#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000590#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200591 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300592 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200593 int opcode; /* Current opcode */
594 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200595 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 PyObject *retval = NULL; /* Return value */
Victor Stinner50b48572018-11-01 01:51:40 +0100597 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000599
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000601
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 is true when the line being executed has changed. The
605 initial values are such as to make this false the first
606 time it is tested. */
607 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000608
Serhiy Storchakaab874002016-09-11 13:48:15 +0300609 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610 PyObject *names;
611 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000612
Brett Cannon368b4b72012-04-02 12:17:59 -0400613#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200614 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400615#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200616
Antoine Pitroub52ec782009-01-25 16:34:23 +0000617/* Computed GOTOs, or
618 the-optimization-commonly-but-improperly-known-as-"threaded code"
619 using gcc's labels-as-values extension
620 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
621
622 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000624 combined with a lookup table of jump addresses. However, since the
625 indirect jump instruction is shared by all opcodes, the CPU will have a
626 hard time making the right prediction for where to jump next (actually,
627 it will be always wrong except in the uncommon case of a sequence of
628 several identical opcodes).
629
630 "Threaded code" in contrast, uses an explicit jump table and an explicit
631 indirect jump instruction at the end of each opcode. Since the jump
632 instruction is at a different address for each opcode, the CPU will make a
633 separate prediction for each of these instructions, which is equivalent to
634 predicting the second opcode of each opcode pair. These predictions have
635 a much better chance to turn out valid, especially in small bytecode loops.
636
637 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000639 and potentially many more instructions (depending on the pipeline width).
640 A correctly predicted branch, however, is nearly free.
641
642 At the time of this writing, the "threaded code" version is up to 15-20%
643 faster than the normal "switch" version, depending on the compiler and the
644 CPU architecture.
645
646 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
647 because it would render the measurements invalid.
648
649
650 NOTE: care must be taken that the compiler doesn't try to "optimize" the
651 indirect jumps by sharing them between all opcodes. Such optimizations
652 can be disabled on gcc by using the -fno-gcse flag (or possibly
653 -fno-crossjumping).
654*/
655
Antoine Pitrou042b1282010-08-13 21:15:58 +0000656#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000657#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000658#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000659#endif
660
Antoine Pitrou042b1282010-08-13 21:15:58 +0000661#ifdef HAVE_COMPUTED_GOTOS
662 #ifndef USE_COMPUTED_GOTOS
663 #define USE_COMPUTED_GOTOS 1
664 #endif
665#else
666 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
667 #error "Computed gotos are not supported on this compiler."
668 #endif
669 #undef USE_COMPUTED_GOTOS
670 #define USE_COMPUTED_GOTOS 0
671#endif
672
673#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000674/* Import the static jump table */
675#include "opcode_targets.h"
676
Antoine Pitroub52ec782009-01-25 16:34:23 +0000677#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -0700678 op: \
679 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000680
Antoine Pitroub52ec782009-01-25 16:34:23 +0000681#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600683 if (!_Py_atomic_load_relaxed(&_PyRuntime.ceval.eval_breaker)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 FAST_DISPATCH(); \
685 } \
686 continue; \
687 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000688
689#ifdef LLTRACE
690#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700692 if (!lltrace && !_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300694 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300695 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 } \
697 goto fast_next_opcode; \
698 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000699#else
700#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700702 if (!_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300704 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300705 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 } \
707 goto fast_next_opcode; \
708 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000709#endif
710
711#else
Benjamin Petersonddd19492018-09-16 22:38:02 -0700712#define TARGET(op) op
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300713
Antoine Pitroub52ec782009-01-25 16:34:23 +0000714#define DISPATCH() continue
715#define FAST_DISPATCH() goto fast_next_opcode
716#endif
717
718
Neal Norwitza81d2202002-07-14 00:27:26 +0000719/* Tuple access macros */
720
721#ifndef Py_DEBUG
722#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
723#else
724#define GETITEM(v, i) PyTuple_GetItem((v), (i))
725#endif
726
Guido van Rossum374a9221991-04-04 10:40:29 +0000727/* Code access macros */
728
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300729/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600730#define INSTR_OFFSET() \
731 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300732#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300733 _Py_CODEUNIT word = *next_instr; \
734 opcode = _Py_OPCODE(word); \
735 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300736 next_instr++; \
737 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300738#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
739#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000740
Raymond Hettingerf606f872003-03-16 03:11:04 +0000741/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 Some opcodes tend to come in pairs thus making it possible to
743 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300744 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000745
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000746 Verifying the prediction costs a single high-speed test of a register
747 variable against a constant. If the pairing was good, then the
748 processor's own internal branch predication has a high likelihood of
749 success, resulting in a nearly zero-overhead transition to the
750 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300751 including its unpredictable switch-case branch. Combined with the
752 processor's internal branch prediction, a successful PREDICT has the
753 effect of making the two opcodes run as if they were a single new opcode
754 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000755
Georg Brandl86b2fb92008-07-16 03:43:04 +0000756 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 predictions turned-on and interpret the results as if some opcodes
758 had been combined or turn-off predictions so that the opcode frequency
759 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000760
761 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 the CPU to record separate branch prediction information for each
763 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000764
Raymond Hettingerf606f872003-03-16 03:11:04 +0000765*/
766
Antoine Pitrou042b1282010-08-13 21:15:58 +0000767#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000769#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300770#define PREDICT(op) \
771 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300772 _Py_CODEUNIT word = *next_instr; \
773 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300774 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300775 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300776 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300777 goto PRED_##op; \
778 } \
779 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000780#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300781#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000782
Raymond Hettingerf606f872003-03-16 03:11:04 +0000783
Guido van Rossum374a9221991-04-04 10:40:29 +0000784/* Stack manipulation macros */
785
Martin v. Löwis18e16552006-02-15 17:27:45 +0000786/* The stack can grow at most MAXINT deep, as co_nlocals and
787 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000788#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
789#define EMPTY() (STACK_LEVEL() == 0)
790#define TOP() (stack_pointer[-1])
791#define SECOND() (stack_pointer[-2])
792#define THIRD() (stack_pointer[-3])
793#define FOURTH() (stack_pointer[-4])
794#define PEEK(n) (stack_pointer[-(n)])
795#define SET_TOP(v) (stack_pointer[-1] = (v))
796#define SET_SECOND(v) (stack_pointer[-2] = (v))
797#define SET_THIRD(v) (stack_pointer[-3] = (v))
798#define SET_FOURTH(v) (stack_pointer[-4] = (v))
799#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
800#define BASIC_STACKADJ(n) (stack_pointer += n)
801#define BASIC_PUSH(v) (*stack_pointer++ = (v))
802#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000803
Guido van Rossum96a42c81992-01-12 02:29:51 +0000804#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000806 lltrace && prtrace(TOP(), "push")); \
807 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000809 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +0000810#define STACK_GROW(n) do { \
811 assert(n >= 0); \
812 (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000813 lltrace && prtrace(TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +0000814 assert(STACK_LEVEL() <= co->co_stacksize); \
815 } while (0)
816#define STACK_SHRINK(n) do { \
817 assert(n >= 0); \
818 (void)(lltrace && prtrace(TOP(), "stackadj")); \
819 (void)(BASIC_STACKADJ(-n)); \
820 assert(STACK_LEVEL() <= co->co_stacksize); \
821 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +0000822#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +0000823 prtrace((STACK_POINTER)[-1], "ext_pop")), \
824 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000825#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000826#define PUSH(v) BASIC_PUSH(v)
827#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +0000828#define STACK_GROW(n) BASIC_STACKADJ(n)
829#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000830#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000831#endif
832
Guido van Rossum681d79a1995-07-18 14:51:37 +0000833/* Local variable macros */
834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000836
837/* The SETLOCAL() macro must not DECREF the local variable in-place and
838 then store the new value; it must copy the old value to a temporary
839 value, then store the new value, and then DECREF the temporary value.
840 This is because it is possible that during the DECREF the frame is
841 accessed by other code (e.g. a __del__ method or gc.collect()) and the
842 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000844 GETLOCAL(i) = value; \
845 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000846
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000847
848#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 while (STACK_LEVEL() > (b)->b_level) { \
850 PyObject *v = POP(); \
851 Py_XDECREF(v); \
852 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000853
854#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300855 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +0100857 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 assert(STACK_LEVEL() >= (b)->b_level + 3); \
859 while (STACK_LEVEL() > (b)->b_level + 3) { \
860 value = POP(); \
861 Py_XDECREF(value); \
862 } \
Mark Shannonae3087c2017-10-22 22:41:51 +0100863 exc_info = tstate->exc_info; \
864 type = exc_info->exc_type; \
865 value = exc_info->exc_value; \
866 traceback = exc_info->exc_traceback; \
867 exc_info->exc_type = POP(); \
868 exc_info->exc_value = POP(); \
869 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 Py_XDECREF(type); \
871 Py_XDECREF(value); \
872 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300873 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000874
Guido van Rossuma027efa1997-05-05 20:56:21 +0000875/* Start of code */
876
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 /* push frame */
878 if (Py_EnterRecursiveCall(""))
879 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000880
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000882
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 if (tstate->use_tracing) {
884 if (tstate->c_tracefunc != NULL) {
885 /* tstate->c_tracefunc, if defined, is a
886 function that will be called on *every* entry
887 to a code block. Its return value, if not
888 None, is a function that will be called at
889 the start of each executed line of code.
890 (Actually, the function must return itself
891 in order to continue tracing.) The trace
892 functions are called with three arguments:
893 a pointer to the current frame, a string
894 indicating why the function is called, and
895 an argument which depends on the situation.
896 The global trace function is also called
897 whenever an exception is detected. */
898 if (call_trace_protected(tstate->c_tracefunc,
899 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100900 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 /* Trace function raised an error */
902 goto exit_eval_frame;
903 }
904 }
905 if (tstate->c_profilefunc != NULL) {
906 /* Similar for c_profilefunc, except it needn't
907 return itself and isn't called for "line" events */
908 if (call_trace_protected(tstate->c_profilefunc,
909 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100910 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 /* Profile function raised an error */
912 goto exit_eval_frame;
913 }
914 }
915 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000916
Łukasz Langaa785c872016-09-09 17:37:37 -0700917 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
918 dtrace_function_entry(f);
919
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 co = f->f_code;
921 names = co->co_names;
922 consts = co->co_consts;
923 fastlocals = f->f_localsplus;
924 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300925 assert(PyBytes_Check(co->co_code));
926 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +0300927 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
928 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
929 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300930 /*
931 f->f_lasti refers to the index of the last instruction,
932 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000933
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300934 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500935 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 When the PREDICT() macros are enabled, some opcode pairs follow in
938 direct succession without updating f->f_lasti. A successful
939 prediction effectively links the two codes together as if they
940 were a single new opcode; accordingly,f->f_lasti will point to
941 the first code in the pair (for instance, GET_ITER followed by
942 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300943 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300945 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300946 next_instr = first_instr;
947 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +0300948 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
949 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300950 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 stack_pointer = f->f_stacktop;
952 assert(stack_pointer != NULL);
953 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +0200954 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000955
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000956
Tim Peters5ca576e2001-06-18 22:08:13 +0000957#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200958 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000959#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000960
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400961 if (throwflag) /* support for generator.throw() */
962 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000963
Victor Stinnerace47d72013-07-18 01:41:08 +0200964#ifdef Py_DEBUG
965 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +0100966 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +0000967 caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +0200968 assert(!PyErr_Occurred());
969#endif
970
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200971main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 assert(stack_pointer >= f->f_valuestack); /* else underflow */
974 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +0200975 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000976
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 /* Do periodic things. Doing this every time through
978 the loop would add too much overhead, so we do it
979 only every Nth instruction. We also do it if
980 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
981 event needs attention (e.g. a signal handler or
982 async I/O handler); see Py_AddPendingCall() and
983 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000984
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600985 if (_Py_atomic_load_relaxed(&_PyRuntime.ceval.eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +0300986 opcode = _Py_OPCODE(*next_instr);
987 if (opcode == SETUP_FINALLY ||
988 opcode == SETUP_WITH ||
989 opcode == BEFORE_ASYNC_WITH ||
990 opcode == YIELD_FROM) {
991 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -0700992 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +0300993 - If we're about to enter the 'with:'. It will prevent
994 emitting a resource warning in the common idiom
995 'with open(path) as file:'.
996 - If we're about to enter the 'async with:'.
997 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -0700998 *very* useful, but might help in some cases and it's
999 traditional)
1000 - If we're resuming a chain of nested 'yield from' or
1001 'await' calls, then each frame is parked with YIELD_FROM
1002 as its next opcode. If the user hit control-C we want to
1003 wait until we've reached the innermost frame before
1004 running the signal handler and raising KeyboardInterrupt
1005 (see bpo-30039).
1006 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 goto fast_next_opcode;
1008 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001009
1010 if (_Py_atomic_load_relaxed(
1011 &_PyRuntime.ceval.signals_pending))
1012 {
1013 if (handle_signals() != 0) {
1014 goto error;
1015 }
1016 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001017 if (_Py_atomic_load_relaxed(
1018 &_PyRuntime.ceval.pending.calls_to_do))
1019 {
Eric Snowfdf282d2019-01-11 14:26:55 -07001020 if (make_pending_calls() != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001021 goto error;
Eric Snowfdf282d2019-01-11 14:26:55 -07001022 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001024
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001025 if (_Py_atomic_load_relaxed(
1026 &_PyRuntime.ceval.gil_drop_request))
1027 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 /* Give another thread a chance */
1029 if (PyThreadState_Swap(NULL) != tstate)
1030 Py_FatalError("ceval: tstate mix-up");
1031 drop_gil(tstate);
1032
1033 /* Other threads may run now */
1034
1035 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001036
1037 /* Check if we should make a quick exit. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001038 if (_Py_IsFinalizing() &&
1039 !_Py_CURRENTLY_FINALIZING(tstate))
1040 {
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001041 drop_gil(tstate);
1042 PyThread_exit_thread();
1043 }
1044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 if (PyThreadState_Swap(tstate) != NULL)
1046 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 }
1048 /* Check for asynchronous exceptions. */
1049 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001050 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 tstate->async_exc = NULL;
1052 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001053 PyErr_SetNone(exc);
1054 Py_DECREF(exc);
1055 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 }
1057 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 fast_next_opcode:
1060 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001061
Łukasz Langaa785c872016-09-09 17:37:37 -07001062 if (PyDTrace_LINE_ENABLED())
1063 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001068 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001069 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 /* see maybe_call_line_trace
1071 for expository comments */
1072 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 err = maybe_call_line_trace(tstate->c_tracefunc,
1075 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001076 tstate, f,
1077 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 /* Reload possibly changed frame fields */
1079 JUMPTO(f->f_lasti);
1080 if (f->f_stacktop != NULL) {
1081 stack_pointer = f->f_stacktop;
1082 f->f_stacktop = NULL;
1083 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001084 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001086 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001088
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001090
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001091 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001092 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001093#ifdef DYNAMIC_EXECUTION_PROFILE
1094#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 dxpairs[lastopcode][opcode]++;
1096 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001097#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001099#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001100
Guido van Rossum96a42c81992-01-12 02:29:51 +00001101#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001102 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001103
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 if (lltrace) {
1105 if (HAS_ARG(opcode)) {
1106 printf("%d: %d, %d\n",
1107 f->f_lasti, opcode, oparg);
1108 }
1109 else {
1110 printf("%d: %d\n",
1111 f->f_lasti, opcode);
1112 }
1113 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001114#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001117
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001119 It is essential that any operation that fails must goto error
1120 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001121
Benjamin Petersonddd19492018-09-16 22:38:02 -07001122 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001124 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001125
Benjamin Petersonddd19492018-09-16 22:38:02 -07001126 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001127 PyObject *value = GETLOCAL(oparg);
1128 if (value == NULL) {
1129 format_exc_check_arg(PyExc_UnboundLocalError,
1130 UNBOUNDLOCAL_ERROR_MSG,
1131 PyTuple_GetItem(co->co_varnames, oparg));
1132 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001134 Py_INCREF(value);
1135 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001136 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001137 }
1138
Benjamin Petersonddd19492018-09-16 22:38:02 -07001139 case TARGET(LOAD_CONST): {
1140 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001141 PyObject *value = GETITEM(consts, oparg);
1142 Py_INCREF(value);
1143 PUSH(value);
1144 FAST_DISPATCH();
1145 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001146
Benjamin Petersonddd19492018-09-16 22:38:02 -07001147 case TARGET(STORE_FAST): {
1148 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001149 PyObject *value = POP();
1150 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001152 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001153
Benjamin Petersonddd19492018-09-16 22:38:02 -07001154 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001155 PyObject *value = POP();
1156 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001157 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001158 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001159
Benjamin Petersonddd19492018-09-16 22:38:02 -07001160 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001161 PyObject *top = TOP();
1162 PyObject *second = SECOND();
1163 SET_TOP(second);
1164 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001165 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001166 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001167
Benjamin Petersonddd19492018-09-16 22:38:02 -07001168 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001169 PyObject *top = TOP();
1170 PyObject *second = SECOND();
1171 PyObject *third = THIRD();
1172 SET_TOP(second);
1173 SET_SECOND(third);
1174 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001176 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001177
Benjamin Petersonddd19492018-09-16 22:38:02 -07001178 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001179 PyObject *top = TOP();
1180 PyObject *second = SECOND();
1181 PyObject *third = THIRD();
1182 PyObject *fourth = FOURTH();
1183 SET_TOP(second);
1184 SET_SECOND(third);
1185 SET_THIRD(fourth);
1186 SET_FOURTH(top);
1187 FAST_DISPATCH();
1188 }
1189
Benjamin Petersonddd19492018-09-16 22:38:02 -07001190 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001191 PyObject *top = TOP();
1192 Py_INCREF(top);
1193 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001195 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001196
Benjamin Petersonddd19492018-09-16 22:38:02 -07001197 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001198 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001199 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001200 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001201 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001202 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001203 SET_TOP(top);
1204 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001205 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001206 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001207
Benjamin Petersonddd19492018-09-16 22:38:02 -07001208 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001209 PyObject *value = TOP();
1210 PyObject *res = PyNumber_Positive(value);
1211 Py_DECREF(value);
1212 SET_TOP(res);
1213 if (res == NULL)
1214 goto error;
1215 DISPATCH();
1216 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001217
Benjamin Petersonddd19492018-09-16 22:38:02 -07001218 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001219 PyObject *value = TOP();
1220 PyObject *res = PyNumber_Negative(value);
1221 Py_DECREF(value);
1222 SET_TOP(res);
1223 if (res == NULL)
1224 goto error;
1225 DISPATCH();
1226 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001227
Benjamin Petersonddd19492018-09-16 22:38:02 -07001228 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001229 PyObject *value = TOP();
1230 int err = PyObject_IsTrue(value);
1231 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 if (err == 0) {
1233 Py_INCREF(Py_True);
1234 SET_TOP(Py_True);
1235 DISPATCH();
1236 }
1237 else if (err > 0) {
1238 Py_INCREF(Py_False);
1239 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 DISPATCH();
1241 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001242 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001243 goto error;
1244 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001245
Benjamin Petersonddd19492018-09-16 22:38:02 -07001246 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001247 PyObject *value = TOP();
1248 PyObject *res = PyNumber_Invert(value);
1249 Py_DECREF(value);
1250 SET_TOP(res);
1251 if (res == NULL)
1252 goto error;
1253 DISPATCH();
1254 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001255
Benjamin Petersonddd19492018-09-16 22:38:02 -07001256 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001257 PyObject *exp = POP();
1258 PyObject *base = TOP();
1259 PyObject *res = PyNumber_Power(base, exp, Py_None);
1260 Py_DECREF(base);
1261 Py_DECREF(exp);
1262 SET_TOP(res);
1263 if (res == NULL)
1264 goto error;
1265 DISPATCH();
1266 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001267
Benjamin Petersonddd19492018-09-16 22:38:02 -07001268 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001269 PyObject *right = POP();
1270 PyObject *left = TOP();
1271 PyObject *res = PyNumber_Multiply(left, right);
1272 Py_DECREF(left);
1273 Py_DECREF(right);
1274 SET_TOP(res);
1275 if (res == NULL)
1276 goto error;
1277 DISPATCH();
1278 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001279
Benjamin Petersonddd19492018-09-16 22:38:02 -07001280 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001281 PyObject *right = POP();
1282 PyObject *left = TOP();
1283 PyObject *res = PyNumber_MatrixMultiply(left, right);
1284 Py_DECREF(left);
1285 Py_DECREF(right);
1286 SET_TOP(res);
1287 if (res == NULL)
1288 goto error;
1289 DISPATCH();
1290 }
1291
Benjamin Petersonddd19492018-09-16 22:38:02 -07001292 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001293 PyObject *divisor = POP();
1294 PyObject *dividend = TOP();
1295 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1296 Py_DECREF(dividend);
1297 Py_DECREF(divisor);
1298 SET_TOP(quotient);
1299 if (quotient == NULL)
1300 goto error;
1301 DISPATCH();
1302 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001303
Benjamin Petersonddd19492018-09-16 22:38:02 -07001304 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001305 PyObject *divisor = POP();
1306 PyObject *dividend = TOP();
1307 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1308 Py_DECREF(dividend);
1309 Py_DECREF(divisor);
1310 SET_TOP(quotient);
1311 if (quotient == NULL)
1312 goto error;
1313 DISPATCH();
1314 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001315
Benjamin Petersonddd19492018-09-16 22:38:02 -07001316 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001317 PyObject *divisor = POP();
1318 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001319 PyObject *res;
1320 if (PyUnicode_CheckExact(dividend) && (
1321 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1322 // fast path; string formatting, but not if the RHS is a str subclass
1323 // (see issue28598)
1324 res = PyUnicode_Format(dividend, divisor);
1325 } else {
1326 res = PyNumber_Remainder(dividend, divisor);
1327 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001328 Py_DECREF(divisor);
1329 Py_DECREF(dividend);
1330 SET_TOP(res);
1331 if (res == NULL)
1332 goto error;
1333 DISPATCH();
1334 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001335
Benjamin Petersonddd19492018-09-16 22:38:02 -07001336 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001337 PyObject *right = POP();
1338 PyObject *left = TOP();
1339 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001340 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1341 CPython using bytecode, it is simply worthless.
1342 See http://bugs.python.org/issue21955 and
1343 http://bugs.python.org/issue10044 for the discussion. In short,
1344 no patch shown any impact on a realistic benchmark, only a minor
1345 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001346 if (PyUnicode_CheckExact(left) &&
1347 PyUnicode_CheckExact(right)) {
1348 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001349 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001350 }
1351 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001352 sum = PyNumber_Add(left, right);
1353 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001354 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001355 Py_DECREF(right);
1356 SET_TOP(sum);
1357 if (sum == NULL)
1358 goto error;
1359 DISPATCH();
1360 }
1361
Benjamin Petersonddd19492018-09-16 22:38:02 -07001362 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001363 PyObject *right = POP();
1364 PyObject *left = TOP();
1365 PyObject *diff = PyNumber_Subtract(left, right);
1366 Py_DECREF(right);
1367 Py_DECREF(left);
1368 SET_TOP(diff);
1369 if (diff == NULL)
1370 goto error;
1371 DISPATCH();
1372 }
1373
Benjamin Petersonddd19492018-09-16 22:38:02 -07001374 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001375 PyObject *sub = POP();
1376 PyObject *container = TOP();
1377 PyObject *res = PyObject_GetItem(container, sub);
1378 Py_DECREF(container);
1379 Py_DECREF(sub);
1380 SET_TOP(res);
1381 if (res == NULL)
1382 goto error;
1383 DISPATCH();
1384 }
1385
Benjamin Petersonddd19492018-09-16 22:38:02 -07001386 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001387 PyObject *right = POP();
1388 PyObject *left = TOP();
1389 PyObject *res = PyNumber_Lshift(left, right);
1390 Py_DECREF(left);
1391 Py_DECREF(right);
1392 SET_TOP(res);
1393 if (res == NULL)
1394 goto error;
1395 DISPATCH();
1396 }
1397
Benjamin Petersonddd19492018-09-16 22:38:02 -07001398 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001399 PyObject *right = POP();
1400 PyObject *left = TOP();
1401 PyObject *res = PyNumber_Rshift(left, right);
1402 Py_DECREF(left);
1403 Py_DECREF(right);
1404 SET_TOP(res);
1405 if (res == NULL)
1406 goto error;
1407 DISPATCH();
1408 }
1409
Benjamin Petersonddd19492018-09-16 22:38:02 -07001410 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001411 PyObject *right = POP();
1412 PyObject *left = TOP();
1413 PyObject *res = PyNumber_And(left, right);
1414 Py_DECREF(left);
1415 Py_DECREF(right);
1416 SET_TOP(res);
1417 if (res == NULL)
1418 goto error;
1419 DISPATCH();
1420 }
1421
Benjamin Petersonddd19492018-09-16 22:38:02 -07001422 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001423 PyObject *right = POP();
1424 PyObject *left = TOP();
1425 PyObject *res = PyNumber_Xor(left, right);
1426 Py_DECREF(left);
1427 Py_DECREF(right);
1428 SET_TOP(res);
1429 if (res == NULL)
1430 goto error;
1431 DISPATCH();
1432 }
1433
Benjamin Petersonddd19492018-09-16 22:38:02 -07001434 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001435 PyObject *right = POP();
1436 PyObject *left = TOP();
1437 PyObject *res = PyNumber_Or(left, right);
1438 Py_DECREF(left);
1439 Py_DECREF(right);
1440 SET_TOP(res);
1441 if (res == NULL)
1442 goto error;
1443 DISPATCH();
1444 }
1445
Benjamin Petersonddd19492018-09-16 22:38:02 -07001446 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001447 PyObject *v = POP();
1448 PyObject *list = PEEK(oparg);
1449 int err;
1450 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001451 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001452 if (err != 0)
1453 goto error;
1454 PREDICT(JUMP_ABSOLUTE);
1455 DISPATCH();
1456 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001457
Benjamin Petersonddd19492018-09-16 22:38:02 -07001458 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001459 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001460 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001461 int err;
1462 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001463 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001464 if (err != 0)
1465 goto error;
1466 PREDICT(JUMP_ABSOLUTE);
1467 DISPATCH();
1468 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001469
Benjamin Petersonddd19492018-09-16 22:38:02 -07001470 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001471 PyObject *exp = POP();
1472 PyObject *base = TOP();
1473 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1474 Py_DECREF(base);
1475 Py_DECREF(exp);
1476 SET_TOP(res);
1477 if (res == NULL)
1478 goto error;
1479 DISPATCH();
1480 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001481
Benjamin Petersonddd19492018-09-16 22:38:02 -07001482 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001483 PyObject *right = POP();
1484 PyObject *left = TOP();
1485 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1486 Py_DECREF(left);
1487 Py_DECREF(right);
1488 SET_TOP(res);
1489 if (res == NULL)
1490 goto error;
1491 DISPATCH();
1492 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001493
Benjamin Petersonddd19492018-09-16 22:38:02 -07001494 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001495 PyObject *right = POP();
1496 PyObject *left = TOP();
1497 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1498 Py_DECREF(left);
1499 Py_DECREF(right);
1500 SET_TOP(res);
1501 if (res == NULL)
1502 goto error;
1503 DISPATCH();
1504 }
1505
Benjamin Petersonddd19492018-09-16 22:38:02 -07001506 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001507 PyObject *divisor = POP();
1508 PyObject *dividend = TOP();
1509 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1510 Py_DECREF(dividend);
1511 Py_DECREF(divisor);
1512 SET_TOP(quotient);
1513 if (quotient == NULL)
1514 goto error;
1515 DISPATCH();
1516 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001517
Benjamin Petersonddd19492018-09-16 22:38:02 -07001518 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001519 PyObject *divisor = POP();
1520 PyObject *dividend = TOP();
1521 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1522 Py_DECREF(dividend);
1523 Py_DECREF(divisor);
1524 SET_TOP(quotient);
1525 if (quotient == NULL)
1526 goto error;
1527 DISPATCH();
1528 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001529
Benjamin Petersonddd19492018-09-16 22:38:02 -07001530 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001531 PyObject *right = POP();
1532 PyObject *left = TOP();
1533 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1534 Py_DECREF(left);
1535 Py_DECREF(right);
1536 SET_TOP(mod);
1537 if (mod == NULL)
1538 goto error;
1539 DISPATCH();
1540 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001541
Benjamin Petersonddd19492018-09-16 22:38:02 -07001542 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001543 PyObject *right = POP();
1544 PyObject *left = TOP();
1545 PyObject *sum;
1546 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1547 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001548 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001549 }
1550 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001551 sum = PyNumber_InPlaceAdd(left, right);
1552 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001553 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001554 Py_DECREF(right);
1555 SET_TOP(sum);
1556 if (sum == NULL)
1557 goto error;
1558 DISPATCH();
1559 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001560
Benjamin Petersonddd19492018-09-16 22:38:02 -07001561 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001562 PyObject *right = POP();
1563 PyObject *left = TOP();
1564 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1565 Py_DECREF(left);
1566 Py_DECREF(right);
1567 SET_TOP(diff);
1568 if (diff == NULL)
1569 goto error;
1570 DISPATCH();
1571 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001572
Benjamin Petersonddd19492018-09-16 22:38:02 -07001573 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001574 PyObject *right = POP();
1575 PyObject *left = TOP();
1576 PyObject *res = PyNumber_InPlaceLshift(left, right);
1577 Py_DECREF(left);
1578 Py_DECREF(right);
1579 SET_TOP(res);
1580 if (res == NULL)
1581 goto error;
1582 DISPATCH();
1583 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001584
Benjamin Petersonddd19492018-09-16 22:38:02 -07001585 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001586 PyObject *right = POP();
1587 PyObject *left = TOP();
1588 PyObject *res = PyNumber_InPlaceRshift(left, right);
1589 Py_DECREF(left);
1590 Py_DECREF(right);
1591 SET_TOP(res);
1592 if (res == NULL)
1593 goto error;
1594 DISPATCH();
1595 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001596
Benjamin Petersonddd19492018-09-16 22:38:02 -07001597 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001598 PyObject *right = POP();
1599 PyObject *left = TOP();
1600 PyObject *res = PyNumber_InPlaceAnd(left, right);
1601 Py_DECREF(left);
1602 Py_DECREF(right);
1603 SET_TOP(res);
1604 if (res == NULL)
1605 goto error;
1606 DISPATCH();
1607 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001608
Benjamin Petersonddd19492018-09-16 22:38:02 -07001609 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001610 PyObject *right = POP();
1611 PyObject *left = TOP();
1612 PyObject *res = PyNumber_InPlaceXor(left, right);
1613 Py_DECREF(left);
1614 Py_DECREF(right);
1615 SET_TOP(res);
1616 if (res == NULL)
1617 goto error;
1618 DISPATCH();
1619 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001620
Benjamin Petersonddd19492018-09-16 22:38:02 -07001621 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001622 PyObject *right = POP();
1623 PyObject *left = TOP();
1624 PyObject *res = PyNumber_InPlaceOr(left, right);
1625 Py_DECREF(left);
1626 Py_DECREF(right);
1627 SET_TOP(res);
1628 if (res == NULL)
1629 goto error;
1630 DISPATCH();
1631 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001632
Benjamin Petersonddd19492018-09-16 22:38:02 -07001633 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001634 PyObject *sub = TOP();
1635 PyObject *container = SECOND();
1636 PyObject *v = THIRD();
1637 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001638 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001639 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001640 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001642 Py_DECREF(container);
1643 Py_DECREF(sub);
1644 if (err != 0)
1645 goto error;
1646 DISPATCH();
1647 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001648
Benjamin Petersonddd19492018-09-16 22:38:02 -07001649 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001650 PyObject *sub = TOP();
1651 PyObject *container = SECOND();
1652 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001653 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00001654 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001655 err = PyObject_DelItem(container, sub);
1656 Py_DECREF(container);
1657 Py_DECREF(sub);
1658 if (err != 0)
1659 goto error;
1660 DISPATCH();
1661 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001662
Benjamin Petersonddd19492018-09-16 22:38:02 -07001663 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01001664 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001665 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001666 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001667 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001668 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 PyErr_SetString(PyExc_RuntimeError,
1670 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001671 Py_DECREF(value);
1672 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001673 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001674 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001675 Py_DECREF(value);
1676 if (res == NULL)
1677 goto error;
1678 Py_DECREF(res);
1679 DISPATCH();
1680 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001681
Benjamin Petersonddd19492018-09-16 22:38:02 -07001682 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001683 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 switch (oparg) {
1685 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001686 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001687 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001689 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001690 /* fall through */
1691 case 0:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001692 if (do_raise(exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001693 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001694 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001695 break;
1696 default:
1697 PyErr_SetString(PyExc_SystemError,
1698 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001699 break;
1700 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001701 goto error;
1702 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001703
Benjamin Petersonddd19492018-09-16 22:38:02 -07001704 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001705 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001706 assert(f->f_iblock == 0);
1707 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001708 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001709
Benjamin Petersonddd19492018-09-16 22:38:02 -07001710 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001711 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001712 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001713 PyObject *obj = TOP();
1714 PyTypeObject *type = Py_TYPE(obj);
1715
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001716 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001717 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001718 }
Yury Selivanov75445082015-05-11 22:57:16 -04001719
1720 if (getter != NULL) {
1721 iter = (*getter)(obj);
1722 Py_DECREF(obj);
1723 if (iter == NULL) {
1724 SET_TOP(NULL);
1725 goto error;
1726 }
1727 }
1728 else {
1729 SET_TOP(NULL);
1730 PyErr_Format(
1731 PyExc_TypeError,
1732 "'async for' requires an object with "
1733 "__aiter__ method, got %.100s",
1734 type->tp_name);
1735 Py_DECREF(obj);
1736 goto error;
1737 }
1738
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001739 if (Py_TYPE(iter)->tp_as_async == NULL ||
1740 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001741
Yury Selivanov398ff912017-03-02 22:20:00 -05001742 SET_TOP(NULL);
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001743 PyErr_Format(
1744 PyExc_TypeError,
1745 "'async for' received an object from __aiter__ "
1746 "that does not implement __anext__: %.100s",
1747 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001748 Py_DECREF(iter);
1749 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001750 }
1751
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001752 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001753 DISPATCH();
1754 }
1755
Benjamin Petersonddd19492018-09-16 22:38:02 -07001756 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001757 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001758 PyObject *next_iter = NULL;
1759 PyObject *awaitable = NULL;
1760 PyObject *aiter = TOP();
1761 PyTypeObject *type = Py_TYPE(aiter);
1762
Yury Selivanoveb636452016-09-08 22:01:51 -07001763 if (PyAsyncGen_CheckExact(aiter)) {
1764 awaitable = type->tp_as_async->am_anext(aiter);
1765 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001766 goto error;
1767 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001768 } else {
1769 if (type->tp_as_async != NULL){
1770 getter = type->tp_as_async->am_anext;
1771 }
Yury Selivanov75445082015-05-11 22:57:16 -04001772
Yury Selivanoveb636452016-09-08 22:01:51 -07001773 if (getter != NULL) {
1774 next_iter = (*getter)(aiter);
1775 if (next_iter == NULL) {
1776 goto error;
1777 }
1778 }
1779 else {
1780 PyErr_Format(
1781 PyExc_TypeError,
1782 "'async for' requires an iterator with "
1783 "__anext__ method, got %.100s",
1784 type->tp_name);
1785 goto error;
1786 }
Yury Selivanov75445082015-05-11 22:57:16 -04001787
Yury Selivanoveb636452016-09-08 22:01:51 -07001788 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1789 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001790 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001791 PyExc_TypeError,
1792 "'async for' received an invalid object "
1793 "from __anext__: %.100s",
1794 Py_TYPE(next_iter)->tp_name);
1795
1796 Py_DECREF(next_iter);
1797 goto error;
1798 } else {
1799 Py_DECREF(next_iter);
1800 }
1801 }
Yury Selivanov75445082015-05-11 22:57:16 -04001802
1803 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001804 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001805 DISPATCH();
1806 }
1807
Benjamin Petersonddd19492018-09-16 22:38:02 -07001808 case TARGET(GET_AWAITABLE): {
1809 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04001810 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04001811 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04001812
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03001813 if (iter == NULL) {
1814 format_awaitable_error(Py_TYPE(iterable),
1815 _Py_OPCODE(next_instr[-2]));
1816 }
1817
Yury Selivanov75445082015-05-11 22:57:16 -04001818 Py_DECREF(iterable);
1819
Yury Selivanovc724bae2016-03-02 11:30:46 -05001820 if (iter != NULL && PyCoro_CheckExact(iter)) {
1821 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
1822 if (yf != NULL) {
1823 /* `iter` is a coroutine object that is being
1824 awaited, `yf` is a pointer to the current awaitable
1825 being awaited on. */
1826 Py_DECREF(yf);
1827 Py_CLEAR(iter);
1828 PyErr_SetString(
1829 PyExc_RuntimeError,
1830 "coroutine is being awaited already");
1831 /* The code below jumps to `error` if `iter` is NULL. */
1832 }
1833 }
1834
Yury Selivanov75445082015-05-11 22:57:16 -04001835 SET_TOP(iter); /* Even if it's NULL */
1836
1837 if (iter == NULL) {
1838 goto error;
1839 }
1840
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001841 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001842 DISPATCH();
1843 }
1844
Benjamin Petersonddd19492018-09-16 22:38:02 -07001845 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001846 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001847 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001848 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001849 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
1850 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001851 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001852 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001853 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001854 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001855 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001856 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001857 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001858 Py_DECREF(v);
1859 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001860 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08001861 if (tstate->c_tracefunc != NULL
1862 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001863 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10001864 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001865 if (err < 0)
1866 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001867 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001868 SET_TOP(val);
1869 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001870 }
Martin Panter95f53c12016-07-18 08:23:26 +00001871 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001872 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001873 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01001874 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03001875 f->f_lasti -= sizeof(_Py_CODEUNIT);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001876 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001877 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001878
Benjamin Petersonddd19492018-09-16 22:38:02 -07001879 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001880 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07001881
1882 if (co->co_flags & CO_ASYNC_GENERATOR) {
1883 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
1884 Py_DECREF(retval);
1885 if (w == NULL) {
1886 retval = NULL;
1887 goto error;
1888 }
1889 retval = w;
1890 }
1891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 f->f_stacktop = stack_pointer;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001893 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001894 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001895
Benjamin Petersonddd19492018-09-16 22:38:02 -07001896 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001897 PyObject *type, *value, *traceback;
1898 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001899 PyTryBlock *b = PyFrame_BlockPop(f);
1900 if (b->b_type != EXCEPT_HANDLER) {
1901 PyErr_SetString(PyExc_SystemError,
1902 "popped block is not an except handler");
1903 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001904 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001905 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
1906 STACK_LEVEL() <= (b)->b_level + 4);
1907 exc_info = tstate->exc_info;
1908 type = exc_info->exc_type;
1909 value = exc_info->exc_value;
1910 traceback = exc_info->exc_traceback;
1911 exc_info->exc_type = POP();
1912 exc_info->exc_value = POP();
1913 exc_info->exc_traceback = POP();
1914 Py_XDECREF(type);
1915 Py_XDECREF(value);
1916 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001917 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001918 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001919
Benjamin Petersonddd19492018-09-16 22:38:02 -07001920 case TARGET(POP_BLOCK): {
1921 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001922 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001923 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001924 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001925
Benjamin Petersonddd19492018-09-16 22:38:02 -07001926 case TARGET(POP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001927 /* If oparg is 0 at the top of the stack are 1 or 6 values:
1928 Either:
1929 - TOP = NULL or an integer
1930 or:
1931 - (TOP, SECOND, THIRD) = exc_info()
1932 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
1933
1934 If oparg is 1 the value for 'return' was additionally pushed
1935 at the top of the stack.
1936 */
1937 PyObject *res = NULL;
1938 if (oparg) {
1939 res = POP();
1940 }
1941 PyObject *exc = POP();
1942 if (exc == NULL || PyLong_CheckExact(exc)) {
1943 Py_XDECREF(exc);
1944 }
1945 else {
1946 Py_DECREF(exc);
1947 Py_DECREF(POP());
1948 Py_DECREF(POP());
1949
1950 PyObject *type, *value, *traceback;
1951 _PyErr_StackItem *exc_info;
1952 PyTryBlock *b = PyFrame_BlockPop(f);
1953 if (b->b_type != EXCEPT_HANDLER) {
1954 PyErr_SetString(PyExc_SystemError,
1955 "popped block is not an except handler");
1956 Py_XDECREF(res);
1957 goto error;
1958 }
1959 assert(STACK_LEVEL() == (b)->b_level + 3);
1960 exc_info = tstate->exc_info;
1961 type = exc_info->exc_type;
1962 value = exc_info->exc_value;
1963 traceback = exc_info->exc_traceback;
1964 exc_info->exc_type = POP();
1965 exc_info->exc_value = POP();
1966 exc_info->exc_traceback = POP();
1967 Py_XDECREF(type);
1968 Py_XDECREF(value);
1969 Py_XDECREF(traceback);
1970 }
1971 if (oparg) {
1972 PUSH(res);
1973 }
1974 DISPATCH();
1975 }
1976
Benjamin Petersonddd19492018-09-16 22:38:02 -07001977 case TARGET(CALL_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001978 PyObject *ret = PyLong_FromLong(INSTR_OFFSET());
1979 if (ret == NULL) {
1980 goto error;
1981 }
1982 PUSH(ret);
1983 JUMPBY(oparg);
1984 FAST_DISPATCH();
1985 }
1986
Benjamin Petersonddd19492018-09-16 22:38:02 -07001987 case TARGET(BEGIN_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001988 /* Push NULL onto the stack for using it in END_FINALLY,
1989 POP_FINALLY, WITH_CLEANUP_START and WITH_CLEANUP_FINISH.
1990 */
1991 PUSH(NULL);
1992 FAST_DISPATCH();
1993 }
1994
Benjamin Petersonddd19492018-09-16 22:38:02 -07001995 case TARGET(END_FINALLY): {
1996 PREDICTED(END_FINALLY);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001997 /* At the top of the stack are 1 or 6 values:
1998 Either:
1999 - TOP = NULL or an integer
2000 or:
2001 - (TOP, SECOND, THIRD) = exc_info()
2002 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2003 */
2004 PyObject *exc = POP();
2005 if (exc == NULL) {
2006 FAST_DISPATCH();
2007 }
2008 else if (PyLong_CheckExact(exc)) {
2009 int ret = _PyLong_AsInt(exc);
2010 Py_DECREF(exc);
2011 if (ret == -1 && PyErr_Occurred()) {
2012 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002013 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002014 JUMPTO(ret);
2015 FAST_DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002016 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002017 else {
2018 assert(PyExceptionClass_Check(exc));
2019 PyObject *val = POP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002020 PyObject *tb = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002021 PyErr_Restore(exc, val, tb);
2022 goto exception_unwind;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002023 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002024 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002025
Benjamin Petersonddd19492018-09-16 22:38:02 -07002026 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002027 PyObject *exc = POP();
2028 assert(PyExceptionClass_Check(exc));
2029 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2030 PyTryBlock *b = PyFrame_BlockPop(f);
2031 assert(b->b_type == EXCEPT_HANDLER);
2032 Py_DECREF(exc);
2033 UNWIND_EXCEPT_HANDLER(b);
2034 Py_DECREF(POP());
2035 JUMPBY(oparg);
2036 FAST_DISPATCH();
2037 }
2038 else {
2039 PyObject *val = POP();
2040 PyObject *tb = POP();
2041 PyErr_Restore(exc, val, tb);
2042 goto exception_unwind;
2043 }
2044 }
2045
Benjamin Petersonddd19492018-09-16 22:38:02 -07002046 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002047 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002048
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002049 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002050 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002051 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
2052 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002053 PyErr_SetString(PyExc_NameError,
2054 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002055 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002056 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002057 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002058 }
2059 else {
2060 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2061 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002062 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002063 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2064 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002065 if (PyErr_ExceptionMatches(PyExc_KeyError))
2066 PyErr_SetString(PyExc_NameError,
2067 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002068 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002069 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002070 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002071 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002072 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002073 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002074
Benjamin Petersonddd19492018-09-16 22:38:02 -07002075 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002076 PyObject *name = GETITEM(names, oparg);
2077 PyObject *v = POP();
2078 PyObject *ns = f->f_locals;
2079 int err;
2080 if (ns == NULL) {
2081 PyErr_Format(PyExc_SystemError,
2082 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002083 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002084 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002085 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002086 if (PyDict_CheckExact(ns))
2087 err = PyDict_SetItem(ns, name, v);
2088 else
2089 err = PyObject_SetItem(ns, name, v);
2090 Py_DECREF(v);
2091 if (err != 0)
2092 goto error;
2093 DISPATCH();
2094 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002095
Benjamin Petersonddd19492018-09-16 22:38:02 -07002096 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002097 PyObject *name = GETITEM(names, oparg);
2098 PyObject *ns = f->f_locals;
2099 int err;
2100 if (ns == NULL) {
2101 PyErr_Format(PyExc_SystemError,
2102 "no locals when deleting %R", name);
2103 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002104 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002105 err = PyObject_DelItem(ns, name);
2106 if (err != 0) {
2107 format_exc_check_arg(PyExc_NameError,
2108 NAME_ERROR_MSG,
2109 name);
2110 goto error;
2111 }
2112 DISPATCH();
2113 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002114
Benjamin Petersonddd19492018-09-16 22:38:02 -07002115 case TARGET(UNPACK_SEQUENCE): {
2116 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002117 PyObject *seq = POP(), *item, **items;
2118 if (PyTuple_CheckExact(seq) &&
2119 PyTuple_GET_SIZE(seq) == oparg) {
2120 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002121 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002122 item = items[oparg];
2123 Py_INCREF(item);
2124 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002125 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002126 } else if (PyList_CheckExact(seq) &&
2127 PyList_GET_SIZE(seq) == oparg) {
2128 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002129 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002130 item = items[oparg];
2131 Py_INCREF(item);
2132 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002133 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002134 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002135 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002136 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002137 } else {
2138 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002139 Py_DECREF(seq);
2140 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002141 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002142 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002143 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002144 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002145
Benjamin Petersonddd19492018-09-16 22:38:02 -07002146 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002147 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2148 PyObject *seq = POP();
2149
2150 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2151 stack_pointer + totalargs)) {
2152 stack_pointer += totalargs;
2153 } else {
2154 Py_DECREF(seq);
2155 goto error;
2156 }
2157 Py_DECREF(seq);
2158 DISPATCH();
2159 }
2160
Benjamin Petersonddd19492018-09-16 22:38:02 -07002161 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002162 PyObject *name = GETITEM(names, oparg);
2163 PyObject *owner = TOP();
2164 PyObject *v = SECOND();
2165 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002166 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002167 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002168 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002169 Py_DECREF(owner);
2170 if (err != 0)
2171 goto error;
2172 DISPATCH();
2173 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002174
Benjamin Petersonddd19492018-09-16 22:38:02 -07002175 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002176 PyObject *name = GETITEM(names, oparg);
2177 PyObject *owner = POP();
2178 int err;
2179 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2180 Py_DECREF(owner);
2181 if (err != 0)
2182 goto error;
2183 DISPATCH();
2184 }
2185
Benjamin Petersonddd19492018-09-16 22:38:02 -07002186 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002187 PyObject *name = GETITEM(names, oparg);
2188 PyObject *v = POP();
2189 int err;
2190 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002191 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002192 if (err != 0)
2193 goto error;
2194 DISPATCH();
2195 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002196
Benjamin Petersonddd19492018-09-16 22:38:02 -07002197 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002198 PyObject *name = GETITEM(names, oparg);
2199 int err;
2200 err = PyDict_DelItem(f->f_globals, name);
2201 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002202 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002203 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002204 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002205 }
2206 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002207 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002208
Benjamin Petersonddd19492018-09-16 22:38:02 -07002209 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002210 PyObject *name = GETITEM(names, oparg);
2211 PyObject *locals = f->f_locals;
2212 PyObject *v;
2213 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002214 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002215 "no locals when loading %R", name);
2216 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002217 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002218 if (PyDict_CheckExact(locals)) {
2219 v = PyDict_GetItem(locals, name);
2220 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002221 }
2222 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002223 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002224 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002225 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2226 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002227 PyErr_Clear();
2228 }
2229 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002230 if (v == NULL) {
2231 v = PyDict_GetItem(f->f_globals, name);
2232 Py_XINCREF(v);
2233 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002234 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002235 v = PyDict_GetItem(f->f_builtins, name);
2236 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002237 format_exc_check_arg(
2238 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002239 NAME_ERROR_MSG, name);
2240 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002241 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002242 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002243 }
2244 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002245 v = PyObject_GetItem(f->f_builtins, name);
2246 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002247 if (PyErr_ExceptionMatches(PyExc_KeyError))
2248 format_exc_check_arg(
2249 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002250 NAME_ERROR_MSG, name);
2251 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002252 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002253 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002254 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002255 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002256 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002257 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002258 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002259
Benjamin Petersonddd19492018-09-16 22:38:02 -07002260 case TARGET(LOAD_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002261 PyObject *name = GETITEM(names, oparg);
2262 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002263 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002264 && PyDict_CheckExact(f->f_builtins))
2265 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002266 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002267 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002268 name);
2269 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002270 if (!_PyErr_OCCURRED()) {
2271 /* _PyDict_LoadGlobal() returns NULL without raising
2272 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002273 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002274 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002275 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002276 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002277 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002278 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002279 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002280 else {
2281 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002282
2283 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002284 v = PyObject_GetItem(f->f_globals, name);
2285 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002286 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2287 goto error;
2288 PyErr_Clear();
2289
Victor Stinnerb4efc962015-11-20 09:24:02 +01002290 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002291 v = PyObject_GetItem(f->f_builtins, name);
2292 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002293 if (PyErr_ExceptionMatches(PyExc_KeyError))
2294 format_exc_check_arg(
2295 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002296 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002297 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002298 }
2299 }
2300 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002301 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002302 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002303 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002304
Benjamin Petersonddd19492018-09-16 22:38:02 -07002305 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002306 PyObject *v = GETLOCAL(oparg);
2307 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002308 SETLOCAL(oparg, NULL);
2309 DISPATCH();
2310 }
2311 format_exc_check_arg(
2312 PyExc_UnboundLocalError,
2313 UNBOUNDLOCAL_ERROR_MSG,
2314 PyTuple_GetItem(co->co_varnames, oparg)
2315 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002316 goto error;
2317 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002318
Benjamin Petersonddd19492018-09-16 22:38:02 -07002319 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002320 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002321 PyObject *oldobj = PyCell_GET(cell);
2322 if (oldobj != NULL) {
2323 PyCell_SET(cell, NULL);
2324 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002325 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002326 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002327 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002328 goto error;
2329 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002330
Benjamin Petersonddd19492018-09-16 22:38:02 -07002331 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002332 PyObject *cell = freevars[oparg];
2333 Py_INCREF(cell);
2334 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002335 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002336 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002337
Benjamin Petersonddd19492018-09-16 22:38:02 -07002338 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002339 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002340 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002341 assert(locals);
2342 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2343 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2344 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2345 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2346 if (PyDict_CheckExact(locals)) {
2347 value = PyDict_GetItem(locals, name);
2348 Py_XINCREF(value);
2349 }
2350 else {
2351 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002352 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002353 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2354 goto error;
2355 PyErr_Clear();
2356 }
2357 }
2358 if (!value) {
2359 PyObject *cell = freevars[oparg];
2360 value = PyCell_GET(cell);
2361 if (value == NULL) {
2362 format_exc_unbound(co, oparg);
2363 goto error;
2364 }
2365 Py_INCREF(value);
2366 }
2367 PUSH(value);
2368 DISPATCH();
2369 }
2370
Benjamin Petersonddd19492018-09-16 22:38:02 -07002371 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002372 PyObject *cell = freevars[oparg];
2373 PyObject *value = PyCell_GET(cell);
2374 if (value == NULL) {
2375 format_exc_unbound(co, oparg);
2376 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002377 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002378 Py_INCREF(value);
2379 PUSH(value);
2380 DISPATCH();
2381 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002382
Benjamin Petersonddd19492018-09-16 22:38:02 -07002383 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002384 PyObject *v = POP();
2385 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002386 PyObject *oldobj = PyCell_GET(cell);
2387 PyCell_SET(cell, v);
2388 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002389 DISPATCH();
2390 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002391
Benjamin Petersonddd19492018-09-16 22:38:02 -07002392 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002393 PyObject *str;
2394 PyObject *empty = PyUnicode_New(0, 0);
2395 if (empty == NULL) {
2396 goto error;
2397 }
2398 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2399 Py_DECREF(empty);
2400 if (str == NULL)
2401 goto error;
2402 while (--oparg >= 0) {
2403 PyObject *item = POP();
2404 Py_DECREF(item);
2405 }
2406 PUSH(str);
2407 DISPATCH();
2408 }
2409
Benjamin Petersonddd19492018-09-16 22:38:02 -07002410 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002411 PyObject *tup = PyTuple_New(oparg);
2412 if (tup == NULL)
2413 goto error;
2414 while (--oparg >= 0) {
2415 PyObject *item = POP();
2416 PyTuple_SET_ITEM(tup, oparg, item);
2417 }
2418 PUSH(tup);
2419 DISPATCH();
2420 }
2421
Benjamin Petersonddd19492018-09-16 22:38:02 -07002422 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002423 PyObject *list = PyList_New(oparg);
2424 if (list == NULL)
2425 goto error;
2426 while (--oparg >= 0) {
2427 PyObject *item = POP();
2428 PyList_SET_ITEM(list, oparg, item);
2429 }
2430 PUSH(list);
2431 DISPATCH();
2432 }
2433
Benjamin Petersonddd19492018-09-16 22:38:02 -07002434 case TARGET(BUILD_TUPLE_UNPACK_WITH_CALL):
2435 case TARGET(BUILD_TUPLE_UNPACK):
2436 case TARGET(BUILD_LIST_UNPACK): {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002437 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002438 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002439 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002440 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002441
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002442 if (sum == NULL)
2443 goto error;
2444
2445 for (i = oparg; i > 0; i--) {
2446 PyObject *none_val;
2447
2448 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2449 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002450 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002451 PyErr_ExceptionMatches(PyExc_TypeError))
2452 {
2453 check_args_iterable(PEEK(1 + oparg), PEEK(i));
Serhiy Storchaka73442852016-10-02 10:33:46 +03002454 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002455 Py_DECREF(sum);
2456 goto error;
2457 }
2458 Py_DECREF(none_val);
2459 }
2460
2461 if (convert_to_tuple) {
2462 return_value = PyList_AsTuple(sum);
2463 Py_DECREF(sum);
2464 if (return_value == NULL)
2465 goto error;
2466 }
2467 else {
2468 return_value = sum;
2469 }
2470
2471 while (oparg--)
2472 Py_DECREF(POP());
2473 PUSH(return_value);
2474 DISPATCH();
2475 }
2476
Benjamin Petersonddd19492018-09-16 22:38:02 -07002477 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002478 PyObject *set = PySet_New(NULL);
2479 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002480 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002481 if (set == NULL)
2482 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002483 for (i = oparg; i > 0; i--) {
2484 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002485 if (err == 0)
2486 err = PySet_Add(set, item);
2487 Py_DECREF(item);
2488 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002489 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002490 if (err != 0) {
2491 Py_DECREF(set);
2492 goto error;
2493 }
2494 PUSH(set);
2495 DISPATCH();
2496 }
2497
Benjamin Petersonddd19492018-09-16 22:38:02 -07002498 case TARGET(BUILD_SET_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002499 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002500 PyObject *sum = PySet_New(NULL);
2501 if (sum == NULL)
2502 goto error;
2503
2504 for (i = oparg; i > 0; i--) {
2505 if (_PySet_Update(sum, PEEK(i)) < 0) {
2506 Py_DECREF(sum);
2507 goto error;
2508 }
2509 }
2510
2511 while (oparg--)
2512 Py_DECREF(POP());
2513 PUSH(sum);
2514 DISPATCH();
2515 }
2516
Benjamin Petersonddd19492018-09-16 22:38:02 -07002517 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002518 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002519 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2520 if (map == NULL)
2521 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002522 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002523 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002524 PyObject *key = PEEK(2*i);
2525 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002526 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002527 if (err != 0) {
2528 Py_DECREF(map);
2529 goto error;
2530 }
2531 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002532
2533 while (oparg--) {
2534 Py_DECREF(POP());
2535 Py_DECREF(POP());
2536 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002537 PUSH(map);
2538 DISPATCH();
2539 }
2540
Benjamin Petersonddd19492018-09-16 22:38:02 -07002541 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002542 _Py_IDENTIFIER(__annotations__);
2543 int err;
2544 PyObject *ann_dict;
2545 if (f->f_locals == NULL) {
2546 PyErr_Format(PyExc_SystemError,
2547 "no locals found when setting up annotations");
2548 goto error;
2549 }
2550 /* check if __annotations__ in locals()... */
2551 if (PyDict_CheckExact(f->f_locals)) {
2552 ann_dict = _PyDict_GetItemId(f->f_locals,
2553 &PyId___annotations__);
2554 if (ann_dict == NULL) {
2555 /* ...if not, create a new one */
2556 ann_dict = PyDict_New();
2557 if (ann_dict == NULL) {
2558 goto error;
2559 }
2560 err = _PyDict_SetItemId(f->f_locals,
2561 &PyId___annotations__, ann_dict);
2562 Py_DECREF(ann_dict);
2563 if (err != 0) {
2564 goto error;
2565 }
2566 }
2567 }
2568 else {
2569 /* do the same if locals() is not a dict */
2570 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2571 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002572 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002573 }
2574 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2575 if (ann_dict == NULL) {
2576 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2577 goto error;
2578 }
2579 PyErr_Clear();
2580 ann_dict = PyDict_New();
2581 if (ann_dict == NULL) {
2582 goto error;
2583 }
2584 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2585 Py_DECREF(ann_dict);
2586 if (err != 0) {
2587 goto error;
2588 }
2589 }
2590 else {
2591 Py_DECREF(ann_dict);
2592 }
2593 }
2594 DISPATCH();
2595 }
2596
Benjamin Petersonddd19492018-09-16 22:38:02 -07002597 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002598 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002599 PyObject *map;
2600 PyObject *keys = TOP();
2601 if (!PyTuple_CheckExact(keys) ||
2602 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2603 PyErr_SetString(PyExc_SystemError,
2604 "bad BUILD_CONST_KEY_MAP keys argument");
2605 goto error;
2606 }
2607 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2608 if (map == NULL) {
2609 goto error;
2610 }
2611 for (i = oparg; i > 0; i--) {
2612 int err;
2613 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2614 PyObject *value = PEEK(i + 1);
2615 err = PyDict_SetItem(map, key, value);
2616 if (err != 0) {
2617 Py_DECREF(map);
2618 goto error;
2619 }
2620 }
2621
2622 Py_DECREF(POP());
2623 while (oparg--) {
2624 Py_DECREF(POP());
2625 }
2626 PUSH(map);
2627 DISPATCH();
2628 }
2629
Benjamin Petersonddd19492018-09-16 22:38:02 -07002630 case TARGET(BUILD_MAP_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002631 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002632 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002633 if (sum == NULL)
2634 goto error;
2635
2636 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002637 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002638 if (PyDict_Update(sum, arg) < 0) {
2639 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2640 PyErr_Format(PyExc_TypeError,
Berker Peksag8e9045d2016-10-02 13:08:25 +03002641 "'%.200s' object is not a mapping",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002642 arg->ob_type->tp_name);
2643 }
2644 Py_DECREF(sum);
2645 goto error;
2646 }
2647 }
2648
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002649 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002650 Py_DECREF(POP());
2651 PUSH(sum);
2652 DISPATCH();
2653 }
2654
Benjamin Petersonddd19492018-09-16 22:38:02 -07002655 case TARGET(BUILD_MAP_UNPACK_WITH_CALL): {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002656 Py_ssize_t i;
2657 PyObject *sum = PyDict_New();
2658 if (sum == NULL)
2659 goto error;
2660
2661 for (i = oparg; i > 0; i--) {
2662 PyObject *arg = PEEK(i);
2663 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002664 Py_DECREF(sum);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02002665 format_kwargs_error(PEEK(2 + oparg), arg);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002666 goto error;
2667 }
2668 }
2669
2670 while (oparg--)
2671 Py_DECREF(POP());
2672 PUSH(sum);
2673 DISPATCH();
2674 }
2675
Benjamin Petersonddd19492018-09-16 22:38:02 -07002676 case TARGET(MAP_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002677 PyObject *key = TOP();
2678 PyObject *value = SECOND();
2679 PyObject *map;
2680 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002681 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002682 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002683 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002684 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002685 Py_DECREF(value);
2686 Py_DECREF(key);
2687 if (err != 0)
2688 goto error;
2689 PREDICT(JUMP_ABSOLUTE);
2690 DISPATCH();
2691 }
2692
Benjamin Petersonddd19492018-09-16 22:38:02 -07002693 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002694 PyObject *name = GETITEM(names, oparg);
2695 PyObject *owner = TOP();
2696 PyObject *res = PyObject_GetAttr(owner, name);
2697 Py_DECREF(owner);
2698 SET_TOP(res);
2699 if (res == NULL)
2700 goto error;
2701 DISPATCH();
2702 }
2703
Benjamin Petersonddd19492018-09-16 22:38:02 -07002704 case TARGET(COMPARE_OP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002705 PyObject *right = POP();
2706 PyObject *left = TOP();
2707 PyObject *res = cmp_outcome(oparg, left, right);
2708 Py_DECREF(left);
2709 Py_DECREF(right);
2710 SET_TOP(res);
2711 if (res == NULL)
2712 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002713 PREDICT(POP_JUMP_IF_FALSE);
2714 PREDICT(POP_JUMP_IF_TRUE);
2715 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002716 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002717
Benjamin Petersonddd19492018-09-16 22:38:02 -07002718 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002719 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002720 PyObject *fromlist = POP();
2721 PyObject *level = TOP();
2722 PyObject *res;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002723 res = import_name(f, name, fromlist, level);
2724 Py_DECREF(level);
2725 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002726 SET_TOP(res);
2727 if (res == NULL)
2728 goto error;
2729 DISPATCH();
2730 }
2731
Benjamin Petersonddd19492018-09-16 22:38:02 -07002732 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002733 PyObject *from = POP(), *locals;
2734 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002735 if (PyFrame_FastToLocalsWithError(f) < 0) {
2736 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002737 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002738 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002739
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002740 locals = f->f_locals;
2741 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002742 PyErr_SetString(PyExc_SystemError,
2743 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002744 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002745 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002746 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002747 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002748 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002749 Py_DECREF(from);
2750 if (err != 0)
2751 goto error;
2752 DISPATCH();
2753 }
Guido van Rossum25831651993-05-19 14:50:45 +00002754
Benjamin Petersonddd19492018-09-16 22:38:02 -07002755 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002756 PyObject *name = GETITEM(names, oparg);
2757 PyObject *from = TOP();
2758 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002759 res = import_from(from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002760 PUSH(res);
2761 if (res == NULL)
2762 goto error;
2763 DISPATCH();
2764 }
Thomas Wouters52152252000-08-17 22:55:00 +00002765
Benjamin Petersonddd19492018-09-16 22:38:02 -07002766 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002767 JUMPBY(oparg);
2768 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002769 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002770
Benjamin Petersonddd19492018-09-16 22:38:02 -07002771 case TARGET(POP_JUMP_IF_FALSE): {
2772 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002773 PyObject *cond = POP();
2774 int err;
2775 if (cond == Py_True) {
2776 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002777 FAST_DISPATCH();
2778 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002779 if (cond == Py_False) {
2780 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002781 JUMPTO(oparg);
2782 FAST_DISPATCH();
2783 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002784 err = PyObject_IsTrue(cond);
2785 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002786 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07002787 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002788 else if (err == 0)
2789 JUMPTO(oparg);
2790 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002791 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002792 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002793 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002794
Benjamin Petersonddd19492018-09-16 22:38:02 -07002795 case TARGET(POP_JUMP_IF_TRUE): {
2796 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002797 PyObject *cond = POP();
2798 int err;
2799 if (cond == Py_False) {
2800 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002801 FAST_DISPATCH();
2802 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002803 if (cond == Py_True) {
2804 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002805 JUMPTO(oparg);
2806 FAST_DISPATCH();
2807 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002808 err = PyObject_IsTrue(cond);
2809 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002810 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002811 JUMPTO(oparg);
2812 }
2813 else if (err == 0)
2814 ;
2815 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002816 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002817 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002818 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002819
Benjamin Petersonddd19492018-09-16 22:38:02 -07002820 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002821 PyObject *cond = TOP();
2822 int err;
2823 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002824 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002825 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002826 FAST_DISPATCH();
2827 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002828 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002829 JUMPTO(oparg);
2830 FAST_DISPATCH();
2831 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002832 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002833 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002834 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002835 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002836 }
2837 else if (err == 0)
2838 JUMPTO(oparg);
2839 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002840 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002841 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002842 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002843
Benjamin Petersonddd19492018-09-16 22:38:02 -07002844 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002845 PyObject *cond = TOP();
2846 int err;
2847 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002848 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002849 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002850 FAST_DISPATCH();
2851 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002852 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002853 JUMPTO(oparg);
2854 FAST_DISPATCH();
2855 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002856 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002857 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002858 JUMPTO(oparg);
2859 }
2860 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002861 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002862 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002863 }
2864 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002865 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002866 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002867 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002868
Benjamin Petersonddd19492018-09-16 22:38:02 -07002869 case TARGET(JUMP_ABSOLUTE): {
2870 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002871 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002872#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002873 /* Enabling this path speeds-up all while and for-loops by bypassing
2874 the per-loop checks for signals. By default, this should be turned-off
2875 because it prevents detection of a control-break in tight loops like
2876 "while 1: pass". Compile with this option turned-on when you need
2877 the speed-up and do not need break checking inside tight loops (ones
2878 that contain only instructions ending with FAST_DISPATCH).
2879 */
2880 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002881#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002882 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002883#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002884 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002885
Benjamin Petersonddd19492018-09-16 22:38:02 -07002886 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002887 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002888 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002889 PyObject *iter = PyObject_GetIter(iterable);
2890 Py_DECREF(iterable);
2891 SET_TOP(iter);
2892 if (iter == NULL)
2893 goto error;
2894 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002895 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04002896 DISPATCH();
2897 }
2898
Benjamin Petersonddd19492018-09-16 22:38:02 -07002899 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04002900 /* before: [obj]; after [getiter(obj)] */
2901 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04002902 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04002903 if (PyCoro_CheckExact(iterable)) {
2904 /* `iterable` is a coroutine */
2905 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
2906 /* and it is used in a 'yield from' expression of a
2907 regular generator. */
2908 Py_DECREF(iterable);
2909 SET_TOP(NULL);
2910 PyErr_SetString(PyExc_TypeError,
2911 "cannot 'yield from' a coroutine object "
2912 "in a non-coroutine generator");
2913 goto error;
2914 }
2915 }
2916 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04002917 /* `iterable` is not a generator. */
2918 iter = PyObject_GetIter(iterable);
2919 Py_DECREF(iterable);
2920 SET_TOP(iter);
2921 if (iter == NULL)
2922 goto error;
2923 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002924 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002925 DISPATCH();
2926 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002927
Benjamin Petersonddd19492018-09-16 22:38:02 -07002928 case TARGET(FOR_ITER): {
2929 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002930 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002931 PyObject *iter = TOP();
2932 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
2933 if (next != NULL) {
2934 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002935 PREDICT(STORE_FAST);
2936 PREDICT(UNPACK_SEQUENCE);
2937 DISPATCH();
2938 }
2939 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002940 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2941 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08002942 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002943 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002944 PyErr_Clear();
2945 }
2946 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00002947 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002948 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002949 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002950 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002951 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002952 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002953
Benjamin Petersonddd19492018-09-16 22:38:02 -07002954 case TARGET(SETUP_FINALLY): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002955 /* NOTE: If you add any new block-setup opcodes that
2956 are not try/except/finally handlers, you may need
2957 to update the PyGen_NeedsFinalizing() function.
2958 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002959
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002960 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002961 STACK_LEVEL());
2962 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002963 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002964
Benjamin Petersonddd19492018-09-16 22:38:02 -07002965 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04002966 _Py_IDENTIFIER(__aexit__);
2967 _Py_IDENTIFIER(__aenter__);
2968
2969 PyObject *mgr = TOP();
2970 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
2971 *enter;
2972 PyObject *res;
2973 if (exit == NULL)
2974 goto error;
2975 SET_TOP(exit);
2976 enter = special_lookup(mgr, &PyId___aenter__);
2977 Py_DECREF(mgr);
2978 if (enter == NULL)
2979 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01002980 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04002981 Py_DECREF(enter);
2982 if (res == NULL)
2983 goto error;
2984 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002985 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002986 DISPATCH();
2987 }
2988
Benjamin Petersonddd19492018-09-16 22:38:02 -07002989 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04002990 PyObject *res = POP();
2991 /* Setup the finally block before pushing the result
2992 of __aenter__ on the stack. */
2993 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2994 STACK_LEVEL());
2995 PUSH(res);
2996 DISPATCH();
2997 }
2998
Benjamin Petersonddd19492018-09-16 22:38:02 -07002999 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003000 _Py_IDENTIFIER(__exit__);
3001 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003002 PyObject *mgr = TOP();
Raymond Hettingera3fec152016-11-21 17:24:23 -08003003 PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003004 PyObject *res;
Raymond Hettingera3fec152016-11-21 17:24:23 -08003005 if (enter == NULL)
3006 goto error;
3007 exit = special_lookup(mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003008 if (exit == NULL) {
3009 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003010 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003011 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003012 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003013 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003014 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003015 Py_DECREF(enter);
3016 if (res == NULL)
3017 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003018 /* Setup the finally block before pushing the result
3019 of __enter__ on the stack. */
3020 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3021 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003022
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003023 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003024 DISPATCH();
3025 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003026
Benjamin Petersonddd19492018-09-16 22:38:02 -07003027 case TARGET(WITH_CLEANUP_START): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003028 /* At the top of the stack are 1 or 6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003029 how/why we entered the finally clause:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003030 - TOP = NULL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003031 - (TOP, SECOND, THIRD) = exc_info()
3032 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003033 Below them is EXIT, the context.__exit__ or context.__aexit__
3034 bound method.
3035 In the first case, we must call
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003036 EXIT(None, None, None)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003037 otherwise we must call
3038 EXIT(TOP, SECOND, THIRD)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003039
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003040 In the first case, we remove EXIT from the
3041 stack, leaving TOP, and push TOP on the stack.
3042 Otherwise we shift the bottom 3 values of the
3043 stack down, replace the empty spot with NULL, and push
3044 None on the stack.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003045
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003046 Finally we push the result of the call.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003047 */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003048 PyObject *stack[3];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003049 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003050 PyObject *exc, *val, *tb, *res;
3051
3052 val = tb = Py_None;
3053 exc = TOP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003054 if (exc == NULL) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003055 STACK_SHRINK(1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003056 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003057 SET_TOP(exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003058 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003059 }
3060 else {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003061 assert(PyExceptionClass_Check(exc));
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003062 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003063 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003064 val = SECOND();
3065 tb = THIRD();
3066 tp2 = FOURTH();
3067 exc2 = PEEK(5);
3068 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003069 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003070 SET_VALUE(7, tb2);
3071 SET_VALUE(6, exc2);
3072 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003073 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3074 SET_FOURTH(NULL);
3075 /* We just shifted the stack down, so we have
3076 to tell the except handler block that the
3077 values are lower than it expects. */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003078 assert(f->f_iblock > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003079 block = &f->f_blockstack[f->f_iblock - 1];
3080 assert(block->b_type == EXCEPT_HANDLER);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003081 assert(block->b_level > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003082 block->b_level--;
3083 }
Victor Stinner842cfff2016-12-01 14:45:31 +01003084
3085 stack[0] = exc;
3086 stack[1] = val;
3087 stack[2] = tb;
3088 res = _PyObject_FastCall(exit_func, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003089 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003090 if (res == NULL)
3091 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003092
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003093 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003094 PUSH(exc);
3095 PUSH(res);
3096 PREDICT(WITH_CLEANUP_FINISH);
3097 DISPATCH();
3098 }
3099
Benjamin Petersonddd19492018-09-16 22:38:02 -07003100 case TARGET(WITH_CLEANUP_FINISH): {
3101 PREDICTED(WITH_CLEANUP_FINISH);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003102 /* TOP = the result of calling the context.__exit__ bound method
3103 SECOND = either None or exception type
3104
3105 If SECOND is None below is NULL or the return address,
3106 otherwise below are 7 values representing an exception.
3107 */
Yury Selivanov75445082015-05-11 22:57:16 -04003108 PyObject *res = POP();
3109 PyObject *exc = POP();
3110 int err;
3111
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003112 if (exc != Py_None)
3113 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003114 else
3115 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003116
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003117 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003118 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003120 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003121 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003122 else if (err > 0) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003123 /* There was an exception and a True return.
3124 * We must manually unwind the EXCEPT_HANDLER block
3125 * which was created when the exception was caught,
Quan Tian3bd0d622018-10-20 05:30:03 +08003126 * otherwise the stack will be in an inconsistent state.
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003127 */
3128 PyTryBlock *b = PyFrame_BlockPop(f);
3129 assert(b->b_type == EXCEPT_HANDLER);
3130 UNWIND_EXCEPT_HANDLER(b);
3131 PUSH(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003132 }
3133 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003134 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003135 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003136
Benjamin Petersonddd19492018-09-16 22:38:02 -07003137 case TARGET(LOAD_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003138 /* Designed to work in tamdem with CALL_METHOD. */
3139 PyObject *name = GETITEM(names, oparg);
3140 PyObject *obj = TOP();
3141 PyObject *meth = NULL;
3142
3143 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3144
Yury Selivanovf2392132016-12-13 19:03:51 -05003145 if (meth == NULL) {
3146 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003147 goto error;
3148 }
3149
3150 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003151 /* We can bypass temporary bound method object.
3152 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003153
INADA Naoki015bce62017-01-16 17:23:30 +09003154 meth | self | arg1 | ... | argN
3155 */
3156 SET_TOP(meth);
3157 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003158 }
3159 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003160 /* meth is not an unbound method (but a regular attr, or
3161 something was returned by a descriptor protocol). Set
3162 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003163 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003164
3165 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003166 */
INADA Naoki015bce62017-01-16 17:23:30 +09003167 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003168 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003169 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003170 }
3171 DISPATCH();
3172 }
3173
Benjamin Petersonddd19492018-09-16 22:38:02 -07003174 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003175 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003176 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003177
3178 sp = stack_pointer;
3179
INADA Naoki015bce62017-01-16 17:23:30 +09003180 meth = PEEK(oparg + 2);
3181 if (meth == NULL) {
3182 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3183 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003184
3185 Stack layout:
3186
INADA Naoki015bce62017-01-16 17:23:30 +09003187 ... | NULL | callable | arg1 | ... | argN
3188 ^- TOP()
3189 ^- (-oparg)
3190 ^- (-oparg-1)
3191 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003192
Ville Skyttä49b27342017-08-03 09:00:59 +03003193 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003194 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003195 */
Yury Selivanovf2392132016-12-13 19:03:51 -05003196 res = call_function(&sp, oparg, NULL);
3197 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003198 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003199 }
3200 else {
3201 /* This is a method call. Stack layout:
3202
INADA Naoki015bce62017-01-16 17:23:30 +09003203 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003204 ^- TOP()
3205 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003206 ^- (-oparg-1)
3207 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003208
INADA Naoki015bce62017-01-16 17:23:30 +09003209 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003210 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003211 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003212 */
3213 res = call_function(&sp, oparg + 1, NULL);
3214 stack_pointer = sp;
3215 }
3216
3217 PUSH(res);
3218 if (res == NULL)
3219 goto error;
3220 DISPATCH();
3221 }
3222
Benjamin Petersonddd19492018-09-16 22:38:02 -07003223 case TARGET(CALL_FUNCTION): {
3224 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003225 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003226 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003227 res = call_function(&sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003228 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003229 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003230 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003231 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003232 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003233 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003234 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003235
Benjamin Petersonddd19492018-09-16 22:38:02 -07003236 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003237 PyObject **sp, *res, *names;
3238
3239 names = POP();
3240 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003241 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003242 res = call_function(&sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003243 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003244 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003245 Py_DECREF(names);
3246
3247 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003248 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003249 }
3250 DISPATCH();
3251 }
3252
Benjamin Petersonddd19492018-09-16 22:38:02 -07003253 case TARGET(CALL_FUNCTION_EX): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003254 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003255 if (oparg & 0x01) {
3256 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003257 if (!PyDict_CheckExact(kwargs)) {
3258 PyObject *d = PyDict_New();
3259 if (d == NULL)
3260 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003261 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003262 Py_DECREF(d);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003263 format_kwargs_error(SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003264 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003265 goto error;
3266 }
3267 Py_DECREF(kwargs);
3268 kwargs = d;
3269 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003270 assert(PyDict_CheckExact(kwargs));
3271 }
3272 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003273 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003274 if (!PyTuple_CheckExact(callargs)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003275 if (check_args_iterable(func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003276 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003277 goto error;
3278 }
3279 Py_SETREF(callargs, PySequence_Tuple(callargs));
3280 if (callargs == NULL) {
3281 goto error;
3282 }
3283 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003284 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003285
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003286 result = do_call_core(func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003287 Py_DECREF(func);
3288 Py_DECREF(callargs);
3289 Py_XDECREF(kwargs);
3290
3291 SET_TOP(result);
3292 if (result == NULL) {
3293 goto error;
3294 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003295 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003296 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003297
Benjamin Petersonddd19492018-09-16 22:38:02 -07003298 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003299 PyObject *qualname = POP();
3300 PyObject *codeobj = POP();
3301 PyFunctionObject *func = (PyFunctionObject *)
3302 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003303
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003304 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003305 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003306 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003307 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003308 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003309
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003310 if (oparg & 0x08) {
3311 assert(PyTuple_CheckExact(TOP()));
3312 func ->func_closure = POP();
3313 }
3314 if (oparg & 0x04) {
3315 assert(PyDict_CheckExact(TOP()));
3316 func->func_annotations = POP();
3317 }
3318 if (oparg & 0x02) {
3319 assert(PyDict_CheckExact(TOP()));
3320 func->func_kwdefaults = POP();
3321 }
3322 if (oparg & 0x01) {
3323 assert(PyTuple_CheckExact(TOP()));
3324 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003325 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003326
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003327 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003328 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003329 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003330
Benjamin Petersonddd19492018-09-16 22:38:02 -07003331 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003332 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003333 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003334 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003335 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003336 step = NULL;
3337 stop = POP();
3338 start = TOP();
3339 slice = PySlice_New(start, stop, step);
3340 Py_DECREF(start);
3341 Py_DECREF(stop);
3342 Py_XDECREF(step);
3343 SET_TOP(slice);
3344 if (slice == NULL)
3345 goto error;
3346 DISPATCH();
3347 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003348
Benjamin Petersonddd19492018-09-16 22:38:02 -07003349 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003350 /* Handles f-string value formatting. */
3351 PyObject *result;
3352 PyObject *fmt_spec;
3353 PyObject *value;
3354 PyObject *(*conv_fn)(PyObject *);
3355 int which_conversion = oparg & FVC_MASK;
3356 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3357
3358 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003359 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003360
3361 /* See if any conversion is specified. */
3362 switch (which_conversion) {
3363 case FVC_STR: conv_fn = PyObject_Str; break;
3364 case FVC_REPR: conv_fn = PyObject_Repr; break;
3365 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3366
3367 /* Must be 0 (meaning no conversion), since only four
3368 values are allowed by (oparg & FVC_MASK). */
3369 default: conv_fn = NULL; break;
3370 }
3371
3372 /* If there's a conversion function, call it and replace
3373 value with that result. Otherwise, just use value,
3374 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003375 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003376 result = conv_fn(value);
3377 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003378 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003379 Py_XDECREF(fmt_spec);
3380 goto error;
3381 }
3382 value = result;
3383 }
3384
3385 /* If value is a unicode object, and there's no fmt_spec,
3386 then we know the result of format(value) is value
3387 itself. In that case, skip calling format(). I plan to
3388 move this optimization in to PyObject_Format()
3389 itself. */
3390 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3391 /* Do nothing, just transfer ownership to result. */
3392 result = value;
3393 } else {
3394 /* Actually call format(). */
3395 result = PyObject_Format(value, fmt_spec);
3396 Py_DECREF(value);
3397 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003398 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003399 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003400 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003401 }
3402
Eric V. Smith135d5f42016-02-05 18:23:08 -05003403 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003404 DISPATCH();
3405 }
3406
Benjamin Petersonddd19492018-09-16 22:38:02 -07003407 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003408 int oldoparg = oparg;
3409 NEXTOPARG();
3410 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003411 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003412 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003413
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003414
Antoine Pitrou042b1282010-08-13 21:15:58 +00003415#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003416 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003417#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003418 default:
3419 fprintf(stderr,
3420 "XXX lineno: %d, opcode: %d\n",
3421 PyFrame_GetLineNumber(f),
3422 opcode);
3423 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003424 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003426 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003427
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003428 /* This should never be reached. Every opcode should end with DISPATCH()
3429 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003430 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003431
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003432error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003433 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003434#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003435 if (!PyErr_Occurred())
3436 PyErr_SetString(PyExc_SystemError,
3437 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003438#else
3439 assert(PyErr_Occurred());
3440#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003441
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003442 /* Log traceback info. */
3443 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003444
Benjamin Peterson51f46162013-01-23 08:38:47 -05003445 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003446 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3447 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003448
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003449exception_unwind:
3450 /* Unwind stacks if an exception occurred */
3451 while (f->f_iblock > 0) {
3452 /* Pop the current block. */
3453 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003455 if (b->b_type == EXCEPT_HANDLER) {
3456 UNWIND_EXCEPT_HANDLER(b);
3457 continue;
3458 }
3459 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003460 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003461 PyObject *exc, *val, *tb;
3462 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003463 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003464 /* Beware, this invalidates all b->b_* fields */
3465 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003466 PUSH(exc_info->exc_traceback);
3467 PUSH(exc_info->exc_value);
3468 if (exc_info->exc_type != NULL) {
3469 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003470 }
3471 else {
3472 Py_INCREF(Py_None);
3473 PUSH(Py_None);
3474 }
3475 PyErr_Fetch(&exc, &val, &tb);
3476 /* Make the raw exception data
3477 available to the handler,
3478 so a program can emulate the
3479 Python main loop. */
3480 PyErr_NormalizeException(
3481 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003482 if (tb != NULL)
3483 PyException_SetTraceback(val, tb);
3484 else
3485 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003486 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003487 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003488 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003489 exc_info->exc_value = val;
3490 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003491 if (tb == NULL)
3492 tb = Py_None;
3493 Py_INCREF(tb);
3494 PUSH(tb);
3495 PUSH(val);
3496 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003497 JUMPTO(handler);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003498 /* Resume normal execution */
3499 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003500 }
3501 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003502
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003503 /* End the loop as we still have an error */
3504 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003505 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003507 /* Pop remaining stack entries. */
3508 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003509 PyObject *o = POP();
3510 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003511 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003512
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003513 assert(retval == NULL);
3514 assert(PyErr_Occurred());
Guido van Rossumac7be682001-01-17 15:42:30 +00003515
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003516return_or_yield:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003517 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003518 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003519 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3520 tstate, f, PyTrace_RETURN, retval)) {
3521 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003522 }
3523 }
3524 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003525 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3526 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003527 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003528 }
3529 }
3530 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003531
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003532 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003533exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003534 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3535 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003536 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003537 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003538 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003539
Victor Stinnerefde1462015-03-21 15:04:43 +01003540 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003541}
3542
Benjamin Petersonb204a422011-06-05 22:04:07 -05003543static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003544format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3545{
3546 int err;
3547 Py_ssize_t len = PyList_GET_SIZE(names);
3548 PyObject *name_str, *comma, *tail, *tmp;
3549
3550 assert(PyList_CheckExact(names));
3551 assert(len >= 1);
3552 /* Deal with the joys of natural language. */
3553 switch (len) {
3554 case 1:
3555 name_str = PyList_GET_ITEM(names, 0);
3556 Py_INCREF(name_str);
3557 break;
3558 case 2:
3559 name_str = PyUnicode_FromFormat("%U and %U",
3560 PyList_GET_ITEM(names, len - 2),
3561 PyList_GET_ITEM(names, len - 1));
3562 break;
3563 default:
3564 tail = PyUnicode_FromFormat(", %U, and %U",
3565 PyList_GET_ITEM(names, len - 2),
3566 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003567 if (tail == NULL)
3568 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003569 /* Chop off the last two objects in the list. This shouldn't actually
3570 fail, but we can't be too careful. */
3571 err = PyList_SetSlice(names, len - 2, len, NULL);
3572 if (err == -1) {
3573 Py_DECREF(tail);
3574 return;
3575 }
3576 /* Stitch everything up into a nice comma-separated list. */
3577 comma = PyUnicode_FromString(", ");
3578 if (comma == NULL) {
3579 Py_DECREF(tail);
3580 return;
3581 }
3582 tmp = PyUnicode_Join(comma, names);
3583 Py_DECREF(comma);
3584 if (tmp == NULL) {
3585 Py_DECREF(tail);
3586 return;
3587 }
3588 name_str = PyUnicode_Concat(tmp, tail);
3589 Py_DECREF(tmp);
3590 Py_DECREF(tail);
3591 break;
3592 }
3593 if (name_str == NULL)
3594 return;
3595 PyErr_Format(PyExc_TypeError,
3596 "%U() missing %i required %s argument%s: %U",
3597 co->co_name,
3598 len,
3599 kind,
3600 len == 1 ? "" : "s",
3601 name_str);
3602 Py_DECREF(name_str);
3603}
3604
3605static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003606missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003607 PyObject **fastlocals)
3608{
Victor Stinner74319ae2016-08-25 00:04:09 +02003609 Py_ssize_t i, j = 0;
3610 Py_ssize_t start, end;
3611 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003612 const char *kind = positional ? "positional" : "keyword-only";
3613 PyObject *missing_names;
3614
3615 /* Compute the names of the arguments that are missing. */
3616 missing_names = PyList_New(missing);
3617 if (missing_names == NULL)
3618 return;
3619 if (positional) {
3620 start = 0;
3621 end = co->co_argcount - defcount;
3622 }
3623 else {
3624 start = co->co_argcount;
3625 end = start + co->co_kwonlyargcount;
3626 }
3627 for (i = start; i < end; i++) {
3628 if (GETLOCAL(i) == NULL) {
3629 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3630 PyObject *name = PyObject_Repr(raw);
3631 if (name == NULL) {
3632 Py_DECREF(missing_names);
3633 return;
3634 }
3635 PyList_SET_ITEM(missing_names, j++, name);
3636 }
3637 }
3638 assert(j == missing);
3639 format_missing(kind, co, missing_names);
3640 Py_DECREF(missing_names);
3641}
3642
3643static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003644too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3645 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003646{
3647 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003648 Py_ssize_t kwonly_given = 0;
3649 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003650 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003651 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003652
Benjamin Petersone109c702011-06-24 09:37:26 -05003653 assert((co->co_flags & CO_VARARGS) == 0);
3654 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003655 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3656 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003657 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003658 }
3659 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003660 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003661 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003662 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003663 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003664 }
3665 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003666 plural = (co_argcount != 1);
3667 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003668 }
3669 if (sig == NULL)
3670 return;
3671 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003672 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3673 kwonly_sig = PyUnicode_FromFormat(format,
3674 given != 1 ? "s" : "",
3675 kwonly_given,
3676 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003677 if (kwonly_sig == NULL) {
3678 Py_DECREF(sig);
3679 return;
3680 }
3681 }
3682 else {
3683 /* This will not fail. */
3684 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003685 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003686 }
3687 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003688 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003689 co->co_name,
3690 sig,
3691 plural ? "s" : "",
3692 given,
3693 kwonly_sig,
3694 given == 1 && !kwonly_given ? "was" : "were");
3695 Py_DECREF(sig);
3696 Py_DECREF(kwonly_sig);
3697}
3698
Guido van Rossumc2e20742006-02-27 22:32:47 +00003699/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02003700 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003701 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003702
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003703PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +02003704_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003705 PyObject *const *args, Py_ssize_t argcount,
3706 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003707 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003708 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003709 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003710 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003711{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003712 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003713 PyFrameObject *f;
3714 PyObject *retval = NULL;
3715 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003716 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003717 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003718 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3719 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003720 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003721
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003722 if (globals == NULL) {
3723 PyErr_SetString(PyExc_SystemError,
3724 "PyEval_EvalCodeEx: NULL globals");
3725 return NULL;
3726 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003727
Victor Stinnerc7020012016-08-16 23:40:29 +02003728 /* Create the frame */
Victor Stinner50b48572018-11-01 01:51:40 +01003729 tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003730 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003731 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003732 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003733 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003734 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003735 fastlocals = f->f_localsplus;
3736 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003737
Victor Stinnerc7020012016-08-16 23:40:29 +02003738 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003739 if (co->co_flags & CO_VARKEYWORDS) {
3740 kwdict = PyDict_New();
3741 if (kwdict == NULL)
3742 goto fail;
3743 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003744 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003745 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003746 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003747 SETLOCAL(i, kwdict);
3748 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003749 else {
3750 kwdict = NULL;
3751 }
3752
3753 /* Copy positional arguments into local variables */
3754 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003755 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003756 }
3757 else {
3758 n = argcount;
3759 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003760 for (i = 0; i < n; i++) {
3761 x = args[i];
3762 Py_INCREF(x);
3763 SETLOCAL(i, x);
3764 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003765
3766 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003767 if (co->co_flags & CO_VARARGS) {
3768 u = PyTuple_New(argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003769 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003770 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003771 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003772 SETLOCAL(total_args, u);
3773 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003774 x = args[i];
3775 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003776 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003777 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003778 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003779
Serhiy Storchakab7281052016-09-12 00:52:40 +03003780 /* Handle keyword arguments passed as two strided arrays */
3781 kwcount *= kwstep;
3782 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003783 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003784 PyObject *keyword = kwnames[i];
3785 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003786 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003787
Benjamin Petersonb204a422011-06-05 22:04:07 -05003788 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3789 PyErr_Format(PyExc_TypeError,
3790 "%U() keywords must be strings",
3791 co->co_name);
3792 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003793 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003794
Benjamin Petersonb204a422011-06-05 22:04:07 -05003795 /* Speed hack: do raw pointer compares. As names are
3796 normally interned this should almost always hit. */
3797 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3798 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003799 PyObject *name = co_varnames[j];
3800 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003801 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003802 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003803 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003804
Benjamin Petersonb204a422011-06-05 22:04:07 -05003805 /* Slow fallback, just in case */
3806 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003807 PyObject *name = co_varnames[j];
3808 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
3809 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003810 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003811 }
3812 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003813 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003814 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003815 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003816
Victor Stinner231d1f32017-01-11 02:12:06 +01003817 assert(j >= total_args);
3818 if (kwdict == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003819 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003820 "%U() got an unexpected keyword argument '%S'",
3821 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003822 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003823 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003824
Christian Heimes0bd447f2013-07-20 14:48:10 +02003825 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3826 goto fail;
3827 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003828 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02003829
Benjamin Petersonb204a422011-06-05 22:04:07 -05003830 kw_found:
3831 if (GETLOCAL(j) != NULL) {
3832 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003833 "%U() got multiple values for argument '%S'",
3834 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003835 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003836 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003837 Py_INCREF(value);
3838 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003839 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003840
3841 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003842 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003843 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003844 goto fail;
3845 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003846
3847 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003848 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02003849 Py_ssize_t m = co->co_argcount - defcount;
3850 Py_ssize_t missing = 0;
3851 for (i = argcount; i < m; i++) {
3852 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003853 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02003854 }
3855 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003856 if (missing) {
3857 missing_arguments(co, missing, defcount, fastlocals);
3858 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003859 }
3860 if (n > m)
3861 i = n - m;
3862 else
3863 i = 0;
3864 for (; i < defcount; i++) {
3865 if (GETLOCAL(m+i) == NULL) {
3866 PyObject *def = defs[i];
3867 Py_INCREF(def);
3868 SETLOCAL(m+i, def);
3869 }
3870 }
3871 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003872
3873 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003874 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02003875 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003876 for (i = co->co_argcount; i < total_args; i++) {
3877 PyObject *name;
3878 if (GETLOCAL(i) != NULL)
3879 continue;
3880 name = PyTuple_GET_ITEM(co->co_varnames, i);
3881 if (kwdefs != NULL) {
3882 PyObject *def = PyDict_GetItem(kwdefs, name);
3883 if (def) {
3884 Py_INCREF(def);
3885 SETLOCAL(i, def);
3886 continue;
3887 }
3888 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003889 missing++;
3890 }
3891 if (missing) {
3892 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003893 goto fail;
3894 }
3895 }
3896
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003897 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003898 vars into frame. */
3899 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003900 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02003901 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05003902 /* Possibly account for the cell variable being an argument. */
3903 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07003904 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05003905 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05003906 /* Clear the local copy. */
3907 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003908 }
3909 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05003910 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003911 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05003912 if (c == NULL)
3913 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05003914 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003915 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003916
3917 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05003918 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3919 PyObject *o = PyTuple_GET_ITEM(closure, i);
3920 Py_INCREF(o);
3921 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003922 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003923
Yury Selivanoveb636452016-09-08 22:01:51 -07003924 /* Handle generator/coroutine/asynchronous generator */
3925 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003926 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04003927 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003928 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04003929
3930 if (is_coro && tstate->in_coroutine_wrapper) {
3931 assert(coro_wrapper != NULL);
3932 PyErr_Format(PyExc_RuntimeError,
3933 "coroutine wrapper %.200R attempted "
3934 "to recursively wrap %.200R",
3935 coro_wrapper,
3936 co);
3937 goto fail;
3938 }
Yury Selivanov75445082015-05-11 22:57:16 -04003939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003940 /* Don't need to keep the reference to f_back, it will be set
3941 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003942 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003944 /* Create a new generator that owns the ready to run frame
3945 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04003946 if (is_coro) {
3947 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07003948 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
3949 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003950 } else {
3951 gen = PyGen_NewWithQualName(f, name, qualname);
3952 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003953 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003954 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003955 }
INADA Naoki9c157762016-12-26 18:52:46 +09003956
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003957 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04003958
Yury Selivanov94c22632015-06-04 10:16:51 -04003959 if (is_coro && coro_wrapper != NULL) {
3960 PyObject *wrapped;
3961 tstate->in_coroutine_wrapper = 1;
3962 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
3963 tstate->in_coroutine_wrapper = 0;
3964 return wrapped;
3965 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04003966
Yury Selivanov75445082015-05-11 22:57:16 -04003967 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003968 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003969
Victor Stinner59a73272016-12-09 18:51:13 +01003970 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003971
Thomas Woutersce272b62007-09-19 21:19:28 +00003972fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003973
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003974 /* decref'ing the frame can cause __del__ methods to get invoked,
3975 which can call back into Python. While we're done with the
3976 current Python frame (f), the associated C stack is still in use,
3977 so recursion_depth must be boosted for the duration.
3978 */
3979 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003980 if (Py_REFCNT(f) > 1) {
3981 Py_DECREF(f);
3982 _PyObject_GC_TRACK(f);
3983 }
3984 else {
3985 ++tstate->recursion_depth;
3986 Py_DECREF(f);
3987 --tstate->recursion_depth;
3988 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003989 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003990}
3991
Victor Stinner40ee3012014-06-16 15:59:28 +02003992PyObject *
3993PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003994 PyObject *const *args, int argcount,
3995 PyObject *const *kws, int kwcount,
3996 PyObject *const *defs, int defcount,
3997 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02003998{
3999 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004000 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004001 kws, kws != NULL ? kws + 1 : NULL,
4002 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004003 defs, defcount,
4004 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004005 NULL, NULL);
4006}
Tim Peters5ca576e2001-06-18 22:08:13 +00004007
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004008static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05004009special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004010{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004011 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004012 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004013 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05004014 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004015 return NULL;
4016 }
4017 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004018}
4019
4020
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004021/* Logic for the raise statement (too complicated for inlining).
4022 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004023static int
Collin Winter828f04a2007-08-31 00:04:24 +00004024do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004025{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004026 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004027
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004028 if (exc == NULL) {
4029 /* Reraise */
Victor Stinner50b48572018-11-01 01:51:40 +01004030 PyThreadState *tstate = _PyThreadState_GET();
Mark Shannonae3087c2017-10-22 22:41:51 +01004031 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004032 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004033 type = exc_info->exc_type;
4034 value = exc_info->exc_value;
4035 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004036 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004037 PyErr_SetString(PyExc_RuntimeError,
4038 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004039 return 0;
4040 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004041 Py_XINCREF(type);
4042 Py_XINCREF(value);
4043 Py_XINCREF(tb);
4044 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004045 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004046 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004048 /* We support the following forms of raise:
4049 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004050 raise <instance>
4051 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004053 if (PyExceptionClass_Check(exc)) {
4054 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004055 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004056 if (value == NULL)
4057 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004058 if (!PyExceptionInstance_Check(value)) {
4059 PyErr_Format(PyExc_TypeError,
4060 "calling %R should have returned an instance of "
4061 "BaseException, not %R",
4062 type, Py_TYPE(value));
4063 goto raise_error;
4064 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004065 }
4066 else if (PyExceptionInstance_Check(exc)) {
4067 value = exc;
4068 type = PyExceptionInstance_Class(exc);
4069 Py_INCREF(type);
4070 }
4071 else {
4072 /* Not something you can raise. You get an exception
4073 anyway, just not what you specified :-) */
4074 Py_DECREF(exc);
4075 PyErr_SetString(PyExc_TypeError,
4076 "exceptions must derive from BaseException");
4077 goto raise_error;
4078 }
Collin Winter828f04a2007-08-31 00:04:24 +00004079
Serhiy Storchakac0191582016-09-27 11:37:10 +03004080 assert(type != NULL);
4081 assert(value != NULL);
4082
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004083 if (cause) {
4084 PyObject *fixed_cause;
4085 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004086 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004087 if (fixed_cause == NULL)
4088 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004089 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004090 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004091 else if (PyExceptionInstance_Check(cause)) {
4092 fixed_cause = cause;
4093 }
4094 else if (cause == Py_None) {
4095 Py_DECREF(cause);
4096 fixed_cause = NULL;
4097 }
4098 else {
4099 PyErr_SetString(PyExc_TypeError,
4100 "exception causes must derive from "
4101 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004102 goto raise_error;
4103 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004104 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004105 }
Collin Winter828f04a2007-08-31 00:04:24 +00004106
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004107 PyErr_SetObject(type, value);
4108 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004109 Py_DECREF(value);
4110 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004111 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004112
4113raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004114 Py_XDECREF(value);
4115 Py_XDECREF(type);
4116 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004117 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004118}
4119
Tim Petersd6d010b2001-06-21 02:49:55 +00004120/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004121 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004122
Guido van Rossum0368b722007-05-11 16:50:42 +00004123 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4124 with a variable target.
4125*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004126
Barry Warsawe42b18f1997-08-25 22:13:04 +00004127static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004128unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004129{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004130 int i = 0, j = 0;
4131 Py_ssize_t ll = 0;
4132 PyObject *it; /* iter(v) */
4133 PyObject *w;
4134 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004135
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004136 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004137
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004138 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004139 if (it == NULL) {
4140 if (PyErr_ExceptionMatches(PyExc_TypeError) &&
4141 v->ob_type->tp_iter == NULL && !PySequence_Check(v))
4142 {
4143 PyErr_Format(PyExc_TypeError,
4144 "cannot unpack non-iterable %.200s object",
4145 v->ob_type->tp_name);
4146 }
4147 return 0;
4148 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004149
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004150 for (; i < argcnt; i++) {
4151 w = PyIter_Next(it);
4152 if (w == NULL) {
4153 /* Iterator done, via error or exhaustion. */
4154 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004155 if (argcntafter == -1) {
4156 PyErr_Format(PyExc_ValueError,
4157 "not enough values to unpack (expected %d, got %d)",
4158 argcnt, i);
4159 }
4160 else {
4161 PyErr_Format(PyExc_ValueError,
4162 "not enough values to unpack "
4163 "(expected at least %d, got %d)",
4164 argcnt + argcntafter, i);
4165 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004166 }
4167 goto Error;
4168 }
4169 *--sp = w;
4170 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004171
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004172 if (argcntafter == -1) {
4173 /* We better have exhausted the iterator now. */
4174 w = PyIter_Next(it);
4175 if (w == NULL) {
4176 if (PyErr_Occurred())
4177 goto Error;
4178 Py_DECREF(it);
4179 return 1;
4180 }
4181 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004182 PyErr_Format(PyExc_ValueError,
4183 "too many values to unpack (expected %d)",
4184 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004185 goto Error;
4186 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004187
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004188 l = PySequence_List(it);
4189 if (l == NULL)
4190 goto Error;
4191 *--sp = l;
4192 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004193
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004194 ll = PyList_GET_SIZE(l);
4195 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004196 PyErr_Format(PyExc_ValueError,
4197 "not enough values to unpack (expected at least %d, got %zd)",
4198 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004199 goto Error;
4200 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004201
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004202 /* Pop the "after-variable" args off the list. */
4203 for (j = argcntafter; j > 0; j--, i++) {
4204 *--sp = PyList_GET_ITEM(l, ll - j);
4205 }
4206 /* Resize the list. */
4207 Py_SIZE(l) = ll - argcntafter;
4208 Py_DECREF(it);
4209 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004210
Tim Petersd6d010b2001-06-21 02:49:55 +00004211Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004212 for (; i > 0; i--, sp++)
4213 Py_DECREF(*sp);
4214 Py_XDECREF(it);
4215 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004216}
4217
4218
Guido van Rossum96a42c81992-01-12 02:29:51 +00004219#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004220static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004221prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004222{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004223 printf("%s ", str);
4224 if (PyObject_Print(v, stdout, 0) != 0)
4225 PyErr_Clear(); /* Don't know what else to do */
4226 printf("\n");
4227 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004228}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004229#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004230
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004231static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004232call_exc_trace(Py_tracefunc func, PyObject *self,
4233 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004234{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004235 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004236 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004237 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004238 if (value == NULL) {
4239 value = Py_None;
4240 Py_INCREF(value);
4241 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004242 PyErr_NormalizeException(&type, &value, &orig_traceback);
4243 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004244 arg = PyTuple_Pack(3, type, value, traceback);
4245 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004246 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004247 return;
4248 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004249 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004250 Py_DECREF(arg);
4251 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004252 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004253 else {
4254 Py_XDECREF(type);
4255 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004256 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004257 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004258}
4259
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004260static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004261call_trace_protected(Py_tracefunc func, PyObject *obj,
4262 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004263 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004264{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004265 PyObject *type, *value, *traceback;
4266 int err;
4267 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004268 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004269 if (err == 0)
4270 {
4271 PyErr_Restore(type, value, traceback);
4272 return 0;
4273 }
4274 else {
4275 Py_XDECREF(type);
4276 Py_XDECREF(value);
4277 Py_XDECREF(traceback);
4278 return -1;
4279 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004280}
4281
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004282static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004283call_trace(Py_tracefunc func, PyObject *obj,
4284 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004285 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004286{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004287 int result;
4288 if (tstate->tracing)
4289 return 0;
4290 tstate->tracing++;
4291 tstate->use_tracing = 0;
4292 result = func(obj, frame, what, arg);
4293 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4294 || (tstate->c_profilefunc != NULL));
4295 tstate->tracing--;
4296 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004297}
4298
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004299PyObject *
4300_PyEval_CallTracing(PyObject *func, PyObject *args)
4301{
Victor Stinner50b48572018-11-01 01:51:40 +01004302 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004303 int save_tracing = tstate->tracing;
4304 int save_use_tracing = tstate->use_tracing;
4305 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004307 tstate->tracing = 0;
4308 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4309 || (tstate->c_profilefunc != NULL));
4310 result = PyObject_Call(func, args, NULL);
4311 tstate->tracing = save_tracing;
4312 tstate->use_tracing = save_use_tracing;
4313 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004314}
4315
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004316/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004317static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004318maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004319 PyThreadState *tstate, PyFrameObject *frame,
4320 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004321{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004322 int result = 0;
4323 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004325 /* If the last instruction executed isn't in the current
4326 instruction window, reset the window.
4327 */
4328 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4329 PyAddrPair bounds;
4330 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4331 &bounds);
4332 *instr_lb = bounds.ap_lower;
4333 *instr_ub = bounds.ap_upper;
4334 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004335 /* If the last instruction falls at the start of a line or if it
4336 represents a jump backwards, update the frame's line number and
4337 then call the trace function if we're tracing source lines.
4338 */
4339 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004340 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004341 if (frame->f_trace_lines) {
4342 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4343 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004344 }
George King20faa682017-10-18 17:44:22 -07004345 /* Always emit an opcode event if we're tracing all opcodes. */
4346 if (frame->f_trace_opcodes) {
4347 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4348 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004349 *instr_prev = frame->f_lasti;
4350 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004351}
4352
Fred Drake5755ce62001-06-27 19:19:46 +00004353void
4354PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004355{
Victor Stinner50b48572018-11-01 01:51:40 +01004356 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004357 PyObject *temp = tstate->c_profileobj;
4358 Py_XINCREF(arg);
4359 tstate->c_profilefunc = NULL;
4360 tstate->c_profileobj = NULL;
4361 /* Must make sure that tracing is not ignored if 'temp' is freed */
4362 tstate->use_tracing = tstate->c_tracefunc != NULL;
4363 Py_XDECREF(temp);
4364 tstate->c_profilefunc = func;
4365 tstate->c_profileobj = arg;
4366 /* Flag that tracing or profiling is turned on */
4367 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004368}
4369
4370void
4371PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4372{
Victor Stinner50b48572018-11-01 01:51:40 +01004373 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004374 PyObject *temp = tstate->c_traceobj;
4375 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4376 Py_XINCREF(arg);
4377 tstate->c_tracefunc = NULL;
4378 tstate->c_traceobj = NULL;
4379 /* Must make sure that profiling is not ignored if 'temp' is freed */
4380 tstate->use_tracing = tstate->c_profilefunc != NULL;
4381 Py_XDECREF(temp);
4382 tstate->c_tracefunc = func;
4383 tstate->c_traceobj = arg;
4384 /* Flag that tracing or profiling is turned on */
4385 tstate->use_tracing = ((func != NULL)
4386 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004387}
4388
Yury Selivanov75445082015-05-11 22:57:16 -04004389void
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004390_PyEval_SetCoroutineOriginTrackingDepth(int new_depth)
4391{
4392 assert(new_depth >= 0);
Victor Stinner50b48572018-11-01 01:51:40 +01004393 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004394 tstate->coroutine_origin_tracking_depth = new_depth;
4395}
4396
4397int
4398_PyEval_GetCoroutineOriginTrackingDepth(void)
4399{
Victor Stinner50b48572018-11-01 01:51:40 +01004400 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004401 return tstate->coroutine_origin_tracking_depth;
4402}
4403
4404void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004405_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004406{
Victor Stinner50b48572018-11-01 01:51:40 +01004407 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanov75445082015-05-11 22:57:16 -04004408
Yury Selivanov75445082015-05-11 22:57:16 -04004409 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004410 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004411}
4412
4413PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004414_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004415{
Victor Stinner50b48572018-11-01 01:51:40 +01004416 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanov75445082015-05-11 22:57:16 -04004417 return tstate->coroutine_wrapper;
4418}
4419
Yury Selivanoveb636452016-09-08 22:01:51 -07004420void
4421_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4422{
Victor Stinner50b48572018-11-01 01:51:40 +01004423 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004424
4425 Py_XINCREF(firstiter);
4426 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4427}
4428
4429PyObject *
4430_PyEval_GetAsyncGenFirstiter(void)
4431{
Victor Stinner50b48572018-11-01 01:51:40 +01004432 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004433 return tstate->async_gen_firstiter;
4434}
4435
4436void
4437_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4438{
Victor Stinner50b48572018-11-01 01:51:40 +01004439 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004440
4441 Py_XINCREF(finalizer);
4442 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4443}
4444
4445PyObject *
4446_PyEval_GetAsyncGenFinalizer(void)
4447{
Victor Stinner50b48572018-11-01 01:51:40 +01004448 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004449 return tstate->async_gen_finalizer;
4450}
4451
Guido van Rossumb209a111997-04-29 18:18:01 +00004452PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004453PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004454{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004455 PyFrameObject *current_frame = PyEval_GetFrame();
4456 if (current_frame == NULL)
Victor Stinnercaba55b2018-08-03 15:33:52 +02004457 return _PyInterpreterState_GET_UNSAFE()->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004458 else
4459 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004460}
4461
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004462/* Convenience function to get a builtin from its name */
4463PyObject *
4464_PyEval_GetBuiltinId(_Py_Identifier *name)
4465{
4466 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4467 if (attr) {
4468 Py_INCREF(attr);
4469 }
4470 else if (!PyErr_Occurred()) {
4471 PyErr_SetObject(PyExc_AttributeError, _PyUnicode_FromId(name));
4472 }
4473 return attr;
4474}
4475
Guido van Rossumb209a111997-04-29 18:18:01 +00004476PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004477PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004478{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004479 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004480 if (current_frame == NULL) {
4481 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004482 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004483 }
4484
4485 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4486 return NULL;
4487
4488 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004489 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004490}
4491
Guido van Rossumb209a111997-04-29 18:18:01 +00004492PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004493PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004494{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004495 PyFrameObject *current_frame = PyEval_GetFrame();
4496 if (current_frame == NULL)
4497 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004498
4499 assert(current_frame->f_globals != NULL);
4500 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004501}
4502
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004503PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004504PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004505{
Victor Stinner50b48572018-11-01 01:51:40 +01004506 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004507 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004508}
4509
Guido van Rossum6135a871995-01-09 17:53:26 +00004510int
Tim Peters5ba58662001-07-16 02:29:45 +00004511PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004512{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004513 PyFrameObject *current_frame = PyEval_GetFrame();
4514 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004516 if (current_frame != NULL) {
4517 const int codeflags = current_frame->f_code->co_flags;
4518 const int compilerflags = codeflags & PyCF_MASK;
4519 if (compilerflags) {
4520 result = 1;
4521 cf->cf_flags |= compilerflags;
4522 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004523#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004524 if (codeflags & CO_GENERATOR_ALLOWED) {
4525 result = 1;
4526 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4527 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004528#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004529 }
4530 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004531}
4532
Guido van Rossum3f5da241990-12-20 15:06:42 +00004533
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004534const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004535PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004536{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004537 if (PyMethod_Check(func))
4538 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4539 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004540 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004541 else if (PyCFunction_Check(func))
4542 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4543 else
4544 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004545}
4546
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004547const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004548PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004549{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004550 if (PyMethod_Check(func))
4551 return "()";
4552 else if (PyFunction_Check(func))
4553 return "()";
4554 else if (PyCFunction_Check(func))
4555 return "()";
4556 else
4557 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004558}
4559
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004560#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004561if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004562 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4563 tstate, tstate->frame, \
4564 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004565 x = NULL; \
4566 } \
4567 else { \
4568 x = call; \
4569 if (tstate->c_profilefunc != NULL) { \
4570 if (x == NULL) { \
4571 call_trace_protected(tstate->c_profilefunc, \
4572 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004573 tstate, tstate->frame, \
4574 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004575 /* XXX should pass (type, value, tb) */ \
4576 } else { \
4577 if (call_trace(tstate->c_profilefunc, \
4578 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004579 tstate, tstate->frame, \
4580 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004581 Py_DECREF(x); \
4582 x = NULL; \
4583 } \
4584 } \
4585 } \
4586 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004587} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004588 x = call; \
4589 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004590
Victor Stinner415c5102017-01-11 00:54:57 +01004591/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4592 to reduce the stack consumption. */
4593Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Benjamin Peterson4fd64b92016-09-09 14:57:58 -07004594call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004595{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004596 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004597 PyObject *func = *pfunc;
4598 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004599 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4600 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004601 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004603 /* Always dispatch PyCFunction first, because these are
4604 presumed to be the most frequent callable object.
4605 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004606 if (PyCFunction_Check(func)) {
Victor Stinner50b48572018-11-01 01:51:40 +01004607 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004608 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004609 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004610 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
Victor Stinner50b48572018-11-01 01:51:40 +01004611 PyThreadState *tstate = _PyThreadState_GET();
jdemeyer56868f92018-07-21 10:30:59 +02004612 if (nargs > 0 && tstate->use_tracing) {
4613 /* We need to create a temporary bound method as argument
4614 for profiling.
4615
4616 If nargs == 0, then this cannot work because we have no
4617 "self". In any case, the call itself would raise
4618 TypeError (foo needs an argument), so we just skip
4619 profiling. */
4620 PyObject *self = stack[0];
4621 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
jdemeyer147d9552018-07-23 18:41:20 +02004622 if (func != NULL) {
4623 C_TRACE(x, _PyCFunction_FastCallKeywords(func,
4624 stack+1, nargs-1,
4625 kwnames));
4626 Py_DECREF(func);
INADA Naoki93fac8d2017-03-07 14:24:37 +09004627 }
jdemeyer147d9552018-07-23 18:41:20 +02004628 else {
4629 x = NULL;
4630 }
INADA Naoki93fac8d2017-03-07 14:24:37 +09004631 }
4632 else {
4633 x = _PyMethodDescr_FastCallKeywords(func, stack, nargs, kwnames);
4634 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004635 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004636 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004637 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
Victor Stinnerb69ee8c2016-11-28 18:32:31 +01004638 /* Optimize access to bound methods. Reuse the Python stack
4639 to pass 'self' as the first argument, replace 'func'
4640 with 'self'. It avoids the creation of a new temporary tuple
4641 for arguments (to replace func with self) when the method uses
4642 FASTCALL. */
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004643 PyObject *self = PyMethod_GET_SELF(func);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004644 Py_INCREF(self);
4645 func = PyMethod_GET_FUNCTION(func);
4646 Py_INCREF(func);
4647 Py_SETREF(*pfunc, self);
4648 nargs++;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004649 stack--;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004650 }
4651 else {
4652 Py_INCREF(func);
4653 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004654
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004655 if (PyFunction_Check(func)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004656 x = _PyFunction_FastCallKeywords(func, stack, nargs, kwnames);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004657 }
4658 else {
4659 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4660 }
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004661 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004662 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004663
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004664 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4665
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004666 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004667 while ((*pp_stack) > pfunc) {
4668 w = EXT_POP(*pp_stack);
4669 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004670 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004672 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004673}
4674
Jeremy Hylton52820442001-01-03 23:52:36 +00004675static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004676do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004677{
jdemeyere89de732018-09-19 12:06:20 +02004678 PyObject *result;
4679
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004680 if (PyCFunction_Check(func)) {
Victor Stinner50b48572018-11-01 01:51:40 +01004681 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004682 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004683 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004684 }
jdemeyere89de732018-09-19 12:06:20 +02004685 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
Victor Stinner50b48572018-11-01 01:51:40 +01004686 PyThreadState *tstate = _PyThreadState_GET();
jdemeyere89de732018-09-19 12:06:20 +02004687 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
4688 if (nargs > 0 && tstate->use_tracing) {
4689 /* We need to create a temporary bound method as argument
4690 for profiling.
4691
4692 If nargs == 0, then this cannot work because we have no
4693 "self". In any case, the call itself would raise
4694 TypeError (foo needs an argument), so we just skip
4695 profiling. */
4696 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
4697 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4698 if (func == NULL) {
4699 return NULL;
4700 }
4701
4702 C_TRACE(result, _PyCFunction_FastCallDict(func,
Victor Stinnerd17a6932018-11-09 16:56:48 +01004703 &_PyTuple_ITEMS(callargs)[1],
jdemeyere89de732018-09-19 12:06:20 +02004704 nargs - 1,
4705 kwdict));
4706 Py_DECREF(func);
4707 return result;
4708 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004709 }
jdemeyere89de732018-09-19 12:06:20 +02004710 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00004711}
4712
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004713/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004714 nb_index slot defined, and store in *pi.
4715 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004716 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004717 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004718*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004719int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004720_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004721{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004722 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004723 Py_ssize_t x;
4724 if (PyIndex_Check(v)) {
4725 x = PyNumber_AsSsize_t(v, NULL);
4726 if (x == -1 && PyErr_Occurred())
4727 return 0;
4728 }
4729 else {
4730 PyErr_SetString(PyExc_TypeError,
4731 "slice indices must be integers or "
4732 "None or have an __index__ method");
4733 return 0;
4734 }
4735 *pi = x;
4736 }
4737 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004738}
4739
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004740int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004741_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004742{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004743 Py_ssize_t x;
4744 if (PyIndex_Check(v)) {
4745 x = PyNumber_AsSsize_t(v, NULL);
4746 if (x == -1 && PyErr_Occurred())
4747 return 0;
4748 }
4749 else {
4750 PyErr_SetString(PyExc_TypeError,
4751 "slice indices must be integers or "
4752 "have an __index__ method");
4753 return 0;
4754 }
4755 *pi = x;
4756 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004757}
4758
4759
Guido van Rossum486364b2007-06-30 05:01:58 +00004760#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004761 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004762
Guido van Rossumb209a111997-04-29 18:18:01 +00004763static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004764cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004765{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004766 int res = 0;
4767 switch (op) {
4768 case PyCmp_IS:
4769 res = (v == w);
4770 break;
4771 case PyCmp_IS_NOT:
4772 res = (v != w);
4773 break;
4774 case PyCmp_IN:
4775 res = PySequence_Contains(w, v);
4776 if (res < 0)
4777 return NULL;
4778 break;
4779 case PyCmp_NOT_IN:
4780 res = PySequence_Contains(w, v);
4781 if (res < 0)
4782 return NULL;
4783 res = !res;
4784 break;
4785 case PyCmp_EXC_MATCH:
4786 if (PyTuple_Check(w)) {
4787 Py_ssize_t i, length;
4788 length = PyTuple_Size(w);
4789 for (i = 0; i < length; i += 1) {
4790 PyObject *exc = PyTuple_GET_ITEM(w, i);
4791 if (!PyExceptionClass_Check(exc)) {
4792 PyErr_SetString(PyExc_TypeError,
4793 CANNOT_CATCH_MSG);
4794 return NULL;
4795 }
4796 }
4797 }
4798 else {
4799 if (!PyExceptionClass_Check(w)) {
4800 PyErr_SetString(PyExc_TypeError,
4801 CANNOT_CATCH_MSG);
4802 return NULL;
4803 }
4804 }
4805 res = PyErr_GivenExceptionMatches(v, w);
4806 break;
4807 default:
4808 return PyObject_RichCompare(v, w, op);
4809 }
4810 v = res ? Py_True : Py_False;
4811 Py_INCREF(v);
4812 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004813}
4814
Thomas Wouters52152252000-08-17 22:55:00 +00004815static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004816import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
4817{
4818 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004819 PyObject *import_func, *res;
4820 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004821
4822 import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
4823 if (import_func == NULL) {
4824 PyErr_SetString(PyExc_ImportError, "__import__ not found");
4825 return NULL;
4826 }
4827
4828 /* Fast path for not overloaded __import__. */
Victor Stinnercaba55b2018-08-03 15:33:52 +02004829 if (import_func == _PyInterpreterState_GET_UNSAFE()->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004830 int ilevel = _PyLong_AsInt(level);
4831 if (ilevel == -1 && PyErr_Occurred()) {
4832 return NULL;
4833 }
4834 res = PyImport_ImportModuleLevelObject(
4835 name,
4836 f->f_globals,
4837 f->f_locals == NULL ? Py_None : f->f_locals,
4838 fromlist,
4839 ilevel);
4840 return res;
4841 }
4842
4843 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004844
4845 stack[0] = name;
4846 stack[1] = f->f_globals;
4847 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
4848 stack[3] = fromlist;
4849 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02004850 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004851 Py_DECREF(import_func);
4852 return res;
4853}
4854
4855static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004856import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004857{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004858 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02004859 _Py_IDENTIFIER(__name__);
Xiang Zhang4830f582017-03-21 11:13:42 +08004860 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004861
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004862 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02004863 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004864 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004865 /* Issue #17636: in case this failed because of a circular relative
4866 import, try to fallback on reading the module directly from
4867 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02004868 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07004869 if (pkgname == NULL) {
4870 goto error;
4871 }
Oren Milman6db70332017-09-19 14:23:01 +03004872 if (!PyUnicode_Check(pkgname)) {
4873 Py_CLEAR(pkgname);
4874 goto error;
4875 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004876 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07004877 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08004878 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004879 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07004880 }
Eric Snow3f9eee62017-09-15 16:35:20 -06004881 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004882 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07004883 if (x == NULL) {
4884 goto error;
4885 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004886 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004887 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07004888 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004889 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004890 if (pkgname == NULL) {
4891 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
4892 if (pkgname_or_unknown == NULL) {
4893 Py_XDECREF(pkgpath);
4894 return NULL;
4895 }
4896 } else {
4897 pkgname_or_unknown = pkgname;
4898 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004899
4900 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
4901 PyErr_Clear();
Xiang Zhang4830f582017-03-21 11:13:42 +08004902 errmsg = PyUnicode_FromFormat(
4903 "cannot import name %R from %R (unknown location)",
4904 name, pkgname_or_unknown
4905 );
4906 /* NULL check for errmsg done by PyErr_SetImportError. */
4907 PyErr_SetImportError(errmsg, pkgname, NULL);
4908 }
4909 else {
4910 errmsg = PyUnicode_FromFormat(
4911 "cannot import name %R from %R (%S)",
4912 name, pkgname_or_unknown, pkgpath
4913 );
4914 /* NULL check for errmsg done by PyErr_SetImportError. */
4915 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004916 }
4917
Xiang Zhang4830f582017-03-21 11:13:42 +08004918 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004919 Py_XDECREF(pkgname_or_unknown);
4920 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07004921 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00004922}
Guido van Rossumac7be682001-01-17 15:42:30 +00004923
Thomas Wouters52152252000-08-17 22:55:00 +00004924static int
4925import_all_from(PyObject *locals, PyObject *v)
4926{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004927 _Py_IDENTIFIER(__all__);
4928 _Py_IDENTIFIER(__dict__);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08004929 _Py_IDENTIFIER(__name__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004930 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004931 int skip_leading_underscores = 0;
4932 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004933
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004934 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
4935 return -1; /* Unexpected error */
4936 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004937 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004938 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
4939 return -1;
4940 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004941 if (dict == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004942 PyErr_SetString(PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004943 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004944 return -1;
4945 }
4946 all = PyMapping_Keys(dict);
4947 Py_DECREF(dict);
4948 if (all == NULL)
4949 return -1;
4950 skip_leading_underscores = 1;
4951 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004952
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004953 for (pos = 0, err = 0; ; pos++) {
4954 name = PySequence_GetItem(all, pos);
4955 if (name == NULL) {
4956 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4957 err = -1;
4958 else
4959 PyErr_Clear();
4960 break;
4961 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08004962 if (!PyUnicode_Check(name)) {
4963 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
4964 if (modname == NULL) {
4965 Py_DECREF(name);
4966 err = -1;
4967 break;
4968 }
4969 if (!PyUnicode_Check(modname)) {
4970 PyErr_Format(PyExc_TypeError,
4971 "module __name__ must be a string, not %.100s",
4972 Py_TYPE(modname)->tp_name);
4973 }
4974 else {
4975 PyErr_Format(PyExc_TypeError,
4976 "%s in %U.%s must be str, not %.100s",
4977 skip_leading_underscores ? "Key" : "Item",
4978 modname,
4979 skip_leading_underscores ? "__dict__" : "__all__",
4980 Py_TYPE(name)->tp_name);
4981 }
4982 Py_DECREF(modname);
4983 Py_DECREF(name);
4984 err = -1;
4985 break;
4986 }
4987 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03004988 if (PyUnicode_READY(name) == -1) {
4989 Py_DECREF(name);
4990 err = -1;
4991 break;
4992 }
4993 if (PyUnicode_READ_CHAR(name, 0) == '_') {
4994 Py_DECREF(name);
4995 continue;
4996 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004997 }
4998 value = PyObject_GetAttr(v, name);
4999 if (value == NULL)
5000 err = -1;
5001 else if (PyDict_CheckExact(locals))
5002 err = PyDict_SetItem(locals, name, value);
5003 else
5004 err = PyObject_SetItem(locals, name, value);
5005 Py_DECREF(name);
5006 Py_XDECREF(value);
5007 if (err != 0)
5008 break;
5009 }
5010 Py_DECREF(all);
5011 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005012}
5013
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005014static int
5015check_args_iterable(PyObject *func, PyObject *args)
5016{
5017 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
5018 PyErr_Format(PyExc_TypeError,
5019 "%.200s%.200s argument after * "
5020 "must be an iterable, not %.200s",
5021 PyEval_GetFuncName(func),
5022 PyEval_GetFuncDesc(func),
5023 args->ob_type->tp_name);
5024 return -1;
5025 }
5026 return 0;
5027}
5028
5029static void
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005030format_kwargs_error(PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005031{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005032 /* _PyDict_MergeEx raises attribute
5033 * error (percolated from an attempt
5034 * to get 'keys' attribute) instead of
5035 * a type error if its second argument
5036 * is not a mapping.
5037 */
5038 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
5039 PyErr_Format(PyExc_TypeError,
5040 "%.200s%.200s argument after ** "
5041 "must be a mapping, not %.200s",
5042 PyEval_GetFuncName(func),
5043 PyEval_GetFuncDesc(func),
5044 kwargs->ob_type->tp_name);
5045 }
5046 else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
5047 PyObject *exc, *val, *tb;
5048 PyErr_Fetch(&exc, &val, &tb);
5049 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
5050 PyObject *key = PyTuple_GET_ITEM(val, 0);
5051 if (!PyUnicode_Check(key)) {
5052 PyErr_Format(PyExc_TypeError,
5053 "%.200s%.200s keywords must be strings",
5054 PyEval_GetFuncName(func),
5055 PyEval_GetFuncDesc(func));
5056 } else {
5057 PyErr_Format(PyExc_TypeError,
5058 "%.200s%.200s got multiple "
5059 "values for keyword argument '%U'",
5060 PyEval_GetFuncName(func),
5061 PyEval_GetFuncDesc(func),
5062 key);
5063 }
5064 Py_XDECREF(exc);
5065 Py_XDECREF(val);
5066 Py_XDECREF(tb);
5067 }
5068 else {
5069 PyErr_Restore(exc, val, tb);
5070 }
5071 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005072}
5073
Guido van Rossumac7be682001-01-17 15:42:30 +00005074static void
Neal Norwitzda059e32007-08-26 05:33:45 +00005075format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005076{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005077 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005078
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005079 if (!obj)
5080 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005081
Serhiy Storchaka06515832016-11-20 09:13:07 +02005082 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005083 if (!obj_str)
5084 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005085
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005086 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005087}
Guido van Rossum950361c1997-01-24 13:49:28 +00005088
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005089static void
5090format_exc_unbound(PyCodeObject *co, int oparg)
5091{
5092 PyObject *name;
5093 /* Don't stomp existing exception */
5094 if (PyErr_Occurred())
5095 return;
5096 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5097 name = PyTuple_GET_ITEM(co->co_cellvars,
5098 oparg);
5099 format_exc_check_arg(
5100 PyExc_UnboundLocalError,
5101 UNBOUNDLOCAL_ERROR_MSG,
5102 name);
5103 } else {
5104 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5105 PyTuple_GET_SIZE(co->co_cellvars));
5106 format_exc_check_arg(PyExc_NameError,
5107 UNBOUNDFREE_ERROR_MSG, name);
5108 }
5109}
5110
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005111static void
5112format_awaitable_error(PyTypeObject *type, int prevopcode)
5113{
5114 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5115 if (prevopcode == BEFORE_ASYNC_WITH) {
5116 PyErr_Format(PyExc_TypeError,
5117 "'async with' received an object from __aenter__ "
5118 "that does not implement __await__: %.100s",
5119 type->tp_name);
5120 }
5121 else if (prevopcode == WITH_CLEANUP_START) {
5122 PyErr_Format(PyExc_TypeError,
5123 "'async with' received an object from __aexit__ "
5124 "that does not implement __await__: %.100s",
5125 type->tp_name);
5126 }
5127 }
5128}
5129
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005130static PyObject *
5131unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005132 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005133{
5134 PyObject *res;
5135 if (Py_REFCNT(v) == 2) {
5136 /* In the common case, there are 2 references to the value
5137 * stored in 'variable' when the += is performed: one on the
5138 * value stack (in 'v') and one still stored in the
5139 * 'variable'. We try to delete the variable now to reduce
5140 * the refcnt to 1.
5141 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005142 int opcode, oparg;
5143 NEXTOPARG();
5144 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005145 case STORE_FAST:
5146 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005147 PyObject **fastlocals = f->f_localsplus;
5148 if (GETLOCAL(oparg) == v)
5149 SETLOCAL(oparg, NULL);
5150 break;
5151 }
5152 case STORE_DEREF:
5153 {
5154 PyObject **freevars = (f->f_localsplus +
5155 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005156 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005157 if (PyCell_GET(c) == v) {
5158 PyCell_SET(c, NULL);
5159 Py_DECREF(v);
5160 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005161 break;
5162 }
5163 case STORE_NAME:
5164 {
5165 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005166 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005167 PyObject *locals = f->f_locals;
Serhiy Storchaka8905fcc2018-12-11 08:38:03 +02005168 if (locals && PyDict_CheckExact(locals) &&
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005169 PyDict_GetItem(locals, name) == v) {
5170 if (PyDict_DelItem(locals, name) != 0) {
5171 PyErr_Clear();
5172 }
5173 }
5174 break;
5175 }
5176 }
5177 }
5178 res = v;
5179 PyUnicode_Append(&res, w);
5180 return res;
5181}
5182
Guido van Rossum950361c1997-01-24 13:49:28 +00005183#ifdef DYNAMIC_EXECUTION_PROFILE
5184
Skip Montanarof118cb12001-10-15 20:51:38 +00005185static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005186getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005187{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005188 int i;
5189 PyObject *l = PyList_New(256);
5190 if (l == NULL) return NULL;
5191 for (i = 0; i < 256; i++) {
5192 PyObject *x = PyLong_FromLong(a[i]);
5193 if (x == NULL) {
5194 Py_DECREF(l);
5195 return NULL;
5196 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005197 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005198 }
5199 for (i = 0; i < 256; i++)
5200 a[i] = 0;
5201 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005202}
5203
5204PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005205_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005206{
5207#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005208 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005209#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005210 int i;
5211 PyObject *l = PyList_New(257);
5212 if (l == NULL) return NULL;
5213 for (i = 0; i < 257; i++) {
5214 PyObject *x = getarray(dxpairs[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 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005222#endif
5223}
5224
5225#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005226
5227Py_ssize_t
5228_PyEval_RequestCodeExtraIndex(freefunc free)
5229{
Victor Stinnercaba55b2018-08-03 15:33:52 +02005230 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -07005231 Py_ssize_t new_index;
5232
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005233 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005234 return -1;
5235 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005236 new_index = interp->co_extra_user_count++;
5237 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005238 return new_index;
5239}
Łukasz Langaa785c872016-09-09 17:37:37 -07005240
5241static void
5242dtrace_function_entry(PyFrameObject *f)
5243{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005244 const char *filename;
5245 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005246 int lineno;
5247
5248 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5249 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5250 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5251
5252 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5253}
5254
5255static void
5256dtrace_function_return(PyFrameObject *f)
5257{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005258 const char *filename;
5259 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005260 int lineno;
5261
5262 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5263 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5264 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5265
5266 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5267}
5268
5269/* DTrace equivalent of maybe_call_line_trace. */
5270static void
5271maybe_dtrace_line(PyFrameObject *frame,
5272 int *instr_lb, int *instr_ub, int *instr_prev)
5273{
5274 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005275 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005276
5277 /* If the last instruction executed isn't in the current
5278 instruction window, reset the window.
5279 */
5280 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5281 PyAddrPair bounds;
5282 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5283 &bounds);
5284 *instr_lb = bounds.ap_lower;
5285 *instr_ub = bounds.ap_upper;
5286 }
5287 /* If the last instruction falls at the start of a line or if
5288 it represents a jump backwards, update the frame's line
5289 number and call the trace function. */
5290 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5291 frame->f_lineno = line;
5292 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5293 if (!co_filename)
5294 co_filename = "?";
5295 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5296 if (!co_name)
5297 co_name = "?";
5298 PyDTrace_LINE(co_filename, co_name, line);
5299 }
5300 *instr_prev = frame->f_lasti;
5301}