blob: 9a96dc7b37b96b3b400a786d815be6db2537d829 [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"
Eric Snow2ebc5ce2017-09-07 23:51:28 -060013#include "internal/pystate.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000014
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000015#include "code.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040016#include "dictobject.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000017#include "frameobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000018#include "opcode.h"
Łukasz Langaa785c872016-09-09 17:37:37 -070019#include "pydtrace.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040020#include "setobject.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000021#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000022
Guido van Rossumc6004111993-11-05 10:22:19 +000023#include <ctype.h>
24
Guido van Rossum408027e1996-12-30 16:17:54 +000025#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000026/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000027#define LLTRACE 1 /* Low-level trace feature */
28#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000029#endif
30
Yury Selivanovf2392132016-12-13 19:03:51 -050031/* Private API for the LOAD_METHOD opcode. */
32extern int _PyObject_GetMethod(PyObject *, PyObject *, PyObject **);
33
Jeremy Hylton52820442001-01-03 23:52:36 +000034typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000035
Guido van Rossum374a9221991-04-04 10:40:29 +000036/* Forward declarations */
Eric Snow2ebc5ce2017-09-07 23:51:28 -060037Py_LOCAL_INLINE(PyObject *) call_function(PyObject ***, Py_ssize_t,
38 PyObject *);
Victor Stinnerf9b760f2016-09-09 10:17:08 -070039static PyObject * do_call_core(PyObject *, PyObject *, PyObject *);
Jeremy Hylton52820442001-01-03 23:52:36 +000040
Guido van Rossum0a066c01992-03-27 17:29:15 +000041#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000042static int lltrace;
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020043static int prtrace(PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000044#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010045static int call_trace(Py_tracefunc, PyObject *,
46 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000048static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010049 PyThreadState *, PyFrameObject *,
50 int, PyObject *);
51static void call_exc_trace(Py_tracefunc, PyObject *,
52 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000053static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Eric Snow2ebc5ce2017-09-07 23:51:28 -060054 PyThreadState *, PyFrameObject *,
55 int *, int *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070056static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *);
57static void dtrace_function_entry(PyFrameObject *);
58static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000059
Thomas Wouters477c8d52006-05-27 19:21:47 +000060static PyObject * cmp_outcome(int, PyObject *, PyObject *);
Eric Snow2ebc5ce2017-09-07 23:51:28 -060061static PyObject * import_name(PyFrameObject *, PyObject *, PyObject *,
62 PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +000063static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +000064static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +000065static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +000066static void format_exc_unbound(PyCodeObject *co, int oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +020067static PyObject * unicode_concatenate(PyObject *, PyObject *,
Serhiy Storchakaab874002016-09-11 13:48:15 +030068 PyFrameObject *, const _Py_CODEUNIT *);
Benjamin Petersonce798522012-01-22 11:24:29 -050069static PyObject * special_lookup(PyObject *, _Py_Identifier *);
Serhiy Storchaka25e4f772017-08-03 11:37:15 +030070static int check_args_iterable(PyObject *func, PyObject *vararg);
71static void format_kwargs_mapping_error(PyObject *func, PyObject *kwargs);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +030072static void format_awaitable_error(PyTypeObject *, int);
Guido van Rossum374a9221991-04-04 10:40:29 +000073
Paul Prescode68140d2000-08-30 20:25:01 +000074#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000076#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000077 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000078#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 "free variable '%.200s' referenced before assignment" \
80 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000081
Guido van Rossum950361c1997-01-24 13:49:28 +000082/* Dynamic execution profile */
83#ifdef DYNAMIC_EXECUTION_PROFILE
84#ifdef DXPAIRS
85static long dxpairs[257][256];
86#define dxp dxpairs[256]
87#else
88static long dxp[256];
89#endif
90#endif
91
Eric Snow2ebc5ce2017-09-07 23:51:28 -060092#define GIL_REQUEST _Py_atomic_load_relaxed(&_PyRuntime.ceval.gil_drop_request)
Benjamin Petersond2be5b42010-09-10 22:47:02 +000093
Jeffrey Yasskin39370832010-05-03 19:29:34 +000094/* This can set eval_breaker to 0 even though gil_drop_request became
95 1. We believe this is all right because the eval loop will release
96 the GIL eventually anyway. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +000097#define COMPUTE_EVAL_BREAKER() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 _Py_atomic_store_relaxed( \
Eric Snow2ebc5ce2017-09-07 23:51:28 -060099 &_PyRuntime.ceval.eval_breaker, \
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000100 GIL_REQUEST | \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600101 _Py_atomic_load_relaxed(&_PyRuntime.ceval.pending.calls_to_do) | \
102 _PyRuntime.ceval.pending.async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000103
104#define SET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 do { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600106 _Py_atomic_store_relaxed(&_PyRuntime.ceval.gil_drop_request, 1); \
107 _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000109
110#define RESET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 do { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600112 _Py_atomic_store_relaxed(&_PyRuntime.ceval.gil_drop_request, 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 COMPUTE_EVAL_BREAKER(); \
114 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000115
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000116/* Pending calls are only modified under pending_lock */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000117#define SIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 do { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600119 _Py_atomic_store_relaxed(&_PyRuntime.ceval.pending.calls_to_do, 1); \
120 _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000122
123#define UNSIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 do { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600125 _Py_atomic_store_relaxed(&_PyRuntime.ceval.pending.calls_to_do, 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 COMPUTE_EVAL_BREAKER(); \
127 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000128
129#define SIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000130 do { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600131 _PyRuntime.ceval.pending.async_exc = 1; \
132 _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000134
135#define UNSIGNAL_ASYNC_EXC() \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600136 do { \
137 _PyRuntime.ceval.pending.async_exc = 0; \
138 COMPUTE_EVAL_BREAKER(); \
139 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000140
141
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000142#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000143#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000144#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000145#include "pythread.h"
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000146#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000147
Tim Peters7f468f22004-10-11 02:40:51 +0000148int
149PyEval_ThreadsInitialized(void)
150{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000152}
153
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000154void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000155PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000156{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000157 if (gil_created())
158 return;
159 create_gil();
160 take_gil(PyThreadState_GET());
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600161 _PyRuntime.ceval.pending.main_thread = PyThread_get_thread_ident();
162 if (!_PyRuntime.ceval.pending.lock)
163 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000164}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000165
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000166void
Antoine Pitrou1df15362010-09-13 14:16:46 +0000167_PyEval_FiniThreads(void)
168{
169 if (!gil_created())
170 return;
171 destroy_gil();
172 assert(!gil_created());
173}
174
175void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000176PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000177{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 PyThreadState *tstate = PyThreadState_GET();
179 if (tstate == NULL)
180 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
181 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000182}
183
184void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000185PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000186{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000187 /* This function must succeed when the current thread state is NULL.
188 We therefore avoid PyThreadState_GET() which dumps a fatal error
189 in debug mode.
190 */
191 drop_gil((PyThreadState*)_Py_atomic_load_relaxed(
192 &_PyThreadState_Current));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000193}
194
195void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000196PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000197{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 if (tstate == NULL)
199 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
200 /* Check someone has called PyEval_InitThreads() to create the lock */
201 assert(gil_created());
202 take_gil(tstate);
203 if (PyThreadState_Swap(tstate) != NULL)
204 Py_FatalError(
205 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000206}
207
208void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000209PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000210{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 if (tstate == NULL)
212 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
213 if (PyThreadState_Swap(NULL) != tstate)
214 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
215 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000216}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000217
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200218/* This function is called from PyOS_AfterFork_Child to destroy all threads
219 * which are not running in the child process, and clear internal locks
220 * which might be held by those threads.
221 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000222
223void
224PyEval_ReInitThreads(void)
225{
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200226 PyThreadState *current_tstate = PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000227
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 if (!gil_created())
229 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 recreate_gil();
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600231 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200232 take_gil(current_tstate);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600233 _PyRuntime.ceval.pending.main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000234
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200235 /* Destroy all threads except the current one */
236 _PyThreadState_DeleteExcept(current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000237}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000238
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000239/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600240 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000241
242void
243_PyEval_SignalAsyncExc(void)
244{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000246}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000247
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000248PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000249PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000250{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 PyThreadState *tstate = PyThreadState_Swap(NULL);
252 if (tstate == NULL)
253 Py_FatalError("PyEval_SaveThread: NULL tstate");
Victor Stinner2914bb32018-01-29 11:57:45 +0100254 assert(gil_created());
255 drop_gil(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000257}
258
259void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000260PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000261{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 if (tstate == NULL)
263 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Victor Stinner2914bb32018-01-29 11:57:45 +0100264 assert(gil_created());
265
266 int err = errno;
267 take_gil(tstate);
268 /* _Py_Finalizing is protected by the GIL */
269 if (_Py_IsFinalizing() && !_Py_CURRENTLY_FINALIZING(tstate)) {
270 drop_gil(tstate);
271 PyThread_exit_thread();
272 Py_UNREACHABLE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 }
Victor Stinner2914bb32018-01-29 11:57:45 +0100274 errno = err;
275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000277}
278
279
Guido van Rossuma9672091994-09-14 13:31:22 +0000280/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
281 signal handlers or Mac I/O completion routines) can schedule calls
282 to a function to be called synchronously.
283 The synchronous function is called with one void* argument.
284 It should return 0 for success or -1 for failure -- failure should
285 be accompanied by an exception.
286
287 If registry succeeds, the registry function returns 0; if it fails
288 (e.g. due to too many pending calls) it returns -1 (without setting
289 an exception condition).
290
291 Note that because registry may occur from within signal handlers,
292 or other asynchronous events, calling malloc() is unsafe!
293
Guido van Rossuma9672091994-09-14 13:31:22 +0000294 Any thread can schedule pending calls, but only the main thread
295 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000296 There is no facility to schedule calls to a particular thread, but
297 that should be easy to change, should that ever be required. In
298 that case, the static variables here should go into the python
299 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000300*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000301
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200302void
303_PyEval_SignalReceived(void)
304{
305 /* bpo-30703: Function called when the C signal handler of Python gets a
306 signal. We cannot queue a callback using Py_AddPendingCall() since
307 that function is not async-signal-safe. */
308 SIGNAL_PENDING_CALLS();
309}
310
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200311/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000312 scheduling to be made from any thread, and even from an executing
313 callback.
314 */
315
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000316int
317Py_AddPendingCall(int (*func)(void *), void *arg)
318{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000319 int i, j, result=0;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600320 PyThread_type_lock lock = _PyRuntime.ceval.pending.lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 /* try a few times for the lock. Since this mechanism is used
323 * for signal handling (on the main thread), there is a (slim)
324 * chance that a signal is delivered on the same thread while we
325 * hold the lock during the Py_MakePendingCalls() function.
326 * This avoids a deadlock in that case.
327 * Note that signals can be delivered on any thread. In particular,
328 * on Windows, a SIGINT is delivered on a system-created worker
329 * thread.
330 * We also check for lock being NULL, in the unlikely case that
331 * this function is called before any bytecode evaluation takes place.
332 */
333 if (lock != NULL) {
334 for (i = 0; i<100; i++) {
335 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
336 break;
337 }
338 if (i == 100)
339 return -1;
340 }
341
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600342 i = _PyRuntime.ceval.pending.last;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 j = (i + 1) % NPENDINGCALLS;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600344 if (j == _PyRuntime.ceval.pending.first) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 result = -1; /* Queue full */
346 } else {
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600347 _PyRuntime.ceval.pending.calls[i].func = func;
348 _PyRuntime.ceval.pending.calls[i].arg = arg;
349 _PyRuntime.ceval.pending.last = j;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 }
351 /* signal main loop */
352 SIGNAL_PENDING_CALLS();
353 if (lock != NULL)
354 PyThread_release_lock(lock);
355 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000356}
357
358int
359Py_MakePendingCalls(void)
360{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200361 static int busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 int i;
363 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000364
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200365 assert(PyGILState_Check());
366
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600367 if (!_PyRuntime.ceval.pending.lock) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 /* initial allocation of the lock */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600369 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
370 if (_PyRuntime.ceval.pending.lock == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 return -1;
372 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 /* only service pending calls on main thread */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600375 if (_PyRuntime.ceval.pending.main_thread &&
376 PyThread_get_thread_ident() != _PyRuntime.ceval.pending.main_thread)
377 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 return 0;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600379 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 /* don't perform recursive pending calls */
Charles-François Natalif23339a2011-07-23 18:15:43 +0200381 if (busy)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 return 0;
Charles-François Natalif23339a2011-07-23 18:15:43 +0200383 busy = 1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200384 /* unsignal before starting to call callbacks, so that any callback
385 added in-between re-signals */
386 UNSIGNAL_PENDING_CALLS();
387
388 /* Python signal handler doesn't really queue a callback: it only signals
389 that a signal was received, see _PyEval_SignalReceived(). */
390 if (PyErr_CheckSignals() < 0) {
391 goto error;
392 }
393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 /* perform a bounded number of calls, in case of recursion */
395 for (i=0; i<NPENDINGCALLS; i++) {
396 int j;
397 int (*func)(void *);
398 void *arg = NULL;
399
400 /* pop one item off the queue while holding the lock */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600401 PyThread_acquire_lock(_PyRuntime.ceval.pending.lock, WAIT_LOCK);
402 j = _PyRuntime.ceval.pending.first;
403 if (j == _PyRuntime.ceval.pending.last) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 func = NULL; /* Queue empty */
405 } else {
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600406 func = _PyRuntime.ceval.pending.calls[j].func;
407 arg = _PyRuntime.ceval.pending.calls[j].arg;
408 _PyRuntime.ceval.pending.first = (j + 1) % NPENDINGCALLS;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600410 PyThread_release_lock(_PyRuntime.ceval.pending.lock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 /* having released the lock, perform the callback */
412 if (func == NULL)
413 break;
414 r = func(arg);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200415 if (r) {
416 goto error;
417 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200419
Charles-François Natalif23339a2011-07-23 18:15:43 +0200420 busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 return r;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200422
423error:
424 busy = 0;
425 SIGNAL_PENDING_CALLS(); /* We're not done yet */
426 return -1;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000427}
428
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000429/* The interpreter's recursion limit */
430
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000431#ifndef Py_DEFAULT_RECURSION_LIMIT
432#define Py_DEFAULT_RECURSION_LIMIT 1000
433#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600434
Eric Snow05351c12017-09-05 21:43:08 -0700435int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000436
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600437void
438_PyEval_Initialize(struct _ceval_runtime_state *state)
439{
440 state->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
441 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
442 _gil_initialize(&state->gil);
443}
444
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000445int
446Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000447{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600448 return _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000449}
450
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000451void
452Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000453{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600454 _PyRuntime.ceval.recursion_limit = new_limit;
455 _Py_CheckRecursionLimit = _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000456}
457
Armin Rigo2b3eb402003-10-28 12:05:48 +0000458/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
459 if the recursion_depth reaches _Py_CheckRecursionLimit.
460 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
461 to guarantee that _Py_CheckRecursiveCall() is regularly called.
462 Without USE_STACKCHECK, there is no need for this. */
463int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300464_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000465{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 PyThreadState *tstate = PyThreadState_GET();
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600467 int recursion_limit = _PyRuntime.ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000468
469#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700470 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 if (PyOS_CheckStack()) {
472 --tstate->recursion_depth;
473 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
474 return -1;
475 }
pdox18967932017-10-25 23:03:01 -0700476 /* Needed for ABI backwards-compatibility (see bpo-31857) */
Eric Snow05351c12017-09-05 21:43:08 -0700477 _Py_CheckRecursionLimit = recursion_limit;
pdox18967932017-10-25 23:03:01 -0700478#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 if (tstate->recursion_critical)
480 /* Somebody asked that we don't check for recursion. */
481 return 0;
482 if (tstate->overflowed) {
483 if (tstate->recursion_depth > recursion_limit + 50) {
484 /* Overflowing while handling an overflow. Give up. */
485 Py_FatalError("Cannot recover from stack overflow.");
486 }
487 return 0;
488 }
489 if (tstate->recursion_depth > recursion_limit) {
490 --tstate->recursion_depth;
491 tstate->overflowed = 1;
Yury Selivanovf488fb42015-07-03 01:04:23 -0400492 PyErr_Format(PyExc_RecursionError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 "maximum recursion depth exceeded%s",
494 where);
495 return -1;
496 }
497 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000498}
499
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400500static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000501static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000502
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600503#define _Py_TracingPossible _PyRuntime.ceval.tracing_possible
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000504
Guido van Rossum374a9221991-04-04 10:40:29 +0000505
Guido van Rossumb209a111997-04-29 18:18:01 +0000506PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000507PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000508{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 return PyEval_EvalCodeEx(co,
510 globals, locals,
511 (PyObject **)NULL, 0,
512 (PyObject **)NULL, 0,
513 (PyObject **)NULL, 0,
514 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000515}
516
517
518/* Interpreter main loop */
519
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000520PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000521PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 /* This is for backward compatibility with extension modules that
523 used this API; core interpreter code should call
524 PyEval_EvalFrameEx() */
525 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000526}
527
528PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000529PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000530{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200531 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
532 return interp->eval_frame(f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700533}
534
Victor Stinnerc6944e72016-11-11 02:13:35 +0100535PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700536_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
537{
Guido van Rossum950361c1997-01-24 13:49:28 +0000538#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000540#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200541 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300542 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200543 int opcode; /* Current opcode */
544 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200545 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 PyObject *retval = NULL; /* Return value */
547 PyThreadState *tstate = PyThreadState_GET();
548 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 is true when the line being executed has changed. The
555 initial values are such as to make this false the first
556 time it is tested. */
557 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000558
Serhiy Storchakaab874002016-09-11 13:48:15 +0300559 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 PyObject *names;
561 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000562
Brett Cannon368b4b72012-04-02 12:17:59 -0400563#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200564 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400565#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200566
Antoine Pitroub52ec782009-01-25 16:34:23 +0000567/* Computed GOTOs, or
568 the-optimization-commonly-but-improperly-known-as-"threaded code"
569 using gcc's labels-as-values extension
570 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
571
572 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000574 combined with a lookup table of jump addresses. However, since the
575 indirect jump instruction is shared by all opcodes, the CPU will have a
576 hard time making the right prediction for where to jump next (actually,
577 it will be always wrong except in the uncommon case of a sequence of
578 several identical opcodes).
579
580 "Threaded code" in contrast, uses an explicit jump table and an explicit
581 indirect jump instruction at the end of each opcode. Since the jump
582 instruction is at a different address for each opcode, the CPU will make a
583 separate prediction for each of these instructions, which is equivalent to
584 predicting the second opcode of each opcode pair. These predictions have
585 a much better chance to turn out valid, especially in small bytecode loops.
586
587 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000589 and potentially many more instructions (depending on the pipeline width).
590 A correctly predicted branch, however, is nearly free.
591
592 At the time of this writing, the "threaded code" version is up to 15-20%
593 faster than the normal "switch" version, depending on the compiler and the
594 CPU architecture.
595
596 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
597 because it would render the measurements invalid.
598
599
600 NOTE: care must be taken that the compiler doesn't try to "optimize" the
601 indirect jumps by sharing them between all opcodes. Such optimizations
602 can be disabled on gcc by using the -fno-gcse flag (or possibly
603 -fno-crossjumping).
604*/
605
Antoine Pitrou042b1282010-08-13 21:15:58 +0000606#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000607#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000608#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000609#endif
610
Antoine Pitrou042b1282010-08-13 21:15:58 +0000611#ifdef HAVE_COMPUTED_GOTOS
612 #ifndef USE_COMPUTED_GOTOS
613 #define USE_COMPUTED_GOTOS 1
614 #endif
615#else
616 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
617 #error "Computed gotos are not supported on this compiler."
618 #endif
619 #undef USE_COMPUTED_GOTOS
620 #define USE_COMPUTED_GOTOS 0
621#endif
622
623#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000624/* Import the static jump table */
625#include "opcode_targets.h"
626
Antoine Pitroub52ec782009-01-25 16:34:23 +0000627#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -0700628 op: \
629 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000630
Antoine Pitroub52ec782009-01-25 16:34:23 +0000631#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600633 if (!_Py_atomic_load_relaxed(&_PyRuntime.ceval.eval_breaker)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 FAST_DISPATCH(); \
635 } \
636 continue; \
637 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000638
639#ifdef LLTRACE
640#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700642 if (!lltrace && !_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300644 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300645 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 } \
647 goto fast_next_opcode; \
648 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000649#else
650#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700652 if (!_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300654 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300655 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 } \
657 goto fast_next_opcode; \
658 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000659#endif
660
661#else
Benjamin Petersonddd19492018-09-16 22:38:02 -0700662#define TARGET(op) op
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300663
Antoine Pitroub52ec782009-01-25 16:34:23 +0000664#define DISPATCH() continue
665#define FAST_DISPATCH() goto fast_next_opcode
666#endif
667
668
Neal Norwitza81d2202002-07-14 00:27:26 +0000669/* Tuple access macros */
670
671#ifndef Py_DEBUG
672#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
673#else
674#define GETITEM(v, i) PyTuple_GetItem((v), (i))
675#endif
676
Guido van Rossum374a9221991-04-04 10:40:29 +0000677/* Code access macros */
678
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300679/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600680#define INSTR_OFFSET() \
681 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300682#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300683 _Py_CODEUNIT word = *next_instr; \
684 opcode = _Py_OPCODE(word); \
685 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300686 next_instr++; \
687 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300688#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
689#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000690
Raymond Hettingerf606f872003-03-16 03:11:04 +0000691/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 Some opcodes tend to come in pairs thus making it possible to
693 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300694 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000695
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 Verifying the prediction costs a single high-speed test of a register
697 variable against a constant. If the pairing was good, then the
698 processor's own internal branch predication has a high likelihood of
699 success, resulting in a nearly zero-overhead transition to the
700 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300701 including its unpredictable switch-case branch. Combined with the
702 processor's internal branch prediction, a successful PREDICT has the
703 effect of making the two opcodes run as if they were a single new opcode
704 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000705
Georg Brandl86b2fb92008-07-16 03:43:04 +0000706 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 predictions turned-on and interpret the results as if some opcodes
708 had been combined or turn-off predictions so that the opcode frequency
709 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000710
711 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 the CPU to record separate branch prediction information for each
713 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000714
Raymond Hettingerf606f872003-03-16 03:11:04 +0000715*/
716
Antoine Pitrou042b1282010-08-13 21:15:58 +0000717#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000719#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300720#define PREDICT(op) \
721 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300722 _Py_CODEUNIT word = *next_instr; \
723 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300724 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300725 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300726 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300727 goto PRED_##op; \
728 } \
729 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000730#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300731#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000732
Raymond Hettingerf606f872003-03-16 03:11:04 +0000733
Guido van Rossum374a9221991-04-04 10:40:29 +0000734/* Stack manipulation macros */
735
Martin v. Löwis18e16552006-02-15 17:27:45 +0000736/* The stack can grow at most MAXINT deep, as co_nlocals and
737 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000738#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
739#define EMPTY() (STACK_LEVEL() == 0)
740#define TOP() (stack_pointer[-1])
741#define SECOND() (stack_pointer[-2])
742#define THIRD() (stack_pointer[-3])
743#define FOURTH() (stack_pointer[-4])
744#define PEEK(n) (stack_pointer[-(n)])
745#define SET_TOP(v) (stack_pointer[-1] = (v))
746#define SET_SECOND(v) (stack_pointer[-2] = (v))
747#define SET_THIRD(v) (stack_pointer[-3] = (v))
748#define SET_FOURTH(v) (stack_pointer[-4] = (v))
749#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
750#define BASIC_STACKADJ(n) (stack_pointer += n)
751#define BASIC_PUSH(v) (*stack_pointer++ = (v))
752#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000753
Guido van Rossum96a42c81992-01-12 02:29:51 +0000754#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000756 lltrace && prtrace(TOP(), "push")); \
757 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000759 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +0000760#define STACK_GROW(n) do { \
761 assert(n >= 0); \
762 (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000763 lltrace && prtrace(TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +0000764 assert(STACK_LEVEL() <= co->co_stacksize); \
765 } while (0)
766#define STACK_SHRINK(n) do { \
767 assert(n >= 0); \
768 (void)(lltrace && prtrace(TOP(), "stackadj")); \
769 (void)(BASIC_STACKADJ(-n)); \
770 assert(STACK_LEVEL() <= co->co_stacksize); \
771 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +0000772#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +0000773 prtrace((STACK_POINTER)[-1], "ext_pop")), \
774 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000775#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000776#define PUSH(v) BASIC_PUSH(v)
777#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +0000778#define STACK_GROW(n) BASIC_STACKADJ(n)
779#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000780#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000781#endif
782
Guido van Rossum681d79a1995-07-18 14:51:37 +0000783/* Local variable macros */
784
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000786
787/* The SETLOCAL() macro must not DECREF the local variable in-place and
788 then store the new value; it must copy the old value to a temporary
789 value, then store the new value, and then DECREF the temporary value.
790 This is because it is possible that during the DECREF the frame is
791 accessed by other code (e.g. a __del__ method or gc.collect()) and the
792 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000794 GETLOCAL(i) = value; \
795 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000796
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000797
798#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 while (STACK_LEVEL() > (b)->b_level) { \
800 PyObject *v = POP(); \
801 Py_XDECREF(v); \
802 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000803
804#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300805 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +0100807 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 assert(STACK_LEVEL() >= (b)->b_level + 3); \
809 while (STACK_LEVEL() > (b)->b_level + 3) { \
810 value = POP(); \
811 Py_XDECREF(value); \
812 } \
Mark Shannonae3087c2017-10-22 22:41:51 +0100813 exc_info = tstate->exc_info; \
814 type = exc_info->exc_type; \
815 value = exc_info->exc_value; \
816 traceback = exc_info->exc_traceback; \
817 exc_info->exc_type = POP(); \
818 exc_info->exc_value = POP(); \
819 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 Py_XDECREF(type); \
821 Py_XDECREF(value); \
822 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300823 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000824
Guido van Rossuma027efa1997-05-05 20:56:21 +0000825/* Start of code */
826
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 /* push frame */
828 if (Py_EnterRecursiveCall(""))
829 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000830
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000832
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 if (tstate->use_tracing) {
834 if (tstate->c_tracefunc != NULL) {
835 /* tstate->c_tracefunc, if defined, is a
836 function that will be called on *every* entry
837 to a code block. Its return value, if not
838 None, is a function that will be called at
839 the start of each executed line of code.
840 (Actually, the function must return itself
841 in order to continue tracing.) The trace
842 functions are called with three arguments:
843 a pointer to the current frame, a string
844 indicating why the function is called, and
845 an argument which depends on the situation.
846 The global trace function is also called
847 whenever an exception is detected. */
848 if (call_trace_protected(tstate->c_tracefunc,
849 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100850 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 /* Trace function raised an error */
852 goto exit_eval_frame;
853 }
854 }
855 if (tstate->c_profilefunc != NULL) {
856 /* Similar for c_profilefunc, except it needn't
857 return itself and isn't called for "line" events */
858 if (call_trace_protected(tstate->c_profilefunc,
859 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100860 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 /* Profile function raised an error */
862 goto exit_eval_frame;
863 }
864 }
865 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000866
Łukasz Langaa785c872016-09-09 17:37:37 -0700867 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
868 dtrace_function_entry(f);
869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 co = f->f_code;
871 names = co->co_names;
872 consts = co->co_consts;
873 fastlocals = f->f_localsplus;
874 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300875 assert(PyBytes_Check(co->co_code));
876 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +0300877 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
878 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
879 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300880 /*
881 f->f_lasti refers to the index of the last instruction,
882 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000883
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300884 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500885 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000886
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 When the PREDICT() macros are enabled, some opcode pairs follow in
888 direct succession without updating f->f_lasti. A successful
889 prediction effectively links the two codes together as if they
890 were a single new opcode; accordingly,f->f_lasti will point to
891 the first code in the pair (for instance, GET_ITER followed by
892 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300893 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300895 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300896 next_instr = first_instr;
897 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +0300898 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
899 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300900 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 stack_pointer = f->f_stacktop;
902 assert(stack_pointer != NULL);
903 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +0200904 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000905
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000906
Tim Peters5ca576e2001-06-18 22:08:13 +0000907#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200908 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000909#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000910
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400911 if (throwflag) /* support for generator.throw() */
912 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000913
Victor Stinnerace47d72013-07-18 01:41:08 +0200914#ifdef Py_DEBUG
915 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +0100916 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +0000917 caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +0200918 assert(!PyErr_Occurred());
919#endif
920
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200921main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 assert(stack_pointer >= f->f_valuestack); /* else underflow */
924 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +0200925 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 /* Do periodic things. Doing this every time through
928 the loop would add too much overhead, so we do it
929 only every Nth instruction. We also do it if
930 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
931 event needs attention (e.g. a signal handler or
932 async I/O handler); see Py_AddPendingCall() and
933 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000934
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600935 if (_Py_atomic_load_relaxed(&_PyRuntime.ceval.eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +0300936 opcode = _Py_OPCODE(*next_instr);
937 if (opcode == SETUP_FINALLY ||
938 opcode == SETUP_WITH ||
939 opcode == BEFORE_ASYNC_WITH ||
940 opcode == YIELD_FROM) {
941 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -0700942 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +0300943 - If we're about to enter the 'with:'. It will prevent
944 emitting a resource warning in the common idiom
945 'with open(path) as file:'.
946 - If we're about to enter the 'async with:'.
947 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -0700948 *very* useful, but might help in some cases and it's
949 traditional)
950 - If we're resuming a chain of nested 'yield from' or
951 'await' calls, then each frame is parked with YIELD_FROM
952 as its next opcode. If the user hit control-C we want to
953 wait until we've reached the innermost frame before
954 running the signal handler and raising KeyboardInterrupt
955 (see bpo-30039).
956 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 goto fast_next_opcode;
958 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600959 if (_Py_atomic_load_relaxed(
960 &_PyRuntime.ceval.pending.calls_to_do))
961 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400962 if (Py_MakePendingCalls() < 0)
963 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600965 if (_Py_atomic_load_relaxed(
966 &_PyRuntime.ceval.gil_drop_request))
967 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 /* Give another thread a chance */
969 if (PyThreadState_Swap(NULL) != tstate)
970 Py_FatalError("ceval: tstate mix-up");
971 drop_gil(tstate);
972
973 /* Other threads may run now */
974
975 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -0700976
977 /* Check if we should make a quick exit. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600978 if (_Py_IsFinalizing() &&
979 !_Py_CURRENTLY_FINALIZING(tstate))
980 {
Benjamin Peterson17548dd2014-06-16 22:59:07 -0700981 drop_gil(tstate);
982 PyThread_exit_thread();
983 }
984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 if (PyThreadState_Swap(tstate) != NULL)
986 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 }
988 /* Check for asynchronous exceptions. */
989 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400990 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 tstate->async_exc = NULL;
992 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400993 PyErr_SetNone(exc);
994 Py_DECREF(exc);
995 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 }
997 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000998
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 fast_next_opcode:
1000 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001001
Łukasz Langaa785c872016-09-09 17:37:37 -07001002 if (PyDTrace_LINE_ENABLED())
1003 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1004
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001008 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001009 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 /* see maybe_call_line_trace
1011 for expository comments */
1012 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001013
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 err = maybe_call_line_trace(tstate->c_tracefunc,
1015 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001016 tstate, f,
1017 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 /* Reload possibly changed frame fields */
1019 JUMPTO(f->f_lasti);
1020 if (f->f_stacktop != NULL) {
1021 stack_pointer = f->f_stacktop;
1022 f->f_stacktop = NULL;
1023 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001024 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001026 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001028
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001030
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001031 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001032 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001033#ifdef DYNAMIC_EXECUTION_PROFILE
1034#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 dxpairs[lastopcode][opcode]++;
1036 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001037#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001039#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001040
Guido van Rossum96a42c81992-01-12 02:29:51 +00001041#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001043
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 if (lltrace) {
1045 if (HAS_ARG(opcode)) {
1046 printf("%d: %d, %d\n",
1047 f->f_lasti, opcode, oparg);
1048 }
1049 else {
1050 printf("%d: %d\n",
1051 f->f_lasti, opcode);
1052 }
1053 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001054#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001059 It is essential that any operation that fails must goto error
1060 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001061
Benjamin Petersonddd19492018-09-16 22:38:02 -07001062 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001064 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001065
Benjamin Petersonddd19492018-09-16 22:38:02 -07001066 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001067 PyObject *value = GETLOCAL(oparg);
1068 if (value == NULL) {
1069 format_exc_check_arg(PyExc_UnboundLocalError,
1070 UNBOUNDLOCAL_ERROR_MSG,
1071 PyTuple_GetItem(co->co_varnames, oparg));
1072 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001074 Py_INCREF(value);
1075 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001077 }
1078
Benjamin Petersonddd19492018-09-16 22:38:02 -07001079 case TARGET(LOAD_CONST): {
1080 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001081 PyObject *value = GETITEM(consts, oparg);
1082 Py_INCREF(value);
1083 PUSH(value);
1084 FAST_DISPATCH();
1085 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001086
Benjamin Petersonddd19492018-09-16 22:38:02 -07001087 case TARGET(STORE_FAST): {
1088 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001089 PyObject *value = POP();
1090 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001092 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001093
Benjamin Petersonddd19492018-09-16 22:38:02 -07001094 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001095 PyObject *value = POP();
1096 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001098 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001099
Benjamin Petersonddd19492018-09-16 22:38:02 -07001100 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001101 PyObject *top = TOP();
1102 PyObject *second = SECOND();
1103 SET_TOP(second);
1104 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001106 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001107
Benjamin Petersonddd19492018-09-16 22:38:02 -07001108 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001109 PyObject *top = TOP();
1110 PyObject *second = SECOND();
1111 PyObject *third = THIRD();
1112 SET_TOP(second);
1113 SET_SECOND(third);
1114 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001116 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001117
Benjamin Petersonddd19492018-09-16 22:38:02 -07001118 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001119 PyObject *top = TOP();
1120 PyObject *second = SECOND();
1121 PyObject *third = THIRD();
1122 PyObject *fourth = FOURTH();
1123 SET_TOP(second);
1124 SET_SECOND(third);
1125 SET_THIRD(fourth);
1126 SET_FOURTH(top);
1127 FAST_DISPATCH();
1128 }
1129
Benjamin Petersonddd19492018-09-16 22:38:02 -07001130 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001131 PyObject *top = TOP();
1132 Py_INCREF(top);
1133 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001134 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001135 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001136
Benjamin Petersonddd19492018-09-16 22:38:02 -07001137 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001138 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001139 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001140 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001141 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001142 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001143 SET_TOP(top);
1144 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001145 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001146 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001147
Benjamin Petersonddd19492018-09-16 22:38:02 -07001148 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001149 PyObject *value = TOP();
1150 PyObject *res = PyNumber_Positive(value);
1151 Py_DECREF(value);
1152 SET_TOP(res);
1153 if (res == NULL)
1154 goto error;
1155 DISPATCH();
1156 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001157
Benjamin Petersonddd19492018-09-16 22:38:02 -07001158 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001159 PyObject *value = TOP();
1160 PyObject *res = PyNumber_Negative(value);
1161 Py_DECREF(value);
1162 SET_TOP(res);
1163 if (res == NULL)
1164 goto error;
1165 DISPATCH();
1166 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001167
Benjamin Petersonddd19492018-09-16 22:38:02 -07001168 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001169 PyObject *value = TOP();
1170 int err = PyObject_IsTrue(value);
1171 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 if (err == 0) {
1173 Py_INCREF(Py_True);
1174 SET_TOP(Py_True);
1175 DISPATCH();
1176 }
1177 else if (err > 0) {
1178 Py_INCREF(Py_False);
1179 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 DISPATCH();
1181 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001182 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001183 goto error;
1184 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001185
Benjamin Petersonddd19492018-09-16 22:38:02 -07001186 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001187 PyObject *value = TOP();
1188 PyObject *res = PyNumber_Invert(value);
1189 Py_DECREF(value);
1190 SET_TOP(res);
1191 if (res == NULL)
1192 goto error;
1193 DISPATCH();
1194 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001195
Benjamin Petersonddd19492018-09-16 22:38:02 -07001196 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001197 PyObject *exp = POP();
1198 PyObject *base = TOP();
1199 PyObject *res = PyNumber_Power(base, exp, Py_None);
1200 Py_DECREF(base);
1201 Py_DECREF(exp);
1202 SET_TOP(res);
1203 if (res == NULL)
1204 goto error;
1205 DISPATCH();
1206 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001207
Benjamin Petersonddd19492018-09-16 22:38:02 -07001208 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001209 PyObject *right = POP();
1210 PyObject *left = TOP();
1211 PyObject *res = PyNumber_Multiply(left, right);
1212 Py_DECREF(left);
1213 Py_DECREF(right);
1214 SET_TOP(res);
1215 if (res == NULL)
1216 goto error;
1217 DISPATCH();
1218 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001219
Benjamin Petersonddd19492018-09-16 22:38:02 -07001220 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001221 PyObject *right = POP();
1222 PyObject *left = TOP();
1223 PyObject *res = PyNumber_MatrixMultiply(left, right);
1224 Py_DECREF(left);
1225 Py_DECREF(right);
1226 SET_TOP(res);
1227 if (res == NULL)
1228 goto error;
1229 DISPATCH();
1230 }
1231
Benjamin Petersonddd19492018-09-16 22:38:02 -07001232 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001233 PyObject *divisor = POP();
1234 PyObject *dividend = TOP();
1235 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1236 Py_DECREF(dividend);
1237 Py_DECREF(divisor);
1238 SET_TOP(quotient);
1239 if (quotient == NULL)
1240 goto error;
1241 DISPATCH();
1242 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001243
Benjamin Petersonddd19492018-09-16 22:38:02 -07001244 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001245 PyObject *divisor = POP();
1246 PyObject *dividend = TOP();
1247 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1248 Py_DECREF(dividend);
1249 Py_DECREF(divisor);
1250 SET_TOP(quotient);
1251 if (quotient == NULL)
1252 goto error;
1253 DISPATCH();
1254 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001255
Benjamin Petersonddd19492018-09-16 22:38:02 -07001256 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001257 PyObject *divisor = POP();
1258 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001259 PyObject *res;
1260 if (PyUnicode_CheckExact(dividend) && (
1261 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1262 // fast path; string formatting, but not if the RHS is a str subclass
1263 // (see issue28598)
1264 res = PyUnicode_Format(dividend, divisor);
1265 } else {
1266 res = PyNumber_Remainder(dividend, divisor);
1267 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001268 Py_DECREF(divisor);
1269 Py_DECREF(dividend);
1270 SET_TOP(res);
1271 if (res == NULL)
1272 goto error;
1273 DISPATCH();
1274 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001275
Benjamin Petersonddd19492018-09-16 22:38:02 -07001276 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001277 PyObject *right = POP();
1278 PyObject *left = TOP();
1279 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001280 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1281 CPython using bytecode, it is simply worthless.
1282 See http://bugs.python.org/issue21955 and
1283 http://bugs.python.org/issue10044 for the discussion. In short,
1284 no patch shown any impact on a realistic benchmark, only a minor
1285 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001286 if (PyUnicode_CheckExact(left) &&
1287 PyUnicode_CheckExact(right)) {
1288 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001289 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001290 }
1291 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001292 sum = PyNumber_Add(left, right);
1293 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001294 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001295 Py_DECREF(right);
1296 SET_TOP(sum);
1297 if (sum == NULL)
1298 goto error;
1299 DISPATCH();
1300 }
1301
Benjamin Petersonddd19492018-09-16 22:38:02 -07001302 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001303 PyObject *right = POP();
1304 PyObject *left = TOP();
1305 PyObject *diff = PyNumber_Subtract(left, right);
1306 Py_DECREF(right);
1307 Py_DECREF(left);
1308 SET_TOP(diff);
1309 if (diff == NULL)
1310 goto error;
1311 DISPATCH();
1312 }
1313
Benjamin Petersonddd19492018-09-16 22:38:02 -07001314 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001315 PyObject *sub = POP();
1316 PyObject *container = TOP();
1317 PyObject *res = PyObject_GetItem(container, sub);
1318 Py_DECREF(container);
1319 Py_DECREF(sub);
1320 SET_TOP(res);
1321 if (res == NULL)
1322 goto error;
1323 DISPATCH();
1324 }
1325
Benjamin Petersonddd19492018-09-16 22:38:02 -07001326 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001327 PyObject *right = POP();
1328 PyObject *left = TOP();
1329 PyObject *res = PyNumber_Lshift(left, right);
1330 Py_DECREF(left);
1331 Py_DECREF(right);
1332 SET_TOP(res);
1333 if (res == NULL)
1334 goto error;
1335 DISPATCH();
1336 }
1337
Benjamin Petersonddd19492018-09-16 22:38:02 -07001338 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001339 PyObject *right = POP();
1340 PyObject *left = TOP();
1341 PyObject *res = PyNumber_Rshift(left, right);
1342 Py_DECREF(left);
1343 Py_DECREF(right);
1344 SET_TOP(res);
1345 if (res == NULL)
1346 goto error;
1347 DISPATCH();
1348 }
1349
Benjamin Petersonddd19492018-09-16 22:38:02 -07001350 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001351 PyObject *right = POP();
1352 PyObject *left = TOP();
1353 PyObject *res = PyNumber_And(left, right);
1354 Py_DECREF(left);
1355 Py_DECREF(right);
1356 SET_TOP(res);
1357 if (res == NULL)
1358 goto error;
1359 DISPATCH();
1360 }
1361
Benjamin Petersonddd19492018-09-16 22:38:02 -07001362 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001363 PyObject *right = POP();
1364 PyObject *left = TOP();
1365 PyObject *res = PyNumber_Xor(left, right);
1366 Py_DECREF(left);
1367 Py_DECREF(right);
1368 SET_TOP(res);
1369 if (res == NULL)
1370 goto error;
1371 DISPATCH();
1372 }
1373
Benjamin Petersonddd19492018-09-16 22:38:02 -07001374 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001375 PyObject *right = POP();
1376 PyObject *left = TOP();
1377 PyObject *res = PyNumber_Or(left, right);
1378 Py_DECREF(left);
1379 Py_DECREF(right);
1380 SET_TOP(res);
1381 if (res == NULL)
1382 goto error;
1383 DISPATCH();
1384 }
1385
Benjamin Petersonddd19492018-09-16 22:38:02 -07001386 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001387 PyObject *v = POP();
1388 PyObject *list = PEEK(oparg);
1389 int err;
1390 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001392 if (err != 0)
1393 goto error;
1394 PREDICT(JUMP_ABSOLUTE);
1395 DISPATCH();
1396 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001397
Benjamin Petersonddd19492018-09-16 22:38:02 -07001398 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001399 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001400 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001401 int err;
1402 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001404 if (err != 0)
1405 goto error;
1406 PREDICT(JUMP_ABSOLUTE);
1407 DISPATCH();
1408 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001409
Benjamin Petersonddd19492018-09-16 22:38:02 -07001410 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001411 PyObject *exp = POP();
1412 PyObject *base = TOP();
1413 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1414 Py_DECREF(base);
1415 Py_DECREF(exp);
1416 SET_TOP(res);
1417 if (res == NULL)
1418 goto error;
1419 DISPATCH();
1420 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001421
Benjamin Petersonddd19492018-09-16 22:38:02 -07001422 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001423 PyObject *right = POP();
1424 PyObject *left = TOP();
1425 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1426 Py_DECREF(left);
1427 Py_DECREF(right);
1428 SET_TOP(res);
1429 if (res == NULL)
1430 goto error;
1431 DISPATCH();
1432 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001433
Benjamin Petersonddd19492018-09-16 22:38:02 -07001434 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001435 PyObject *right = POP();
1436 PyObject *left = TOP();
1437 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1438 Py_DECREF(left);
1439 Py_DECREF(right);
1440 SET_TOP(res);
1441 if (res == NULL)
1442 goto error;
1443 DISPATCH();
1444 }
1445
Benjamin Petersonddd19492018-09-16 22:38:02 -07001446 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001447 PyObject *divisor = POP();
1448 PyObject *dividend = TOP();
1449 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1450 Py_DECREF(dividend);
1451 Py_DECREF(divisor);
1452 SET_TOP(quotient);
1453 if (quotient == NULL)
1454 goto error;
1455 DISPATCH();
1456 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001457
Benjamin Petersonddd19492018-09-16 22:38:02 -07001458 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001459 PyObject *divisor = POP();
1460 PyObject *dividend = TOP();
1461 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1462 Py_DECREF(dividend);
1463 Py_DECREF(divisor);
1464 SET_TOP(quotient);
1465 if (quotient == NULL)
1466 goto error;
1467 DISPATCH();
1468 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001469
Benjamin Petersonddd19492018-09-16 22:38:02 -07001470 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001471 PyObject *right = POP();
1472 PyObject *left = TOP();
1473 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1474 Py_DECREF(left);
1475 Py_DECREF(right);
1476 SET_TOP(mod);
1477 if (mod == NULL)
1478 goto error;
1479 DISPATCH();
1480 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001481
Benjamin Petersonddd19492018-09-16 22:38:02 -07001482 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001483 PyObject *right = POP();
1484 PyObject *left = TOP();
1485 PyObject *sum;
1486 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1487 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001488 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001489 }
1490 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001491 sum = PyNumber_InPlaceAdd(left, right);
1492 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001493 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001494 Py_DECREF(right);
1495 SET_TOP(sum);
1496 if (sum == 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_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001502 PyObject *right = POP();
1503 PyObject *left = TOP();
1504 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1505 Py_DECREF(left);
1506 Py_DECREF(right);
1507 SET_TOP(diff);
1508 if (diff == NULL)
1509 goto error;
1510 DISPATCH();
1511 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001512
Benjamin Petersonddd19492018-09-16 22:38:02 -07001513 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001514 PyObject *right = POP();
1515 PyObject *left = TOP();
1516 PyObject *res = PyNumber_InPlaceLshift(left, right);
1517 Py_DECREF(left);
1518 Py_DECREF(right);
1519 SET_TOP(res);
1520 if (res == 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_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001526 PyObject *right = POP();
1527 PyObject *left = TOP();
1528 PyObject *res = PyNumber_InPlaceRshift(left, right);
1529 Py_DECREF(left);
1530 Py_DECREF(right);
1531 SET_TOP(res);
1532 if (res == 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_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001538 PyObject *right = POP();
1539 PyObject *left = TOP();
1540 PyObject *res = PyNumber_InPlaceAnd(left, right);
1541 Py_DECREF(left);
1542 Py_DECREF(right);
1543 SET_TOP(res);
1544 if (res == 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_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001550 PyObject *right = POP();
1551 PyObject *left = TOP();
1552 PyObject *res = PyNumber_InPlaceXor(left, right);
1553 Py_DECREF(left);
1554 Py_DECREF(right);
1555 SET_TOP(res);
1556 if (res == NULL)
1557 goto error;
1558 DISPATCH();
1559 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001560
Benjamin Petersonddd19492018-09-16 22:38:02 -07001561 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001562 PyObject *right = POP();
1563 PyObject *left = TOP();
1564 PyObject *res = PyNumber_InPlaceOr(left, right);
1565 Py_DECREF(left);
1566 Py_DECREF(right);
1567 SET_TOP(res);
1568 if (res == NULL)
1569 goto error;
1570 DISPATCH();
1571 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001572
Benjamin Petersonddd19492018-09-16 22:38:02 -07001573 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001574 PyObject *sub = TOP();
1575 PyObject *container = SECOND();
1576 PyObject *v = THIRD();
1577 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001578 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001579 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001580 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001582 Py_DECREF(container);
1583 Py_DECREF(sub);
1584 if (err != 0)
1585 goto error;
1586 DISPATCH();
1587 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001588
Benjamin Petersonddd19492018-09-16 22:38:02 -07001589 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001590 PyObject *sub = TOP();
1591 PyObject *container = SECOND();
1592 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001593 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00001594 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001595 err = PyObject_DelItem(container, sub);
1596 Py_DECREF(container);
1597 Py_DECREF(sub);
1598 if (err != 0)
1599 goto error;
1600 DISPATCH();
1601 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001602
Benjamin Petersonddd19492018-09-16 22:38:02 -07001603 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01001604 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001605 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001606 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001607 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001608 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001609 PyErr_SetString(PyExc_RuntimeError,
1610 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001611 Py_DECREF(value);
1612 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001614 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001615 Py_DECREF(value);
1616 if (res == NULL)
1617 goto error;
1618 Py_DECREF(res);
1619 DISPATCH();
1620 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001621
Benjamin Petersonddd19492018-09-16 22:38:02 -07001622 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001623 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 switch (oparg) {
1625 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001626 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001627 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001628 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001629 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001630 /* fall through */
1631 case 0:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001632 if (do_raise(exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001633 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001634 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001635 break;
1636 default:
1637 PyErr_SetString(PyExc_SystemError,
1638 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 break;
1640 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001641 goto error;
1642 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001643
Benjamin Petersonddd19492018-09-16 22:38:02 -07001644 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001645 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001646 assert(f->f_iblock == 0);
1647 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001648 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001649
Benjamin Petersonddd19492018-09-16 22:38:02 -07001650 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001651 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001652 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001653 PyObject *obj = TOP();
1654 PyTypeObject *type = Py_TYPE(obj);
1655
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001656 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001657 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001658 }
Yury Selivanov75445082015-05-11 22:57:16 -04001659
1660 if (getter != NULL) {
1661 iter = (*getter)(obj);
1662 Py_DECREF(obj);
1663 if (iter == NULL) {
1664 SET_TOP(NULL);
1665 goto error;
1666 }
1667 }
1668 else {
1669 SET_TOP(NULL);
1670 PyErr_Format(
1671 PyExc_TypeError,
1672 "'async for' requires an object with "
1673 "__aiter__ method, got %.100s",
1674 type->tp_name);
1675 Py_DECREF(obj);
1676 goto error;
1677 }
1678
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001679 if (Py_TYPE(iter)->tp_as_async == NULL ||
1680 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001681
Yury Selivanov398ff912017-03-02 22:20:00 -05001682 SET_TOP(NULL);
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001683 PyErr_Format(
1684 PyExc_TypeError,
1685 "'async for' received an object from __aiter__ "
1686 "that does not implement __anext__: %.100s",
1687 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001688 Py_DECREF(iter);
1689 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001690 }
1691
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001692 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001693 DISPATCH();
1694 }
1695
Benjamin Petersonddd19492018-09-16 22:38:02 -07001696 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001697 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001698 PyObject *next_iter = NULL;
1699 PyObject *awaitable = NULL;
1700 PyObject *aiter = TOP();
1701 PyTypeObject *type = Py_TYPE(aiter);
1702
Yury Selivanoveb636452016-09-08 22:01:51 -07001703 if (PyAsyncGen_CheckExact(aiter)) {
1704 awaitable = type->tp_as_async->am_anext(aiter);
1705 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001706 goto error;
1707 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001708 } else {
1709 if (type->tp_as_async != NULL){
1710 getter = type->tp_as_async->am_anext;
1711 }
Yury Selivanov75445082015-05-11 22:57:16 -04001712
Yury Selivanoveb636452016-09-08 22:01:51 -07001713 if (getter != NULL) {
1714 next_iter = (*getter)(aiter);
1715 if (next_iter == NULL) {
1716 goto error;
1717 }
1718 }
1719 else {
1720 PyErr_Format(
1721 PyExc_TypeError,
1722 "'async for' requires an iterator with "
1723 "__anext__ method, got %.100s",
1724 type->tp_name);
1725 goto error;
1726 }
Yury Selivanov75445082015-05-11 22:57:16 -04001727
Yury Selivanoveb636452016-09-08 22:01:51 -07001728 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1729 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001730 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001731 PyExc_TypeError,
1732 "'async for' received an invalid object "
1733 "from __anext__: %.100s",
1734 Py_TYPE(next_iter)->tp_name);
1735
1736 Py_DECREF(next_iter);
1737 goto error;
1738 } else {
1739 Py_DECREF(next_iter);
1740 }
1741 }
Yury Selivanov75445082015-05-11 22:57:16 -04001742
1743 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001744 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001745 DISPATCH();
1746 }
1747
Benjamin Petersonddd19492018-09-16 22:38:02 -07001748 case TARGET(GET_AWAITABLE): {
1749 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04001750 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04001751 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04001752
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03001753 if (iter == NULL) {
1754 format_awaitable_error(Py_TYPE(iterable),
1755 _Py_OPCODE(next_instr[-2]));
1756 }
1757
Yury Selivanov75445082015-05-11 22:57:16 -04001758 Py_DECREF(iterable);
1759
Yury Selivanovc724bae2016-03-02 11:30:46 -05001760 if (iter != NULL && PyCoro_CheckExact(iter)) {
1761 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
1762 if (yf != NULL) {
1763 /* `iter` is a coroutine object that is being
1764 awaited, `yf` is a pointer to the current awaitable
1765 being awaited on. */
1766 Py_DECREF(yf);
1767 Py_CLEAR(iter);
1768 PyErr_SetString(
1769 PyExc_RuntimeError,
1770 "coroutine is being awaited already");
1771 /* The code below jumps to `error` if `iter` is NULL. */
1772 }
1773 }
1774
Yury Selivanov75445082015-05-11 22:57:16 -04001775 SET_TOP(iter); /* Even if it's NULL */
1776
1777 if (iter == NULL) {
1778 goto error;
1779 }
1780
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001781 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001782 DISPATCH();
1783 }
1784
Benjamin Petersonddd19492018-09-16 22:38:02 -07001785 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001786 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001787 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001788 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001789 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
1790 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001791 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001792 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001793 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001794 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001795 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001796 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001797 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001798 Py_DECREF(v);
1799 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001800 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08001801 if (tstate->c_tracefunc != NULL
1802 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001803 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10001804 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001805 if (err < 0)
1806 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001807 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001808 SET_TOP(val);
1809 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001810 }
Martin Panter95f53c12016-07-18 08:23:26 +00001811 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001812 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001813 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01001814 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03001815 f->f_lasti -= sizeof(_Py_CODEUNIT);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001816 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001817 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001818
Benjamin Petersonddd19492018-09-16 22:38:02 -07001819 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001820 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07001821
1822 if (co->co_flags & CO_ASYNC_GENERATOR) {
1823 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
1824 Py_DECREF(retval);
1825 if (w == NULL) {
1826 retval = NULL;
1827 goto error;
1828 }
1829 retval = w;
1830 }
1831
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001832 f->f_stacktop = stack_pointer;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001833 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001834 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001835
Benjamin Petersonddd19492018-09-16 22:38:02 -07001836 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001837 PyObject *type, *value, *traceback;
1838 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001839 PyTryBlock *b = PyFrame_BlockPop(f);
1840 if (b->b_type != EXCEPT_HANDLER) {
1841 PyErr_SetString(PyExc_SystemError,
1842 "popped block is not an except handler");
1843 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001844 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001845 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
1846 STACK_LEVEL() <= (b)->b_level + 4);
1847 exc_info = tstate->exc_info;
1848 type = exc_info->exc_type;
1849 value = exc_info->exc_value;
1850 traceback = exc_info->exc_traceback;
1851 exc_info->exc_type = POP();
1852 exc_info->exc_value = POP();
1853 exc_info->exc_traceback = POP();
1854 Py_XDECREF(type);
1855 Py_XDECREF(value);
1856 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001857 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001858 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001859
Benjamin Petersonddd19492018-09-16 22:38:02 -07001860 case TARGET(POP_BLOCK): {
1861 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001862 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001863 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001864 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001865
Benjamin Petersonddd19492018-09-16 22:38:02 -07001866 case TARGET(POP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001867 /* If oparg is 0 at the top of the stack are 1 or 6 values:
1868 Either:
1869 - TOP = NULL or an integer
1870 or:
1871 - (TOP, SECOND, THIRD) = exc_info()
1872 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
1873
1874 If oparg is 1 the value for 'return' was additionally pushed
1875 at the top of the stack.
1876 */
1877 PyObject *res = NULL;
1878 if (oparg) {
1879 res = POP();
1880 }
1881 PyObject *exc = POP();
1882 if (exc == NULL || PyLong_CheckExact(exc)) {
1883 Py_XDECREF(exc);
1884 }
1885 else {
1886 Py_DECREF(exc);
1887 Py_DECREF(POP());
1888 Py_DECREF(POP());
1889
1890 PyObject *type, *value, *traceback;
1891 _PyErr_StackItem *exc_info;
1892 PyTryBlock *b = PyFrame_BlockPop(f);
1893 if (b->b_type != EXCEPT_HANDLER) {
1894 PyErr_SetString(PyExc_SystemError,
1895 "popped block is not an except handler");
1896 Py_XDECREF(res);
1897 goto error;
1898 }
1899 assert(STACK_LEVEL() == (b)->b_level + 3);
1900 exc_info = tstate->exc_info;
1901 type = exc_info->exc_type;
1902 value = exc_info->exc_value;
1903 traceback = exc_info->exc_traceback;
1904 exc_info->exc_type = POP();
1905 exc_info->exc_value = POP();
1906 exc_info->exc_traceback = POP();
1907 Py_XDECREF(type);
1908 Py_XDECREF(value);
1909 Py_XDECREF(traceback);
1910 }
1911 if (oparg) {
1912 PUSH(res);
1913 }
1914 DISPATCH();
1915 }
1916
Benjamin Petersonddd19492018-09-16 22:38:02 -07001917 case TARGET(CALL_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001918 PyObject *ret = PyLong_FromLong(INSTR_OFFSET());
1919 if (ret == NULL) {
1920 goto error;
1921 }
1922 PUSH(ret);
1923 JUMPBY(oparg);
1924 FAST_DISPATCH();
1925 }
1926
Benjamin Petersonddd19492018-09-16 22:38:02 -07001927 case TARGET(BEGIN_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001928 /* Push NULL onto the stack for using it in END_FINALLY,
1929 POP_FINALLY, WITH_CLEANUP_START and WITH_CLEANUP_FINISH.
1930 */
1931 PUSH(NULL);
1932 FAST_DISPATCH();
1933 }
1934
Benjamin Petersonddd19492018-09-16 22:38:02 -07001935 case TARGET(END_FINALLY): {
1936 PREDICTED(END_FINALLY);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001937 /* At the top of the stack are 1 or 6 values:
1938 Either:
1939 - TOP = NULL or an integer
1940 or:
1941 - (TOP, SECOND, THIRD) = exc_info()
1942 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
1943 */
1944 PyObject *exc = POP();
1945 if (exc == NULL) {
1946 FAST_DISPATCH();
1947 }
1948 else if (PyLong_CheckExact(exc)) {
1949 int ret = _PyLong_AsInt(exc);
1950 Py_DECREF(exc);
1951 if (ret == -1 && PyErr_Occurred()) {
1952 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001953 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001954 JUMPTO(ret);
1955 FAST_DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001956 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001957 else {
1958 assert(PyExceptionClass_Check(exc));
1959 PyObject *val = POP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001960 PyObject *tb = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001961 PyErr_Restore(exc, val, tb);
1962 goto exception_unwind;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001963 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001964 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001965
Benjamin Petersonddd19492018-09-16 22:38:02 -07001966 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02001967 PyObject *exc = POP();
1968 assert(PyExceptionClass_Check(exc));
1969 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
1970 PyTryBlock *b = PyFrame_BlockPop(f);
1971 assert(b->b_type == EXCEPT_HANDLER);
1972 Py_DECREF(exc);
1973 UNWIND_EXCEPT_HANDLER(b);
1974 Py_DECREF(POP());
1975 JUMPBY(oparg);
1976 FAST_DISPATCH();
1977 }
1978 else {
1979 PyObject *val = POP();
1980 PyObject *tb = POP();
1981 PyErr_Restore(exc, val, tb);
1982 goto exception_unwind;
1983 }
1984 }
1985
Benjamin Petersonddd19492018-09-16 22:38:02 -07001986 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02001987 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02001988
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001989 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02001990 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001991 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
1992 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02001993 PyErr_SetString(PyExc_NameError,
1994 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001995 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02001996 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001997 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02001998 }
1999 else {
2000 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2001 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002002 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002003 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2004 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002005 if (PyErr_ExceptionMatches(PyExc_KeyError))
2006 PyErr_SetString(PyExc_NameError,
2007 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002008 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002009 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002010 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002011 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002012 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002013 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002014
Benjamin Petersonddd19492018-09-16 22:38:02 -07002015 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002016 PyObject *name = GETITEM(names, oparg);
2017 PyObject *v = POP();
2018 PyObject *ns = f->f_locals;
2019 int err;
2020 if (ns == NULL) {
2021 PyErr_Format(PyExc_SystemError,
2022 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002023 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002024 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002026 if (PyDict_CheckExact(ns))
2027 err = PyDict_SetItem(ns, name, v);
2028 else
2029 err = PyObject_SetItem(ns, name, v);
2030 Py_DECREF(v);
2031 if (err != 0)
2032 goto error;
2033 DISPATCH();
2034 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002035
Benjamin Petersonddd19492018-09-16 22:38:02 -07002036 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002037 PyObject *name = GETITEM(names, oparg);
2038 PyObject *ns = f->f_locals;
2039 int err;
2040 if (ns == NULL) {
2041 PyErr_Format(PyExc_SystemError,
2042 "no locals when deleting %R", name);
2043 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002044 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002045 err = PyObject_DelItem(ns, name);
2046 if (err != 0) {
2047 format_exc_check_arg(PyExc_NameError,
2048 NAME_ERROR_MSG,
2049 name);
2050 goto error;
2051 }
2052 DISPATCH();
2053 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002054
Benjamin Petersonddd19492018-09-16 22:38:02 -07002055 case TARGET(UNPACK_SEQUENCE): {
2056 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002057 PyObject *seq = POP(), *item, **items;
2058 if (PyTuple_CheckExact(seq) &&
2059 PyTuple_GET_SIZE(seq) == oparg) {
2060 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002061 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002062 item = items[oparg];
2063 Py_INCREF(item);
2064 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002065 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002066 } else if (PyList_CheckExact(seq) &&
2067 PyList_GET_SIZE(seq) == oparg) {
2068 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002069 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002070 item = items[oparg];
2071 Py_INCREF(item);
2072 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002073 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002074 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002075 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002076 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002077 } else {
2078 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002079 Py_DECREF(seq);
2080 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002081 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002082 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002083 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002084 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002085
Benjamin Petersonddd19492018-09-16 22:38:02 -07002086 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002087 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2088 PyObject *seq = POP();
2089
2090 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2091 stack_pointer + totalargs)) {
2092 stack_pointer += totalargs;
2093 } else {
2094 Py_DECREF(seq);
2095 goto error;
2096 }
2097 Py_DECREF(seq);
2098 DISPATCH();
2099 }
2100
Benjamin Petersonddd19492018-09-16 22:38:02 -07002101 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002102 PyObject *name = GETITEM(names, oparg);
2103 PyObject *owner = TOP();
2104 PyObject *v = SECOND();
2105 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002106 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002107 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002108 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002109 Py_DECREF(owner);
2110 if (err != 0)
2111 goto error;
2112 DISPATCH();
2113 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002114
Benjamin Petersonddd19492018-09-16 22:38:02 -07002115 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002116 PyObject *name = GETITEM(names, oparg);
2117 PyObject *owner = POP();
2118 int err;
2119 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2120 Py_DECREF(owner);
2121 if (err != 0)
2122 goto error;
2123 DISPATCH();
2124 }
2125
Benjamin Petersonddd19492018-09-16 22:38:02 -07002126 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002127 PyObject *name = GETITEM(names, oparg);
2128 PyObject *v = POP();
2129 int err;
2130 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002131 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002132 if (err != 0)
2133 goto error;
2134 DISPATCH();
2135 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002136
Benjamin Petersonddd19492018-09-16 22:38:02 -07002137 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002138 PyObject *name = GETITEM(names, oparg);
2139 int err;
2140 err = PyDict_DelItem(f->f_globals, name);
2141 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002142 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002143 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002144 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002145 }
2146 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002147 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002148
Benjamin Petersonddd19492018-09-16 22:38:02 -07002149 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002150 PyObject *name = GETITEM(names, oparg);
2151 PyObject *locals = f->f_locals;
2152 PyObject *v;
2153 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002154 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002155 "no locals when loading %R", name);
2156 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002157 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002158 if (PyDict_CheckExact(locals)) {
2159 v = PyDict_GetItem(locals, name);
2160 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002161 }
2162 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002163 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002164 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002165 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2166 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002167 PyErr_Clear();
2168 }
2169 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002170 if (v == NULL) {
2171 v = PyDict_GetItem(f->f_globals, name);
2172 Py_XINCREF(v);
2173 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002174 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002175 v = PyDict_GetItem(f->f_builtins, name);
2176 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002177 format_exc_check_arg(
2178 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002179 NAME_ERROR_MSG, name);
2180 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002181 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002182 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002183 }
2184 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002185 v = PyObject_GetItem(f->f_builtins, name);
2186 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002187 if (PyErr_ExceptionMatches(PyExc_KeyError))
2188 format_exc_check_arg(
2189 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002190 NAME_ERROR_MSG, name);
2191 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002192 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002193 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002194 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002195 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002196 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002197 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002198 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002199
Benjamin Petersonddd19492018-09-16 22:38:02 -07002200 case TARGET(LOAD_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002201 PyObject *name = GETITEM(names, oparg);
2202 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002203 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002204 && PyDict_CheckExact(f->f_builtins))
2205 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002206 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002207 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002208 name);
2209 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002210 if (!_PyErr_OCCURRED()) {
2211 /* _PyDict_LoadGlobal() returns NULL without raising
2212 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002213 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002214 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002215 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002216 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002217 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002218 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002219 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002220 else {
2221 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002222
2223 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002224 v = PyObject_GetItem(f->f_globals, name);
2225 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002226 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2227 goto error;
2228 PyErr_Clear();
2229
Victor Stinnerb4efc962015-11-20 09:24:02 +01002230 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002231 v = PyObject_GetItem(f->f_builtins, name);
2232 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002233 if (PyErr_ExceptionMatches(PyExc_KeyError))
2234 format_exc_check_arg(
2235 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002236 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002237 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002238 }
2239 }
2240 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002241 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002242 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002243 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002244
Benjamin Petersonddd19492018-09-16 22:38:02 -07002245 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002246 PyObject *v = GETLOCAL(oparg);
2247 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002248 SETLOCAL(oparg, NULL);
2249 DISPATCH();
2250 }
2251 format_exc_check_arg(
2252 PyExc_UnboundLocalError,
2253 UNBOUNDLOCAL_ERROR_MSG,
2254 PyTuple_GetItem(co->co_varnames, oparg)
2255 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002256 goto error;
2257 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002258
Benjamin Petersonddd19492018-09-16 22:38:02 -07002259 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002260 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002261 PyObject *oldobj = PyCell_GET(cell);
2262 if (oldobj != NULL) {
2263 PyCell_SET(cell, NULL);
2264 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002265 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002266 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002267 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002268 goto error;
2269 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002270
Benjamin Petersonddd19492018-09-16 22:38:02 -07002271 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002272 PyObject *cell = freevars[oparg];
2273 Py_INCREF(cell);
2274 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002275 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002276 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002277
Benjamin Petersonddd19492018-09-16 22:38:02 -07002278 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002279 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002280 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002281 assert(locals);
2282 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2283 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2284 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2285 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2286 if (PyDict_CheckExact(locals)) {
2287 value = PyDict_GetItem(locals, name);
2288 Py_XINCREF(value);
2289 }
2290 else {
2291 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002292 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002293 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2294 goto error;
2295 PyErr_Clear();
2296 }
2297 }
2298 if (!value) {
2299 PyObject *cell = freevars[oparg];
2300 value = PyCell_GET(cell);
2301 if (value == NULL) {
2302 format_exc_unbound(co, oparg);
2303 goto error;
2304 }
2305 Py_INCREF(value);
2306 }
2307 PUSH(value);
2308 DISPATCH();
2309 }
2310
Benjamin Petersonddd19492018-09-16 22:38:02 -07002311 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002312 PyObject *cell = freevars[oparg];
2313 PyObject *value = PyCell_GET(cell);
2314 if (value == NULL) {
2315 format_exc_unbound(co, oparg);
2316 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002317 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002318 Py_INCREF(value);
2319 PUSH(value);
2320 DISPATCH();
2321 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002322
Benjamin Petersonddd19492018-09-16 22:38:02 -07002323 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002324 PyObject *v = POP();
2325 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002326 PyObject *oldobj = PyCell_GET(cell);
2327 PyCell_SET(cell, v);
2328 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002329 DISPATCH();
2330 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002331
Benjamin Petersonddd19492018-09-16 22:38:02 -07002332 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002333 PyObject *str;
2334 PyObject *empty = PyUnicode_New(0, 0);
2335 if (empty == NULL) {
2336 goto error;
2337 }
2338 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2339 Py_DECREF(empty);
2340 if (str == NULL)
2341 goto error;
2342 while (--oparg >= 0) {
2343 PyObject *item = POP();
2344 Py_DECREF(item);
2345 }
2346 PUSH(str);
2347 DISPATCH();
2348 }
2349
Benjamin Petersonddd19492018-09-16 22:38:02 -07002350 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002351 PyObject *tup = PyTuple_New(oparg);
2352 if (tup == NULL)
2353 goto error;
2354 while (--oparg >= 0) {
2355 PyObject *item = POP();
2356 PyTuple_SET_ITEM(tup, oparg, item);
2357 }
2358 PUSH(tup);
2359 DISPATCH();
2360 }
2361
Benjamin Petersonddd19492018-09-16 22:38:02 -07002362 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002363 PyObject *list = PyList_New(oparg);
2364 if (list == NULL)
2365 goto error;
2366 while (--oparg >= 0) {
2367 PyObject *item = POP();
2368 PyList_SET_ITEM(list, oparg, item);
2369 }
2370 PUSH(list);
2371 DISPATCH();
2372 }
2373
Benjamin Petersonddd19492018-09-16 22:38:02 -07002374 case TARGET(BUILD_TUPLE_UNPACK_WITH_CALL):
2375 case TARGET(BUILD_TUPLE_UNPACK):
2376 case TARGET(BUILD_LIST_UNPACK): {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002377 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002378 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002379 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002380 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002381
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002382 if (sum == NULL)
2383 goto error;
2384
2385 for (i = oparg; i > 0; i--) {
2386 PyObject *none_val;
2387
2388 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2389 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002390 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002391 PyErr_ExceptionMatches(PyExc_TypeError))
2392 {
2393 check_args_iterable(PEEK(1 + oparg), PEEK(i));
Serhiy Storchaka73442852016-10-02 10:33:46 +03002394 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002395 Py_DECREF(sum);
2396 goto error;
2397 }
2398 Py_DECREF(none_val);
2399 }
2400
2401 if (convert_to_tuple) {
2402 return_value = PyList_AsTuple(sum);
2403 Py_DECREF(sum);
2404 if (return_value == NULL)
2405 goto error;
2406 }
2407 else {
2408 return_value = sum;
2409 }
2410
2411 while (oparg--)
2412 Py_DECREF(POP());
2413 PUSH(return_value);
2414 DISPATCH();
2415 }
2416
Benjamin Petersonddd19492018-09-16 22:38:02 -07002417 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002418 PyObject *set = PySet_New(NULL);
2419 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002420 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002421 if (set == NULL)
2422 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002423 for (i = oparg; i > 0; i--) {
2424 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002425 if (err == 0)
2426 err = PySet_Add(set, item);
2427 Py_DECREF(item);
2428 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002429 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002430 if (err != 0) {
2431 Py_DECREF(set);
2432 goto error;
2433 }
2434 PUSH(set);
2435 DISPATCH();
2436 }
2437
Benjamin Petersonddd19492018-09-16 22:38:02 -07002438 case TARGET(BUILD_SET_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002439 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002440 PyObject *sum = PySet_New(NULL);
2441 if (sum == NULL)
2442 goto error;
2443
2444 for (i = oparg; i > 0; i--) {
2445 if (_PySet_Update(sum, PEEK(i)) < 0) {
2446 Py_DECREF(sum);
2447 goto error;
2448 }
2449 }
2450
2451 while (oparg--)
2452 Py_DECREF(POP());
2453 PUSH(sum);
2454 DISPATCH();
2455 }
2456
Benjamin Petersonddd19492018-09-16 22:38:02 -07002457 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002458 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002459 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2460 if (map == NULL)
2461 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002462 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002463 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002464 PyObject *key = PEEK(2*i);
2465 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002466 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002467 if (err != 0) {
2468 Py_DECREF(map);
2469 goto error;
2470 }
2471 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002472
2473 while (oparg--) {
2474 Py_DECREF(POP());
2475 Py_DECREF(POP());
2476 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002477 PUSH(map);
2478 DISPATCH();
2479 }
2480
Benjamin Petersonddd19492018-09-16 22:38:02 -07002481 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002482 _Py_IDENTIFIER(__annotations__);
2483 int err;
2484 PyObject *ann_dict;
2485 if (f->f_locals == NULL) {
2486 PyErr_Format(PyExc_SystemError,
2487 "no locals found when setting up annotations");
2488 goto error;
2489 }
2490 /* check if __annotations__ in locals()... */
2491 if (PyDict_CheckExact(f->f_locals)) {
2492 ann_dict = _PyDict_GetItemId(f->f_locals,
2493 &PyId___annotations__);
2494 if (ann_dict == NULL) {
2495 /* ...if not, create a new one */
2496 ann_dict = PyDict_New();
2497 if (ann_dict == NULL) {
2498 goto error;
2499 }
2500 err = _PyDict_SetItemId(f->f_locals,
2501 &PyId___annotations__, ann_dict);
2502 Py_DECREF(ann_dict);
2503 if (err != 0) {
2504 goto error;
2505 }
2506 }
2507 }
2508 else {
2509 /* do the same if locals() is not a dict */
2510 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2511 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002512 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002513 }
2514 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2515 if (ann_dict == NULL) {
2516 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2517 goto error;
2518 }
2519 PyErr_Clear();
2520 ann_dict = PyDict_New();
2521 if (ann_dict == NULL) {
2522 goto error;
2523 }
2524 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2525 Py_DECREF(ann_dict);
2526 if (err != 0) {
2527 goto error;
2528 }
2529 }
2530 else {
2531 Py_DECREF(ann_dict);
2532 }
2533 }
2534 DISPATCH();
2535 }
2536
Benjamin Petersonddd19492018-09-16 22:38:02 -07002537 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002538 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002539 PyObject *map;
2540 PyObject *keys = TOP();
2541 if (!PyTuple_CheckExact(keys) ||
2542 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2543 PyErr_SetString(PyExc_SystemError,
2544 "bad BUILD_CONST_KEY_MAP keys argument");
2545 goto error;
2546 }
2547 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2548 if (map == NULL) {
2549 goto error;
2550 }
2551 for (i = oparg; i > 0; i--) {
2552 int err;
2553 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2554 PyObject *value = PEEK(i + 1);
2555 err = PyDict_SetItem(map, key, value);
2556 if (err != 0) {
2557 Py_DECREF(map);
2558 goto error;
2559 }
2560 }
2561
2562 Py_DECREF(POP());
2563 while (oparg--) {
2564 Py_DECREF(POP());
2565 }
2566 PUSH(map);
2567 DISPATCH();
2568 }
2569
Benjamin Petersonddd19492018-09-16 22:38:02 -07002570 case TARGET(BUILD_MAP_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002571 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002572 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002573 if (sum == NULL)
2574 goto error;
2575
2576 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002577 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002578 if (PyDict_Update(sum, arg) < 0) {
2579 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2580 PyErr_Format(PyExc_TypeError,
Berker Peksag8e9045d2016-10-02 13:08:25 +03002581 "'%.200s' object is not a mapping",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002582 arg->ob_type->tp_name);
2583 }
2584 Py_DECREF(sum);
2585 goto error;
2586 }
2587 }
2588
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002589 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002590 Py_DECREF(POP());
2591 PUSH(sum);
2592 DISPATCH();
2593 }
2594
Benjamin Petersonddd19492018-09-16 22:38:02 -07002595 case TARGET(BUILD_MAP_UNPACK_WITH_CALL): {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002596 Py_ssize_t i;
2597 PyObject *sum = PyDict_New();
2598 if (sum == NULL)
2599 goto error;
2600
2601 for (i = oparg; i > 0; i--) {
2602 PyObject *arg = PEEK(i);
2603 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
2604 PyObject *func = PEEK(2 + oparg);
2605 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002606 format_kwargs_mapping_error(func, arg);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002607 }
2608 else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
2609 PyObject *exc, *val, *tb;
2610 PyErr_Fetch(&exc, &val, &tb);
2611 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
2612 PyObject *key = PyTuple_GET_ITEM(val, 0);
2613 if (!PyUnicode_Check(key)) {
2614 PyErr_Format(PyExc_TypeError,
2615 "%.200s%.200s keywords must be strings",
2616 PyEval_GetFuncName(func),
2617 PyEval_GetFuncDesc(func));
2618 } else {
2619 PyErr_Format(PyExc_TypeError,
2620 "%.200s%.200s got multiple "
2621 "values for keyword argument '%U'",
2622 PyEval_GetFuncName(func),
2623 PyEval_GetFuncDesc(func),
2624 key);
2625 }
2626 Py_XDECREF(exc);
2627 Py_XDECREF(val);
2628 Py_XDECREF(tb);
2629 }
2630 else {
2631 PyErr_Restore(exc, val, tb);
2632 }
2633 }
2634 Py_DECREF(sum);
2635 goto error;
2636 }
2637 }
2638
2639 while (oparg--)
2640 Py_DECREF(POP());
2641 PUSH(sum);
2642 DISPATCH();
2643 }
2644
Benjamin Petersonddd19492018-09-16 22:38:02 -07002645 case TARGET(MAP_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002646 PyObject *key = TOP();
2647 PyObject *value = SECOND();
2648 PyObject *map;
2649 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002650 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002651 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002652 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002653 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002654 Py_DECREF(value);
2655 Py_DECREF(key);
2656 if (err != 0)
2657 goto error;
2658 PREDICT(JUMP_ABSOLUTE);
2659 DISPATCH();
2660 }
2661
Benjamin Petersonddd19492018-09-16 22:38:02 -07002662 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002663 PyObject *name = GETITEM(names, oparg);
2664 PyObject *owner = TOP();
2665 PyObject *res = PyObject_GetAttr(owner, name);
2666 Py_DECREF(owner);
2667 SET_TOP(res);
2668 if (res == NULL)
2669 goto error;
2670 DISPATCH();
2671 }
2672
Benjamin Petersonddd19492018-09-16 22:38:02 -07002673 case TARGET(COMPARE_OP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002674 PyObject *right = POP();
2675 PyObject *left = TOP();
2676 PyObject *res = cmp_outcome(oparg, left, right);
2677 Py_DECREF(left);
2678 Py_DECREF(right);
2679 SET_TOP(res);
2680 if (res == NULL)
2681 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002682 PREDICT(POP_JUMP_IF_FALSE);
2683 PREDICT(POP_JUMP_IF_TRUE);
2684 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002685 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002686
Benjamin Petersonddd19492018-09-16 22:38:02 -07002687 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002688 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002689 PyObject *fromlist = POP();
2690 PyObject *level = TOP();
2691 PyObject *res;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002692 res = import_name(f, name, fromlist, level);
2693 Py_DECREF(level);
2694 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002695 SET_TOP(res);
2696 if (res == NULL)
2697 goto error;
2698 DISPATCH();
2699 }
2700
Benjamin Petersonddd19492018-09-16 22:38:02 -07002701 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002702 PyObject *from = POP(), *locals;
2703 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002704 if (PyFrame_FastToLocalsWithError(f) < 0) {
2705 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002706 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002707 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002708
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002709 locals = f->f_locals;
2710 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002711 PyErr_SetString(PyExc_SystemError,
2712 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002713 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002714 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002715 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002716 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002717 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002718 Py_DECREF(from);
2719 if (err != 0)
2720 goto error;
2721 DISPATCH();
2722 }
Guido van Rossum25831651993-05-19 14:50:45 +00002723
Benjamin Petersonddd19492018-09-16 22:38:02 -07002724 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002725 PyObject *name = GETITEM(names, oparg);
2726 PyObject *from = TOP();
2727 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002728 res = import_from(from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002729 PUSH(res);
2730 if (res == NULL)
2731 goto error;
2732 DISPATCH();
2733 }
Thomas Wouters52152252000-08-17 22:55:00 +00002734
Benjamin Petersonddd19492018-09-16 22:38:02 -07002735 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002736 JUMPBY(oparg);
2737 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002738 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002739
Benjamin Petersonddd19492018-09-16 22:38:02 -07002740 case TARGET(POP_JUMP_IF_FALSE): {
2741 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002742 PyObject *cond = POP();
2743 int err;
2744 if (cond == Py_True) {
2745 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002746 FAST_DISPATCH();
2747 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002748 if (cond == Py_False) {
2749 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002750 JUMPTO(oparg);
2751 FAST_DISPATCH();
2752 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002753 err = PyObject_IsTrue(cond);
2754 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002755 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07002756 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002757 else if (err == 0)
2758 JUMPTO(oparg);
2759 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002760 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002761 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002762 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002763
Benjamin Petersonddd19492018-09-16 22:38:02 -07002764 case TARGET(POP_JUMP_IF_TRUE): {
2765 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002766 PyObject *cond = POP();
2767 int err;
2768 if (cond == Py_False) {
2769 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002770 FAST_DISPATCH();
2771 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002772 if (cond == Py_True) {
2773 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002774 JUMPTO(oparg);
2775 FAST_DISPATCH();
2776 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002777 err = PyObject_IsTrue(cond);
2778 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002779 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002780 JUMPTO(oparg);
2781 }
2782 else if (err == 0)
2783 ;
2784 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002785 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002786 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002787 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002788
Benjamin Petersonddd19492018-09-16 22:38:02 -07002789 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002790 PyObject *cond = TOP();
2791 int err;
2792 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002793 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002794 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002795 FAST_DISPATCH();
2796 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002797 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002798 JUMPTO(oparg);
2799 FAST_DISPATCH();
2800 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002801 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002802 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002803 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002804 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002805 }
2806 else if (err == 0)
2807 JUMPTO(oparg);
2808 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002809 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002810 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002811 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002812
Benjamin Petersonddd19492018-09-16 22:38:02 -07002813 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002814 PyObject *cond = TOP();
2815 int err;
2816 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002817 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002818 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002819 FAST_DISPATCH();
2820 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002821 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002822 JUMPTO(oparg);
2823 FAST_DISPATCH();
2824 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002825 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002826 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002827 JUMPTO(oparg);
2828 }
2829 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002830 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002831 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002832 }
2833 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002834 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002835 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002836 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002837
Benjamin Petersonddd19492018-09-16 22:38:02 -07002838 case TARGET(JUMP_ABSOLUTE): {
2839 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002840 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002841#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002842 /* Enabling this path speeds-up all while and for-loops by bypassing
2843 the per-loop checks for signals. By default, this should be turned-off
2844 because it prevents detection of a control-break in tight loops like
2845 "while 1: pass". Compile with this option turned-on when you need
2846 the speed-up and do not need break checking inside tight loops (ones
2847 that contain only instructions ending with FAST_DISPATCH).
2848 */
2849 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002850#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002851 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002852#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002853 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002854
Benjamin Petersonddd19492018-09-16 22:38:02 -07002855 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002856 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002857 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002858 PyObject *iter = PyObject_GetIter(iterable);
2859 Py_DECREF(iterable);
2860 SET_TOP(iter);
2861 if (iter == NULL)
2862 goto error;
2863 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002864 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04002865 DISPATCH();
2866 }
2867
Benjamin Petersonddd19492018-09-16 22:38:02 -07002868 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04002869 /* before: [obj]; after [getiter(obj)] */
2870 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04002871 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04002872 if (PyCoro_CheckExact(iterable)) {
2873 /* `iterable` is a coroutine */
2874 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
2875 /* and it is used in a 'yield from' expression of a
2876 regular generator. */
2877 Py_DECREF(iterable);
2878 SET_TOP(NULL);
2879 PyErr_SetString(PyExc_TypeError,
2880 "cannot 'yield from' a coroutine object "
2881 "in a non-coroutine generator");
2882 goto error;
2883 }
2884 }
2885 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04002886 /* `iterable` is not a generator. */
2887 iter = PyObject_GetIter(iterable);
2888 Py_DECREF(iterable);
2889 SET_TOP(iter);
2890 if (iter == NULL)
2891 goto error;
2892 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002893 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002894 DISPATCH();
2895 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002896
Benjamin Petersonddd19492018-09-16 22:38:02 -07002897 case TARGET(FOR_ITER): {
2898 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002899 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002900 PyObject *iter = TOP();
2901 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
2902 if (next != NULL) {
2903 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002904 PREDICT(STORE_FAST);
2905 PREDICT(UNPACK_SEQUENCE);
2906 DISPATCH();
2907 }
2908 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002909 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2910 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08002911 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002912 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002913 PyErr_Clear();
2914 }
2915 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00002916 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002917 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002918 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002919 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002920 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002921 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002922
Benjamin Petersonddd19492018-09-16 22:38:02 -07002923 case TARGET(SETUP_FINALLY): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002924 /* NOTE: If you add any new block-setup opcodes that
2925 are not try/except/finally handlers, you may need
2926 to update the PyGen_NeedsFinalizing() function.
2927 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002928
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002929 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002930 STACK_LEVEL());
2931 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002932 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002933
Benjamin Petersonddd19492018-09-16 22:38:02 -07002934 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04002935 _Py_IDENTIFIER(__aexit__);
2936 _Py_IDENTIFIER(__aenter__);
2937
2938 PyObject *mgr = TOP();
2939 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
2940 *enter;
2941 PyObject *res;
2942 if (exit == NULL)
2943 goto error;
2944 SET_TOP(exit);
2945 enter = special_lookup(mgr, &PyId___aenter__);
2946 Py_DECREF(mgr);
2947 if (enter == NULL)
2948 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01002949 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04002950 Py_DECREF(enter);
2951 if (res == NULL)
2952 goto error;
2953 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002954 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002955 DISPATCH();
2956 }
2957
Benjamin Petersonddd19492018-09-16 22:38:02 -07002958 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04002959 PyObject *res = POP();
2960 /* Setup the finally block before pushing the result
2961 of __aenter__ on the stack. */
2962 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2963 STACK_LEVEL());
2964 PUSH(res);
2965 DISPATCH();
2966 }
2967
Benjamin Petersonddd19492018-09-16 22:38:02 -07002968 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05002969 _Py_IDENTIFIER(__exit__);
2970 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002971 PyObject *mgr = TOP();
Raymond Hettingera3fec152016-11-21 17:24:23 -08002972 PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002973 PyObject *res;
Raymond Hettingera3fec152016-11-21 17:24:23 -08002974 if (enter == NULL)
2975 goto error;
2976 exit = special_lookup(mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08002977 if (exit == NULL) {
2978 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002979 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08002980 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002981 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002982 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01002983 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002984 Py_DECREF(enter);
2985 if (res == NULL)
2986 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002987 /* Setup the finally block before pushing the result
2988 of __enter__ on the stack. */
2989 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2990 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002991
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002992 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002993 DISPATCH();
2994 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002995
Benjamin Petersonddd19492018-09-16 22:38:02 -07002996 case TARGET(WITH_CLEANUP_START): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002997 /* At the top of the stack are 1 or 6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002998 how/why we entered the finally clause:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002999 - TOP = NULL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003000 - (TOP, SECOND, THIRD) = exc_info()
3001 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003002 Below them is EXIT, the context.__exit__ or context.__aexit__
3003 bound method.
3004 In the first case, we must call
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003005 EXIT(None, None, None)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003006 otherwise we must call
3007 EXIT(TOP, SECOND, THIRD)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003008
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003009 In the first case, we remove EXIT from the
3010 stack, leaving TOP, and push TOP on the stack.
3011 Otherwise we shift the bottom 3 values of the
3012 stack down, replace the empty spot with NULL, and push
3013 None on the stack.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003014
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003015 Finally we push the result of the call.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003016 */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003017 PyObject *stack[3];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003018 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003019 PyObject *exc, *val, *tb, *res;
3020
3021 val = tb = Py_None;
3022 exc = TOP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003023 if (exc == NULL) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003024 STACK_SHRINK(1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003025 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003026 SET_TOP(exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003027 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003028 }
3029 else {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003030 assert(PyExceptionClass_Check(exc));
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003031 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003032 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003033 val = SECOND();
3034 tb = THIRD();
3035 tp2 = FOURTH();
3036 exc2 = PEEK(5);
3037 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003038 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003039 SET_VALUE(7, tb2);
3040 SET_VALUE(6, exc2);
3041 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003042 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3043 SET_FOURTH(NULL);
3044 /* We just shifted the stack down, so we have
3045 to tell the except handler block that the
3046 values are lower than it expects. */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003047 assert(f->f_iblock > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003048 block = &f->f_blockstack[f->f_iblock - 1];
3049 assert(block->b_type == EXCEPT_HANDLER);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003050 assert(block->b_level > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003051 block->b_level--;
3052 }
Victor Stinner842cfff2016-12-01 14:45:31 +01003053
3054 stack[0] = exc;
3055 stack[1] = val;
3056 stack[2] = tb;
3057 res = _PyObject_FastCall(exit_func, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003058 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003059 if (res == NULL)
3060 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003061
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003062 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003063 PUSH(exc);
3064 PUSH(res);
3065 PREDICT(WITH_CLEANUP_FINISH);
3066 DISPATCH();
3067 }
3068
Benjamin Petersonddd19492018-09-16 22:38:02 -07003069 case TARGET(WITH_CLEANUP_FINISH): {
3070 PREDICTED(WITH_CLEANUP_FINISH);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003071 /* TOP = the result of calling the context.__exit__ bound method
3072 SECOND = either None or exception type
3073
3074 If SECOND is None below is NULL or the return address,
3075 otherwise below are 7 values representing an exception.
3076 */
Yury Selivanov75445082015-05-11 22:57:16 -04003077 PyObject *res = POP();
3078 PyObject *exc = POP();
3079 int err;
3080
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003081 if (exc != Py_None)
3082 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003083 else
3084 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003085
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003086 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003087 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003088
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003089 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003090 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003091 else if (err > 0) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003092 /* There was an exception and a True return.
3093 * We must manually unwind the EXCEPT_HANDLER block
3094 * which was created when the exception was caught,
Quan Tian3bd0d622018-10-20 05:30:03 +08003095 * otherwise the stack will be in an inconsistent state.
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003096 */
3097 PyTryBlock *b = PyFrame_BlockPop(f);
3098 assert(b->b_type == EXCEPT_HANDLER);
3099 UNWIND_EXCEPT_HANDLER(b);
3100 PUSH(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003101 }
3102 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003103 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003104 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003105
Benjamin Petersonddd19492018-09-16 22:38:02 -07003106 case TARGET(LOAD_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003107 /* Designed to work in tamdem with CALL_METHOD. */
3108 PyObject *name = GETITEM(names, oparg);
3109 PyObject *obj = TOP();
3110 PyObject *meth = NULL;
3111
3112 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3113
Yury Selivanovf2392132016-12-13 19:03:51 -05003114 if (meth == NULL) {
3115 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003116 goto error;
3117 }
3118
3119 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003120 /* We can bypass temporary bound method object.
3121 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003122
INADA Naoki015bce62017-01-16 17:23:30 +09003123 meth | self | arg1 | ... | argN
3124 */
3125 SET_TOP(meth);
3126 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003127 }
3128 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003129 /* meth is not an unbound method (but a regular attr, or
3130 something was returned by a descriptor protocol). Set
3131 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003132 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003133
3134 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003135 */
INADA Naoki015bce62017-01-16 17:23:30 +09003136 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003137 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003138 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003139 }
3140 DISPATCH();
3141 }
3142
Benjamin Petersonddd19492018-09-16 22:38:02 -07003143 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003144 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003145 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003146
3147 sp = stack_pointer;
3148
INADA Naoki015bce62017-01-16 17:23:30 +09003149 meth = PEEK(oparg + 2);
3150 if (meth == NULL) {
3151 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3152 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003153
3154 Stack layout:
3155
INADA Naoki015bce62017-01-16 17:23:30 +09003156 ... | NULL | callable | arg1 | ... | argN
3157 ^- TOP()
3158 ^- (-oparg)
3159 ^- (-oparg-1)
3160 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003161
Ville Skyttä49b27342017-08-03 09:00:59 +03003162 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003163 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003164 */
Yury Selivanovf2392132016-12-13 19:03:51 -05003165 res = call_function(&sp, oparg, NULL);
3166 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003167 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003168 }
3169 else {
3170 /* This is a method call. Stack layout:
3171
INADA Naoki015bce62017-01-16 17:23:30 +09003172 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003173 ^- TOP()
3174 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003175 ^- (-oparg-1)
3176 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003177
INADA Naoki015bce62017-01-16 17:23:30 +09003178 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003179 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003180 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003181 */
3182 res = call_function(&sp, oparg + 1, NULL);
3183 stack_pointer = sp;
3184 }
3185
3186 PUSH(res);
3187 if (res == NULL)
3188 goto error;
3189 DISPATCH();
3190 }
3191
Benjamin Petersonddd19492018-09-16 22:38:02 -07003192 case TARGET(CALL_FUNCTION): {
3193 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003194 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003195 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003196 res = call_function(&sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003197 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003198 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003199 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003200 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003201 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003202 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003203 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003204
Benjamin Petersonddd19492018-09-16 22:38:02 -07003205 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003206 PyObject **sp, *res, *names;
3207
3208 names = POP();
3209 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003210 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003211 res = call_function(&sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003212 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003213 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003214 Py_DECREF(names);
3215
3216 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003217 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003218 }
3219 DISPATCH();
3220 }
3221
Benjamin Petersonddd19492018-09-16 22:38:02 -07003222 case TARGET(CALL_FUNCTION_EX): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003223 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003224 if (oparg & 0x01) {
3225 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003226 if (!PyDict_CheckExact(kwargs)) {
3227 PyObject *d = PyDict_New();
3228 if (d == NULL)
3229 goto error;
3230 if (PyDict_Update(d, kwargs) != 0) {
3231 Py_DECREF(d);
3232 /* PyDict_Update raises attribute
3233 * error (percolated from an attempt
3234 * to get 'keys' attribute) instead of
3235 * a type error if its second argument
3236 * is not a mapping.
3237 */
3238 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003239 format_kwargs_mapping_error(SECOND(), kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003240 }
Victor Stinnereece2222016-09-12 11:16:37 +02003241 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003242 goto error;
3243 }
3244 Py_DECREF(kwargs);
3245 kwargs = d;
3246 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003247 assert(PyDict_CheckExact(kwargs));
3248 }
3249 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003250 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003251 if (!PyTuple_CheckExact(callargs)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003252 if (check_args_iterable(func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003253 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003254 goto error;
3255 }
3256 Py_SETREF(callargs, PySequence_Tuple(callargs));
3257 if (callargs == NULL) {
3258 goto error;
3259 }
3260 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003261 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003262
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003263 result = do_call_core(func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003264 Py_DECREF(func);
3265 Py_DECREF(callargs);
3266 Py_XDECREF(kwargs);
3267
3268 SET_TOP(result);
3269 if (result == NULL) {
3270 goto error;
3271 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003272 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003273 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003274
Benjamin Petersonddd19492018-09-16 22:38:02 -07003275 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003276 PyObject *qualname = POP();
3277 PyObject *codeobj = POP();
3278 PyFunctionObject *func = (PyFunctionObject *)
3279 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003280
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003281 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003282 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003283 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003284 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003285 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003286
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003287 if (oparg & 0x08) {
3288 assert(PyTuple_CheckExact(TOP()));
3289 func ->func_closure = POP();
3290 }
3291 if (oparg & 0x04) {
3292 assert(PyDict_CheckExact(TOP()));
3293 func->func_annotations = POP();
3294 }
3295 if (oparg & 0x02) {
3296 assert(PyDict_CheckExact(TOP()));
3297 func->func_kwdefaults = POP();
3298 }
3299 if (oparg & 0x01) {
3300 assert(PyTuple_CheckExact(TOP()));
3301 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003302 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003303
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003304 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003305 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003306 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003307
Benjamin Petersonddd19492018-09-16 22:38:02 -07003308 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003309 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003310 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003311 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003312 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003313 step = NULL;
3314 stop = POP();
3315 start = TOP();
3316 slice = PySlice_New(start, stop, step);
3317 Py_DECREF(start);
3318 Py_DECREF(stop);
3319 Py_XDECREF(step);
3320 SET_TOP(slice);
3321 if (slice == NULL)
3322 goto error;
3323 DISPATCH();
3324 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003325
Benjamin Petersonddd19492018-09-16 22:38:02 -07003326 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003327 /* Handles f-string value formatting. */
3328 PyObject *result;
3329 PyObject *fmt_spec;
3330 PyObject *value;
3331 PyObject *(*conv_fn)(PyObject *);
3332 int which_conversion = oparg & FVC_MASK;
3333 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3334
3335 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003336 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003337
3338 /* See if any conversion is specified. */
3339 switch (which_conversion) {
3340 case FVC_STR: conv_fn = PyObject_Str; break;
3341 case FVC_REPR: conv_fn = PyObject_Repr; break;
3342 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3343
3344 /* Must be 0 (meaning no conversion), since only four
3345 values are allowed by (oparg & FVC_MASK). */
3346 default: conv_fn = NULL; break;
3347 }
3348
3349 /* If there's a conversion function, call it and replace
3350 value with that result. Otherwise, just use value,
3351 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003352 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003353 result = conv_fn(value);
3354 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003355 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003356 Py_XDECREF(fmt_spec);
3357 goto error;
3358 }
3359 value = result;
3360 }
3361
3362 /* If value is a unicode object, and there's no fmt_spec,
3363 then we know the result of format(value) is value
3364 itself. In that case, skip calling format(). I plan to
3365 move this optimization in to PyObject_Format()
3366 itself. */
3367 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3368 /* Do nothing, just transfer ownership to result. */
3369 result = value;
3370 } else {
3371 /* Actually call format(). */
3372 result = PyObject_Format(value, fmt_spec);
3373 Py_DECREF(value);
3374 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003375 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003376 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003377 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003378 }
3379
Eric V. Smith135d5f42016-02-05 18:23:08 -05003380 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003381 DISPATCH();
3382 }
3383
Benjamin Petersonddd19492018-09-16 22:38:02 -07003384 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003385 int oldoparg = oparg;
3386 NEXTOPARG();
3387 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003388 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003389 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003390
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003391
Antoine Pitrou042b1282010-08-13 21:15:58 +00003392#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003393 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003394#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003395 default:
3396 fprintf(stderr,
3397 "XXX lineno: %d, opcode: %d\n",
3398 PyFrame_GetLineNumber(f),
3399 opcode);
3400 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003401 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003403 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003404
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003405 /* This should never be reached. Every opcode should end with DISPATCH()
3406 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003407 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003408
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003409error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003410 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003411#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003412 if (!PyErr_Occurred())
3413 PyErr_SetString(PyExc_SystemError,
3414 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003415#else
3416 assert(PyErr_Occurred());
3417#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003418
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003419 /* Log traceback info. */
3420 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003421
Benjamin Peterson51f46162013-01-23 08:38:47 -05003422 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003423 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3424 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003425
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003426exception_unwind:
3427 /* Unwind stacks if an exception occurred */
3428 while (f->f_iblock > 0) {
3429 /* Pop the current block. */
3430 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003432 if (b->b_type == EXCEPT_HANDLER) {
3433 UNWIND_EXCEPT_HANDLER(b);
3434 continue;
3435 }
3436 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003437 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003438 PyObject *exc, *val, *tb;
3439 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003440 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003441 /* Beware, this invalidates all b->b_* fields */
3442 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003443 PUSH(exc_info->exc_traceback);
3444 PUSH(exc_info->exc_value);
3445 if (exc_info->exc_type != NULL) {
3446 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003447 }
3448 else {
3449 Py_INCREF(Py_None);
3450 PUSH(Py_None);
3451 }
3452 PyErr_Fetch(&exc, &val, &tb);
3453 /* Make the raw exception data
3454 available to the handler,
3455 so a program can emulate the
3456 Python main loop. */
3457 PyErr_NormalizeException(
3458 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003459 if (tb != NULL)
3460 PyException_SetTraceback(val, tb);
3461 else
3462 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003463 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003464 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003465 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003466 exc_info->exc_value = val;
3467 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003468 if (tb == NULL)
3469 tb = Py_None;
3470 Py_INCREF(tb);
3471 PUSH(tb);
3472 PUSH(val);
3473 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003474 JUMPTO(handler);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003475 /* Resume normal execution */
3476 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003477 }
3478 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003479
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003480 /* End the loop as we still have an error */
3481 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003482 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003484 /* Pop remaining stack entries. */
3485 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003486 PyObject *o = POP();
3487 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003488 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003489
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003490 assert(retval == NULL);
3491 assert(PyErr_Occurred());
Guido van Rossumac7be682001-01-17 15:42:30 +00003492
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003493return_or_yield:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003494 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003495 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003496 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3497 tstate, f, PyTrace_RETURN, retval)) {
3498 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003499 }
3500 }
3501 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003502 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3503 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003504 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003505 }
3506 }
3507 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003509 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003510exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003511 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3512 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003513 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003514 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003515 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003516
Victor Stinnerefde1462015-03-21 15:04:43 +01003517 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003518}
3519
Benjamin Petersonb204a422011-06-05 22:04:07 -05003520static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003521format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3522{
3523 int err;
3524 Py_ssize_t len = PyList_GET_SIZE(names);
3525 PyObject *name_str, *comma, *tail, *tmp;
3526
3527 assert(PyList_CheckExact(names));
3528 assert(len >= 1);
3529 /* Deal with the joys of natural language. */
3530 switch (len) {
3531 case 1:
3532 name_str = PyList_GET_ITEM(names, 0);
3533 Py_INCREF(name_str);
3534 break;
3535 case 2:
3536 name_str = PyUnicode_FromFormat("%U and %U",
3537 PyList_GET_ITEM(names, len - 2),
3538 PyList_GET_ITEM(names, len - 1));
3539 break;
3540 default:
3541 tail = PyUnicode_FromFormat(", %U, and %U",
3542 PyList_GET_ITEM(names, len - 2),
3543 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003544 if (tail == NULL)
3545 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003546 /* Chop off the last two objects in the list. This shouldn't actually
3547 fail, but we can't be too careful. */
3548 err = PyList_SetSlice(names, len - 2, len, NULL);
3549 if (err == -1) {
3550 Py_DECREF(tail);
3551 return;
3552 }
3553 /* Stitch everything up into a nice comma-separated list. */
3554 comma = PyUnicode_FromString(", ");
3555 if (comma == NULL) {
3556 Py_DECREF(tail);
3557 return;
3558 }
3559 tmp = PyUnicode_Join(comma, names);
3560 Py_DECREF(comma);
3561 if (tmp == NULL) {
3562 Py_DECREF(tail);
3563 return;
3564 }
3565 name_str = PyUnicode_Concat(tmp, tail);
3566 Py_DECREF(tmp);
3567 Py_DECREF(tail);
3568 break;
3569 }
3570 if (name_str == NULL)
3571 return;
3572 PyErr_Format(PyExc_TypeError,
3573 "%U() missing %i required %s argument%s: %U",
3574 co->co_name,
3575 len,
3576 kind,
3577 len == 1 ? "" : "s",
3578 name_str);
3579 Py_DECREF(name_str);
3580}
3581
3582static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003583missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003584 PyObject **fastlocals)
3585{
Victor Stinner74319ae2016-08-25 00:04:09 +02003586 Py_ssize_t i, j = 0;
3587 Py_ssize_t start, end;
3588 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003589 const char *kind = positional ? "positional" : "keyword-only";
3590 PyObject *missing_names;
3591
3592 /* Compute the names of the arguments that are missing. */
3593 missing_names = PyList_New(missing);
3594 if (missing_names == NULL)
3595 return;
3596 if (positional) {
3597 start = 0;
3598 end = co->co_argcount - defcount;
3599 }
3600 else {
3601 start = co->co_argcount;
3602 end = start + co->co_kwonlyargcount;
3603 }
3604 for (i = start; i < end; i++) {
3605 if (GETLOCAL(i) == NULL) {
3606 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3607 PyObject *name = PyObject_Repr(raw);
3608 if (name == NULL) {
3609 Py_DECREF(missing_names);
3610 return;
3611 }
3612 PyList_SET_ITEM(missing_names, j++, name);
3613 }
3614 }
3615 assert(j == missing);
3616 format_missing(kind, co, missing_names);
3617 Py_DECREF(missing_names);
3618}
3619
3620static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003621too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3622 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003623{
3624 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003625 Py_ssize_t kwonly_given = 0;
3626 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003627 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003628 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003629
Benjamin Petersone109c702011-06-24 09:37:26 -05003630 assert((co->co_flags & CO_VARARGS) == 0);
3631 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003632 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3633 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003634 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003635 }
3636 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003637 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003638 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003639 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003640 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003641 }
3642 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003643 plural = (co_argcount != 1);
3644 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003645 }
3646 if (sig == NULL)
3647 return;
3648 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003649 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3650 kwonly_sig = PyUnicode_FromFormat(format,
3651 given != 1 ? "s" : "",
3652 kwonly_given,
3653 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003654 if (kwonly_sig == NULL) {
3655 Py_DECREF(sig);
3656 return;
3657 }
3658 }
3659 else {
3660 /* This will not fail. */
3661 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003662 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003663 }
3664 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003665 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003666 co->co_name,
3667 sig,
3668 plural ? "s" : "",
3669 given,
3670 kwonly_sig,
3671 given == 1 && !kwonly_given ? "was" : "were");
3672 Py_DECREF(sig);
3673 Py_DECREF(kwonly_sig);
3674}
3675
Guido van Rossumc2e20742006-02-27 22:32:47 +00003676/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02003677 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003678 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003679
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003680PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +02003681_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003682 PyObject *const *args, Py_ssize_t argcount,
3683 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003684 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003685 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003686 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003687 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003688{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003689 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003690 PyFrameObject *f;
3691 PyObject *retval = NULL;
3692 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003693 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003694 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003695 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3696 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003697 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003699 if (globals == NULL) {
3700 PyErr_SetString(PyExc_SystemError,
3701 "PyEval_EvalCodeEx: NULL globals");
3702 return NULL;
3703 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003704
Victor Stinnerc7020012016-08-16 23:40:29 +02003705 /* Create the frame */
3706 tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003707 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003708 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003709 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003710 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003711 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003712 fastlocals = f->f_localsplus;
3713 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003714
Victor Stinnerc7020012016-08-16 23:40:29 +02003715 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003716 if (co->co_flags & CO_VARKEYWORDS) {
3717 kwdict = PyDict_New();
3718 if (kwdict == NULL)
3719 goto fail;
3720 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003721 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003722 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003723 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003724 SETLOCAL(i, kwdict);
3725 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003726 else {
3727 kwdict = NULL;
3728 }
3729
3730 /* Copy positional arguments into local variables */
3731 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003732 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003733 }
3734 else {
3735 n = argcount;
3736 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003737 for (i = 0; i < n; i++) {
3738 x = args[i];
3739 Py_INCREF(x);
3740 SETLOCAL(i, x);
3741 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003742
3743 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003744 if (co->co_flags & CO_VARARGS) {
3745 u = PyTuple_New(argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003746 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003747 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003748 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003749 SETLOCAL(total_args, u);
3750 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003751 x = args[i];
3752 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003753 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003754 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003755 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003756
Serhiy Storchakab7281052016-09-12 00:52:40 +03003757 /* Handle keyword arguments passed as two strided arrays */
3758 kwcount *= kwstep;
3759 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003760 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003761 PyObject *keyword = kwnames[i];
3762 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003763 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003764
Benjamin Petersonb204a422011-06-05 22:04:07 -05003765 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3766 PyErr_Format(PyExc_TypeError,
3767 "%U() keywords must be strings",
3768 co->co_name);
3769 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003770 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003771
Benjamin Petersonb204a422011-06-05 22:04:07 -05003772 /* Speed hack: do raw pointer compares. As names are
3773 normally interned this should almost always hit. */
3774 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3775 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003776 PyObject *name = co_varnames[j];
3777 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003778 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003779 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003780 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003781
Benjamin Petersonb204a422011-06-05 22:04:07 -05003782 /* Slow fallback, just in case */
3783 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003784 PyObject *name = co_varnames[j];
3785 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
3786 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003787 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003788 }
3789 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003790 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003791 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003792 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003793
Victor Stinner231d1f32017-01-11 02:12:06 +01003794 assert(j >= total_args);
3795 if (kwdict == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003796 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003797 "%U() got an unexpected keyword argument '%S'",
3798 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003799 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003800 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003801
Christian Heimes0bd447f2013-07-20 14:48:10 +02003802 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3803 goto fail;
3804 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003805 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02003806
Benjamin Petersonb204a422011-06-05 22:04:07 -05003807 kw_found:
3808 if (GETLOCAL(j) != NULL) {
3809 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003810 "%U() got multiple values for argument '%S'",
3811 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003812 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003813 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003814 Py_INCREF(value);
3815 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003816 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003817
3818 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003819 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003820 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003821 goto fail;
3822 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003823
3824 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003825 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02003826 Py_ssize_t m = co->co_argcount - defcount;
3827 Py_ssize_t missing = 0;
3828 for (i = argcount; i < m; i++) {
3829 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003830 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02003831 }
3832 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003833 if (missing) {
3834 missing_arguments(co, missing, defcount, fastlocals);
3835 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003836 }
3837 if (n > m)
3838 i = n - m;
3839 else
3840 i = 0;
3841 for (; i < defcount; i++) {
3842 if (GETLOCAL(m+i) == NULL) {
3843 PyObject *def = defs[i];
3844 Py_INCREF(def);
3845 SETLOCAL(m+i, def);
3846 }
3847 }
3848 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003849
3850 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003851 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02003852 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003853 for (i = co->co_argcount; i < total_args; i++) {
3854 PyObject *name;
3855 if (GETLOCAL(i) != NULL)
3856 continue;
3857 name = PyTuple_GET_ITEM(co->co_varnames, i);
3858 if (kwdefs != NULL) {
3859 PyObject *def = PyDict_GetItem(kwdefs, name);
3860 if (def) {
3861 Py_INCREF(def);
3862 SETLOCAL(i, def);
3863 continue;
3864 }
3865 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003866 missing++;
3867 }
3868 if (missing) {
3869 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003870 goto fail;
3871 }
3872 }
3873
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003874 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003875 vars into frame. */
3876 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003877 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02003878 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05003879 /* Possibly account for the cell variable being an argument. */
3880 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07003881 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05003882 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05003883 /* Clear the local copy. */
3884 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003885 }
3886 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05003887 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003888 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05003889 if (c == NULL)
3890 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05003891 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003892 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003893
3894 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05003895 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3896 PyObject *o = PyTuple_GET_ITEM(closure, i);
3897 Py_INCREF(o);
3898 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003899 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003900
Yury Selivanoveb636452016-09-08 22:01:51 -07003901 /* Handle generator/coroutine/asynchronous generator */
3902 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003903 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04003904 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003905 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04003906
3907 if (is_coro && tstate->in_coroutine_wrapper) {
3908 assert(coro_wrapper != NULL);
3909 PyErr_Format(PyExc_RuntimeError,
3910 "coroutine wrapper %.200R attempted "
3911 "to recursively wrap %.200R",
3912 coro_wrapper,
3913 co);
3914 goto fail;
3915 }
Yury Selivanov75445082015-05-11 22:57:16 -04003916
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003917 /* Don't need to keep the reference to f_back, it will be set
3918 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003919 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003921 /* Create a new generator that owns the ready to run frame
3922 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04003923 if (is_coro) {
3924 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07003925 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
3926 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003927 } else {
3928 gen = PyGen_NewWithQualName(f, name, qualname);
3929 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003930 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003931 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003932 }
INADA Naoki9c157762016-12-26 18:52:46 +09003933
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003934 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04003935
Yury Selivanov94c22632015-06-04 10:16:51 -04003936 if (is_coro && coro_wrapper != NULL) {
3937 PyObject *wrapped;
3938 tstate->in_coroutine_wrapper = 1;
3939 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
3940 tstate->in_coroutine_wrapper = 0;
3941 return wrapped;
3942 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04003943
Yury Selivanov75445082015-05-11 22:57:16 -04003944 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003945 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003946
Victor Stinner59a73272016-12-09 18:51:13 +01003947 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003948
Thomas Woutersce272b62007-09-19 21:19:28 +00003949fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003951 /* decref'ing the frame can cause __del__ methods to get invoked,
3952 which can call back into Python. While we're done with the
3953 current Python frame (f), the associated C stack is still in use,
3954 so recursion_depth must be boosted for the duration.
3955 */
3956 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003957 if (Py_REFCNT(f) > 1) {
3958 Py_DECREF(f);
3959 _PyObject_GC_TRACK(f);
3960 }
3961 else {
3962 ++tstate->recursion_depth;
3963 Py_DECREF(f);
3964 --tstate->recursion_depth;
3965 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003966 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003967}
3968
Victor Stinner40ee3012014-06-16 15:59:28 +02003969PyObject *
3970PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003971 PyObject *const *args, int argcount,
3972 PyObject *const *kws, int kwcount,
3973 PyObject *const *defs, int defcount,
3974 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02003975{
3976 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02003977 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06003978 kws, kws != NULL ? kws + 1 : NULL,
3979 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02003980 defs, defcount,
3981 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003982 NULL, NULL);
3983}
Tim Peters5ca576e2001-06-18 22:08:13 +00003984
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003985static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05003986special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003987{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003988 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05003989 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003990 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003991 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003992 return NULL;
3993 }
3994 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003995}
3996
3997
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003998/* Logic for the raise statement (too complicated for inlining).
3999 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004000static int
Collin Winter828f04a2007-08-31 00:04:24 +00004001do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004002{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004003 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004004
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004005 if (exc == NULL) {
4006 /* Reraise */
4007 PyThreadState *tstate = PyThreadState_GET();
Mark Shannonae3087c2017-10-22 22:41:51 +01004008 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004009 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004010 type = exc_info->exc_type;
4011 value = exc_info->exc_value;
4012 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004013 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004014 PyErr_SetString(PyExc_RuntimeError,
4015 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004016 return 0;
4017 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004018 Py_XINCREF(type);
4019 Py_XINCREF(value);
4020 Py_XINCREF(tb);
4021 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004022 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004023 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004024
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004025 /* We support the following forms of raise:
4026 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004027 raise <instance>
4028 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004029
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004030 if (PyExceptionClass_Check(exc)) {
4031 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004032 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004033 if (value == NULL)
4034 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004035 if (!PyExceptionInstance_Check(value)) {
4036 PyErr_Format(PyExc_TypeError,
4037 "calling %R should have returned an instance of "
4038 "BaseException, not %R",
4039 type, Py_TYPE(value));
4040 goto raise_error;
4041 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004042 }
4043 else if (PyExceptionInstance_Check(exc)) {
4044 value = exc;
4045 type = PyExceptionInstance_Class(exc);
4046 Py_INCREF(type);
4047 }
4048 else {
4049 /* Not something you can raise. You get an exception
4050 anyway, just not what you specified :-) */
4051 Py_DECREF(exc);
4052 PyErr_SetString(PyExc_TypeError,
4053 "exceptions must derive from BaseException");
4054 goto raise_error;
4055 }
Collin Winter828f04a2007-08-31 00:04:24 +00004056
Serhiy Storchakac0191582016-09-27 11:37:10 +03004057 assert(type != NULL);
4058 assert(value != NULL);
4059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004060 if (cause) {
4061 PyObject *fixed_cause;
4062 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004063 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004064 if (fixed_cause == NULL)
4065 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004066 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004067 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004068 else if (PyExceptionInstance_Check(cause)) {
4069 fixed_cause = cause;
4070 }
4071 else if (cause == Py_None) {
4072 Py_DECREF(cause);
4073 fixed_cause = NULL;
4074 }
4075 else {
4076 PyErr_SetString(PyExc_TypeError,
4077 "exception causes must derive from "
4078 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004079 goto raise_error;
4080 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004081 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004082 }
Collin Winter828f04a2007-08-31 00:04:24 +00004083
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004084 PyErr_SetObject(type, value);
4085 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004086 Py_DECREF(value);
4087 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004088 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004089
4090raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004091 Py_XDECREF(value);
4092 Py_XDECREF(type);
4093 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004094 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004095}
4096
Tim Petersd6d010b2001-06-21 02:49:55 +00004097/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004098 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004099
Guido van Rossum0368b722007-05-11 16:50:42 +00004100 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4101 with a variable target.
4102*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004103
Barry Warsawe42b18f1997-08-25 22:13:04 +00004104static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004105unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004106{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004107 int i = 0, j = 0;
4108 Py_ssize_t ll = 0;
4109 PyObject *it; /* iter(v) */
4110 PyObject *w;
4111 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004112
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004113 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004115 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004116 if (it == NULL) {
4117 if (PyErr_ExceptionMatches(PyExc_TypeError) &&
4118 v->ob_type->tp_iter == NULL && !PySequence_Check(v))
4119 {
4120 PyErr_Format(PyExc_TypeError,
4121 "cannot unpack non-iterable %.200s object",
4122 v->ob_type->tp_name);
4123 }
4124 return 0;
4125 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004127 for (; i < argcnt; i++) {
4128 w = PyIter_Next(it);
4129 if (w == NULL) {
4130 /* Iterator done, via error or exhaustion. */
4131 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004132 if (argcntafter == -1) {
4133 PyErr_Format(PyExc_ValueError,
4134 "not enough values to unpack (expected %d, got %d)",
4135 argcnt, i);
4136 }
4137 else {
4138 PyErr_Format(PyExc_ValueError,
4139 "not enough values to unpack "
4140 "(expected at least %d, got %d)",
4141 argcnt + argcntafter, i);
4142 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004143 }
4144 goto Error;
4145 }
4146 *--sp = w;
4147 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004149 if (argcntafter == -1) {
4150 /* We better have exhausted the iterator now. */
4151 w = PyIter_Next(it);
4152 if (w == NULL) {
4153 if (PyErr_Occurred())
4154 goto Error;
4155 Py_DECREF(it);
4156 return 1;
4157 }
4158 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004159 PyErr_Format(PyExc_ValueError,
4160 "too many values to unpack (expected %d)",
4161 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004162 goto Error;
4163 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004165 l = PySequence_List(it);
4166 if (l == NULL)
4167 goto Error;
4168 *--sp = l;
4169 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004171 ll = PyList_GET_SIZE(l);
4172 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004173 PyErr_Format(PyExc_ValueError,
4174 "not enough values to unpack (expected at least %d, got %zd)",
4175 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004176 goto Error;
4177 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004178
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004179 /* Pop the "after-variable" args off the list. */
4180 for (j = argcntafter; j > 0; j--, i++) {
4181 *--sp = PyList_GET_ITEM(l, ll - j);
4182 }
4183 /* Resize the list. */
4184 Py_SIZE(l) = ll - argcntafter;
4185 Py_DECREF(it);
4186 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004187
Tim Petersd6d010b2001-06-21 02:49:55 +00004188Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004189 for (; i > 0; i--, sp++)
4190 Py_DECREF(*sp);
4191 Py_XDECREF(it);
4192 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004193}
4194
4195
Guido van Rossum96a42c81992-01-12 02:29:51 +00004196#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004197static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004198prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004199{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004200 printf("%s ", str);
4201 if (PyObject_Print(v, stdout, 0) != 0)
4202 PyErr_Clear(); /* Don't know what else to do */
4203 printf("\n");
4204 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004205}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004206#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004207
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004208static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004209call_exc_trace(Py_tracefunc func, PyObject *self,
4210 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004211{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004212 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004213 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004214 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004215 if (value == NULL) {
4216 value = Py_None;
4217 Py_INCREF(value);
4218 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004219 PyErr_NormalizeException(&type, &value, &orig_traceback);
4220 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004221 arg = PyTuple_Pack(3, type, value, traceback);
4222 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004223 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004224 return;
4225 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004226 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004227 Py_DECREF(arg);
4228 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004229 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004230 else {
4231 Py_XDECREF(type);
4232 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004233 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004234 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004235}
4236
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004237static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004238call_trace_protected(Py_tracefunc func, PyObject *obj,
4239 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004240 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004241{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004242 PyObject *type, *value, *traceback;
4243 int err;
4244 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004245 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004246 if (err == 0)
4247 {
4248 PyErr_Restore(type, value, traceback);
4249 return 0;
4250 }
4251 else {
4252 Py_XDECREF(type);
4253 Py_XDECREF(value);
4254 Py_XDECREF(traceback);
4255 return -1;
4256 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004257}
4258
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004259static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004260call_trace(Py_tracefunc func, PyObject *obj,
4261 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004262 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004263{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004264 int result;
4265 if (tstate->tracing)
4266 return 0;
4267 tstate->tracing++;
4268 tstate->use_tracing = 0;
4269 result = func(obj, frame, what, arg);
4270 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4271 || (tstate->c_profilefunc != NULL));
4272 tstate->tracing--;
4273 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004274}
4275
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004276PyObject *
4277_PyEval_CallTracing(PyObject *func, PyObject *args)
4278{
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004279 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004280 int save_tracing = tstate->tracing;
4281 int save_use_tracing = tstate->use_tracing;
4282 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004284 tstate->tracing = 0;
4285 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4286 || (tstate->c_profilefunc != NULL));
4287 result = PyObject_Call(func, args, NULL);
4288 tstate->tracing = save_tracing;
4289 tstate->use_tracing = save_use_tracing;
4290 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004291}
4292
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004293/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004294static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004295maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004296 PyThreadState *tstate, PyFrameObject *frame,
4297 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004298{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004299 int result = 0;
4300 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004302 /* If the last instruction executed isn't in the current
4303 instruction window, reset the window.
4304 */
4305 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4306 PyAddrPair bounds;
4307 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4308 &bounds);
4309 *instr_lb = bounds.ap_lower;
4310 *instr_ub = bounds.ap_upper;
4311 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004312 /* If the last instruction falls at the start of a line or if it
4313 represents a jump backwards, update the frame's line number and
4314 then call the trace function if we're tracing source lines.
4315 */
4316 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004317 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004318 if (frame->f_trace_lines) {
4319 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4320 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004321 }
George King20faa682017-10-18 17:44:22 -07004322 /* Always emit an opcode event if we're tracing all opcodes. */
4323 if (frame->f_trace_opcodes) {
4324 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4325 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004326 *instr_prev = frame->f_lasti;
4327 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004328}
4329
Fred Drake5755ce62001-06-27 19:19:46 +00004330void
4331PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004332{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004333 PyThreadState *tstate = PyThreadState_GET();
4334 PyObject *temp = tstate->c_profileobj;
4335 Py_XINCREF(arg);
4336 tstate->c_profilefunc = NULL;
4337 tstate->c_profileobj = NULL;
4338 /* Must make sure that tracing is not ignored if 'temp' is freed */
4339 tstate->use_tracing = tstate->c_tracefunc != NULL;
4340 Py_XDECREF(temp);
4341 tstate->c_profilefunc = func;
4342 tstate->c_profileobj = arg;
4343 /* Flag that tracing or profiling is turned on */
4344 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004345}
4346
4347void
4348PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4349{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004350 PyThreadState *tstate = PyThreadState_GET();
4351 PyObject *temp = tstate->c_traceobj;
4352 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4353 Py_XINCREF(arg);
4354 tstate->c_tracefunc = NULL;
4355 tstate->c_traceobj = NULL;
4356 /* Must make sure that profiling is not ignored if 'temp' is freed */
4357 tstate->use_tracing = tstate->c_profilefunc != NULL;
4358 Py_XDECREF(temp);
4359 tstate->c_tracefunc = func;
4360 tstate->c_traceobj = arg;
4361 /* Flag that tracing or profiling is turned on */
4362 tstate->use_tracing = ((func != NULL)
4363 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004364}
4365
Yury Selivanov75445082015-05-11 22:57:16 -04004366void
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004367_PyEval_SetCoroutineOriginTrackingDepth(int new_depth)
4368{
4369 assert(new_depth >= 0);
4370 PyThreadState *tstate = PyThreadState_GET();
4371 tstate->coroutine_origin_tracking_depth = new_depth;
4372}
4373
4374int
4375_PyEval_GetCoroutineOriginTrackingDepth(void)
4376{
4377 PyThreadState *tstate = PyThreadState_GET();
4378 return tstate->coroutine_origin_tracking_depth;
4379}
4380
4381void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004382_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004383{
4384 PyThreadState *tstate = PyThreadState_GET();
4385
Yury Selivanov75445082015-05-11 22:57:16 -04004386 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004387 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004388}
4389
4390PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004391_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004392{
4393 PyThreadState *tstate = PyThreadState_GET();
4394 return tstate->coroutine_wrapper;
4395}
4396
Yury Selivanoveb636452016-09-08 22:01:51 -07004397void
4398_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4399{
4400 PyThreadState *tstate = PyThreadState_GET();
4401
4402 Py_XINCREF(firstiter);
4403 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4404}
4405
4406PyObject *
4407_PyEval_GetAsyncGenFirstiter(void)
4408{
4409 PyThreadState *tstate = PyThreadState_GET();
4410 return tstate->async_gen_firstiter;
4411}
4412
4413void
4414_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4415{
4416 PyThreadState *tstate = PyThreadState_GET();
4417
4418 Py_XINCREF(finalizer);
4419 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4420}
4421
4422PyObject *
4423_PyEval_GetAsyncGenFinalizer(void)
4424{
4425 PyThreadState *tstate = PyThreadState_GET();
4426 return tstate->async_gen_finalizer;
4427}
4428
Guido van Rossumb209a111997-04-29 18:18:01 +00004429PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004430PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004431{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004432 PyFrameObject *current_frame = PyEval_GetFrame();
4433 if (current_frame == NULL)
Victor Stinnercaba55b2018-08-03 15:33:52 +02004434 return _PyInterpreterState_GET_UNSAFE()->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004435 else
4436 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004437}
4438
Guido van Rossumb209a111997-04-29 18:18:01 +00004439PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004440PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004441{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004442 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004443 if (current_frame == NULL) {
4444 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004445 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004446 }
4447
4448 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4449 return NULL;
4450
4451 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004452 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004453}
4454
Guido van Rossumb209a111997-04-29 18:18:01 +00004455PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004456PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004457{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004458 PyFrameObject *current_frame = PyEval_GetFrame();
4459 if (current_frame == NULL)
4460 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004461
4462 assert(current_frame->f_globals != NULL);
4463 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004464}
4465
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004466PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004467PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004468{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004469 PyThreadState *tstate = PyThreadState_GET();
4470 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004471}
4472
Guido van Rossum6135a871995-01-09 17:53:26 +00004473int
Tim Peters5ba58662001-07-16 02:29:45 +00004474PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004475{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004476 PyFrameObject *current_frame = PyEval_GetFrame();
4477 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004478
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004479 if (current_frame != NULL) {
4480 const int codeflags = current_frame->f_code->co_flags;
4481 const int compilerflags = codeflags & PyCF_MASK;
4482 if (compilerflags) {
4483 result = 1;
4484 cf->cf_flags |= compilerflags;
4485 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004486#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004487 if (codeflags & CO_GENERATOR_ALLOWED) {
4488 result = 1;
4489 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4490 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004491#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004492 }
4493 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004494}
4495
Guido van Rossum3f5da241990-12-20 15:06:42 +00004496
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004497const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004498PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004499{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004500 if (PyMethod_Check(func))
4501 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4502 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004503 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004504 else if (PyCFunction_Check(func))
4505 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4506 else
4507 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004508}
4509
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004510const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004511PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004512{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004513 if (PyMethod_Check(func))
4514 return "()";
4515 else if (PyFunction_Check(func))
4516 return "()";
4517 else if (PyCFunction_Check(func))
4518 return "()";
4519 else
4520 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004521}
4522
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004523#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004524if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004525 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4526 tstate, tstate->frame, \
4527 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004528 x = NULL; \
4529 } \
4530 else { \
4531 x = call; \
4532 if (tstate->c_profilefunc != NULL) { \
4533 if (x == NULL) { \
4534 call_trace_protected(tstate->c_profilefunc, \
4535 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004536 tstate, tstate->frame, \
4537 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004538 /* XXX should pass (type, value, tb) */ \
4539 } else { \
4540 if (call_trace(tstate->c_profilefunc, \
4541 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004542 tstate, tstate->frame, \
4543 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004544 Py_DECREF(x); \
4545 x = NULL; \
4546 } \
4547 } \
4548 } \
4549 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004550} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004551 x = call; \
4552 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004553
Victor Stinner415c5102017-01-11 00:54:57 +01004554/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4555 to reduce the stack consumption. */
4556Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Benjamin Peterson4fd64b92016-09-09 14:57:58 -07004557call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004558{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004559 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004560 PyObject *func = *pfunc;
4561 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004562 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4563 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004564 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004566 /* Always dispatch PyCFunction first, because these are
4567 presumed to be the most frequent callable object.
4568 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004569 if (PyCFunction_Check(func)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004570 PyThreadState *tstate = PyThreadState_GET();
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004571 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004572 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004573 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
4574 PyThreadState *tstate = PyThreadState_GET();
jdemeyer56868f92018-07-21 10:30:59 +02004575 if (nargs > 0 && tstate->use_tracing) {
4576 /* We need to create a temporary bound method as argument
4577 for profiling.
4578
4579 If nargs == 0, then this cannot work because we have no
4580 "self". In any case, the call itself would raise
4581 TypeError (foo needs an argument), so we just skip
4582 profiling. */
4583 PyObject *self = stack[0];
4584 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
jdemeyer147d9552018-07-23 18:41:20 +02004585 if (func != NULL) {
4586 C_TRACE(x, _PyCFunction_FastCallKeywords(func,
4587 stack+1, nargs-1,
4588 kwnames));
4589 Py_DECREF(func);
INADA Naoki93fac8d2017-03-07 14:24:37 +09004590 }
jdemeyer147d9552018-07-23 18:41:20 +02004591 else {
4592 x = NULL;
4593 }
INADA Naoki93fac8d2017-03-07 14:24:37 +09004594 }
4595 else {
4596 x = _PyMethodDescr_FastCallKeywords(func, stack, nargs, kwnames);
4597 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004598 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004599 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004600 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
Victor Stinnerb69ee8c2016-11-28 18:32:31 +01004601 /* Optimize access to bound methods. Reuse the Python stack
4602 to pass 'self' as the first argument, replace 'func'
4603 with 'self'. It avoids the creation of a new temporary tuple
4604 for arguments (to replace func with self) when the method uses
4605 FASTCALL. */
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004606 PyObject *self = PyMethod_GET_SELF(func);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004607 Py_INCREF(self);
4608 func = PyMethod_GET_FUNCTION(func);
4609 Py_INCREF(func);
4610 Py_SETREF(*pfunc, self);
4611 nargs++;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004612 stack--;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004613 }
4614 else {
4615 Py_INCREF(func);
4616 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004617
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004618 if (PyFunction_Check(func)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004619 x = _PyFunction_FastCallKeywords(func, stack, nargs, kwnames);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004620 }
4621 else {
4622 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4623 }
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004624 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004625 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004626
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004627 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4628
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004629 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004630 while ((*pp_stack) > pfunc) {
4631 w = EXT_POP(*pp_stack);
4632 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004633 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004634
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004635 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004636}
4637
Jeremy Hylton52820442001-01-03 23:52:36 +00004638static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004639do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004640{
jdemeyere89de732018-09-19 12:06:20 +02004641 PyObject *result;
4642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004643 if (PyCFunction_Check(func)) {
4644 PyThreadState *tstate = PyThreadState_GET();
4645 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004646 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004647 }
jdemeyere89de732018-09-19 12:06:20 +02004648 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
4649 PyThreadState *tstate = PyThreadState_GET();
4650 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
4651 if (nargs > 0 && tstate->use_tracing) {
4652 /* We need to create a temporary bound method as argument
4653 for profiling.
4654
4655 If nargs == 0, then this cannot work because we have no
4656 "self". In any case, the call itself would raise
4657 TypeError (foo needs an argument), so we just skip
4658 profiling. */
4659 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
4660 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4661 if (func == NULL) {
4662 return NULL;
4663 }
4664
4665 C_TRACE(result, _PyCFunction_FastCallDict(func,
4666 &PyTuple_GET_ITEM(callargs, 1),
4667 nargs - 1,
4668 kwdict));
4669 Py_DECREF(func);
4670 return result;
4671 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004672 }
jdemeyere89de732018-09-19 12:06:20 +02004673 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00004674}
4675
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004676/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004677 nb_index slot defined, and store in *pi.
4678 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004679 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004680 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004681*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004682int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004683_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004684{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004685 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004686 Py_ssize_t x;
4687 if (PyIndex_Check(v)) {
4688 x = PyNumber_AsSsize_t(v, NULL);
4689 if (x == -1 && PyErr_Occurred())
4690 return 0;
4691 }
4692 else {
4693 PyErr_SetString(PyExc_TypeError,
4694 "slice indices must be integers or "
4695 "None or have an __index__ method");
4696 return 0;
4697 }
4698 *pi = x;
4699 }
4700 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004701}
4702
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004703int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004704_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004705{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004706 Py_ssize_t x;
4707 if (PyIndex_Check(v)) {
4708 x = PyNumber_AsSsize_t(v, NULL);
4709 if (x == -1 && PyErr_Occurred())
4710 return 0;
4711 }
4712 else {
4713 PyErr_SetString(PyExc_TypeError,
4714 "slice indices must be integers or "
4715 "have an __index__ method");
4716 return 0;
4717 }
4718 *pi = x;
4719 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004720}
4721
4722
Guido van Rossum486364b2007-06-30 05:01:58 +00004723#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004724 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004725
Guido van Rossumb209a111997-04-29 18:18:01 +00004726static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004727cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004728{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004729 int res = 0;
4730 switch (op) {
4731 case PyCmp_IS:
4732 res = (v == w);
4733 break;
4734 case PyCmp_IS_NOT:
4735 res = (v != w);
4736 break;
4737 case PyCmp_IN:
4738 res = PySequence_Contains(w, v);
4739 if (res < 0)
4740 return NULL;
4741 break;
4742 case PyCmp_NOT_IN:
4743 res = PySequence_Contains(w, v);
4744 if (res < 0)
4745 return NULL;
4746 res = !res;
4747 break;
4748 case PyCmp_EXC_MATCH:
4749 if (PyTuple_Check(w)) {
4750 Py_ssize_t i, length;
4751 length = PyTuple_Size(w);
4752 for (i = 0; i < length; i += 1) {
4753 PyObject *exc = PyTuple_GET_ITEM(w, i);
4754 if (!PyExceptionClass_Check(exc)) {
4755 PyErr_SetString(PyExc_TypeError,
4756 CANNOT_CATCH_MSG);
4757 return NULL;
4758 }
4759 }
4760 }
4761 else {
4762 if (!PyExceptionClass_Check(w)) {
4763 PyErr_SetString(PyExc_TypeError,
4764 CANNOT_CATCH_MSG);
4765 return NULL;
4766 }
4767 }
4768 res = PyErr_GivenExceptionMatches(v, w);
4769 break;
4770 default:
4771 return PyObject_RichCompare(v, w, op);
4772 }
4773 v = res ? Py_True : Py_False;
4774 Py_INCREF(v);
4775 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004776}
4777
Thomas Wouters52152252000-08-17 22:55:00 +00004778static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004779import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
4780{
4781 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004782 PyObject *import_func, *res;
4783 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004784
4785 import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
4786 if (import_func == NULL) {
4787 PyErr_SetString(PyExc_ImportError, "__import__ not found");
4788 return NULL;
4789 }
4790
4791 /* Fast path for not overloaded __import__. */
Victor Stinnercaba55b2018-08-03 15:33:52 +02004792 if (import_func == _PyInterpreterState_GET_UNSAFE()->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004793 int ilevel = _PyLong_AsInt(level);
4794 if (ilevel == -1 && PyErr_Occurred()) {
4795 return NULL;
4796 }
4797 res = PyImport_ImportModuleLevelObject(
4798 name,
4799 f->f_globals,
4800 f->f_locals == NULL ? Py_None : f->f_locals,
4801 fromlist,
4802 ilevel);
4803 return res;
4804 }
4805
4806 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004807
4808 stack[0] = name;
4809 stack[1] = f->f_globals;
4810 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
4811 stack[3] = fromlist;
4812 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02004813 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004814 Py_DECREF(import_func);
4815 return res;
4816}
4817
4818static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004819import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004820{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004821 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02004822 _Py_IDENTIFIER(__name__);
Xiang Zhang4830f582017-03-21 11:13:42 +08004823 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004824
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004825 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02004826 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004827 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004828 /* Issue #17636: in case this failed because of a circular relative
4829 import, try to fallback on reading the module directly from
4830 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02004831 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07004832 if (pkgname == NULL) {
4833 goto error;
4834 }
Oren Milman6db70332017-09-19 14:23:01 +03004835 if (!PyUnicode_Check(pkgname)) {
4836 Py_CLEAR(pkgname);
4837 goto error;
4838 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004839 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07004840 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08004841 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004842 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07004843 }
Eric Snow3f9eee62017-09-15 16:35:20 -06004844 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004845 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07004846 if (x == NULL) {
4847 goto error;
4848 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004849 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004850 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07004851 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004852 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004853 if (pkgname == NULL) {
4854 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
4855 if (pkgname_or_unknown == NULL) {
4856 Py_XDECREF(pkgpath);
4857 return NULL;
4858 }
4859 } else {
4860 pkgname_or_unknown = pkgname;
4861 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004862
4863 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
4864 PyErr_Clear();
Xiang Zhang4830f582017-03-21 11:13:42 +08004865 errmsg = PyUnicode_FromFormat(
4866 "cannot import name %R from %R (unknown location)",
4867 name, pkgname_or_unknown
4868 );
4869 /* NULL check for errmsg done by PyErr_SetImportError. */
4870 PyErr_SetImportError(errmsg, pkgname, NULL);
4871 }
4872 else {
4873 errmsg = PyUnicode_FromFormat(
4874 "cannot import name %R from %R (%S)",
4875 name, pkgname_or_unknown, pkgpath
4876 );
4877 /* NULL check for errmsg done by PyErr_SetImportError. */
4878 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004879 }
4880
Xiang Zhang4830f582017-03-21 11:13:42 +08004881 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004882 Py_XDECREF(pkgname_or_unknown);
4883 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07004884 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00004885}
Guido van Rossumac7be682001-01-17 15:42:30 +00004886
Thomas Wouters52152252000-08-17 22:55:00 +00004887static int
4888import_all_from(PyObject *locals, PyObject *v)
4889{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004890 _Py_IDENTIFIER(__all__);
4891 _Py_IDENTIFIER(__dict__);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08004892 _Py_IDENTIFIER(__name__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004893 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004894 int skip_leading_underscores = 0;
4895 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004896
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004897 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
4898 return -1; /* Unexpected error */
4899 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004900 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004901 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
4902 return -1;
4903 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004904 if (dict == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004905 PyErr_SetString(PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004906 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004907 return -1;
4908 }
4909 all = PyMapping_Keys(dict);
4910 Py_DECREF(dict);
4911 if (all == NULL)
4912 return -1;
4913 skip_leading_underscores = 1;
4914 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004915
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004916 for (pos = 0, err = 0; ; pos++) {
4917 name = PySequence_GetItem(all, pos);
4918 if (name == NULL) {
4919 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4920 err = -1;
4921 else
4922 PyErr_Clear();
4923 break;
4924 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08004925 if (!PyUnicode_Check(name)) {
4926 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
4927 if (modname == NULL) {
4928 Py_DECREF(name);
4929 err = -1;
4930 break;
4931 }
4932 if (!PyUnicode_Check(modname)) {
4933 PyErr_Format(PyExc_TypeError,
4934 "module __name__ must be a string, not %.100s",
4935 Py_TYPE(modname)->tp_name);
4936 }
4937 else {
4938 PyErr_Format(PyExc_TypeError,
4939 "%s in %U.%s must be str, not %.100s",
4940 skip_leading_underscores ? "Key" : "Item",
4941 modname,
4942 skip_leading_underscores ? "__dict__" : "__all__",
4943 Py_TYPE(name)->tp_name);
4944 }
4945 Py_DECREF(modname);
4946 Py_DECREF(name);
4947 err = -1;
4948 break;
4949 }
4950 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03004951 if (PyUnicode_READY(name) == -1) {
4952 Py_DECREF(name);
4953 err = -1;
4954 break;
4955 }
4956 if (PyUnicode_READ_CHAR(name, 0) == '_') {
4957 Py_DECREF(name);
4958 continue;
4959 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004960 }
4961 value = PyObject_GetAttr(v, name);
4962 if (value == NULL)
4963 err = -1;
4964 else if (PyDict_CheckExact(locals))
4965 err = PyDict_SetItem(locals, name, value);
4966 else
4967 err = PyObject_SetItem(locals, name, value);
4968 Py_DECREF(name);
4969 Py_XDECREF(value);
4970 if (err != 0)
4971 break;
4972 }
4973 Py_DECREF(all);
4974 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004975}
4976
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03004977static int
4978check_args_iterable(PyObject *func, PyObject *args)
4979{
4980 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
4981 PyErr_Format(PyExc_TypeError,
4982 "%.200s%.200s argument after * "
4983 "must be an iterable, not %.200s",
4984 PyEval_GetFuncName(func),
4985 PyEval_GetFuncDesc(func),
4986 args->ob_type->tp_name);
4987 return -1;
4988 }
4989 return 0;
4990}
4991
4992static void
4993format_kwargs_mapping_error(PyObject *func, PyObject *kwargs)
4994{
4995 PyErr_Format(PyExc_TypeError,
4996 "%.200s%.200s argument after ** "
4997 "must be a mapping, not %.200s",
4998 PyEval_GetFuncName(func),
4999 PyEval_GetFuncDesc(func),
5000 kwargs->ob_type->tp_name);
5001}
5002
Guido van Rossumac7be682001-01-17 15:42:30 +00005003static void
Neal Norwitzda059e32007-08-26 05:33:45 +00005004format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005005{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005006 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005007
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005008 if (!obj)
5009 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005010
Serhiy Storchaka06515832016-11-20 09:13:07 +02005011 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005012 if (!obj_str)
5013 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005014
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005015 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005016}
Guido van Rossum950361c1997-01-24 13:49:28 +00005017
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005018static void
5019format_exc_unbound(PyCodeObject *co, int oparg)
5020{
5021 PyObject *name;
5022 /* Don't stomp existing exception */
5023 if (PyErr_Occurred())
5024 return;
5025 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5026 name = PyTuple_GET_ITEM(co->co_cellvars,
5027 oparg);
5028 format_exc_check_arg(
5029 PyExc_UnboundLocalError,
5030 UNBOUNDLOCAL_ERROR_MSG,
5031 name);
5032 } else {
5033 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5034 PyTuple_GET_SIZE(co->co_cellvars));
5035 format_exc_check_arg(PyExc_NameError,
5036 UNBOUNDFREE_ERROR_MSG, name);
5037 }
5038}
5039
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005040static void
5041format_awaitable_error(PyTypeObject *type, int prevopcode)
5042{
5043 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5044 if (prevopcode == BEFORE_ASYNC_WITH) {
5045 PyErr_Format(PyExc_TypeError,
5046 "'async with' received an object from __aenter__ "
5047 "that does not implement __await__: %.100s",
5048 type->tp_name);
5049 }
5050 else if (prevopcode == WITH_CLEANUP_START) {
5051 PyErr_Format(PyExc_TypeError,
5052 "'async with' received an object from __aexit__ "
5053 "that does not implement __await__: %.100s",
5054 type->tp_name);
5055 }
5056 }
5057}
5058
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005059static PyObject *
5060unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005061 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005062{
5063 PyObject *res;
5064 if (Py_REFCNT(v) == 2) {
5065 /* In the common case, there are 2 references to the value
5066 * stored in 'variable' when the += is performed: one on the
5067 * value stack (in 'v') and one still stored in the
5068 * 'variable'. We try to delete the variable now to reduce
5069 * the refcnt to 1.
5070 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005071 int opcode, oparg;
5072 NEXTOPARG();
5073 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005074 case STORE_FAST:
5075 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005076 PyObject **fastlocals = f->f_localsplus;
5077 if (GETLOCAL(oparg) == v)
5078 SETLOCAL(oparg, NULL);
5079 break;
5080 }
5081 case STORE_DEREF:
5082 {
5083 PyObject **freevars = (f->f_localsplus +
5084 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005085 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005086 if (PyCell_GET(c) == v) {
5087 PyCell_SET(c, NULL);
5088 Py_DECREF(v);
5089 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005090 break;
5091 }
5092 case STORE_NAME:
5093 {
5094 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005095 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005096 PyObject *locals = f->f_locals;
5097 if (PyDict_CheckExact(locals) &&
5098 PyDict_GetItem(locals, name) == v) {
5099 if (PyDict_DelItem(locals, name) != 0) {
5100 PyErr_Clear();
5101 }
5102 }
5103 break;
5104 }
5105 }
5106 }
5107 res = v;
5108 PyUnicode_Append(&res, w);
5109 return res;
5110}
5111
Guido van Rossum950361c1997-01-24 13:49:28 +00005112#ifdef DYNAMIC_EXECUTION_PROFILE
5113
Skip Montanarof118cb12001-10-15 20:51:38 +00005114static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005115getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005116{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005117 int i;
5118 PyObject *l = PyList_New(256);
5119 if (l == NULL) return NULL;
5120 for (i = 0; i < 256; i++) {
5121 PyObject *x = PyLong_FromLong(a[i]);
5122 if (x == NULL) {
5123 Py_DECREF(l);
5124 return NULL;
5125 }
5126 PyList_SetItem(l, i, x);
5127 }
5128 for (i = 0; i < 256; i++)
5129 a[i] = 0;
5130 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005131}
5132
5133PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005134_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005135{
5136#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005137 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005138#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005139 int i;
5140 PyObject *l = PyList_New(257);
5141 if (l == NULL) return NULL;
5142 for (i = 0; i < 257; i++) {
5143 PyObject *x = getarray(dxpairs[i]);
5144 if (x == NULL) {
5145 Py_DECREF(l);
5146 return NULL;
5147 }
5148 PyList_SetItem(l, i, x);
5149 }
5150 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005151#endif
5152}
5153
5154#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005155
5156Py_ssize_t
5157_PyEval_RequestCodeExtraIndex(freefunc free)
5158{
Victor Stinnercaba55b2018-08-03 15:33:52 +02005159 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -07005160 Py_ssize_t new_index;
5161
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005162 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005163 return -1;
5164 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005165 new_index = interp->co_extra_user_count++;
5166 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005167 return new_index;
5168}
Łukasz Langaa785c872016-09-09 17:37:37 -07005169
5170static void
5171dtrace_function_entry(PyFrameObject *f)
5172{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005173 const char *filename;
5174 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005175 int lineno;
5176
5177 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5178 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5179 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5180
5181 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5182}
5183
5184static void
5185dtrace_function_return(PyFrameObject *f)
5186{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005187 const char *filename;
5188 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005189 int lineno;
5190
5191 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5192 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5193 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5194
5195 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5196}
5197
5198/* DTrace equivalent of maybe_call_line_trace. */
5199static void
5200maybe_dtrace_line(PyFrameObject *frame,
5201 int *instr_lb, int *instr_ub, int *instr_prev)
5202{
5203 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005204 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005205
5206 /* If the last instruction executed isn't in the current
5207 instruction window, reset the window.
5208 */
5209 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5210 PyAddrPair bounds;
5211 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5212 &bounds);
5213 *instr_lb = bounds.ap_lower;
5214 *instr_ub = bounds.ap_upper;
5215 }
5216 /* If the last instruction falls at the start of a line or if
5217 it represents a jump backwards, update the frame's line
5218 number and call the trace function. */
5219 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5220 frame->f_lineno = line;
5221 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5222 if (!co_filename)
5223 co_filename = "?";
5224 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5225 if (!co_name)
5226 co_name = "?";
5227 PyDTrace_LINE(co_filename, co_name, line);
5228 }
5229 *instr_prev = frame->f_lasti;
5230}