blob: 3db7c7c92a0e04c4406517b937c1c2c2d225460f [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;
174 create_gil();
Victor Stinner50b48572018-11-01 01:51:40 +0100175 take_gil(_PyThreadState_GET());
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600176 _PyRuntime.ceval.pending.main_thread = PyThread_get_thread_ident();
177 if (!_PyRuntime.ceval.pending.lock)
178 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000179}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000180
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000181void
Antoine Pitrou1df15362010-09-13 14:16:46 +0000182_PyEval_FiniThreads(void)
183{
184 if (!gil_created())
185 return;
186 destroy_gil();
187 assert(!gil_created());
188}
189
190void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000191PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000192{
Victor Stinner50b48572018-11-01 01:51:40 +0100193 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 if (tstate == NULL)
195 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
196 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000197}
198
199void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000200PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000201{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100203 We therefore avoid PyThreadState_Get() which dumps a fatal error
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 in debug mode.
205 */
Victor Stinner50b48572018-11-01 01:51:40 +0100206 drop_gil(_PyThreadState_GET());
Guido van Rossum25ce5661997-08-02 03:10:38 +0000207}
208
209void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000210PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000211{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000212 if (tstate == NULL)
213 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
214 /* Check someone has called PyEval_InitThreads() to create the lock */
215 assert(gil_created());
216 take_gil(tstate);
217 if (PyThreadState_Swap(tstate) != NULL)
218 Py_FatalError(
219 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000220}
221
222void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000223PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000224{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 if (tstate == NULL)
226 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
227 if (PyThreadState_Swap(NULL) != tstate)
228 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
229 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000230}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000231
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200232/* This function is called from PyOS_AfterFork_Child to destroy all threads
233 * which are not running in the child process, and clear internal locks
234 * which might be held by those threads.
235 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000236
237void
238PyEval_ReInitThreads(void)
239{
Victor Stinner50b48572018-11-01 01:51:40 +0100240 PyThreadState *current_tstate = _PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 if (!gil_created())
243 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 recreate_gil();
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600245 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200246 take_gil(current_tstate);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600247 _PyRuntime.ceval.pending.main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000248
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200249 /* Destroy all threads except the current one */
250 _PyThreadState_DeleteExcept(current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000251}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000252
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000253/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600254 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000255
256void
257_PyEval_SignalAsyncExc(void)
258{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000260}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000261
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000262PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000263PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000264{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 PyThreadState *tstate = PyThreadState_Swap(NULL);
266 if (tstate == NULL)
267 Py_FatalError("PyEval_SaveThread: NULL tstate");
Victor Stinner2914bb32018-01-29 11:57:45 +0100268 assert(gil_created());
269 drop_gil(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000271}
272
273void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000274PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000275{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 if (tstate == NULL)
277 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Victor Stinner2914bb32018-01-29 11:57:45 +0100278 assert(gil_created());
279
280 int err = errno;
281 take_gil(tstate);
282 /* _Py_Finalizing is protected by the GIL */
283 if (_Py_IsFinalizing() && !_Py_CURRENTLY_FINALIZING(tstate)) {
284 drop_gil(tstate);
285 PyThread_exit_thread();
286 Py_UNREACHABLE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 }
Victor Stinner2914bb32018-01-29 11:57:45 +0100288 errno = err;
289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000291}
292
293
Guido van Rossuma9672091994-09-14 13:31:22 +0000294/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
295 signal handlers or Mac I/O completion routines) can schedule calls
296 to a function to be called synchronously.
297 The synchronous function is called with one void* argument.
298 It should return 0 for success or -1 for failure -- failure should
299 be accompanied by an exception.
300
301 If registry succeeds, the registry function returns 0; if it fails
302 (e.g. due to too many pending calls) it returns -1 (without setting
303 an exception condition).
304
305 Note that because registry may occur from within signal handlers,
306 or other asynchronous events, calling malloc() is unsafe!
307
Guido van Rossuma9672091994-09-14 13:31:22 +0000308 Any thread can schedule pending calls, but only the main thread
309 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000310 There is no facility to schedule calls to a particular thread, but
311 that should be easy to change, should that ever be required. In
312 that case, the static variables here should go into the python
313 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000314*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000315
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200316void
317_PyEval_SignalReceived(void)
318{
319 /* bpo-30703: Function called when the C signal handler of Python gets a
320 signal. We cannot queue a callback using Py_AddPendingCall() since
321 that function is not async-signal-safe. */
Eric Snowfdf282d2019-01-11 14:26:55 -0700322 SIGNAL_PENDING_SIGNALS();
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200323}
324
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200325/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000326 scheduling to be made from any thread, and even from an executing
327 callback.
328 */
329
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000330int
331Py_AddPendingCall(int (*func)(void *), void *arg)
332{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 int i, j, result=0;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600334 PyThread_type_lock lock = _PyRuntime.ceval.pending.lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 /* try a few times for the lock. Since this mechanism is used
337 * for signal handling (on the main thread), there is a (slim)
338 * chance that a signal is delivered on the same thread while we
339 * hold the lock during the Py_MakePendingCalls() function.
340 * This avoids a deadlock in that case.
341 * Note that signals can be delivered on any thread. In particular,
342 * on Windows, a SIGINT is delivered on a system-created worker
343 * thread.
344 * We also check for lock being NULL, in the unlikely case that
345 * this function is called before any bytecode evaluation takes place.
346 */
347 if (lock != NULL) {
348 for (i = 0; i<100; i++) {
349 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
350 break;
351 }
352 if (i == 100)
353 return -1;
354 }
355
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600356 i = _PyRuntime.ceval.pending.last;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 j = (i + 1) % NPENDINGCALLS;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600358 if (j == _PyRuntime.ceval.pending.first) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 result = -1; /* Queue full */
360 } else {
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600361 _PyRuntime.ceval.pending.calls[i].func = func;
362 _PyRuntime.ceval.pending.calls[i].arg = arg;
363 _PyRuntime.ceval.pending.last = j;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 }
365 /* signal main loop */
366 SIGNAL_PENDING_CALLS();
367 if (lock != NULL)
368 PyThread_release_lock(lock);
369 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000370}
371
Eric Snowfdf282d2019-01-11 14:26:55 -0700372static int
373handle_signals(void)
374{
375 /* Only handle signals on main thread. */
376 if (_PyRuntime.ceval.pending.main_thread &&
377 PyThread_get_thread_ident() != _PyRuntime.ceval.pending.main_thread)
378 {
379 return 0;
380 }
381
382 UNSIGNAL_PENDING_SIGNALS();
383 if (PyErr_CheckSignals() < 0) {
384 SIGNAL_PENDING_SIGNALS(); /* We're not done yet */
385 return -1;
386 }
387 return 0;
388}
389
390static int
391make_pending_calls(void)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000392{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200393 static int busy = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 /* only service pending calls on main thread */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600396 if (_PyRuntime.ceval.pending.main_thread &&
397 PyThread_get_thread_ident() != _PyRuntime.ceval.pending.main_thread)
398 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 return 0;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600400 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 /* don't perform recursive pending calls */
Eric Snowfdf282d2019-01-11 14:26:55 -0700403 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700405 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200406 busy = 1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200407 /* unsignal before starting to call callbacks, so that any callback
408 added in-between re-signals */
409 UNSIGNAL_PENDING_CALLS();
Eric Snowfdf282d2019-01-11 14:26:55 -0700410 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200411
Eric Snowfdf282d2019-01-11 14:26:55 -0700412 if (!_PyRuntime.ceval.pending.lock) {
413 /* initial allocation of the lock */
414 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
415 if (_PyRuntime.ceval.pending.lock == NULL) {
416 res = -1;
417 goto error;
418 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200419 }
420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 /* perform a bounded number of calls, in case of recursion */
Eric Snowfdf282d2019-01-11 14:26:55 -0700422 for (int i=0; i<NPENDINGCALLS; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 int j;
424 int (*func)(void *);
425 void *arg = NULL;
426
427 /* pop one item off the queue while holding the lock */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600428 PyThread_acquire_lock(_PyRuntime.ceval.pending.lock, WAIT_LOCK);
429 j = _PyRuntime.ceval.pending.first;
430 if (j == _PyRuntime.ceval.pending.last) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 func = NULL; /* Queue empty */
432 } else {
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600433 func = _PyRuntime.ceval.pending.calls[j].func;
434 arg = _PyRuntime.ceval.pending.calls[j].arg;
435 _PyRuntime.ceval.pending.first = (j + 1) % NPENDINGCALLS;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600437 PyThread_release_lock(_PyRuntime.ceval.pending.lock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 /* having released the lock, perform the callback */
439 if (func == NULL)
440 break;
Eric Snowfdf282d2019-01-11 14:26:55 -0700441 res = func(arg);
442 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200443 goto error;
444 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200446
Charles-François Natalif23339a2011-07-23 18:15:43 +0200447 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700448 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200449
450error:
451 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700452 SIGNAL_PENDING_CALLS();
453 return res;
454}
455
456/* Py_MakePendingCalls() is a simple wrapper for the sake
457 of backward-compatibility. */
458int
459Py_MakePendingCalls(void)
460{
461 assert(PyGILState_Check());
462
463 /* Python signal handler doesn't really queue a callback: it only signals
464 that a signal was received, see _PyEval_SignalReceived(). */
465 int res = handle_signals();
466 if (res != 0) {
467 return res;
468 }
469
470 res = make_pending_calls();
471 if (res != 0) {
472 return res;
473 }
474
475 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000476}
477
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000478/* The interpreter's recursion limit */
479
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000480#ifndef Py_DEFAULT_RECURSION_LIMIT
481#define Py_DEFAULT_RECURSION_LIMIT 1000
482#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600483
Eric Snow05351c12017-09-05 21:43:08 -0700484int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000485
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600486void
487_PyEval_Initialize(struct _ceval_runtime_state *state)
488{
489 state->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
490 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
491 _gil_initialize(&state->gil);
492}
493
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000494int
495Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000496{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600497 return _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000498}
499
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000500void
501Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000502{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600503 _PyRuntime.ceval.recursion_limit = new_limit;
504 _Py_CheckRecursionLimit = _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000505}
506
Armin Rigo2b3eb402003-10-28 12:05:48 +0000507/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
508 if the recursion_depth reaches _Py_CheckRecursionLimit.
509 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
510 to guarantee that _Py_CheckRecursiveCall() is regularly called.
511 Without USE_STACKCHECK, there is no need for this. */
512int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300513_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000514{
Victor Stinner50b48572018-11-01 01:51:40 +0100515 PyThreadState *tstate = _PyThreadState_GET();
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600516 int recursion_limit = _PyRuntime.ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000517
518#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700519 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 if (PyOS_CheckStack()) {
521 --tstate->recursion_depth;
522 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
523 return -1;
524 }
pdox18967932017-10-25 23:03:01 -0700525 /* Needed for ABI backwards-compatibility (see bpo-31857) */
Eric Snow05351c12017-09-05 21:43:08 -0700526 _Py_CheckRecursionLimit = recursion_limit;
pdox18967932017-10-25 23:03:01 -0700527#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 if (tstate->recursion_critical)
529 /* Somebody asked that we don't check for recursion. */
530 return 0;
531 if (tstate->overflowed) {
532 if (tstate->recursion_depth > recursion_limit + 50) {
533 /* Overflowing while handling an overflow. Give up. */
534 Py_FatalError("Cannot recover from stack overflow.");
535 }
536 return 0;
537 }
538 if (tstate->recursion_depth > recursion_limit) {
539 --tstate->recursion_depth;
540 tstate->overflowed = 1;
Yury Selivanovf488fb42015-07-03 01:04:23 -0400541 PyErr_Format(PyExc_RecursionError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 "maximum recursion depth exceeded%s",
543 where);
544 return -1;
545 }
546 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000547}
548
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400549static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000550static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000551
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600552#define _Py_TracingPossible _PyRuntime.ceval.tracing_possible
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000553
Guido van Rossum374a9221991-04-04 10:40:29 +0000554
Guido van Rossumb209a111997-04-29 18:18:01 +0000555PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000556PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000557{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 return PyEval_EvalCodeEx(co,
559 globals, locals,
560 (PyObject **)NULL, 0,
561 (PyObject **)NULL, 0,
562 (PyObject **)NULL, 0,
563 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000564}
565
566
567/* Interpreter main loop */
568
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000569PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000570PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 /* This is for backward compatibility with extension modules that
572 used this API; core interpreter code should call
573 PyEval_EvalFrameEx() */
574 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000575}
576
577PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000578PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000579{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200580 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
581 return interp->eval_frame(f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700582}
583
Victor Stinnerc6944e72016-11-11 02:13:35 +0100584PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700585_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
586{
Guido van Rossum950361c1997-01-24 13:49:28 +0000587#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000589#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200590 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300591 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200592 int opcode; /* Current opcode */
593 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200594 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 PyObject *retval = NULL; /* Return value */
Victor Stinner50b48572018-11-01 01:51:40 +0100596 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 is true when the line being executed has changed. The
604 initial values are such as to make this false the first
605 time it is tested. */
606 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000607
Serhiy Storchakaab874002016-09-11 13:48:15 +0300608 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000609 PyObject *names;
610 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000611
Brett Cannon368b4b72012-04-02 12:17:59 -0400612#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200613 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400614#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200615
Antoine Pitroub52ec782009-01-25 16:34:23 +0000616/* Computed GOTOs, or
617 the-optimization-commonly-but-improperly-known-as-"threaded code"
618 using gcc's labels-as-values extension
619 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
620
621 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000623 combined with a lookup table of jump addresses. However, since the
624 indirect jump instruction is shared by all opcodes, the CPU will have a
625 hard time making the right prediction for where to jump next (actually,
626 it will be always wrong except in the uncommon case of a sequence of
627 several identical opcodes).
628
629 "Threaded code" in contrast, uses an explicit jump table and an explicit
630 indirect jump instruction at the end of each opcode. Since the jump
631 instruction is at a different address for each opcode, the CPU will make a
632 separate prediction for each of these instructions, which is equivalent to
633 predicting the second opcode of each opcode pair. These predictions have
634 a much better chance to turn out valid, especially in small bytecode loops.
635
636 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000638 and potentially many more instructions (depending on the pipeline width).
639 A correctly predicted branch, however, is nearly free.
640
641 At the time of this writing, the "threaded code" version is up to 15-20%
642 faster than the normal "switch" version, depending on the compiler and the
643 CPU architecture.
644
645 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
646 because it would render the measurements invalid.
647
648
649 NOTE: care must be taken that the compiler doesn't try to "optimize" the
650 indirect jumps by sharing them between all opcodes. Such optimizations
651 can be disabled on gcc by using the -fno-gcse flag (or possibly
652 -fno-crossjumping).
653*/
654
Antoine Pitrou042b1282010-08-13 21:15:58 +0000655#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000656#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000657#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000658#endif
659
Antoine Pitrou042b1282010-08-13 21:15:58 +0000660#ifdef HAVE_COMPUTED_GOTOS
661 #ifndef USE_COMPUTED_GOTOS
662 #define USE_COMPUTED_GOTOS 1
663 #endif
664#else
665 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
666 #error "Computed gotos are not supported on this compiler."
667 #endif
668 #undef USE_COMPUTED_GOTOS
669 #define USE_COMPUTED_GOTOS 0
670#endif
671
672#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000673/* Import the static jump table */
674#include "opcode_targets.h"
675
Antoine Pitroub52ec782009-01-25 16:34:23 +0000676#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -0700677 op: \
678 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000679
Antoine Pitroub52ec782009-01-25 16:34:23 +0000680#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600682 if (!_Py_atomic_load_relaxed(&_PyRuntime.ceval.eval_breaker)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 FAST_DISPATCH(); \
684 } \
685 continue; \
686 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000687
688#ifdef LLTRACE
689#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000690 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700691 if (!lltrace && !_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300693 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300694 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 } \
696 goto fast_next_opcode; \
697 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000698#else
699#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700701 if (!_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300703 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300704 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 } \
706 goto fast_next_opcode; \
707 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000708#endif
709
710#else
Benjamin Petersonddd19492018-09-16 22:38:02 -0700711#define TARGET(op) op
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300712
Antoine Pitroub52ec782009-01-25 16:34:23 +0000713#define DISPATCH() continue
714#define FAST_DISPATCH() goto fast_next_opcode
715#endif
716
717
Neal Norwitza81d2202002-07-14 00:27:26 +0000718/* Tuple access macros */
719
720#ifndef Py_DEBUG
721#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
722#else
723#define GETITEM(v, i) PyTuple_GetItem((v), (i))
724#endif
725
Guido van Rossum374a9221991-04-04 10:40:29 +0000726/* Code access macros */
727
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300728/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600729#define INSTR_OFFSET() \
730 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300731#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300732 _Py_CODEUNIT word = *next_instr; \
733 opcode = _Py_OPCODE(word); \
734 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300735 next_instr++; \
736 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300737#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
738#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000739
Raymond Hettingerf606f872003-03-16 03:11:04 +0000740/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 Some opcodes tend to come in pairs thus making it possible to
742 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300743 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000744
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 Verifying the prediction costs a single high-speed test of a register
746 variable against a constant. If the pairing was good, then the
747 processor's own internal branch predication has a high likelihood of
748 success, resulting in a nearly zero-overhead transition to the
749 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300750 including its unpredictable switch-case branch. Combined with the
751 processor's internal branch prediction, a successful PREDICT has the
752 effect of making the two opcodes run as if they were a single new opcode
753 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000754
Georg Brandl86b2fb92008-07-16 03:43:04 +0000755 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 predictions turned-on and interpret the results as if some opcodes
757 had been combined or turn-off predictions so that the opcode frequency
758 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000759
760 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 the CPU to record separate branch prediction information for each
762 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000763
Raymond Hettingerf606f872003-03-16 03:11:04 +0000764*/
765
Antoine Pitrou042b1282010-08-13 21:15:58 +0000766#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000768#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300769#define PREDICT(op) \
770 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300771 _Py_CODEUNIT word = *next_instr; \
772 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300773 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300774 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300775 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300776 goto PRED_##op; \
777 } \
778 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000779#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300780#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000781
Raymond Hettingerf606f872003-03-16 03:11:04 +0000782
Guido van Rossum374a9221991-04-04 10:40:29 +0000783/* Stack manipulation macros */
784
Martin v. Löwis18e16552006-02-15 17:27:45 +0000785/* The stack can grow at most MAXINT deep, as co_nlocals and
786 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000787#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
788#define EMPTY() (STACK_LEVEL() == 0)
789#define TOP() (stack_pointer[-1])
790#define SECOND() (stack_pointer[-2])
791#define THIRD() (stack_pointer[-3])
792#define FOURTH() (stack_pointer[-4])
793#define PEEK(n) (stack_pointer[-(n)])
794#define SET_TOP(v) (stack_pointer[-1] = (v))
795#define SET_SECOND(v) (stack_pointer[-2] = (v))
796#define SET_THIRD(v) (stack_pointer[-3] = (v))
797#define SET_FOURTH(v) (stack_pointer[-4] = (v))
798#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
799#define BASIC_STACKADJ(n) (stack_pointer += n)
800#define BASIC_PUSH(v) (*stack_pointer++ = (v))
801#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000802
Guido van Rossum96a42c81992-01-12 02:29:51 +0000803#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000805 lltrace && prtrace(TOP(), "push")); \
806 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000808 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +0000809#define STACK_GROW(n) do { \
810 assert(n >= 0); \
811 (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000812 lltrace && prtrace(TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +0000813 assert(STACK_LEVEL() <= co->co_stacksize); \
814 } while (0)
815#define STACK_SHRINK(n) do { \
816 assert(n >= 0); \
817 (void)(lltrace && prtrace(TOP(), "stackadj")); \
818 (void)(BASIC_STACKADJ(-n)); \
819 assert(STACK_LEVEL() <= co->co_stacksize); \
820 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +0000821#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +0000822 prtrace((STACK_POINTER)[-1], "ext_pop")), \
823 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000824#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000825#define PUSH(v) BASIC_PUSH(v)
826#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +0000827#define STACK_GROW(n) BASIC_STACKADJ(n)
828#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000829#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000830#endif
831
Guido van Rossum681d79a1995-07-18 14:51:37 +0000832/* Local variable macros */
833
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000835
836/* The SETLOCAL() macro must not DECREF the local variable in-place and
837 then store the new value; it must copy the old value to a temporary
838 value, then store the new value, and then DECREF the temporary value.
839 This is because it is possible that during the DECREF the frame is
840 accessed by other code (e.g. a __del__ method or gc.collect()) and the
841 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000843 GETLOCAL(i) = value; \
844 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000845
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000846
847#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 while (STACK_LEVEL() > (b)->b_level) { \
849 PyObject *v = POP(); \
850 Py_XDECREF(v); \
851 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000852
853#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300854 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +0100856 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 assert(STACK_LEVEL() >= (b)->b_level + 3); \
858 while (STACK_LEVEL() > (b)->b_level + 3) { \
859 value = POP(); \
860 Py_XDECREF(value); \
861 } \
Mark Shannonae3087c2017-10-22 22:41:51 +0100862 exc_info = tstate->exc_info; \
863 type = exc_info->exc_type; \
864 value = exc_info->exc_value; \
865 traceback = exc_info->exc_traceback; \
866 exc_info->exc_type = POP(); \
867 exc_info->exc_value = POP(); \
868 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 Py_XDECREF(type); \
870 Py_XDECREF(value); \
871 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300872 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000873
Guido van Rossuma027efa1997-05-05 20:56:21 +0000874/* Start of code */
875
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 /* push frame */
877 if (Py_EnterRecursiveCall(""))
878 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000879
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 if (tstate->use_tracing) {
883 if (tstate->c_tracefunc != NULL) {
884 /* tstate->c_tracefunc, if defined, is a
885 function that will be called on *every* entry
886 to a code block. Its return value, if not
887 None, is a function that will be called at
888 the start of each executed line of code.
889 (Actually, the function must return itself
890 in order to continue tracing.) The trace
891 functions are called with three arguments:
892 a pointer to the current frame, a string
893 indicating why the function is called, and
894 an argument which depends on the situation.
895 The global trace function is also called
896 whenever an exception is detected. */
897 if (call_trace_protected(tstate->c_tracefunc,
898 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100899 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 /* Trace function raised an error */
901 goto exit_eval_frame;
902 }
903 }
904 if (tstate->c_profilefunc != NULL) {
905 /* Similar for c_profilefunc, except it needn't
906 return itself and isn't called for "line" events */
907 if (call_trace_protected(tstate->c_profilefunc,
908 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100909 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 /* Profile function raised an error */
911 goto exit_eval_frame;
912 }
913 }
914 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000915
Łukasz Langaa785c872016-09-09 17:37:37 -0700916 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
917 dtrace_function_entry(f);
918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 co = f->f_code;
920 names = co->co_names;
921 consts = co->co_consts;
922 fastlocals = f->f_localsplus;
923 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300924 assert(PyBytes_Check(co->co_code));
925 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +0300926 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
927 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
928 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300929 /*
930 f->f_lasti refers to the index of the last instruction,
931 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000932
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300933 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500934 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000935
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 When the PREDICT() macros are enabled, some opcode pairs follow in
937 direct succession without updating f->f_lasti. A successful
938 prediction effectively links the two codes together as if they
939 were a single new opcode; accordingly,f->f_lasti will point to
940 the first code in the pair (for instance, GET_ITER followed by
941 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300942 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300944 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300945 next_instr = first_instr;
946 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +0300947 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
948 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300949 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 stack_pointer = f->f_stacktop;
951 assert(stack_pointer != NULL);
952 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +0200953 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000954
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000955
Tim Peters5ca576e2001-06-18 22:08:13 +0000956#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200957 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000958#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000959
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400960 if (throwflag) /* support for generator.throw() */
961 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000962
Victor Stinnerace47d72013-07-18 01:41:08 +0200963#ifdef Py_DEBUG
964 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +0100965 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +0000966 caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +0200967 assert(!PyErr_Occurred());
968#endif
969
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200970main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 assert(stack_pointer >= f->f_valuestack); /* else underflow */
973 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +0200974 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000975
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 /* Do periodic things. Doing this every time through
977 the loop would add too much overhead, so we do it
978 only every Nth instruction. We also do it if
979 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
980 event needs attention (e.g. a signal handler or
981 async I/O handler); see Py_AddPendingCall() and
982 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000983
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600984 if (_Py_atomic_load_relaxed(&_PyRuntime.ceval.eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +0300985 opcode = _Py_OPCODE(*next_instr);
986 if (opcode == SETUP_FINALLY ||
987 opcode == SETUP_WITH ||
988 opcode == BEFORE_ASYNC_WITH ||
989 opcode == YIELD_FROM) {
990 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -0700991 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +0300992 - If we're about to enter the 'with:'. It will prevent
993 emitting a resource warning in the common idiom
994 'with open(path) as file:'.
995 - If we're about to enter the 'async with:'.
996 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -0700997 *very* useful, but might help in some cases and it's
998 traditional)
999 - If we're resuming a chain of nested 'yield from' or
1000 'await' calls, then each frame is parked with YIELD_FROM
1001 as its next opcode. If the user hit control-C we want to
1002 wait until we've reached the innermost frame before
1003 running the signal handler and raising KeyboardInterrupt
1004 (see bpo-30039).
1005 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 goto fast_next_opcode;
1007 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001008
1009 if (_Py_atomic_load_relaxed(
1010 &_PyRuntime.ceval.signals_pending))
1011 {
1012 if (handle_signals() != 0) {
1013 goto error;
1014 }
1015 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001016 if (_Py_atomic_load_relaxed(
1017 &_PyRuntime.ceval.pending.calls_to_do))
1018 {
Eric Snowfdf282d2019-01-11 14:26:55 -07001019 if (make_pending_calls() != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001020 goto error;
Eric Snowfdf282d2019-01-11 14:26:55 -07001021 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001023
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001024 if (_Py_atomic_load_relaxed(
1025 &_PyRuntime.ceval.gil_drop_request))
1026 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 /* Give another thread a chance */
1028 if (PyThreadState_Swap(NULL) != tstate)
1029 Py_FatalError("ceval: tstate mix-up");
1030 drop_gil(tstate);
1031
1032 /* Other threads may run now */
1033
1034 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001035
1036 /* Check if we should make a quick exit. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001037 if (_Py_IsFinalizing() &&
1038 !_Py_CURRENTLY_FINALIZING(tstate))
1039 {
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001040 drop_gil(tstate);
1041 PyThread_exit_thread();
1042 }
1043
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 if (PyThreadState_Swap(tstate) != NULL)
1045 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001046 }
1047 /* Check for asynchronous exceptions. */
1048 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001049 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 tstate->async_exc = NULL;
1051 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001052 PyErr_SetNone(exc);
1053 Py_DECREF(exc);
1054 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 }
1056 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 fast_next_opcode:
1059 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001060
Łukasz Langaa785c872016-09-09 17:37:37 -07001061 if (PyDTrace_LINE_ENABLED())
1062 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1063
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001067 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001068 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 /* see maybe_call_line_trace
1070 for expository comments */
1071 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001072
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 err = maybe_call_line_trace(tstate->c_tracefunc,
1074 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001075 tstate, f,
1076 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 /* Reload possibly changed frame fields */
1078 JUMPTO(f->f_lasti);
1079 if (f->f_stacktop != NULL) {
1080 stack_pointer = f->f_stacktop;
1081 f->f_stacktop = NULL;
1082 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001083 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001085 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001086 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001087
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001089
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001090 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001091 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001092#ifdef DYNAMIC_EXECUTION_PROFILE
1093#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 dxpairs[lastopcode][opcode]++;
1095 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001096#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001098#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001099
Guido van Rossum96a42c81992-01-12 02:29:51 +00001100#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001102
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 if (lltrace) {
1104 if (HAS_ARG(opcode)) {
1105 printf("%d: %d, %d\n",
1106 f->f_lasti, opcode, oparg);
1107 }
1108 else {
1109 printf("%d: %d\n",
1110 f->f_lasti, opcode);
1111 }
1112 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001113#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001116
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001118 It is essential that any operation that fails must goto error
1119 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001120
Benjamin Petersonddd19492018-09-16 22:38:02 -07001121 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001123 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001124
Benjamin Petersonddd19492018-09-16 22:38:02 -07001125 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001126 PyObject *value = GETLOCAL(oparg);
1127 if (value == NULL) {
1128 format_exc_check_arg(PyExc_UnboundLocalError,
1129 UNBOUNDLOCAL_ERROR_MSG,
1130 PyTuple_GetItem(co->co_varnames, oparg));
1131 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001133 Py_INCREF(value);
1134 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001136 }
1137
Benjamin Petersonddd19492018-09-16 22:38:02 -07001138 case TARGET(LOAD_CONST): {
1139 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001140 PyObject *value = GETITEM(consts, oparg);
1141 Py_INCREF(value);
1142 PUSH(value);
1143 FAST_DISPATCH();
1144 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001145
Benjamin Petersonddd19492018-09-16 22:38:02 -07001146 case TARGET(STORE_FAST): {
1147 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001148 PyObject *value = POP();
1149 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001151 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001152
Benjamin Petersonddd19492018-09-16 22:38:02 -07001153 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001154 PyObject *value = POP();
1155 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001157 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001158
Benjamin Petersonddd19492018-09-16 22:38:02 -07001159 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001160 PyObject *top = TOP();
1161 PyObject *second = SECOND();
1162 SET_TOP(second);
1163 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001165 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001166
Benjamin Petersonddd19492018-09-16 22:38:02 -07001167 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001168 PyObject *top = TOP();
1169 PyObject *second = SECOND();
1170 PyObject *third = THIRD();
1171 SET_TOP(second);
1172 SET_SECOND(third);
1173 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001174 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001175 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001176
Benjamin Petersonddd19492018-09-16 22:38:02 -07001177 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001178 PyObject *top = TOP();
1179 PyObject *second = SECOND();
1180 PyObject *third = THIRD();
1181 PyObject *fourth = FOURTH();
1182 SET_TOP(second);
1183 SET_SECOND(third);
1184 SET_THIRD(fourth);
1185 SET_FOURTH(top);
1186 FAST_DISPATCH();
1187 }
1188
Benjamin Petersonddd19492018-09-16 22:38:02 -07001189 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001190 PyObject *top = TOP();
1191 Py_INCREF(top);
1192 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001194 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001195
Benjamin Petersonddd19492018-09-16 22:38:02 -07001196 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001197 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001198 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001199 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001200 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001201 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001202 SET_TOP(top);
1203 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001204 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001205 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001206
Benjamin Petersonddd19492018-09-16 22:38:02 -07001207 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001208 PyObject *value = TOP();
1209 PyObject *res = PyNumber_Positive(value);
1210 Py_DECREF(value);
1211 SET_TOP(res);
1212 if (res == NULL)
1213 goto error;
1214 DISPATCH();
1215 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001216
Benjamin Petersonddd19492018-09-16 22:38:02 -07001217 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001218 PyObject *value = TOP();
1219 PyObject *res = PyNumber_Negative(value);
1220 Py_DECREF(value);
1221 SET_TOP(res);
1222 if (res == NULL)
1223 goto error;
1224 DISPATCH();
1225 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001226
Benjamin Petersonddd19492018-09-16 22:38:02 -07001227 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001228 PyObject *value = TOP();
1229 int err = PyObject_IsTrue(value);
1230 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001231 if (err == 0) {
1232 Py_INCREF(Py_True);
1233 SET_TOP(Py_True);
1234 DISPATCH();
1235 }
1236 else if (err > 0) {
1237 Py_INCREF(Py_False);
1238 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 DISPATCH();
1240 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001241 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001242 goto error;
1243 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001244
Benjamin Petersonddd19492018-09-16 22:38:02 -07001245 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001246 PyObject *value = TOP();
1247 PyObject *res = PyNumber_Invert(value);
1248 Py_DECREF(value);
1249 SET_TOP(res);
1250 if (res == NULL)
1251 goto error;
1252 DISPATCH();
1253 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001254
Benjamin Petersonddd19492018-09-16 22:38:02 -07001255 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001256 PyObject *exp = POP();
1257 PyObject *base = TOP();
1258 PyObject *res = PyNumber_Power(base, exp, Py_None);
1259 Py_DECREF(base);
1260 Py_DECREF(exp);
1261 SET_TOP(res);
1262 if (res == NULL)
1263 goto error;
1264 DISPATCH();
1265 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001266
Benjamin Petersonddd19492018-09-16 22:38:02 -07001267 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001268 PyObject *right = POP();
1269 PyObject *left = TOP();
1270 PyObject *res = PyNumber_Multiply(left, right);
1271 Py_DECREF(left);
1272 Py_DECREF(right);
1273 SET_TOP(res);
1274 if (res == NULL)
1275 goto error;
1276 DISPATCH();
1277 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001278
Benjamin Petersonddd19492018-09-16 22:38:02 -07001279 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001280 PyObject *right = POP();
1281 PyObject *left = TOP();
1282 PyObject *res = PyNumber_MatrixMultiply(left, right);
1283 Py_DECREF(left);
1284 Py_DECREF(right);
1285 SET_TOP(res);
1286 if (res == NULL)
1287 goto error;
1288 DISPATCH();
1289 }
1290
Benjamin Petersonddd19492018-09-16 22:38:02 -07001291 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001292 PyObject *divisor = POP();
1293 PyObject *dividend = TOP();
1294 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1295 Py_DECREF(dividend);
1296 Py_DECREF(divisor);
1297 SET_TOP(quotient);
1298 if (quotient == NULL)
1299 goto error;
1300 DISPATCH();
1301 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001302
Benjamin Petersonddd19492018-09-16 22:38:02 -07001303 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001304 PyObject *divisor = POP();
1305 PyObject *dividend = TOP();
1306 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1307 Py_DECREF(dividend);
1308 Py_DECREF(divisor);
1309 SET_TOP(quotient);
1310 if (quotient == NULL)
1311 goto error;
1312 DISPATCH();
1313 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001314
Benjamin Petersonddd19492018-09-16 22:38:02 -07001315 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001316 PyObject *divisor = POP();
1317 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001318 PyObject *res;
1319 if (PyUnicode_CheckExact(dividend) && (
1320 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1321 // fast path; string formatting, but not if the RHS is a str subclass
1322 // (see issue28598)
1323 res = PyUnicode_Format(dividend, divisor);
1324 } else {
1325 res = PyNumber_Remainder(dividend, divisor);
1326 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001327 Py_DECREF(divisor);
1328 Py_DECREF(dividend);
1329 SET_TOP(res);
1330 if (res == NULL)
1331 goto error;
1332 DISPATCH();
1333 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001334
Benjamin Petersonddd19492018-09-16 22:38:02 -07001335 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001336 PyObject *right = POP();
1337 PyObject *left = TOP();
1338 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001339 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1340 CPython using bytecode, it is simply worthless.
1341 See http://bugs.python.org/issue21955 and
1342 http://bugs.python.org/issue10044 for the discussion. In short,
1343 no patch shown any impact on a realistic benchmark, only a minor
1344 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001345 if (PyUnicode_CheckExact(left) &&
1346 PyUnicode_CheckExact(right)) {
1347 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001348 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001349 }
1350 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001351 sum = PyNumber_Add(left, right);
1352 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001353 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001354 Py_DECREF(right);
1355 SET_TOP(sum);
1356 if (sum == NULL)
1357 goto error;
1358 DISPATCH();
1359 }
1360
Benjamin Petersonddd19492018-09-16 22:38:02 -07001361 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001362 PyObject *right = POP();
1363 PyObject *left = TOP();
1364 PyObject *diff = PyNumber_Subtract(left, right);
1365 Py_DECREF(right);
1366 Py_DECREF(left);
1367 SET_TOP(diff);
1368 if (diff == NULL)
1369 goto error;
1370 DISPATCH();
1371 }
1372
Benjamin Petersonddd19492018-09-16 22:38:02 -07001373 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001374 PyObject *sub = POP();
1375 PyObject *container = TOP();
1376 PyObject *res = PyObject_GetItem(container, sub);
1377 Py_DECREF(container);
1378 Py_DECREF(sub);
1379 SET_TOP(res);
1380 if (res == NULL)
1381 goto error;
1382 DISPATCH();
1383 }
1384
Benjamin Petersonddd19492018-09-16 22:38:02 -07001385 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001386 PyObject *right = POP();
1387 PyObject *left = TOP();
1388 PyObject *res = PyNumber_Lshift(left, right);
1389 Py_DECREF(left);
1390 Py_DECREF(right);
1391 SET_TOP(res);
1392 if (res == NULL)
1393 goto error;
1394 DISPATCH();
1395 }
1396
Benjamin Petersonddd19492018-09-16 22:38:02 -07001397 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001398 PyObject *right = POP();
1399 PyObject *left = TOP();
1400 PyObject *res = PyNumber_Rshift(left, right);
1401 Py_DECREF(left);
1402 Py_DECREF(right);
1403 SET_TOP(res);
1404 if (res == NULL)
1405 goto error;
1406 DISPATCH();
1407 }
1408
Benjamin Petersonddd19492018-09-16 22:38:02 -07001409 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001410 PyObject *right = POP();
1411 PyObject *left = TOP();
1412 PyObject *res = PyNumber_And(left, right);
1413 Py_DECREF(left);
1414 Py_DECREF(right);
1415 SET_TOP(res);
1416 if (res == NULL)
1417 goto error;
1418 DISPATCH();
1419 }
1420
Benjamin Petersonddd19492018-09-16 22:38:02 -07001421 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001422 PyObject *right = POP();
1423 PyObject *left = TOP();
1424 PyObject *res = PyNumber_Xor(left, right);
1425 Py_DECREF(left);
1426 Py_DECREF(right);
1427 SET_TOP(res);
1428 if (res == NULL)
1429 goto error;
1430 DISPATCH();
1431 }
1432
Benjamin Petersonddd19492018-09-16 22:38:02 -07001433 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001434 PyObject *right = POP();
1435 PyObject *left = TOP();
1436 PyObject *res = PyNumber_Or(left, right);
1437 Py_DECREF(left);
1438 Py_DECREF(right);
1439 SET_TOP(res);
1440 if (res == NULL)
1441 goto error;
1442 DISPATCH();
1443 }
1444
Benjamin Petersonddd19492018-09-16 22:38:02 -07001445 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001446 PyObject *v = POP();
1447 PyObject *list = PEEK(oparg);
1448 int err;
1449 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001450 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001451 if (err != 0)
1452 goto error;
1453 PREDICT(JUMP_ABSOLUTE);
1454 DISPATCH();
1455 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001456
Benjamin Petersonddd19492018-09-16 22:38:02 -07001457 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001458 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001459 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001460 int err;
1461 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001462 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001463 if (err != 0)
1464 goto error;
1465 PREDICT(JUMP_ABSOLUTE);
1466 DISPATCH();
1467 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001468
Benjamin Petersonddd19492018-09-16 22:38:02 -07001469 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001470 PyObject *exp = POP();
1471 PyObject *base = TOP();
1472 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1473 Py_DECREF(base);
1474 Py_DECREF(exp);
1475 SET_TOP(res);
1476 if (res == NULL)
1477 goto error;
1478 DISPATCH();
1479 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001480
Benjamin Petersonddd19492018-09-16 22:38:02 -07001481 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001482 PyObject *right = POP();
1483 PyObject *left = TOP();
1484 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1485 Py_DECREF(left);
1486 Py_DECREF(right);
1487 SET_TOP(res);
1488 if (res == NULL)
1489 goto error;
1490 DISPATCH();
1491 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001492
Benjamin Petersonddd19492018-09-16 22:38:02 -07001493 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001494 PyObject *right = POP();
1495 PyObject *left = TOP();
1496 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1497 Py_DECREF(left);
1498 Py_DECREF(right);
1499 SET_TOP(res);
1500 if (res == NULL)
1501 goto error;
1502 DISPATCH();
1503 }
1504
Benjamin Petersonddd19492018-09-16 22:38:02 -07001505 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001506 PyObject *divisor = POP();
1507 PyObject *dividend = TOP();
1508 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1509 Py_DECREF(dividend);
1510 Py_DECREF(divisor);
1511 SET_TOP(quotient);
1512 if (quotient == NULL)
1513 goto error;
1514 DISPATCH();
1515 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001516
Benjamin Petersonddd19492018-09-16 22:38:02 -07001517 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001518 PyObject *divisor = POP();
1519 PyObject *dividend = TOP();
1520 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1521 Py_DECREF(dividend);
1522 Py_DECREF(divisor);
1523 SET_TOP(quotient);
1524 if (quotient == NULL)
1525 goto error;
1526 DISPATCH();
1527 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001528
Benjamin Petersonddd19492018-09-16 22:38:02 -07001529 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001530 PyObject *right = POP();
1531 PyObject *left = TOP();
1532 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1533 Py_DECREF(left);
1534 Py_DECREF(right);
1535 SET_TOP(mod);
1536 if (mod == NULL)
1537 goto error;
1538 DISPATCH();
1539 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001540
Benjamin Petersonddd19492018-09-16 22:38:02 -07001541 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001542 PyObject *right = POP();
1543 PyObject *left = TOP();
1544 PyObject *sum;
1545 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1546 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001547 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001548 }
1549 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001550 sum = PyNumber_InPlaceAdd(left, right);
1551 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001552 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001553 Py_DECREF(right);
1554 SET_TOP(sum);
1555 if (sum == NULL)
1556 goto error;
1557 DISPATCH();
1558 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001559
Benjamin Petersonddd19492018-09-16 22:38:02 -07001560 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001561 PyObject *right = POP();
1562 PyObject *left = TOP();
1563 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1564 Py_DECREF(left);
1565 Py_DECREF(right);
1566 SET_TOP(diff);
1567 if (diff == NULL)
1568 goto error;
1569 DISPATCH();
1570 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001571
Benjamin Petersonddd19492018-09-16 22:38:02 -07001572 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001573 PyObject *right = POP();
1574 PyObject *left = TOP();
1575 PyObject *res = PyNumber_InPlaceLshift(left, right);
1576 Py_DECREF(left);
1577 Py_DECREF(right);
1578 SET_TOP(res);
1579 if (res == NULL)
1580 goto error;
1581 DISPATCH();
1582 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001583
Benjamin Petersonddd19492018-09-16 22:38:02 -07001584 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001585 PyObject *right = POP();
1586 PyObject *left = TOP();
1587 PyObject *res = PyNumber_InPlaceRshift(left, right);
1588 Py_DECREF(left);
1589 Py_DECREF(right);
1590 SET_TOP(res);
1591 if (res == NULL)
1592 goto error;
1593 DISPATCH();
1594 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001595
Benjamin Petersonddd19492018-09-16 22:38:02 -07001596 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001597 PyObject *right = POP();
1598 PyObject *left = TOP();
1599 PyObject *res = PyNumber_InPlaceAnd(left, right);
1600 Py_DECREF(left);
1601 Py_DECREF(right);
1602 SET_TOP(res);
1603 if (res == NULL)
1604 goto error;
1605 DISPATCH();
1606 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001607
Benjamin Petersonddd19492018-09-16 22:38:02 -07001608 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001609 PyObject *right = POP();
1610 PyObject *left = TOP();
1611 PyObject *res = PyNumber_InPlaceXor(left, right);
1612 Py_DECREF(left);
1613 Py_DECREF(right);
1614 SET_TOP(res);
1615 if (res == NULL)
1616 goto error;
1617 DISPATCH();
1618 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001619
Benjamin Petersonddd19492018-09-16 22:38:02 -07001620 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001621 PyObject *right = POP();
1622 PyObject *left = TOP();
1623 PyObject *res = PyNumber_InPlaceOr(left, right);
1624 Py_DECREF(left);
1625 Py_DECREF(right);
1626 SET_TOP(res);
1627 if (res == NULL)
1628 goto error;
1629 DISPATCH();
1630 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001631
Benjamin Petersonddd19492018-09-16 22:38:02 -07001632 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001633 PyObject *sub = TOP();
1634 PyObject *container = SECOND();
1635 PyObject *v = THIRD();
1636 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001637 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001638 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001639 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001641 Py_DECREF(container);
1642 Py_DECREF(sub);
1643 if (err != 0)
1644 goto error;
1645 DISPATCH();
1646 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001647
Benjamin Petersonddd19492018-09-16 22:38:02 -07001648 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001649 PyObject *sub = TOP();
1650 PyObject *container = SECOND();
1651 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001652 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00001653 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001654 err = PyObject_DelItem(container, sub);
1655 Py_DECREF(container);
1656 Py_DECREF(sub);
1657 if (err != 0)
1658 goto error;
1659 DISPATCH();
1660 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001661
Benjamin Petersonddd19492018-09-16 22:38:02 -07001662 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01001663 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001664 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001665 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001666 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001667 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001668 PyErr_SetString(PyExc_RuntimeError,
1669 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001670 Py_DECREF(value);
1671 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001672 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001673 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001674 Py_DECREF(value);
1675 if (res == NULL)
1676 goto error;
1677 Py_DECREF(res);
1678 DISPATCH();
1679 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001680
Benjamin Petersonddd19492018-09-16 22:38:02 -07001681 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001682 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683 switch (oparg) {
1684 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001685 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001686 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001687 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001688 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001689 /* fall through */
1690 case 0:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001691 if (do_raise(exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001692 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001693 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001694 break;
1695 default:
1696 PyErr_SetString(PyExc_SystemError,
1697 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001698 break;
1699 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001700 goto error;
1701 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001702
Benjamin Petersonddd19492018-09-16 22:38:02 -07001703 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001704 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001705 assert(f->f_iblock == 0);
1706 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001707 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001708
Benjamin Petersonddd19492018-09-16 22:38:02 -07001709 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001710 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001711 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001712 PyObject *obj = TOP();
1713 PyTypeObject *type = Py_TYPE(obj);
1714
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001715 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001716 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001717 }
Yury Selivanov75445082015-05-11 22:57:16 -04001718
1719 if (getter != NULL) {
1720 iter = (*getter)(obj);
1721 Py_DECREF(obj);
1722 if (iter == NULL) {
1723 SET_TOP(NULL);
1724 goto error;
1725 }
1726 }
1727 else {
1728 SET_TOP(NULL);
1729 PyErr_Format(
1730 PyExc_TypeError,
1731 "'async for' requires an object with "
1732 "__aiter__ method, got %.100s",
1733 type->tp_name);
1734 Py_DECREF(obj);
1735 goto error;
1736 }
1737
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001738 if (Py_TYPE(iter)->tp_as_async == NULL ||
1739 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001740
Yury Selivanov398ff912017-03-02 22:20:00 -05001741 SET_TOP(NULL);
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001742 PyErr_Format(
1743 PyExc_TypeError,
1744 "'async for' received an object from __aiter__ "
1745 "that does not implement __anext__: %.100s",
1746 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001747 Py_DECREF(iter);
1748 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001749 }
1750
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001751 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001752 DISPATCH();
1753 }
1754
Benjamin Petersonddd19492018-09-16 22:38:02 -07001755 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001756 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001757 PyObject *next_iter = NULL;
1758 PyObject *awaitable = NULL;
1759 PyObject *aiter = TOP();
1760 PyTypeObject *type = Py_TYPE(aiter);
1761
Yury Selivanoveb636452016-09-08 22:01:51 -07001762 if (PyAsyncGen_CheckExact(aiter)) {
1763 awaitable = type->tp_as_async->am_anext(aiter);
1764 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001765 goto error;
1766 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001767 } else {
1768 if (type->tp_as_async != NULL){
1769 getter = type->tp_as_async->am_anext;
1770 }
Yury Selivanov75445082015-05-11 22:57:16 -04001771
Yury Selivanoveb636452016-09-08 22:01:51 -07001772 if (getter != NULL) {
1773 next_iter = (*getter)(aiter);
1774 if (next_iter == NULL) {
1775 goto error;
1776 }
1777 }
1778 else {
1779 PyErr_Format(
1780 PyExc_TypeError,
1781 "'async for' requires an iterator with "
1782 "__anext__ method, got %.100s",
1783 type->tp_name);
1784 goto error;
1785 }
Yury Selivanov75445082015-05-11 22:57:16 -04001786
Yury Selivanoveb636452016-09-08 22:01:51 -07001787 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1788 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001789 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001790 PyExc_TypeError,
1791 "'async for' received an invalid object "
1792 "from __anext__: %.100s",
1793 Py_TYPE(next_iter)->tp_name);
1794
1795 Py_DECREF(next_iter);
1796 goto error;
1797 } else {
1798 Py_DECREF(next_iter);
1799 }
1800 }
Yury Selivanov75445082015-05-11 22:57:16 -04001801
1802 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001803 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001804 DISPATCH();
1805 }
1806
Benjamin Petersonddd19492018-09-16 22:38:02 -07001807 case TARGET(GET_AWAITABLE): {
1808 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04001809 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04001810 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04001811
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03001812 if (iter == NULL) {
1813 format_awaitable_error(Py_TYPE(iterable),
1814 _Py_OPCODE(next_instr[-2]));
1815 }
1816
Yury Selivanov75445082015-05-11 22:57:16 -04001817 Py_DECREF(iterable);
1818
Yury Selivanovc724bae2016-03-02 11:30:46 -05001819 if (iter != NULL && PyCoro_CheckExact(iter)) {
1820 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
1821 if (yf != NULL) {
1822 /* `iter` is a coroutine object that is being
1823 awaited, `yf` is a pointer to the current awaitable
1824 being awaited on. */
1825 Py_DECREF(yf);
1826 Py_CLEAR(iter);
1827 PyErr_SetString(
1828 PyExc_RuntimeError,
1829 "coroutine is being awaited already");
1830 /* The code below jumps to `error` if `iter` is NULL. */
1831 }
1832 }
1833
Yury Selivanov75445082015-05-11 22:57:16 -04001834 SET_TOP(iter); /* Even if it's NULL */
1835
1836 if (iter == NULL) {
1837 goto error;
1838 }
1839
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001840 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001841 DISPATCH();
1842 }
1843
Benjamin Petersonddd19492018-09-16 22:38:02 -07001844 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001845 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001846 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001847 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001848 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
1849 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001850 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001851 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001852 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001853 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001854 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001855 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001856 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001857 Py_DECREF(v);
1858 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001859 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08001860 if (tstate->c_tracefunc != NULL
1861 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001862 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10001863 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001864 if (err < 0)
1865 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001866 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001867 SET_TOP(val);
1868 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001869 }
Martin Panter95f53c12016-07-18 08:23:26 +00001870 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001871 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001872 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01001873 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03001874 f->f_lasti -= sizeof(_Py_CODEUNIT);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001875 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001876 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001877
Benjamin Petersonddd19492018-09-16 22:38:02 -07001878 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001879 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07001880
1881 if (co->co_flags & CO_ASYNC_GENERATOR) {
1882 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
1883 Py_DECREF(retval);
1884 if (w == NULL) {
1885 retval = NULL;
1886 goto error;
1887 }
1888 retval = w;
1889 }
1890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001891 f->f_stacktop = stack_pointer;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001892 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001893 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001894
Benjamin Petersonddd19492018-09-16 22:38:02 -07001895 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001896 PyObject *type, *value, *traceback;
1897 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001898 PyTryBlock *b = PyFrame_BlockPop(f);
1899 if (b->b_type != EXCEPT_HANDLER) {
1900 PyErr_SetString(PyExc_SystemError,
1901 "popped block is not an except handler");
1902 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001904 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
1905 STACK_LEVEL() <= (b)->b_level + 4);
1906 exc_info = tstate->exc_info;
1907 type = exc_info->exc_type;
1908 value = exc_info->exc_value;
1909 traceback = exc_info->exc_traceback;
1910 exc_info->exc_type = POP();
1911 exc_info->exc_value = POP();
1912 exc_info->exc_traceback = POP();
1913 Py_XDECREF(type);
1914 Py_XDECREF(value);
1915 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001916 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001917 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001918
Benjamin Petersonddd19492018-09-16 22:38:02 -07001919 case TARGET(POP_BLOCK): {
1920 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001921 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001922 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001923 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001924
Benjamin Petersonddd19492018-09-16 22:38:02 -07001925 case TARGET(POP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001926 /* If oparg is 0 at the top of the stack are 1 or 6 values:
1927 Either:
1928 - TOP = NULL or an integer
1929 or:
1930 - (TOP, SECOND, THIRD) = exc_info()
1931 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
1932
1933 If oparg is 1 the value for 'return' was additionally pushed
1934 at the top of the stack.
1935 */
1936 PyObject *res = NULL;
1937 if (oparg) {
1938 res = POP();
1939 }
1940 PyObject *exc = POP();
1941 if (exc == NULL || PyLong_CheckExact(exc)) {
1942 Py_XDECREF(exc);
1943 }
1944 else {
1945 Py_DECREF(exc);
1946 Py_DECREF(POP());
1947 Py_DECREF(POP());
1948
1949 PyObject *type, *value, *traceback;
1950 _PyErr_StackItem *exc_info;
1951 PyTryBlock *b = PyFrame_BlockPop(f);
1952 if (b->b_type != EXCEPT_HANDLER) {
1953 PyErr_SetString(PyExc_SystemError,
1954 "popped block is not an except handler");
1955 Py_XDECREF(res);
1956 goto error;
1957 }
1958 assert(STACK_LEVEL() == (b)->b_level + 3);
1959 exc_info = tstate->exc_info;
1960 type = exc_info->exc_type;
1961 value = exc_info->exc_value;
1962 traceback = exc_info->exc_traceback;
1963 exc_info->exc_type = POP();
1964 exc_info->exc_value = POP();
1965 exc_info->exc_traceback = POP();
1966 Py_XDECREF(type);
1967 Py_XDECREF(value);
1968 Py_XDECREF(traceback);
1969 }
1970 if (oparg) {
1971 PUSH(res);
1972 }
1973 DISPATCH();
1974 }
1975
Benjamin Petersonddd19492018-09-16 22:38:02 -07001976 case TARGET(CALL_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001977 PyObject *ret = PyLong_FromLong(INSTR_OFFSET());
1978 if (ret == NULL) {
1979 goto error;
1980 }
1981 PUSH(ret);
1982 JUMPBY(oparg);
1983 FAST_DISPATCH();
1984 }
1985
Benjamin Petersonddd19492018-09-16 22:38:02 -07001986 case TARGET(BEGIN_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001987 /* Push NULL onto the stack for using it in END_FINALLY,
1988 POP_FINALLY, WITH_CLEANUP_START and WITH_CLEANUP_FINISH.
1989 */
1990 PUSH(NULL);
1991 FAST_DISPATCH();
1992 }
1993
Benjamin Petersonddd19492018-09-16 22:38:02 -07001994 case TARGET(END_FINALLY): {
1995 PREDICTED(END_FINALLY);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001996 /* At the top of the stack are 1 or 6 values:
1997 Either:
1998 - TOP = NULL or an integer
1999 or:
2000 - (TOP, SECOND, THIRD) = exc_info()
2001 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2002 */
2003 PyObject *exc = POP();
2004 if (exc == NULL) {
2005 FAST_DISPATCH();
2006 }
2007 else if (PyLong_CheckExact(exc)) {
2008 int ret = _PyLong_AsInt(exc);
2009 Py_DECREF(exc);
2010 if (ret == -1 && PyErr_Occurred()) {
2011 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002012 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002013 JUMPTO(ret);
2014 FAST_DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002015 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002016 else {
2017 assert(PyExceptionClass_Check(exc));
2018 PyObject *val = POP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002019 PyObject *tb = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002020 PyErr_Restore(exc, val, tb);
2021 goto exception_unwind;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002022 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002023 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002024
Benjamin Petersonddd19492018-09-16 22:38:02 -07002025 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002026 PyObject *exc = POP();
2027 assert(PyExceptionClass_Check(exc));
2028 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2029 PyTryBlock *b = PyFrame_BlockPop(f);
2030 assert(b->b_type == EXCEPT_HANDLER);
2031 Py_DECREF(exc);
2032 UNWIND_EXCEPT_HANDLER(b);
2033 Py_DECREF(POP());
2034 JUMPBY(oparg);
2035 FAST_DISPATCH();
2036 }
2037 else {
2038 PyObject *val = POP();
2039 PyObject *tb = POP();
2040 PyErr_Restore(exc, val, tb);
2041 goto exception_unwind;
2042 }
2043 }
2044
Benjamin Petersonddd19492018-09-16 22:38:02 -07002045 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002046 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002047
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002048 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002049 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002050 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
2051 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002052 PyErr_SetString(PyExc_NameError,
2053 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002054 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002055 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002056 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002057 }
2058 else {
2059 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2060 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002061 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002062 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2063 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002064 if (PyErr_ExceptionMatches(PyExc_KeyError))
2065 PyErr_SetString(PyExc_NameError,
2066 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002067 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002068 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002069 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002070 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002071 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002072 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002073
Benjamin Petersonddd19492018-09-16 22:38:02 -07002074 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002075 PyObject *name = GETITEM(names, oparg);
2076 PyObject *v = POP();
2077 PyObject *ns = f->f_locals;
2078 int err;
2079 if (ns == NULL) {
2080 PyErr_Format(PyExc_SystemError,
2081 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002082 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002083 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002084 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002085 if (PyDict_CheckExact(ns))
2086 err = PyDict_SetItem(ns, name, v);
2087 else
2088 err = PyObject_SetItem(ns, name, v);
2089 Py_DECREF(v);
2090 if (err != 0)
2091 goto error;
2092 DISPATCH();
2093 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002094
Benjamin Petersonddd19492018-09-16 22:38:02 -07002095 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002096 PyObject *name = GETITEM(names, oparg);
2097 PyObject *ns = f->f_locals;
2098 int err;
2099 if (ns == NULL) {
2100 PyErr_Format(PyExc_SystemError,
2101 "no locals when deleting %R", name);
2102 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002103 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002104 err = PyObject_DelItem(ns, name);
2105 if (err != 0) {
2106 format_exc_check_arg(PyExc_NameError,
2107 NAME_ERROR_MSG,
2108 name);
2109 goto error;
2110 }
2111 DISPATCH();
2112 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002113
Benjamin Petersonddd19492018-09-16 22:38:02 -07002114 case TARGET(UNPACK_SEQUENCE): {
2115 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002116 PyObject *seq = POP(), *item, **items;
2117 if (PyTuple_CheckExact(seq) &&
2118 PyTuple_GET_SIZE(seq) == oparg) {
2119 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002120 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002121 item = items[oparg];
2122 Py_INCREF(item);
2123 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002124 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002125 } else if (PyList_CheckExact(seq) &&
2126 PyList_GET_SIZE(seq) == oparg) {
2127 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002128 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002129 item = items[oparg];
2130 Py_INCREF(item);
2131 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002132 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002133 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002134 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002135 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002136 } else {
2137 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002138 Py_DECREF(seq);
2139 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002140 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002141 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002142 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002143 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002144
Benjamin Petersonddd19492018-09-16 22:38:02 -07002145 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002146 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2147 PyObject *seq = POP();
2148
2149 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2150 stack_pointer + totalargs)) {
2151 stack_pointer += totalargs;
2152 } else {
2153 Py_DECREF(seq);
2154 goto error;
2155 }
2156 Py_DECREF(seq);
2157 DISPATCH();
2158 }
2159
Benjamin Petersonddd19492018-09-16 22:38:02 -07002160 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002161 PyObject *name = GETITEM(names, oparg);
2162 PyObject *owner = TOP();
2163 PyObject *v = SECOND();
2164 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002165 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002166 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002167 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002168 Py_DECREF(owner);
2169 if (err != 0)
2170 goto error;
2171 DISPATCH();
2172 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002173
Benjamin Petersonddd19492018-09-16 22:38:02 -07002174 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002175 PyObject *name = GETITEM(names, oparg);
2176 PyObject *owner = POP();
2177 int err;
2178 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2179 Py_DECREF(owner);
2180 if (err != 0)
2181 goto error;
2182 DISPATCH();
2183 }
2184
Benjamin Petersonddd19492018-09-16 22:38:02 -07002185 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002186 PyObject *name = GETITEM(names, oparg);
2187 PyObject *v = POP();
2188 int err;
2189 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002190 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002191 if (err != 0)
2192 goto error;
2193 DISPATCH();
2194 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002195
Benjamin Petersonddd19492018-09-16 22:38:02 -07002196 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002197 PyObject *name = GETITEM(names, oparg);
2198 int err;
2199 err = PyDict_DelItem(f->f_globals, name);
2200 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002201 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002202 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002203 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002204 }
2205 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002206 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002207
Benjamin Petersonddd19492018-09-16 22:38:02 -07002208 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002209 PyObject *name = GETITEM(names, oparg);
2210 PyObject *locals = f->f_locals;
2211 PyObject *v;
2212 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002213 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002214 "no locals when loading %R", name);
2215 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002216 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002217 if (PyDict_CheckExact(locals)) {
2218 v = PyDict_GetItem(locals, name);
2219 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002220 }
2221 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002222 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002223 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002224 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2225 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002226 PyErr_Clear();
2227 }
2228 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002229 if (v == NULL) {
2230 v = PyDict_GetItem(f->f_globals, name);
2231 Py_XINCREF(v);
2232 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002233 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002234 v = PyDict_GetItem(f->f_builtins, name);
2235 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002236 format_exc_check_arg(
2237 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002238 NAME_ERROR_MSG, name);
2239 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002240 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002241 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002242 }
2243 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002244 v = PyObject_GetItem(f->f_builtins, name);
2245 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002246 if (PyErr_ExceptionMatches(PyExc_KeyError))
2247 format_exc_check_arg(
2248 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002249 NAME_ERROR_MSG, name);
2250 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002251 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002252 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002253 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002254 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002255 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002256 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002257 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002258
Benjamin Petersonddd19492018-09-16 22:38:02 -07002259 case TARGET(LOAD_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002260 PyObject *name = GETITEM(names, oparg);
2261 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002262 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002263 && PyDict_CheckExact(f->f_builtins))
2264 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002265 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002266 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002267 name);
2268 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002269 if (!_PyErr_OCCURRED()) {
2270 /* _PyDict_LoadGlobal() returns NULL without raising
2271 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002272 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002273 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002274 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002275 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002276 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002277 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002279 else {
2280 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002281
2282 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002283 v = PyObject_GetItem(f->f_globals, name);
2284 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002285 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2286 goto error;
2287 PyErr_Clear();
2288
Victor Stinnerb4efc962015-11-20 09:24:02 +01002289 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002290 v = PyObject_GetItem(f->f_builtins, name);
2291 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002292 if (PyErr_ExceptionMatches(PyExc_KeyError))
2293 format_exc_check_arg(
2294 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002295 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002296 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002297 }
2298 }
2299 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002300 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002301 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002302 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002303
Benjamin Petersonddd19492018-09-16 22:38:02 -07002304 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002305 PyObject *v = GETLOCAL(oparg);
2306 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002307 SETLOCAL(oparg, NULL);
2308 DISPATCH();
2309 }
2310 format_exc_check_arg(
2311 PyExc_UnboundLocalError,
2312 UNBOUNDLOCAL_ERROR_MSG,
2313 PyTuple_GetItem(co->co_varnames, oparg)
2314 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002315 goto error;
2316 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002317
Benjamin Petersonddd19492018-09-16 22:38:02 -07002318 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002319 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002320 PyObject *oldobj = PyCell_GET(cell);
2321 if (oldobj != NULL) {
2322 PyCell_SET(cell, NULL);
2323 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002324 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002325 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002326 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002327 goto error;
2328 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002329
Benjamin Petersonddd19492018-09-16 22:38:02 -07002330 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002331 PyObject *cell = freevars[oparg];
2332 Py_INCREF(cell);
2333 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002334 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002335 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002336
Benjamin Petersonddd19492018-09-16 22:38:02 -07002337 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002338 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002339 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002340 assert(locals);
2341 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2342 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2343 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2344 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2345 if (PyDict_CheckExact(locals)) {
2346 value = PyDict_GetItem(locals, name);
2347 Py_XINCREF(value);
2348 }
2349 else {
2350 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002351 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002352 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2353 goto error;
2354 PyErr_Clear();
2355 }
2356 }
2357 if (!value) {
2358 PyObject *cell = freevars[oparg];
2359 value = PyCell_GET(cell);
2360 if (value == NULL) {
2361 format_exc_unbound(co, oparg);
2362 goto error;
2363 }
2364 Py_INCREF(value);
2365 }
2366 PUSH(value);
2367 DISPATCH();
2368 }
2369
Benjamin Petersonddd19492018-09-16 22:38:02 -07002370 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002371 PyObject *cell = freevars[oparg];
2372 PyObject *value = PyCell_GET(cell);
2373 if (value == NULL) {
2374 format_exc_unbound(co, oparg);
2375 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002376 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002377 Py_INCREF(value);
2378 PUSH(value);
2379 DISPATCH();
2380 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002381
Benjamin Petersonddd19492018-09-16 22:38:02 -07002382 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002383 PyObject *v = POP();
2384 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002385 PyObject *oldobj = PyCell_GET(cell);
2386 PyCell_SET(cell, v);
2387 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002388 DISPATCH();
2389 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002390
Benjamin Petersonddd19492018-09-16 22:38:02 -07002391 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002392 PyObject *str;
2393 PyObject *empty = PyUnicode_New(0, 0);
2394 if (empty == NULL) {
2395 goto error;
2396 }
2397 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2398 Py_DECREF(empty);
2399 if (str == NULL)
2400 goto error;
2401 while (--oparg >= 0) {
2402 PyObject *item = POP();
2403 Py_DECREF(item);
2404 }
2405 PUSH(str);
2406 DISPATCH();
2407 }
2408
Benjamin Petersonddd19492018-09-16 22:38:02 -07002409 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002410 PyObject *tup = PyTuple_New(oparg);
2411 if (tup == NULL)
2412 goto error;
2413 while (--oparg >= 0) {
2414 PyObject *item = POP();
2415 PyTuple_SET_ITEM(tup, oparg, item);
2416 }
2417 PUSH(tup);
2418 DISPATCH();
2419 }
2420
Benjamin Petersonddd19492018-09-16 22:38:02 -07002421 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002422 PyObject *list = PyList_New(oparg);
2423 if (list == NULL)
2424 goto error;
2425 while (--oparg >= 0) {
2426 PyObject *item = POP();
2427 PyList_SET_ITEM(list, oparg, item);
2428 }
2429 PUSH(list);
2430 DISPATCH();
2431 }
2432
Benjamin Petersonddd19492018-09-16 22:38:02 -07002433 case TARGET(BUILD_TUPLE_UNPACK_WITH_CALL):
2434 case TARGET(BUILD_TUPLE_UNPACK):
2435 case TARGET(BUILD_LIST_UNPACK): {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002436 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002437 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002438 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002439 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002440
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002441 if (sum == NULL)
2442 goto error;
2443
2444 for (i = oparg; i > 0; i--) {
2445 PyObject *none_val;
2446
2447 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2448 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002449 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002450 PyErr_ExceptionMatches(PyExc_TypeError))
2451 {
2452 check_args_iterable(PEEK(1 + oparg), PEEK(i));
Serhiy Storchaka73442852016-10-02 10:33:46 +03002453 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002454 Py_DECREF(sum);
2455 goto error;
2456 }
2457 Py_DECREF(none_val);
2458 }
2459
2460 if (convert_to_tuple) {
2461 return_value = PyList_AsTuple(sum);
2462 Py_DECREF(sum);
2463 if (return_value == NULL)
2464 goto error;
2465 }
2466 else {
2467 return_value = sum;
2468 }
2469
2470 while (oparg--)
2471 Py_DECREF(POP());
2472 PUSH(return_value);
2473 DISPATCH();
2474 }
2475
Benjamin Petersonddd19492018-09-16 22:38:02 -07002476 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002477 PyObject *set = PySet_New(NULL);
2478 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002479 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002480 if (set == NULL)
2481 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002482 for (i = oparg; i > 0; i--) {
2483 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002484 if (err == 0)
2485 err = PySet_Add(set, item);
2486 Py_DECREF(item);
2487 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002488 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002489 if (err != 0) {
2490 Py_DECREF(set);
2491 goto error;
2492 }
2493 PUSH(set);
2494 DISPATCH();
2495 }
2496
Benjamin Petersonddd19492018-09-16 22:38:02 -07002497 case TARGET(BUILD_SET_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002498 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002499 PyObject *sum = PySet_New(NULL);
2500 if (sum == NULL)
2501 goto error;
2502
2503 for (i = oparg; i > 0; i--) {
2504 if (_PySet_Update(sum, PEEK(i)) < 0) {
2505 Py_DECREF(sum);
2506 goto error;
2507 }
2508 }
2509
2510 while (oparg--)
2511 Py_DECREF(POP());
2512 PUSH(sum);
2513 DISPATCH();
2514 }
2515
Benjamin Petersonddd19492018-09-16 22:38:02 -07002516 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002517 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002518 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2519 if (map == NULL)
2520 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002521 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002522 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002523 PyObject *key = PEEK(2*i);
2524 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002525 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002526 if (err != 0) {
2527 Py_DECREF(map);
2528 goto error;
2529 }
2530 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002531
2532 while (oparg--) {
2533 Py_DECREF(POP());
2534 Py_DECREF(POP());
2535 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002536 PUSH(map);
2537 DISPATCH();
2538 }
2539
Benjamin Petersonddd19492018-09-16 22:38:02 -07002540 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002541 _Py_IDENTIFIER(__annotations__);
2542 int err;
2543 PyObject *ann_dict;
2544 if (f->f_locals == NULL) {
2545 PyErr_Format(PyExc_SystemError,
2546 "no locals found when setting up annotations");
2547 goto error;
2548 }
2549 /* check if __annotations__ in locals()... */
2550 if (PyDict_CheckExact(f->f_locals)) {
2551 ann_dict = _PyDict_GetItemId(f->f_locals,
2552 &PyId___annotations__);
2553 if (ann_dict == NULL) {
2554 /* ...if not, create a new one */
2555 ann_dict = PyDict_New();
2556 if (ann_dict == NULL) {
2557 goto error;
2558 }
2559 err = _PyDict_SetItemId(f->f_locals,
2560 &PyId___annotations__, ann_dict);
2561 Py_DECREF(ann_dict);
2562 if (err != 0) {
2563 goto error;
2564 }
2565 }
2566 }
2567 else {
2568 /* do the same if locals() is not a dict */
2569 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2570 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002571 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002572 }
2573 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2574 if (ann_dict == NULL) {
2575 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2576 goto error;
2577 }
2578 PyErr_Clear();
2579 ann_dict = PyDict_New();
2580 if (ann_dict == NULL) {
2581 goto error;
2582 }
2583 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2584 Py_DECREF(ann_dict);
2585 if (err != 0) {
2586 goto error;
2587 }
2588 }
2589 else {
2590 Py_DECREF(ann_dict);
2591 }
2592 }
2593 DISPATCH();
2594 }
2595
Benjamin Petersonddd19492018-09-16 22:38:02 -07002596 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002597 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002598 PyObject *map;
2599 PyObject *keys = TOP();
2600 if (!PyTuple_CheckExact(keys) ||
2601 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2602 PyErr_SetString(PyExc_SystemError,
2603 "bad BUILD_CONST_KEY_MAP keys argument");
2604 goto error;
2605 }
2606 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2607 if (map == NULL) {
2608 goto error;
2609 }
2610 for (i = oparg; i > 0; i--) {
2611 int err;
2612 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2613 PyObject *value = PEEK(i + 1);
2614 err = PyDict_SetItem(map, key, value);
2615 if (err != 0) {
2616 Py_DECREF(map);
2617 goto error;
2618 }
2619 }
2620
2621 Py_DECREF(POP());
2622 while (oparg--) {
2623 Py_DECREF(POP());
2624 }
2625 PUSH(map);
2626 DISPATCH();
2627 }
2628
Benjamin Petersonddd19492018-09-16 22:38:02 -07002629 case TARGET(BUILD_MAP_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002630 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002631 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002632 if (sum == NULL)
2633 goto error;
2634
2635 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002636 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002637 if (PyDict_Update(sum, arg) < 0) {
2638 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2639 PyErr_Format(PyExc_TypeError,
Berker Peksag8e9045d2016-10-02 13:08:25 +03002640 "'%.200s' object is not a mapping",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002641 arg->ob_type->tp_name);
2642 }
2643 Py_DECREF(sum);
2644 goto error;
2645 }
2646 }
2647
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002648 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002649 Py_DECREF(POP());
2650 PUSH(sum);
2651 DISPATCH();
2652 }
2653
Benjamin Petersonddd19492018-09-16 22:38:02 -07002654 case TARGET(BUILD_MAP_UNPACK_WITH_CALL): {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002655 Py_ssize_t i;
2656 PyObject *sum = PyDict_New();
2657 if (sum == NULL)
2658 goto error;
2659
2660 for (i = oparg; i > 0; i--) {
2661 PyObject *arg = PEEK(i);
2662 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002663 Py_DECREF(sum);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02002664 format_kwargs_error(PEEK(2 + oparg), arg);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002665 goto error;
2666 }
2667 }
2668
2669 while (oparg--)
2670 Py_DECREF(POP());
2671 PUSH(sum);
2672 DISPATCH();
2673 }
2674
Benjamin Petersonddd19492018-09-16 22:38:02 -07002675 case TARGET(MAP_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002676 PyObject *key = TOP();
2677 PyObject *value = SECOND();
2678 PyObject *map;
2679 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002680 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002681 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002682 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002683 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002684 Py_DECREF(value);
2685 Py_DECREF(key);
2686 if (err != 0)
2687 goto error;
2688 PREDICT(JUMP_ABSOLUTE);
2689 DISPATCH();
2690 }
2691
Benjamin Petersonddd19492018-09-16 22:38:02 -07002692 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002693 PyObject *name = GETITEM(names, oparg);
2694 PyObject *owner = TOP();
2695 PyObject *res = PyObject_GetAttr(owner, name);
2696 Py_DECREF(owner);
2697 SET_TOP(res);
2698 if (res == NULL)
2699 goto error;
2700 DISPATCH();
2701 }
2702
Benjamin Petersonddd19492018-09-16 22:38:02 -07002703 case TARGET(COMPARE_OP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002704 PyObject *right = POP();
2705 PyObject *left = TOP();
2706 PyObject *res = cmp_outcome(oparg, left, right);
2707 Py_DECREF(left);
2708 Py_DECREF(right);
2709 SET_TOP(res);
2710 if (res == NULL)
2711 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002712 PREDICT(POP_JUMP_IF_FALSE);
2713 PREDICT(POP_JUMP_IF_TRUE);
2714 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002715 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002716
Benjamin Petersonddd19492018-09-16 22:38:02 -07002717 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002718 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002719 PyObject *fromlist = POP();
2720 PyObject *level = TOP();
2721 PyObject *res;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002722 res = import_name(f, name, fromlist, level);
2723 Py_DECREF(level);
2724 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002725 SET_TOP(res);
2726 if (res == NULL)
2727 goto error;
2728 DISPATCH();
2729 }
2730
Benjamin Petersonddd19492018-09-16 22:38:02 -07002731 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002732 PyObject *from = POP(), *locals;
2733 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002734 if (PyFrame_FastToLocalsWithError(f) < 0) {
2735 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002736 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002737 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002738
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002739 locals = f->f_locals;
2740 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002741 PyErr_SetString(PyExc_SystemError,
2742 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002743 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002744 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002745 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002746 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002747 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002748 Py_DECREF(from);
2749 if (err != 0)
2750 goto error;
2751 DISPATCH();
2752 }
Guido van Rossum25831651993-05-19 14:50:45 +00002753
Benjamin Petersonddd19492018-09-16 22:38:02 -07002754 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002755 PyObject *name = GETITEM(names, oparg);
2756 PyObject *from = TOP();
2757 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002758 res = import_from(from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002759 PUSH(res);
2760 if (res == NULL)
2761 goto error;
2762 DISPATCH();
2763 }
Thomas Wouters52152252000-08-17 22:55:00 +00002764
Benjamin Petersonddd19492018-09-16 22:38:02 -07002765 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002766 JUMPBY(oparg);
2767 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002768 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002769
Benjamin Petersonddd19492018-09-16 22:38:02 -07002770 case TARGET(POP_JUMP_IF_FALSE): {
2771 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002772 PyObject *cond = POP();
2773 int err;
2774 if (cond == Py_True) {
2775 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002776 FAST_DISPATCH();
2777 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002778 if (cond == Py_False) {
2779 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002780 JUMPTO(oparg);
2781 FAST_DISPATCH();
2782 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002783 err = PyObject_IsTrue(cond);
2784 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002785 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07002786 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002787 else if (err == 0)
2788 JUMPTO(oparg);
2789 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002790 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002791 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002792 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002793
Benjamin Petersonddd19492018-09-16 22:38:02 -07002794 case TARGET(POP_JUMP_IF_TRUE): {
2795 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002796 PyObject *cond = POP();
2797 int err;
2798 if (cond == Py_False) {
2799 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002800 FAST_DISPATCH();
2801 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002802 if (cond == Py_True) {
2803 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002804 JUMPTO(oparg);
2805 FAST_DISPATCH();
2806 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002807 err = PyObject_IsTrue(cond);
2808 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002809 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002810 JUMPTO(oparg);
2811 }
2812 else if (err == 0)
2813 ;
2814 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002815 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002816 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002817 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002818
Benjamin Petersonddd19492018-09-16 22:38:02 -07002819 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002820 PyObject *cond = TOP();
2821 int err;
2822 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002823 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002824 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002825 FAST_DISPATCH();
2826 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002827 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002828 JUMPTO(oparg);
2829 FAST_DISPATCH();
2830 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002831 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002832 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002833 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002834 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002835 }
2836 else if (err == 0)
2837 JUMPTO(oparg);
2838 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002839 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002840 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002841 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002842
Benjamin Petersonddd19492018-09-16 22:38:02 -07002843 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002844 PyObject *cond = TOP();
2845 int err;
2846 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002847 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002848 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002849 FAST_DISPATCH();
2850 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002851 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002852 JUMPTO(oparg);
2853 FAST_DISPATCH();
2854 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002855 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002856 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002857 JUMPTO(oparg);
2858 }
2859 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002860 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002861 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002862 }
2863 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002864 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002865 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002866 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002867
Benjamin Petersonddd19492018-09-16 22:38:02 -07002868 case TARGET(JUMP_ABSOLUTE): {
2869 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002870 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002871#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002872 /* Enabling this path speeds-up all while and for-loops by bypassing
2873 the per-loop checks for signals. By default, this should be turned-off
2874 because it prevents detection of a control-break in tight loops like
2875 "while 1: pass". Compile with this option turned-on when you need
2876 the speed-up and do not need break checking inside tight loops (ones
2877 that contain only instructions ending with FAST_DISPATCH).
2878 */
2879 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002880#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002881 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002882#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002883 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002884
Benjamin Petersonddd19492018-09-16 22:38:02 -07002885 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002886 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002887 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002888 PyObject *iter = PyObject_GetIter(iterable);
2889 Py_DECREF(iterable);
2890 SET_TOP(iter);
2891 if (iter == NULL)
2892 goto error;
2893 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002894 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04002895 DISPATCH();
2896 }
2897
Benjamin Petersonddd19492018-09-16 22:38:02 -07002898 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04002899 /* before: [obj]; after [getiter(obj)] */
2900 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04002901 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04002902 if (PyCoro_CheckExact(iterable)) {
2903 /* `iterable` is a coroutine */
2904 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
2905 /* and it is used in a 'yield from' expression of a
2906 regular generator. */
2907 Py_DECREF(iterable);
2908 SET_TOP(NULL);
2909 PyErr_SetString(PyExc_TypeError,
2910 "cannot 'yield from' a coroutine object "
2911 "in a non-coroutine generator");
2912 goto error;
2913 }
2914 }
2915 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04002916 /* `iterable` is not a generator. */
2917 iter = PyObject_GetIter(iterable);
2918 Py_DECREF(iterable);
2919 SET_TOP(iter);
2920 if (iter == NULL)
2921 goto error;
2922 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002923 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002924 DISPATCH();
2925 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002926
Benjamin Petersonddd19492018-09-16 22:38:02 -07002927 case TARGET(FOR_ITER): {
2928 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002929 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002930 PyObject *iter = TOP();
2931 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
2932 if (next != NULL) {
2933 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002934 PREDICT(STORE_FAST);
2935 PREDICT(UNPACK_SEQUENCE);
2936 DISPATCH();
2937 }
2938 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002939 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2940 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08002941 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002942 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002943 PyErr_Clear();
2944 }
2945 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00002946 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002947 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002948 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002949 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002950 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002951 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002952
Benjamin Petersonddd19492018-09-16 22:38:02 -07002953 case TARGET(SETUP_FINALLY): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002954 /* NOTE: If you add any new block-setup opcodes that
2955 are not try/except/finally handlers, you may need
2956 to update the PyGen_NeedsFinalizing() function.
2957 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002958
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002959 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002960 STACK_LEVEL());
2961 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002962 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002963
Benjamin Petersonddd19492018-09-16 22:38:02 -07002964 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04002965 _Py_IDENTIFIER(__aexit__);
2966 _Py_IDENTIFIER(__aenter__);
2967
2968 PyObject *mgr = TOP();
2969 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
2970 *enter;
2971 PyObject *res;
2972 if (exit == NULL)
2973 goto error;
2974 SET_TOP(exit);
2975 enter = special_lookup(mgr, &PyId___aenter__);
2976 Py_DECREF(mgr);
2977 if (enter == NULL)
2978 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01002979 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04002980 Py_DECREF(enter);
2981 if (res == NULL)
2982 goto error;
2983 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002984 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002985 DISPATCH();
2986 }
2987
Benjamin Petersonddd19492018-09-16 22:38:02 -07002988 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04002989 PyObject *res = POP();
2990 /* Setup the finally block before pushing the result
2991 of __aenter__ on the stack. */
2992 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2993 STACK_LEVEL());
2994 PUSH(res);
2995 DISPATCH();
2996 }
2997
Benjamin Petersonddd19492018-09-16 22:38:02 -07002998 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05002999 _Py_IDENTIFIER(__exit__);
3000 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003001 PyObject *mgr = TOP();
Raymond Hettingera3fec152016-11-21 17:24:23 -08003002 PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003003 PyObject *res;
Raymond Hettingera3fec152016-11-21 17:24:23 -08003004 if (enter == NULL)
3005 goto error;
3006 exit = special_lookup(mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003007 if (exit == NULL) {
3008 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003009 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003010 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003011 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003012 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003013 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003014 Py_DECREF(enter);
3015 if (res == NULL)
3016 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003017 /* Setup the finally block before pushing the result
3018 of __enter__ on the stack. */
3019 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3020 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003021
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003022 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003023 DISPATCH();
3024 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003025
Benjamin Petersonddd19492018-09-16 22:38:02 -07003026 case TARGET(WITH_CLEANUP_START): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003027 /* At the top of the stack are 1 or 6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003028 how/why we entered the finally clause:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003029 - TOP = NULL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003030 - (TOP, SECOND, THIRD) = exc_info()
3031 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003032 Below them is EXIT, the context.__exit__ or context.__aexit__
3033 bound method.
3034 In the first case, we must call
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003035 EXIT(None, None, None)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003036 otherwise we must call
3037 EXIT(TOP, SECOND, THIRD)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003038
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003039 In the first case, we remove EXIT from the
3040 stack, leaving TOP, and push TOP on the stack.
3041 Otherwise we shift the bottom 3 values of the
3042 stack down, replace the empty spot with NULL, and push
3043 None on the stack.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003044
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003045 Finally we push the result of the call.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003046 */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003047 PyObject *stack[3];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003048 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003049 PyObject *exc, *val, *tb, *res;
3050
3051 val = tb = Py_None;
3052 exc = TOP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003053 if (exc == NULL) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003054 STACK_SHRINK(1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003055 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003056 SET_TOP(exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003057 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003058 }
3059 else {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003060 assert(PyExceptionClass_Check(exc));
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003061 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003062 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003063 val = SECOND();
3064 tb = THIRD();
3065 tp2 = FOURTH();
3066 exc2 = PEEK(5);
3067 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003068 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003069 SET_VALUE(7, tb2);
3070 SET_VALUE(6, exc2);
3071 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003072 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3073 SET_FOURTH(NULL);
3074 /* We just shifted the stack down, so we have
3075 to tell the except handler block that the
3076 values are lower than it expects. */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003077 assert(f->f_iblock > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003078 block = &f->f_blockstack[f->f_iblock - 1];
3079 assert(block->b_type == EXCEPT_HANDLER);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003080 assert(block->b_level > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003081 block->b_level--;
3082 }
Victor Stinner842cfff2016-12-01 14:45:31 +01003083
3084 stack[0] = exc;
3085 stack[1] = val;
3086 stack[2] = tb;
3087 res = _PyObject_FastCall(exit_func, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003088 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003089 if (res == NULL)
3090 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003091
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003092 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003093 PUSH(exc);
3094 PUSH(res);
3095 PREDICT(WITH_CLEANUP_FINISH);
3096 DISPATCH();
3097 }
3098
Benjamin Petersonddd19492018-09-16 22:38:02 -07003099 case TARGET(WITH_CLEANUP_FINISH): {
3100 PREDICTED(WITH_CLEANUP_FINISH);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003101 /* TOP = the result of calling the context.__exit__ bound method
3102 SECOND = either None or exception type
3103
3104 If SECOND is None below is NULL or the return address,
3105 otherwise below are 7 values representing an exception.
3106 */
Yury Selivanov75445082015-05-11 22:57:16 -04003107 PyObject *res = POP();
3108 PyObject *exc = POP();
3109 int err;
3110
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003111 if (exc != Py_None)
3112 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003113 else
3114 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003115
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003116 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003117 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003118
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003119 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003120 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003121 else if (err > 0) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003122 /* There was an exception and a True return.
3123 * We must manually unwind the EXCEPT_HANDLER block
3124 * which was created when the exception was caught,
Quan Tian3bd0d622018-10-20 05:30:03 +08003125 * otherwise the stack will be in an inconsistent state.
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003126 */
3127 PyTryBlock *b = PyFrame_BlockPop(f);
3128 assert(b->b_type == EXCEPT_HANDLER);
3129 UNWIND_EXCEPT_HANDLER(b);
3130 PUSH(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003131 }
3132 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003133 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003134 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003135
Benjamin Petersonddd19492018-09-16 22:38:02 -07003136 case TARGET(LOAD_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003137 /* Designed to work in tamdem with CALL_METHOD. */
3138 PyObject *name = GETITEM(names, oparg);
3139 PyObject *obj = TOP();
3140 PyObject *meth = NULL;
3141
3142 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3143
Yury Selivanovf2392132016-12-13 19:03:51 -05003144 if (meth == NULL) {
3145 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003146 goto error;
3147 }
3148
3149 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003150 /* We can bypass temporary bound method object.
3151 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003152
INADA Naoki015bce62017-01-16 17:23:30 +09003153 meth | self | arg1 | ... | argN
3154 */
3155 SET_TOP(meth);
3156 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003157 }
3158 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003159 /* meth is not an unbound method (but a regular attr, or
3160 something was returned by a descriptor protocol). Set
3161 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003162 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003163
3164 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003165 */
INADA Naoki015bce62017-01-16 17:23:30 +09003166 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003167 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003168 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003169 }
3170 DISPATCH();
3171 }
3172
Benjamin Petersonddd19492018-09-16 22:38:02 -07003173 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003174 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003175 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003176
3177 sp = stack_pointer;
3178
INADA Naoki015bce62017-01-16 17:23:30 +09003179 meth = PEEK(oparg + 2);
3180 if (meth == NULL) {
3181 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3182 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003183
3184 Stack layout:
3185
INADA Naoki015bce62017-01-16 17:23:30 +09003186 ... | NULL | callable | arg1 | ... | argN
3187 ^- TOP()
3188 ^- (-oparg)
3189 ^- (-oparg-1)
3190 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003191
Ville Skyttä49b27342017-08-03 09:00:59 +03003192 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003193 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003194 */
Yury Selivanovf2392132016-12-13 19:03:51 -05003195 res = call_function(&sp, oparg, NULL);
3196 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003197 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003198 }
3199 else {
3200 /* This is a method call. Stack layout:
3201
INADA Naoki015bce62017-01-16 17:23:30 +09003202 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003203 ^- TOP()
3204 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003205 ^- (-oparg-1)
3206 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003207
INADA Naoki015bce62017-01-16 17:23:30 +09003208 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003209 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003210 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003211 */
3212 res = call_function(&sp, oparg + 1, NULL);
3213 stack_pointer = sp;
3214 }
3215
3216 PUSH(res);
3217 if (res == NULL)
3218 goto error;
3219 DISPATCH();
3220 }
3221
Benjamin Petersonddd19492018-09-16 22:38:02 -07003222 case TARGET(CALL_FUNCTION): {
3223 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003224 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003225 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003226 res = call_function(&sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003227 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003228 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003229 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003230 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003231 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003232 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003233 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003234
Benjamin Petersonddd19492018-09-16 22:38:02 -07003235 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003236 PyObject **sp, *res, *names;
3237
3238 names = POP();
3239 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003240 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003241 res = call_function(&sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003242 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003243 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003244 Py_DECREF(names);
3245
3246 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003247 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003248 }
3249 DISPATCH();
3250 }
3251
Benjamin Petersonddd19492018-09-16 22:38:02 -07003252 case TARGET(CALL_FUNCTION_EX): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003253 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003254 if (oparg & 0x01) {
3255 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003256 if (!PyDict_CheckExact(kwargs)) {
3257 PyObject *d = PyDict_New();
3258 if (d == NULL)
3259 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003260 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003261 Py_DECREF(d);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003262 format_kwargs_error(SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003263 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003264 goto error;
3265 }
3266 Py_DECREF(kwargs);
3267 kwargs = d;
3268 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003269 assert(PyDict_CheckExact(kwargs));
3270 }
3271 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003272 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003273 if (!PyTuple_CheckExact(callargs)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003274 if (check_args_iterable(func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003275 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003276 goto error;
3277 }
3278 Py_SETREF(callargs, PySequence_Tuple(callargs));
3279 if (callargs == NULL) {
3280 goto error;
3281 }
3282 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003283 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003284
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003285 result = do_call_core(func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003286 Py_DECREF(func);
3287 Py_DECREF(callargs);
3288 Py_XDECREF(kwargs);
3289
3290 SET_TOP(result);
3291 if (result == NULL) {
3292 goto error;
3293 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003294 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003295 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003296
Benjamin Petersonddd19492018-09-16 22:38:02 -07003297 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003298 PyObject *qualname = POP();
3299 PyObject *codeobj = POP();
3300 PyFunctionObject *func = (PyFunctionObject *)
3301 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003302
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003303 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003304 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003305 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003306 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003307 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003308
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003309 if (oparg & 0x08) {
3310 assert(PyTuple_CheckExact(TOP()));
3311 func ->func_closure = POP();
3312 }
3313 if (oparg & 0x04) {
3314 assert(PyDict_CheckExact(TOP()));
3315 func->func_annotations = POP();
3316 }
3317 if (oparg & 0x02) {
3318 assert(PyDict_CheckExact(TOP()));
3319 func->func_kwdefaults = POP();
3320 }
3321 if (oparg & 0x01) {
3322 assert(PyTuple_CheckExact(TOP()));
3323 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003324 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003325
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003326 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003327 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003328 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003329
Benjamin Petersonddd19492018-09-16 22:38:02 -07003330 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003331 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003332 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003333 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003334 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003335 step = NULL;
3336 stop = POP();
3337 start = TOP();
3338 slice = PySlice_New(start, stop, step);
3339 Py_DECREF(start);
3340 Py_DECREF(stop);
3341 Py_XDECREF(step);
3342 SET_TOP(slice);
3343 if (slice == NULL)
3344 goto error;
3345 DISPATCH();
3346 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003347
Benjamin Petersonddd19492018-09-16 22:38:02 -07003348 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003349 /* Handles f-string value formatting. */
3350 PyObject *result;
3351 PyObject *fmt_spec;
3352 PyObject *value;
3353 PyObject *(*conv_fn)(PyObject *);
3354 int which_conversion = oparg & FVC_MASK;
3355 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3356
3357 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003358 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003359
3360 /* See if any conversion is specified. */
3361 switch (which_conversion) {
3362 case FVC_STR: conv_fn = PyObject_Str; break;
3363 case FVC_REPR: conv_fn = PyObject_Repr; break;
3364 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3365
3366 /* Must be 0 (meaning no conversion), since only four
3367 values are allowed by (oparg & FVC_MASK). */
3368 default: conv_fn = NULL; break;
3369 }
3370
3371 /* If there's a conversion function, call it and replace
3372 value with that result. Otherwise, just use value,
3373 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003374 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003375 result = conv_fn(value);
3376 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003377 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003378 Py_XDECREF(fmt_spec);
3379 goto error;
3380 }
3381 value = result;
3382 }
3383
3384 /* If value is a unicode object, and there's no fmt_spec,
3385 then we know the result of format(value) is value
3386 itself. In that case, skip calling format(). I plan to
3387 move this optimization in to PyObject_Format()
3388 itself. */
3389 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3390 /* Do nothing, just transfer ownership to result. */
3391 result = value;
3392 } else {
3393 /* Actually call format(). */
3394 result = PyObject_Format(value, fmt_spec);
3395 Py_DECREF(value);
3396 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003397 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003398 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003399 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003400 }
3401
Eric V. Smith135d5f42016-02-05 18:23:08 -05003402 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003403 DISPATCH();
3404 }
3405
Benjamin Petersonddd19492018-09-16 22:38:02 -07003406 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003407 int oldoparg = oparg;
3408 NEXTOPARG();
3409 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003410 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003411 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003412
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003413
Antoine Pitrou042b1282010-08-13 21:15:58 +00003414#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003415 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003416#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003417 default:
3418 fprintf(stderr,
3419 "XXX lineno: %d, opcode: %d\n",
3420 PyFrame_GetLineNumber(f),
3421 opcode);
3422 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003423 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003425 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003426
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003427 /* This should never be reached. Every opcode should end with DISPATCH()
3428 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003429 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003430
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003431error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003432 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003433#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003434 if (!PyErr_Occurred())
3435 PyErr_SetString(PyExc_SystemError,
3436 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003437#else
3438 assert(PyErr_Occurred());
3439#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003440
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003441 /* Log traceback info. */
3442 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003443
Benjamin Peterson51f46162013-01-23 08:38:47 -05003444 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003445 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3446 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003447
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003448exception_unwind:
3449 /* Unwind stacks if an exception occurred */
3450 while (f->f_iblock > 0) {
3451 /* Pop the current block. */
3452 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003454 if (b->b_type == EXCEPT_HANDLER) {
3455 UNWIND_EXCEPT_HANDLER(b);
3456 continue;
3457 }
3458 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003459 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003460 PyObject *exc, *val, *tb;
3461 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003462 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003463 /* Beware, this invalidates all b->b_* fields */
3464 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003465 PUSH(exc_info->exc_traceback);
3466 PUSH(exc_info->exc_value);
3467 if (exc_info->exc_type != NULL) {
3468 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003469 }
3470 else {
3471 Py_INCREF(Py_None);
3472 PUSH(Py_None);
3473 }
3474 PyErr_Fetch(&exc, &val, &tb);
3475 /* Make the raw exception data
3476 available to the handler,
3477 so a program can emulate the
3478 Python main loop. */
3479 PyErr_NormalizeException(
3480 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003481 if (tb != NULL)
3482 PyException_SetTraceback(val, tb);
3483 else
3484 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003485 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003486 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003487 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003488 exc_info->exc_value = val;
3489 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003490 if (tb == NULL)
3491 tb = Py_None;
3492 Py_INCREF(tb);
3493 PUSH(tb);
3494 PUSH(val);
3495 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003496 JUMPTO(handler);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003497 /* Resume normal execution */
3498 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003499 }
3500 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003501
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003502 /* End the loop as we still have an error */
3503 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003504 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003506 /* Pop remaining stack entries. */
3507 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003508 PyObject *o = POP();
3509 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003510 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003511
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003512 assert(retval == NULL);
3513 assert(PyErr_Occurred());
Guido van Rossumac7be682001-01-17 15:42:30 +00003514
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003515return_or_yield:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003516 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003517 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003518 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3519 tstate, f, PyTrace_RETURN, retval)) {
3520 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003521 }
3522 }
3523 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003524 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3525 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003526 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003527 }
3528 }
3529 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003531 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003532exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003533 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3534 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003535 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003536 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003537 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003538
Victor Stinnerefde1462015-03-21 15:04:43 +01003539 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003540}
3541
Benjamin Petersonb204a422011-06-05 22:04:07 -05003542static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003543format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3544{
3545 int err;
3546 Py_ssize_t len = PyList_GET_SIZE(names);
3547 PyObject *name_str, *comma, *tail, *tmp;
3548
3549 assert(PyList_CheckExact(names));
3550 assert(len >= 1);
3551 /* Deal with the joys of natural language. */
3552 switch (len) {
3553 case 1:
3554 name_str = PyList_GET_ITEM(names, 0);
3555 Py_INCREF(name_str);
3556 break;
3557 case 2:
3558 name_str = PyUnicode_FromFormat("%U and %U",
3559 PyList_GET_ITEM(names, len - 2),
3560 PyList_GET_ITEM(names, len - 1));
3561 break;
3562 default:
3563 tail = PyUnicode_FromFormat(", %U, and %U",
3564 PyList_GET_ITEM(names, len - 2),
3565 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003566 if (tail == NULL)
3567 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003568 /* Chop off the last two objects in the list. This shouldn't actually
3569 fail, but we can't be too careful. */
3570 err = PyList_SetSlice(names, len - 2, len, NULL);
3571 if (err == -1) {
3572 Py_DECREF(tail);
3573 return;
3574 }
3575 /* Stitch everything up into a nice comma-separated list. */
3576 comma = PyUnicode_FromString(", ");
3577 if (comma == NULL) {
3578 Py_DECREF(tail);
3579 return;
3580 }
3581 tmp = PyUnicode_Join(comma, names);
3582 Py_DECREF(comma);
3583 if (tmp == NULL) {
3584 Py_DECREF(tail);
3585 return;
3586 }
3587 name_str = PyUnicode_Concat(tmp, tail);
3588 Py_DECREF(tmp);
3589 Py_DECREF(tail);
3590 break;
3591 }
3592 if (name_str == NULL)
3593 return;
3594 PyErr_Format(PyExc_TypeError,
3595 "%U() missing %i required %s argument%s: %U",
3596 co->co_name,
3597 len,
3598 kind,
3599 len == 1 ? "" : "s",
3600 name_str);
3601 Py_DECREF(name_str);
3602}
3603
3604static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003605missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003606 PyObject **fastlocals)
3607{
Victor Stinner74319ae2016-08-25 00:04:09 +02003608 Py_ssize_t i, j = 0;
3609 Py_ssize_t start, end;
3610 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003611 const char *kind = positional ? "positional" : "keyword-only";
3612 PyObject *missing_names;
3613
3614 /* Compute the names of the arguments that are missing. */
3615 missing_names = PyList_New(missing);
3616 if (missing_names == NULL)
3617 return;
3618 if (positional) {
3619 start = 0;
3620 end = co->co_argcount - defcount;
3621 }
3622 else {
3623 start = co->co_argcount;
3624 end = start + co->co_kwonlyargcount;
3625 }
3626 for (i = start; i < end; i++) {
3627 if (GETLOCAL(i) == NULL) {
3628 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3629 PyObject *name = PyObject_Repr(raw);
3630 if (name == NULL) {
3631 Py_DECREF(missing_names);
3632 return;
3633 }
3634 PyList_SET_ITEM(missing_names, j++, name);
3635 }
3636 }
3637 assert(j == missing);
3638 format_missing(kind, co, missing_names);
3639 Py_DECREF(missing_names);
3640}
3641
3642static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003643too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3644 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003645{
3646 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003647 Py_ssize_t kwonly_given = 0;
3648 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003649 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003650 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003651
Benjamin Petersone109c702011-06-24 09:37:26 -05003652 assert((co->co_flags & CO_VARARGS) == 0);
3653 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003654 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3655 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003656 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003657 }
3658 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003659 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003660 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003661 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003662 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003663 }
3664 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003665 plural = (co_argcount != 1);
3666 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003667 }
3668 if (sig == NULL)
3669 return;
3670 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003671 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3672 kwonly_sig = PyUnicode_FromFormat(format,
3673 given != 1 ? "s" : "",
3674 kwonly_given,
3675 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003676 if (kwonly_sig == NULL) {
3677 Py_DECREF(sig);
3678 return;
3679 }
3680 }
3681 else {
3682 /* This will not fail. */
3683 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003684 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003685 }
3686 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003687 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003688 co->co_name,
3689 sig,
3690 plural ? "s" : "",
3691 given,
3692 kwonly_sig,
3693 given == 1 && !kwonly_given ? "was" : "were");
3694 Py_DECREF(sig);
3695 Py_DECREF(kwonly_sig);
3696}
3697
Guido van Rossumc2e20742006-02-27 22:32:47 +00003698/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02003699 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003700 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003701
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003702PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +02003703_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003704 PyObject *const *args, Py_ssize_t argcount,
3705 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003706 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003707 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003708 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003709 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003710{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003711 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003712 PyFrameObject *f;
3713 PyObject *retval = NULL;
3714 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003715 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003716 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003717 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3718 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003719 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003720
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003721 if (globals == NULL) {
3722 PyErr_SetString(PyExc_SystemError,
3723 "PyEval_EvalCodeEx: NULL globals");
3724 return NULL;
3725 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003726
Victor Stinnerc7020012016-08-16 23:40:29 +02003727 /* Create the frame */
Victor Stinner50b48572018-11-01 01:51:40 +01003728 tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003729 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003730 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003731 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003732 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003733 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003734 fastlocals = f->f_localsplus;
3735 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003736
Victor Stinnerc7020012016-08-16 23:40:29 +02003737 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003738 if (co->co_flags & CO_VARKEYWORDS) {
3739 kwdict = PyDict_New();
3740 if (kwdict == NULL)
3741 goto fail;
3742 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003743 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003744 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003745 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003746 SETLOCAL(i, kwdict);
3747 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003748 else {
3749 kwdict = NULL;
3750 }
3751
3752 /* Copy positional arguments into local variables */
3753 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003754 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003755 }
3756 else {
3757 n = argcount;
3758 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003759 for (i = 0; i < n; i++) {
3760 x = args[i];
3761 Py_INCREF(x);
3762 SETLOCAL(i, x);
3763 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003764
3765 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003766 if (co->co_flags & CO_VARARGS) {
3767 u = PyTuple_New(argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003768 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003769 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003770 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003771 SETLOCAL(total_args, u);
3772 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003773 x = args[i];
3774 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003775 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003776 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003777 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003778
Serhiy Storchakab7281052016-09-12 00:52:40 +03003779 /* Handle keyword arguments passed as two strided arrays */
3780 kwcount *= kwstep;
3781 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003782 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003783 PyObject *keyword = kwnames[i];
3784 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003785 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003786
Benjamin Petersonb204a422011-06-05 22:04:07 -05003787 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3788 PyErr_Format(PyExc_TypeError,
3789 "%U() keywords must be strings",
3790 co->co_name);
3791 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003792 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003793
Benjamin Petersonb204a422011-06-05 22:04:07 -05003794 /* Speed hack: do raw pointer compares. As names are
3795 normally interned this should almost always hit. */
3796 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3797 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003798 PyObject *name = co_varnames[j];
3799 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003800 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003801 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003802 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003803
Benjamin Petersonb204a422011-06-05 22:04:07 -05003804 /* Slow fallback, just in case */
3805 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003806 PyObject *name = co_varnames[j];
3807 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
3808 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003809 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003810 }
3811 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003812 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003813 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003814 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003815
Victor Stinner231d1f32017-01-11 02:12:06 +01003816 assert(j >= total_args);
3817 if (kwdict == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003818 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003819 "%U() got an unexpected keyword argument '%S'",
3820 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003821 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003822 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003823
Christian Heimes0bd447f2013-07-20 14:48:10 +02003824 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3825 goto fail;
3826 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003827 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02003828
Benjamin Petersonb204a422011-06-05 22:04:07 -05003829 kw_found:
3830 if (GETLOCAL(j) != NULL) {
3831 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003832 "%U() got multiple values for argument '%S'",
3833 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003834 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003835 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003836 Py_INCREF(value);
3837 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003838 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003839
3840 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003841 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003842 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003843 goto fail;
3844 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003845
3846 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003847 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02003848 Py_ssize_t m = co->co_argcount - defcount;
3849 Py_ssize_t missing = 0;
3850 for (i = argcount; i < m; i++) {
3851 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003852 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02003853 }
3854 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003855 if (missing) {
3856 missing_arguments(co, missing, defcount, fastlocals);
3857 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003858 }
3859 if (n > m)
3860 i = n - m;
3861 else
3862 i = 0;
3863 for (; i < defcount; i++) {
3864 if (GETLOCAL(m+i) == NULL) {
3865 PyObject *def = defs[i];
3866 Py_INCREF(def);
3867 SETLOCAL(m+i, def);
3868 }
3869 }
3870 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003871
3872 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003873 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02003874 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003875 for (i = co->co_argcount; i < total_args; i++) {
3876 PyObject *name;
3877 if (GETLOCAL(i) != NULL)
3878 continue;
3879 name = PyTuple_GET_ITEM(co->co_varnames, i);
3880 if (kwdefs != NULL) {
3881 PyObject *def = PyDict_GetItem(kwdefs, name);
3882 if (def) {
3883 Py_INCREF(def);
3884 SETLOCAL(i, def);
3885 continue;
3886 }
3887 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003888 missing++;
3889 }
3890 if (missing) {
3891 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003892 goto fail;
3893 }
3894 }
3895
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003896 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003897 vars into frame. */
3898 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003899 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02003900 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05003901 /* Possibly account for the cell variable being an argument. */
3902 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07003903 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05003904 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05003905 /* Clear the local copy. */
3906 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003907 }
3908 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05003909 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003910 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05003911 if (c == NULL)
3912 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05003913 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003914 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003915
3916 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05003917 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3918 PyObject *o = PyTuple_GET_ITEM(closure, i);
3919 Py_INCREF(o);
3920 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003921 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003922
Yury Selivanoveb636452016-09-08 22:01:51 -07003923 /* Handle generator/coroutine/asynchronous generator */
3924 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003925 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04003926 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003927 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04003928
3929 if (is_coro && tstate->in_coroutine_wrapper) {
3930 assert(coro_wrapper != NULL);
3931 PyErr_Format(PyExc_RuntimeError,
3932 "coroutine wrapper %.200R attempted "
3933 "to recursively wrap %.200R",
3934 coro_wrapper,
3935 co);
3936 goto fail;
3937 }
Yury Selivanov75445082015-05-11 22:57:16 -04003938
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003939 /* Don't need to keep the reference to f_back, it will be set
3940 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003941 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003942
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003943 /* Create a new generator that owns the ready to run frame
3944 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04003945 if (is_coro) {
3946 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07003947 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
3948 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003949 } else {
3950 gen = PyGen_NewWithQualName(f, name, qualname);
3951 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003952 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003953 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003954 }
INADA Naoki9c157762016-12-26 18:52:46 +09003955
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003956 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04003957
Yury Selivanov94c22632015-06-04 10:16:51 -04003958 if (is_coro && coro_wrapper != NULL) {
3959 PyObject *wrapped;
3960 tstate->in_coroutine_wrapper = 1;
3961 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
3962 tstate->in_coroutine_wrapper = 0;
3963 return wrapped;
3964 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04003965
Yury Selivanov75445082015-05-11 22:57:16 -04003966 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003967 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003968
Victor Stinner59a73272016-12-09 18:51:13 +01003969 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003970
Thomas Woutersce272b62007-09-19 21:19:28 +00003971fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003973 /* decref'ing the frame can cause __del__ methods to get invoked,
3974 which can call back into Python. While we're done with the
3975 current Python frame (f), the associated C stack is still in use,
3976 so recursion_depth must be boosted for the duration.
3977 */
3978 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003979 if (Py_REFCNT(f) > 1) {
3980 Py_DECREF(f);
3981 _PyObject_GC_TRACK(f);
3982 }
3983 else {
3984 ++tstate->recursion_depth;
3985 Py_DECREF(f);
3986 --tstate->recursion_depth;
3987 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003988 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003989}
3990
Victor Stinner40ee3012014-06-16 15:59:28 +02003991PyObject *
3992PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003993 PyObject *const *args, int argcount,
3994 PyObject *const *kws, int kwcount,
3995 PyObject *const *defs, int defcount,
3996 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02003997{
3998 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02003999 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004000 kws, kws != NULL ? kws + 1 : NULL,
4001 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004002 defs, defcount,
4003 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004004 NULL, NULL);
4005}
Tim Peters5ca576e2001-06-18 22:08:13 +00004006
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004007static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05004008special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004009{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004010 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004011 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004012 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05004013 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004014 return NULL;
4015 }
4016 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004017}
4018
4019
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004020/* Logic for the raise statement (too complicated for inlining).
4021 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004022static int
Collin Winter828f04a2007-08-31 00:04:24 +00004023do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004024{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004025 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004026
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004027 if (exc == NULL) {
4028 /* Reraise */
Victor Stinner50b48572018-11-01 01:51:40 +01004029 PyThreadState *tstate = _PyThreadState_GET();
Mark Shannonae3087c2017-10-22 22:41:51 +01004030 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004031 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004032 type = exc_info->exc_type;
4033 value = exc_info->exc_value;
4034 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004035 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004036 PyErr_SetString(PyExc_RuntimeError,
4037 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004038 return 0;
4039 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004040 Py_XINCREF(type);
4041 Py_XINCREF(value);
4042 Py_XINCREF(tb);
4043 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004044 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004045 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004046
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004047 /* We support the following forms of raise:
4048 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004049 raise <instance>
4050 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004052 if (PyExceptionClass_Check(exc)) {
4053 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004054 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004055 if (value == NULL)
4056 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004057 if (!PyExceptionInstance_Check(value)) {
4058 PyErr_Format(PyExc_TypeError,
4059 "calling %R should have returned an instance of "
4060 "BaseException, not %R",
4061 type, Py_TYPE(value));
4062 goto raise_error;
4063 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004064 }
4065 else if (PyExceptionInstance_Check(exc)) {
4066 value = exc;
4067 type = PyExceptionInstance_Class(exc);
4068 Py_INCREF(type);
4069 }
4070 else {
4071 /* Not something you can raise. You get an exception
4072 anyway, just not what you specified :-) */
4073 Py_DECREF(exc);
4074 PyErr_SetString(PyExc_TypeError,
4075 "exceptions must derive from BaseException");
4076 goto raise_error;
4077 }
Collin Winter828f04a2007-08-31 00:04:24 +00004078
Serhiy Storchakac0191582016-09-27 11:37:10 +03004079 assert(type != NULL);
4080 assert(value != NULL);
4081
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004082 if (cause) {
4083 PyObject *fixed_cause;
4084 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004085 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004086 if (fixed_cause == NULL)
4087 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004088 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004089 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004090 else if (PyExceptionInstance_Check(cause)) {
4091 fixed_cause = cause;
4092 }
4093 else if (cause == Py_None) {
4094 Py_DECREF(cause);
4095 fixed_cause = NULL;
4096 }
4097 else {
4098 PyErr_SetString(PyExc_TypeError,
4099 "exception causes must derive from "
4100 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004101 goto raise_error;
4102 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004103 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004104 }
Collin Winter828f04a2007-08-31 00:04:24 +00004105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004106 PyErr_SetObject(type, value);
4107 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004108 Py_DECREF(value);
4109 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004110 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004111
4112raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004113 Py_XDECREF(value);
4114 Py_XDECREF(type);
4115 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004116 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004117}
4118
Tim Petersd6d010b2001-06-21 02:49:55 +00004119/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004120 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004121
Guido van Rossum0368b722007-05-11 16:50:42 +00004122 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4123 with a variable target.
4124*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004125
Barry Warsawe42b18f1997-08-25 22:13:04 +00004126static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004127unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004128{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004129 int i = 0, j = 0;
4130 Py_ssize_t ll = 0;
4131 PyObject *it; /* iter(v) */
4132 PyObject *w;
4133 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004134
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004135 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004136
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004137 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004138 if (it == NULL) {
4139 if (PyErr_ExceptionMatches(PyExc_TypeError) &&
4140 v->ob_type->tp_iter == NULL && !PySequence_Check(v))
4141 {
4142 PyErr_Format(PyExc_TypeError,
4143 "cannot unpack non-iterable %.200s object",
4144 v->ob_type->tp_name);
4145 }
4146 return 0;
4147 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004149 for (; i < argcnt; i++) {
4150 w = PyIter_Next(it);
4151 if (w == NULL) {
4152 /* Iterator done, via error or exhaustion. */
4153 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004154 if (argcntafter == -1) {
4155 PyErr_Format(PyExc_ValueError,
4156 "not enough values to unpack (expected %d, got %d)",
4157 argcnt, i);
4158 }
4159 else {
4160 PyErr_Format(PyExc_ValueError,
4161 "not enough values to unpack "
4162 "(expected at least %d, got %d)",
4163 argcnt + argcntafter, i);
4164 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004165 }
4166 goto Error;
4167 }
4168 *--sp = w;
4169 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004171 if (argcntafter == -1) {
4172 /* We better have exhausted the iterator now. */
4173 w = PyIter_Next(it);
4174 if (w == NULL) {
4175 if (PyErr_Occurred())
4176 goto Error;
4177 Py_DECREF(it);
4178 return 1;
4179 }
4180 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004181 PyErr_Format(PyExc_ValueError,
4182 "too many values to unpack (expected %d)",
4183 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004184 goto Error;
4185 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004186
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004187 l = PySequence_List(it);
4188 if (l == NULL)
4189 goto Error;
4190 *--sp = l;
4191 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004193 ll = PyList_GET_SIZE(l);
4194 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004195 PyErr_Format(PyExc_ValueError,
4196 "not enough values to unpack (expected at least %d, got %zd)",
4197 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004198 goto Error;
4199 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004201 /* Pop the "after-variable" args off the list. */
4202 for (j = argcntafter; j > 0; j--, i++) {
4203 *--sp = PyList_GET_ITEM(l, ll - j);
4204 }
4205 /* Resize the list. */
4206 Py_SIZE(l) = ll - argcntafter;
4207 Py_DECREF(it);
4208 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004209
Tim Petersd6d010b2001-06-21 02:49:55 +00004210Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004211 for (; i > 0; i--, sp++)
4212 Py_DECREF(*sp);
4213 Py_XDECREF(it);
4214 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004215}
4216
4217
Guido van Rossum96a42c81992-01-12 02:29:51 +00004218#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004219static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004220prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004221{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004222 printf("%s ", str);
4223 if (PyObject_Print(v, stdout, 0) != 0)
4224 PyErr_Clear(); /* Don't know what else to do */
4225 printf("\n");
4226 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004227}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004228#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004229
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004230static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004231call_exc_trace(Py_tracefunc func, PyObject *self,
4232 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004233{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004234 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004235 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004236 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004237 if (value == NULL) {
4238 value = Py_None;
4239 Py_INCREF(value);
4240 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004241 PyErr_NormalizeException(&type, &value, &orig_traceback);
4242 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004243 arg = PyTuple_Pack(3, type, value, traceback);
4244 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004245 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004246 return;
4247 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004248 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004249 Py_DECREF(arg);
4250 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004251 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004252 else {
4253 Py_XDECREF(type);
4254 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004255 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004256 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004257}
4258
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004259static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004260call_trace_protected(Py_tracefunc func, PyObject *obj,
4261 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004262 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004263{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004264 PyObject *type, *value, *traceback;
4265 int err;
4266 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004267 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004268 if (err == 0)
4269 {
4270 PyErr_Restore(type, value, traceback);
4271 return 0;
4272 }
4273 else {
4274 Py_XDECREF(type);
4275 Py_XDECREF(value);
4276 Py_XDECREF(traceback);
4277 return -1;
4278 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004279}
4280
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004281static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004282call_trace(Py_tracefunc func, PyObject *obj,
4283 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004284 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004285{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004286 int result;
4287 if (tstate->tracing)
4288 return 0;
4289 tstate->tracing++;
4290 tstate->use_tracing = 0;
4291 result = func(obj, frame, what, arg);
4292 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4293 || (tstate->c_profilefunc != NULL));
4294 tstate->tracing--;
4295 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004296}
4297
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004298PyObject *
4299_PyEval_CallTracing(PyObject *func, PyObject *args)
4300{
Victor Stinner50b48572018-11-01 01:51:40 +01004301 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004302 int save_tracing = tstate->tracing;
4303 int save_use_tracing = tstate->use_tracing;
4304 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004306 tstate->tracing = 0;
4307 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4308 || (tstate->c_profilefunc != NULL));
4309 result = PyObject_Call(func, args, NULL);
4310 tstate->tracing = save_tracing;
4311 tstate->use_tracing = save_use_tracing;
4312 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004313}
4314
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004315/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004316static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004317maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004318 PyThreadState *tstate, PyFrameObject *frame,
4319 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004320{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004321 int result = 0;
4322 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004324 /* If the last instruction executed isn't in the current
4325 instruction window, reset the window.
4326 */
4327 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4328 PyAddrPair bounds;
4329 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4330 &bounds);
4331 *instr_lb = bounds.ap_lower;
4332 *instr_ub = bounds.ap_upper;
4333 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004334 /* If the last instruction falls at the start of a line or if it
4335 represents a jump backwards, update the frame's line number and
4336 then call the trace function if we're tracing source lines.
4337 */
4338 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004339 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004340 if (frame->f_trace_lines) {
4341 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4342 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004343 }
George King20faa682017-10-18 17:44:22 -07004344 /* Always emit an opcode event if we're tracing all opcodes. */
4345 if (frame->f_trace_opcodes) {
4346 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4347 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004348 *instr_prev = frame->f_lasti;
4349 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004350}
4351
Fred Drake5755ce62001-06-27 19:19:46 +00004352void
4353PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004354{
Victor Stinner50b48572018-11-01 01:51:40 +01004355 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004356 PyObject *temp = tstate->c_profileobj;
4357 Py_XINCREF(arg);
4358 tstate->c_profilefunc = NULL;
4359 tstate->c_profileobj = NULL;
4360 /* Must make sure that tracing is not ignored if 'temp' is freed */
4361 tstate->use_tracing = tstate->c_tracefunc != NULL;
4362 Py_XDECREF(temp);
4363 tstate->c_profilefunc = func;
4364 tstate->c_profileobj = arg;
4365 /* Flag that tracing or profiling is turned on */
4366 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004367}
4368
4369void
4370PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4371{
Victor Stinner50b48572018-11-01 01:51:40 +01004372 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004373 PyObject *temp = tstate->c_traceobj;
4374 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4375 Py_XINCREF(arg);
4376 tstate->c_tracefunc = NULL;
4377 tstate->c_traceobj = NULL;
4378 /* Must make sure that profiling is not ignored if 'temp' is freed */
4379 tstate->use_tracing = tstate->c_profilefunc != NULL;
4380 Py_XDECREF(temp);
4381 tstate->c_tracefunc = func;
4382 tstate->c_traceobj = arg;
4383 /* Flag that tracing or profiling is turned on */
4384 tstate->use_tracing = ((func != NULL)
4385 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004386}
4387
Yury Selivanov75445082015-05-11 22:57:16 -04004388void
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004389_PyEval_SetCoroutineOriginTrackingDepth(int new_depth)
4390{
4391 assert(new_depth >= 0);
Victor Stinner50b48572018-11-01 01:51:40 +01004392 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004393 tstate->coroutine_origin_tracking_depth = new_depth;
4394}
4395
4396int
4397_PyEval_GetCoroutineOriginTrackingDepth(void)
4398{
Victor Stinner50b48572018-11-01 01:51:40 +01004399 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004400 return tstate->coroutine_origin_tracking_depth;
4401}
4402
4403void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004404_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004405{
Victor Stinner50b48572018-11-01 01:51:40 +01004406 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanov75445082015-05-11 22:57:16 -04004407
Yury Selivanov75445082015-05-11 22:57:16 -04004408 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004409 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004410}
4411
4412PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004413_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004414{
Victor Stinner50b48572018-11-01 01:51:40 +01004415 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanov75445082015-05-11 22:57:16 -04004416 return tstate->coroutine_wrapper;
4417}
4418
Yury Selivanoveb636452016-09-08 22:01:51 -07004419void
4420_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4421{
Victor Stinner50b48572018-11-01 01:51:40 +01004422 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004423
4424 Py_XINCREF(firstiter);
4425 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4426}
4427
4428PyObject *
4429_PyEval_GetAsyncGenFirstiter(void)
4430{
Victor Stinner50b48572018-11-01 01:51:40 +01004431 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004432 return tstate->async_gen_firstiter;
4433}
4434
4435void
4436_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4437{
Victor Stinner50b48572018-11-01 01:51:40 +01004438 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004439
4440 Py_XINCREF(finalizer);
4441 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4442}
4443
4444PyObject *
4445_PyEval_GetAsyncGenFinalizer(void)
4446{
Victor Stinner50b48572018-11-01 01:51:40 +01004447 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004448 return tstate->async_gen_finalizer;
4449}
4450
Guido van Rossumb209a111997-04-29 18:18:01 +00004451PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004452PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004453{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004454 PyFrameObject *current_frame = PyEval_GetFrame();
4455 if (current_frame == NULL)
Victor Stinnercaba55b2018-08-03 15:33:52 +02004456 return _PyInterpreterState_GET_UNSAFE()->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004457 else
4458 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004459}
4460
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004461/* Convenience function to get a builtin from its name */
4462PyObject *
4463_PyEval_GetBuiltinId(_Py_Identifier *name)
4464{
4465 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4466 if (attr) {
4467 Py_INCREF(attr);
4468 }
4469 else if (!PyErr_Occurred()) {
4470 PyErr_SetObject(PyExc_AttributeError, _PyUnicode_FromId(name));
4471 }
4472 return attr;
4473}
4474
Guido van Rossumb209a111997-04-29 18:18:01 +00004475PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004476PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004477{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004478 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004479 if (current_frame == NULL) {
4480 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004481 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004482 }
4483
4484 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4485 return NULL;
4486
4487 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004488 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004489}
4490
Guido van Rossumb209a111997-04-29 18:18:01 +00004491PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004492PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004493{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004494 PyFrameObject *current_frame = PyEval_GetFrame();
4495 if (current_frame == NULL)
4496 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004497
4498 assert(current_frame->f_globals != NULL);
4499 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004500}
4501
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004502PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004503PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004504{
Victor Stinner50b48572018-11-01 01:51:40 +01004505 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004506 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004507}
4508
Guido van Rossum6135a871995-01-09 17:53:26 +00004509int
Tim Peters5ba58662001-07-16 02:29:45 +00004510PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004511{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004512 PyFrameObject *current_frame = PyEval_GetFrame();
4513 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004515 if (current_frame != NULL) {
4516 const int codeflags = current_frame->f_code->co_flags;
4517 const int compilerflags = codeflags & PyCF_MASK;
4518 if (compilerflags) {
4519 result = 1;
4520 cf->cf_flags |= compilerflags;
4521 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004522#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004523 if (codeflags & CO_GENERATOR_ALLOWED) {
4524 result = 1;
4525 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4526 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004527#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004528 }
4529 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004530}
4531
Guido van Rossum3f5da241990-12-20 15:06:42 +00004532
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004533const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004534PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004535{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004536 if (PyMethod_Check(func))
4537 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4538 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004539 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004540 else if (PyCFunction_Check(func))
4541 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4542 else
4543 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004544}
4545
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004546const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004547PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004548{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004549 if (PyMethod_Check(func))
4550 return "()";
4551 else if (PyFunction_Check(func))
4552 return "()";
4553 else if (PyCFunction_Check(func))
4554 return "()";
4555 else
4556 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004557}
4558
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004559#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004560if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004561 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4562 tstate, tstate->frame, \
4563 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004564 x = NULL; \
4565 } \
4566 else { \
4567 x = call; \
4568 if (tstate->c_profilefunc != NULL) { \
4569 if (x == NULL) { \
4570 call_trace_protected(tstate->c_profilefunc, \
4571 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004572 tstate, tstate->frame, \
4573 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004574 /* XXX should pass (type, value, tb) */ \
4575 } else { \
4576 if (call_trace(tstate->c_profilefunc, \
4577 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004578 tstate, tstate->frame, \
4579 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004580 Py_DECREF(x); \
4581 x = NULL; \
4582 } \
4583 } \
4584 } \
4585 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004586} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004587 x = call; \
4588 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004589
Victor Stinner415c5102017-01-11 00:54:57 +01004590/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4591 to reduce the stack consumption. */
4592Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Benjamin Peterson4fd64b92016-09-09 14:57:58 -07004593call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004594{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004595 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004596 PyObject *func = *pfunc;
4597 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004598 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4599 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004600 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004601
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004602 /* Always dispatch PyCFunction first, because these are
4603 presumed to be the most frequent callable object.
4604 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004605 if (PyCFunction_Check(func)) {
Victor Stinner50b48572018-11-01 01:51:40 +01004606 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004607 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004608 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004609 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
Victor Stinner50b48572018-11-01 01:51:40 +01004610 PyThreadState *tstate = _PyThreadState_GET();
jdemeyer56868f92018-07-21 10:30:59 +02004611 if (nargs > 0 && tstate->use_tracing) {
4612 /* We need to create a temporary bound method as argument
4613 for profiling.
4614
4615 If nargs == 0, then this cannot work because we have no
4616 "self". In any case, the call itself would raise
4617 TypeError (foo needs an argument), so we just skip
4618 profiling. */
4619 PyObject *self = stack[0];
4620 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
jdemeyer147d9552018-07-23 18:41:20 +02004621 if (func != NULL) {
4622 C_TRACE(x, _PyCFunction_FastCallKeywords(func,
4623 stack+1, nargs-1,
4624 kwnames));
4625 Py_DECREF(func);
INADA Naoki93fac8d2017-03-07 14:24:37 +09004626 }
jdemeyer147d9552018-07-23 18:41:20 +02004627 else {
4628 x = NULL;
4629 }
INADA Naoki93fac8d2017-03-07 14:24:37 +09004630 }
4631 else {
4632 x = _PyMethodDescr_FastCallKeywords(func, stack, nargs, kwnames);
4633 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004634 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004635 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004636 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
Victor Stinnerb69ee8c2016-11-28 18:32:31 +01004637 /* Optimize access to bound methods. Reuse the Python stack
4638 to pass 'self' as the first argument, replace 'func'
4639 with 'self'. It avoids the creation of a new temporary tuple
4640 for arguments (to replace func with self) when the method uses
4641 FASTCALL. */
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004642 PyObject *self = PyMethod_GET_SELF(func);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004643 Py_INCREF(self);
4644 func = PyMethod_GET_FUNCTION(func);
4645 Py_INCREF(func);
4646 Py_SETREF(*pfunc, self);
4647 nargs++;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004648 stack--;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004649 }
4650 else {
4651 Py_INCREF(func);
4652 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004653
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004654 if (PyFunction_Check(func)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004655 x = _PyFunction_FastCallKeywords(func, stack, nargs, kwnames);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004656 }
4657 else {
4658 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4659 }
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004660 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004661 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004662
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004663 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4664
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004665 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004666 while ((*pp_stack) > pfunc) {
4667 w = EXT_POP(*pp_stack);
4668 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004669 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004671 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004672}
4673
Jeremy Hylton52820442001-01-03 23:52:36 +00004674static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004675do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004676{
jdemeyere89de732018-09-19 12:06:20 +02004677 PyObject *result;
4678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004679 if (PyCFunction_Check(func)) {
Victor Stinner50b48572018-11-01 01:51:40 +01004680 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004681 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004682 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004683 }
jdemeyere89de732018-09-19 12:06:20 +02004684 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
Victor Stinner50b48572018-11-01 01:51:40 +01004685 PyThreadState *tstate = _PyThreadState_GET();
jdemeyere89de732018-09-19 12:06:20 +02004686 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
4687 if (nargs > 0 && tstate->use_tracing) {
4688 /* We need to create a temporary bound method as argument
4689 for profiling.
4690
4691 If nargs == 0, then this cannot work because we have no
4692 "self". In any case, the call itself would raise
4693 TypeError (foo needs an argument), so we just skip
4694 profiling. */
4695 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
4696 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4697 if (func == NULL) {
4698 return NULL;
4699 }
4700
4701 C_TRACE(result, _PyCFunction_FastCallDict(func,
Victor Stinnerd17a6932018-11-09 16:56:48 +01004702 &_PyTuple_ITEMS(callargs)[1],
jdemeyere89de732018-09-19 12:06:20 +02004703 nargs - 1,
4704 kwdict));
4705 Py_DECREF(func);
4706 return result;
4707 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004708 }
jdemeyere89de732018-09-19 12:06:20 +02004709 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00004710}
4711
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004712/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004713 nb_index slot defined, and store in *pi.
4714 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004715 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004716 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004717*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004718int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004719_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004720{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004721 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004722 Py_ssize_t x;
4723 if (PyIndex_Check(v)) {
4724 x = PyNumber_AsSsize_t(v, NULL);
4725 if (x == -1 && PyErr_Occurred())
4726 return 0;
4727 }
4728 else {
4729 PyErr_SetString(PyExc_TypeError,
4730 "slice indices must be integers or "
4731 "None or have an __index__ method");
4732 return 0;
4733 }
4734 *pi = x;
4735 }
4736 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004737}
4738
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004739int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004740_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004741{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004742 Py_ssize_t x;
4743 if (PyIndex_Check(v)) {
4744 x = PyNumber_AsSsize_t(v, NULL);
4745 if (x == -1 && PyErr_Occurred())
4746 return 0;
4747 }
4748 else {
4749 PyErr_SetString(PyExc_TypeError,
4750 "slice indices must be integers or "
4751 "have an __index__ method");
4752 return 0;
4753 }
4754 *pi = x;
4755 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004756}
4757
4758
Guido van Rossum486364b2007-06-30 05:01:58 +00004759#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004760 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004761
Guido van Rossumb209a111997-04-29 18:18:01 +00004762static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004763cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004764{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004765 int res = 0;
4766 switch (op) {
4767 case PyCmp_IS:
4768 res = (v == w);
4769 break;
4770 case PyCmp_IS_NOT:
4771 res = (v != w);
4772 break;
4773 case PyCmp_IN:
4774 res = PySequence_Contains(w, v);
4775 if (res < 0)
4776 return NULL;
4777 break;
4778 case PyCmp_NOT_IN:
4779 res = PySequence_Contains(w, v);
4780 if (res < 0)
4781 return NULL;
4782 res = !res;
4783 break;
4784 case PyCmp_EXC_MATCH:
4785 if (PyTuple_Check(w)) {
4786 Py_ssize_t i, length;
4787 length = PyTuple_Size(w);
4788 for (i = 0; i < length; i += 1) {
4789 PyObject *exc = PyTuple_GET_ITEM(w, i);
4790 if (!PyExceptionClass_Check(exc)) {
4791 PyErr_SetString(PyExc_TypeError,
4792 CANNOT_CATCH_MSG);
4793 return NULL;
4794 }
4795 }
4796 }
4797 else {
4798 if (!PyExceptionClass_Check(w)) {
4799 PyErr_SetString(PyExc_TypeError,
4800 CANNOT_CATCH_MSG);
4801 return NULL;
4802 }
4803 }
4804 res = PyErr_GivenExceptionMatches(v, w);
4805 break;
4806 default:
4807 return PyObject_RichCompare(v, w, op);
4808 }
4809 v = res ? Py_True : Py_False;
4810 Py_INCREF(v);
4811 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004812}
4813
Thomas Wouters52152252000-08-17 22:55:00 +00004814static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004815import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
4816{
4817 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004818 PyObject *import_func, *res;
4819 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004820
4821 import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
4822 if (import_func == NULL) {
4823 PyErr_SetString(PyExc_ImportError, "__import__ not found");
4824 return NULL;
4825 }
4826
4827 /* Fast path for not overloaded __import__. */
Victor Stinnercaba55b2018-08-03 15:33:52 +02004828 if (import_func == _PyInterpreterState_GET_UNSAFE()->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004829 int ilevel = _PyLong_AsInt(level);
4830 if (ilevel == -1 && PyErr_Occurred()) {
4831 return NULL;
4832 }
4833 res = PyImport_ImportModuleLevelObject(
4834 name,
4835 f->f_globals,
4836 f->f_locals == NULL ? Py_None : f->f_locals,
4837 fromlist,
4838 ilevel);
4839 return res;
4840 }
4841
4842 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004843
4844 stack[0] = name;
4845 stack[1] = f->f_globals;
4846 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
4847 stack[3] = fromlist;
4848 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02004849 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004850 Py_DECREF(import_func);
4851 return res;
4852}
4853
4854static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004855import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004856{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004857 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02004858 _Py_IDENTIFIER(__name__);
Xiang Zhang4830f582017-03-21 11:13:42 +08004859 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004860
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004861 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02004862 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004863 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004864 /* Issue #17636: in case this failed because of a circular relative
4865 import, try to fallback on reading the module directly from
4866 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02004867 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07004868 if (pkgname == NULL) {
4869 goto error;
4870 }
Oren Milman6db70332017-09-19 14:23:01 +03004871 if (!PyUnicode_Check(pkgname)) {
4872 Py_CLEAR(pkgname);
4873 goto error;
4874 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004875 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07004876 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08004877 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004878 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07004879 }
Eric Snow3f9eee62017-09-15 16:35:20 -06004880 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004881 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07004882 if (x == NULL) {
4883 goto error;
4884 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004885 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004886 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07004887 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004888 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004889 if (pkgname == NULL) {
4890 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
4891 if (pkgname_or_unknown == NULL) {
4892 Py_XDECREF(pkgpath);
4893 return NULL;
4894 }
4895 } else {
4896 pkgname_or_unknown = pkgname;
4897 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004898
4899 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
4900 PyErr_Clear();
Xiang Zhang4830f582017-03-21 11:13:42 +08004901 errmsg = PyUnicode_FromFormat(
4902 "cannot import name %R from %R (unknown location)",
4903 name, pkgname_or_unknown
4904 );
4905 /* NULL check for errmsg done by PyErr_SetImportError. */
4906 PyErr_SetImportError(errmsg, pkgname, NULL);
4907 }
4908 else {
4909 errmsg = PyUnicode_FromFormat(
4910 "cannot import name %R from %R (%S)",
4911 name, pkgname_or_unknown, pkgpath
4912 );
4913 /* NULL check for errmsg done by PyErr_SetImportError. */
4914 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004915 }
4916
Xiang Zhang4830f582017-03-21 11:13:42 +08004917 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004918 Py_XDECREF(pkgname_or_unknown);
4919 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07004920 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00004921}
Guido van Rossumac7be682001-01-17 15:42:30 +00004922
Thomas Wouters52152252000-08-17 22:55:00 +00004923static int
4924import_all_from(PyObject *locals, PyObject *v)
4925{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004926 _Py_IDENTIFIER(__all__);
4927 _Py_IDENTIFIER(__dict__);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08004928 _Py_IDENTIFIER(__name__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004929 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004930 int skip_leading_underscores = 0;
4931 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004932
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004933 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
4934 return -1; /* Unexpected error */
4935 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004936 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004937 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
4938 return -1;
4939 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004940 if (dict == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004941 PyErr_SetString(PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004942 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004943 return -1;
4944 }
4945 all = PyMapping_Keys(dict);
4946 Py_DECREF(dict);
4947 if (all == NULL)
4948 return -1;
4949 skip_leading_underscores = 1;
4950 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004952 for (pos = 0, err = 0; ; pos++) {
4953 name = PySequence_GetItem(all, pos);
4954 if (name == NULL) {
4955 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4956 err = -1;
4957 else
4958 PyErr_Clear();
4959 break;
4960 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08004961 if (!PyUnicode_Check(name)) {
4962 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
4963 if (modname == NULL) {
4964 Py_DECREF(name);
4965 err = -1;
4966 break;
4967 }
4968 if (!PyUnicode_Check(modname)) {
4969 PyErr_Format(PyExc_TypeError,
4970 "module __name__ must be a string, not %.100s",
4971 Py_TYPE(modname)->tp_name);
4972 }
4973 else {
4974 PyErr_Format(PyExc_TypeError,
4975 "%s in %U.%s must be str, not %.100s",
4976 skip_leading_underscores ? "Key" : "Item",
4977 modname,
4978 skip_leading_underscores ? "__dict__" : "__all__",
4979 Py_TYPE(name)->tp_name);
4980 }
4981 Py_DECREF(modname);
4982 Py_DECREF(name);
4983 err = -1;
4984 break;
4985 }
4986 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03004987 if (PyUnicode_READY(name) == -1) {
4988 Py_DECREF(name);
4989 err = -1;
4990 break;
4991 }
4992 if (PyUnicode_READ_CHAR(name, 0) == '_') {
4993 Py_DECREF(name);
4994 continue;
4995 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004996 }
4997 value = PyObject_GetAttr(v, name);
4998 if (value == NULL)
4999 err = -1;
5000 else if (PyDict_CheckExact(locals))
5001 err = PyDict_SetItem(locals, name, value);
5002 else
5003 err = PyObject_SetItem(locals, name, value);
5004 Py_DECREF(name);
5005 Py_XDECREF(value);
5006 if (err != 0)
5007 break;
5008 }
5009 Py_DECREF(all);
5010 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005011}
5012
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005013static int
5014check_args_iterable(PyObject *func, PyObject *args)
5015{
5016 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
5017 PyErr_Format(PyExc_TypeError,
5018 "%.200s%.200s argument after * "
5019 "must be an iterable, not %.200s",
5020 PyEval_GetFuncName(func),
5021 PyEval_GetFuncDesc(func),
5022 args->ob_type->tp_name);
5023 return -1;
5024 }
5025 return 0;
5026}
5027
5028static void
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005029format_kwargs_error(PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005030{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005031 /* _PyDict_MergeEx raises attribute
5032 * error (percolated from an attempt
5033 * to get 'keys' attribute) instead of
5034 * a type error if its second argument
5035 * is not a mapping.
5036 */
5037 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
5038 PyErr_Format(PyExc_TypeError,
5039 "%.200s%.200s argument after ** "
5040 "must be a mapping, not %.200s",
5041 PyEval_GetFuncName(func),
5042 PyEval_GetFuncDesc(func),
5043 kwargs->ob_type->tp_name);
5044 }
5045 else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
5046 PyObject *exc, *val, *tb;
5047 PyErr_Fetch(&exc, &val, &tb);
5048 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
5049 PyObject *key = PyTuple_GET_ITEM(val, 0);
5050 if (!PyUnicode_Check(key)) {
5051 PyErr_Format(PyExc_TypeError,
5052 "%.200s%.200s keywords must be strings",
5053 PyEval_GetFuncName(func),
5054 PyEval_GetFuncDesc(func));
5055 } else {
5056 PyErr_Format(PyExc_TypeError,
5057 "%.200s%.200s got multiple "
5058 "values for keyword argument '%U'",
5059 PyEval_GetFuncName(func),
5060 PyEval_GetFuncDesc(func),
5061 key);
5062 }
5063 Py_XDECREF(exc);
5064 Py_XDECREF(val);
5065 Py_XDECREF(tb);
5066 }
5067 else {
5068 PyErr_Restore(exc, val, tb);
5069 }
5070 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005071}
5072
Guido van Rossumac7be682001-01-17 15:42:30 +00005073static void
Neal Norwitzda059e32007-08-26 05:33:45 +00005074format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005075{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005076 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005077
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005078 if (!obj)
5079 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005080
Serhiy Storchaka06515832016-11-20 09:13:07 +02005081 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005082 if (!obj_str)
5083 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005085 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005086}
Guido van Rossum950361c1997-01-24 13:49:28 +00005087
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005088static void
5089format_exc_unbound(PyCodeObject *co, int oparg)
5090{
5091 PyObject *name;
5092 /* Don't stomp existing exception */
5093 if (PyErr_Occurred())
5094 return;
5095 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5096 name = PyTuple_GET_ITEM(co->co_cellvars,
5097 oparg);
5098 format_exc_check_arg(
5099 PyExc_UnboundLocalError,
5100 UNBOUNDLOCAL_ERROR_MSG,
5101 name);
5102 } else {
5103 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5104 PyTuple_GET_SIZE(co->co_cellvars));
5105 format_exc_check_arg(PyExc_NameError,
5106 UNBOUNDFREE_ERROR_MSG, name);
5107 }
5108}
5109
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005110static void
5111format_awaitable_error(PyTypeObject *type, int prevopcode)
5112{
5113 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5114 if (prevopcode == BEFORE_ASYNC_WITH) {
5115 PyErr_Format(PyExc_TypeError,
5116 "'async with' received an object from __aenter__ "
5117 "that does not implement __await__: %.100s",
5118 type->tp_name);
5119 }
5120 else if (prevopcode == WITH_CLEANUP_START) {
5121 PyErr_Format(PyExc_TypeError,
5122 "'async with' received an object from __aexit__ "
5123 "that does not implement __await__: %.100s",
5124 type->tp_name);
5125 }
5126 }
5127}
5128
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005129static PyObject *
5130unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005131 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005132{
5133 PyObject *res;
5134 if (Py_REFCNT(v) == 2) {
5135 /* In the common case, there are 2 references to the value
5136 * stored in 'variable' when the += is performed: one on the
5137 * value stack (in 'v') and one still stored in the
5138 * 'variable'. We try to delete the variable now to reduce
5139 * the refcnt to 1.
5140 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005141 int opcode, oparg;
5142 NEXTOPARG();
5143 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005144 case STORE_FAST:
5145 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005146 PyObject **fastlocals = f->f_localsplus;
5147 if (GETLOCAL(oparg) == v)
5148 SETLOCAL(oparg, NULL);
5149 break;
5150 }
5151 case STORE_DEREF:
5152 {
5153 PyObject **freevars = (f->f_localsplus +
5154 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005155 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005156 if (PyCell_GET(c) == v) {
5157 PyCell_SET(c, NULL);
5158 Py_DECREF(v);
5159 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005160 break;
5161 }
5162 case STORE_NAME:
5163 {
5164 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005165 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005166 PyObject *locals = f->f_locals;
Serhiy Storchaka8905fcc2018-12-11 08:38:03 +02005167 if (locals && PyDict_CheckExact(locals) &&
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005168 PyDict_GetItem(locals, name) == v) {
5169 if (PyDict_DelItem(locals, name) != 0) {
5170 PyErr_Clear();
5171 }
5172 }
5173 break;
5174 }
5175 }
5176 }
5177 res = v;
5178 PyUnicode_Append(&res, w);
5179 return res;
5180}
5181
Guido van Rossum950361c1997-01-24 13:49:28 +00005182#ifdef DYNAMIC_EXECUTION_PROFILE
5183
Skip Montanarof118cb12001-10-15 20:51:38 +00005184static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005185getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005186{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005187 int i;
5188 PyObject *l = PyList_New(256);
5189 if (l == NULL) return NULL;
5190 for (i = 0; i < 256; i++) {
5191 PyObject *x = PyLong_FromLong(a[i]);
5192 if (x == NULL) {
5193 Py_DECREF(l);
5194 return NULL;
5195 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005196 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005197 }
5198 for (i = 0; i < 256; i++)
5199 a[i] = 0;
5200 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005201}
5202
5203PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005204_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005205{
5206#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005207 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005208#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005209 int i;
5210 PyObject *l = PyList_New(257);
5211 if (l == NULL) return NULL;
5212 for (i = 0; i < 257; i++) {
5213 PyObject *x = getarray(dxpairs[i]);
5214 if (x == NULL) {
5215 Py_DECREF(l);
5216 return NULL;
5217 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005218 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005219 }
5220 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005221#endif
5222}
5223
5224#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005225
5226Py_ssize_t
5227_PyEval_RequestCodeExtraIndex(freefunc free)
5228{
Victor Stinnercaba55b2018-08-03 15:33:52 +02005229 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -07005230 Py_ssize_t new_index;
5231
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005232 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005233 return -1;
5234 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005235 new_index = interp->co_extra_user_count++;
5236 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005237 return new_index;
5238}
Łukasz Langaa785c872016-09-09 17:37:37 -07005239
5240static void
5241dtrace_function_entry(PyFrameObject *f)
5242{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005243 const char *filename;
5244 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005245 int lineno;
5246
5247 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5248 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5249 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5250
5251 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5252}
5253
5254static void
5255dtrace_function_return(PyFrameObject *f)
5256{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005257 const char *filename;
5258 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005259 int lineno;
5260
5261 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5262 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5263 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5264
5265 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5266}
5267
5268/* DTrace equivalent of maybe_call_line_trace. */
5269static void
5270maybe_dtrace_line(PyFrameObject *frame,
5271 int *instr_lb, int *instr_ub, int *instr_prev)
5272{
5273 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005274 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005275
5276 /* If the last instruction executed isn't in the current
5277 instruction window, reset the window.
5278 */
5279 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5280 PyAddrPair bounds;
5281 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5282 &bounds);
5283 *instr_lb = bounds.ap_lower;
5284 *instr_ub = bounds.ap_upper;
5285 }
5286 /* If the last instruction falls at the start of a line or if
5287 it represents a jump backwards, update the frame's line
5288 number and call the trace function. */
5289 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5290 frame->f_lineno = line;
5291 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5292 if (!co_filename)
5293 co_filename = "?";
5294 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5295 if (!co_name)
5296 co_name = "?";
5297 PyDTrace_LINE(co_filename, co_name, line);
5298 }
5299 *instr_prev = frame->f_lasti;
5300}