blob: 439f4f156e8b09c094e20dfe0bc2a0eadfc0f0ed [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Thomas Wouters477c8d52006-05-27 19:21:47 +00009/* enable more aggressive intra-module optimizations, where available */
10#define PY_LOCAL_AGGRESSIVE
11
Guido van Rossumb209a111997-04-29 18:18:01 +000012#include "Python.h"
Victor Stinnerbcda8f12018-11-21 22:27:47 +010013#include "pycore_object.h"
Victor Stinner621cebe2018-11-12 16:53:38 +010014#include "pycore_pystate.h"
Victor Stinnerec13b932018-11-25 23:56:17 +010015#include "pycore_tupleobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000016
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000017#include "code.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040018#include "dictobject.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000019#include "frameobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000020#include "opcode.h"
Łukasz Langaa785c872016-09-09 17:37:37 -070021#include "pydtrace.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040022#include "setobject.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000023#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000024
Guido van Rossumc6004111993-11-05 10:22:19 +000025#include <ctype.h>
26
Guido van Rossum408027e1996-12-30 16:17:54 +000027#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000028/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000029#define LLTRACE 1 /* Low-level trace feature */
30#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000031#endif
32
Yury Selivanovf2392132016-12-13 19:03:51 -050033/* Private API for the LOAD_METHOD opcode. */
34extern int _PyObject_GetMethod(PyObject *, PyObject *, PyObject **);
35
Jeremy Hylton52820442001-01-03 23:52:36 +000036typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000037
Guido van Rossum374a9221991-04-04 10:40:29 +000038/* Forward declarations */
Eric Snow2ebc5ce2017-09-07 23:51:28 -060039Py_LOCAL_INLINE(PyObject *) call_function(PyObject ***, Py_ssize_t,
40 PyObject *);
Victor Stinnerf9b760f2016-09-09 10:17:08 -070041static PyObject * do_call_core(PyObject *, PyObject *, PyObject *);
Jeremy Hylton52820442001-01-03 23:52:36 +000042
Guido van Rossum0a066c01992-03-27 17:29:15 +000043#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000044static int lltrace;
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020045static int prtrace(PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000046#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010047static int call_trace(Py_tracefunc, PyObject *,
48 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000050static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010051 PyThreadState *, PyFrameObject *,
52 int, PyObject *);
53static void call_exc_trace(Py_tracefunc, PyObject *,
54 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000055static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Eric Snow2ebc5ce2017-09-07 23:51:28 -060056 PyThreadState *, PyFrameObject *,
57 int *, int *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070058static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *);
59static void dtrace_function_entry(PyFrameObject *);
60static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000061
Thomas Wouters477c8d52006-05-27 19:21:47 +000062static PyObject * cmp_outcome(int, PyObject *, PyObject *);
Eric Snow2ebc5ce2017-09-07 23:51:28 -060063static PyObject * import_name(PyFrameObject *, PyObject *, PyObject *,
64 PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +000065static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +000066static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +000067static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +000068static void format_exc_unbound(PyCodeObject *co, int oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +020069static PyObject * unicode_concatenate(PyObject *, PyObject *,
Serhiy Storchakaab874002016-09-11 13:48:15 +030070 PyFrameObject *, const _Py_CODEUNIT *);
Benjamin Petersonce798522012-01-22 11:24:29 -050071static PyObject * special_lookup(PyObject *, _Py_Identifier *);
Serhiy Storchaka25e4f772017-08-03 11:37:15 +030072static int check_args_iterable(PyObject *func, PyObject *vararg);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +020073static void format_kwargs_error(PyObject *func, PyObject *kwargs);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +030074static void format_awaitable_error(PyTypeObject *, int);
Guido van Rossum374a9221991-04-04 10:40:29 +000075
Paul Prescode68140d2000-08-30 20:25:01 +000076#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000077 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000078#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000080#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 "free variable '%.200s' referenced before assignment" \
82 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000083
Guido van Rossum950361c1997-01-24 13:49:28 +000084/* Dynamic execution profile */
85#ifdef DYNAMIC_EXECUTION_PROFILE
86#ifdef DXPAIRS
87static long dxpairs[257][256];
88#define dxp dxpairs[256]
89#else
90static long dxp[256];
91#endif
92#endif
93
Eric Snow2ebc5ce2017-09-07 23:51:28 -060094#define GIL_REQUEST _Py_atomic_load_relaxed(&_PyRuntime.ceval.gil_drop_request)
Benjamin Petersond2be5b42010-09-10 22:47:02 +000095
Jeffrey Yasskin39370832010-05-03 19:29:34 +000096/* This can set eval_breaker to 0 even though gil_drop_request became
97 1. We believe this is all right because the eval loop will release
98 the GIL eventually anyway. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +000099#define COMPUTE_EVAL_BREAKER() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 _Py_atomic_store_relaxed( \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600101 &_PyRuntime.ceval.eval_breaker, \
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000102 GIL_REQUEST | \
Eric Snowfdf282d2019-01-11 14:26:55 -0700103 _Py_atomic_load_relaxed(&_PyRuntime.ceval.signals_pending) | \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600104 _Py_atomic_load_relaxed(&_PyRuntime.ceval.pending.calls_to_do) | \
105 _PyRuntime.ceval.pending.async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000106
107#define SET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 do { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600109 _Py_atomic_store_relaxed(&_PyRuntime.ceval.gil_drop_request, 1); \
110 _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000112
113#define RESET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 do { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600115 _Py_atomic_store_relaxed(&_PyRuntime.ceval.gil_drop_request, 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 COMPUTE_EVAL_BREAKER(); \
117 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000118
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000119/* Pending calls are only modified under pending_lock */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000120#define SIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 do { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600122 _Py_atomic_store_relaxed(&_PyRuntime.ceval.pending.calls_to_do, 1); \
123 _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000125
126#define UNSIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 do { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600128 _Py_atomic_store_relaxed(&_PyRuntime.ceval.pending.calls_to_do, 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 COMPUTE_EVAL_BREAKER(); \
130 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000131
Eric Snowfdf282d2019-01-11 14:26:55 -0700132#define SIGNAL_PENDING_SIGNALS() \
133 do { \
134 _Py_atomic_store_relaxed(&_PyRuntime.ceval.signals_pending, 1); \
135 _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \
136 } while (0)
137
138#define UNSIGNAL_PENDING_SIGNALS() \
139 do { \
140 _Py_atomic_store_relaxed(&_PyRuntime.ceval.signals_pending, 0); \
141 COMPUTE_EVAL_BREAKER(); \
142 } while (0)
143
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000144#define SIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 do { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600146 _PyRuntime.ceval.pending.async_exc = 1; \
147 _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000149
150#define UNSIGNAL_ASYNC_EXC() \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600151 do { \
152 _PyRuntime.ceval.pending.async_exc = 0; \
153 COMPUTE_EVAL_BREAKER(); \
154 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000155
156
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000157#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000158#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000159#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000160#include "pythread.h"
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000161#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000162
Tim Peters7f468f22004-10-11 02:40:51 +0000163int
164PyEval_ThreadsInitialized(void)
165{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000167}
168
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000169void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000170PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000171{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 if (gil_created())
173 return;
Inada Naoki001fee12019-02-20 10:00:09 +0900174 PyThread_init_thread();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 create_gil();
Victor Stinner50b48572018-11-01 01:51:40 +0100176 take_gil(_PyThreadState_GET());
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600177 _PyRuntime.ceval.pending.main_thread = PyThread_get_thread_ident();
178 if (!_PyRuntime.ceval.pending.lock)
179 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000180}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000181
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000182void
Antoine Pitrou1df15362010-09-13 14:16:46 +0000183_PyEval_FiniThreads(void)
184{
185 if (!gil_created())
186 return;
187 destroy_gil();
188 assert(!gil_created());
189}
190
191void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000192PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000193{
Victor Stinner50b48572018-11-01 01:51:40 +0100194 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 if (tstate == NULL)
196 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
197 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000198}
199
200void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000201PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000202{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100204 We therefore avoid PyThreadState_Get() which dumps a fatal error
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 in debug mode.
206 */
Victor Stinner50b48572018-11-01 01:51:40 +0100207 drop_gil(_PyThreadState_GET());
Guido van Rossum25ce5661997-08-02 03:10:38 +0000208}
209
210void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000211PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000212{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 if (tstate == NULL)
214 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
215 /* Check someone has called PyEval_InitThreads() to create the lock */
216 assert(gil_created());
217 take_gil(tstate);
218 if (PyThreadState_Swap(tstate) != NULL)
219 Py_FatalError(
220 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000221}
222
223void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000224PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000225{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 if (tstate == NULL)
227 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
228 if (PyThreadState_Swap(NULL) != tstate)
229 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
230 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000231}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000232
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200233/* This function is called from PyOS_AfterFork_Child to destroy all threads
234 * which are not running in the child process, and clear internal locks
235 * which might be held by those threads.
236 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000237
238void
239PyEval_ReInitThreads(void)
240{
Victor Stinner50b48572018-11-01 01:51:40 +0100241 PyThreadState *current_tstate = _PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 if (!gil_created())
244 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 recreate_gil();
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600246 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200247 take_gil(current_tstate);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600248 _PyRuntime.ceval.pending.main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000249
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200250 /* Destroy all threads except the current one */
251 _PyThreadState_DeleteExcept(current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000252}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000253
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000254/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600255 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000256
257void
258_PyEval_SignalAsyncExc(void)
259{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000261}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000262
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000263PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000264PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000265{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 PyThreadState *tstate = PyThreadState_Swap(NULL);
267 if (tstate == NULL)
268 Py_FatalError("PyEval_SaveThread: NULL tstate");
Victor Stinner2914bb32018-01-29 11:57:45 +0100269 assert(gil_created());
270 drop_gil(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000272}
273
274void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000275PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000276{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 if (tstate == NULL)
278 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Victor Stinner2914bb32018-01-29 11:57:45 +0100279 assert(gil_created());
280
281 int err = errno;
282 take_gil(tstate);
283 /* _Py_Finalizing is protected by the GIL */
284 if (_Py_IsFinalizing() && !_Py_CURRENTLY_FINALIZING(tstate)) {
285 drop_gil(tstate);
286 PyThread_exit_thread();
287 Py_UNREACHABLE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 }
Victor Stinner2914bb32018-01-29 11:57:45 +0100289 errno = err;
290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000292}
293
294
Guido van Rossuma9672091994-09-14 13:31:22 +0000295/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
296 signal handlers or Mac I/O completion routines) can schedule calls
297 to a function to be called synchronously.
298 The synchronous function is called with one void* argument.
299 It should return 0 for success or -1 for failure -- failure should
300 be accompanied by an exception.
301
302 If registry succeeds, the registry function returns 0; if it fails
303 (e.g. due to too many pending calls) it returns -1 (without setting
304 an exception condition).
305
306 Note that because registry may occur from within signal handlers,
307 or other asynchronous events, calling malloc() is unsafe!
308
Guido van Rossuma9672091994-09-14 13:31:22 +0000309 Any thread can schedule pending calls, but only the main thread
310 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000311 There is no facility to schedule calls to a particular thread, but
312 that should be easy to change, should that ever be required. In
313 that case, the static variables here should go into the python
314 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000315*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000316
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200317void
318_PyEval_SignalReceived(void)
319{
320 /* bpo-30703: Function called when the C signal handler of Python gets a
321 signal. We cannot queue a callback using Py_AddPendingCall() since
322 that function is not async-signal-safe. */
Eric Snowfdf282d2019-01-11 14:26:55 -0700323 SIGNAL_PENDING_SIGNALS();
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200324}
325
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200326/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000327 scheduling to be made from any thread, and even from an executing
328 callback.
329 */
330
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000331int
332Py_AddPendingCall(int (*func)(void *), void *arg)
333{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 int i, j, result=0;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600335 PyThread_type_lock lock = _PyRuntime.ceval.pending.lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000337 /* try a few times for the lock. Since this mechanism is used
338 * for signal handling (on the main thread), there is a (slim)
339 * chance that a signal is delivered on the same thread while we
340 * hold the lock during the Py_MakePendingCalls() function.
341 * This avoids a deadlock in that case.
342 * Note that signals can be delivered on any thread. In particular,
343 * on Windows, a SIGINT is delivered on a system-created worker
344 * thread.
345 * We also check for lock being NULL, in the unlikely case that
346 * this function is called before any bytecode evaluation takes place.
347 */
348 if (lock != NULL) {
349 for (i = 0; i<100; i++) {
350 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
351 break;
352 }
353 if (i == 100)
354 return -1;
355 }
356
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600357 i = _PyRuntime.ceval.pending.last;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 j = (i + 1) % NPENDINGCALLS;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600359 if (j == _PyRuntime.ceval.pending.first) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 result = -1; /* Queue full */
361 } else {
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600362 _PyRuntime.ceval.pending.calls[i].func = func;
363 _PyRuntime.ceval.pending.calls[i].arg = arg;
364 _PyRuntime.ceval.pending.last = j;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 }
366 /* signal main loop */
367 SIGNAL_PENDING_CALLS();
368 if (lock != NULL)
369 PyThread_release_lock(lock);
370 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000371}
372
Eric Snowfdf282d2019-01-11 14:26:55 -0700373static int
374handle_signals(void)
375{
376 /* Only handle signals on main thread. */
377 if (_PyRuntime.ceval.pending.main_thread &&
378 PyThread_get_thread_ident() != _PyRuntime.ceval.pending.main_thread)
379 {
380 return 0;
381 }
Eric Snow64d6cc82019-02-23 15:40:43 -0700382 /*
383 * Ensure that the thread isn't currently running some other
384 * interpreter.
385 */
386 if (_PyInterpreterState_GET_UNSAFE() != _PyRuntime.interpreters.main) {
387 return 0;
388 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700389
390 UNSIGNAL_PENDING_SIGNALS();
Eric Snow64d6cc82019-02-23 15:40:43 -0700391 if (_PyErr_CheckSignals() < 0) {
Eric Snowfdf282d2019-01-11 14:26:55 -0700392 SIGNAL_PENDING_SIGNALS(); /* We're not done yet */
393 return -1;
394 }
395 return 0;
396}
397
398static int
399make_pending_calls(void)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000400{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200401 static int busy = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 /* only service pending calls on main thread */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600404 if (_PyRuntime.ceval.pending.main_thread &&
405 PyThread_get_thread_ident() != _PyRuntime.ceval.pending.main_thread)
406 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 return 0;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600408 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 /* don't perform recursive pending calls */
Eric Snowfdf282d2019-01-11 14:26:55 -0700411 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700413 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200414 busy = 1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200415 /* unsignal before starting to call callbacks, so that any callback
416 added in-between re-signals */
417 UNSIGNAL_PENDING_CALLS();
Eric Snowfdf282d2019-01-11 14:26:55 -0700418 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200419
Eric Snowfdf282d2019-01-11 14:26:55 -0700420 if (!_PyRuntime.ceval.pending.lock) {
421 /* initial allocation of the lock */
422 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
423 if (_PyRuntime.ceval.pending.lock == NULL) {
424 res = -1;
425 goto error;
426 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200427 }
428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 /* perform a bounded number of calls, in case of recursion */
Eric Snowfdf282d2019-01-11 14:26:55 -0700430 for (int i=0; i<NPENDINGCALLS; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 int j;
432 int (*func)(void *);
433 void *arg = NULL;
434
435 /* pop one item off the queue while holding the lock */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600436 PyThread_acquire_lock(_PyRuntime.ceval.pending.lock, WAIT_LOCK);
437 j = _PyRuntime.ceval.pending.first;
438 if (j == _PyRuntime.ceval.pending.last) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 func = NULL; /* Queue empty */
440 } else {
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600441 func = _PyRuntime.ceval.pending.calls[j].func;
442 arg = _PyRuntime.ceval.pending.calls[j].arg;
443 _PyRuntime.ceval.pending.first = (j + 1) % NPENDINGCALLS;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600445 PyThread_release_lock(_PyRuntime.ceval.pending.lock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 /* having released the lock, perform the callback */
447 if (func == NULL)
448 break;
Eric Snowfdf282d2019-01-11 14:26:55 -0700449 res = func(arg);
450 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200451 goto error;
452 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200454
Charles-François Natalif23339a2011-07-23 18:15:43 +0200455 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700456 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200457
458error:
459 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700460 SIGNAL_PENDING_CALLS();
461 return res;
462}
463
464/* Py_MakePendingCalls() is a simple wrapper for the sake
465 of backward-compatibility. */
466int
467Py_MakePendingCalls(void)
468{
469 assert(PyGILState_Check());
470
471 /* Python signal handler doesn't really queue a callback: it only signals
472 that a signal was received, see _PyEval_SignalReceived(). */
473 int res = handle_signals();
474 if (res != 0) {
475 return res;
476 }
477
478 res = make_pending_calls();
479 if (res != 0) {
480 return res;
481 }
482
483 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000484}
485
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000486/* The interpreter's recursion limit */
487
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000488#ifndef Py_DEFAULT_RECURSION_LIMIT
489#define Py_DEFAULT_RECURSION_LIMIT 1000
490#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600491
Eric Snow05351c12017-09-05 21:43:08 -0700492int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000493
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600494void
495_PyEval_Initialize(struct _ceval_runtime_state *state)
496{
497 state->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
498 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
499 _gil_initialize(&state->gil);
500}
501
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000502int
503Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000504{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600505 return _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000506}
507
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000508void
509Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000510{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600511 _PyRuntime.ceval.recursion_limit = new_limit;
512 _Py_CheckRecursionLimit = _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000513}
514
Armin Rigo2b3eb402003-10-28 12:05:48 +0000515/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
516 if the recursion_depth reaches _Py_CheckRecursionLimit.
517 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
518 to guarantee that _Py_CheckRecursiveCall() is regularly called.
519 Without USE_STACKCHECK, there is no need for this. */
520int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300521_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000522{
Victor Stinner50b48572018-11-01 01:51:40 +0100523 PyThreadState *tstate = _PyThreadState_GET();
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600524 int recursion_limit = _PyRuntime.ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000525
526#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700527 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 if (PyOS_CheckStack()) {
529 --tstate->recursion_depth;
530 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
531 return -1;
532 }
pdox18967932017-10-25 23:03:01 -0700533 /* Needed for ABI backwards-compatibility (see bpo-31857) */
Eric Snow05351c12017-09-05 21:43:08 -0700534 _Py_CheckRecursionLimit = recursion_limit;
pdox18967932017-10-25 23:03:01 -0700535#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 if (tstate->recursion_critical)
537 /* Somebody asked that we don't check for recursion. */
538 return 0;
539 if (tstate->overflowed) {
540 if (tstate->recursion_depth > recursion_limit + 50) {
541 /* Overflowing while handling an overflow. Give up. */
542 Py_FatalError("Cannot recover from stack overflow.");
543 }
544 return 0;
545 }
546 if (tstate->recursion_depth > recursion_limit) {
547 --tstate->recursion_depth;
548 tstate->overflowed = 1;
Yury Selivanovf488fb42015-07-03 01:04:23 -0400549 PyErr_Format(PyExc_RecursionError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 "maximum recursion depth exceeded%s",
551 where);
552 return -1;
553 }
554 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000555}
556
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400557static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000558static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000559
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600560#define _Py_TracingPossible _PyRuntime.ceval.tracing_possible
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000561
Guido van Rossum374a9221991-04-04 10:40:29 +0000562
Guido van Rossumb209a111997-04-29 18:18:01 +0000563PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000564PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000565{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 return PyEval_EvalCodeEx(co,
567 globals, locals,
568 (PyObject **)NULL, 0,
569 (PyObject **)NULL, 0,
570 (PyObject **)NULL, 0,
571 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000572}
573
574
575/* Interpreter main loop */
576
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000577PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000578PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 /* This is for backward compatibility with extension modules that
580 used this API; core interpreter code should call
581 PyEval_EvalFrameEx() */
582 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000583}
584
585PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000586PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000587{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200588 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
589 return interp->eval_frame(f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700590}
591
Victor Stinnerc6944e72016-11-11 02:13:35 +0100592PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700593_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
594{
Guido van Rossum950361c1997-01-24 13:49:28 +0000595#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000597#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200598 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300599 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200600 int opcode; /* Current opcode */
601 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200602 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 PyObject *retval = NULL; /* Return value */
Victor Stinner50b48572018-11-01 01:51:40 +0100604 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000608
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000609 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611 is true when the line being executed has changed. The
612 initial values are such as to make this false the first
613 time it is tested. */
614 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000615
Serhiy Storchakaab874002016-09-11 13:48:15 +0300616 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000617 PyObject *names;
618 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000619
Brett Cannon368b4b72012-04-02 12:17:59 -0400620#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200621 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400622#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200623
Antoine Pitroub52ec782009-01-25 16:34:23 +0000624/* Computed GOTOs, or
625 the-optimization-commonly-but-improperly-known-as-"threaded code"
626 using gcc's labels-as-values extension
627 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
628
629 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000631 combined with a lookup table of jump addresses. However, since the
632 indirect jump instruction is shared by all opcodes, the CPU will have a
633 hard time making the right prediction for where to jump next (actually,
634 it will be always wrong except in the uncommon case of a sequence of
635 several identical opcodes).
636
637 "Threaded code" in contrast, uses an explicit jump table and an explicit
638 indirect jump instruction at the end of each opcode. Since the jump
639 instruction is at a different address for each opcode, the CPU will make a
640 separate prediction for each of these instructions, which is equivalent to
641 predicting the second opcode of each opcode pair. These predictions have
642 a much better chance to turn out valid, especially in small bytecode loops.
643
644 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000646 and potentially many more instructions (depending on the pipeline width).
647 A correctly predicted branch, however, is nearly free.
648
649 At the time of this writing, the "threaded code" version is up to 15-20%
650 faster than the normal "switch" version, depending on the compiler and the
651 CPU architecture.
652
653 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
654 because it would render the measurements invalid.
655
656
657 NOTE: care must be taken that the compiler doesn't try to "optimize" the
658 indirect jumps by sharing them between all opcodes. Such optimizations
659 can be disabled on gcc by using the -fno-gcse flag (or possibly
660 -fno-crossjumping).
661*/
662
Antoine Pitrou042b1282010-08-13 21:15:58 +0000663#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000664#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000665#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000666#endif
667
Antoine Pitrou042b1282010-08-13 21:15:58 +0000668#ifdef HAVE_COMPUTED_GOTOS
669 #ifndef USE_COMPUTED_GOTOS
670 #define USE_COMPUTED_GOTOS 1
671 #endif
672#else
673 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
674 #error "Computed gotos are not supported on this compiler."
675 #endif
676 #undef USE_COMPUTED_GOTOS
677 #define USE_COMPUTED_GOTOS 0
678#endif
679
680#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000681/* Import the static jump table */
682#include "opcode_targets.h"
683
Antoine Pitroub52ec782009-01-25 16:34:23 +0000684#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -0700685 op: \
686 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000687
Antoine Pitroub52ec782009-01-25 16:34:23 +0000688#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600690 if (!_Py_atomic_load_relaxed(&_PyRuntime.ceval.eval_breaker)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 FAST_DISPATCH(); \
692 } \
693 continue; \
694 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000695
696#ifdef LLTRACE
697#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700699 if (!lltrace && !_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300701 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300702 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 } \
704 goto fast_next_opcode; \
705 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000706#else
707#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700709 if (!_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300711 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300712 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 } \
714 goto fast_next_opcode; \
715 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000716#endif
717
718#else
Benjamin Petersonddd19492018-09-16 22:38:02 -0700719#define TARGET(op) op
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300720
Antoine Pitroub52ec782009-01-25 16:34:23 +0000721#define DISPATCH() continue
722#define FAST_DISPATCH() goto fast_next_opcode
723#endif
724
725
Neal Norwitza81d2202002-07-14 00:27:26 +0000726/* Tuple access macros */
727
728#ifndef Py_DEBUG
729#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
730#else
731#define GETITEM(v, i) PyTuple_GetItem((v), (i))
732#endif
733
Guido van Rossum374a9221991-04-04 10:40:29 +0000734/* Code access macros */
735
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300736/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600737#define INSTR_OFFSET() \
738 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300739#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300740 _Py_CODEUNIT word = *next_instr; \
741 opcode = _Py_OPCODE(word); \
742 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300743 next_instr++; \
744 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300745#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
746#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000747
Raymond Hettingerf606f872003-03-16 03:11:04 +0000748/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 Some opcodes tend to come in pairs thus making it possible to
750 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300751 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 Verifying the prediction costs a single high-speed test of a register
754 variable against a constant. If the pairing was good, then the
755 processor's own internal branch predication has a high likelihood of
756 success, resulting in a nearly zero-overhead transition to the
757 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300758 including its unpredictable switch-case branch. Combined with the
759 processor's internal branch prediction, a successful PREDICT has the
760 effect of making the two opcodes run as if they were a single new opcode
761 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000762
Georg Brandl86b2fb92008-07-16 03:43:04 +0000763 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 predictions turned-on and interpret the results as if some opcodes
765 had been combined or turn-off predictions so that the opcode frequency
766 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000767
768 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 the CPU to record separate branch prediction information for each
770 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000771
Raymond Hettingerf606f872003-03-16 03:11:04 +0000772*/
773
Antoine Pitrou042b1282010-08-13 21:15:58 +0000774#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000776#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300777#define PREDICT(op) \
778 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300779 _Py_CODEUNIT word = *next_instr; \
780 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300781 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300782 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300783 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300784 goto PRED_##op; \
785 } \
786 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000787#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300788#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000789
Raymond Hettingerf606f872003-03-16 03:11:04 +0000790
Guido van Rossum374a9221991-04-04 10:40:29 +0000791/* Stack manipulation macros */
792
Martin v. Löwis18e16552006-02-15 17:27:45 +0000793/* The stack can grow at most MAXINT deep, as co_nlocals and
794 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000795#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
796#define EMPTY() (STACK_LEVEL() == 0)
797#define TOP() (stack_pointer[-1])
798#define SECOND() (stack_pointer[-2])
799#define THIRD() (stack_pointer[-3])
800#define FOURTH() (stack_pointer[-4])
801#define PEEK(n) (stack_pointer[-(n)])
802#define SET_TOP(v) (stack_pointer[-1] = (v))
803#define SET_SECOND(v) (stack_pointer[-2] = (v))
804#define SET_THIRD(v) (stack_pointer[-3] = (v))
805#define SET_FOURTH(v) (stack_pointer[-4] = (v))
806#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
807#define BASIC_STACKADJ(n) (stack_pointer += n)
808#define BASIC_PUSH(v) (*stack_pointer++ = (v))
809#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000810
Guido van Rossum96a42c81992-01-12 02:29:51 +0000811#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000813 lltrace && prtrace(TOP(), "push")); \
814 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000816 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +0000817#define STACK_GROW(n) do { \
818 assert(n >= 0); \
819 (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000820 lltrace && prtrace(TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +0000821 assert(STACK_LEVEL() <= co->co_stacksize); \
822 } while (0)
823#define STACK_SHRINK(n) do { \
824 assert(n >= 0); \
825 (void)(lltrace && prtrace(TOP(), "stackadj")); \
826 (void)(BASIC_STACKADJ(-n)); \
827 assert(STACK_LEVEL() <= co->co_stacksize); \
828 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +0000829#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +0000830 prtrace((STACK_POINTER)[-1], "ext_pop")), \
831 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000832#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000833#define PUSH(v) BASIC_PUSH(v)
834#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +0000835#define STACK_GROW(n) BASIC_STACKADJ(n)
836#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000837#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000838#endif
839
Guido van Rossum681d79a1995-07-18 14:51:37 +0000840/* Local variable macros */
841
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000843
844/* The SETLOCAL() macro must not DECREF the local variable in-place and
845 then store the new value; it must copy the old value to a temporary
846 value, then store the new value, and then DECREF the temporary value.
847 This is because it is possible that during the DECREF the frame is
848 accessed by other code (e.g. a __del__ method or gc.collect()) and the
849 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000851 GETLOCAL(i) = value; \
852 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000853
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000854
855#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 while (STACK_LEVEL() > (b)->b_level) { \
857 PyObject *v = POP(); \
858 Py_XDECREF(v); \
859 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000860
861#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300862 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +0100864 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 assert(STACK_LEVEL() >= (b)->b_level + 3); \
866 while (STACK_LEVEL() > (b)->b_level + 3) { \
867 value = POP(); \
868 Py_XDECREF(value); \
869 } \
Mark Shannonae3087c2017-10-22 22:41:51 +0100870 exc_info = tstate->exc_info; \
871 type = exc_info->exc_type; \
872 value = exc_info->exc_value; \
873 traceback = exc_info->exc_traceback; \
874 exc_info->exc_type = POP(); \
875 exc_info->exc_value = POP(); \
876 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 Py_XDECREF(type); \
878 Py_XDECREF(value); \
879 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300880 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000881
Guido van Rossuma027efa1997-05-05 20:56:21 +0000882/* Start of code */
883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 /* push frame */
885 if (Py_EnterRecursiveCall(""))
886 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000887
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000889
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 if (tstate->use_tracing) {
891 if (tstate->c_tracefunc != NULL) {
892 /* tstate->c_tracefunc, if defined, is a
893 function that will be called on *every* entry
894 to a code block. Its return value, if not
895 None, is a function that will be called at
896 the start of each executed line of code.
897 (Actually, the function must return itself
898 in order to continue tracing.) The trace
899 functions are called with three arguments:
900 a pointer to the current frame, a string
901 indicating why the function is called, and
902 an argument which depends on the situation.
903 The global trace function is also called
904 whenever an exception is detected. */
905 if (call_trace_protected(tstate->c_tracefunc,
906 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100907 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 /* Trace function raised an error */
909 goto exit_eval_frame;
910 }
911 }
912 if (tstate->c_profilefunc != NULL) {
913 /* Similar for c_profilefunc, except it needn't
914 return itself and isn't called for "line" events */
915 if (call_trace_protected(tstate->c_profilefunc,
916 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100917 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 /* Profile function raised an error */
919 goto exit_eval_frame;
920 }
921 }
922 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000923
Łukasz Langaa785c872016-09-09 17:37:37 -0700924 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
925 dtrace_function_entry(f);
926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 co = f->f_code;
928 names = co->co_names;
929 consts = co->co_consts;
930 fastlocals = f->f_localsplus;
931 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300932 assert(PyBytes_Check(co->co_code));
933 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +0300934 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
935 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
936 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300937 /*
938 f->f_lasti refers to the index of the last instruction,
939 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000940
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300941 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500942 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 When the PREDICT() macros are enabled, some opcode pairs follow in
945 direct succession without updating f->f_lasti. A successful
946 prediction effectively links the two codes together as if they
947 were a single new opcode; accordingly,f->f_lasti will point to
948 the first code in the pair (for instance, GET_ITER followed by
949 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300950 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300952 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300953 next_instr = first_instr;
954 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +0300955 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
956 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300957 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 stack_pointer = f->f_stacktop;
959 assert(stack_pointer != NULL);
960 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +0200961 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000962
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000963
Tim Peters5ca576e2001-06-18 22:08:13 +0000964#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200965 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000966#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000967
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400968 if (throwflag) /* support for generator.throw() */
969 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000970
Victor Stinnerace47d72013-07-18 01:41:08 +0200971#ifdef Py_DEBUG
972 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +0100973 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +0000974 caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +0200975 assert(!PyErr_Occurred());
976#endif
977
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200978main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 assert(stack_pointer >= f->f_valuestack); /* else underflow */
981 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +0200982 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 /* Do periodic things. Doing this every time through
985 the loop would add too much overhead, so we do it
986 only every Nth instruction. We also do it if
987 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
988 event needs attention (e.g. a signal handler or
989 async I/O handler); see Py_AddPendingCall() and
990 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000991
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600992 if (_Py_atomic_load_relaxed(&_PyRuntime.ceval.eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +0300993 opcode = _Py_OPCODE(*next_instr);
994 if (opcode == SETUP_FINALLY ||
995 opcode == SETUP_WITH ||
996 opcode == BEFORE_ASYNC_WITH ||
997 opcode == YIELD_FROM) {
998 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -0700999 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001000 - If we're about to enter the 'with:'. It will prevent
1001 emitting a resource warning in the common idiom
1002 'with open(path) as file:'.
1003 - If we're about to enter the 'async with:'.
1004 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001005 *very* useful, but might help in some cases and it's
1006 traditional)
1007 - If we're resuming a chain of nested 'yield from' or
1008 'await' calls, then each frame is parked with YIELD_FROM
1009 as its next opcode. If the user hit control-C we want to
1010 wait until we've reached the innermost frame before
1011 running the signal handler and raising KeyboardInterrupt
1012 (see bpo-30039).
1013 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 goto fast_next_opcode;
1015 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001016
1017 if (_Py_atomic_load_relaxed(
1018 &_PyRuntime.ceval.signals_pending))
1019 {
1020 if (handle_signals() != 0) {
1021 goto error;
1022 }
1023 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001024 if (_Py_atomic_load_relaxed(
1025 &_PyRuntime.ceval.pending.calls_to_do))
1026 {
Eric Snowfdf282d2019-01-11 14:26:55 -07001027 if (make_pending_calls() != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001028 goto error;
Eric Snowfdf282d2019-01-11 14:26:55 -07001029 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001031
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001032 if (_Py_atomic_load_relaxed(
1033 &_PyRuntime.ceval.gil_drop_request))
1034 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 /* Give another thread a chance */
1036 if (PyThreadState_Swap(NULL) != tstate)
1037 Py_FatalError("ceval: tstate mix-up");
1038 drop_gil(tstate);
1039
1040 /* Other threads may run now */
1041
1042 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001043
1044 /* Check if we should make a quick exit. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001045 if (_Py_IsFinalizing() &&
1046 !_Py_CURRENTLY_FINALIZING(tstate))
1047 {
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001048 drop_gil(tstate);
1049 PyThread_exit_thread();
1050 }
1051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 if (PyThreadState_Swap(tstate) != NULL)
1053 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 }
1055 /* Check for asynchronous exceptions. */
1056 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001057 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 tstate->async_exc = NULL;
1059 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001060 PyErr_SetNone(exc);
1061 Py_DECREF(exc);
1062 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 }
1064 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 fast_next_opcode:
1067 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001068
Łukasz Langaa785c872016-09-09 17:37:37 -07001069 if (PyDTrace_LINE_ENABLED())
1070 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1071
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001075 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001076 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 /* see maybe_call_line_trace
1078 for expository comments */
1079 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001080
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081 err = maybe_call_line_trace(tstate->c_tracefunc,
1082 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001083 tstate, f,
1084 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 /* Reload possibly changed frame fields */
1086 JUMPTO(f->f_lasti);
1087 if (f->f_stacktop != NULL) {
1088 stack_pointer = f->f_stacktop;
1089 f->f_stacktop = NULL;
1090 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001091 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001093 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001095
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001097
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001098 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001099 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001100#ifdef DYNAMIC_EXECUTION_PROFILE
1101#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001102 dxpairs[lastopcode][opcode]++;
1103 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001104#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001106#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001107
Guido van Rossum96a42c81992-01-12 02:29:51 +00001108#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001110
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 if (lltrace) {
1112 if (HAS_ARG(opcode)) {
1113 printf("%d: %d, %d\n",
1114 f->f_lasti, opcode, oparg);
1115 }
1116 else {
1117 printf("%d: %d\n",
1118 f->f_lasti, opcode);
1119 }
1120 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001121#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001122
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001124
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001126 It is essential that any operation that fails must goto error
1127 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001128
Benjamin Petersonddd19492018-09-16 22:38:02 -07001129 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001131 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001132
Benjamin Petersonddd19492018-09-16 22:38:02 -07001133 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001134 PyObject *value = GETLOCAL(oparg);
1135 if (value == NULL) {
1136 format_exc_check_arg(PyExc_UnboundLocalError,
1137 UNBOUNDLOCAL_ERROR_MSG,
1138 PyTuple_GetItem(co->co_varnames, oparg));
1139 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001141 Py_INCREF(value);
1142 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001144 }
1145
Benjamin Petersonddd19492018-09-16 22:38:02 -07001146 case TARGET(LOAD_CONST): {
1147 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001148 PyObject *value = GETITEM(consts, oparg);
1149 Py_INCREF(value);
1150 PUSH(value);
1151 FAST_DISPATCH();
1152 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001153
Benjamin Petersonddd19492018-09-16 22:38:02 -07001154 case TARGET(STORE_FAST): {
1155 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001156 PyObject *value = POP();
1157 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001159 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001160
Benjamin Petersonddd19492018-09-16 22:38:02 -07001161 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001162 PyObject *value = POP();
1163 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001165 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001166
Benjamin Petersonddd19492018-09-16 22:38:02 -07001167 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001168 PyObject *top = TOP();
1169 PyObject *second = SECOND();
1170 SET_TOP(second);
1171 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001173 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001174
Benjamin Petersonddd19492018-09-16 22:38:02 -07001175 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001176 PyObject *top = TOP();
1177 PyObject *second = SECOND();
1178 PyObject *third = THIRD();
1179 SET_TOP(second);
1180 SET_SECOND(third);
1181 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001183 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001184
Benjamin Petersonddd19492018-09-16 22:38:02 -07001185 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001186 PyObject *top = TOP();
1187 PyObject *second = SECOND();
1188 PyObject *third = THIRD();
1189 PyObject *fourth = FOURTH();
1190 SET_TOP(second);
1191 SET_SECOND(third);
1192 SET_THIRD(fourth);
1193 SET_FOURTH(top);
1194 FAST_DISPATCH();
1195 }
1196
Benjamin Petersonddd19492018-09-16 22:38:02 -07001197 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001198 PyObject *top = TOP();
1199 Py_INCREF(top);
1200 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001202 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001203
Benjamin Petersonddd19492018-09-16 22:38:02 -07001204 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001205 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001206 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001207 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001208 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001209 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001210 SET_TOP(top);
1211 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001212 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001213 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001214
Benjamin Petersonddd19492018-09-16 22:38:02 -07001215 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001216 PyObject *value = TOP();
1217 PyObject *res = PyNumber_Positive(value);
1218 Py_DECREF(value);
1219 SET_TOP(res);
1220 if (res == NULL)
1221 goto error;
1222 DISPATCH();
1223 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001224
Benjamin Petersonddd19492018-09-16 22:38:02 -07001225 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001226 PyObject *value = TOP();
1227 PyObject *res = PyNumber_Negative(value);
1228 Py_DECREF(value);
1229 SET_TOP(res);
1230 if (res == NULL)
1231 goto error;
1232 DISPATCH();
1233 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001234
Benjamin Petersonddd19492018-09-16 22:38:02 -07001235 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001236 PyObject *value = TOP();
1237 int err = PyObject_IsTrue(value);
1238 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 if (err == 0) {
1240 Py_INCREF(Py_True);
1241 SET_TOP(Py_True);
1242 DISPATCH();
1243 }
1244 else if (err > 0) {
1245 Py_INCREF(Py_False);
1246 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 DISPATCH();
1248 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001249 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001250 goto error;
1251 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001252
Benjamin Petersonddd19492018-09-16 22:38:02 -07001253 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001254 PyObject *value = TOP();
1255 PyObject *res = PyNumber_Invert(value);
1256 Py_DECREF(value);
1257 SET_TOP(res);
1258 if (res == NULL)
1259 goto error;
1260 DISPATCH();
1261 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001262
Benjamin Petersonddd19492018-09-16 22:38:02 -07001263 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001264 PyObject *exp = POP();
1265 PyObject *base = TOP();
1266 PyObject *res = PyNumber_Power(base, exp, Py_None);
1267 Py_DECREF(base);
1268 Py_DECREF(exp);
1269 SET_TOP(res);
1270 if (res == NULL)
1271 goto error;
1272 DISPATCH();
1273 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001274
Benjamin Petersonddd19492018-09-16 22:38:02 -07001275 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001276 PyObject *right = POP();
1277 PyObject *left = TOP();
1278 PyObject *res = PyNumber_Multiply(left, right);
1279 Py_DECREF(left);
1280 Py_DECREF(right);
1281 SET_TOP(res);
1282 if (res == NULL)
1283 goto error;
1284 DISPATCH();
1285 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001286
Benjamin Petersonddd19492018-09-16 22:38:02 -07001287 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001288 PyObject *right = POP();
1289 PyObject *left = TOP();
1290 PyObject *res = PyNumber_MatrixMultiply(left, right);
1291 Py_DECREF(left);
1292 Py_DECREF(right);
1293 SET_TOP(res);
1294 if (res == NULL)
1295 goto error;
1296 DISPATCH();
1297 }
1298
Benjamin Petersonddd19492018-09-16 22:38:02 -07001299 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001300 PyObject *divisor = POP();
1301 PyObject *dividend = TOP();
1302 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1303 Py_DECREF(dividend);
1304 Py_DECREF(divisor);
1305 SET_TOP(quotient);
1306 if (quotient == NULL)
1307 goto error;
1308 DISPATCH();
1309 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001310
Benjamin Petersonddd19492018-09-16 22:38:02 -07001311 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001312 PyObject *divisor = POP();
1313 PyObject *dividend = TOP();
1314 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1315 Py_DECREF(dividend);
1316 Py_DECREF(divisor);
1317 SET_TOP(quotient);
1318 if (quotient == NULL)
1319 goto error;
1320 DISPATCH();
1321 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001322
Benjamin Petersonddd19492018-09-16 22:38:02 -07001323 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001324 PyObject *divisor = POP();
1325 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001326 PyObject *res;
1327 if (PyUnicode_CheckExact(dividend) && (
1328 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1329 // fast path; string formatting, but not if the RHS is a str subclass
1330 // (see issue28598)
1331 res = PyUnicode_Format(dividend, divisor);
1332 } else {
1333 res = PyNumber_Remainder(dividend, divisor);
1334 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001335 Py_DECREF(divisor);
1336 Py_DECREF(dividend);
1337 SET_TOP(res);
1338 if (res == NULL)
1339 goto error;
1340 DISPATCH();
1341 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001342
Benjamin Petersonddd19492018-09-16 22:38:02 -07001343 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001344 PyObject *right = POP();
1345 PyObject *left = TOP();
1346 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001347 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1348 CPython using bytecode, it is simply worthless.
1349 See http://bugs.python.org/issue21955 and
1350 http://bugs.python.org/issue10044 for the discussion. In short,
1351 no patch shown any impact on a realistic benchmark, only a minor
1352 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001353 if (PyUnicode_CheckExact(left) &&
1354 PyUnicode_CheckExact(right)) {
1355 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001356 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001357 }
1358 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001359 sum = PyNumber_Add(left, right);
1360 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001361 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001362 Py_DECREF(right);
1363 SET_TOP(sum);
1364 if (sum == NULL)
1365 goto error;
1366 DISPATCH();
1367 }
1368
Benjamin Petersonddd19492018-09-16 22:38:02 -07001369 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001370 PyObject *right = POP();
1371 PyObject *left = TOP();
1372 PyObject *diff = PyNumber_Subtract(left, right);
1373 Py_DECREF(right);
1374 Py_DECREF(left);
1375 SET_TOP(diff);
1376 if (diff == NULL)
1377 goto error;
1378 DISPATCH();
1379 }
1380
Benjamin Petersonddd19492018-09-16 22:38:02 -07001381 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001382 PyObject *sub = POP();
1383 PyObject *container = TOP();
1384 PyObject *res = PyObject_GetItem(container, sub);
1385 Py_DECREF(container);
1386 Py_DECREF(sub);
1387 SET_TOP(res);
1388 if (res == NULL)
1389 goto error;
1390 DISPATCH();
1391 }
1392
Benjamin Petersonddd19492018-09-16 22:38:02 -07001393 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001394 PyObject *right = POP();
1395 PyObject *left = TOP();
1396 PyObject *res = PyNumber_Lshift(left, right);
1397 Py_DECREF(left);
1398 Py_DECREF(right);
1399 SET_TOP(res);
1400 if (res == NULL)
1401 goto error;
1402 DISPATCH();
1403 }
1404
Benjamin Petersonddd19492018-09-16 22:38:02 -07001405 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001406 PyObject *right = POP();
1407 PyObject *left = TOP();
1408 PyObject *res = PyNumber_Rshift(left, right);
1409 Py_DECREF(left);
1410 Py_DECREF(right);
1411 SET_TOP(res);
1412 if (res == NULL)
1413 goto error;
1414 DISPATCH();
1415 }
1416
Benjamin Petersonddd19492018-09-16 22:38:02 -07001417 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001418 PyObject *right = POP();
1419 PyObject *left = TOP();
1420 PyObject *res = PyNumber_And(left, right);
1421 Py_DECREF(left);
1422 Py_DECREF(right);
1423 SET_TOP(res);
1424 if (res == NULL)
1425 goto error;
1426 DISPATCH();
1427 }
1428
Benjamin Petersonddd19492018-09-16 22:38:02 -07001429 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001430 PyObject *right = POP();
1431 PyObject *left = TOP();
1432 PyObject *res = PyNumber_Xor(left, right);
1433 Py_DECREF(left);
1434 Py_DECREF(right);
1435 SET_TOP(res);
1436 if (res == NULL)
1437 goto error;
1438 DISPATCH();
1439 }
1440
Benjamin Petersonddd19492018-09-16 22:38:02 -07001441 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001442 PyObject *right = POP();
1443 PyObject *left = TOP();
1444 PyObject *res = PyNumber_Or(left, right);
1445 Py_DECREF(left);
1446 Py_DECREF(right);
1447 SET_TOP(res);
1448 if (res == NULL)
1449 goto error;
1450 DISPATCH();
1451 }
1452
Benjamin Petersonddd19492018-09-16 22:38:02 -07001453 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001454 PyObject *v = POP();
1455 PyObject *list = PEEK(oparg);
1456 int err;
1457 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001458 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001459 if (err != 0)
1460 goto error;
1461 PREDICT(JUMP_ABSOLUTE);
1462 DISPATCH();
1463 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001464
Benjamin Petersonddd19492018-09-16 22:38:02 -07001465 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001466 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001467 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001468 int err;
1469 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001470 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001471 if (err != 0)
1472 goto error;
1473 PREDICT(JUMP_ABSOLUTE);
1474 DISPATCH();
1475 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001476
Benjamin Petersonddd19492018-09-16 22:38:02 -07001477 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001478 PyObject *exp = POP();
1479 PyObject *base = TOP();
1480 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1481 Py_DECREF(base);
1482 Py_DECREF(exp);
1483 SET_TOP(res);
1484 if (res == NULL)
1485 goto error;
1486 DISPATCH();
1487 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001488
Benjamin Petersonddd19492018-09-16 22:38:02 -07001489 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001490 PyObject *right = POP();
1491 PyObject *left = TOP();
1492 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1493 Py_DECREF(left);
1494 Py_DECREF(right);
1495 SET_TOP(res);
1496 if (res == NULL)
1497 goto error;
1498 DISPATCH();
1499 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001500
Benjamin Petersonddd19492018-09-16 22:38:02 -07001501 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001502 PyObject *right = POP();
1503 PyObject *left = TOP();
1504 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1505 Py_DECREF(left);
1506 Py_DECREF(right);
1507 SET_TOP(res);
1508 if (res == NULL)
1509 goto error;
1510 DISPATCH();
1511 }
1512
Benjamin Petersonddd19492018-09-16 22:38:02 -07001513 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001514 PyObject *divisor = POP();
1515 PyObject *dividend = TOP();
1516 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1517 Py_DECREF(dividend);
1518 Py_DECREF(divisor);
1519 SET_TOP(quotient);
1520 if (quotient == NULL)
1521 goto error;
1522 DISPATCH();
1523 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001524
Benjamin Petersonddd19492018-09-16 22:38:02 -07001525 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001526 PyObject *divisor = POP();
1527 PyObject *dividend = TOP();
1528 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1529 Py_DECREF(dividend);
1530 Py_DECREF(divisor);
1531 SET_TOP(quotient);
1532 if (quotient == NULL)
1533 goto error;
1534 DISPATCH();
1535 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001536
Benjamin Petersonddd19492018-09-16 22:38:02 -07001537 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001538 PyObject *right = POP();
1539 PyObject *left = TOP();
1540 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1541 Py_DECREF(left);
1542 Py_DECREF(right);
1543 SET_TOP(mod);
1544 if (mod == NULL)
1545 goto error;
1546 DISPATCH();
1547 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001548
Benjamin Petersonddd19492018-09-16 22:38:02 -07001549 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001550 PyObject *right = POP();
1551 PyObject *left = TOP();
1552 PyObject *sum;
1553 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1554 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001555 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001556 }
1557 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001558 sum = PyNumber_InPlaceAdd(left, right);
1559 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001560 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001561 Py_DECREF(right);
1562 SET_TOP(sum);
1563 if (sum == NULL)
1564 goto error;
1565 DISPATCH();
1566 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001567
Benjamin Petersonddd19492018-09-16 22:38:02 -07001568 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001569 PyObject *right = POP();
1570 PyObject *left = TOP();
1571 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1572 Py_DECREF(left);
1573 Py_DECREF(right);
1574 SET_TOP(diff);
1575 if (diff == NULL)
1576 goto error;
1577 DISPATCH();
1578 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001579
Benjamin Petersonddd19492018-09-16 22:38:02 -07001580 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001581 PyObject *right = POP();
1582 PyObject *left = TOP();
1583 PyObject *res = PyNumber_InPlaceLshift(left, right);
1584 Py_DECREF(left);
1585 Py_DECREF(right);
1586 SET_TOP(res);
1587 if (res == NULL)
1588 goto error;
1589 DISPATCH();
1590 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001591
Benjamin Petersonddd19492018-09-16 22:38:02 -07001592 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001593 PyObject *right = POP();
1594 PyObject *left = TOP();
1595 PyObject *res = PyNumber_InPlaceRshift(left, right);
1596 Py_DECREF(left);
1597 Py_DECREF(right);
1598 SET_TOP(res);
1599 if (res == NULL)
1600 goto error;
1601 DISPATCH();
1602 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001603
Benjamin Petersonddd19492018-09-16 22:38:02 -07001604 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001605 PyObject *right = POP();
1606 PyObject *left = TOP();
1607 PyObject *res = PyNumber_InPlaceAnd(left, right);
1608 Py_DECREF(left);
1609 Py_DECREF(right);
1610 SET_TOP(res);
1611 if (res == NULL)
1612 goto error;
1613 DISPATCH();
1614 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001615
Benjamin Petersonddd19492018-09-16 22:38:02 -07001616 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001617 PyObject *right = POP();
1618 PyObject *left = TOP();
1619 PyObject *res = PyNumber_InPlaceXor(left, right);
1620 Py_DECREF(left);
1621 Py_DECREF(right);
1622 SET_TOP(res);
1623 if (res == NULL)
1624 goto error;
1625 DISPATCH();
1626 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001627
Benjamin Petersonddd19492018-09-16 22:38:02 -07001628 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001629 PyObject *right = POP();
1630 PyObject *left = TOP();
1631 PyObject *res = PyNumber_InPlaceOr(left, right);
1632 Py_DECREF(left);
1633 Py_DECREF(right);
1634 SET_TOP(res);
1635 if (res == NULL)
1636 goto error;
1637 DISPATCH();
1638 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001639
Benjamin Petersonddd19492018-09-16 22:38:02 -07001640 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001641 PyObject *sub = TOP();
1642 PyObject *container = SECOND();
1643 PyObject *v = THIRD();
1644 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001645 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001646 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001647 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001649 Py_DECREF(container);
1650 Py_DECREF(sub);
1651 if (err != 0)
1652 goto error;
1653 DISPATCH();
1654 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001655
Benjamin Petersonddd19492018-09-16 22:38:02 -07001656 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001657 PyObject *sub = TOP();
1658 PyObject *container = SECOND();
1659 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001660 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00001661 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001662 err = PyObject_DelItem(container, sub);
1663 Py_DECREF(container);
1664 Py_DECREF(sub);
1665 if (err != 0)
1666 goto error;
1667 DISPATCH();
1668 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001669
Benjamin Petersonddd19492018-09-16 22:38:02 -07001670 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01001671 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001672 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001673 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001674 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001675 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 PyErr_SetString(PyExc_RuntimeError,
1677 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001678 Py_DECREF(value);
1679 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001681 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001682 Py_DECREF(value);
1683 if (res == NULL)
1684 goto error;
1685 Py_DECREF(res);
1686 DISPATCH();
1687 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001688
Benjamin Petersonddd19492018-09-16 22:38:02 -07001689 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001690 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001691 switch (oparg) {
1692 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001693 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001694 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001695 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001696 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001697 /* fall through */
1698 case 0:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001699 if (do_raise(exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001700 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001701 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702 break;
1703 default:
1704 PyErr_SetString(PyExc_SystemError,
1705 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001706 break;
1707 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001708 goto error;
1709 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001710
Benjamin Petersonddd19492018-09-16 22:38:02 -07001711 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001713 assert(f->f_iblock == 0);
1714 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001715 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001716
Benjamin Petersonddd19492018-09-16 22:38:02 -07001717 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001718 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001719 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001720 PyObject *obj = TOP();
1721 PyTypeObject *type = Py_TYPE(obj);
1722
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001723 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001724 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001725 }
Yury Selivanov75445082015-05-11 22:57:16 -04001726
1727 if (getter != NULL) {
1728 iter = (*getter)(obj);
1729 Py_DECREF(obj);
1730 if (iter == NULL) {
1731 SET_TOP(NULL);
1732 goto error;
1733 }
1734 }
1735 else {
1736 SET_TOP(NULL);
1737 PyErr_Format(
1738 PyExc_TypeError,
1739 "'async for' requires an object with "
1740 "__aiter__ method, got %.100s",
1741 type->tp_name);
1742 Py_DECREF(obj);
1743 goto error;
1744 }
1745
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001746 if (Py_TYPE(iter)->tp_as_async == NULL ||
1747 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001748
Yury Selivanov398ff912017-03-02 22:20:00 -05001749 SET_TOP(NULL);
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001750 PyErr_Format(
1751 PyExc_TypeError,
1752 "'async for' received an object from __aiter__ "
1753 "that does not implement __anext__: %.100s",
1754 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001755 Py_DECREF(iter);
1756 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001757 }
1758
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001759 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001760 DISPATCH();
1761 }
1762
Benjamin Petersonddd19492018-09-16 22:38:02 -07001763 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001764 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001765 PyObject *next_iter = NULL;
1766 PyObject *awaitable = NULL;
1767 PyObject *aiter = TOP();
1768 PyTypeObject *type = Py_TYPE(aiter);
1769
Yury Selivanoveb636452016-09-08 22:01:51 -07001770 if (PyAsyncGen_CheckExact(aiter)) {
1771 awaitable = type->tp_as_async->am_anext(aiter);
1772 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001773 goto error;
1774 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001775 } else {
1776 if (type->tp_as_async != NULL){
1777 getter = type->tp_as_async->am_anext;
1778 }
Yury Selivanov75445082015-05-11 22:57:16 -04001779
Yury Selivanoveb636452016-09-08 22:01:51 -07001780 if (getter != NULL) {
1781 next_iter = (*getter)(aiter);
1782 if (next_iter == NULL) {
1783 goto error;
1784 }
1785 }
1786 else {
1787 PyErr_Format(
1788 PyExc_TypeError,
1789 "'async for' requires an iterator with "
1790 "__anext__ method, got %.100s",
1791 type->tp_name);
1792 goto error;
1793 }
Yury Selivanov75445082015-05-11 22:57:16 -04001794
Yury Selivanoveb636452016-09-08 22:01:51 -07001795 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1796 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001797 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001798 PyExc_TypeError,
1799 "'async for' received an invalid object "
1800 "from __anext__: %.100s",
1801 Py_TYPE(next_iter)->tp_name);
1802
1803 Py_DECREF(next_iter);
1804 goto error;
1805 } else {
1806 Py_DECREF(next_iter);
1807 }
1808 }
Yury Selivanov75445082015-05-11 22:57:16 -04001809
1810 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001811 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001812 DISPATCH();
1813 }
1814
Benjamin Petersonddd19492018-09-16 22:38:02 -07001815 case TARGET(GET_AWAITABLE): {
1816 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04001817 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04001818 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04001819
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03001820 if (iter == NULL) {
1821 format_awaitable_error(Py_TYPE(iterable),
1822 _Py_OPCODE(next_instr[-2]));
1823 }
1824
Yury Selivanov75445082015-05-11 22:57:16 -04001825 Py_DECREF(iterable);
1826
Yury Selivanovc724bae2016-03-02 11:30:46 -05001827 if (iter != NULL && PyCoro_CheckExact(iter)) {
1828 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
1829 if (yf != NULL) {
1830 /* `iter` is a coroutine object that is being
1831 awaited, `yf` is a pointer to the current awaitable
1832 being awaited on. */
1833 Py_DECREF(yf);
1834 Py_CLEAR(iter);
1835 PyErr_SetString(
1836 PyExc_RuntimeError,
1837 "coroutine is being awaited already");
1838 /* The code below jumps to `error` if `iter` is NULL. */
1839 }
1840 }
1841
Yury Selivanov75445082015-05-11 22:57:16 -04001842 SET_TOP(iter); /* Even if it's NULL */
1843
1844 if (iter == NULL) {
1845 goto error;
1846 }
1847
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001848 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001849 DISPATCH();
1850 }
1851
Benjamin Petersonddd19492018-09-16 22:38:02 -07001852 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001853 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001854 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001855 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001856 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
1857 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001858 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001859 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001860 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001861 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001862 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001863 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001864 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001865 Py_DECREF(v);
1866 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001867 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08001868 if (tstate->c_tracefunc != NULL
1869 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001870 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10001871 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001872 if (err < 0)
1873 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001874 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001875 SET_TOP(val);
1876 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001877 }
Martin Panter95f53c12016-07-18 08:23:26 +00001878 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001879 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001880 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01001881 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03001882 f->f_lasti -= sizeof(_Py_CODEUNIT);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001883 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001884 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001885
Benjamin Petersonddd19492018-09-16 22:38:02 -07001886 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001887 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07001888
1889 if (co->co_flags & CO_ASYNC_GENERATOR) {
1890 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
1891 Py_DECREF(retval);
1892 if (w == NULL) {
1893 retval = NULL;
1894 goto error;
1895 }
1896 retval = w;
1897 }
1898
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001899 f->f_stacktop = stack_pointer;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001900 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001901 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001902
Benjamin Petersonddd19492018-09-16 22:38:02 -07001903 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001904 PyObject *type, *value, *traceback;
1905 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001906 PyTryBlock *b = PyFrame_BlockPop(f);
1907 if (b->b_type != EXCEPT_HANDLER) {
1908 PyErr_SetString(PyExc_SystemError,
1909 "popped block is not an except handler");
1910 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001911 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001912 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
1913 STACK_LEVEL() <= (b)->b_level + 4);
1914 exc_info = tstate->exc_info;
1915 type = exc_info->exc_type;
1916 value = exc_info->exc_value;
1917 traceback = exc_info->exc_traceback;
1918 exc_info->exc_type = POP();
1919 exc_info->exc_value = POP();
1920 exc_info->exc_traceback = POP();
1921 Py_XDECREF(type);
1922 Py_XDECREF(value);
1923 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001925 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001926
Benjamin Petersonddd19492018-09-16 22:38:02 -07001927 case TARGET(POP_BLOCK): {
1928 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001929 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001931 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001932
Benjamin Petersonddd19492018-09-16 22:38:02 -07001933 case TARGET(POP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001934 /* If oparg is 0 at the top of the stack are 1 or 6 values:
1935 Either:
1936 - TOP = NULL or an integer
1937 or:
1938 - (TOP, SECOND, THIRD) = exc_info()
1939 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
1940
1941 If oparg is 1 the value for 'return' was additionally pushed
1942 at the top of the stack.
1943 */
1944 PyObject *res = NULL;
1945 if (oparg) {
1946 res = POP();
1947 }
1948 PyObject *exc = POP();
1949 if (exc == NULL || PyLong_CheckExact(exc)) {
1950 Py_XDECREF(exc);
1951 }
1952 else {
1953 Py_DECREF(exc);
1954 Py_DECREF(POP());
1955 Py_DECREF(POP());
1956
1957 PyObject *type, *value, *traceback;
1958 _PyErr_StackItem *exc_info;
1959 PyTryBlock *b = PyFrame_BlockPop(f);
1960 if (b->b_type != EXCEPT_HANDLER) {
1961 PyErr_SetString(PyExc_SystemError,
1962 "popped block is not an except handler");
1963 Py_XDECREF(res);
1964 goto error;
1965 }
1966 assert(STACK_LEVEL() == (b)->b_level + 3);
1967 exc_info = tstate->exc_info;
1968 type = exc_info->exc_type;
1969 value = exc_info->exc_value;
1970 traceback = exc_info->exc_traceback;
1971 exc_info->exc_type = POP();
1972 exc_info->exc_value = POP();
1973 exc_info->exc_traceback = POP();
1974 Py_XDECREF(type);
1975 Py_XDECREF(value);
1976 Py_XDECREF(traceback);
1977 }
1978 if (oparg) {
1979 PUSH(res);
1980 }
1981 DISPATCH();
1982 }
1983
Benjamin Petersonddd19492018-09-16 22:38:02 -07001984 case TARGET(CALL_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001985 PyObject *ret = PyLong_FromLong(INSTR_OFFSET());
1986 if (ret == NULL) {
1987 goto error;
1988 }
1989 PUSH(ret);
1990 JUMPBY(oparg);
1991 FAST_DISPATCH();
1992 }
1993
Benjamin Petersonddd19492018-09-16 22:38:02 -07001994 case TARGET(BEGIN_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001995 /* Push NULL onto the stack for using it in END_FINALLY,
1996 POP_FINALLY, WITH_CLEANUP_START and WITH_CLEANUP_FINISH.
1997 */
1998 PUSH(NULL);
1999 FAST_DISPATCH();
2000 }
2001
Benjamin Petersonddd19492018-09-16 22:38:02 -07002002 case TARGET(END_FINALLY): {
2003 PREDICTED(END_FINALLY);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002004 /* At the top of the stack are 1 or 6 values:
2005 Either:
2006 - TOP = NULL or an integer
2007 or:
2008 - (TOP, SECOND, THIRD) = exc_info()
2009 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2010 */
2011 PyObject *exc = POP();
2012 if (exc == NULL) {
2013 FAST_DISPATCH();
2014 }
2015 else if (PyLong_CheckExact(exc)) {
2016 int ret = _PyLong_AsInt(exc);
2017 Py_DECREF(exc);
2018 if (ret == -1 && PyErr_Occurred()) {
2019 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002020 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002021 JUMPTO(ret);
2022 FAST_DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002023 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002024 else {
2025 assert(PyExceptionClass_Check(exc));
2026 PyObject *val = POP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002027 PyObject *tb = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002028 PyErr_Restore(exc, val, tb);
2029 goto exception_unwind;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002030 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002031 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002032
Benjamin Petersonddd19492018-09-16 22:38:02 -07002033 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002034 PyObject *exc = POP();
2035 assert(PyExceptionClass_Check(exc));
2036 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2037 PyTryBlock *b = PyFrame_BlockPop(f);
2038 assert(b->b_type == EXCEPT_HANDLER);
2039 Py_DECREF(exc);
2040 UNWIND_EXCEPT_HANDLER(b);
2041 Py_DECREF(POP());
2042 JUMPBY(oparg);
2043 FAST_DISPATCH();
2044 }
2045 else {
2046 PyObject *val = POP();
2047 PyObject *tb = POP();
2048 PyErr_Restore(exc, val, tb);
2049 goto exception_unwind;
2050 }
2051 }
2052
Benjamin Petersonddd19492018-09-16 22:38:02 -07002053 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002054 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002055
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002056 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002057 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002058 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
2059 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002060 PyErr_SetString(PyExc_NameError,
2061 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002062 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002063 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002064 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002065 }
2066 else {
2067 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2068 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002069 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002070 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2071 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002072 if (PyErr_ExceptionMatches(PyExc_KeyError))
2073 PyErr_SetString(PyExc_NameError,
2074 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002075 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002076 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002077 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002078 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002079 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002080 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002081
Benjamin Petersonddd19492018-09-16 22:38:02 -07002082 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002083 PyObject *name = GETITEM(names, oparg);
2084 PyObject *v = POP();
2085 PyObject *ns = f->f_locals;
2086 int err;
2087 if (ns == NULL) {
2088 PyErr_Format(PyExc_SystemError,
2089 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002090 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002091 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002092 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002093 if (PyDict_CheckExact(ns))
2094 err = PyDict_SetItem(ns, name, v);
2095 else
2096 err = PyObject_SetItem(ns, name, v);
2097 Py_DECREF(v);
2098 if (err != 0)
2099 goto error;
2100 DISPATCH();
2101 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002102
Benjamin Petersonddd19492018-09-16 22:38:02 -07002103 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002104 PyObject *name = GETITEM(names, oparg);
2105 PyObject *ns = f->f_locals;
2106 int err;
2107 if (ns == NULL) {
2108 PyErr_Format(PyExc_SystemError,
2109 "no locals when deleting %R", name);
2110 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002111 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002112 err = PyObject_DelItem(ns, name);
2113 if (err != 0) {
2114 format_exc_check_arg(PyExc_NameError,
2115 NAME_ERROR_MSG,
2116 name);
2117 goto error;
2118 }
2119 DISPATCH();
2120 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002121
Benjamin Petersonddd19492018-09-16 22:38:02 -07002122 case TARGET(UNPACK_SEQUENCE): {
2123 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002124 PyObject *seq = POP(), *item, **items;
2125 if (PyTuple_CheckExact(seq) &&
2126 PyTuple_GET_SIZE(seq) == oparg) {
2127 items = ((PyTupleObject *)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 (PyList_CheckExact(seq) &&
2134 PyList_GET_SIZE(seq) == oparg) {
2135 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002136 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002137 item = items[oparg];
2138 Py_INCREF(item);
2139 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002140 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002141 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002142 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002143 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002144 } else {
2145 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002146 Py_DECREF(seq);
2147 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002148 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002149 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002150 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002151 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002152
Benjamin Petersonddd19492018-09-16 22:38:02 -07002153 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002154 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2155 PyObject *seq = POP();
2156
2157 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2158 stack_pointer + totalargs)) {
2159 stack_pointer += totalargs;
2160 } else {
2161 Py_DECREF(seq);
2162 goto error;
2163 }
2164 Py_DECREF(seq);
2165 DISPATCH();
2166 }
2167
Benjamin Petersonddd19492018-09-16 22:38:02 -07002168 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002169 PyObject *name = GETITEM(names, oparg);
2170 PyObject *owner = TOP();
2171 PyObject *v = SECOND();
2172 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002173 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002174 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002175 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002176 Py_DECREF(owner);
2177 if (err != 0)
2178 goto error;
2179 DISPATCH();
2180 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002181
Benjamin Petersonddd19492018-09-16 22:38:02 -07002182 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002183 PyObject *name = GETITEM(names, oparg);
2184 PyObject *owner = POP();
2185 int err;
2186 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2187 Py_DECREF(owner);
2188 if (err != 0)
2189 goto error;
2190 DISPATCH();
2191 }
2192
Benjamin Petersonddd19492018-09-16 22:38:02 -07002193 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002194 PyObject *name = GETITEM(names, oparg);
2195 PyObject *v = POP();
2196 int err;
2197 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002198 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002199 if (err != 0)
2200 goto error;
2201 DISPATCH();
2202 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002203
Benjamin Petersonddd19492018-09-16 22:38:02 -07002204 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002205 PyObject *name = GETITEM(names, oparg);
2206 int err;
2207 err = PyDict_DelItem(f->f_globals, name);
2208 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002209 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002210 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002211 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002212 }
2213 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002214 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002215
Benjamin Petersonddd19492018-09-16 22:38:02 -07002216 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002217 PyObject *name = GETITEM(names, oparg);
2218 PyObject *locals = f->f_locals;
2219 PyObject *v;
2220 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002221 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002222 "no locals when loading %R", name);
2223 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002224 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002225 if (PyDict_CheckExact(locals)) {
2226 v = PyDict_GetItem(locals, name);
2227 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002228 }
2229 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002230 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002231 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002232 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2233 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002234 PyErr_Clear();
2235 }
2236 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002237 if (v == NULL) {
2238 v = PyDict_GetItem(f->f_globals, name);
2239 Py_XINCREF(v);
2240 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002241 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002242 v = PyDict_GetItem(f->f_builtins, name);
2243 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002244 format_exc_check_arg(
2245 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002246 NAME_ERROR_MSG, name);
2247 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002248 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002249 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002250 }
2251 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002252 v = PyObject_GetItem(f->f_builtins, name);
2253 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002254 if (PyErr_ExceptionMatches(PyExc_KeyError))
2255 format_exc_check_arg(
2256 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002257 NAME_ERROR_MSG, name);
2258 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002259 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002260 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002261 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002262 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002263 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002264 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002265 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002266
Benjamin Petersonddd19492018-09-16 22:38:02 -07002267 case TARGET(LOAD_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002268 PyObject *name = GETITEM(names, oparg);
2269 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002270 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002271 && PyDict_CheckExact(f->f_builtins))
2272 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002273 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002274 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002275 name);
2276 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002277 if (!_PyErr_OCCURRED()) {
2278 /* _PyDict_LoadGlobal() returns NULL without raising
2279 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002280 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002281 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002282 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002283 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002284 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002285 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002286 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002287 else {
2288 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002289
2290 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002291 v = PyObject_GetItem(f->f_globals, name);
2292 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002293 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2294 goto error;
2295 PyErr_Clear();
2296
Victor Stinnerb4efc962015-11-20 09:24:02 +01002297 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002298 v = PyObject_GetItem(f->f_builtins, name);
2299 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002300 if (PyErr_ExceptionMatches(PyExc_KeyError))
2301 format_exc_check_arg(
2302 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002303 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002304 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002305 }
2306 }
2307 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002308 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002309 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002310 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002311
Benjamin Petersonddd19492018-09-16 22:38:02 -07002312 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002313 PyObject *v = GETLOCAL(oparg);
2314 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002315 SETLOCAL(oparg, NULL);
2316 DISPATCH();
2317 }
2318 format_exc_check_arg(
2319 PyExc_UnboundLocalError,
2320 UNBOUNDLOCAL_ERROR_MSG,
2321 PyTuple_GetItem(co->co_varnames, oparg)
2322 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002323 goto error;
2324 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002325
Benjamin Petersonddd19492018-09-16 22:38:02 -07002326 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002327 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002328 PyObject *oldobj = PyCell_GET(cell);
2329 if (oldobj != NULL) {
2330 PyCell_SET(cell, NULL);
2331 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002332 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002333 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002334 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002335 goto error;
2336 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002337
Benjamin Petersonddd19492018-09-16 22:38:02 -07002338 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002339 PyObject *cell = freevars[oparg];
2340 Py_INCREF(cell);
2341 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002342 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002343 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002344
Benjamin Petersonddd19492018-09-16 22:38:02 -07002345 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002346 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002347 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002348 assert(locals);
2349 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2350 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2351 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2352 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2353 if (PyDict_CheckExact(locals)) {
2354 value = PyDict_GetItem(locals, name);
2355 Py_XINCREF(value);
2356 }
2357 else {
2358 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002359 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002360 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2361 goto error;
2362 PyErr_Clear();
2363 }
2364 }
2365 if (!value) {
2366 PyObject *cell = freevars[oparg];
2367 value = PyCell_GET(cell);
2368 if (value == NULL) {
2369 format_exc_unbound(co, oparg);
2370 goto error;
2371 }
2372 Py_INCREF(value);
2373 }
2374 PUSH(value);
2375 DISPATCH();
2376 }
2377
Benjamin Petersonddd19492018-09-16 22:38:02 -07002378 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002379 PyObject *cell = freevars[oparg];
2380 PyObject *value = PyCell_GET(cell);
2381 if (value == NULL) {
2382 format_exc_unbound(co, oparg);
2383 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002384 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002385 Py_INCREF(value);
2386 PUSH(value);
2387 DISPATCH();
2388 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002389
Benjamin Petersonddd19492018-09-16 22:38:02 -07002390 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002391 PyObject *v = POP();
2392 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002393 PyObject *oldobj = PyCell_GET(cell);
2394 PyCell_SET(cell, v);
2395 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002396 DISPATCH();
2397 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002398
Benjamin Petersonddd19492018-09-16 22:38:02 -07002399 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002400 PyObject *str;
2401 PyObject *empty = PyUnicode_New(0, 0);
2402 if (empty == NULL) {
2403 goto error;
2404 }
2405 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2406 Py_DECREF(empty);
2407 if (str == NULL)
2408 goto error;
2409 while (--oparg >= 0) {
2410 PyObject *item = POP();
2411 Py_DECREF(item);
2412 }
2413 PUSH(str);
2414 DISPATCH();
2415 }
2416
Benjamin Petersonddd19492018-09-16 22:38:02 -07002417 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002418 PyObject *tup = PyTuple_New(oparg);
2419 if (tup == NULL)
2420 goto error;
2421 while (--oparg >= 0) {
2422 PyObject *item = POP();
2423 PyTuple_SET_ITEM(tup, oparg, item);
2424 }
2425 PUSH(tup);
2426 DISPATCH();
2427 }
2428
Benjamin Petersonddd19492018-09-16 22:38:02 -07002429 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002430 PyObject *list = PyList_New(oparg);
2431 if (list == NULL)
2432 goto error;
2433 while (--oparg >= 0) {
2434 PyObject *item = POP();
2435 PyList_SET_ITEM(list, oparg, item);
2436 }
2437 PUSH(list);
2438 DISPATCH();
2439 }
2440
Benjamin Petersonddd19492018-09-16 22:38:02 -07002441 case TARGET(BUILD_TUPLE_UNPACK_WITH_CALL):
2442 case TARGET(BUILD_TUPLE_UNPACK):
2443 case TARGET(BUILD_LIST_UNPACK): {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002444 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002445 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002446 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002447 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002448
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002449 if (sum == NULL)
2450 goto error;
2451
2452 for (i = oparg; i > 0; i--) {
2453 PyObject *none_val;
2454
2455 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2456 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002457 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002458 PyErr_ExceptionMatches(PyExc_TypeError))
2459 {
2460 check_args_iterable(PEEK(1 + oparg), PEEK(i));
Serhiy Storchaka73442852016-10-02 10:33:46 +03002461 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002462 Py_DECREF(sum);
2463 goto error;
2464 }
2465 Py_DECREF(none_val);
2466 }
2467
2468 if (convert_to_tuple) {
2469 return_value = PyList_AsTuple(sum);
2470 Py_DECREF(sum);
2471 if (return_value == NULL)
2472 goto error;
2473 }
2474 else {
2475 return_value = sum;
2476 }
2477
2478 while (oparg--)
2479 Py_DECREF(POP());
2480 PUSH(return_value);
2481 DISPATCH();
2482 }
2483
Benjamin Petersonddd19492018-09-16 22:38:02 -07002484 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002485 PyObject *set = PySet_New(NULL);
2486 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002487 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002488 if (set == NULL)
2489 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002490 for (i = oparg; i > 0; i--) {
2491 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002492 if (err == 0)
2493 err = PySet_Add(set, item);
2494 Py_DECREF(item);
2495 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002496 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002497 if (err != 0) {
2498 Py_DECREF(set);
2499 goto error;
2500 }
2501 PUSH(set);
2502 DISPATCH();
2503 }
2504
Benjamin Petersonddd19492018-09-16 22:38:02 -07002505 case TARGET(BUILD_SET_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002506 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002507 PyObject *sum = PySet_New(NULL);
2508 if (sum == NULL)
2509 goto error;
2510
2511 for (i = oparg; i > 0; i--) {
2512 if (_PySet_Update(sum, PEEK(i)) < 0) {
2513 Py_DECREF(sum);
2514 goto error;
2515 }
2516 }
2517
2518 while (oparg--)
2519 Py_DECREF(POP());
2520 PUSH(sum);
2521 DISPATCH();
2522 }
2523
Benjamin Petersonddd19492018-09-16 22:38:02 -07002524 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002525 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002526 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2527 if (map == NULL)
2528 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002529 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002530 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002531 PyObject *key = PEEK(2*i);
2532 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002533 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002534 if (err != 0) {
2535 Py_DECREF(map);
2536 goto error;
2537 }
2538 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002539
2540 while (oparg--) {
2541 Py_DECREF(POP());
2542 Py_DECREF(POP());
2543 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002544 PUSH(map);
2545 DISPATCH();
2546 }
2547
Benjamin Petersonddd19492018-09-16 22:38:02 -07002548 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002549 _Py_IDENTIFIER(__annotations__);
2550 int err;
2551 PyObject *ann_dict;
2552 if (f->f_locals == NULL) {
2553 PyErr_Format(PyExc_SystemError,
2554 "no locals found when setting up annotations");
2555 goto error;
2556 }
2557 /* check if __annotations__ in locals()... */
2558 if (PyDict_CheckExact(f->f_locals)) {
2559 ann_dict = _PyDict_GetItemId(f->f_locals,
2560 &PyId___annotations__);
2561 if (ann_dict == NULL) {
2562 /* ...if not, create a new one */
2563 ann_dict = PyDict_New();
2564 if (ann_dict == NULL) {
2565 goto error;
2566 }
2567 err = _PyDict_SetItemId(f->f_locals,
2568 &PyId___annotations__, ann_dict);
2569 Py_DECREF(ann_dict);
2570 if (err != 0) {
2571 goto error;
2572 }
2573 }
2574 }
2575 else {
2576 /* do the same if locals() is not a dict */
2577 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2578 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002579 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002580 }
2581 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2582 if (ann_dict == NULL) {
2583 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2584 goto error;
2585 }
2586 PyErr_Clear();
2587 ann_dict = PyDict_New();
2588 if (ann_dict == NULL) {
2589 goto error;
2590 }
2591 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2592 Py_DECREF(ann_dict);
2593 if (err != 0) {
2594 goto error;
2595 }
2596 }
2597 else {
2598 Py_DECREF(ann_dict);
2599 }
2600 }
2601 DISPATCH();
2602 }
2603
Benjamin Petersonddd19492018-09-16 22:38:02 -07002604 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002605 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002606 PyObject *map;
2607 PyObject *keys = TOP();
2608 if (!PyTuple_CheckExact(keys) ||
2609 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2610 PyErr_SetString(PyExc_SystemError,
2611 "bad BUILD_CONST_KEY_MAP keys argument");
2612 goto error;
2613 }
2614 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2615 if (map == NULL) {
2616 goto error;
2617 }
2618 for (i = oparg; i > 0; i--) {
2619 int err;
2620 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2621 PyObject *value = PEEK(i + 1);
2622 err = PyDict_SetItem(map, key, value);
2623 if (err != 0) {
2624 Py_DECREF(map);
2625 goto error;
2626 }
2627 }
2628
2629 Py_DECREF(POP());
2630 while (oparg--) {
2631 Py_DECREF(POP());
2632 }
2633 PUSH(map);
2634 DISPATCH();
2635 }
2636
Benjamin Petersonddd19492018-09-16 22:38:02 -07002637 case TARGET(BUILD_MAP_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002638 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002639 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002640 if (sum == NULL)
2641 goto error;
2642
2643 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002644 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002645 if (PyDict_Update(sum, arg) < 0) {
2646 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2647 PyErr_Format(PyExc_TypeError,
Berker Peksag8e9045d2016-10-02 13:08:25 +03002648 "'%.200s' object is not a mapping",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002649 arg->ob_type->tp_name);
2650 }
2651 Py_DECREF(sum);
2652 goto error;
2653 }
2654 }
2655
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002656 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002657 Py_DECREF(POP());
2658 PUSH(sum);
2659 DISPATCH();
2660 }
2661
Benjamin Petersonddd19492018-09-16 22:38:02 -07002662 case TARGET(BUILD_MAP_UNPACK_WITH_CALL): {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002663 Py_ssize_t i;
2664 PyObject *sum = PyDict_New();
2665 if (sum == NULL)
2666 goto error;
2667
2668 for (i = oparg; i > 0; i--) {
2669 PyObject *arg = PEEK(i);
2670 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002671 Py_DECREF(sum);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02002672 format_kwargs_error(PEEK(2 + oparg), arg);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002673 goto error;
2674 }
2675 }
2676
2677 while (oparg--)
2678 Py_DECREF(POP());
2679 PUSH(sum);
2680 DISPATCH();
2681 }
2682
Benjamin Petersonddd19492018-09-16 22:38:02 -07002683 case TARGET(MAP_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002684 PyObject *key = TOP();
2685 PyObject *value = SECOND();
2686 PyObject *map;
2687 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002688 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002689 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002690 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002691 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002692 Py_DECREF(value);
2693 Py_DECREF(key);
2694 if (err != 0)
2695 goto error;
2696 PREDICT(JUMP_ABSOLUTE);
2697 DISPATCH();
2698 }
2699
Benjamin Petersonddd19492018-09-16 22:38:02 -07002700 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002701 PyObject *name = GETITEM(names, oparg);
2702 PyObject *owner = TOP();
2703 PyObject *res = PyObject_GetAttr(owner, name);
2704 Py_DECREF(owner);
2705 SET_TOP(res);
2706 if (res == NULL)
2707 goto error;
2708 DISPATCH();
2709 }
2710
Benjamin Petersonddd19492018-09-16 22:38:02 -07002711 case TARGET(COMPARE_OP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002712 PyObject *right = POP();
2713 PyObject *left = TOP();
2714 PyObject *res = cmp_outcome(oparg, left, right);
2715 Py_DECREF(left);
2716 Py_DECREF(right);
2717 SET_TOP(res);
2718 if (res == NULL)
2719 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002720 PREDICT(POP_JUMP_IF_FALSE);
2721 PREDICT(POP_JUMP_IF_TRUE);
2722 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002723 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002724
Benjamin Petersonddd19492018-09-16 22:38:02 -07002725 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002726 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002727 PyObject *fromlist = POP();
2728 PyObject *level = TOP();
2729 PyObject *res;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002730 res = import_name(f, name, fromlist, level);
2731 Py_DECREF(level);
2732 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002733 SET_TOP(res);
2734 if (res == NULL)
2735 goto error;
2736 DISPATCH();
2737 }
2738
Benjamin Petersonddd19492018-09-16 22:38:02 -07002739 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002740 PyObject *from = POP(), *locals;
2741 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002742 if (PyFrame_FastToLocalsWithError(f) < 0) {
2743 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002744 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002745 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002746
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002747 locals = f->f_locals;
2748 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002749 PyErr_SetString(PyExc_SystemError,
2750 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002751 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002752 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002753 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002754 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002755 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002756 Py_DECREF(from);
2757 if (err != 0)
2758 goto error;
2759 DISPATCH();
2760 }
Guido van Rossum25831651993-05-19 14:50:45 +00002761
Benjamin Petersonddd19492018-09-16 22:38:02 -07002762 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002763 PyObject *name = GETITEM(names, oparg);
2764 PyObject *from = TOP();
2765 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002766 res = import_from(from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002767 PUSH(res);
2768 if (res == NULL)
2769 goto error;
2770 DISPATCH();
2771 }
Thomas Wouters52152252000-08-17 22:55:00 +00002772
Benjamin Petersonddd19492018-09-16 22:38:02 -07002773 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002774 JUMPBY(oparg);
2775 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002776 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002777
Benjamin Petersonddd19492018-09-16 22:38:02 -07002778 case TARGET(POP_JUMP_IF_FALSE): {
2779 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002780 PyObject *cond = POP();
2781 int err;
2782 if (cond == Py_True) {
2783 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002784 FAST_DISPATCH();
2785 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002786 if (cond == Py_False) {
2787 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002788 JUMPTO(oparg);
2789 FAST_DISPATCH();
2790 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002791 err = PyObject_IsTrue(cond);
2792 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002793 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07002794 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002795 else if (err == 0)
2796 JUMPTO(oparg);
2797 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002798 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002799 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002800 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002801
Benjamin Petersonddd19492018-09-16 22:38:02 -07002802 case TARGET(POP_JUMP_IF_TRUE): {
2803 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002804 PyObject *cond = POP();
2805 int err;
2806 if (cond == Py_False) {
2807 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002808 FAST_DISPATCH();
2809 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002810 if (cond == Py_True) {
2811 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002812 JUMPTO(oparg);
2813 FAST_DISPATCH();
2814 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002815 err = PyObject_IsTrue(cond);
2816 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002817 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002818 JUMPTO(oparg);
2819 }
2820 else if (err == 0)
2821 ;
2822 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002823 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002824 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002825 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002826
Benjamin Petersonddd19492018-09-16 22:38:02 -07002827 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002828 PyObject *cond = TOP();
2829 int err;
2830 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002831 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002832 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002833 FAST_DISPATCH();
2834 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002835 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002836 JUMPTO(oparg);
2837 FAST_DISPATCH();
2838 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002839 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002840 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002841 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002842 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002843 }
2844 else if (err == 0)
2845 JUMPTO(oparg);
2846 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002847 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002848 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002849 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002850
Benjamin Petersonddd19492018-09-16 22:38:02 -07002851 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002852 PyObject *cond = TOP();
2853 int err;
2854 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002855 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002856 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002857 FAST_DISPATCH();
2858 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002859 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002860 JUMPTO(oparg);
2861 FAST_DISPATCH();
2862 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002863 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002864 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002865 JUMPTO(oparg);
2866 }
2867 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002868 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002869 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002870 }
2871 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002872 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002873 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002874 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002875
Benjamin Petersonddd19492018-09-16 22:38:02 -07002876 case TARGET(JUMP_ABSOLUTE): {
2877 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002878 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002879#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002880 /* Enabling this path speeds-up all while and for-loops by bypassing
2881 the per-loop checks for signals. By default, this should be turned-off
2882 because it prevents detection of a control-break in tight loops like
2883 "while 1: pass". Compile with this option turned-on when you need
2884 the speed-up and do not need break checking inside tight loops (ones
2885 that contain only instructions ending with FAST_DISPATCH).
2886 */
2887 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002888#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002889 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002890#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002891 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002892
Benjamin Petersonddd19492018-09-16 22:38:02 -07002893 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002894 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002895 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002896 PyObject *iter = PyObject_GetIter(iterable);
2897 Py_DECREF(iterable);
2898 SET_TOP(iter);
2899 if (iter == NULL)
2900 goto error;
2901 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002902 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04002903 DISPATCH();
2904 }
2905
Benjamin Petersonddd19492018-09-16 22:38:02 -07002906 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04002907 /* before: [obj]; after [getiter(obj)] */
2908 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04002909 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04002910 if (PyCoro_CheckExact(iterable)) {
2911 /* `iterable` is a coroutine */
2912 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
2913 /* and it is used in a 'yield from' expression of a
2914 regular generator. */
2915 Py_DECREF(iterable);
2916 SET_TOP(NULL);
2917 PyErr_SetString(PyExc_TypeError,
2918 "cannot 'yield from' a coroutine object "
2919 "in a non-coroutine generator");
2920 goto error;
2921 }
2922 }
2923 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04002924 /* `iterable` is not a generator. */
2925 iter = PyObject_GetIter(iterable);
2926 Py_DECREF(iterable);
2927 SET_TOP(iter);
2928 if (iter == NULL)
2929 goto error;
2930 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002931 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002932 DISPATCH();
2933 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002934
Benjamin Petersonddd19492018-09-16 22:38:02 -07002935 case TARGET(FOR_ITER): {
2936 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002937 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002938 PyObject *iter = TOP();
2939 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
2940 if (next != NULL) {
2941 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002942 PREDICT(STORE_FAST);
2943 PREDICT(UNPACK_SEQUENCE);
2944 DISPATCH();
2945 }
2946 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002947 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2948 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08002949 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002950 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002951 PyErr_Clear();
2952 }
2953 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00002954 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002955 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002956 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002957 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002958 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002959 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002960
Benjamin Petersonddd19492018-09-16 22:38:02 -07002961 case TARGET(SETUP_FINALLY): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002962 /* NOTE: If you add any new block-setup opcodes that
2963 are not try/except/finally handlers, you may need
2964 to update the PyGen_NeedsFinalizing() function.
2965 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002966
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002967 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002968 STACK_LEVEL());
2969 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002970 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002971
Benjamin Petersonddd19492018-09-16 22:38:02 -07002972 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04002973 _Py_IDENTIFIER(__aexit__);
2974 _Py_IDENTIFIER(__aenter__);
2975
2976 PyObject *mgr = TOP();
2977 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
2978 *enter;
2979 PyObject *res;
2980 if (exit == NULL)
2981 goto error;
2982 SET_TOP(exit);
2983 enter = special_lookup(mgr, &PyId___aenter__);
2984 Py_DECREF(mgr);
2985 if (enter == NULL)
2986 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01002987 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04002988 Py_DECREF(enter);
2989 if (res == NULL)
2990 goto error;
2991 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002992 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002993 DISPATCH();
2994 }
2995
Benjamin Petersonddd19492018-09-16 22:38:02 -07002996 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04002997 PyObject *res = POP();
2998 /* Setup the finally block before pushing the result
2999 of __aenter__ on the stack. */
3000 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3001 STACK_LEVEL());
3002 PUSH(res);
3003 DISPATCH();
3004 }
3005
Benjamin Petersonddd19492018-09-16 22:38:02 -07003006 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003007 _Py_IDENTIFIER(__exit__);
3008 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003009 PyObject *mgr = TOP();
Raymond Hettingera3fec152016-11-21 17:24:23 -08003010 PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003011 PyObject *res;
Raymond Hettingera3fec152016-11-21 17:24:23 -08003012 if (enter == NULL)
3013 goto error;
3014 exit = special_lookup(mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003015 if (exit == NULL) {
3016 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003017 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003018 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003019 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003020 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003021 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003022 Py_DECREF(enter);
3023 if (res == NULL)
3024 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003025 /* Setup the finally block before pushing the result
3026 of __enter__ on the stack. */
3027 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3028 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003029
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003030 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003031 DISPATCH();
3032 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003033
Benjamin Petersonddd19492018-09-16 22:38:02 -07003034 case TARGET(WITH_CLEANUP_START): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003035 /* At the top of the stack are 1 or 6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003036 how/why we entered the finally clause:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003037 - TOP = NULL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003038 - (TOP, SECOND, THIRD) = exc_info()
3039 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003040 Below them is EXIT, the context.__exit__ or context.__aexit__
3041 bound method.
3042 In the first case, we must call
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003043 EXIT(None, None, None)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003044 otherwise we must call
3045 EXIT(TOP, SECOND, THIRD)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003046
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003047 In the first case, we remove EXIT from the
3048 stack, leaving TOP, and push TOP on the stack.
3049 Otherwise we shift the bottom 3 values of the
3050 stack down, replace the empty spot with NULL, and push
3051 None on the stack.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003052
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003053 Finally we push the result of the call.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003054 */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003055 PyObject *stack[3];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003056 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003057 PyObject *exc, *val, *tb, *res;
3058
3059 val = tb = Py_None;
3060 exc = TOP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003061 if (exc == NULL) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003062 STACK_SHRINK(1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003063 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003064 SET_TOP(exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003065 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003066 }
3067 else {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003068 assert(PyExceptionClass_Check(exc));
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003069 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003070 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003071 val = SECOND();
3072 tb = THIRD();
3073 tp2 = FOURTH();
3074 exc2 = PEEK(5);
3075 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003076 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003077 SET_VALUE(7, tb2);
3078 SET_VALUE(6, exc2);
3079 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003080 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3081 SET_FOURTH(NULL);
3082 /* We just shifted the stack down, so we have
3083 to tell the except handler block that the
3084 values are lower than it expects. */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003085 assert(f->f_iblock > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003086 block = &f->f_blockstack[f->f_iblock - 1];
3087 assert(block->b_type == EXCEPT_HANDLER);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003088 assert(block->b_level > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003089 block->b_level--;
3090 }
Victor Stinner842cfff2016-12-01 14:45:31 +01003091
3092 stack[0] = exc;
3093 stack[1] = val;
3094 stack[2] = tb;
3095 res = _PyObject_FastCall(exit_func, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003096 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003097 if (res == NULL)
3098 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003099
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003100 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003101 PUSH(exc);
3102 PUSH(res);
3103 PREDICT(WITH_CLEANUP_FINISH);
3104 DISPATCH();
3105 }
3106
Benjamin Petersonddd19492018-09-16 22:38:02 -07003107 case TARGET(WITH_CLEANUP_FINISH): {
3108 PREDICTED(WITH_CLEANUP_FINISH);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003109 /* TOP = the result of calling the context.__exit__ bound method
3110 SECOND = either None or exception type
3111
3112 If SECOND is None below is NULL or the return address,
3113 otherwise below are 7 values representing an exception.
3114 */
Yury Selivanov75445082015-05-11 22:57:16 -04003115 PyObject *res = POP();
3116 PyObject *exc = POP();
3117 int err;
3118
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003119 if (exc != Py_None)
3120 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003121 else
3122 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003123
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003124 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003125 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003127 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003128 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003129 else if (err > 0) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003130 /* There was an exception and a True return.
3131 * We must manually unwind the EXCEPT_HANDLER block
3132 * which was created when the exception was caught,
Quan Tian3bd0d622018-10-20 05:30:03 +08003133 * otherwise the stack will be in an inconsistent state.
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003134 */
3135 PyTryBlock *b = PyFrame_BlockPop(f);
3136 assert(b->b_type == EXCEPT_HANDLER);
3137 UNWIND_EXCEPT_HANDLER(b);
3138 PUSH(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003139 }
3140 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003141 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003142 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003143
Benjamin Petersonddd19492018-09-16 22:38:02 -07003144 case TARGET(LOAD_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003145 /* Designed to work in tamdem with CALL_METHOD. */
3146 PyObject *name = GETITEM(names, oparg);
3147 PyObject *obj = TOP();
3148 PyObject *meth = NULL;
3149
3150 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3151
Yury Selivanovf2392132016-12-13 19:03:51 -05003152 if (meth == NULL) {
3153 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003154 goto error;
3155 }
3156
3157 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003158 /* We can bypass temporary bound method object.
3159 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003160
INADA Naoki015bce62017-01-16 17:23:30 +09003161 meth | self | arg1 | ... | argN
3162 */
3163 SET_TOP(meth);
3164 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003165 }
3166 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003167 /* meth is not an unbound method (but a regular attr, or
3168 something was returned by a descriptor protocol). Set
3169 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003170 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003171
3172 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003173 */
INADA Naoki015bce62017-01-16 17:23:30 +09003174 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003175 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003176 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003177 }
3178 DISPATCH();
3179 }
3180
Benjamin Petersonddd19492018-09-16 22:38:02 -07003181 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003182 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003183 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003184
3185 sp = stack_pointer;
3186
INADA Naoki015bce62017-01-16 17:23:30 +09003187 meth = PEEK(oparg + 2);
3188 if (meth == NULL) {
3189 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3190 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003191
3192 Stack layout:
3193
INADA Naoki015bce62017-01-16 17:23:30 +09003194 ... | NULL | callable | arg1 | ... | argN
3195 ^- TOP()
3196 ^- (-oparg)
3197 ^- (-oparg-1)
3198 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003199
Ville Skyttä49b27342017-08-03 09:00:59 +03003200 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003201 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003202 */
Yury Selivanovf2392132016-12-13 19:03:51 -05003203 res = call_function(&sp, oparg, NULL);
3204 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003205 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003206 }
3207 else {
3208 /* This is a method call. Stack layout:
3209
INADA Naoki015bce62017-01-16 17:23:30 +09003210 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003211 ^- TOP()
3212 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003213 ^- (-oparg-1)
3214 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003215
INADA Naoki015bce62017-01-16 17:23:30 +09003216 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003217 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003218 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003219 */
3220 res = call_function(&sp, oparg + 1, NULL);
3221 stack_pointer = sp;
3222 }
3223
3224 PUSH(res);
3225 if (res == NULL)
3226 goto error;
3227 DISPATCH();
3228 }
3229
Benjamin Petersonddd19492018-09-16 22:38:02 -07003230 case TARGET(CALL_FUNCTION): {
3231 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003232 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003233 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003234 res = call_function(&sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003235 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003236 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003237 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003238 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003239 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003240 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003241 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003242
Benjamin Petersonddd19492018-09-16 22:38:02 -07003243 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003244 PyObject **sp, *res, *names;
3245
3246 names = POP();
3247 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003248 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003249 res = call_function(&sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003250 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003251 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003252 Py_DECREF(names);
3253
3254 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003255 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003256 }
3257 DISPATCH();
3258 }
3259
Benjamin Petersonddd19492018-09-16 22:38:02 -07003260 case TARGET(CALL_FUNCTION_EX): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003261 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003262 if (oparg & 0x01) {
3263 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003264 if (!PyDict_CheckExact(kwargs)) {
3265 PyObject *d = PyDict_New();
3266 if (d == NULL)
3267 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003268 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003269 Py_DECREF(d);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003270 format_kwargs_error(SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003271 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003272 goto error;
3273 }
3274 Py_DECREF(kwargs);
3275 kwargs = d;
3276 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003277 assert(PyDict_CheckExact(kwargs));
3278 }
3279 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003280 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003281 if (!PyTuple_CheckExact(callargs)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003282 if (check_args_iterable(func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003283 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003284 goto error;
3285 }
3286 Py_SETREF(callargs, PySequence_Tuple(callargs));
3287 if (callargs == NULL) {
3288 goto error;
3289 }
3290 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003291 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003292
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003293 result = do_call_core(func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003294 Py_DECREF(func);
3295 Py_DECREF(callargs);
3296 Py_XDECREF(kwargs);
3297
3298 SET_TOP(result);
3299 if (result == NULL) {
3300 goto error;
3301 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003302 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003303 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003304
Benjamin Petersonddd19492018-09-16 22:38:02 -07003305 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003306 PyObject *qualname = POP();
3307 PyObject *codeobj = POP();
3308 PyFunctionObject *func = (PyFunctionObject *)
3309 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003310
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003311 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003312 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003313 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003314 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003315 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003316
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003317 if (oparg & 0x08) {
3318 assert(PyTuple_CheckExact(TOP()));
3319 func ->func_closure = POP();
3320 }
3321 if (oparg & 0x04) {
3322 assert(PyDict_CheckExact(TOP()));
3323 func->func_annotations = POP();
3324 }
3325 if (oparg & 0x02) {
3326 assert(PyDict_CheckExact(TOP()));
3327 func->func_kwdefaults = POP();
3328 }
3329 if (oparg & 0x01) {
3330 assert(PyTuple_CheckExact(TOP()));
3331 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003332 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003333
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003334 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003335 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003336 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003337
Benjamin Petersonddd19492018-09-16 22:38:02 -07003338 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003339 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003340 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003341 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003342 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003343 step = NULL;
3344 stop = POP();
3345 start = TOP();
3346 slice = PySlice_New(start, stop, step);
3347 Py_DECREF(start);
3348 Py_DECREF(stop);
3349 Py_XDECREF(step);
3350 SET_TOP(slice);
3351 if (slice == NULL)
3352 goto error;
3353 DISPATCH();
3354 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003355
Benjamin Petersonddd19492018-09-16 22:38:02 -07003356 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003357 /* Handles f-string value formatting. */
3358 PyObject *result;
3359 PyObject *fmt_spec;
3360 PyObject *value;
3361 PyObject *(*conv_fn)(PyObject *);
3362 int which_conversion = oparg & FVC_MASK;
3363 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3364
3365 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003366 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003367
3368 /* See if any conversion is specified. */
3369 switch (which_conversion) {
3370 case FVC_STR: conv_fn = PyObject_Str; break;
3371 case FVC_REPR: conv_fn = PyObject_Repr; break;
3372 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3373
3374 /* Must be 0 (meaning no conversion), since only four
3375 values are allowed by (oparg & FVC_MASK). */
3376 default: conv_fn = NULL; break;
3377 }
3378
3379 /* If there's a conversion function, call it and replace
3380 value with that result. Otherwise, just use value,
3381 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003382 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003383 result = conv_fn(value);
3384 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003385 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003386 Py_XDECREF(fmt_spec);
3387 goto error;
3388 }
3389 value = result;
3390 }
3391
3392 /* If value is a unicode object, and there's no fmt_spec,
3393 then we know the result of format(value) is value
3394 itself. In that case, skip calling format(). I plan to
3395 move this optimization in to PyObject_Format()
3396 itself. */
3397 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3398 /* Do nothing, just transfer ownership to result. */
3399 result = value;
3400 } else {
3401 /* Actually call format(). */
3402 result = PyObject_Format(value, fmt_spec);
3403 Py_DECREF(value);
3404 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003405 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003406 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003407 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003408 }
3409
Eric V. Smith135d5f42016-02-05 18:23:08 -05003410 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003411 DISPATCH();
3412 }
3413
Benjamin Petersonddd19492018-09-16 22:38:02 -07003414 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003415 int oldoparg = oparg;
3416 NEXTOPARG();
3417 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003418 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003419 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003420
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003421
Antoine Pitrou042b1282010-08-13 21:15:58 +00003422#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003423 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003424#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003425 default:
3426 fprintf(stderr,
3427 "XXX lineno: %d, opcode: %d\n",
3428 PyFrame_GetLineNumber(f),
3429 opcode);
3430 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003431 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003433 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003434
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003435 /* This should never be reached. Every opcode should end with DISPATCH()
3436 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003437 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003438
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003439error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003440 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003441#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003442 if (!PyErr_Occurred())
3443 PyErr_SetString(PyExc_SystemError,
3444 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003445#else
3446 assert(PyErr_Occurred());
3447#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003448
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003449 /* Log traceback info. */
3450 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003451
Benjamin Peterson51f46162013-01-23 08:38:47 -05003452 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003453 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3454 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003455
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003456exception_unwind:
3457 /* Unwind stacks if an exception occurred */
3458 while (f->f_iblock > 0) {
3459 /* Pop the current block. */
3460 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003462 if (b->b_type == EXCEPT_HANDLER) {
3463 UNWIND_EXCEPT_HANDLER(b);
3464 continue;
3465 }
3466 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003467 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003468 PyObject *exc, *val, *tb;
3469 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003470 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003471 /* Beware, this invalidates all b->b_* fields */
3472 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003473 PUSH(exc_info->exc_traceback);
3474 PUSH(exc_info->exc_value);
3475 if (exc_info->exc_type != NULL) {
3476 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003477 }
3478 else {
3479 Py_INCREF(Py_None);
3480 PUSH(Py_None);
3481 }
3482 PyErr_Fetch(&exc, &val, &tb);
3483 /* Make the raw exception data
3484 available to the handler,
3485 so a program can emulate the
3486 Python main loop. */
3487 PyErr_NormalizeException(
3488 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003489 if (tb != NULL)
3490 PyException_SetTraceback(val, tb);
3491 else
3492 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003493 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003494 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003495 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003496 exc_info->exc_value = val;
3497 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003498 if (tb == NULL)
3499 tb = Py_None;
3500 Py_INCREF(tb);
3501 PUSH(tb);
3502 PUSH(val);
3503 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003504 JUMPTO(handler);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003505 /* Resume normal execution */
3506 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003507 }
3508 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003509
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003510 /* End the loop as we still have an error */
3511 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003512 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003514 /* Pop remaining stack entries. */
3515 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003516 PyObject *o = POP();
3517 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003518 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003519
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003520 assert(retval == NULL);
3521 assert(PyErr_Occurred());
Guido van Rossumac7be682001-01-17 15:42:30 +00003522
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003523return_or_yield:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003524 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003525 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003526 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3527 tstate, f, PyTrace_RETURN, retval)) {
3528 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003529 }
3530 }
3531 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003532 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3533 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003534 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003535 }
3536 }
3537 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003539 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003540exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003541 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3542 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003543 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003544 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003545 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003546
Victor Stinnerefde1462015-03-21 15:04:43 +01003547 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003548}
3549
Benjamin Petersonb204a422011-06-05 22:04:07 -05003550static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003551format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3552{
3553 int err;
3554 Py_ssize_t len = PyList_GET_SIZE(names);
3555 PyObject *name_str, *comma, *tail, *tmp;
3556
3557 assert(PyList_CheckExact(names));
3558 assert(len >= 1);
3559 /* Deal with the joys of natural language. */
3560 switch (len) {
3561 case 1:
3562 name_str = PyList_GET_ITEM(names, 0);
3563 Py_INCREF(name_str);
3564 break;
3565 case 2:
3566 name_str = PyUnicode_FromFormat("%U and %U",
3567 PyList_GET_ITEM(names, len - 2),
3568 PyList_GET_ITEM(names, len - 1));
3569 break;
3570 default:
3571 tail = PyUnicode_FromFormat(", %U, and %U",
3572 PyList_GET_ITEM(names, len - 2),
3573 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003574 if (tail == NULL)
3575 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003576 /* Chop off the last two objects in the list. This shouldn't actually
3577 fail, but we can't be too careful. */
3578 err = PyList_SetSlice(names, len - 2, len, NULL);
3579 if (err == -1) {
3580 Py_DECREF(tail);
3581 return;
3582 }
3583 /* Stitch everything up into a nice comma-separated list. */
3584 comma = PyUnicode_FromString(", ");
3585 if (comma == NULL) {
3586 Py_DECREF(tail);
3587 return;
3588 }
3589 tmp = PyUnicode_Join(comma, names);
3590 Py_DECREF(comma);
3591 if (tmp == NULL) {
3592 Py_DECREF(tail);
3593 return;
3594 }
3595 name_str = PyUnicode_Concat(tmp, tail);
3596 Py_DECREF(tmp);
3597 Py_DECREF(tail);
3598 break;
3599 }
3600 if (name_str == NULL)
3601 return;
3602 PyErr_Format(PyExc_TypeError,
3603 "%U() missing %i required %s argument%s: %U",
3604 co->co_name,
3605 len,
3606 kind,
3607 len == 1 ? "" : "s",
3608 name_str);
3609 Py_DECREF(name_str);
3610}
3611
3612static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003613missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003614 PyObject **fastlocals)
3615{
Victor Stinner74319ae2016-08-25 00:04:09 +02003616 Py_ssize_t i, j = 0;
3617 Py_ssize_t start, end;
3618 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003619 const char *kind = positional ? "positional" : "keyword-only";
3620 PyObject *missing_names;
3621
3622 /* Compute the names of the arguments that are missing. */
3623 missing_names = PyList_New(missing);
3624 if (missing_names == NULL)
3625 return;
3626 if (positional) {
3627 start = 0;
3628 end = co->co_argcount - defcount;
3629 }
3630 else {
3631 start = co->co_argcount;
3632 end = start + co->co_kwonlyargcount;
3633 }
3634 for (i = start; i < end; i++) {
3635 if (GETLOCAL(i) == NULL) {
3636 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3637 PyObject *name = PyObject_Repr(raw);
3638 if (name == NULL) {
3639 Py_DECREF(missing_names);
3640 return;
3641 }
3642 PyList_SET_ITEM(missing_names, j++, name);
3643 }
3644 }
3645 assert(j == missing);
3646 format_missing(kind, co, missing_names);
3647 Py_DECREF(missing_names);
3648}
3649
3650static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003651too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3652 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003653{
3654 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003655 Py_ssize_t kwonly_given = 0;
3656 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003657 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003658 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003659
Benjamin Petersone109c702011-06-24 09:37:26 -05003660 assert((co->co_flags & CO_VARARGS) == 0);
3661 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003662 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3663 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003664 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003665 }
3666 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003667 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003668 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003669 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003670 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003671 }
3672 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003673 plural = (co_argcount != 1);
3674 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003675 }
3676 if (sig == NULL)
3677 return;
3678 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003679 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3680 kwonly_sig = PyUnicode_FromFormat(format,
3681 given != 1 ? "s" : "",
3682 kwonly_given,
3683 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003684 if (kwonly_sig == NULL) {
3685 Py_DECREF(sig);
3686 return;
3687 }
3688 }
3689 else {
3690 /* This will not fail. */
3691 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003692 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003693 }
3694 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003695 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003696 co->co_name,
3697 sig,
3698 plural ? "s" : "",
3699 given,
3700 kwonly_sig,
3701 given == 1 && !kwonly_given ? "was" : "were");
3702 Py_DECREF(sig);
3703 Py_DECREF(kwonly_sig);
3704}
3705
Guido van Rossumc2e20742006-02-27 22:32:47 +00003706/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02003707 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003708 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003709
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003710PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +02003711_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003712 PyObject *const *args, Py_ssize_t argcount,
3713 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003714 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003715 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003716 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003717 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003718{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003719 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003720 PyFrameObject *f;
3721 PyObject *retval = NULL;
3722 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003723 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003724 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003725 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3726 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003727 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003729 if (globals == NULL) {
3730 PyErr_SetString(PyExc_SystemError,
3731 "PyEval_EvalCodeEx: NULL globals");
3732 return NULL;
3733 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003734
Victor Stinnerc7020012016-08-16 23:40:29 +02003735 /* Create the frame */
Victor Stinner50b48572018-11-01 01:51:40 +01003736 tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003737 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003738 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003739 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003740 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003741 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003742 fastlocals = f->f_localsplus;
3743 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003744
Victor Stinnerc7020012016-08-16 23:40:29 +02003745 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003746 if (co->co_flags & CO_VARKEYWORDS) {
3747 kwdict = PyDict_New();
3748 if (kwdict == NULL)
3749 goto fail;
3750 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003751 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003752 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003753 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003754 SETLOCAL(i, kwdict);
3755 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003756 else {
3757 kwdict = NULL;
3758 }
3759
3760 /* Copy positional arguments into local variables */
3761 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003762 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003763 }
3764 else {
3765 n = argcount;
3766 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003767 for (i = 0; i < n; i++) {
3768 x = args[i];
3769 Py_INCREF(x);
3770 SETLOCAL(i, x);
3771 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003772
3773 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003774 if (co->co_flags & CO_VARARGS) {
3775 u = PyTuple_New(argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003776 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003777 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003778 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003779 SETLOCAL(total_args, u);
3780 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003781 x = args[i];
3782 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003783 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003784 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003785 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003786
Serhiy Storchakab7281052016-09-12 00:52:40 +03003787 /* Handle keyword arguments passed as two strided arrays */
3788 kwcount *= kwstep;
3789 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003790 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003791 PyObject *keyword = kwnames[i];
3792 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003793 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003794
Benjamin Petersonb204a422011-06-05 22:04:07 -05003795 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3796 PyErr_Format(PyExc_TypeError,
3797 "%U() keywords must be strings",
3798 co->co_name);
3799 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003800 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003801
Benjamin Petersonb204a422011-06-05 22:04:07 -05003802 /* Speed hack: do raw pointer compares. As names are
3803 normally interned this should almost always hit. */
3804 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3805 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003806 PyObject *name = co_varnames[j];
3807 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003808 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003809 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003810 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003811
Benjamin Petersonb204a422011-06-05 22:04:07 -05003812 /* Slow fallback, just in case */
3813 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003814 PyObject *name = co_varnames[j];
3815 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
3816 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003817 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003818 }
3819 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003820 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003821 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003822 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003823
Victor Stinner231d1f32017-01-11 02:12:06 +01003824 assert(j >= total_args);
3825 if (kwdict == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003826 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003827 "%U() got an unexpected keyword argument '%S'",
3828 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003829 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003830 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003831
Christian Heimes0bd447f2013-07-20 14:48:10 +02003832 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3833 goto fail;
3834 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003835 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02003836
Benjamin Petersonb204a422011-06-05 22:04:07 -05003837 kw_found:
3838 if (GETLOCAL(j) != NULL) {
3839 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003840 "%U() got multiple values for argument '%S'",
3841 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003842 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003843 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003844 Py_INCREF(value);
3845 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003846 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003847
3848 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003849 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003850 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003851 goto fail;
3852 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003853
3854 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003855 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02003856 Py_ssize_t m = co->co_argcount - defcount;
3857 Py_ssize_t missing = 0;
3858 for (i = argcount; i < m; i++) {
3859 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003860 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02003861 }
3862 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003863 if (missing) {
3864 missing_arguments(co, missing, defcount, fastlocals);
3865 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003866 }
3867 if (n > m)
3868 i = n - m;
3869 else
3870 i = 0;
3871 for (; i < defcount; i++) {
3872 if (GETLOCAL(m+i) == NULL) {
3873 PyObject *def = defs[i];
3874 Py_INCREF(def);
3875 SETLOCAL(m+i, def);
3876 }
3877 }
3878 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003879
3880 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003881 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02003882 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003883 for (i = co->co_argcount; i < total_args; i++) {
3884 PyObject *name;
3885 if (GETLOCAL(i) != NULL)
3886 continue;
3887 name = PyTuple_GET_ITEM(co->co_varnames, i);
3888 if (kwdefs != NULL) {
3889 PyObject *def = PyDict_GetItem(kwdefs, name);
3890 if (def) {
3891 Py_INCREF(def);
3892 SETLOCAL(i, def);
3893 continue;
3894 }
3895 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003896 missing++;
3897 }
3898 if (missing) {
3899 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003900 goto fail;
3901 }
3902 }
3903
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003904 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003905 vars into frame. */
3906 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003907 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02003908 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05003909 /* Possibly account for the cell variable being an argument. */
3910 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07003911 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05003912 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05003913 /* Clear the local copy. */
3914 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003915 }
3916 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05003917 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003918 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05003919 if (c == NULL)
3920 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05003921 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003922 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003923
3924 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05003925 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3926 PyObject *o = PyTuple_GET_ITEM(closure, i);
3927 Py_INCREF(o);
3928 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003929 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003930
Yury Selivanoveb636452016-09-08 22:01:51 -07003931 /* Handle generator/coroutine/asynchronous generator */
3932 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003933 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04003934 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003935 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04003936
3937 if (is_coro && tstate->in_coroutine_wrapper) {
3938 assert(coro_wrapper != NULL);
3939 PyErr_Format(PyExc_RuntimeError,
3940 "coroutine wrapper %.200R attempted "
3941 "to recursively wrap %.200R",
3942 coro_wrapper,
3943 co);
3944 goto fail;
3945 }
Yury Selivanov75445082015-05-11 22:57:16 -04003946
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003947 /* Don't need to keep the reference to f_back, it will be set
3948 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003949 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003951 /* Create a new generator that owns the ready to run frame
3952 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04003953 if (is_coro) {
3954 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07003955 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
3956 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003957 } else {
3958 gen = PyGen_NewWithQualName(f, name, qualname);
3959 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003960 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003961 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003962 }
INADA Naoki9c157762016-12-26 18:52:46 +09003963
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003964 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04003965
Yury Selivanov94c22632015-06-04 10:16:51 -04003966 if (is_coro && coro_wrapper != NULL) {
3967 PyObject *wrapped;
3968 tstate->in_coroutine_wrapper = 1;
3969 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
3970 tstate->in_coroutine_wrapper = 0;
3971 return wrapped;
3972 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04003973
Yury Selivanov75445082015-05-11 22:57:16 -04003974 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003975 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003976
Victor Stinner59a73272016-12-09 18:51:13 +01003977 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003978
Thomas Woutersce272b62007-09-19 21:19:28 +00003979fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003981 /* decref'ing the frame can cause __del__ methods to get invoked,
3982 which can call back into Python. While we're done with the
3983 current Python frame (f), the associated C stack is still in use,
3984 so recursion_depth must be boosted for the duration.
3985 */
3986 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003987 if (Py_REFCNT(f) > 1) {
3988 Py_DECREF(f);
3989 _PyObject_GC_TRACK(f);
3990 }
3991 else {
3992 ++tstate->recursion_depth;
3993 Py_DECREF(f);
3994 --tstate->recursion_depth;
3995 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003996 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003997}
3998
Victor Stinner40ee3012014-06-16 15:59:28 +02003999PyObject *
4000PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004001 PyObject *const *args, int argcount,
4002 PyObject *const *kws, int kwcount,
4003 PyObject *const *defs, int defcount,
4004 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004005{
4006 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004007 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004008 kws, kws != NULL ? kws + 1 : NULL,
4009 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004010 defs, defcount,
4011 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004012 NULL, NULL);
4013}
Tim Peters5ca576e2001-06-18 22:08:13 +00004014
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004015static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05004016special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004017{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004018 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004019 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004020 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05004021 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004022 return NULL;
4023 }
4024 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004025}
4026
4027
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004028/* Logic for the raise statement (too complicated for inlining).
4029 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004030static int
Collin Winter828f04a2007-08-31 00:04:24 +00004031do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004032{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004033 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004034
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004035 if (exc == NULL) {
4036 /* Reraise */
Victor Stinner50b48572018-11-01 01:51:40 +01004037 PyThreadState *tstate = _PyThreadState_GET();
Mark Shannonae3087c2017-10-22 22:41:51 +01004038 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004039 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004040 type = exc_info->exc_type;
4041 value = exc_info->exc_value;
4042 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004043 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004044 PyErr_SetString(PyExc_RuntimeError,
4045 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004046 return 0;
4047 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004048 Py_XINCREF(type);
4049 Py_XINCREF(value);
4050 Py_XINCREF(tb);
4051 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004052 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004053 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004055 /* We support the following forms of raise:
4056 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004057 raise <instance>
4058 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004060 if (PyExceptionClass_Check(exc)) {
4061 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004062 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004063 if (value == NULL)
4064 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004065 if (!PyExceptionInstance_Check(value)) {
4066 PyErr_Format(PyExc_TypeError,
4067 "calling %R should have returned an instance of "
4068 "BaseException, not %R",
4069 type, Py_TYPE(value));
4070 goto raise_error;
4071 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004072 }
4073 else if (PyExceptionInstance_Check(exc)) {
4074 value = exc;
4075 type = PyExceptionInstance_Class(exc);
4076 Py_INCREF(type);
4077 }
4078 else {
4079 /* Not something you can raise. You get an exception
4080 anyway, just not what you specified :-) */
4081 Py_DECREF(exc);
4082 PyErr_SetString(PyExc_TypeError,
4083 "exceptions must derive from BaseException");
4084 goto raise_error;
4085 }
Collin Winter828f04a2007-08-31 00:04:24 +00004086
Serhiy Storchakac0191582016-09-27 11:37:10 +03004087 assert(type != NULL);
4088 assert(value != NULL);
4089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004090 if (cause) {
4091 PyObject *fixed_cause;
4092 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004093 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004094 if (fixed_cause == NULL)
4095 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004096 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004097 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004098 else if (PyExceptionInstance_Check(cause)) {
4099 fixed_cause = cause;
4100 }
4101 else if (cause == Py_None) {
4102 Py_DECREF(cause);
4103 fixed_cause = NULL;
4104 }
4105 else {
4106 PyErr_SetString(PyExc_TypeError,
4107 "exception causes must derive from "
4108 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004109 goto raise_error;
4110 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004111 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004112 }
Collin Winter828f04a2007-08-31 00:04:24 +00004113
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004114 PyErr_SetObject(type, value);
4115 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004116 Py_DECREF(value);
4117 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004118 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004119
4120raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004121 Py_XDECREF(value);
4122 Py_XDECREF(type);
4123 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004124 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004125}
4126
Tim Petersd6d010b2001-06-21 02:49:55 +00004127/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004128 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004129
Guido van Rossum0368b722007-05-11 16:50:42 +00004130 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4131 with a variable target.
4132*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004133
Barry Warsawe42b18f1997-08-25 22:13:04 +00004134static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004135unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004136{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004137 int i = 0, j = 0;
4138 Py_ssize_t ll = 0;
4139 PyObject *it; /* iter(v) */
4140 PyObject *w;
4141 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004142
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004143 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004144
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004145 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004146 if (it == NULL) {
4147 if (PyErr_ExceptionMatches(PyExc_TypeError) &&
4148 v->ob_type->tp_iter == NULL && !PySequence_Check(v))
4149 {
4150 PyErr_Format(PyExc_TypeError,
4151 "cannot unpack non-iterable %.200s object",
4152 v->ob_type->tp_name);
4153 }
4154 return 0;
4155 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004156
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004157 for (; i < argcnt; i++) {
4158 w = PyIter_Next(it);
4159 if (w == NULL) {
4160 /* Iterator done, via error or exhaustion. */
4161 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004162 if (argcntafter == -1) {
4163 PyErr_Format(PyExc_ValueError,
4164 "not enough values to unpack (expected %d, got %d)",
4165 argcnt, i);
4166 }
4167 else {
4168 PyErr_Format(PyExc_ValueError,
4169 "not enough values to unpack "
4170 "(expected at least %d, got %d)",
4171 argcnt + argcntafter, i);
4172 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004173 }
4174 goto Error;
4175 }
4176 *--sp = w;
4177 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004178
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004179 if (argcntafter == -1) {
4180 /* We better have exhausted the iterator now. */
4181 w = PyIter_Next(it);
4182 if (w == NULL) {
4183 if (PyErr_Occurred())
4184 goto Error;
4185 Py_DECREF(it);
4186 return 1;
4187 }
4188 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004189 PyErr_Format(PyExc_ValueError,
4190 "too many values to unpack (expected %d)",
4191 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004192 goto Error;
4193 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004195 l = PySequence_List(it);
4196 if (l == NULL)
4197 goto Error;
4198 *--sp = l;
4199 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004201 ll = PyList_GET_SIZE(l);
4202 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004203 PyErr_Format(PyExc_ValueError,
4204 "not enough values to unpack (expected at least %d, got %zd)",
4205 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004206 goto Error;
4207 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004209 /* Pop the "after-variable" args off the list. */
4210 for (j = argcntafter; j > 0; j--, i++) {
4211 *--sp = PyList_GET_ITEM(l, ll - j);
4212 }
4213 /* Resize the list. */
4214 Py_SIZE(l) = ll - argcntafter;
4215 Py_DECREF(it);
4216 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004217
Tim Petersd6d010b2001-06-21 02:49:55 +00004218Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004219 for (; i > 0; i--, sp++)
4220 Py_DECREF(*sp);
4221 Py_XDECREF(it);
4222 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004223}
4224
4225
Guido van Rossum96a42c81992-01-12 02:29:51 +00004226#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004227static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004228prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004229{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004230 printf("%s ", str);
4231 if (PyObject_Print(v, stdout, 0) != 0)
4232 PyErr_Clear(); /* Don't know what else to do */
4233 printf("\n");
4234 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004235}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004236#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004237
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004238static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004239call_exc_trace(Py_tracefunc func, PyObject *self,
4240 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004241{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004242 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004243 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004244 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004245 if (value == NULL) {
4246 value = Py_None;
4247 Py_INCREF(value);
4248 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004249 PyErr_NormalizeException(&type, &value, &orig_traceback);
4250 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004251 arg = PyTuple_Pack(3, type, value, traceback);
4252 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004253 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004254 return;
4255 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004256 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004257 Py_DECREF(arg);
4258 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004259 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004260 else {
4261 Py_XDECREF(type);
4262 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004263 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004264 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004265}
4266
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004267static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004268call_trace_protected(Py_tracefunc func, PyObject *obj,
4269 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004270 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004271{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004272 PyObject *type, *value, *traceback;
4273 int err;
4274 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004275 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004276 if (err == 0)
4277 {
4278 PyErr_Restore(type, value, traceback);
4279 return 0;
4280 }
4281 else {
4282 Py_XDECREF(type);
4283 Py_XDECREF(value);
4284 Py_XDECREF(traceback);
4285 return -1;
4286 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004287}
4288
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004289static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004290call_trace(Py_tracefunc func, PyObject *obj,
4291 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004292 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004293{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004294 int result;
4295 if (tstate->tracing)
4296 return 0;
4297 tstate->tracing++;
4298 tstate->use_tracing = 0;
4299 result = func(obj, frame, what, arg);
4300 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4301 || (tstate->c_profilefunc != NULL));
4302 tstate->tracing--;
4303 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004304}
4305
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004306PyObject *
4307_PyEval_CallTracing(PyObject *func, PyObject *args)
4308{
Victor Stinner50b48572018-11-01 01:51:40 +01004309 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004310 int save_tracing = tstate->tracing;
4311 int save_use_tracing = tstate->use_tracing;
4312 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004314 tstate->tracing = 0;
4315 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4316 || (tstate->c_profilefunc != NULL));
4317 result = PyObject_Call(func, args, NULL);
4318 tstate->tracing = save_tracing;
4319 tstate->use_tracing = save_use_tracing;
4320 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004321}
4322
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004323/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004324static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004325maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004326 PyThreadState *tstate, PyFrameObject *frame,
4327 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004328{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004329 int result = 0;
4330 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004332 /* If the last instruction executed isn't in the current
4333 instruction window, reset the window.
4334 */
4335 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4336 PyAddrPair bounds;
4337 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4338 &bounds);
4339 *instr_lb = bounds.ap_lower;
4340 *instr_ub = bounds.ap_upper;
4341 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004342 /* If the last instruction falls at the start of a line or if it
4343 represents a jump backwards, update the frame's line number and
4344 then call the trace function if we're tracing source lines.
4345 */
4346 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004347 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004348 if (frame->f_trace_lines) {
4349 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4350 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004351 }
George King20faa682017-10-18 17:44:22 -07004352 /* Always emit an opcode event if we're tracing all opcodes. */
4353 if (frame->f_trace_opcodes) {
4354 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4355 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004356 *instr_prev = frame->f_lasti;
4357 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004358}
4359
Fred Drake5755ce62001-06-27 19:19:46 +00004360void
4361PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004362{
Victor Stinner50b48572018-11-01 01:51:40 +01004363 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004364 PyObject *temp = tstate->c_profileobj;
4365 Py_XINCREF(arg);
4366 tstate->c_profilefunc = NULL;
4367 tstate->c_profileobj = NULL;
4368 /* Must make sure that tracing is not ignored if 'temp' is freed */
4369 tstate->use_tracing = tstate->c_tracefunc != NULL;
4370 Py_XDECREF(temp);
4371 tstate->c_profilefunc = func;
4372 tstate->c_profileobj = arg;
4373 /* Flag that tracing or profiling is turned on */
4374 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004375}
4376
4377void
4378PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4379{
Victor Stinner50b48572018-11-01 01:51:40 +01004380 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004381 PyObject *temp = tstate->c_traceobj;
4382 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4383 Py_XINCREF(arg);
4384 tstate->c_tracefunc = NULL;
4385 tstate->c_traceobj = NULL;
4386 /* Must make sure that profiling is not ignored if 'temp' is freed */
4387 tstate->use_tracing = tstate->c_profilefunc != NULL;
4388 Py_XDECREF(temp);
4389 tstate->c_tracefunc = func;
4390 tstate->c_traceobj = arg;
4391 /* Flag that tracing or profiling is turned on */
4392 tstate->use_tracing = ((func != NULL)
4393 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004394}
4395
Yury Selivanov75445082015-05-11 22:57:16 -04004396void
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004397_PyEval_SetCoroutineOriginTrackingDepth(int new_depth)
4398{
4399 assert(new_depth >= 0);
Victor Stinner50b48572018-11-01 01:51:40 +01004400 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004401 tstate->coroutine_origin_tracking_depth = new_depth;
4402}
4403
4404int
4405_PyEval_GetCoroutineOriginTrackingDepth(void)
4406{
Victor Stinner50b48572018-11-01 01:51:40 +01004407 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004408 return tstate->coroutine_origin_tracking_depth;
4409}
4410
4411void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004412_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004413{
Victor Stinner50b48572018-11-01 01:51:40 +01004414 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanov75445082015-05-11 22:57:16 -04004415
Yury Selivanov75445082015-05-11 22:57:16 -04004416 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004417 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004418}
4419
4420PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004421_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004422{
Victor Stinner50b48572018-11-01 01:51:40 +01004423 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanov75445082015-05-11 22:57:16 -04004424 return tstate->coroutine_wrapper;
4425}
4426
Yury Selivanoveb636452016-09-08 22:01:51 -07004427void
4428_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4429{
Victor Stinner50b48572018-11-01 01:51:40 +01004430 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004431
4432 Py_XINCREF(firstiter);
4433 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4434}
4435
4436PyObject *
4437_PyEval_GetAsyncGenFirstiter(void)
4438{
Victor Stinner50b48572018-11-01 01:51:40 +01004439 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004440 return tstate->async_gen_firstiter;
4441}
4442
4443void
4444_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4445{
Victor Stinner50b48572018-11-01 01:51:40 +01004446 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004447
4448 Py_XINCREF(finalizer);
4449 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4450}
4451
4452PyObject *
4453_PyEval_GetAsyncGenFinalizer(void)
4454{
Victor Stinner50b48572018-11-01 01:51:40 +01004455 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004456 return tstate->async_gen_finalizer;
4457}
4458
Guido van Rossumb209a111997-04-29 18:18:01 +00004459PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004460PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004461{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004462 PyFrameObject *current_frame = PyEval_GetFrame();
4463 if (current_frame == NULL)
Victor Stinnercaba55b2018-08-03 15:33:52 +02004464 return _PyInterpreterState_GET_UNSAFE()->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004465 else
4466 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004467}
4468
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004469/* Convenience function to get a builtin from its name */
4470PyObject *
4471_PyEval_GetBuiltinId(_Py_Identifier *name)
4472{
4473 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4474 if (attr) {
4475 Py_INCREF(attr);
4476 }
4477 else if (!PyErr_Occurred()) {
4478 PyErr_SetObject(PyExc_AttributeError, _PyUnicode_FromId(name));
4479 }
4480 return attr;
4481}
4482
Guido van Rossumb209a111997-04-29 18:18:01 +00004483PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004484PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004485{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004486 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004487 if (current_frame == NULL) {
4488 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004489 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004490 }
4491
4492 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4493 return NULL;
4494
4495 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004496 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004497}
4498
Guido van Rossumb209a111997-04-29 18:18:01 +00004499PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004500PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004501{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004502 PyFrameObject *current_frame = PyEval_GetFrame();
4503 if (current_frame == NULL)
4504 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004505
4506 assert(current_frame->f_globals != NULL);
4507 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004508}
4509
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004510PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004511PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004512{
Victor Stinner50b48572018-11-01 01:51:40 +01004513 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004514 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004515}
4516
Guido van Rossum6135a871995-01-09 17:53:26 +00004517int
Tim Peters5ba58662001-07-16 02:29:45 +00004518PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004519{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004520 PyFrameObject *current_frame = PyEval_GetFrame();
4521 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004523 if (current_frame != NULL) {
4524 const int codeflags = current_frame->f_code->co_flags;
4525 const int compilerflags = codeflags & PyCF_MASK;
4526 if (compilerflags) {
4527 result = 1;
4528 cf->cf_flags |= compilerflags;
4529 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004530#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004531 if (codeflags & CO_GENERATOR_ALLOWED) {
4532 result = 1;
4533 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4534 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004535#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004536 }
4537 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004538}
4539
Guido van Rossum3f5da241990-12-20 15:06:42 +00004540
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004541const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004542PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004543{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004544 if (PyMethod_Check(func))
4545 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4546 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004547 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004548 else if (PyCFunction_Check(func))
4549 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4550 else
4551 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004552}
4553
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004554const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004555PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004556{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004557 if (PyMethod_Check(func))
4558 return "()";
4559 else if (PyFunction_Check(func))
4560 return "()";
4561 else if (PyCFunction_Check(func))
4562 return "()";
4563 else
4564 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004565}
4566
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004567#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004568if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004569 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4570 tstate, tstate->frame, \
4571 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004572 x = NULL; \
4573 } \
4574 else { \
4575 x = call; \
4576 if (tstate->c_profilefunc != NULL) { \
4577 if (x == NULL) { \
4578 call_trace_protected(tstate->c_profilefunc, \
4579 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004580 tstate, tstate->frame, \
4581 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004582 /* XXX should pass (type, value, tb) */ \
4583 } else { \
4584 if (call_trace(tstate->c_profilefunc, \
4585 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004586 tstate, tstate->frame, \
4587 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004588 Py_DECREF(x); \
4589 x = NULL; \
4590 } \
4591 } \
4592 } \
4593 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004594} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004595 x = call; \
4596 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004597
Victor Stinner415c5102017-01-11 00:54:57 +01004598/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4599 to reduce the stack consumption. */
4600Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Benjamin Peterson4fd64b92016-09-09 14:57:58 -07004601call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004602{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004603 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004604 PyObject *func = *pfunc;
4605 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004606 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4607 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004608 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004609
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004610 /* Always dispatch PyCFunction first, because these are
4611 presumed to be the most frequent callable object.
4612 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004613 if (PyCFunction_Check(func)) {
Victor Stinner50b48572018-11-01 01:51:40 +01004614 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004615 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004616 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004617 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
Victor Stinner50b48572018-11-01 01:51:40 +01004618 PyThreadState *tstate = _PyThreadState_GET();
jdemeyer56868f92018-07-21 10:30:59 +02004619 if (nargs > 0 && tstate->use_tracing) {
4620 /* We need to create a temporary bound method as argument
4621 for profiling.
4622
4623 If nargs == 0, then this cannot work because we have no
4624 "self". In any case, the call itself would raise
4625 TypeError (foo needs an argument), so we just skip
4626 profiling. */
4627 PyObject *self = stack[0];
4628 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
jdemeyer147d9552018-07-23 18:41:20 +02004629 if (func != NULL) {
4630 C_TRACE(x, _PyCFunction_FastCallKeywords(func,
4631 stack+1, nargs-1,
4632 kwnames));
4633 Py_DECREF(func);
INADA Naoki93fac8d2017-03-07 14:24:37 +09004634 }
jdemeyer147d9552018-07-23 18:41:20 +02004635 else {
4636 x = NULL;
4637 }
INADA Naoki93fac8d2017-03-07 14:24:37 +09004638 }
4639 else {
4640 x = _PyMethodDescr_FastCallKeywords(func, stack, nargs, kwnames);
4641 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004642 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004643 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004644 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
Victor Stinnerb69ee8c2016-11-28 18:32:31 +01004645 /* Optimize access to bound methods. Reuse the Python stack
4646 to pass 'self' as the first argument, replace 'func'
4647 with 'self'. It avoids the creation of a new temporary tuple
4648 for arguments (to replace func with self) when the method uses
4649 FASTCALL. */
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004650 PyObject *self = PyMethod_GET_SELF(func);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004651 Py_INCREF(self);
4652 func = PyMethod_GET_FUNCTION(func);
4653 Py_INCREF(func);
4654 Py_SETREF(*pfunc, self);
4655 nargs++;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004656 stack--;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004657 }
4658 else {
4659 Py_INCREF(func);
4660 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004661
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004662 if (PyFunction_Check(func)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004663 x = _PyFunction_FastCallKeywords(func, stack, nargs, kwnames);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004664 }
4665 else {
4666 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4667 }
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004668 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004669 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004670
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004671 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4672
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004673 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004674 while ((*pp_stack) > pfunc) {
4675 w = EXT_POP(*pp_stack);
4676 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004677 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004679 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004680}
4681
Jeremy Hylton52820442001-01-03 23:52:36 +00004682static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004683do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004684{
jdemeyere89de732018-09-19 12:06:20 +02004685 PyObject *result;
4686
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004687 if (PyCFunction_Check(func)) {
Victor Stinner50b48572018-11-01 01:51:40 +01004688 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004689 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004690 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004691 }
jdemeyere89de732018-09-19 12:06:20 +02004692 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
Victor Stinner50b48572018-11-01 01:51:40 +01004693 PyThreadState *tstate = _PyThreadState_GET();
jdemeyere89de732018-09-19 12:06:20 +02004694 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
4695 if (nargs > 0 && tstate->use_tracing) {
4696 /* We need to create a temporary bound method as argument
4697 for profiling.
4698
4699 If nargs == 0, then this cannot work because we have no
4700 "self". In any case, the call itself would raise
4701 TypeError (foo needs an argument), so we just skip
4702 profiling. */
4703 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
4704 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4705 if (func == NULL) {
4706 return NULL;
4707 }
4708
4709 C_TRACE(result, _PyCFunction_FastCallDict(func,
Victor Stinnerd17a6932018-11-09 16:56:48 +01004710 &_PyTuple_ITEMS(callargs)[1],
jdemeyere89de732018-09-19 12:06:20 +02004711 nargs - 1,
4712 kwdict));
4713 Py_DECREF(func);
4714 return result;
4715 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004716 }
jdemeyere89de732018-09-19 12:06:20 +02004717 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00004718}
4719
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004720/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004721 nb_index slot defined, and store in *pi.
4722 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004723 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004724 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004725*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004726int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004727_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004728{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004729 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004730 Py_ssize_t x;
4731 if (PyIndex_Check(v)) {
4732 x = PyNumber_AsSsize_t(v, NULL);
4733 if (x == -1 && PyErr_Occurred())
4734 return 0;
4735 }
4736 else {
4737 PyErr_SetString(PyExc_TypeError,
4738 "slice indices must be integers or "
4739 "None or have an __index__ method");
4740 return 0;
4741 }
4742 *pi = x;
4743 }
4744 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004745}
4746
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004747int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004748_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004749{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004750 Py_ssize_t x;
4751 if (PyIndex_Check(v)) {
4752 x = PyNumber_AsSsize_t(v, NULL);
4753 if (x == -1 && PyErr_Occurred())
4754 return 0;
4755 }
4756 else {
4757 PyErr_SetString(PyExc_TypeError,
4758 "slice indices must be integers or "
4759 "have an __index__ method");
4760 return 0;
4761 }
4762 *pi = x;
4763 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004764}
4765
4766
Guido van Rossum486364b2007-06-30 05:01:58 +00004767#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004768 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004769
Guido van Rossumb209a111997-04-29 18:18:01 +00004770static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004771cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004772{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004773 int res = 0;
4774 switch (op) {
4775 case PyCmp_IS:
4776 res = (v == w);
4777 break;
4778 case PyCmp_IS_NOT:
4779 res = (v != w);
4780 break;
4781 case PyCmp_IN:
4782 res = PySequence_Contains(w, v);
4783 if (res < 0)
4784 return NULL;
4785 break;
4786 case PyCmp_NOT_IN:
4787 res = PySequence_Contains(w, v);
4788 if (res < 0)
4789 return NULL;
4790 res = !res;
4791 break;
4792 case PyCmp_EXC_MATCH:
4793 if (PyTuple_Check(w)) {
4794 Py_ssize_t i, length;
4795 length = PyTuple_Size(w);
4796 for (i = 0; i < length; i += 1) {
4797 PyObject *exc = PyTuple_GET_ITEM(w, i);
4798 if (!PyExceptionClass_Check(exc)) {
4799 PyErr_SetString(PyExc_TypeError,
4800 CANNOT_CATCH_MSG);
4801 return NULL;
4802 }
4803 }
4804 }
4805 else {
4806 if (!PyExceptionClass_Check(w)) {
4807 PyErr_SetString(PyExc_TypeError,
4808 CANNOT_CATCH_MSG);
4809 return NULL;
4810 }
4811 }
4812 res = PyErr_GivenExceptionMatches(v, w);
4813 break;
4814 default:
4815 return PyObject_RichCompare(v, w, op);
4816 }
4817 v = res ? Py_True : Py_False;
4818 Py_INCREF(v);
4819 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004820}
4821
Thomas Wouters52152252000-08-17 22:55:00 +00004822static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004823import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
4824{
4825 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004826 PyObject *import_func, *res;
4827 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004828
4829 import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
4830 if (import_func == NULL) {
4831 PyErr_SetString(PyExc_ImportError, "__import__ not found");
4832 return NULL;
4833 }
4834
4835 /* Fast path for not overloaded __import__. */
Victor Stinnercaba55b2018-08-03 15:33:52 +02004836 if (import_func == _PyInterpreterState_GET_UNSAFE()->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004837 int ilevel = _PyLong_AsInt(level);
4838 if (ilevel == -1 && PyErr_Occurred()) {
4839 return NULL;
4840 }
4841 res = PyImport_ImportModuleLevelObject(
4842 name,
4843 f->f_globals,
4844 f->f_locals == NULL ? Py_None : f->f_locals,
4845 fromlist,
4846 ilevel);
4847 return res;
4848 }
4849
4850 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004851
4852 stack[0] = name;
4853 stack[1] = f->f_globals;
4854 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
4855 stack[3] = fromlist;
4856 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02004857 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004858 Py_DECREF(import_func);
4859 return res;
4860}
4861
4862static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004863import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004864{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004865 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02004866 _Py_IDENTIFIER(__name__);
Xiang Zhang4830f582017-03-21 11:13:42 +08004867 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004868
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004869 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02004870 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004871 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004872 /* Issue #17636: in case this failed because of a circular relative
4873 import, try to fallback on reading the module directly from
4874 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02004875 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07004876 if (pkgname == NULL) {
4877 goto error;
4878 }
Oren Milman6db70332017-09-19 14:23:01 +03004879 if (!PyUnicode_Check(pkgname)) {
4880 Py_CLEAR(pkgname);
4881 goto error;
4882 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004883 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07004884 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08004885 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004886 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07004887 }
Eric Snow3f9eee62017-09-15 16:35:20 -06004888 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004889 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07004890 if (x == NULL) {
4891 goto error;
4892 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004893 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004894 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07004895 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004896 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004897 if (pkgname == NULL) {
4898 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
4899 if (pkgname_or_unknown == NULL) {
4900 Py_XDECREF(pkgpath);
4901 return NULL;
4902 }
4903 } else {
4904 pkgname_or_unknown = pkgname;
4905 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004906
4907 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
4908 PyErr_Clear();
Xiang Zhang4830f582017-03-21 11:13:42 +08004909 errmsg = PyUnicode_FromFormat(
4910 "cannot import name %R from %R (unknown location)",
4911 name, pkgname_or_unknown
4912 );
4913 /* NULL check for errmsg done by PyErr_SetImportError. */
4914 PyErr_SetImportError(errmsg, pkgname, NULL);
4915 }
4916 else {
4917 errmsg = PyUnicode_FromFormat(
4918 "cannot import name %R from %R (%S)",
4919 name, pkgname_or_unknown, pkgpath
4920 );
4921 /* NULL check for errmsg done by PyErr_SetImportError. */
4922 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004923 }
4924
Xiang Zhang4830f582017-03-21 11:13:42 +08004925 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004926 Py_XDECREF(pkgname_or_unknown);
4927 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07004928 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00004929}
Guido van Rossumac7be682001-01-17 15:42:30 +00004930
Thomas Wouters52152252000-08-17 22:55:00 +00004931static int
4932import_all_from(PyObject *locals, PyObject *v)
4933{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004934 _Py_IDENTIFIER(__all__);
4935 _Py_IDENTIFIER(__dict__);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08004936 _Py_IDENTIFIER(__name__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004937 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004938 int skip_leading_underscores = 0;
4939 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004940
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004941 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
4942 return -1; /* Unexpected error */
4943 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004944 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004945 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
4946 return -1;
4947 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004948 if (dict == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004949 PyErr_SetString(PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004950 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004951 return -1;
4952 }
4953 all = PyMapping_Keys(dict);
4954 Py_DECREF(dict);
4955 if (all == NULL)
4956 return -1;
4957 skip_leading_underscores = 1;
4958 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004959
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004960 for (pos = 0, err = 0; ; pos++) {
4961 name = PySequence_GetItem(all, pos);
4962 if (name == NULL) {
4963 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4964 err = -1;
4965 else
4966 PyErr_Clear();
4967 break;
4968 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08004969 if (!PyUnicode_Check(name)) {
4970 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
4971 if (modname == NULL) {
4972 Py_DECREF(name);
4973 err = -1;
4974 break;
4975 }
4976 if (!PyUnicode_Check(modname)) {
4977 PyErr_Format(PyExc_TypeError,
4978 "module __name__ must be a string, not %.100s",
4979 Py_TYPE(modname)->tp_name);
4980 }
4981 else {
4982 PyErr_Format(PyExc_TypeError,
4983 "%s in %U.%s must be str, not %.100s",
4984 skip_leading_underscores ? "Key" : "Item",
4985 modname,
4986 skip_leading_underscores ? "__dict__" : "__all__",
4987 Py_TYPE(name)->tp_name);
4988 }
4989 Py_DECREF(modname);
4990 Py_DECREF(name);
4991 err = -1;
4992 break;
4993 }
4994 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03004995 if (PyUnicode_READY(name) == -1) {
4996 Py_DECREF(name);
4997 err = -1;
4998 break;
4999 }
5000 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5001 Py_DECREF(name);
5002 continue;
5003 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005004 }
5005 value = PyObject_GetAttr(v, name);
5006 if (value == NULL)
5007 err = -1;
5008 else if (PyDict_CheckExact(locals))
5009 err = PyDict_SetItem(locals, name, value);
5010 else
5011 err = PyObject_SetItem(locals, name, value);
5012 Py_DECREF(name);
5013 Py_XDECREF(value);
5014 if (err != 0)
5015 break;
5016 }
5017 Py_DECREF(all);
5018 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005019}
5020
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005021static int
5022check_args_iterable(PyObject *func, PyObject *args)
5023{
5024 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
5025 PyErr_Format(PyExc_TypeError,
5026 "%.200s%.200s argument after * "
5027 "must be an iterable, not %.200s",
5028 PyEval_GetFuncName(func),
5029 PyEval_GetFuncDesc(func),
5030 args->ob_type->tp_name);
5031 return -1;
5032 }
5033 return 0;
5034}
5035
5036static void
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005037format_kwargs_error(PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005038{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005039 /* _PyDict_MergeEx raises attribute
5040 * error (percolated from an attempt
5041 * to get 'keys' attribute) instead of
5042 * a type error if its second argument
5043 * is not a mapping.
5044 */
5045 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
5046 PyErr_Format(PyExc_TypeError,
5047 "%.200s%.200s argument after ** "
5048 "must be a mapping, not %.200s",
5049 PyEval_GetFuncName(func),
5050 PyEval_GetFuncDesc(func),
5051 kwargs->ob_type->tp_name);
5052 }
5053 else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
5054 PyObject *exc, *val, *tb;
5055 PyErr_Fetch(&exc, &val, &tb);
5056 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
5057 PyObject *key = PyTuple_GET_ITEM(val, 0);
5058 if (!PyUnicode_Check(key)) {
5059 PyErr_Format(PyExc_TypeError,
5060 "%.200s%.200s keywords must be strings",
5061 PyEval_GetFuncName(func),
5062 PyEval_GetFuncDesc(func));
5063 } else {
5064 PyErr_Format(PyExc_TypeError,
5065 "%.200s%.200s got multiple "
5066 "values for keyword argument '%U'",
5067 PyEval_GetFuncName(func),
5068 PyEval_GetFuncDesc(func),
5069 key);
5070 }
5071 Py_XDECREF(exc);
5072 Py_XDECREF(val);
5073 Py_XDECREF(tb);
5074 }
5075 else {
5076 PyErr_Restore(exc, val, tb);
5077 }
5078 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005079}
5080
Guido van Rossumac7be682001-01-17 15:42:30 +00005081static void
Neal Norwitzda059e32007-08-26 05:33:45 +00005082format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005083{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005084 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005085
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005086 if (!obj)
5087 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005088
Serhiy Storchaka06515832016-11-20 09:13:07 +02005089 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005090 if (!obj_str)
5091 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005092
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005093 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005094}
Guido van Rossum950361c1997-01-24 13:49:28 +00005095
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005096static void
5097format_exc_unbound(PyCodeObject *co, int oparg)
5098{
5099 PyObject *name;
5100 /* Don't stomp existing exception */
5101 if (PyErr_Occurred())
5102 return;
5103 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5104 name = PyTuple_GET_ITEM(co->co_cellvars,
5105 oparg);
5106 format_exc_check_arg(
5107 PyExc_UnboundLocalError,
5108 UNBOUNDLOCAL_ERROR_MSG,
5109 name);
5110 } else {
5111 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5112 PyTuple_GET_SIZE(co->co_cellvars));
5113 format_exc_check_arg(PyExc_NameError,
5114 UNBOUNDFREE_ERROR_MSG, name);
5115 }
5116}
5117
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005118static void
5119format_awaitable_error(PyTypeObject *type, int prevopcode)
5120{
5121 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5122 if (prevopcode == BEFORE_ASYNC_WITH) {
5123 PyErr_Format(PyExc_TypeError,
5124 "'async with' received an object from __aenter__ "
5125 "that does not implement __await__: %.100s",
5126 type->tp_name);
5127 }
5128 else if (prevopcode == WITH_CLEANUP_START) {
5129 PyErr_Format(PyExc_TypeError,
5130 "'async with' received an object from __aexit__ "
5131 "that does not implement __await__: %.100s",
5132 type->tp_name);
5133 }
5134 }
5135}
5136
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005137static PyObject *
5138unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005139 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005140{
5141 PyObject *res;
5142 if (Py_REFCNT(v) == 2) {
5143 /* In the common case, there are 2 references to the value
5144 * stored in 'variable' when the += is performed: one on the
5145 * value stack (in 'v') and one still stored in the
5146 * 'variable'. We try to delete the variable now to reduce
5147 * the refcnt to 1.
5148 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005149 int opcode, oparg;
5150 NEXTOPARG();
5151 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005152 case STORE_FAST:
5153 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005154 PyObject **fastlocals = f->f_localsplus;
5155 if (GETLOCAL(oparg) == v)
5156 SETLOCAL(oparg, NULL);
5157 break;
5158 }
5159 case STORE_DEREF:
5160 {
5161 PyObject **freevars = (f->f_localsplus +
5162 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005163 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005164 if (PyCell_GET(c) == v) {
5165 PyCell_SET(c, NULL);
5166 Py_DECREF(v);
5167 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005168 break;
5169 }
5170 case STORE_NAME:
5171 {
5172 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005173 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005174 PyObject *locals = f->f_locals;
Serhiy Storchaka8905fcc2018-12-11 08:38:03 +02005175 if (locals && PyDict_CheckExact(locals) &&
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005176 PyDict_GetItem(locals, name) == v) {
5177 if (PyDict_DelItem(locals, name) != 0) {
5178 PyErr_Clear();
5179 }
5180 }
5181 break;
5182 }
5183 }
5184 }
5185 res = v;
5186 PyUnicode_Append(&res, w);
5187 return res;
5188}
5189
Guido van Rossum950361c1997-01-24 13:49:28 +00005190#ifdef DYNAMIC_EXECUTION_PROFILE
5191
Skip Montanarof118cb12001-10-15 20:51:38 +00005192static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005193getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005194{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005195 int i;
5196 PyObject *l = PyList_New(256);
5197 if (l == NULL) return NULL;
5198 for (i = 0; i < 256; i++) {
5199 PyObject *x = PyLong_FromLong(a[i]);
5200 if (x == NULL) {
5201 Py_DECREF(l);
5202 return NULL;
5203 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005204 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005205 }
5206 for (i = 0; i < 256; i++)
5207 a[i] = 0;
5208 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005209}
5210
5211PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005212_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005213{
5214#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005215 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005216#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005217 int i;
5218 PyObject *l = PyList_New(257);
5219 if (l == NULL) return NULL;
5220 for (i = 0; i < 257; i++) {
5221 PyObject *x = getarray(dxpairs[i]);
5222 if (x == NULL) {
5223 Py_DECREF(l);
5224 return NULL;
5225 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005226 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005227 }
5228 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005229#endif
5230}
5231
5232#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005233
5234Py_ssize_t
5235_PyEval_RequestCodeExtraIndex(freefunc free)
5236{
Victor Stinnercaba55b2018-08-03 15:33:52 +02005237 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -07005238 Py_ssize_t new_index;
5239
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005240 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005241 return -1;
5242 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005243 new_index = interp->co_extra_user_count++;
5244 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005245 return new_index;
5246}
Łukasz Langaa785c872016-09-09 17:37:37 -07005247
5248static void
5249dtrace_function_entry(PyFrameObject *f)
5250{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005251 const char *filename;
5252 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005253 int lineno;
5254
5255 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5256 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5257 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5258
5259 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5260}
5261
5262static void
5263dtrace_function_return(PyFrameObject *f)
5264{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005265 const char *filename;
5266 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005267 int lineno;
5268
5269 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5270 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5271 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5272
5273 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5274}
5275
5276/* DTrace equivalent of maybe_call_line_trace. */
5277static void
5278maybe_dtrace_line(PyFrameObject *frame,
5279 int *instr_lb, int *instr_ub, int *instr_prev)
5280{
5281 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005282 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005283
5284 /* If the last instruction executed isn't in the current
5285 instruction window, reset the window.
5286 */
5287 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5288 PyAddrPair bounds;
5289 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5290 &bounds);
5291 *instr_lb = bounds.ap_lower;
5292 *instr_ub = bounds.ap_upper;
5293 }
5294 /* If the last instruction falls at the start of a line or if
5295 it represents a jump backwards, update the frame's line
5296 number and call the trace function. */
5297 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5298 frame->f_lineno = line;
5299 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5300 if (!co_filename)
5301 co_filename = "?";
5302 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5303 if (!co_name)
5304 co_name = "?";
5305 PyDTrace_LINE(co_filename, co_name, line);
5306 }
5307 *instr_prev = frame->f_lasti;
5308}